Use the external esplora client library
This commit is contained in:
parent
92ad4876c4
commit
5baf46f84d
@ -23,10 +23,9 @@ rand = "^0.7"
|
|||||||
# Optional dependencies
|
# Optional dependencies
|
||||||
sled = { version = "0.34", optional = true }
|
sled = { version = "0.34", optional = true }
|
||||||
electrum-client = { version = "0.11", 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 }
|
rusqlite = { version = "0.27.0", optional = true }
|
||||||
ahash = { version = "0.7.6", 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 }
|
futures = { version = "0.3", optional = true }
|
||||||
async-trait = { version = "0.1", optional = true }
|
async-trait = { version = "0.1", optional = true }
|
||||||
rocksdb = { version = "0.14", default-features = false, features = ["snappy"], optional = true }
|
rocksdb = { version = "0.14", default-features = false, features = ["snappy"], optional = true }
|
||||||
@ -79,13 +78,13 @@ hardware-signer = ["hwi"]
|
|||||||
async-interface = ["async-trait"]
|
async-interface = ["async-trait"]
|
||||||
electrum = ["electrum-client"]
|
electrum = ["electrum-client"]
|
||||||
# MUST ALSO USE `--no-default-features`.
|
# MUST ALSO USE `--no-default-features`.
|
||||||
use-esplora-reqwest = ["esplora", "reqwest", "reqwest/socks", "futures"]
|
use-esplora-reqwest = ["esplora", "esplora-client/async", "futures"]
|
||||||
use-esplora-ureq = ["esplora", "ureq", "ureq/socks"]
|
use-esplora-ureq = ["esplora", "esplora-client/blocking"]
|
||||||
# Typical configurations will not need to use `esplora` feature directly.
|
# Typical configurations will not need to use `esplora` feature directly.
|
||||||
esplora = []
|
esplora = []
|
||||||
|
|
||||||
# Use below feature with `use-esplora-reqwest` to enable reqwest default TLS support
|
# 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
|
# Debug/Test features
|
||||||
test-blockchains = ["bitcoincore-rpc", "electrum-client"]
|
test-blockchains = ["bitcoincore-rpc", "electrum-client"]
|
||||||
|
@ -1,117 +0,0 @@
|
|||||||
//! 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)
|
|
||||||
}
|
|
@ -17,85 +17,21 @@
|
|||||||
//! Please note, to configure the Esplora HTTP client correctly use one of:
|
//! Please note, to configure the Esplora HTTP client correctly use one of:
|
||||||
//! Blocking: --features='esplora,ureq'
|
//! Blocking: --features='esplora,ureq'
|
||||||
//! Async: --features='async-interface,esplora,reqwest' --no-default-features
|
//! Async: --features='async-interface,esplora,reqwest' --no-default-features
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::fmt;
|
|
||||||
use std::io;
|
|
||||||
|
|
||||||
use bitcoin::consensus;
|
pub use esplora_client::Error as EsploraError;
|
||||||
use bitcoin::{BlockHash, Txid};
|
|
||||||
|
|
||||||
use crate::error::Error;
|
#[cfg(feature = "use-esplora-reqwest")]
|
||||||
use crate::FeeRate;
|
|
||||||
|
|
||||||
#[cfg(feature = "reqwest")]
|
|
||||||
mod reqwest;
|
mod reqwest;
|
||||||
|
|
||||||
#[cfg(feature = "reqwest")]
|
#[cfg(feature = "use-esplora-reqwest")]
|
||||||
pub use self::reqwest::*;
|
pub use self::reqwest::*;
|
||||||
|
|
||||||
#[cfg(feature = "ureq")]
|
#[cfg(feature = "use-esplora-ureq")]
|
||||||
mod ureq;
|
mod ureq;
|
||||||
|
|
||||||
#[cfg(feature = "ureq")]
|
#[cfg(feature = "use-esplora-ureq")]
|
||||||
pub use self::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`]
|
/// Configuration for an [`EsploraBlockchain`]
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
|
||||||
pub struct EsploraBlockchainConfig {
|
pub struct EsploraBlockchainConfig {
|
||||||
@ -138,16 +74,11 @@ impl EsploraBlockchainConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for EsploraError {}
|
impl From<esplora_client::BlockTime> for crate::BlockTime {
|
||||||
|
fn from(esplora_client::BlockTime { timestamp, height }: esplora_client::BlockTime) -> Self {
|
||||||
#[cfg(feature = "ureq")]
|
Self { timestamp, height }
|
||||||
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);
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[cfg(feature = "test-esplora")]
|
#[cfg(feature = "test-esplora")]
|
||||||
@ -161,58 +92,11 @@ const DEFAULT_CONCURRENT_REQUESTS: u8 = 4;
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod 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]
|
#[test]
|
||||||
#[cfg(feature = "test-esplora")]
|
#[cfg(feature = "test-esplora")]
|
||||||
fn test_esplora_with_variable_configs() {
|
fn test_esplora_with_variable_configs() {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
use crate::testutils::{
|
use crate::testutils::{
|
||||||
blockchain_tests::TestClient,
|
blockchain_tests::TestClient,
|
||||||
configurable_blockchain_tests::ConfigurableBlockchainTester,
|
configurable_blockchain_tests::ConfigurableBlockchainTester,
|
||||||
|
@ -14,49 +14,36 @@
|
|||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
use bitcoin::consensus::{deserialize, serialize};
|
use bitcoin::{Transaction, Txid};
|
||||||
use bitcoin::hashes::hex::{FromHex, ToHex};
|
|
||||||
use bitcoin::hashes::{sha256, Hash};
|
|
||||||
use bitcoin::{BlockHeader, Script, Transaction, Txid};
|
|
||||||
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use log::{debug, error, info, trace};
|
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 futures::stream::{FuturesOrdered, TryStreamExt};
|
||||||
|
|
||||||
use super::api::Tx;
|
|
||||||
use crate::blockchain::esplora::EsploraError;
|
|
||||||
use crate::blockchain::*;
|
use crate::blockchain::*;
|
||||||
use crate::database::BatchDatabase;
|
use crate::database::BatchDatabase;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::FeeRate;
|
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
|
/// Structure that implements the logic to sync with Esplora
|
||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
/// See the [`blockchain::esplora`](crate::blockchain::esplora) module for a usage example.
|
/// See the [`blockchain::esplora`](crate::blockchain::esplora) module for a usage example.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EsploraBlockchain {
|
pub struct EsploraBlockchain {
|
||||||
url_client: UrlClient,
|
url_client: AsyncClient,
|
||||||
stop_gap: usize,
|
stop_gap: usize,
|
||||||
|
concurrency: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::convert::From<UrlClient> for EsploraBlockchain {
|
impl std::convert::From<AsyncClient> for EsploraBlockchain {
|
||||||
fn from(url_client: UrlClient) -> Self {
|
fn from(url_client: AsyncClient) -> Self {
|
||||||
EsploraBlockchain {
|
EsploraBlockchain {
|
||||||
url_client,
|
url_client,
|
||||||
stop_gap: 20,
|
stop_gap: 20,
|
||||||
|
concurrency: super::DEFAULT_CONCURRENT_REQUESTS,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,19 +51,25 @@ impl std::convert::From<UrlClient> for EsploraBlockchain {
|
|||||||
impl EsploraBlockchain {
|
impl EsploraBlockchain {
|
||||||
/// Create a new instance of the client from a base URL and `stop_gap`.
|
/// Create a new instance of the client from a base URL and `stop_gap`.
|
||||||
pub fn new(base_url: &str, stop_gap: usize) -> Self {
|
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 {
|
EsploraBlockchain {
|
||||||
url_client: UrlClient {
|
url_client,
|
||||||
url: base_url.to_string(),
|
|
||||||
client: Client::new(),
|
|
||||||
concurrency: super::DEFAULT_CONCURRENT_REQUESTS,
|
|
||||||
},
|
|
||||||
stop_gap,
|
stop_gap,
|
||||||
|
concurrency: super::DEFAULT_CONCURRENT_REQUESTS,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the concurrency to use when doing batch queries against the Esplora instance.
|
/// Set the concurrency to use when doing batch queries against the Esplora instance.
|
||||||
pub fn with_concurrency(mut self, concurrency: u8) -> Self {
|
pub fn with_concurrency(mut self, concurrency: u8) -> Self {
|
||||||
self.url_client.concurrency = concurrency;
|
self.concurrency = concurrency;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,17 +87,19 @@ impl Blockchain for EsploraBlockchain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
|
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> {
|
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
|
||||||
let estimates = await_or_block!(self.url_client._get_fee_estimates())?;
|
let estimates = await_or_block!(self.url_client.get_fee_estimates())?;
|
||||||
super::into_fee_rate(target, estimates)
|
Ok(FeeRate::from_sat_per_vb(convert_fee_rate(
|
||||||
|
target, estimates,
|
||||||
|
)?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for EsploraBlockchain {
|
impl Deref for EsploraBlockchain {
|
||||||
type Target = UrlClient;
|
type Target = AsyncClient;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.url_client
|
&self.url_client
|
||||||
@ -116,21 +111,21 @@ impl StatelessBlockchain for EsploraBlockchain {}
|
|||||||
#[maybe_async]
|
#[maybe_async]
|
||||||
impl GetHeight for EsploraBlockchain {
|
impl GetHeight for EsploraBlockchain {
|
||||||
fn get_height(&self) -> Result<u32, Error> {
|
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]
|
#[maybe_async]
|
||||||
impl GetTx for EsploraBlockchain {
|
impl GetTx for EsploraBlockchain {
|
||||||
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
|
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]
|
#[maybe_async]
|
||||||
impl GetBlockHash for EsploraBlockchain {
|
impl GetBlockHash for EsploraBlockchain {
|
||||||
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
|
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())
|
Ok(block_header.block_hash())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -151,10 +146,10 @@ impl WalletSync for EsploraBlockchain {
|
|||||||
Request::Script(script_req) => {
|
Request::Script(script_req) => {
|
||||||
let futures: FuturesOrdered<_> = script_req
|
let futures: FuturesOrdered<_> = script_req
|
||||||
.request()
|
.request()
|
||||||
.take(self.url_client.concurrency as usize)
|
.take(self.concurrency as usize)
|
||||||
.map(|script| async move {
|
.map(|script| async move {
|
||||||
let mut related_txs: Vec<Tx> =
|
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 =
|
let n_confirmed =
|
||||||
related_txs.iter().filter(|tx| tx.status.confirmed).count();
|
related_txs.iter().filter(|tx| tx.status.confirmed).count();
|
||||||
@ -164,7 +159,7 @@ impl WalletSync for EsploraBlockchain {
|
|||||||
loop {
|
loop {
|
||||||
let new_related_txs: Vec<Tx> = self
|
let new_related_txs: Vec<Tx> = self
|
||||||
.url_client
|
.url_client
|
||||||
._scripthash_txs(
|
.scripthash_txs(
|
||||||
script,
|
script,
|
||||||
Some(related_txs.last().unwrap().txid),
|
Some(related_txs.last().unwrap().txid),
|
||||||
)
|
)
|
||||||
@ -204,6 +199,7 @@ impl WalletSync for EsploraBlockchain {
|
|||||||
.get(txid)
|
.get(txid)
|
||||||
.expect("must be in index")
|
.expect("must be in index")
|
||||||
.confirmation_time()
|
.confirmation_time()
|
||||||
|
.map(Into::into)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
conftime_req.satisfy(conftimes)?
|
conftime_req.satisfy(conftimes)?
|
||||||
@ -227,132 +223,26 @@ impl WalletSync for EsploraBlockchain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
impl ConfigurableBlockchain for EsploraBlockchain {
|
||||||
type Config = super::EsploraBlockchainConfig;
|
type Config = super::EsploraBlockchainConfig;
|
||||||
|
|
||||||
fn from_config(config: &Self::Config) -> Result<Self, Error> {
|
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;
|
|
||||||
}
|
|
||||||
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 {
|
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)
|
Ok(blockchain)
|
||||||
}
|
}
|
||||||
|
@ -12,42 +12,26 @@
|
|||||||
//! Esplora by way of `ureq` HTTP client.
|
//! Esplora by way of `ureq` HTTP client.
|
||||||
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::io;
|
|
||||||
use std::io::Read;
|
|
||||||
use std::ops::Deref;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use log::{debug, error, info, trace};
|
use log::{debug, error, info, trace};
|
||||||
|
|
||||||
use ureq::{Agent, Proxy, Response};
|
use bitcoin::{Transaction, Txid};
|
||||||
|
|
||||||
use bitcoin::consensus::{deserialize, serialize};
|
use esplora_client::{convert_fee_rate, BlockingClient, Builder, Tx};
|
||||||
use bitcoin::hashes::hex::{FromHex, ToHex};
|
|
||||||
use bitcoin::hashes::{sha256, Hash};
|
|
||||||
use bitcoin::{BlockHeader, Script, Transaction, Txid};
|
|
||||||
|
|
||||||
use super::api::Tx;
|
|
||||||
use crate::blockchain::esplora::EsploraError;
|
|
||||||
use crate::blockchain::*;
|
use crate::blockchain::*;
|
||||||
use crate::database::BatchDatabase;
|
use crate::database::BatchDatabase;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::FeeRate;
|
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
|
/// Structure that implements the logic to sync with Esplora
|
||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
/// See the [`blockchain::esplora`](crate::blockchain::esplora) module for a usage example.
|
/// See the [`blockchain::esplora`](crate::blockchain::esplora) module for a usage example.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EsploraBlockchain {
|
pub struct EsploraBlockchain {
|
||||||
url_client: UrlClient,
|
url_client: BlockingClient,
|
||||||
stop_gap: usize,
|
stop_gap: usize,
|
||||||
concurrency: u8,
|
concurrency: u8,
|
||||||
}
|
}
|
||||||
@ -55,22 +39,22 @@ pub struct EsploraBlockchain {
|
|||||||
impl EsploraBlockchain {
|
impl EsploraBlockchain {
|
||||||
/// Create a new instance of the client from a base URL and the `stop_gap`.
|
/// 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 {
|
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 {
|
EsploraBlockchain {
|
||||||
url_client: UrlClient {
|
url_client,
|
||||||
url: base_url.to_string(),
|
|
||||||
agent: Agent::new(),
|
|
||||||
},
|
|
||||||
concurrency: super::DEFAULT_CONCURRENT_REQUESTS,
|
concurrency: super::DEFAULT_CONCURRENT_REQUESTS,
|
||||||
stop_gap,
|
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.
|
/// Set the number of parallel requests the client can make.
|
||||||
pub fn with_concurrency(mut self, concurrency: u8) -> Self {
|
pub fn with_concurrency(mut self, concurrency: u8) -> Self {
|
||||||
self.concurrency = concurrency;
|
self.concurrency = concurrency;
|
||||||
@ -90,18 +74,20 @@ impl Blockchain for EsploraBlockchain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
|
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
|
||||||
self.url_client._broadcast(tx)?;
|
self.url_client.broadcast(tx)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
|
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
|
||||||
let estimates = self.url_client._get_fee_estimates()?;
|
let estimates = self.url_client.get_fee_estimates()?;
|
||||||
super::into_fee_rate(target, estimates)
|
Ok(FeeRate::from_sat_per_vb(convert_fee_rate(
|
||||||
|
target, estimates,
|
||||||
|
)?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for EsploraBlockchain {
|
impl Deref for EsploraBlockchain {
|
||||||
type Target = UrlClient;
|
type Target = BlockingClient;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.url_client
|
&self.url_client
|
||||||
@ -112,19 +98,19 @@ impl StatelessBlockchain for EsploraBlockchain {}
|
|||||||
|
|
||||||
impl GetHeight for EsploraBlockchain {
|
impl GetHeight for EsploraBlockchain {
|
||||||
fn get_height(&self) -> Result<u32, Error> {
|
fn get_height(&self) -> Result<u32, Error> {
|
||||||
Ok(self.url_client._get_height()?)
|
Ok(self.url_client.get_height()?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GetTx for EsploraBlockchain {
|
impl GetTx for EsploraBlockchain {
|
||||||
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
|
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 {
|
impl GetBlockHash for EsploraBlockchain {
|
||||||
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
|
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())
|
Ok(block_header.block_hash())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -151,7 +137,7 @@ impl WalletSync for EsploraBlockchain {
|
|||||||
let client = self.url_client.clone();
|
let client = self.url_client.clone();
|
||||||
// make each request in its own thread.
|
// make each request in its own thread.
|
||||||
handles.push(std::thread::spawn(move || {
|
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 =
|
let n_confirmed =
|
||||||
related_txs.iter().filter(|tx| tx.status.confirmed).count();
|
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.
|
// keep requesting to see if there's more.
|
||||||
if n_confirmed >= 25 {
|
if n_confirmed >= 25 {
|
||||||
loop {
|
loop {
|
||||||
let new_related_txs: Vec<Tx> = client._scripthash_txs(
|
let new_related_txs: Vec<Tx> = client.scripthash_txs(
|
||||||
&script,
|
&script,
|
||||||
Some(related_txs.last().unwrap().txid),
|
Some(related_txs.last().unwrap().txid),
|
||||||
)?;
|
)?;
|
||||||
@ -202,6 +188,7 @@ impl WalletSync for EsploraBlockchain {
|
|||||||
.get(txid)
|
.get(txid)
|
||||||
.expect("must be in index")
|
.expect("must be in index")
|
||||||
.confirmation_time()
|
.confirmation_time()
|
||||||
|
.map(Into::into)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
conftime_req.satisfy(conftimes)?
|
conftime_req.satisfy(conftimes)?
|
||||||
@ -226,159 +213,22 @@ impl WalletSync for EsploraBlockchain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
impl ConfigurableBlockchain for EsploraBlockchain {
|
||||||
type Config = super::EsploraBlockchainConfig;
|
type Config = super::EsploraBlockchainConfig;
|
||||||
|
|
||||||
fn from_config(config: &Self::Config) -> Result<Self, Error> {
|
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 {
|
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 {
|
if let Some(proxy) = &config.proxy {
|
||||||
agent_builder = agent_builder
|
builder = builder.proxy(proxy);
|
||||||
.proxy(Proxy::new(proxy).map_err(|e| Error::Esplora(Box::new(e.into())))?);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut blockchain = EsploraBlockchain::new(config.base_url.as_str(), config.stop_gap)
|
let mut blockchain =
|
||||||
.with_agent(agent_builder.build());
|
EsploraBlockchain::from_client(builder.build_blocking()?, config.stop_gap);
|
||||||
|
|
||||||
if let Some(concurrency) = config.concurrency {
|
if let Some(concurrency) = config.concurrency {
|
||||||
blockchain = blockchain.with_concurrency(concurrency);
|
blockchain = blockchain.with_concurrency(concurrency);
|
||||||
@ -387,12 +237,3 @@ impl ConfigurableBlockchain for EsploraBlockchain {
|
|||||||
Ok(blockchain)
|
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user