@@ -342,7 +342,7 @@ where
342
342
L :: Target : Logger ,
343
343
O :: Target : OutputSpender ,
344
344
{
345
- sweeper_state : Mutex < SweeperState > ,
345
+ sweeper_state : Mutex < RuntimeSweeperState > ,
346
346
broadcaster : B ,
347
347
fee_estimator : E ,
348
348
chain_data_source : Option < F > ,
@@ -372,7 +372,8 @@ where
372
372
output_spender : O , change_destination_source : D , kv_store : K , logger : L ,
373
373
) -> Self {
374
374
let outputs = Vec :: new ( ) ;
375
- let sweeper_state = Mutex :: new ( SweeperState { outputs, best_block } ) ;
375
+ let sweeper_state =
376
+ Mutex :: new ( RuntimeSweeperState { persistent : SweeperState { outputs, best_block } } ) ;
376
377
Self {
377
378
sweeper_state,
378
379
broadcaster,
@@ -416,7 +417,7 @@ where
416
417
return Ok ( ( ) ) ;
417
418
}
418
419
419
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
420
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
420
421
for descriptor in relevant_descriptors {
421
422
let output_info = TrackedSpendableOutput {
422
423
descriptor,
@@ -433,25 +434,25 @@ where
433
434
434
435
state_lock. outputs . push ( output_info) ;
435
436
}
436
- self . persist_state ( & * state_lock) . map_err ( |e| {
437
+ self . persist_state ( & state_lock) . map_err ( |e| {
437
438
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
438
439
} )
439
440
}
440
441
441
442
/// Returns a list of the currently tracked spendable outputs.
442
443
pub fn tracked_spendable_outputs ( & self ) -> Vec < TrackedSpendableOutput > {
443
- self . sweeper_state . lock ( ) . unwrap ( ) . outputs . clone ( )
444
+ self . sweeper_state . lock ( ) . unwrap ( ) . persistent . outputs . clone ( )
444
445
}
445
446
446
447
/// Gets the latest best block which was connected either via the [`Listen`] or
447
448
/// [`Confirm`] interfaces.
448
449
pub fn current_best_block ( & self ) -> BestBlock {
449
- self . sweeper_state . lock ( ) . unwrap ( ) . best_block
450
+ self . sweeper_state . lock ( ) . unwrap ( ) . persistent . best_block
450
451
}
451
452
452
453
/// Regenerates and broadcasts the spending transaction for any outputs that are pending
453
454
pub fn regenerate_and_broadcast_spend_if_necessary ( & self ) -> Result < ( ) , ( ) > {
454
- let mut sweeper_state = self . sweeper_state . lock ( ) . unwrap ( ) ;
455
+ let sweeper_state = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
455
456
456
457
let cur_height = sweeper_state. best_block . height ;
457
458
let cur_hash = sweeper_state. best_block . block_hash ;
@@ -614,22 +615,22 @@ where
614
615
fn filtered_block_connected (
615
616
& self , header : & Header , txdata : & chain:: transaction:: TransactionData , height : u32 ,
616
617
) {
617
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
618
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
618
619
assert_eq ! ( state_lock. best_block. block_hash, header. prev_blockhash,
619
620
"Blocks must be connected in chain-order - the connected header must build on the last connected header" ) ;
620
621
assert_eq ! ( state_lock. best_block. height, height - 1 ,
621
622
"Blocks must be connected in chain-order - the connected block height must be one greater than the previous height" ) ;
622
623
623
- self . transactions_confirmed_internal ( & mut * state_lock, header, txdata, height) ;
624
- self . best_block_updated_internal ( & mut * state_lock, header, height) ;
624
+ self . transactions_confirmed_internal ( state_lock, header, txdata, height) ;
625
+ self . best_block_updated_internal ( state_lock, header, height) ;
625
626
626
- let _ = self . persist_state ( & * state_lock) . map_err ( |e| {
627
+ let _ = self . persist_state ( & state_lock) . map_err ( |e| {
627
628
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
628
629
} ) ;
629
630
}
630
631
631
632
fn block_disconnected ( & self , header : & Header , height : u32 ) {
632
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
633
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
633
634
634
635
let new_height = height - 1 ;
635
636
let block_hash = header. block_hash ( ) ;
@@ -667,15 +668,15 @@ where
667
668
fn transactions_confirmed (
668
669
& self , header : & Header , txdata : & chain:: transaction:: TransactionData , height : u32 ,
669
670
) {
670
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
671
- self . transactions_confirmed_internal ( & mut * state_lock, header, txdata, height) ;
672
- self . persist_state ( & * state_lock) . unwrap_or_else ( |e| {
671
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
672
+ self . transactions_confirmed_internal ( state_lock, header, txdata, height) ;
673
+ self . persist_state ( state_lock) . unwrap_or_else ( |e| {
673
674
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
674
675
} ) ;
675
676
}
676
677
677
678
fn transaction_unconfirmed ( & self , txid : & Txid ) {
678
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
679
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
679
680
680
681
// Get what height was unconfirmed.
681
682
let unconf_height = state_lock
@@ -692,22 +693,22 @@ where
692
693
. filter ( |o| o. status . confirmation_height ( ) >= Some ( unconf_height) )
693
694
. for_each ( |o| o. status . unconfirmed ( ) ) ;
694
695
695
- self . persist_state ( & * state_lock) . unwrap_or_else ( |e| {
696
+ self . persist_state ( state_lock) . unwrap_or_else ( |e| {
696
697
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
697
698
} ) ;
698
699
}
699
700
}
700
701
701
702
fn best_block_updated ( & self , header : & Header , height : u32 ) {
702
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
703
- self . best_block_updated_internal ( & mut * state_lock, header, height) ;
704
- let _ = self . persist_state ( & * state_lock) . map_err ( |e| {
703
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
704
+ self . best_block_updated_internal ( state_lock, header, height) ;
705
+ let _ = self . persist_state ( state_lock) . map_err ( |e| {
705
706
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
706
707
} ) ;
707
708
}
708
709
709
710
fn get_relevant_txids ( & self ) -> Vec < ( Txid , u32 , Option < BlockHash > ) > {
710
- let state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
711
+ let state_lock = & self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
711
712
state_lock
712
713
. outputs
713
714
. iter ( )
@@ -728,6 +729,11 @@ where
728
729
}
729
730
}
730
731
732
+ #[ derive( Debug , Clone ) ]
733
+ struct RuntimeSweeperState {
734
+ persistent : SweeperState ,
735
+ }
736
+
731
737
#[ derive( Debug , Clone ) ]
732
738
struct SweeperState {
733
739
outputs : Vec < TrackedSpendableOutput > ,
@@ -790,7 +796,7 @@ where
790
796
}
791
797
}
792
798
793
- let sweeper_state = Mutex :: new ( state) ;
799
+ let sweeper_state = Mutex :: new ( RuntimeSweeperState { persistent : state } ) ;
794
800
Ok ( Self {
795
801
sweeper_state,
796
802
broadcaster,
@@ -838,7 +844,7 @@ where
838
844
}
839
845
}
840
846
841
- let sweeper_state = Mutex :: new ( state) ;
847
+ let sweeper_state = Mutex :: new ( RuntimeSweeperState { persistent : state } ) ;
842
848
Ok ( (
843
849
best_block,
844
850
OutputSweeper {
0 commit comments