Skip to content

Commit 5baf46f

Browse files
committed
Use the external esplora client library
1 parent 92ad487 commit 5baf46f

File tree

5 files changed

+88
-591
lines changed

5 files changed

+88
-591
lines changed

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ rand = "^0.7"
2323
# Optional dependencies
2424
sled = { version = "0.34", optional = true }
2525
electrum-client = { version = "0.11", optional = true }
26+
esplora-client = { version = "0.1.1", default-features = false, optional = true }
2627
rusqlite = { version = "0.27.0", optional = true }
2728
ahash = { version = "0.7.6", optional = true }
28-
reqwest = { version = "0.11", optional = true, default-features = false, features = ["json"] }
29-
ureq = { version = "~2.2.0", features = ["json"], optional = true }
3029
futures = { version = "0.3", optional = true }
3130
async-trait = { version = "0.1", optional = true }
3231
rocksdb = { version = "0.14", default-features = false, features = ["snappy"], optional = true }
@@ -79,13 +78,13 @@ hardware-signer = ["hwi"]
7978
async-interface = ["async-trait"]
8079
electrum = ["electrum-client"]
8180
# MUST ALSO USE `--no-default-features`.
82-
use-esplora-reqwest = ["esplora", "reqwest", "reqwest/socks", "futures"]
83-
use-esplora-ureq = ["esplora", "ureq", "ureq/socks"]
81+
use-esplora-reqwest = ["esplora", "esplora-client/async", "futures"]
82+
use-esplora-ureq = ["esplora", "esplora-client/blocking"]
8483
# Typical configurations will not need to use `esplora` feature directly.
8584
esplora = []
8685

8786
# Use below feature with `use-esplora-reqwest` to enable reqwest default TLS support
88-
reqwest-default-tls = ["reqwest/default-tls"]
87+
reqwest-default-tls = ["esplora-client/async-https"]
8988

9089
# Debug/Test features
9190
test-blockchains = ["bitcoincore-rpc", "electrum-client"]

src/blockchain/esplora/api.rs

Lines changed: 0 additions & 117 deletions
This file was deleted.

src/blockchain/esplora/mod.rs

Lines changed: 12 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -17,85 +17,21 @@
1717
//! Please note, to configure the Esplora HTTP client correctly use one of:
1818
//! Blocking: --features='esplora,ureq'
1919
//! Async: --features='async-interface,esplora,reqwest' --no-default-features
20-
use std::collections::HashMap;
21-
use std::fmt;
22-
use std::io;
2320
24-
use bitcoin::consensus;
25-
use bitcoin::{BlockHash, Txid};
21+
pub use esplora_client::Error as EsploraError;
2622

27-
use crate::error::Error;
28-
use crate::FeeRate;
29-
30-
#[cfg(feature = "reqwest")]
23+
#[cfg(feature = "use-esplora-reqwest")]
3124
mod reqwest;
3225

33-
#[cfg(feature = "reqwest")]
26+
#[cfg(feature = "use-esplora-reqwest")]
3427
pub use self::reqwest::*;
3528

36-
#[cfg(feature = "ureq")]
29+
#[cfg(feature = "use-esplora-ureq")]
3730
mod ureq;
3831

39-
#[cfg(feature = "ureq")]
32+
#[cfg(feature = "use-esplora-ureq")]
4033
pub use self::ureq::*;
4134

