@@ -12,78 +12,61 @@ use electrsd::bitcoind::bitcoincore_rpc::bitcoincore_rpc_json::AddressType;
12
12
use bitcoind:: bitcoincore_rpc:: RpcApi ;
13
13
use electrum_client:: ElectrumApi ;
14
14
15
- use once_cell:: sync:: OnceCell ;
16
-
17
15
use std:: env;
18
16
use std:: sync:: Mutex ;
19
17
use std:: time:: Duration ;
20
18
use std:: collections:: { HashMap , HashSet } ;
21
19
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)
55
38
}
56
39
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" ) ;
62
46
// 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) ;
65
49
}
66
50
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 ( ) {
69
53
Ok ( header) => header,
70
54
Err ( _) => {
71
55
// While subscribing should succeed the first time around, we ran into some cases where
72
56
// it didn't. Since we can't proceed without subscribing, we try again after a delay
73
57
// and panic if it still fails.
74
58
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" )
76
60
}
77
61
} ;
78
-
79
62
loop {
80
63
if header. height >= min_height {
81
64
break ;
82
65
}
83
66
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" )
87
70
} ) ;
88
71
}
89
72
}
@@ -109,12 +92,6 @@ where
109
92
}
110
93
}
111
94
112
- fn premine ( ) {
113
- PREMINE . get_or_init ( || {
114
- generate_blocks_and_wait ( 101 ) ;
115
- } ) ;
116
- }
117
-
118
95
#[ derive( Debug ) ]
119
96
enum TestConfirmableEvent {
120
97
Confirmed ( Txid , BlockHash , u32 ) ,
@@ -182,27 +159,25 @@ impl Logger for TestLogger {
182
159
#[ test]
183
160
#[ cfg( feature = "esplora-blocking" ) ]
184
161
fn test_esplora_syncs ( ) {
185
- premine ( ) ;
162
+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
163
+ generate_blocks_and_wait ( & bitcoind, & electrsd, 101 ) ;
186
164
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( ) ) ;
188
166
let tx_sync = EsploraSyncClient :: new ( esplora_url, & mut logger) ;
189
167
let confirmable = TestConfirmable :: new ( ) ;
190
168
191
169
// 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 ) ;
194
171
195
172
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 ) ;
199
174
200
175
let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
201
176
assert_eq ! ( events. len( ) , 1 ) ;
202
177
203
178
// 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 ( ) ;
206
181
tx_sync. register_tx ( & txid, & new_address. script_pubkey ( ) ) ;
207
182
208
183
tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
@@ -212,7 +187,7 @@ fn test_esplora_syncs() {
212
187
assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
213
188
assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
214
189
215
- generate_blocks_and_wait ( 1 ) ;
190
+ generate_blocks_and_wait ( & bitcoind , & electrsd , 1 ) ;
216
191
tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
217
192
218
193
let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
@@ -221,19 +196,19 @@ fn test_esplora_syncs() {
221
196
assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
222
197
223
198
// 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 ( ) ;
226
201
227
202
// 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) ;
230
205
tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
231
206
let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
232
207
assert_eq ! ( events. len( ) , 0 ) ;
233
208
234
209
// 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) ;
237
212
tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
238
213
239
214
// Transaction still confirmed but under new tip.
@@ -267,27 +242,25 @@ fn test_esplora_syncs() {
267
242
#[ tokio:: test]
268
243
#[ cfg( feature = "esplora-async" ) ]
269
244
async fn test_esplora_syncs ( ) {
270
- premine ( ) ;
245
+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
246
+ generate_blocks_and_wait ( & bitcoind, & electrsd, 101 ) ;
271
247
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( ) ) ;
273
249
let tx_sync = EsploraSyncClient :: new ( esplora_url, & mut logger) ;
274
250
let confirmable = TestConfirmable :: new ( ) ;
275
251
276
252
// 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 ) ;
279
254
280
255
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 ) ;
284
257
285
258
let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
286
259
assert_eq ! ( events. len( ) , 1 ) ;
287
260
288
261
// 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 ( ) ;
291
264
tx_sync. register_tx ( & txid, & new_address. script_pubkey ( ) ) ;
292
265
293
266
tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
@@ -297,7 +270,7 @@ async fn test_esplora_syncs() {
297
270
assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
298
271
assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
299
272
300
- generate_blocks_and_wait ( 1 ) ;
273
+ generate_blocks_and_wait ( & bitcoind , & electrsd , 1 ) ;
301
274
tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
302
275
303
276
let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
@@ -306,19 +279,19 @@ async fn test_esplora_syncs() {
306
279
assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
307
280
308
281
// 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 ( ) ;
311
284
312
285
// 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) ;
315
288
tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
316
289
let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
317
290
assert_eq ! ( events. len( ) , 0 ) ;
318
291
319
292
// 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) ;
322
295
tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
323
296
324
297
// Transaction still confirmed but under new tip.
0 commit comments