@@ -32,7 +32,9 @@ fn get_bitcoind() -> &'static BitcoinD {
32
32
) ;
33
33
let mut conf = bitcoind:: Conf :: default ( ) ;
34
34
conf. network = "regtest" ;
35
- BitcoinD :: with_conf ( bitcoind_exe, & conf) . unwrap ( )
35
+ let bitcoind = BitcoinD :: with_conf ( bitcoind_exe, & conf) . unwrap ( ) ;
36
+ std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
37
+ bitcoind
36
38
} )
37
39
}
38
40
@@ -46,29 +48,42 @@ fn get_electrsd() -> &'static ElectrsD {
46
48
let mut conf = electrsd:: Conf :: default ( ) ;
47
49
conf. http_enabled = true ;
48
50
conf. network = "regtest" ;
49
- ElectrsD :: with_conf ( electrs_exe, & bitcoind, & conf) . unwrap ( )
51
+ let electrsd = ElectrsD :: with_conf ( electrs_exe, & bitcoind, & conf) . unwrap ( ) ;
52
+ std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
53
+ electrsd
50
54
} )
51
55
}
52
56
53
57
fn generate_blocks_and_wait ( num : usize ) {
54
58
let miner_lock = MINER_LOCK . get_or_init ( || Mutex :: new ( ( ) ) ) ;
55
59
let _miner = miner_lock. lock ( ) . unwrap ( ) ;
56
- let cur_height = get_bitcoind ( ) . client . get_block_count ( ) . unwrap ( ) ;
57
- let address = get_bitcoind ( ) . client . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) ) . unwrap ( ) ;
58
- let _block_hashes = get_bitcoind ( ) . client . generate_to_address ( num as u64 , & address) . 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" ) ;
62
+ // 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) ;
59
64
wait_for_block ( cur_height as usize + num) ;
60
65
}
61
66
62
67
fn wait_for_block ( min_height : usize ) {
63
- let mut header = get_electrsd ( ) . client . block_headers_subscribe ( ) . unwrap ( ) ;
68
+ let mut header = match get_electrsd ( ) . client . block_headers_subscribe ( ) {
69
+ Ok ( header) => header,
70
+ Err ( _) => {
71
+ // While subscribing should succeed the first time around, we ran into some cases where
72
+ // it didn't. Since we can't proceed without subscribing, we try again after a delay
73
+ // and panic if it still fails.
74
+ std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
75
+ get_electrsd ( ) . client . block_headers_subscribe ( ) . expect ( "failed to subscribe to block headers" )
76
+ }
77
+ } ;
78
+
64
79
loop {
65
80
if header. height >= min_height {
66
81
break ;
67
82
}
68
83
header = exponential_backoff_poll ( || {
69
- get_electrsd ( ) . trigger ( ) . unwrap ( ) ;
70
- get_electrsd ( ) . client . ping ( ) . unwrap ( ) ;
71
- get_electrsd ( ) . client . block_headers_pop ( ) . unwrap ( )
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" )
72
87
} ) ;
73
88
}
74
89
}
0 commit comments