@@ -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,26 +434,26 @@ 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_locked ( & self ) -> Result < ( ) , ( ) > {
454
- let mut sweeper_state = self . sweeper_state . lock ( ) . unwrap ( ) ;
455
- self . regenerate_and_broadcast_spend_if_necessary ( & mut * sweeper_state)
455
+ let sweeper_state = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
456
+ self . regenerate_and_broadcast_spend_if_necessary ( sweeper_state)
456
457
}
457
458
458
459
fn regenerate_and_broadcast_spend_if_necessary (
@@ -628,22 +629,22 @@ where
628
629
fn filtered_block_connected (
629
630
& self , header : & Header , txdata : & chain:: transaction:: TransactionData , height : u32 ,
630
631
) {
631
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
632
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
632
633
assert_eq ! ( state_lock. best_block. block_hash, header. prev_blockhash,
633
634
"Blocks must be connected in chain-order - the connected header must build on the last connected header" ) ;
634
635
assert_eq ! ( state_lock. best_block. height, height - 1 ,
635
636
"Blocks must be connected in chain-order - the connected block height must be one greater than the previous height" ) ;
636
637
637
- self . transactions_confirmed_internal ( & mut * state_lock, header, txdata, height) ;
638
- self . best_block_updated_internal ( & mut * state_lock, header, height) ;
638
+ self . transactions_confirmed_internal ( state_lock, header, txdata, height) ;
639
+ self . best_block_updated_internal ( state_lock, header, height) ;
639
640
640
- let _ = self . persist_state ( & * state_lock) . map_err ( |e| {
641
+ let _ = self . persist_state ( & state_lock) . map_err ( |e| {
641
642
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
642
643
} ) ;
643
644
}
644
645
645
646
fn block_disconnected ( & self , header : & Header , height : u32 ) {
646
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
647
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
647
648
648
649
let new_height = height - 1 ;
649
650
let block_hash = header. block_hash ( ) ;
@@ -681,15 +682,15 @@ where
681
682
fn transactions_confirmed (
682
683
& self , header : & Header , txdata : & chain:: transaction:: TransactionData , height : u32 ,
683
684
) {
684
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
685
- self . transactions_confirmed_internal ( & mut * state_lock, header, txdata, height) ;
686
- self . persist_state ( & * state_lock) . unwrap_or_else ( |e| {
685
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
686
+ self . transactions_confirmed_internal ( state_lock, header, txdata, height) ;
687
+ self . persist_state ( state_lock) . unwrap_or_else ( |e| {
687
688
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
688
689
} ) ;
689
690
}
690
691
691
692
fn transaction_unconfirmed ( & self , txid : & Txid ) {
692
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
693
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
693
694
694
695
// Get what height was unconfirmed.
695
696
let unconf_height = state_lock
@@ -706,22 +707,22 @@ where
706
707
. filter ( |o| o. status . confirmation_height ( ) >= Some ( unconf_height) )
707
708
. for_each ( |o| o. status . unconfirmed ( ) ) ;
708
709
709
- self . persist_state ( & * state_lock) . unwrap_or_else ( |e| {
710
+ self . persist_state ( state_lock) . unwrap_or_else ( |e| {
710
711
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
711
712
} ) ;
712
713
}
713
714
}
714
715
715
716
fn best_block_updated ( & self , header : & Header , height : u32 ) {
716
- let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
717
- self . best_block_updated_internal ( & mut * state_lock, header, height) ;
718
- let _ = self . persist_state ( & * state_lock) . map_err ( |e| {
717
+ let state_lock = & mut self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
718
+ self . best_block_updated_internal ( state_lock, header, height) ;
719
+ let _ = self . persist_state ( state_lock) . map_err ( |e| {
719
720
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
720
721
} ) ;
721
722
}
722
723
723
724
fn get_relevant_txids ( & self ) -> Vec < ( Txid , u32 , Option < BlockHash > ) > {
724
- let state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
725
+ let state_lock = & self . sweeper_state . lock ( ) . unwrap ( ) . persistent ;
725
726
state_lock
726
727
. outputs
727
728
. iter ( )
@@ -742,6 +743,11 @@ where
742
743
}
743
744
}
744
745
746
+ #[ derive( Debug , Clone ) ]
747
+ struct RuntimeSweeperState {
748
+ persistent : SweeperState ,
749
+ }
750
+
745
751
#[ derive( Debug , Clone ) ]
746
752
struct SweeperState {
747
753
outputs : Vec < TrackedSpendableOutput > ,
@@ -804,7 +810,7 @@ where
804
810
}
805
811
}
806
812
807
- let sweeper_state = Mutex :: new ( state) ;
813
+ let sweeper_state = Mutex :: new ( RuntimeSweeperState { persistent : state } ) ;
808
814
Ok ( Self {
809
815
sweeper_state,
810
816
broadcaster,
@@ -852,7 +858,7 @@ where
852
858
}
853
859
}
854
860
855
- let sweeper_state = Mutex :: new ( state) ;
861
+ let sweeper_state = Mutex :: new ( RuntimeSweeperState { persistent : state } ) ;
856
862
Ok ( (
857
863
best_block,
858
864
OutputSweeper {
0 commit comments