diff --git a/.github/workflows/cont_integration.yml b/.github/workflows/cont_integration.yml index af7e256f..3b083902 100644 --- a/.github/workflows/cont_integration.yml +++ b/.github/workflows/cont_integration.yml @@ -17,16 +17,16 @@ jobs: - default - minimal - all-keys - - minimal,use-esplora-ureq + - minimal,use-esplora-blocking - key-value-db - electrum - compact_filters - - esplora,ureq,key-value-db,electrum + - use-esplora-blocking,key-value-db,electrum - compiler - rpc - verify - async-interface - - use-esplora-reqwest + - use-esplora-async - sqlite - sqlite-bundled steps: @@ -100,10 +100,10 @@ jobs: features: test-rpc-legacy - name: esplora testprefix: esplora - features: test-esplora,use-esplora-reqwest,verify + features: test-esplora,use-esplora-async,verify - name: esplora testprefix: esplora - features: test-esplora,use-esplora-ureq,verify + features: test-esplora,use-esplora-blocking,verify steps: - name: Checkout uses: actions/checkout@v2 @@ -154,7 +154,7 @@ jobs: - name: Update toolchain run: rustup update - name: Check - run: cargo check --target wasm32-unknown-unknown --features use-esplora-reqwest --no-default-features + run: cargo check --target wasm32-unknown-unknown --features use-esplora-async --no-default-features fmt: name: Rust fmt diff --git a/.github/workflows/nightly_docs.yml b/.github/workflows/nightly_docs.yml index a82beb2d..190006ea 100644 --- a/.github/workflows/nightly_docs.yml +++ b/.github/workflows/nightly_docs.yml @@ -24,7 +24,7 @@ jobs: - name: Update toolchain run: rustup update - name: Build docs - run: cargo rustdoc --verbose --features=compiler,electrum,esplora,ureq,compact_filters,key-value-db,all-keys,sqlite -- --cfg docsrs -Dwarnings + run: cargo rustdoc --verbose --features=compiler,electrum,esplora,use-esplora-blocking,compact_filters,rpc,key-value-db,sqlite,all-keys,verify,hardware-signer -- --cfg docsrs -Dwarnings - name: Upload artifact uses: actions/upload-artifact@v2 with: diff --git a/Cargo.toml b/Cargo.toml index e0b73698..cac6c17e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,10 +23,9 @@ rand = "^0.7" # 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 } @@ -69,23 +68,26 @@ hardware-signer = ["hwi"] # # - Users wanting asynchronous HTTP calls should enable `async-interface` to get # access to the asynchronous method implementations. Then, if Esplora is wanted, -# enable `esplora` AND `reqwest` (`--features=use-esplora-reqwest`). +# enable the `use-esplora-async` feature. # - Users wanting blocking HTTP calls can use any of the other blockchain # implementations (`compact_filters`, `electrum`, or `esplora`). Users wanting to -# use Esplora should enable `esplora` AND `ureq` (`--features=use-esplora-ureq`). +# use Esplora should enable the `use-esplora-blocking` feature. # # WARNING: Please take care with the features below, various combinations will # fail to build. We cannot currently build `bdk` with `--all-features`. 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-async = ["esplora", "esplora-client/async", "futures"] +use-esplora-blocking = ["esplora", "esplora-client/blocking"] +# Deprecated aliases +use-esplora-reqwest = ["use-esplora-async"] +use-esplora-ureq = ["use-esplora-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"] +# Use below feature with `use-esplora-async` to enable reqwest default TLS support +reqwest-default-tls = ["esplora-client/async-https"] # Debug/Test features test-blockchains = ["bitcoincore-rpc", "electrum-client"] @@ -129,6 +131,6 @@ required-features = ["electrum"] [workspace] members = ["macros"] [package.metadata.docs.rs] -features = ["compiler", "electrum", "esplora", "use-esplora-ureq", "compact_filters", "rpc", "key-value-db", "sqlite", "all-keys", "verify", "hardware-signer"] +features = ["compiler", "electrum", "esplora", "use-esplora-blocking", "compact_filters", "rpc", "key-value-db", "sqlite", "all-keys", "verify", "hardware-signer"] # defines the configuration attribute `docsrs` rustdoc-args = ["--cfg", "docsrs"] diff --git a/src/blockchain/esplora/api.rs b/src/blockchain/esplora/api.rs deleted file mode 100644 index d548b5be..00000000 --- a/src/blockchain/esplora/api.rs +++ /dev/null @@ -1,117 +0,0 @@ -//! structs from the esplora API -//! -//! see: -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, - pub scriptsig: Script, - #[serde(deserialize_with = "deserialize_witness", default)] - pub witness: Vec>, - 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, - pub block_time: Option, -} - -#[derive(serde::Deserialize, Clone, Debug)] -pub struct Tx { - pub txid: Txid, - pub version: i32, - pub locktime: u32, - pub vin: Vec, - pub vout: Vec, - 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 { - 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> { - 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>, D::Error> -where - D: serde::de::Deserializer<'de>, -{ - use crate::serde::Deserialize; - use bitcoin::hashes::hex::FromHex; - let list = Vec::::deserialize(d)?; - list.into_iter() - .map(|hex_str| Vec::::from_hex(&hex_str)) - .collect::>, _>>() - .map_err(serde::de::Error::custom) -} diff --git a/src/blockchain/esplora/reqwest.rs b/src/blockchain/esplora/async.rs similarity index 56% rename from src/blockchain/esplora/reqwest.rs rename to src/blockchain/esplora/async.rs index b549f30a..5ddbdeb4 100644 --- a/src/blockchain/esplora/reqwest.rs +++ b/src/blockchain/esplora/async.rs @@ -14,49 +14,36 @@ 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 for EsploraBlockchain { - fn from(url_client: UrlClient) -> Self { +impl std::convert::From for EsploraBlockchain { + fn from(url_client: AsyncClient) -> Self { EsploraBlockchain { url_client, stop_gap: 20, + concurrency: super::DEFAULT_CONCURRENT_REQUESTS, } } } @@ -64,19 +51,25 @@ impl std::convert::From for EsploraBlockchain { 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 } } @@ -94,17 +87,19 @@ impl Blockchain for EsploraBlockchain { } 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 { - 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 @@ -116,21 +111,21 @@ impl StatelessBlockchain for EsploraBlockchain {} #[maybe_async] impl GetHeight for EsploraBlockchain { fn get_height(&self) -> Result { - 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, 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 { - 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()) } } @@ -151,10 +146,10 @@ impl WalletSync for EsploraBlockchain { 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 = - 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(); @@ -164,7 +159,7 @@ impl WalletSync for EsploraBlockchain { loop { let new_related_txs: Vec = self .url_client - ._scripthash_txs( + .scripthash_txs( script, Some(related_txs.last().unwrap().txid), ) @@ -204,6 +199,7 @@ impl WalletSync for EsploraBlockchain { .get(txid) .expect("must be in index") .confirmation_time() + .map(Into::into) }) .collect(); conftime_req.satisfy(conftimes)? @@ -227,132 +223,26 @@ impl WalletSync for EsploraBlockchain { } } -impl UrlClient { - async fn _get_tx(&self, txid: &Txid) -> Result, 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 { - 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 { - 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 { - 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, - ) -> Result, 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::>() - .await?) - } - - async fn _get_fee_estimates(&self) -> Result, EsploraError> { - Ok(self - .client - .get(&format!("{}/fee-estimates", self.url,)) - .send() - .await? - .error_for_status()? - .json::>() - .await?) - } -} - impl ConfigurableBlockchain for EsploraBlockchain { type Config = super::EsploraBlockchainConfig; fn from_config(config: &Self::Config) -> Result { - 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; - } - 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)?); - } - - #[cfg(not(target_arch = "wasm32"))] if let Some(timeout) = config.timeout { - builder = builder.timeout(core::time::Duration::from_secs(timeout)); + builder = builder.timeout(timeout); } - blockchain.url_client.client = builder.build().map_err(map_e)?; + if let Some(proxy) = &config.proxy { + builder = builder.proxy(proxy); + } + + let mut blockchain = + EsploraBlockchain::from_client(builder.build_async()?, config.stop_gap); + + if let Some(concurrency) = config.concurrency { + blockchain = blockchain.with_concurrency(concurrency); + } Ok(blockchain) } diff --git a/src/blockchain/esplora/ureq.rs b/src/blockchain/esplora/blocking.rs similarity index 52% rename from src/blockchain/esplora/ureq.rs rename to src/blockchain/esplora/blocking.rs index 7a9388a0..1e9d1cfc 100644 --- a/src/blockchain/esplora/ureq.rs +++ b/src/blockchain/esplora/blocking.rs @@ -12,42 +12,26 @@ //! 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, } @@ -55,22 +39,22 @@ pub struct EsploraBlockchain { 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; @@ -90,18 +74,20 @@ impl Blockchain for EsploraBlockchain { } 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 { - 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 @@ -112,19 +98,19 @@ impl StatelessBlockchain for EsploraBlockchain {} impl GetHeight for EsploraBlockchain { fn get_height(&self) -> Result { - Ok(self.url_client._get_height()?) + Ok(self.url_client.get_height()?) } } impl GetTx for EsploraBlockchain { fn get_tx(&self, txid: &Txid) -> Result, 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 { - 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()) } } @@ -151,7 +137,7 @@ impl WalletSync for EsploraBlockchain { let client = self.url_client.clone(); // make each request in its own thread. handles.push(std::thread::spawn(move || { - let mut related_txs: Vec = client._scripthash_txs(&script, None)?; + let mut related_txs: Vec = client.scripthash_txs(&script, None)?; let n_confirmed = related_txs.iter().filter(|tx| tx.status.confirmed).count(); @@ -159,7 +145,7 @@ impl WalletSync for EsploraBlockchain { // keep requesting to see if there's more. if n_confirmed >= 25 { loop { - let new_related_txs: Vec = client._scripthash_txs( + let new_related_txs: Vec = client.scripthash_txs( &script, Some(related_txs.last().unwrap().txid), )?; @@ -202,6 +188,7 @@ impl WalletSync for EsploraBlockchain { .get(txid) .expect("must be in index") .confirmation_time() + .map(Into::into) }) .collect(); conftime_req.satisfy(conftimes)? @@ -226,159 +213,22 @@ impl WalletSync for EsploraBlockchain { } } -impl UrlClient { - fn _get_tx(&self, txid: &Txid) -> Result, 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 { - 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 { - 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 { - 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, EsploraError> { - let resp = self - .agent - .get(&format!("{}/fee-estimates", self.url,)) - .call(); - - let map = match resp { - Ok(resp) => { - let map: HashMap = 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, - ) -> Result, 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, io::Error> { - const BYTES_LIMIT: usize = 10 * 1_024 * 1_024; - - let mut buf: Vec = 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 { - 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); @@ -387,12 +237,3 @@ impl ConfigurableBlockchain for EsploraBlockchain { Ok(blockchain) } } - -impl From for EsploraError { - fn from(e: ureq::Error) -> Self { - match e { - ureq::Error::Status(code, _) => EsploraError::HttpResponse(code), - e => EsploraError::Ureq(e), - } - } -} diff --git a/src/blockchain/esplora/mod.rs b/src/blockchain/esplora/mod.rs index 30d29d64..57032e49 100644 --- a/src/blockchain/esplora/mod.rs +++ b/src/blockchain/esplora/mod.rs @@ -15,86 +15,22 @@ //! depending on your needs (blocking or async respectively). //! //! 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; +//! Blocking: --features='use-esplora-blocking' +//! Async: --features='async-interface,use-esplora-async' --no-default-features -use bitcoin::consensus; -use bitcoin::{BlockHash, Txid}; +pub use esplora_client::Error as EsploraError; -use crate::error::Error; -use crate::FeeRate; +#[cfg(feature = "use-esplora-async")] +mod r#async; -#[cfg(feature = "reqwest")] -mod reqwest; +#[cfg(feature = "use-esplora-async")] +pub use self::r#async::*; -#[cfg(feature = "reqwest")] -pub use self::reqwest::*; +#[cfg(feature = "use-esplora-blocking")] +mod blocking; -#[cfg(feature = "ureq")] -mod ureq; - -#[cfg(feature = "ureq")] -pub use self::ureq::*; - -mod api; - -fn into_fee_rate(target: usize, estimates: HashMap) -> Result { - let fee_val = { - let mut pairs = estimates - .into_iter() - .filter_map(|(k, v)| Some((k.parse::().ok()?, v))) - .collect::>(); - 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) - } -} +#[cfg(feature = "use-esplora-blocking")] +pub use self::blocking::*; /// Configuration for an [`EsploraBlockchain`] #[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)] @@ -138,16 +74,11 @@ impl 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 for crate::BlockTime { + fn from(esplora_client::BlockTime { timestamp, height }: esplora_client::BlockTime) -> Self { + Self { timestamp, height } + } +} #[cfg(test)] #[cfg(feature = "test-esplora")] @@ -161,58 +92,11 @@ const DEFAULT_CONCURRENT_REQUESTS: u8 = 4; #[cfg(test)] mod test { - use super::*; - - #[test] - fn feerate_parsing() { - let esplora_fees = serde_json::from_str::>( - 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, diff --git a/src/lib.rs b/src/lib.rs index bed048cf..65e35a72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -237,6 +237,9 @@ pub extern crate bitcoincore_rpc; #[cfg(feature = "electrum")] pub extern crate electrum_client; +#[cfg(feature = "esplora")] +pub extern crate esplora_client; + #[cfg(feature = "key-value-db")] pub extern crate sled; diff --git a/src/wallet/verify.rs b/src/wallet/verify.rs index 0f12e056..084388b9 100644 --- a/src/wallet/verify.rs +++ b/src/wallet/verify.rs @@ -29,6 +29,8 @@ use crate::error::Error; /// Depending on the [capabilities](crate::blockchain::Blockchain::get_capabilities) of the /// [`Blockchain`] backend, the method could fail when called with old "historical" transactions or /// with unconfirmed transactions that have been evicted from the backend's memory. +/// +/// [`Blockchain`]: crate::blockchain::Blockchain pub fn verify_tx( tx: &Transaction, database: &D,