42-
mod api;
43-
44-
fn into_fee_rate(target: usize, estimates: HashMap<String, f64>) -> Result<FeeRate, Error> {
45-
let fee_val = {
46-
let mut pairs = estimates
47-
.into_iter()
48-
.filter_map(|(k, v)| Some((k.parse::<usize>().ok()?, v)))
49-
.collect::<Vec<_>>();
50-
pairs.sort_unstable_by_key(|(k, _)| std::cmp::Reverse(*k));
51-
pairs
52-
.into_iter()
53-
.find(|(k, _)| k <= &target)
54-
.map(|(_, v)| v)
55-
.unwrap_or(1.0)
56-
};
57-
Ok(FeeRate::from_sat_per_vb(fee_val as f32))
58-
}
59-
60-
/// Errors that can happen during a sync with [`EsploraBlockchain`]
61-
#[derive(Debug)]
62-
pub enum EsploraError {
63-
/// Error during ureq HTTP request
64-
#[cfg(feature = "ureq")]
65-
Ureq(::ureq::Error),
66-
/// Transport error during the ureq HTTP call
67-
#[cfg(feature = "ureq")]
68-
UreqTransport(::ureq::Transport),
69-
/// Error during reqwest HTTP request
70-
#[cfg(feature = "reqwest")]
71-
Reqwest(::reqwest::Error),
72-
/// HTTP response error
73-
HttpResponse(u16),
74-
/// IO error during ureq response read
75-
Io(io::Error),
76-
/// No header found in ureq response
77-
NoHeader,
78-
/// Invalid number returned
79-
Parsing(std::num::ParseIntError),
80-
/// Invalid Bitcoin data returned
81-
BitcoinEncoding(bitcoin::consensus::encode::Error),
82-
/// Invalid Hex data returned
83-
Hex(bitcoin::hashes::hex::Error),
84-
85-
/// Transaction not found
86-
TransactionNotFound(Txid),
87-
/// Header height not found
88-
HeaderHeightNotFound(u32),
89-
/// Header hash not found
90-
HeaderHashNotFound(BlockHash),
91-
}
92-
93-
impl fmt::Display for EsploraError {
94-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95-
write!(f, "{:?}", self)
96-
}
97-
}
98-
9935
/// Configuration for an [`EsploraBlockchain`]
10036
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
10137
pub struct EsploraBlockchainConfig {
@@ -138,16 +74,11 @@ impl EsploraBlockchainConfig {
13874
}
13975
}
14076

141-
impl std::error::Error for EsploraError {}
142-
143-
#[cfg(feature = "ureq")]
144-
impl_error!(::ureq::Transport, UreqTransport, EsploraError);
145-
#[cfg(feature = "reqwest")]
146-
impl_error!(::reqwest::Error, Reqwest, EsploraError);
147-
impl_error!(io::Error, Io, EsploraError);
148-
impl_error!(std::num::ParseIntError, Parsing, EsploraError);
149-
impl_error!(consensus::encode::Error, BitcoinEncoding, EsploraError);
150-
impl_error!(bitcoin::hashes::hex::Error, Hex, EsploraError);
77+
impl From<esplora_client::BlockTime> for crate::BlockTime {
78+
fn from(esplora_client::BlockTime { timestamp, height }: esplora_client::BlockTime) -> Self {
79+
Self { timestamp, height }
80+
}
81+
}
15182

15283
#[cfg(test)]
15384
#[cfg(feature = "test-esplora")]
@@ -161,58 +92,11 @@ const DEFAULT_CONCURRENT_REQUESTS: u8 = 4;
16192

16293
#[cfg(test)]
16394
mod test {
164-
use super::*;
165-
166-
#[test]
167-
fn feerate_parsing() {
168-
let esplora_fees = serde_json::from_str::<HashMap<String, f64>>(
169-
r#"{
170-
"25": 1.015,
171-
"5": 2.3280000000000003,
172-
"12": 2.0109999999999997,
173-
"15": 1.018,
174-
"17": 1.018,
175-
"11": 2.0109999999999997,
176-
"3": 3.01,
177-
"2": 4.9830000000000005,
178-
"6": 2.2359999999999998,
179-
"21": 1.018,
180-
"13": 1.081,
181-
"7": 2.2359999999999998,
182-
"8": 2.2359999999999998,
183-
"16": 1.018,
184-
"20": 1.018,
185-
"22": 1.017,
186-
"23": 1.017,
187-
"504": 1,
188-
"9": 2.2359999999999998,
189-
"14": 1.018,
190-
"10": 2.0109999999999997,
191-
"24": 1.017,
192-
"1008": 1,
193-
"1": 4.9830000000000005,
194-
"4": 2.3280000000000003,
195-
"19": 1.018,
196-
"144": 1,
197-
"18": 1.018
198-
}
199-
"#,
200-
)
201-
.unwrap();
202-
assert_eq!(
203-
into_fee_rate(6, esplora_fees.clone()).unwrap(),
204-
FeeRate::from_sat_per_vb(2.236)
205-
);
206-
assert_eq!(
207-
into_fee_rate(26, esplora_fees).unwrap(),
208-
FeeRate::from_sat_per_vb(1.015),
209-
"should inherit from value for 25"
210-
);
211-
}
212-
21395
#[test]
21496
#[cfg(feature = "test-esplora")]
21597
fn test_esplora_with_variable_configs() {
98+
use super::*;
99+
216100
use crate::testutils::{
217101
blockchain_tests::TestClient,
218102
configurable_blockchain_tests::ConfigurableBlockchainTester,

0 commit comments

Comments
 (0)