# Optional dependencies
sled = { version = "0.34", optional = true }
electrum-client = { version = "0.11", optional = true }
+esplora-client = { version = "0.1.1", default-features = false, optional = true }
rusqlite = { version = "0.27.0", optional = true }
ahash = { version = "0.7.6", optional = true }
-reqwest = { version = "0.11", optional = true, default-features = false, features = ["json"] }
-ureq = { version = "~2.2.0", features = ["json"], optional = true }
futures = { version = "0.3", optional = true }
async-trait = { version = "0.1", optional = true }
rocksdb = { version = "0.14", default-features = false, features = ["snappy"], optional = true }
async-interface = ["async-trait"]
electrum = ["electrum-client"]
# MUST ALSO USE `--no-default-features`.
-use-esplora-reqwest = ["esplora", "reqwest", "reqwest/socks", "futures"]
-use-esplora-ureq = ["esplora", "ureq", "ureq/socks"]
+use-esplora-reqwest = ["esplora", "esplora-client/async", "futures"]
+use-esplora-ureq = ["esplora", "esplora-client/blocking"]
# Typical configurations will not need to use `esplora` feature directly.
esplora = []
# Use below feature with `use-esplora-reqwest` to enable reqwest default TLS support
-reqwest-default-tls = ["reqwest/default-tls"]
+reqwest-default-tls = ["esplora-client/async-https"]
# Debug/Test features
test-blockchains = ["bitcoincore-rpc", "electrum-client"]
+++ /dev/null
-//! structs from the esplora API
-//!
-//! see: <https://github.com/Blockstream/esplora/blob/master/API.md>
-use crate::BlockTime;
-use bitcoin::{OutPoint, Script, Transaction, TxIn, TxOut, Txid, Witness};
-
-#[derive(serde::Deserialize, Clone, Debug)]
-pub struct PrevOut {
- pub value: u64,
- pub scriptpubkey: Script,
-}
-
-#[derive(serde::Deserialize, Clone, Debug)]
-pub struct Vin {
- pub txid: Txid,
- pub vout: u32,
- // None if coinbase
- pub prevout: Option<PrevOut>,
- pub scriptsig: Script,
- #[serde(deserialize_with = "deserialize_witness", default)]
- pub witness: Vec<Vec<u8>>,
- pub sequence: u32,
- pub is_coinbase: bool,
-}
-
-#[derive(serde::Deserialize, Clone, Debug)]
-pub struct Vout {
- pub value: u64,
- pub scriptpubkey: Script,
-}
-
-#[derive(serde::Deserialize, Clone, Debug)]
-pub struct TxStatus {
- pub confirmed: bool,
- pub block_height: Option<u32>,
- pub block_time: Option<u64>,
-}
-
-#[derive(serde::Deserialize, Clone, Debug)]
-pub struct Tx {
- pub txid: Txid,
- pub version: i32,
- pub locktime: u32,
- pub vin: Vec<Vin>,
- pub vout: Vec<Vout>,
- pub status: TxStatus,
- pub fee: u64,
-}
-
-impl Tx {
- pub fn to_tx(&self) -> Transaction {
- Transaction {
- version: self.version,
- lock_time: self.locktime,
- input: self
- .vin
- .iter()
- .cloned()
- .map(|vin| TxIn {
- previous_output: OutPoint {
- txid: vin.txid,
- vout: vin.vout,
- },
- script_sig: vin.scriptsig,
- sequence: vin.sequence,
- witness: Witness::from_vec(vin.witness),
- })
- .collect(),
- output: self
- .vout
- .iter()
- .cloned()
- .map(|vout| TxOut {
- value: vout.value,
- script_pubkey: vout.scriptpubkey,
- })
- .collect(),
- }
- }
-
- pub fn confirmation_time(&self) -> Option<BlockTime> {
- match self.status {
- TxStatus {
- confirmed: true,
- block_height: Some(height),
- block_time: Some(timestamp),
- } => Some(BlockTime { timestamp, height }),
- _ => None,
- }
- }
-
- pub fn previous_outputs(&self) -> Vec<Option<TxOut>> {
- self.vin
- .iter()
- .cloned()
- .map(|vin| {
- vin.prevout.map(|po| TxOut {
- script_pubkey: po.scriptpubkey,
- value: po.value,
- })
- })
- .collect()
- }
-}
-
-fn deserialize_witness<'de, D>(d: D) -> Result<Vec<Vec<u8>>, D::Error>
-where
- D: serde::de::Deserializer<'de>,
-{
- use crate::serde::Deserialize;
- use bitcoin::hashes::hex::FromHex;
- let list = Vec::<String>::deserialize(d)?;
- list.into_iter()
- .map(|hex_str| Vec::<u8>::from_hex(&hex_str))
- .collect::<Result<Vec<Vec<u8>>, _>>()
- .map_err(serde::de::Error::custom)
-}
//! Please note, to configure the Esplora HTTP client correctly use one of:
//! Blocking: --features='esplora,ureq'
//! Async: --features='async-interface,esplora,reqwest' --no-default-features
-use std::collections::HashMap;
-use std::fmt;
-use std::io;
-use bitcoin::consensus;
-use bitcoin::{BlockHash, Txid};
+pub use esplora_client::Error as EsploraError;
-use crate::error::Error;
-use crate::FeeRate;
-
-#[cfg(feature = "reqwest")]
+#[cfg(feature = "use-esplora-reqwest")]
mod reqwest;
-#[cfg(feature = "reqwest")]
+#[cfg(feature = "use-esplora-reqwest")]
pub use self::reqwest::*;
-#[cfg(feature = "ureq")]
+#[cfg(feature = "use-esplora-ureq")]
mod ureq;
-#[cfg(feature = "ureq")]
+#[cfg(feature = "use-esplora-ureq")]
pub use self::ureq::*;
-mod api;
-
-fn into_fee_rate(target: usize, estimates: HashMap<String, f64>) -> Result<FeeRate, Error> {
- let fee_val = {
- let mut pairs = estimates
- .into_iter()
- .filter_map(|(k, v)| Some((k.parse::<usize>().ok()?, v)))
- .collect::<Vec<_>>();
- pairs.sort_unstable_by_key(|(k, _)| std::cmp::Reverse(*k));
- pairs
- .into_iter()
- .find(|(k, _)| k <= &target)
- .map(|(_, v)| v)
- .unwrap_or(1.0)
- };
- Ok(FeeRate::from_sat_per_vb(fee_val as f32))
-}
-
-/// Errors that can happen during a sync with [`EsploraBlockchain`]
-#[derive(Debug)]
-pub enum EsploraError {
- /// Error during ureq HTTP request
- #[cfg(feature = "ureq")]
- Ureq(::ureq::Error),
- /// Transport error during the ureq HTTP call
- #[cfg(feature = "ureq")]
- UreqTransport(::ureq::Transport),
- /// Error during reqwest HTTP request
- #[cfg(feature = "reqwest")]
- Reqwest(::reqwest::Error),
- /// HTTP response error
- HttpResponse(u16),
- /// IO error during ureq response read
- Io(io::Error),
- /// No header found in ureq response
- NoHeader,
- /// Invalid number returned
- Parsing(std::num::ParseIntError),
- /// Invalid Bitcoin data returned
- BitcoinEncoding(bitcoin::consensus::encode::Error),
- /// Invalid Hex data returned
- Hex(bitcoin::hashes::hex::Error),
-
- /// Transaction not found
- TransactionNotFound(Txid),
- /// Header height not found
- HeaderHeightNotFound(u32),
- /// Header hash not found
- HeaderHashNotFound(BlockHash),
-}
-
-impl fmt::Display for EsploraError {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "{:?}", self)
- }
-}
-
/// Configuration for an [`EsploraBlockchain`]
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
pub struct EsploraBlockchainConfig {
}
}
-impl std::error::Error for EsploraError {}
-
-#[cfg(feature = "ureq")]
-impl_error!(::ureq::Transport, UreqTransport, EsploraError);
-#[cfg(feature = "reqwest")]
-impl_error!(::reqwest::Error, Reqwest, EsploraError);
-impl_error!(io::Error, Io, EsploraError);
-impl_error!(std::num::ParseIntError, Parsing, EsploraError);
-impl_error!(consensus::encode::Error, BitcoinEncoding, EsploraError);
-impl_error!(bitcoin::hashes::hex::Error, Hex, EsploraError);
+impl From<esplora_client::BlockTime> for crate::BlockTime {
+ fn from(esplora_client::BlockTime { timestamp, height }: esplora_client::BlockTime) -> Self {
+ Self { timestamp, height }
+ }
+}
#[cfg(test)]
#[cfg(feature = "test-esplora")]
#[cfg(test)]
mod test {
- use super::*;
-
- #[test]
- fn feerate_parsing() {
- let esplora_fees = serde_json::from_str::<HashMap<String, f64>>(
- r#"{
- "25": 1.015,
- "5": 2.3280000000000003,
- "12": 2.0109999999999997,
- "15": 1.018,
- "17": 1.018,
- "11": 2.0109999999999997,
- "3": 3.01,
- "2": 4.9830000000000005,
- "6": 2.2359999999999998,
- "21": 1.018,
- "13": 1.081,
- "7": 2.2359999999999998,
- "8": 2.2359999999999998,
- "16": 1.018,
- "20": 1.018,
- "22": 1.017,
- "23": 1.017,
- "504": 1,
- "9": 2.2359999999999998,
- "14": 1.018,
- "10": 2.0109999999999997,
- "24": 1.017,
- "1008": 1,
- "1": 4.9830000000000005,
- "4": 2.3280000000000003,
- "19": 1.018,
- "144": 1,
- "18": 1.018
-}
-"#,
- )
- .unwrap();
- assert_eq!(
- into_fee_rate(6, esplora_fees.clone()).unwrap(),
- FeeRate::from_sat_per_vb(2.236)
- );
- assert_eq!(
- into_fee_rate(26, esplora_fees).unwrap(),
- FeeRate::from_sat_per_vb(1.015),
- "should inherit from value for 25"
- );
- }
-
#[test]
#[cfg(feature = "test-esplora")]
fn test_esplora_with_variable_configs() {
+ use super::*;
+
use crate::testutils::{
blockchain_tests::TestClient,
configurable_blockchain_tests::ConfigurableBlockchainTester,
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
-use bitcoin::consensus::{deserialize, serialize};
-use bitcoin::hashes::hex::{FromHex, ToHex};
-use bitcoin::hashes::{sha256, Hash};
-use bitcoin::{BlockHeader, Script, Transaction, Txid};
+use bitcoin::{Transaction, Txid};
#[allow(unused_imports)]
use log::{debug, error, info, trace};
-use ::reqwest::{Client, StatusCode};
+use esplora_client::{convert_fee_rate, AsyncClient, Builder, Tx};
use futures::stream::{FuturesOrdered, TryStreamExt};
-use super::api::Tx;
-use crate::blockchain::esplora::EsploraError;
use crate::blockchain::*;
use crate::database::BatchDatabase;
use crate::error::Error;
use crate::FeeRate;
-/// Structure encapsulates Esplora client
-#[derive(Debug)]
-pub struct UrlClient {
- url: String,
- // We use the async client instead of the blocking one because it automatically uses `fetch`
- // when the target platform is wasm32.
- client: Client,
- concurrency: u8,
-}
-
/// Structure that implements the logic to sync with Esplora
///
/// ## Example
/// See the [`blockchain::esplora`](crate::blockchain::esplora) module for a usage example.
#[derive(Debug)]
pub struct EsploraBlockchain {
- url_client: UrlClient,
+ url_client: AsyncClient,
stop_gap: usize,
+ concurrency: u8,
}
-impl std::convert::From<UrlClient> for EsploraBlockchain {
- fn from(url_client: UrlClient) -> Self {
+impl std::convert::From<AsyncClient> for EsploraBlockchain {
+ fn from(url_client: AsyncClient) -> Self {
EsploraBlockchain {
url_client,
stop_gap: 20,
+ concurrency: super::DEFAULT_CONCURRENT_REQUESTS,
}
}
}
impl EsploraBlockchain {
/// Create a new instance of the client from a base URL and `stop_gap`.
pub fn new(base_url: &str, stop_gap: usize) -> Self {
+ let url_client = Builder::new(base_url)
+ .build_async()
+ .expect("Should never fail with no proxy and timeout");
+
+ Self::from_client(url_client, stop_gap)
+ }
+
+ /// Build a new instance given a client
+ pub fn from_client(url_client: AsyncClient, stop_gap: usize) -> Self {
EsploraBlockchain {
- url_client: UrlClient {
- url: base_url.to_string(),
- client: Client::new(),
- concurrency: super::DEFAULT_CONCURRENT_REQUESTS,
- },
+ url_client,
stop_gap,
+ concurrency: super::DEFAULT_CONCURRENT_REQUESTS,
}
}
/// Set the concurrency to use when doing batch queries against the Esplora instance.
pub fn with_concurrency(mut self, concurrency: u8) -> Self {
- self.url_client.concurrency = concurrency;
+ self.concurrency = concurrency;
self
}
}
}
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
- Ok(await_or_block!(self.url_client._broadcast(tx))?)
+ Ok(await_or_block!(self.url_client.broadcast(tx))?)
}
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
- let estimates = await_or_block!(self.url_client._get_fee_estimates())?;
- super::into_fee_rate(target, estimates)
+ let estimates = await_or_block!(self.url_client.get_fee_estimates())?;
+ Ok(FeeRate::from_sat_per_vb(convert_fee_rate(
+ target, estimates,
+ )?))
}
}
impl Deref for EsploraBlockchain {
- type Target = UrlClient;
+ type Target = AsyncClient;
fn deref(&self) -> &Self::Target {
&self.url_client
#[maybe_async]
impl GetHeight for EsploraBlockchain {
fn get_height(&self) -> Result<u32, Error> {
- Ok(await_or_block!(self.url_client._get_height())?)
+ Ok(await_or_block!(self.url_client.get_height())?)
}
}
#[maybe_async]
impl GetTx for EsploraBlockchain {
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
- Ok(await_or_block!(self.url_client._get_tx(txid))?)
+ Ok(await_or_block!(self.url_client.get_tx(txid))?)
}
}
#[maybe_async]
impl GetBlockHash for EsploraBlockchain {
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
- let block_header = await_or_block!(self.url_client._get_header(height as u32))?;
+ let block_header = await_or_block!(self.url_client.get_header(height as u32))?;
Ok(block_header.block_hash())
}
}
Request::Script(script_req) => {
let futures: FuturesOrdered<_> = script_req
.request()
- .take(self.url_client.concurrency as usize)
+ .take(self.concurrency as usize)
.map(|script| async move {
let mut related_txs: Vec<Tx> =
- self.url_client._scripthash_txs(script, None).await?;
+ self.url_client.scripthash_txs(script, None).await?;
let n_confirmed =
related_txs.iter().filter(|tx| tx.status.confirmed).count();
loop {
let new_related_txs: Vec<Tx> = self
.url_client
- ._scripthash_txs(
+ .scripthash_txs(
script,
Some(related_txs.last().unwrap().txid),
)
.get(txid)
.expect("must be in index")
.confirmation_time()
+ .map(Into::into)
})
.collect();
conftime_req.satisfy(conftimes)?
}
}
-impl UrlClient {
- async fn _get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, EsploraError> {
- let resp = self
- .client
- .get(&format!("{}/tx/{}/raw", self.url, txid))
- .send()
- .await?;
-
- if let StatusCode::NOT_FOUND = resp.status() {
- return Ok(None);
- }
-
- Ok(Some(deserialize(&resp.error_for_status()?.bytes().await?)?))
- }
-
- async fn _get_tx_no_opt(&self, txid: &Txid) -> Result<Transaction, EsploraError> {
- match self._get_tx(txid).await {
- Ok(Some(tx)) => Ok(tx),
- Ok(None) => Err(EsploraError::TransactionNotFound(*txid)),
- Err(e) => Err(e),
- }
- }
-
- async fn _get_header(&self, block_height: u32) -> Result<BlockHeader, EsploraError> {
- let resp = self
- .client
- .get(&format!("{}/block-height/{}", self.url, block_height))
- .send()
- .await?;
-
- if let StatusCode::NOT_FOUND = resp.status() {
- return Err(EsploraError::HeaderHeightNotFound(block_height));
- }
- let bytes = resp.bytes().await?;
- let hash = std::str::from_utf8(&bytes)
- .map_err(|_| EsploraError::HeaderHeightNotFound(block_height))?;
-
- let resp = self
- .client
- .get(&format!("{}/block/{}/header", self.url, hash))
- .send()
- .await?;
-
- let header = deserialize(&Vec::from_hex(&resp.text().await?)?)?;
-
- Ok(header)
- }
-
- async fn _broadcast(&self, transaction: &Transaction) -> Result<(), EsploraError> {
- self.client
- .post(&format!("{}/tx", self.url))
- .body(serialize(transaction).to_hex())
- .send()
- .await?
- .error_for_status()?;
-
- Ok(())
- }
-
- async fn _get_height(&self) -> Result<u32, EsploraError> {
- let req = self
- .client
- .get(&format!("{}/blocks/tip/height", self.url))
- .send()
- .await?;
-
- Ok(req.error_for_status()?.text().await?.parse()?)
- }
-
- async fn _scripthash_txs(
- &self,
- script: &Script,
- last_seen: Option<Txid>,
- ) -> Result<Vec<Tx>, EsploraError> {
- let script_hash = sha256::Hash::hash(script.as_bytes()).into_inner().to_hex();
- let url = match last_seen {
- Some(last_seen) => format!(
- "{}/scripthash/{}/txs/chain/{}",
- self.url, script_hash, last_seen
- ),
- None => format!("{}/scripthash/{}/txs", self.url, script_hash),
- };
- Ok(self
- .client
- .get(url)
- .send()
- .await?
- .error_for_status()?
- .json::<Vec<Tx>>()
- .await?)
- }
-
- async fn _get_fee_estimates(&self) -> Result<HashMap<String, f64>, EsploraError> {
- Ok(self
- .client
- .get(&format!("{}/fee-estimates", self.url,))
- .send()
- .await?
- .error_for_status()?
- .json::<HashMap<String, f64>>()
- .await?)
- }
-}
-
impl ConfigurableBlockchain for EsploraBlockchain {
type Config = super::EsploraBlockchainConfig;
fn from_config(config: &Self::Config) -> Result<Self, Error> {
- let map_e = |e: reqwest::Error| Error::Esplora(Box::new(e.into()));
+ let mut builder = Builder::new(config.base_url.as_str());
- let mut blockchain = EsploraBlockchain::new(config.base_url.as_str(), config.stop_gap);
- if let Some(concurrency) = config.concurrency {
- blockchain.url_client.concurrency = concurrency;
+ if let Some(timeout) = config.timeout {
+ builder = builder.timeout(timeout);
}
- let mut builder = Client::builder();
- #[cfg(not(target_arch = "wasm32"))]
+
if let Some(proxy) = &config.proxy {
- builder = builder.proxy(reqwest::Proxy::all(proxy).map_err(map_e)?);
+ builder = builder.proxy(proxy);
}
- #[cfg(not(target_arch = "wasm32"))]
- if let Some(timeout) = config.timeout {
- builder = builder.timeout(core::time::Duration::from_secs(timeout));
- }
+ let mut blockchain =
+ EsploraBlockchain::from_client(builder.build_async()?, config.stop_gap);
- blockchain.url_client.client = builder.build().map_err(map_e)?;
+ if let Some(concurrency) = config.concurrency {
+ blockchain = blockchain.with_concurrency(concurrency);
+ }
Ok(blockchain)
}
//! Esplora by way of `ureq` HTTP client.
use std::collections::{HashMap, HashSet};
-use std::io;
-use std::io::Read;
-use std::ops::Deref;
-use std::time::Duration;
#[allow(unused_imports)]
use log::{debug, error, info, trace};
-use ureq::{Agent, Proxy, Response};
+use bitcoin::{Transaction, Txid};
-use bitcoin::consensus::{deserialize, serialize};
-use bitcoin::hashes::hex::{FromHex, ToHex};
-use bitcoin::hashes::{sha256, Hash};
-use bitcoin::{BlockHeader, Script, Transaction, Txid};
+use esplora_client::{convert_fee_rate, BlockingClient, Builder, Tx};
-use super::api::Tx;
-use crate::blockchain::esplora::EsploraError;
use crate::blockchain::*;
use crate::database::BatchDatabase;
use crate::error::Error;
use crate::FeeRate;
-/// Structure encapsulates ureq Esplora client
-#[derive(Debug, Clone)]
-pub struct UrlClient {
- url: String,
- agent: Agent,
-}
-
/// Structure that implements the logic to sync with Esplora
///
/// ## Example
/// See the [`blockchain::esplora`](crate::blockchain::esplora) module for a usage example.
#[derive(Debug)]
pub struct EsploraBlockchain {
- url_client: UrlClient,
+ url_client: BlockingClient,
stop_gap: usize,
concurrency: u8,
}
impl EsploraBlockchain {
/// Create a new instance of the client from a base URL and the `stop_gap`.
pub fn new(base_url: &str, stop_gap: usize) -> Self {
+ let url_client = Builder::new(base_url)
+ .build_blocking()
+ .expect("Should never fail with no proxy and timeout");
+
+ Self::from_client(url_client, stop_gap)
+ }
+
+ /// Build a new instance given a client
+ pub fn from_client(url_client: BlockingClient, stop_gap: usize) -> Self {
EsploraBlockchain {
- url_client: UrlClient {
- url: base_url.to_string(),
- agent: Agent::new(),
- },
+ url_client,
concurrency: super::DEFAULT_CONCURRENT_REQUESTS,
stop_gap,
}
}
- /// Set the inner `ureq` agent.
- pub fn with_agent(mut self, agent: Agent) -> Self {
- self.url_client.agent = agent;
- self
- }
-
/// Set the number of parallel requests the client can make.
pub fn with_concurrency(mut self, concurrency: u8) -> Self {
self.concurrency = concurrency;
}
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
- self.url_client._broadcast(tx)?;
+ self.url_client.broadcast(tx)?;
Ok(())
}
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
- let estimates = self.url_client._get_fee_estimates()?;
- super::into_fee_rate(target, estimates)
+ let estimates = self.url_client.get_fee_estimates()?;
+ Ok(FeeRate::from_sat_per_vb(convert_fee_rate(
+ target, estimates,
+ )?))
}
}
impl Deref for EsploraBlockchain {
- type Target = UrlClient;
+ type Target = BlockingClient;
fn deref(&self) -> &Self::Target {
&self.url_client
impl GetHeight for EsploraBlockchain {
fn get_height(&self) -> Result<u32, Error> {
- Ok(self.url_client._get_height()?)
+ Ok(self.url_client.get_height()?)
}
}
impl GetTx for EsploraBlockchain {
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
- Ok(self.url_client._get_tx(txid)?)
+ Ok(self.url_client.get_tx(txid)?)
}
}
impl GetBlockHash for EsploraBlockchain {
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
- let block_header = self.url_client._get_header(height as u32)?;
+ let block_header = self.url_client.get_header(height as u32)?;
Ok(block_header.block_hash())
}
}
let client = self.url_client.clone();
// make each request in its own thread.
handles.push(std::thread::spawn(move || {
- let mut related_txs: Vec<Tx> = client._scripthash_txs(&script, None)?;
+ let mut related_txs: Vec<Tx> = client.scripthash_txs(&script, None)?;
let n_confirmed =
related_txs.iter().filter(|tx| tx.status.confirmed).count();
// keep requesting to see if there's more.
if n_confirmed >= 25 {
loop {
- let new_related_txs: Vec<Tx> = client._scripthash_txs(
+ let new_related_txs: Vec<Tx> = client.scripthash_txs(
&script,
Some(related_txs.last().unwrap().txid),
)?;
.get(txid)
.expect("must be in index")
.confirmation_time()
+ .map(Into::into)
})
.collect();
conftime_req.satisfy(conftimes)?
}
}
-impl UrlClient {
- fn _get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, EsploraError> {
- let resp = self
- .agent
- .get(&format!("{}/tx/{}/raw", self.url, txid))
- .call();
-
- match resp {
- Ok(resp) => Ok(Some(deserialize(&into_bytes(resp)?)?)),
- Err(ureq::Error::Status(code, _)) => {
- if is_status_not_found(code) {
- return Ok(None);
- }
- Err(EsploraError::HttpResponse(code))
- }
- Err(e) => Err(EsploraError::Ureq(e)),
- }
- }
-
- fn _get_tx_no_opt(&self, txid: &Txid) -> Result<Transaction, EsploraError> {
- match self._get_tx(txid) {
- Ok(Some(tx)) => Ok(tx),
- Ok(None) => Err(EsploraError::TransactionNotFound(*txid)),
- Err(e) => Err(e),
- }
- }
-
- fn _get_header(&self, block_height: u32) -> Result<BlockHeader, EsploraError> {
- let resp = self
- .agent
- .get(&format!("{}/block-height/{}", self.url, block_height))
- .call();
-
- let bytes = match resp {
- Ok(resp) => Ok(into_bytes(resp)?),
- Err(ureq::Error::Status(code, _)) => Err(EsploraError::HttpResponse(code)),
- Err(e) => Err(EsploraError::Ureq(e)),
- }?;
-
- let hash = std::str::from_utf8(&bytes)
- .map_err(|_| EsploraError::HeaderHeightNotFound(block_height))?;
-
- let resp = self
- .agent
- .get(&format!("{}/block/{}/header", self.url, hash))
- .call();
-
- match resp {
- Ok(resp) => Ok(deserialize(&Vec::from_hex(&resp.into_string()?)?)?),
- Err(ureq::Error::Status(code, _)) => Err(EsploraError::HttpResponse(code)),
- Err(e) => Err(EsploraError::Ureq(e)),
- }
- }
-
- fn _broadcast(&self, transaction: &Transaction) -> Result<(), EsploraError> {
- let resp = self
- .agent
- .post(&format!("{}/tx", self.url))
- .send_string(&serialize(transaction).to_hex());
-
- match resp {
- Ok(_) => Ok(()), // We do not return the txid?
- Err(ureq::Error::Status(code, _)) => Err(EsploraError::HttpResponse(code)),
- Err(e) => Err(EsploraError::Ureq(e)),
- }
- }
-
- fn _get_height(&self) -> Result<u32, EsploraError> {
- let resp = self
- .agent
- .get(&format!("{}/blocks/tip/height", self.url))
- .call();
-
- match resp {
- Ok(resp) => Ok(resp.into_string()?.parse()?),
- Err(ureq::Error::Status(code, _)) => Err(EsploraError::HttpResponse(code)),
- Err(e) => Err(EsploraError::Ureq(e)),
- }
- }
-
- fn _get_fee_estimates(&self) -> Result<HashMap<String, f64>, EsploraError> {
- let resp = self
- .agent
- .get(&format!("{}/fee-estimates", self.url,))
- .call();
-
- let map = match resp {
- Ok(resp) => {
- let map: HashMap<String, f64> = resp.into_json()?;
- Ok(map)
- }
- Err(ureq::Error::Status(code, _)) => Err(EsploraError::HttpResponse(code)),
- Err(e) => Err(EsploraError::Ureq(e)),
- }?;
-
- Ok(map)
- }
-
- fn _scripthash_txs(
- &self,
- script: &Script,
- last_seen: Option<Txid>,
- ) -> Result<Vec<Tx>, EsploraError> {
- let script_hash = sha256::Hash::hash(script.as_bytes()).into_inner().to_hex();
- let url = match last_seen {
- Some(last_seen) => format!(
- "{}/scripthash/{}/txs/chain/{}",
- self.url, script_hash, last_seen
- ),
- None => format!("{}/scripthash/{}/txs", self.url, script_hash),
- };
- Ok(self.agent.get(&url).call()?.into_json()?)
- }
-}
-
-fn is_status_not_found(status: u16) -> bool {
- status == 404
-}
-
-fn into_bytes(resp: Response) -> Result<Vec<u8>, io::Error> {
- const BYTES_LIMIT: usize = 10 * 1_024 * 1_024;
-
- let mut buf: Vec<u8> = vec![];
- resp.into_reader()
- .take((BYTES_LIMIT + 1) as u64)
- .read_to_end(&mut buf)?;
- if buf.len() > BYTES_LIMIT {
- return Err(io::Error::new(
- io::ErrorKind::Other,
- "response too big for into_bytes",
- ));
- }
-
- Ok(buf)
-}
-
impl ConfigurableBlockchain for EsploraBlockchain {
type Config = super::EsploraBlockchainConfig;
fn from_config(config: &Self::Config) -> Result<Self, Error> {
- let mut agent_builder = ureq::AgentBuilder::new();
+ let mut builder = Builder::new(config.base_url.as_str());
if let Some(timeout) = config.timeout {
- agent_builder = agent_builder.timeout(Duration::from_secs(timeout));
+ builder = builder.timeout(timeout);
}
if let Some(proxy) = &config.proxy {
- agent_builder = agent_builder
- .proxy(Proxy::new(proxy).map_err(|e| Error::Esplora(Box::new(e.into())))?);
+ builder = builder.proxy(proxy);
}
- let mut blockchain = EsploraBlockchain::new(config.base_url.as_str(), config.stop_gap)
- .with_agent(agent_builder.build());
+ let mut blockchain =
+ EsploraBlockchain::from_client(builder.build_blocking()?, config.stop_gap);
if let Some(concurrency) = config.concurrency {
blockchain = blockchain.with_concurrency(concurrency);
Ok(blockchain)
}
}
-
-impl From<ureq::Error> for EsploraError {
- fn from(e: ureq::Error) -> Self {
- match e {
- ureq::Error::Status(code, _) => EsploraError::HttpResponse(code),
- e => EsploraError::Ureq(e),
- }
- }
-}