@@ -10,32 +10,28 @@ use bdk_chain::{
1010 } ,
1111 local_chain:: CheckPoint ,
1212} ;
13- use bitcoincore_rpc:: {
14- bitcoincore_rpc_json:: { GetBlockTemplateModes , GetBlockTemplateRules } ,
15- RpcApi ,
16- } ;
17- use electrsd:: bitcoind:: anyhow:: Context ;
13+ use electrsd:: corepc_node:: { anyhow:: Context , TemplateRequest , TemplateRules } ;
1814
1915pub use electrsd;
20- pub use electrsd:: bitcoind ;
21- pub use electrsd:: bitcoind :: anyhow ;
22- pub use electrsd:: bitcoind :: bitcoincore_rpc ;
16+ pub use electrsd:: corepc_client ;
17+ pub use electrsd:: corepc_node ;
18+ pub use electrsd:: corepc_node :: anyhow ;
2319pub use electrsd:: electrum_client;
2420use electrsd:: electrum_client:: ElectrumApi ;
25- use std:: time:: Duration ;
21+ use std:: { str :: FromStr , time:: Duration } ;
2622
2723/// Struct for running a regtest environment with a single `bitcoind` node with an `electrs`
2824/// instance connected to it.
2925pub struct TestEnv {
30- pub bitcoind : electrsd:: bitcoind :: BitcoinD ,
26+ pub bitcoind : electrsd:: corepc_node :: Node ,
3127 pub electrsd : electrsd:: ElectrsD ,
3228}
3329
3430/// Configuration parameters.
3531#[ derive( Debug ) ]
3632pub struct Config < ' a > {
3733 /// [`bitcoind::Conf`]
38- pub bitcoind : bitcoind :: Conf < ' a > ,
34+ pub bitcoind : corepc_node :: Conf < ' a > ,
3935 /// [`electrsd::Conf`]
4036 pub electrsd : electrsd:: Conf < ' a > ,
4137}
@@ -45,7 +41,7 @@ impl Default for Config<'_> {
4541 /// which is required for testing `bdk_esplora`.
4642 fn default ( ) -> Self {
4743 Self {
48- bitcoind : bitcoind :: Conf :: default ( ) ,
44+ bitcoind : corepc_node :: Conf :: default ( ) ,
4945 electrsd : {
5046 let mut conf = electrsd:: Conf :: default ( ) ;
5147 conf. http_enabled = true ;
@@ -65,11 +61,11 @@ impl TestEnv {
6561 pub fn new_with_config ( config : Config ) -> anyhow:: Result < Self > {
6662 let bitcoind_exe = match std:: env:: var ( "BITCOIND_EXE" ) {
6763 Ok ( path) => path,
68- Err ( _) => bitcoind :: downloaded_exe_path ( ) . context (
64+ Err ( _) => corepc_node :: downloaded_exe_path ( ) . context (
6965 "you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature" ,
7066 ) ?,
7167 } ;
72- let bitcoind = bitcoind :: BitcoinD :: with_conf ( bitcoind_exe, & config. bitcoind ) ?;
68+ let bitcoind = corepc_node :: Node :: with_conf ( bitcoind_exe, & config. bitcoind ) ?;
7369
7470 let electrs_exe = match std:: env:: var ( "ELECTRS_EXE" ) {
7571 Ok ( path) => path,
@@ -87,7 +83,7 @@ impl TestEnv {
8783 }
8884
8985 /// Exposes the [`RpcApi`] calls from [`bitcoincore_rpc`].
90- pub fn rpc_client ( & self ) -> & impl RpcApi {
86+ pub fn rpc_client ( & self ) -> & corepc_node :: Client {
9187 & self . bitcoind . client
9288 }
9389
@@ -118,26 +114,23 @@ impl TestEnv {
118114 ) -> anyhow:: Result < Vec < BlockHash > > {
119115 let coinbase_address = match address {
120116 Some ( address) => address,
121- None => self
122- . bitcoind
123- . client
124- . get_new_address ( None , None ) ?
125- . assume_checked ( ) ,
117+ None => self . bitcoind . client . new_address ( ) ?,
126118 } ;
127119 let block_hashes = self
128120 . bitcoind
129121 . client
130- . generate_to_address ( count as _ , & coinbase_address) ?;
122+ . generate_to_address ( count as _ , & coinbase_address) ?
123+ . into_model ( ) ?
124+ . 0 ;
131125 Ok ( block_hashes)
132126 }
133127
134128 /// Mine a block that is guaranteed to be empty even with transactions in the mempool.
135129 pub fn mine_empty_block ( & self ) -> anyhow:: Result < ( usize , BlockHash ) > {
136- let bt = self . bitcoind . client . get_block_template (
137- GetBlockTemplateModes :: Template ,
138- & [ GetBlockTemplateRules :: SegWit ] ,
139- & [ ] ,
140- ) ?;
130+ let request = TemplateRequest {
131+ rules : vec ! [ TemplateRules :: Segwit ] ,
132+ } ;
133+ let bt = self . bitcoind . client . get_block_template ( & request) ?;
141134
142135 let txdata = vec ! [ Transaction {
143136 version: transaction:: Version :: ONE ,
@@ -146,7 +139,7 @@ impl TestEnv {
146139 previous_output: bdk_chain:: bitcoin:: OutPoint :: default ( ) ,
147140 script_sig: ScriptBuf :: builder( )
148141 . push_int( bt. height as _)
149- // randomn number so that re-mining creates unique block
142+ // random number so that re-mining creates unique block
150143 . push_int( random( ) )
151144 . into_script( ) ,
152145 sequence: bdk_chain:: bitcoin:: Sequence :: default ( ) ,
@@ -158,18 +151,22 @@ impl TestEnv {
158151 } ] ,
159152 } ] ;
160153
161- let bits: [ u8 ; 4 ] = bt
162- . bits
163- . clone ( )
164- . try_into ( )
165- . expect ( "rpc provided us with invalid bits" ) ;
154+ // TODO: (@leonardo) double-check if an `.into_bytes()` wouldn't be enough instead.
155+ let bits: [ u8 ; 4 ] =
156+ bdk_chain:: bitcoin:: consensus:: encode:: deserialize_hex :: < Vec < u8 > > ( & bt. bits ) ?
157+ . clone ( )
158+ . try_into ( )
159+ . expect ( "rpc provided us with invalid bits" ) ;
166160
167161 let mut block = Block {
168162 header : Header {
169163 version : bdk_chain:: bitcoin:: block:: Version :: default ( ) ,
170- prev_blockhash : bt. previous_block_hash ,
164+ prev_blockhash : BlockHash :: from_str ( & bt. previous_block_hash ) ? ,
171165 merkle_root : TxMerkleNode :: all_zeros ( ) ,
172- time : Ord :: max ( bt. min_time , std:: time:: UNIX_EPOCH . elapsed ( ) ?. as_secs ( ) ) as u32 ,
166+ time : Ord :: max (
167+ bt. min_time ,
168+ std:: time:: UNIX_EPOCH . elapsed ( ) ?. as_secs ( ) as u32 ,
169+ ) ,
173170 bits : CompactTarget :: from_consensus ( u32:: from_be_bytes ( bits) ) ,
174171 nonce : 0 ,
175172 } ,
@@ -186,6 +183,7 @@ impl TestEnv {
186183 }
187184
188185 self . bitcoind . client . submit_block ( & block) ?;
186+
189187 Ok ( ( bt. height as usize , block. block_hash ( ) ) )
190188 }
191189
@@ -238,18 +236,16 @@ impl TestEnv {
238236
239237 /// Invalidate a number of blocks of a given size `count`.
240238 pub fn invalidate_blocks ( & self , count : usize ) -> anyhow:: Result < ( ) > {
241- let mut hash = self . bitcoind . client . get_best_block_hash ( ) ?;
239+ let mut hash = self . bitcoind . client . get_best_block_hash ( ) ?. block_hash ( ) ? ;
242240 for _ in 0 ..count {
243- let prev_hash = self
244- . bitcoind
245- . client
246- . get_block_info ( & hash) ?
247- . previousblockhash ;
248- self . bitcoind . client . invalidate_block ( & hash) ?;
249- match prev_hash {
250- Some ( prev_hash) => hash = prev_hash,
251- None => break ,
252- }
241+ let prev_hash = self . bitcoind . client . get_block ( hash) ?. header . prev_blockhash ;
242+ self . bitcoind . client . invalidate_block ( hash) ?;
243+ hash = prev_hash
244+ // TODO: (@leonardo) It requires a double check if there is any side-effect with this
245+ // break removal. match prev_hash {
246+ // Some(prev_hash) => hash = prev_hash,
247+ // None => break,
248+ // }
253249 }
254250 Ok ( ( ) )
255251 }
@@ -290,7 +286,8 @@ impl TestEnv {
290286 let txid = self
291287 . bitcoind
292288 . client
293- . send_to_address ( address, amount, None , None , None , None , None , None ) ?;
289+ . send_to_address ( address, amount) ?
290+ . txid ( ) ?;
294291 Ok ( txid)
295292 }
296293
@@ -301,14 +298,19 @@ impl TestEnv {
301298 . client
302299 . get_block_hash ( height as u64 )
303300 . ok ( )
304- . map ( |hash| ( height, hash) )
301+ . map ( |get_block_hash| {
302+ let hash = get_block_hash
303+ . block_hash ( )
304+ . expect ( "should `successfully convert to `BlockHash` from `GetBlockHash`" ) ;
305+ ( height, hash)
306+ } )
305307 } ) )
306308 . expect ( "must craft tip" )
307309 }
308310
309311 /// Get the genesis hash of the blockchain.
310312 pub fn genesis_hash ( & self ) -> anyhow:: Result < BlockHash > {
311- let hash = self . bitcoind . client . get_block_hash ( 0 ) ?;
313+ let hash = self . bitcoind . client . get_block_hash ( 0 ) ?. into_model ( ) ? . 0 ;
312314 Ok ( hash)
313315 }
314316}
@@ -318,7 +320,7 @@ impl TestEnv {
318320mod test {
319321 use crate :: TestEnv ;
320322 use core:: time:: Duration ;
321- use electrsd:: bitcoind :: { anyhow:: Result , bitcoincore_rpc :: RpcApi } ;
323+ use electrsd:: corepc_node :: anyhow:: Result ;
322324
323325 /// This checks that reorgs initiated by `bitcoind` is detected by our `electrsd` instance.
324326 #[ test]
@@ -328,15 +330,15 @@ mod test {
328330 // Mine some blocks.
329331 env. mine_blocks ( 101 , None ) ?;
330332 env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
331- let height = env. bitcoind . client . get_block_count ( ) ?;
333+ let height = env. bitcoind . client . get_block_count ( ) ?. into_model ( ) . 0 ;
332334 let blocks = ( 0 ..=height)
333335 . map ( |i| env. bitcoind . client . get_block_hash ( i) )
334336 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
335337
336338 // Perform reorg on six blocks.
337339 env. reorg ( 6 ) ?;
338340 env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
339- let reorged_height = env. bitcoind . client . get_block_count ( ) ?;
341+ let reorged_height = env. bitcoind . client . get_block_count ( ) ?. into_model ( ) . 0 ;
340342 let reorged_blocks = ( 0 ..=height)
341343 . map ( |i| env. bitcoind . client . get_block_hash ( i) )
342344 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
0 commit comments