@@ -12,78 +12,61 @@ use electrsd::bitcoind::bitcoincore_rpc::bitcoincore_rpc_json::AddressType;
1212use bitcoind:: bitcoincore_rpc:: RpcApi ;
1313use electrum_client:: ElectrumApi ;
1414
15- use once_cell:: sync:: OnceCell ;
16-
1715use std:: env;
1816use std:: sync:: Mutex ;
1917use std:: time:: Duration ;
2018use std:: collections:: { HashMap , HashSet } ;
2119
22- static BITCOIND : OnceCell < BitcoinD > = OnceCell :: new ( ) ;
23- static ELECTRSD : OnceCell < ElectrsD > = OnceCell :: new ( ) ;
24- static PREMINE : OnceCell < ( ) > = OnceCell :: new ( ) ;
25- static MINER_LOCK : OnceCell < Mutex < ( ) > > = OnceCell :: new ( ) ;
26-
27- fn get_bitcoind ( ) -> & ' static BitcoinD {
28- BITCOIND . get_or_init ( || {
29- let bitcoind_exe =
30- env:: var ( "BITCOIND_EXE" ) . ok ( ) . or_else ( || bitcoind:: downloaded_exe_path ( ) . ok ( ) ) . expect (
31- "you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature" ,
32- ) ;
33- let mut conf = bitcoind:: Conf :: default ( ) ;
34- conf. network = "regtest" ;
35- let bitcoind = BitcoinD :: with_conf ( bitcoind_exe, & conf) . unwrap ( ) ;
36- std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
37- bitcoind
38- } )
39- }
40-
41- fn get_electrsd ( ) -> & ' static ElectrsD {
42- ELECTRSD . get_or_init ( || {
43- let bitcoind = get_bitcoind ( ) ;
44- let electrs_exe =
45- env:: var ( "ELECTRS_EXE" ) . ok ( ) . or_else ( electrsd:: downloaded_exe_path) . expect (
46- "you need to provide env var ELECTRS_EXE or specify an electrsd version feature" ,
47- ) ;
48- let mut conf = electrsd:: Conf :: default ( ) ;
49- conf. http_enabled = true ;
50- conf. network = "regtest" ;
51- let electrsd = ElectrsD :: with_conf ( electrs_exe, & bitcoind, & conf) . unwrap ( ) ;
52- std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
53- electrsd
54- } )
20+ pub fn setup_bitcoind_and_electrsd ( ) -> ( BitcoinD , ElectrsD ) {
21+ let bitcoind_exe =
22+ env:: var ( "BITCOIND_EXE" ) . ok ( ) . or_else ( || bitcoind:: downloaded_exe_path ( ) . ok ( ) ) . expect (
23+ "you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature" ,
24+ ) ;
25+ let mut bitcoind_conf = bitcoind:: Conf :: default ( ) ;
26+ bitcoind_conf. network = "regtest" ;
27+ let bitcoind = BitcoinD :: with_conf ( bitcoind_exe, & bitcoind_conf) . unwrap ( ) ;
28+
29+ let electrs_exe = env:: var ( "ELECTRS_EXE" )
30+ . ok ( )
31+ . or_else ( electrsd:: downloaded_exe_path)
32+ . expect ( "you need to provide env var ELECTRS_EXE or specify an electrsd version feature" ) ;
33+ let mut electrsd_conf = electrsd:: Conf :: default ( ) ;
34+ electrsd_conf. http_enabled = true ;
35+ electrsd_conf. network = "regtest" ;
36+ let electrsd = ElectrsD :: with_conf ( electrs_exe, & bitcoind, & electrsd_conf) . unwrap ( ) ;
37+ ( bitcoind, electrsd)
5538}
5639
57- fn generate_blocks_and_wait ( num : usize ) {
58- let miner_lock = MINER_LOCK . get_or_init ( || Mutex :: new ( ( ) ) ) ;
59- let _miner = miner_lock. lock ( ) . unwrap ( ) ;
60- let cur_height = get_bitcoind ( ) . client . get_block_count ( ) . expect ( "failed to get current block height" ) ;
61- let address = get_bitcoind ( ) . client . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) ) . expect ( "failed to get new address" ) ;
40+ pub fn generate_blocks_and_wait ( bitcoind : & BitcoinD , electrsd : & ElectrsD , num : usize ) {
41+ let cur_height = bitcoind. client . get_block_count ( ) . expect ( "failed to get current block height" ) ;
42+ let address = bitcoind
43+ . client
44+ . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) )
45+ . expect ( "failed to get new address" ) ;
6246 // TODO: expect this Result once the WouldBlock issue is resolved upstream.
63- let _block_hashes_res = get_bitcoind ( ) . client . generate_to_address ( num as u64 , & address) ;
64- wait_for_block ( cur_height as usize + num) ;
47+ let _block_hashes_res = bitcoind . client . generate_to_address ( num as u64 , & address) ;
48+ wait_for_block ( electrsd , cur_height as usize + num) ;
6549}
6650
67- fn wait_for_block ( min_height : usize ) {
68- let mut header = match get_electrsd ( ) . client . block_headers_subscribe ( ) {
51+ pub fn wait_for_block ( electrsd : & ElectrsD , min_height : usize ) {
52+ let mut header = match electrsd . client . block_headers_subscribe ( ) {
6953 Ok ( header) => header,
7054 Err ( _) => {
7155 // While subscribing should succeed the first time around, we ran into some cases where
7256 // it didn't. Since we can't proceed without subscribing, we try again after a delay
7357 // and panic if it still fails.
7458 std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
75- get_electrsd ( ) . client . block_headers_subscribe ( ) . expect ( "failed to subscribe to block headers" )
59+ electrsd . client . block_headers_subscribe ( ) . expect ( "failed to subscribe to block headers" )
7660 }
7761 } ;
78-
7962 loop {
8063 if header. height >= min_height {
8164 break ;
8265 }
8366 header = exponential_backoff_poll ( || {
84- get_electrsd ( ) . trigger ( ) . expect ( "failed to trigger electrsd" ) ;
85- get_electrsd ( ) . client . ping ( ) . expect ( "failed to ping electrsd" ) ;
86- get_electrsd ( ) . client . block_headers_pop ( ) . expect ( "failed to pop block header" )
67+ electrsd . trigger ( ) . expect ( "failed to trigger electrsd" ) ;
68+ electrsd . client . ping ( ) . expect ( "failed to ping electrsd" ) ;
69+ electrsd . client . block_headers_pop ( ) . expect ( "failed to pop block header" )
8770 } ) ;
8871 }
8972}
@@ -109,12 +92,6 @@ where
10992 }
11093}
11194
112- fn premine ( ) {
113- PREMINE . get_or_init ( || {
114- generate_blocks_and_wait ( 101 ) ;
115- } ) ;
116- }
117-
11895#[ derive( Debug ) ]
11996enum TestConfirmableEvent {
12097 Confirmed ( Txid , BlockHash , u32 ) ,
@@ -182,27 +159,25 @@ impl Logger for TestLogger {
182159#[ test]
183160#[ cfg( feature = "esplora-blocking" ) ]
184161fn test_esplora_syncs ( ) {
185- premine ( ) ;
162+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
163+ generate_blocks_and_wait ( & bitcoind, & electrsd, 101 ) ;
186164 let mut logger = TestLogger { } ;
187- let esplora_url = format ! ( "http://{}" , get_electrsd ( ) . esplora_url. as_ref( ) . unwrap( ) ) ;
165+ let esplora_url = format ! ( "http://{}" , electrsd . esplora_url. as_ref( ) . unwrap( ) ) ;
188166 let tx_sync = EsploraSyncClient :: new ( esplora_url, & mut logger) ;
189167 let confirmable = TestConfirmable :: new ( ) ;
190168
191169 // Check we pick up on new best blocks
192- let expected_height = 0u32 ;
193- assert_eq ! ( confirmable. best_block. lock( ) . unwrap( ) . 1 , expected_height) ;
170+ assert_eq ! ( confirmable. best_block. lock( ) . unwrap( ) . 1 , 0 ) ;
194171
195172 tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
196-
197- let expected_height = get_bitcoind ( ) . client . get_block_count ( ) . unwrap ( ) as u32 ;
198- assert_eq ! ( confirmable. best_block. lock( ) . unwrap( ) . 1 , expected_height) ;
173+ assert_eq ! ( confirmable. best_block. lock( ) . unwrap( ) . 1 , 102 ) ;
199174
200175 let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
201176 assert_eq ! ( events. len( ) , 1 ) ;
202177
203178 // Check registered confirmed transactions are marked confirmed
204- let new_address = get_bitcoind ( ) . client . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) ) . unwrap ( ) ;
205- let txid = get_bitcoind ( ) . client . send_to_address ( & new_address, Amount :: from_sat ( 5000 ) , None , None , None , None , None , None ) . unwrap ( ) ;
179+ let new_address = bitcoind . client . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) ) . unwrap ( ) ;
180+ let txid = bitcoind . client . send_to_address ( & new_address, Amount :: from_sat ( 5000 ) , None , None , None , None , None , None ) . unwrap ( ) ;
206181 tx_sync. register_tx ( & txid, & new_address. script_pubkey ( ) ) ;
207182
208183 tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
@@ -212,7 +187,7 @@ fn test_esplora_syncs() {
212187 assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
213188 assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
214189
215- generate_blocks_and_wait ( 1 ) ;
190+ generate_blocks_and_wait ( & bitcoind , & electrsd , 1 ) ;
216191 tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
217192
218193 let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
@@ -221,19 +196,19 @@ fn test_esplora_syncs() {
221196 assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
222197
223198 // Check previously confirmed transactions are marked unconfirmed when they are reorged.
224- let best_block_hash = get_bitcoind ( ) . client . get_best_block_hash ( ) . unwrap ( ) ;
225- get_bitcoind ( ) . client . invalidate_block ( & best_block_hash) . unwrap ( ) ;
199+ let best_block_hash = bitcoind . client . get_best_block_hash ( ) . unwrap ( ) ;
200+ bitcoind . client . invalidate_block ( & best_block_hash) . unwrap ( ) ;
226201
227202 // We're getting back to the previous height with a new tip, but best block shouldn't change.
228- generate_blocks_and_wait ( 1 ) ;
229- assert_ne ! ( get_bitcoind ( ) . client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
203+ generate_blocks_and_wait ( & bitcoind , & electrsd , 1 ) ;
204+ assert_ne ! ( bitcoind . client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
230205 tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
231206 let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
232207 assert_eq ! ( events. len( ) , 0 ) ;
233208
234209 // Now we're surpassing previous height, getting new tip.
235- generate_blocks_and_wait ( 1 ) ;
236- assert_ne ! ( get_bitcoind ( ) . client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
210+ generate_blocks_and_wait ( & bitcoind , & electrsd , 1 ) ;
211+ assert_ne ! ( bitcoind . client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
237212 tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
238213
239214 // Transaction still confirmed but under new tip.
@@ -267,27 +242,25 @@ fn test_esplora_syncs() {
267242#[ tokio:: test]
268243#[ cfg( feature = "esplora-async" ) ]
269244async fn test_esplora_syncs ( ) {
270- premine ( ) ;
245+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
246+ generate_blocks_and_wait ( & bitcoind, & electrsd, 101 ) ;
271247 let mut logger = TestLogger { } ;
272- let esplora_url = format ! ( "http://{}" , get_electrsd ( ) . esplora_url. as_ref( ) . unwrap( ) ) ;
248+ let esplora_url = format ! ( "http://{}" , electrsd . esplora_url. as_ref( ) . unwrap( ) ) ;
273249 let tx_sync = EsploraSyncClient :: new ( esplora_url, & mut logger) ;
274250 let confirmable = TestConfirmable :: new ( ) ;
275251
276252 // Check we pick up on new best blocks
277- let expected_height = 0u32 ;
278- assert_eq ! ( confirmable. best_block. lock( ) . unwrap( ) . 1 , expected_height) ;
253+ assert_eq ! ( confirmable. best_block. lock( ) . unwrap( ) . 1 , 0 ) ;
279254
280255 tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
281-
282- let expected_height = get_bitcoind ( ) . client . get_block_count ( ) . unwrap ( ) as u32 ;
283- assert_eq ! ( confirmable. best_block. lock( ) . unwrap( ) . 1 , expected_height) ;
256+ assert_eq ! ( confirmable. best_block. lock( ) . unwrap( ) . 1 , 102 ) ;
284257
285258 let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
286259 assert_eq ! ( events. len( ) , 1 ) ;
287260
288261 // Check registered confirmed transactions are marked confirmed
289- let new_address = get_bitcoind ( ) . client . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) ) . unwrap ( ) ;
290- let txid = get_bitcoind ( ) . client . send_to_address ( & new_address, Amount :: from_sat ( 5000 ) , None , None , None , None , None , None ) . unwrap ( ) ;
262+ let new_address = bitcoind . client . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) ) . unwrap ( ) ;
263+ let txid = bitcoind . client . send_to_address ( & new_address, Amount :: from_sat ( 5000 ) , None , None , None , None , None , None ) . unwrap ( ) ;
291264 tx_sync. register_tx ( & txid, & new_address. script_pubkey ( ) ) ;
292265
293266 tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
@@ -297,7 +270,7 @@ async fn test_esplora_syncs() {
297270 assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
298271 assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
299272
300- generate_blocks_and_wait ( 1 ) ;
273+ generate_blocks_and_wait ( & bitcoind , & electrsd , 1 ) ;
301274 tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
302275
303276 let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
@@ -306,19 +279,19 @@ async fn test_esplora_syncs() {
306279 assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
307280
308281 // Check previously confirmed transactions are marked unconfirmed when they are reorged.
309- let best_block_hash = get_bitcoind ( ) . client . get_best_block_hash ( ) . unwrap ( ) ;
310- get_bitcoind ( ) . client . invalidate_block ( & best_block_hash) . unwrap ( ) ;
282+ let best_block_hash = bitcoind . client . get_best_block_hash ( ) . unwrap ( ) ;
283+ bitcoind . client . invalidate_block ( & best_block_hash) . unwrap ( ) ;
311284
312285 // We're getting back to the previous height with a new tip, but best block shouldn't change.
313- generate_blocks_and_wait ( 1 ) ;
314- assert_ne ! ( get_bitcoind ( ) . client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
286+ generate_blocks_and_wait ( & bitcoind , & electrsd , 1 ) ;
287+ assert_ne ! ( bitcoind . client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
315288 tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
316289 let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
317290 assert_eq ! ( events. len( ) , 0 ) ;
318291
319292 // Now we're surpassing previous height, getting new tip.
320- generate_blocks_and_wait ( 1 ) ;
321- assert_ne ! ( get_bitcoind ( ) . client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
293+ generate_blocks_and_wait ( & bitcoind , & electrsd , 1 ) ;
294+ assert_ne ! ( bitcoind . client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
322295 tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
323296
324297 // Transaction still confirmed but under new tip.
0 commit comments