@@ -11,32 +11,28 @@ use bdk_chain::{
1111 local_chain:: CheckPoint ,
1212 BlockId ,
1313} ;
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 } ;
1915
2016pub 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 ;
2420pub use electrsd:: electrum_client;
2521use electrsd:: electrum_client:: ElectrumApi ;
26- use std:: time:: Duration ;
22+ use std:: { str :: FromStr , time:: Duration } ;
2723
2824/// Struct for running a regtest environment with a single `bitcoind` node with an `electrs`
2925/// instance connected to it.
3026pub struct TestEnv {
31- pub bitcoind : electrsd:: bitcoind :: BitcoinD ,
27+ pub bitcoind : electrsd:: corepc_node :: Node ,
3228 pub electrsd : electrsd:: ElectrsD ,
3329}
3430
3531/// Configuration parameters.
3632#[ derive( Debug ) ]
3733pub struct Config < ' a > {
3834 /// [`bitcoind::Conf`]
39- pub bitcoind : bitcoind :: Conf < ' a > ,
35+ pub bitcoind : corepc_node :: Conf < ' a > ,
4036 /// [`electrsd::Conf`]
4137 pub electrsd : electrsd:: Conf < ' a > ,
4238}
@@ -46,7 +42,7 @@ impl Default for Config<'_> {
4642 /// which is required for testing `bdk_esplora`.
4743 fn default ( ) -> Self {
4844 Self {
49- bitcoind : bitcoind :: Conf :: default ( ) ,
45+ bitcoind : corepc_node :: Conf :: default ( ) ,
5046 electrsd : {
5147 let mut conf = electrsd:: Conf :: default ( ) ;
5248 conf. http_enabled = true ;
@@ -66,11 +62,11 @@ impl TestEnv {
6662 pub fn new_with_config ( config : Config ) -> anyhow:: Result < Self > {
6763 let bitcoind_exe = match std:: env:: var ( "BITCOIND_EXE" ) {
6864 Ok ( path) => path,
69- Err ( _) => bitcoind :: downloaded_exe_path ( ) . context (
65+ Err ( _) => corepc_node :: downloaded_exe_path ( ) . context (
7066 "you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature" ,
7167 ) ?,
7268 } ;
73- let bitcoind = bitcoind :: BitcoinD :: with_conf ( bitcoind_exe, & config. bitcoind ) ?;
69+ let bitcoind = corepc_node :: Node :: with_conf ( bitcoind_exe, & config. bitcoind ) ?;
7470
7571 let electrs_exe = match std:: env:: var ( "ELECTRS_EXE" ) {
7672 Ok ( path) => path,
@@ -88,7 +84,7 @@ impl TestEnv {
8884 }
8985
9086 /// Exposes the [`RpcApi`] calls from [`bitcoincore_rpc`].
91- pub fn rpc_client ( & self ) -> & impl RpcApi {
87+ pub fn rpc_client ( & self ) -> & corepc_node :: Client {
9288 & self . bitcoind . client
9389 }
9490
@@ -119,26 +115,23 @@ impl TestEnv {
119115 ) -> anyhow:: Result < Vec < BlockHash > > {
120116 let coinbase_address = match address {
121117 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 ( ) ?,
127119 } ;
128120 let block_hashes = self
129121 . bitcoind
130122 . client
131- . generate_to_address ( count as _ , & coinbase_address) ?;
123+ . generate_to_address ( count as _ , & coinbase_address) ?
124+ . into_model ( ) ?
125+ . 0 ;
132126 Ok ( block_hashes)
133127 }
134128
135129 /// Mine a block that is guaranteed to be empty even with transactions in the mempool.
136130 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) ?;
142135
143136 let txdata = vec ! [ Transaction {
144137 version: transaction:: Version :: ONE ,
@@ -147,7 +140,7 @@ impl TestEnv {
147140 previous_output: bdk_chain:: bitcoin:: OutPoint :: default ( ) ,
148141 script_sig: ScriptBuf :: builder( )
149142 . 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
151144 . push_int( random( ) )
152145 . into_script( ) ,
153146 sequence: bdk_chain:: bitcoin:: Sequence :: default ( ) ,
@@ -159,18 +152,22 @@ impl TestEnv {
159152 } ] ,
160153 } ] ;
161154
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" ) ;
167161
168162 let mut block = Block {
169163 header : Header {
170164 version : bdk_chain:: bitcoin:: block:: Version :: default ( ) ,
171- prev_blockhash : bt. previous_block_hash ,
165+ prev_blockhash : BlockHash :: from_str ( & bt. previous_block_hash ) ? ,
172166 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+ ) ,
174171 bits : CompactTarget :: from_consensus ( u32:: from_be_bytes ( bits) ) ,
175172 nonce : 0 ,
176173 } ,
@@ -187,6 +184,7 @@ impl TestEnv {
187184 }
188185
189186 self . bitcoind . client . submit_block ( & block) ?;
187+
190188 Ok ( ( bt. height as usize , block. block_hash ( ) ) )
191189 }
192190
@@ -237,18 +235,16 @@ impl TestEnv {
237235
238236 /// Invalidate a number of blocks of a given size `count`.
239237 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 ( ) ? ;
241239 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+ // }
252248 }
253249 Ok ( ( ) )
254250 }
@@ -289,7 +285,8 @@ impl TestEnv {
289285 let txid = self
290286 . bitcoind
291287 . client
292- . send_to_address ( address, amount, None , None , None , None , None , None ) ?;
288+ . send_to_address ( address, amount) ?
289+ . txid ( ) ?;
293290 Ok ( txid)
294291 }
295292
@@ -300,14 +297,19 @@ impl TestEnv {
300297 . client
301298 . get_block_hash ( height as u64 )
302299 . 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+ } )
304306 } ) )
305307 . expect ( "must craft tip" )
306308 }
307309
308310 /// Get the genesis hash of the blockchain.
309311 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 ;
311313 Ok ( hash)
312314 }
313315}
@@ -317,7 +319,7 @@ impl TestEnv {
317319mod test {
318320 use crate :: TestEnv ;
319321 use core:: time:: Duration ;
320- use electrsd:: bitcoind :: { anyhow:: Result , bitcoincore_rpc :: RpcApi } ;
322+ use electrsd:: corepc_node :: anyhow:: Result ;
321323
322324 /// This checks that reorgs initiated by `bitcoind` is detected by our `electrsd` instance.
323325 #[ test]
@@ -327,15 +329,15 @@ mod test {
327329 // Mine some blocks.
328330 env. mine_blocks ( 101 , None ) ?;
329331 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 ;
331333 let blocks = ( 0 ..=height)
332334 . map ( |i| env. bitcoind . client . get_block_hash ( i) )
333335 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
334336
335337 // Perform reorg on six blocks.
336338 env. reorg ( 6 ) ?;
337339 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 ;
339341 let reorged_blocks = ( 0 ..=height)
340342 . map ( |i| env. bitcoind . client . get_block_hash ( i) )
341343 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
0 commit comments