@@ -11,32 +11,28 @@ use bdk_chain::{
11
11
local_chain:: CheckPoint ,
12
12
BlockId ,
13
13
} ;
14
- use bitcoincore_rpc:: {
15
- bitcoincore_rpc_json:: { GetBlockTemplateModes , GetBlockTemplateRules } ,
16
- RpcApi ,
17
- } ;
18
- use electrsd:: bitcoind:: anyhow:: Context ;
14
+ use electrsd:: corepc_node:: { anyhow:: Context , TemplateRequest , TemplateRules } ;
19
15
20
16
pub use electrsd;
21
- pub use electrsd:: bitcoind ;
22
- pub use electrsd:: bitcoind :: anyhow ;
23
- pub use electrsd:: bitcoind :: bitcoincore_rpc ;
17
+ pub use electrsd:: corepc_client ;
18
+ pub use electrsd:: corepc_node ;
19
+ pub use electrsd:: corepc_node :: anyhow ;
24
20
pub use electrsd:: electrum_client;
25
21
use electrsd:: electrum_client:: ElectrumApi ;
26
- use std:: time:: Duration ;
22
+ use std:: { str :: FromStr , time:: Duration } ;
27
23
28
24
/// Struct for running a regtest environment with a single `bitcoind` node with an `electrs`
29
25
/// instance connected to it.
30
26
pub struct TestEnv {
31
- pub bitcoind : electrsd:: bitcoind :: BitcoinD ,
27
+ pub bitcoind : electrsd:: corepc_node :: Node ,
32
28
pub electrsd : electrsd:: ElectrsD ,
33
29
}
34
30
35
31
/// Configuration parameters.
36
32
#[ derive( Debug ) ]
37
33
pub struct Config < ' a > {
38
34
/// [`bitcoind::Conf`]
39
- pub bitcoind : bitcoind :: Conf < ' a > ,
35
+ pub bitcoind : corepc_node :: Conf < ' a > ,
40
36
/// [`electrsd::Conf`]
41
37
pub electrsd : electrsd:: Conf < ' a > ,
42
38
}
@@ -46,7 +42,7 @@ impl Default for Config<'_> {
46
42
/// which is required for testing `bdk_esplora`.
47
43
fn default ( ) -> Self {
48
44
Self {
49
- bitcoind : bitcoind :: Conf :: default ( ) ,
45
+ bitcoind : corepc_node :: Conf :: default ( ) ,
50
46
electrsd : {
51
47
let mut conf = electrsd:: Conf :: default ( ) ;
52
48
conf. http_enabled = true ;
@@ -66,11 +62,11 @@ impl TestEnv {
66
62
pub fn new_with_config ( config : Config ) -> anyhow:: Result < Self > {
67
63
let bitcoind_exe = match std:: env:: var ( "BITCOIND_EXE" ) {
68
64
Ok ( path) => path,
69
- Err ( _) => bitcoind :: downloaded_exe_path ( ) . context (
65
+ Err ( _) => corepc_node :: downloaded_exe_path ( ) . context (
70
66
"you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature" ,
71
67
) ?,
72
68
} ;
73
- let bitcoind = bitcoind :: BitcoinD :: with_conf ( bitcoind_exe, & config. bitcoind ) ?;
69
+ let bitcoind = corepc_node :: Node :: with_conf ( bitcoind_exe, & config. bitcoind ) ?;
74
70
75
71
let electrs_exe = match std:: env:: var ( "ELECTRS_EXE" ) {
76
72
Ok ( path) => path,
@@ -88,7 +84,7 @@ impl TestEnv {
88
84
}
89
85
90
86
/// Exposes the [`RpcApi`] calls from [`bitcoincore_rpc`].
91
- pub fn rpc_client ( & self ) -> & impl RpcApi {
87
+ pub fn rpc_client ( & self ) -> & corepc_node :: Client {
92
88
& self . bitcoind . client
93
89
}
94
90
@@ -119,26 +115,23 @@ impl TestEnv {
119
115
) -> anyhow:: Result < Vec < BlockHash > > {
120
116
let coinbase_address = match address {
121
117
Some ( address) => address,
122
- None => self
123
- . bitcoind
124
- . client
125
- . get_new_address ( None , None ) ?
126
- . assume_checked ( ) ,
118
+ None => self . bitcoind . client . new_address ( ) ?,
127
119
} ;
128
120
let block_hashes = self
129
121
. bitcoind
130
122
. client
131
- . generate_to_address ( count as _ , & coinbase_address) ?;
123
+ . generate_to_address ( count as _ , & coinbase_address) ?
124
+ . into_model ( ) ?
125
+ . 0 ;
132
126
Ok ( block_hashes)
133
127
}
134
128
135
129
/// Mine a block that is guaranteed to be empty even with transactions in the mempool.
136
130
pub fn mine_empty_block ( & self ) -> anyhow:: Result < ( usize , BlockHash ) > {
137
- let bt = self . bitcoind . client . get_block_template (
138
- GetBlockTemplateModes :: Template ,
139
- & [ GetBlockTemplateRules :: SegWit ] ,
140
- & [ ] ,
141
- ) ?;
131
+ let request = TemplateRequest {
132
+ rules : vec ! [ TemplateRules :: Segwit ] ,
133
+ } ;
134
+ let bt = self . bitcoind . client . get_block_template ( & request) ?;
142
135
143
136
let txdata = vec ! [ Transaction {
144
137
version: transaction:: Version :: ONE ,
@@ -147,7 +140,7 @@ impl TestEnv {
147
140
previous_output: bdk_chain:: bitcoin:: OutPoint :: default ( ) ,
148
141
script_sig: ScriptBuf :: builder( )
149
142
. push_int( bt. height as _)
150
- // randomn number so that re-mining creates unique block
143
+ // random number so that re-mining creates unique block
151
144
. push_int( random( ) )
152
145
. into_script( ) ,
153
146
sequence: bdk_chain:: bitcoin:: Sequence :: default ( ) ,
@@ -159,18 +152,22 @@ impl TestEnv {
159
152
} ] ,
160
153
} ] ;
161
154
162
- let bits: [ u8 ; 4 ] = bt
163
- . bits
164
- . clone ( )
165
- . try_into ( )
166
- . expect ( "rpc provided us with invalid bits" ) ;
155
+ // TODO: (@leonardo) double-check if an `.into_bytes()` wouldn't be enough instead.
156
+ let bits: [ u8 ; 4 ] =
157
+ bdk_chain:: bitcoin:: consensus:: encode:: deserialize_hex :: < Vec < u8 > > ( & bt. bits ) ?
158
+ . clone ( )
159
+ . try_into ( )
160
+ . expect ( "rpc provided us with invalid bits" ) ;
167
161
168
162
let mut block = Block {
169
163
header : Header {
170
164
version : bdk_chain:: bitcoin:: block:: Version :: default ( ) ,
171
- prev_blockhash : bt. previous_block_hash ,
165
+ prev_blockhash : BlockHash :: from_str ( & bt. previous_block_hash ) ? ,
172
166
merkle_root : TxMerkleNode :: all_zeros ( ) ,
173
- time : Ord :: max ( bt. min_time , std:: time:: UNIX_EPOCH . elapsed ( ) ?. as_secs ( ) ) as u32 ,
167
+ time : Ord :: max (
168
+ bt. min_time ,
169
+ std:: time:: UNIX_EPOCH . elapsed ( ) ?. as_secs ( ) as u32 ,
170
+ ) ,
174
171
bits : CompactTarget :: from_consensus ( u32:: from_be_bytes ( bits) ) ,
175
172
nonce : 0 ,
176
173
} ,
@@ -187,6 +184,7 @@ impl TestEnv {
187
184
}
188
185
189
186
self . bitcoind . client . submit_block ( & block) ?;
187
+
190
188
Ok ( ( bt. height as usize , block. block_hash ( ) ) )
191
189
}
192
190
@@ -237,18 +235,16 @@ impl TestEnv {
237
235
238
236
/// Invalidate a number of blocks of a given size `count`.
239
237
pub fn invalidate_blocks ( & self , count : usize ) -> anyhow:: Result < ( ) > {
240
- let mut hash = self . bitcoind . client . get_best_block_hash ( ) ?;
238
+ let mut hash = self . bitcoind . client . get_best_block_hash ( ) ?. block_hash ( ) ? ;
241
239
for _ in 0 ..count {
242
- let prev_hash = self
243
- . bitcoind
244
- . client
245
- . get_block_info ( & hash) ?
246
- . previousblockhash ;
247
- self . bitcoind . client . invalidate_block ( & hash) ?;
248
- match prev_hash {
249
- Some ( prev_hash) => hash = prev_hash,
250
- None => break ,
251
- }
240
+ let prev_hash = self . bitcoind . client . get_block ( hash) ?. header . prev_blockhash ;
241
+ self . bitcoind . client . invalidate_block ( hash) ?;
242
+ hash = prev_hash
243
+ // TODO: (@leonardo) It requires a double check if there is any side-effect with this
244
+ // break removal. match prev_hash {
245
+ // Some(prev_hash) => hash = prev_hash,
246
+ // None => break,
247
+ // }
252
248
}
253
249
Ok ( ( ) )
254
250
}
@@ -289,7 +285,8 @@ impl TestEnv {
289
285
let txid = self
290
286
. bitcoind
291
287
. client
292
- . send_to_address ( address, amount, None , None , None , None , None , None ) ?;
288
+ . send_to_address ( address, amount) ?
289
+ . txid ( ) ?;
293
290
Ok ( txid)
294
291
}
295
292
@@ -300,14 +297,19 @@ impl TestEnv {
300
297
. client
301
298
. get_block_hash ( height as u64 )
302
299
. ok ( )
303
- . map ( |hash| BlockId { height, hash } )
300
+ . map ( |get_block_hash| {
301
+ let hash = get_block_hash
302
+ . block_hash ( )
303
+ . expect ( "should `successfully convert to `BlockHash` from `GetBlockHash`" ) ;
304
+ BlockId { height, hash }
305
+ } )
304
306
} ) )
305
307
. expect ( "must craft tip" )
306
308
}
307
309
308
310
/// Get the genesis hash of the blockchain.
309
311
pub fn genesis_hash ( & self ) -> anyhow:: Result < BlockHash > {
310
- let hash = self . bitcoind . client . get_block_hash ( 0 ) ?;
312
+ let hash = self . bitcoind . client . get_block_hash ( 0 ) ?. into_model ( ) ? . 0 ;
311
313
Ok ( hash)
312
314
}
313
315
}
@@ -317,7 +319,7 @@ impl TestEnv {
317
319
mod test {
318
320
use crate :: TestEnv ;
319
321
use core:: time:: Duration ;
320
- use electrsd:: bitcoind :: { anyhow:: Result , bitcoincore_rpc :: RpcApi } ;
322
+ use electrsd:: corepc_node :: anyhow:: Result ;
321
323
322
324
/// This checks that reorgs initiated by `bitcoind` is detected by our `electrsd` instance.
323
325
#[ test]
@@ -327,15 +329,15 @@ mod test {
327
329
// Mine some blocks.
328
330
env. mine_blocks ( 101 , None ) ?;
329
331
env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
330
- let height = env. bitcoind . client . get_block_count ( ) ?;
332
+ let height = env. bitcoind . client . get_block_count ( ) ?. into_model ( ) . 0 ;
331
333
let blocks = ( 0 ..=height)
332
334
. map ( |i| env. bitcoind . client . get_block_hash ( i) )
333
335
. collect :: < Result < Vec < _ > , _ > > ( ) ?;
334
336
335
337
// Perform reorg on six blocks.
336
338
env. reorg ( 6 ) ?;
337
339
env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
338
- let reorged_height = env. bitcoind . client . get_block_count ( ) ?;
340
+ let reorged_height = env. bitcoind . client . get_block_count ( ) ?. into_model ( ) . 0 ;
339
341
let reorged_blocks = ( 0 ..=height)
340
342
. map ( |i| env. bitcoind . client . get_block_hash ( i) )
341
343
. collect :: < Result < Vec < _ > , _ > > ( ) ?;
0 commit comments