@@ -43,15 +43,23 @@ fn block_to_chain_update(block: &bitcoin::Block, height: u32) -> local_chain::Up
43
43
pub fn test_sync_local_chain ( ) -> anyhow:: Result < ( ) > {
44
44
let env = TestEnv :: new ( ) ?;
45
45
let mut local_chain = LocalChain :: default ( ) ;
46
- let mut emitter = Emitter :: from_height ( & env. client , 0 ) ;
46
+ let tip = env. rpc_client ( ) . get_block_count ( ) ?;
47
+ let mut emitter = Emitter :: from_height ( env. rpc_client ( ) , tip as u32 ) ;
47
48
48
49
// mine some blocks and returned the actual block hashes
49
50
let exp_hashes = {
50
- let mut hashes = vec ! [ env. client. get_block_hash( 0 ) ?] ; // include genesis block
51
- hashes. extend ( env. mine_blocks ( 101 , None ) ?) ;
51
+ let mut hashes = ( 0 ..=tip)
52
+ . map ( |height| env. rpc_client ( ) . get_block_hash ( height) )
53
+ . collect :: < Result < Vec < _ > , _ > > ( ) ?; // includes genesis block
54
+ hashes. extend ( env. mine_blocks ( 101 - tip as usize , None ) ?) ;
52
55
hashes
53
56
} ;
54
57
58
+ ( 0 ..tip) . for_each ( |height| {
59
+ let changeset = BTreeMap :: from ( [ ( height as u32 , Some ( exp_hashes[ height as usize ] ) ) ] ) ;
60
+ local_chain. apply_changeset ( & changeset) ;
61
+ } ) ;
62
+
55
63
// see if the emitter outputs the right blocks
56
64
println ! ( "first sync:" ) ;
57
65
while let Some ( ( height, block) ) = emitter. next_block ( ) ? {
@@ -141,9 +149,18 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
141
149
let env = TestEnv :: new ( ) ?;
142
150
143
151
println ! ( "getting new addresses!" ) ;
144
- let addr_0 = env. client . get_new_address ( None , None ) ?. assume_checked ( ) ;
145
- let addr_1 = env. client . get_new_address ( None , None ) ?. assume_checked ( ) ;
146
- let addr_2 = env. client . get_new_address ( None , None ) ?. assume_checked ( ) ;
152
+ let addr_0 = env
153
+ . rpc_client ( )
154
+ . get_new_address ( None , None ) ?
155
+ . assume_checked ( ) ;
156
+ let addr_1 = env
157
+ . rpc_client ( )
158
+ . get_new_address ( None , None ) ?
159
+ . assume_checked ( ) ;
160
+ let addr_2 = env
161
+ . rpc_client ( )
162
+ . get_new_address ( None , None ) ?
163
+ . assume_checked ( ) ;
147
164
println ! ( "got new addresses!" ) ;
148
165
149
166
println ! ( "mining block!" ) ;
@@ -159,7 +176,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
159
176
index
160
177
} ) ;
161
178
162
- let emitter = & mut Emitter :: from_height ( & env. client , 0 ) ;
179
+ let emitter = & mut Emitter :: from_height ( env. rpc_client ( ) , 0 ) ;
163
180
164
181
while let Some ( ( height, block) ) = emitter. next_block ( ) ? {
165
182
let _ = chain. apply_update ( block_to_chain_update ( & block, height) ) ?;
@@ -171,7 +188,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
171
188
let exp_txids = {
172
189
let mut txids = BTreeSet :: new ( ) ;
173
190
for _ in 0 ..3 {
174
- txids. insert ( env. client . send_to_address (
191
+ txids. insert ( env. rpc_client ( ) . send_to_address (
175
192
& addr_0,
176
193
Amount :: from_sat ( 10_000 ) ,
177
194
None ,
@@ -207,7 +224,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
207
224
208
225
// mine a block that confirms the 3 txs
209
226
let exp_block_hash = env. mine_blocks ( 1 , None ) ?[ 0 ] ;
210
- let exp_block_height = env. client . get_block_info ( & exp_block_hash) ?. height as u32 ;
227
+ let exp_block_height = env. rpc_client ( ) . get_block_info ( & exp_block_hash) ?. height as u32 ;
211
228
let exp_anchors = exp_txids
212
229
. iter ( )
213
230
. map ( {
@@ -247,7 +264,7 @@ fn ensure_block_emitted_after_reorg_is_at_reorg_height() -> anyhow::Result<()> {
247
264
const CHAIN_TIP_HEIGHT : usize = 110 ;
248
265
249
266
let env = TestEnv :: new ( ) ?;
250
- let mut emitter = Emitter :: from_height ( & env. client , EMITTER_START_HEIGHT as _ ) ;
267
+ let mut emitter = Emitter :: from_height ( env. rpc_client ( ) , EMITTER_START_HEIGHT as _ ) ;
251
268
252
269
env. mine_blocks ( CHAIN_TIP_HEIGHT , None ) ?;
253
270
while emitter. next_header ( ) ?. is_some ( ) { }
@@ -315,10 +332,13 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
315
332
const SEND_AMOUNT : Amount = Amount :: from_sat ( 10_000 ) ;
316
333
317
334
let env = TestEnv :: new ( ) ?;
318
- let mut emitter = Emitter :: from_height ( & env. client , 0 ) ;
335
+ let mut emitter = Emitter :: from_height ( env. rpc_client ( ) , 0 ) ;
319
336
320
337
// setup addresses
321
- let addr_to_mine = env. client . get_new_address ( None , None ) ?. assume_checked ( ) ;
338
+ let addr_to_mine = env
339
+ . rpc_client ( )
340
+ . get_new_address ( None , None ) ?
341
+ . assume_checked ( ) ;
322
342
let spk_to_track = ScriptBuf :: new_v0_p2wsh ( & WScriptHash :: all_zeros ( ) ) ;
323
343
let addr_to_track = Address :: from_script ( & spk_to_track, bitcoin:: Network :: Regtest ) ?;
324
344
@@ -339,7 +359,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
339
359
340
360
// lock outputs that send to `addr_to_track`
341
361
let outpoints_to_lock = env
342
- . client
362
+ . rpc_client ( )
343
363
. get_transaction ( & txid, None ) ?
344
364
. transaction ( ) ?
345
365
. output
@@ -348,7 +368,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
348
368
. filter ( |( _, txo) | txo. script_pubkey == spk_to_track)
349
369
. map ( |( vout, _) | OutPoint :: new ( txid, vout as _ ) )
350
370
. collect :: < Vec < _ > > ( ) ;
351
- env. client . lock_unspent ( & outpoints_to_lock) ?;
371
+ env. rpc_client ( ) . lock_unspent ( & outpoints_to_lock) ?;
352
372
353
373
let _ = env. mine_blocks ( 1 , None ) ?;
354
374
}
@@ -396,10 +416,13 @@ fn mempool_avoids_re_emission() -> anyhow::Result<()> {
396
416
const MEMPOOL_TX_COUNT : usize = 2 ;
397
417
398
418
let env = TestEnv :: new ( ) ?;
399
- let mut emitter = Emitter :: from_height ( & env. client , 0 ) ;
419
+ let mut emitter = Emitter :: from_height ( env. rpc_client ( ) , 0 ) ;
400
420
401
421
// mine blocks and sync up emitter
402
- let addr = env. client . get_new_address ( None , None ) ?. assume_checked ( ) ;
422
+ let addr = env
423
+ . rpc_client ( )
424
+ . get_new_address ( None , None ) ?
425
+ . assume_checked ( ) ;
403
426
env. mine_blocks ( BLOCKS_TO_MINE , Some ( addr. clone ( ) ) ) ?;
404
427
while emitter. next_header ( ) ?. is_some ( ) { }
405
428
@@ -451,10 +474,13 @@ fn mempool_re_emits_if_tx_introduction_height_not_reached() -> anyhow::Result<()
451
474
const MEMPOOL_TX_COUNT : usize = 21 ;
452
475
453
476
let env = TestEnv :: new ( ) ?;
454
- let mut emitter = Emitter :: from_height ( & env. client , 0 ) ;
477
+ let mut emitter = Emitter :: from_height ( env. rpc_client ( ) , 0 ) ;
455
478
456
479
// mine blocks to get initial balance, sync emitter up to tip
457
- let addr = env. client . get_new_address ( None , None ) ?. assume_checked ( ) ;
480
+ let addr = env
481
+ . rpc_client ( )
482
+ . get_new_address ( None , None ) ?
483
+ . assume_checked ( ) ;
458
484
env. mine_blocks ( PREMINE_COUNT , Some ( addr. clone ( ) ) ) ?;
459
485
while emitter. next_header ( ) ?. is_some ( ) { }
460
486
@@ -528,10 +554,13 @@ fn mempool_during_reorg() -> anyhow::Result<()> {
528
554
const PREMINE_COUNT : usize = 101 ;
529
555
530
556
let env = TestEnv :: new ( ) ?;
531
- let mut emitter = Emitter :: from_height ( & env. client , 0 ) ;
557
+ let mut emitter = Emitter :: from_height ( env. rpc_client ( ) , 0 ) ;
532
558
533
559
// mine blocks to get initial balance
534
- let addr = env. client . get_new_address ( None , None ) ?. assume_checked ( ) ;
560
+ let addr = env
561
+ . rpc_client ( )
562
+ . get_new_address ( None , None ) ?
563
+ . assume_checked ( ) ;
535
564
env. mine_blocks ( PREMINE_COUNT , Some ( addr. clone ( ) ) ) ?;
536
565
537
566
// introduce mempool tx at each block extension
@@ -549,7 +578,7 @@ fn mempool_during_reorg() -> anyhow::Result<()> {
549
578
. into_iter( )
550
579
. map( |( tx, _) | tx. txid( ) )
551
580
. collect:: <BTreeSet <_>>( ) ,
552
- env. client
581
+ env. rpc_client ( )
553
582
. get_raw_mempool( ) ?
554
583
. into_iter( )
555
584
. collect:: <BTreeSet <_>>( ) ,
@@ -568,7 +597,7 @@ fn mempool_during_reorg() -> anyhow::Result<()> {
568
597
// emission.
569
598
// TODO: How can have have reorg logic in `TestEnv` NOT blacklast old blocks first?
570
599
let tx_introductions = dbg ! ( env
571
- . client
600
+ . rpc_client ( )
572
601
. get_raw_mempool_verbose( ) ?
573
602
. into_iter( )
574
603
. map( |( txid, entry) | ( txid, entry. height as usize ) )
@@ -643,7 +672,7 @@ fn no_agreement_point() -> anyhow::Result<()> {
643
672
let env = TestEnv :: new ( ) ?;
644
673
645
674
// start height is 99
646
- let mut emitter = Emitter :: from_height ( & env. client , ( PREMINE_COUNT - 2 ) as u32 ) ;
675
+ let mut emitter = Emitter :: from_height ( env. rpc_client ( ) , ( PREMINE_COUNT - 2 ) as u32 ) ;
647
676
648
677
// mine 101 blocks
649
678
env. mine_blocks ( PREMINE_COUNT , None ) ?;
@@ -658,12 +687,12 @@ fn no_agreement_point() -> anyhow::Result<()> {
658
687
let block_hash_100a = block_header_100a. block_hash ( ) ;
659
688
660
689
// get hash for block 101a
661
- let block_hash_101a = env. client . get_block_hash ( 101 ) ?;
690
+ let block_hash_101a = env. rpc_client ( ) . get_block_hash ( 101 ) ?;
662
691
663
692
// invalidate blocks 99a, 100a, 101a
664
- env. client . invalidate_block ( & block_hash_99a) ?;
665
- env. client . invalidate_block ( & block_hash_100a) ?;
666
- env. client . invalidate_block ( & block_hash_101a) ?;
693
+ env. rpc_client ( ) . invalidate_block ( & block_hash_99a) ?;
694
+ env. rpc_client ( ) . invalidate_block ( & block_hash_100a) ?;
695
+ env. rpc_client ( ) . invalidate_block ( & block_hash_101a) ?;
667
696
668
697
// mine new blocks 99b, 100b, 101b
669
698
env. mine_blocks ( 3 , None ) ?;
0 commit comments