17
17
//! Please note, to configure the Esplora HTTP client correctly use one of:
18
18
//! Blocking: --features='esplora,ureq'
19
19
//! Async: --features='async-interface,esplora,reqwest' --no-default-features
20
- use std:: collections:: HashMap ;
21
- use std:: fmt;
22
- use std:: io;
23
20
24
- use bitcoin:: consensus;
25
- use bitcoin:: { BlockHash , Txid } ;
21
+ pub use esplora_client:: Error as EsploraError ;
26
22
27
- use crate :: error:: Error ;
28
- use crate :: FeeRate ;
29
-
30
- #[ cfg( feature = "reqwest" ) ]
23
+ #[ cfg( feature = "use-esplora-reqwest" ) ]
31
24
mod reqwest;
32
25
33
- #[ cfg( feature = "reqwest" ) ]
26
+ #[ cfg( feature = "use-esplora- reqwest" ) ]
34
27
pub use self :: reqwest:: * ;
35
28
36
- #[ cfg( feature = "ureq" ) ]
29
+ #[ cfg( feature = "use-esplora- ureq" ) ]
37
30
mod ureq;
38
31
39
- #[ cfg( feature = "ureq" ) ]
32
+ #[ cfg( feature = "use-esplora- ureq" ) ]
40
33
pub use self :: ureq:: * ;
41
34
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
-
99
35
/// Configuration for an [`EsploraBlockchain`]
100
36
#[ derive( Debug , serde:: Deserialize , serde:: Serialize , Clone , PartialEq ) ]
101
37
pub struct EsploraBlockchainConfig {
@@ -138,16 +74,11 @@ impl EsploraBlockchainConfig {
138
74
}
139
75
}
140
76
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
+ }
151
82
152
83
#[ cfg( test) ]
153
84
#[ cfg( feature = "test-esplora" ) ]
@@ -161,58 +92,11 @@ const DEFAULT_CONCURRENT_REQUESTS: u8 = 4;
161
92
162
93
#[ cfg( test) ]
163
94
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
-
213
95
#[ test]
214
96
#[ cfg( feature = "test-esplora" ) ]
215
97
fn test_esplora_with_variable_configs ( ) {
98
+ use super :: * ;
99
+
216
100
use crate :: testutils:: {
217
101
blockchain_tests:: TestClient ,
218
102
configurable_blockchain_tests:: ConfigurableBlockchainTester ,
0 commit comments