@@ -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_client:: client_sync:: { TemplateRequest , TemplateRules } , corepc_node:: anyhow:: Context } ;
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,21 @@ 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 { rules : vec ! [ TemplateRules :: Segwit ] } ;
132
+ let bt = self . bitcoind . client . get_block_template ( & request) ?;
142
133
143
134
let txdata = vec ! [ Transaction {
144
135
version: transaction:: Version :: ONE ,
@@ -147,7 +138,7 @@ impl TestEnv {
147
138
previous_output: bdk_chain:: bitcoin:: OutPoint :: default ( ) ,
148
139
script_sig: ScriptBuf :: builder( )
149
140
. push_int( bt. height as _)
150
- // randomn number so that re-mining creates unique block
141
+ // random number so that re-mining creates unique block
151
142
. push_int( random( ) )
152
143
. into_script( ) ,
153
144
sequence: bdk_chain:: bitcoin:: Sequence :: default ( ) ,
@@ -159,18 +150,19 @@ impl TestEnv {
159
150
} ] ,
160
151
} ] ;
161
152
162
- let bits: [ u8 ; 4 ] = bt
163
- . bits
164
- . clone ( )
165
- . try_into ( )
166
- . expect ( "rpc provided us with invalid bits" ) ;
153
+ // TODO: (@leonardo) double-check if an `.into_bytes()` wouldn't be enough instead.
154
+ let bits: [ u8 ; 4 ] =
155
+ bdk_chain:: bitcoin:: consensus:: encode:: deserialize_hex :: < Vec < u8 > > ( & bt. bits ) ?
156
+ . clone ( )
157
+ . try_into ( )
158
+ . expect ( "rpc provided us with invalid bits" ) ;
167
159
168
160
let mut block = Block {
169
161
header : Header {
170
162
version : bdk_chain:: bitcoin:: block:: Version :: default ( ) ,
171
- prev_blockhash : bt. previous_block_hash ,
163
+ prev_blockhash : BlockHash :: from_str ( & bt. previous_block_hash ) ? ,
172
164
merkle_root : TxMerkleNode :: all_zeros ( ) ,
173
- time : Ord :: max ( bt. min_time , std:: time:: UNIX_EPOCH . elapsed ( ) ?. as_secs ( ) ) as u32 ,
165
+ time : Ord :: max ( bt. min_time , std:: time:: UNIX_EPOCH . elapsed ( ) ?. as_secs ( ) as u32 ) ,
174
166
bits : CompactTarget :: from_consensus ( u32:: from_be_bytes ( bits) ) ,
175
167
nonce : 0 ,
176
168
} ,
@@ -187,6 +179,7 @@ impl TestEnv {
187
179
}
188
180
189
181
self . bitcoind . client . submit_block ( & block) ?;
182
+
190
183
Ok ( ( bt. height as usize , block. block_hash ( ) ) )
191
184
}
192
185
@@ -237,18 +230,16 @@ impl TestEnv {
237
230
238
231
/// Invalidate a number of blocks of a given size `count`.
239
232
pub fn invalidate_blocks ( & self , count : usize ) -> anyhow:: Result < ( ) > {
240
- let mut hash = self . bitcoind . client . get_best_block_hash ( ) ?;
233
+ let mut hash = self . bitcoind . client . get_best_block_hash ( ) ?. block_hash ( ) ? ;
241
234
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
- }
235
+ let prev_hash = self . bitcoind . client . get_block ( hash) ?. header . prev_blockhash ;
236
+ self . bitcoind . client . invalidate_block ( hash) ?;
237
+ hash = prev_hash
238
+ // TODO: (@leonardo) It requires a double check if there is any side-effect with this break removal.
239
+ // match prev_hash {
240
+ // Some(prev_hash) => hash = prev_hash,
241
+ // None => break,
242
+ // }
252
243
}
253
244
Ok ( ( ) )
254
245
}
@@ -289,7 +280,8 @@ impl TestEnv {
289
280
let txid = self
290
281
. bitcoind
291
282
. client
292
- . send_to_address ( address, amount, None , None , None , None , None , None ) ?;
283
+ . send_to_address ( address, amount) ?
284
+ . txid ( ) ?;
293
285
Ok ( txid)
294
286
}
295
287
@@ -300,14 +292,19 @@ impl TestEnv {
300
292
. client
301
293
. get_block_hash ( height as u64 )
302
294
. ok ( )
303
- . map ( |hash| BlockId { height, hash } )
295
+ . map ( |get_block_hash| {
296
+ let hash = get_block_hash
297
+ . block_hash ( )
298
+ . expect ( "should `successfully convert to `BlockHash` from `GetBlockHash`" ) ;
299
+ BlockId { height, hash }
300
+ } )
304
301
} ) )
305
302
. expect ( "must craft tip" )
306
303
}
307
304
308
305
/// Get the genesis hash of the blockchain.
309
306
pub fn genesis_hash ( & self ) -> anyhow:: Result < BlockHash > {
310
- let hash = self . bitcoind . client . get_block_hash ( 0 ) ?;
307
+ let hash = self . bitcoind . client . get_block_hash ( 0 ) ?. into_model ( ) ? . 0 ;
311
308
Ok ( hash)
312
309
}
313
310
}
@@ -317,7 +314,7 @@ impl TestEnv {
317
314
mod test {
318
315
use crate :: TestEnv ;
319
316
use core:: time:: Duration ;
320
- use electrsd:: bitcoind :: { anyhow:: Result , bitcoincore_rpc :: RpcApi } ;
317
+ use electrsd:: corepc_node :: anyhow:: Result ;
321
318
322
319
/// This checks that reorgs initiated by `bitcoind` is detected by our `electrsd` instance.
323
320
#[ test]
@@ -327,15 +324,15 @@ mod test {
327
324
// Mine some blocks.
328
325
env. mine_blocks ( 101 , None ) ?;
329
326
env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
330
- let height = env. bitcoind . client . get_block_count ( ) ?;
327
+ let height = env. bitcoind . client . get_block_count ( ) ?. into_model ( ) . 0 ;
331
328
let blocks = ( 0 ..=height)
332
329
. map ( |i| env. bitcoind . client . get_block_hash ( i) )
333
330
. collect :: < Result < Vec < _ > , _ > > ( ) ?;
334
331
335
332
// Perform reorg on six blocks.
336
333
env. reorg ( 6 ) ?;
337
334
env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
338
- let reorged_height = env. bitcoind . client . get_block_count ( ) ?;
335
+ let reorged_height = env. bitcoind . client . get_block_count ( ) ?. into_model ( ) . 0 ;
339
336
let reorged_blocks = ( 0 ..=height)
340
337
. map ( |i| env. bitcoind . client . get_block_hash ( i) )
341
338
. collect :: < Result < Vec < _ > , _ > > ( ) ?;
0 commit comments