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 serde::de::DeserializeOwned;
use std::time::Duration;
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,
}
const MAX_RETRIES: u32 = 3;
const RETRY_BASE_MS: u64 = 1_000;
impl SidewinderClient {
pub fn from_algo_ops(algo: AlgoOps, config: SidewinderConfig) -> Self {
Self { algo, config }
}
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>,
) -> Result<(u16, Vec<u8>)> {
let url = self.url(path);
let token = self.config.token.clone();
let fut = async move {
let client = reqwest::Client::builder()
.build()
.map_err(|e| anyhow!("failed to build HTTP client: {e}"))?;
let mut req = client.request(method, &url);
if authenticated {
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| anyhow!("{e}"))?;
let status = resp.status().as_u16();
let bytes = resp.bytes().await.map_err(|e| anyhow!("{e}"))?.to_vec();
Ok::<(u16, Vec<u8>), anyhow::Error>((status, bytes))
};
self.rt_block_on(fut)?
}
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(e) => {
let msg = e.to_string();
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(e);
}
}
}
}
}
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()),
}
}