use crate::config::SidewinderConfig;
use crate::error::SidewinderError;
use crate::transaction::{SignedTransaction, TransactionRequest, build_signed};
use crate::types::{
NodeStatus, NodeStatusWire, OperationSchema, OperationSchemaWire, PendingTransaction,
PendingWire, PostTransactionResponseWire, SuggestedParams, SuggestedParamsWire,
};
use algo_ops::AlgoOps;
use anyhow::{Result, anyhow};
use reqwest::Method;
use rustls::ClientConfig;
use serde::de::DeserializeOwned;
use std::sync::Arc;
use std::time::Duration;
use crate::discovery::{DiscoveredNode, DiscoveryConfig, identity_key, pinned_client_config};
pub trait SidewinderOps {
fn submit(&self, signed_txn: &[u8]) -> Result<String>;
fn status(&self, txid: &str, proof: bool) -> Result<PendingTransaction>;
fn watch(&self, txid: &str, proof: bool, wait_secs: u64) -> Result<PendingTransaction>;
fn params(&self) -> Result<SuggestedParams>;
fn node_status(&self) -> Result<NodeStatus>;
fn operations(&self, typ: u32) -> Result<Option<OperationSchema>>;
fn health(&self) -> Result<bool>;
}
pub struct SidewinderClient {
algo: AlgoOps,
config: SidewinderConfig,
tls: Option<Arc<ClientConfig>>,
}
const MAX_RETRIES: u32 = 3;
const RETRY_BASE_MS: u64 = 1_000;
enum SendFailure {
IdentityRefused(String),
Transport(String),
Local(anyhow::Error),
}
impl SendFailure {
fn from_reqwest(error: reqwest::Error, mutual_tls: bool) -> Self {
let chain = cause_chain(&error);
if mutual_tls && refused_with_access_denied(&error, &chain) {
SendFailure::IdentityRefused(chain)
} else {
SendFailure::Transport(chain)
}
}
}
fn cause_chain(error: &(dyn std::error::Error + 'static)) -> String {
let mut chain = error.to_string();
let mut cause = error.source();
while let Some(inner) = cause {
let text = inner.to_string();
if !chain.contains(&text) {
chain.push_str(": ");
chain.push_str(&text);
}
cause = inner.source();
}
chain
}
fn refused_with_access_denied(error: &(dyn std::error::Error + 'static), chain: &str) -> bool {
fn is_access_denied(error: &(dyn std::error::Error + 'static)) -> bool {
matches!(
error.downcast_ref::<rustls::Error>(),
Some(rustls::Error::AlertReceived(
rustls::AlertDescription::AccessDenied
))
)
}
let mut cause: Option<&(dyn std::error::Error + 'static)> = Some(error);
while let Some(current) = cause {
if is_access_denied(current) {
return true;
}
if let Some(io) = current.downcast_ref::<std::io::Error>()
&& let Some(payload) = io.get_ref()
&& is_access_denied(payload)
{
return true;
}
cause = current.source();
}
chain.contains("received fatal alert: AccessDenied")
}
impl SidewinderClient {
pub fn from_algo_ops(algo: AlgoOps, config: SidewinderConfig) -> Self {
Self {
algo,
config,
tls: None,
}
}
pub fn mtls(algo: AlgoOps, base_url: impl Into<String>, tls: Arc<ClientConfig>) -> Self {
Self {
algo,
config: SidewinderConfig::new(base_url, String::new()),
tls: Some(tls),
}
}
pub fn connect(algo: AlgoOps, node: &DiscoveredNode) -> Result<Self> {
let base_url = node.base_url().ok_or_else(|| {
anyhow!(
"resolved node {} published no reachable endpoint",
node.identity
)
})?;
let own = identity_key(&algo)?;
let tls = pinned_client_config(&own, &node.identity)?;
Ok(Self::mtls(algo, base_url, Arc::new(tls)))
}
pub fn connect_pinned(
algo: AlgoOps,
base_url: impl Into<String>,
node_identity: &str,
) -> Result<Self> {
let own = identity_key(&algo)?;
let tls = pinned_client_config(&own, node_identity)?;
Ok(Self::mtls(algo, base_url, Arc::new(tls)))
}
pub fn resolve_and_connect(
algo: AlgoOps,
cfg: &DiscoveryConfig,
) -> Result<(Self, DiscoveredNode)> {
let nodes = crate::discovery::resolve_nodes(&algo, cfg)?;
let node = nodes
.into_iter()
.find(|node| node.base_url().is_some())
.ok_or_else(|| {
anyhow!(
"no permitted cluster node of app {} has published a reachable endpoint",
cfg.app_id
)
})?;
let client = Self::connect(algo, &node)?;
Ok((client, node))
}
pub fn get(&self, path: &str) -> Result<(u16, Vec<u8>)> {
self.send("get", Method::GET, path, None, true, None)
}
pub fn algo_ops(&self) -> &AlgoOps {
&self.algo
}
pub fn config(&self) -> &SidewinderConfig {
&self.config
}
pub fn build_signed_transaction(
&self,
request: &TransactionRequest,
) -> Result<SignedTransaction> {
let op = "build_signed_transaction";
let sender = self.algo.public_key_bytes().map_err(|e| {
SidewinderError::invalid_transaction(op, &format!("no signing key available: {e}"))
})?;
build_signed(op, request, sender, |bytes| self.algo.sign_bytes(bytes))
}
pub fn submit_transaction(&self, request: &TransactionRequest) -> Result<String> {
let signed = self.build_signed_transaction(request)?;
self.submit(&signed.bytes)
}
fn url(&self, path: &str) -> String {
format!("{}{}", self.config.trimmed_base(), path)
}
fn rt_block_on<T: Send>(&self, fut: impl std::future::Future<Output = T> + Send) -> Result<T> {
if tokio::runtime::Handle::try_current().is_ok() {
return std::thread::scope(|s| {
let handle = s.spawn(|| {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("failed to build temporary tokio runtime");
rt.block_on(fut)
});
handle
.join()
.map_err(|_| anyhow!("rt_block_on thread panicked"))
});
}
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|e| anyhow!("failed to build tokio runtime: {e}"))?;
Ok(rt.block_on(fut))
}
fn send_once(
&self,
method: Method,
path: &str,
body: Option<Vec<u8>>,
authenticated: bool,
timeout: Option<Duration>,
) -> std::result::Result<(u16, Vec<u8>), SendFailure> {
let url = self.url(path);
let token = self.config.token.clone();
let tls = self.tls.clone();
let fut = async move {
let mut builder = reqwest::Client::builder();
if let Some(tls) = &tls {
builder = builder.tls_backend_preconfigured((**tls).clone());
}
let mutual_tls = tls.is_some();
let client = builder.build().map_err(|e| {
SendFailure::Local(anyhow!("failed to build HTTP client: {}", cause_chain(&e)))
})?;
let mut req = client.request(method, &url);
if authenticated && tls.is_none() {
req = req.bearer_auth(&token);
}
if let Some(bytes) = body {
req = req
.header(reqwest::header::CONTENT_TYPE, "application/msgpack")
.body(bytes);
}
if let Some(t) = timeout {
req = req.timeout(t);
}
let resp = req
.send()
.await
.map_err(|e| SendFailure::from_reqwest(e, mutual_tls))?;
let status = resp.status().as_u16();
let bytes = resp
.bytes()
.await
.map_err(|e| SendFailure::from_reqwest(e, mutual_tls))?
.to_vec();
Ok::<(u16, Vec<u8>), SendFailure>((status, bytes))
};
self.rt_block_on(fut).map_err(SendFailure::Local)?
}
fn identity_refused(&self, operation: &str, chain: &str) -> SidewinderError {
let identity = self
.algo
.address_str()
.unwrap_or_else(|_| "(unknown address)".to_string());
SidewinderError::identity_refused(
operation,
&format!(
"the node at {} refused this client's identity {identity}: it is not a permitted \
client there. Check that the account is opted in to the node's membership \
application and has the client permission bit set (a newly set bit is only seen at \
the node's next membership poll). Cause: {chain}",
self.config.trimmed_base()
),
)
}
fn send(
&self,
operation: &str,
method: Method,
path: &str,
body: Option<Vec<u8>>,
authenticated: bool,
timeout: Option<Duration>,
) -> Result<(u16, Vec<u8>)> {
let mut attempt = 0u32;
loop {
match self.send_once(method.clone(), path, body.clone(), authenticated, timeout) {
Ok(pair) => return Ok(pair),
Err(SendFailure::IdentityRefused(chain)) => {
return Err(self.identity_refused(operation, &chain).into());
}
Err(SendFailure::Local(e)) => return Err(e),
Err(SendFailure::Transport(msg)) => {
if attempt < MAX_RETRIES && SidewinderError::looks_unreachable(&msg) {
let delay = Duration::from_millis(RETRY_BASE_MS * (1u64 << attempt));
tracing::warn!(
"sidewinder transient error on {} (attempt {}/{}), retrying in {:?}: {}",
operation,
attempt + 1,
MAX_RETRIES,
delay,
msg
);
std::thread::sleep(delay);
attempt += 1;
continue;
}
if SidewinderError::looks_unreachable(&msg) {
return Err(SidewinderError::unreachable(operation, &msg).into());
}
return Err(anyhow!("{msg}"));
}
}
}
}
}
fn parse_json<T: DeserializeOwned>(operation: &str, bytes: &[u8]) -> Result<T> {
serde_json::from_slice(bytes).map_err(|e| {
SidewinderError::malformed_response(operation, &format!("bad JSON: {e}")).into()
})
}
fn error_message(bytes: &[u8]) -> String {
match serde_json::from_slice::<crate::types::ErrorWire>(bytes) {
Ok(err) => err.message,
Err(_) => String::from_utf8_lossy(bytes).trim().to_string(),
}
}
impl SidewinderOps for SidewinderClient {
fn submit(&self, signed_txn: &[u8]) -> Result<String> {
let op = "submit";
let (status, body) = self.send(
op,
Method::POST,
"/v2/transactions",
Some(signed_txn.to_vec()),
true,
None,
)?;
match status {
200 => Ok(parse_json::<PostTransactionResponseWire>(op, &body)?.tx_id),
400 => Err(SidewinderError::bad_request(op, &error_message(&body)).into()),
401 => Err(SidewinderError::unauthorized(op, &error_message(&body)).into()),
other => {
Err(SidewinderError::unexpected_status(op, other, &error_message(&body)).into())
}
}
}
fn status(&self, txid: &str, proof: bool) -> Result<PendingTransaction> {
let op = "status";
let path = format!("/v2/transactions/pending/{txid}?proof={proof}");
let (status, body) = self.send(op, Method::GET, &path, None, true, None)?;
pending_from(op, status, &body)
}
fn watch(&self, txid: &str, proof: bool, wait_secs: u64) -> Result<PendingTransaction> {
let op = "watch";
let path = format!("/v2/transactions/pending/{txid}?proof={proof}&wait={wait_secs}");
let timeout = Duration::from_secs(wait_secs.saturating_add(30));
let (status, body) = self.send(op, Method::GET, &path, None, true, Some(timeout))?;
pending_from(op, status, &body)
}
fn params(&self) -> Result<SuggestedParams> {
let op = "params";
let (status, body) =
self.send(op, Method::GET, "/v2/transactions/params", None, true, None)?;
match status {
200 => parse_json::<SuggestedParamsWire>(op, &body)?
.into_params(op)
.map_err(Into::into),
401 => Err(SidewinderError::unauthorized(op, &error_message(&body)).into()),
other => {
Err(SidewinderError::unexpected_status(op, other, &error_message(&body)).into())
}
}
}
fn node_status(&self) -> Result<NodeStatus> {
let op = "node_status";
let (status, body) = self.send(op, Method::GET, "/v2/status", None, true, None)?;
match status {
200 => parse_json::<NodeStatusWire>(op, &body)?
.into_status(op)
.map_err(Into::into),
401 => Err(SidewinderError::unauthorized(op, &error_message(&body)).into()),
other => {
Err(SidewinderError::unexpected_status(op, other, &error_message(&body)).into())
}
}
}
fn operations(&self, typ: u32) -> Result<Option<OperationSchema>> {
let op = "operations";
let path = format!("/v2/operations/{typ}");
let (status, body) = self.send(op, Method::GET, &path, None, true, None)?;
match status {
200 => Ok(Some(
parse_json::<OperationSchemaWire>(op, &body)?.into_schema(op)?,
)),
404 => Ok(None),
401 => Err(SidewinderError::unauthorized(op, &error_message(&body)).into()),
other => {
Err(SidewinderError::unexpected_status(op, other, &error_message(&body)).into())
}
}
}
fn health(&self) -> Result<bool> {
let op = "health";
let (status, body) = self.send(op, Method::GET, "/health", None, false, None)?;
match status {
200 => Ok(true),
503 => Ok(false),
other => {
Err(SidewinderError::unexpected_status(op, other, &error_message(&body)).into())
}
}
}
}
fn pending_from(op: &str, status: u16, body: &[u8]) -> Result<PendingTransaction> {
match status {
200 => parse_json::<PendingWire>(op, body)?
.into_pending(op)
.map_err(Into::into),
401 => Err(SidewinderError::unauthorized(op, &error_message(body)).into()),
404 => Err(SidewinderError::not_found(op, &error_message(body)).into()),
other => Err(SidewinderError::unexpected_status(op, other, &error_message(body)).into()),
}
}