@@ -382,7 +382,8 @@ where
382
382
output_spender : O , change_destination_source : D , kv_store : K , logger : L ,
383
383
) -> Self {
384
384
let outputs = Vec :: new ( ) ;
385
- let sweeper_state = Mutex :: new ( SweeperState { outputs, best_block } ) ;
385
+ let sweeper_state =
386
+ Mutex :: new ( SweeperState { persistent : PersistentSweeperState { outputs, best_block } } ) ;
386
387
Self {
387
388
sweeper_state,
388
389
pending_sweep : AtomicBool :: new ( false ) ,
@@ -437,27 +438,27 @@ where
437
438
} ,
438
439
} ;
439
440
440
- if state_lock . outputs . iter ( ) . find ( |o| o . descriptor == output_info . descriptor ) . is_some ( )
441
- {
441
+ let mut outputs = state_lock . persistent . outputs . iter ( ) ;
442
+ if outputs . find ( |o| o . descriptor == output_info . descriptor ) . is_some ( ) {
442
443
continue ;
443
444
}
444
445
445
- state_lock. outputs . push ( output_info) ;
446
+ state_lock. persistent . outputs . push ( output_info) ;
446
447
}
447
- self . persist_state ( & * state_lock) . map_err ( |e| {
448
+ self . persist_state ( & state_lock) . map_err ( |e| {
448
449
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
449
450
} )
450
451
}
451
452
452
453
/// Returns a list of the currently tracked spendable outputs.
453
454
pub fn tracked_spendable_outputs ( & self ) -> Vec < TrackedSpendableOutput > {
454
- self . sweeper_state . lock ( ) . unwrap ( ) . outputs . clone ( )
455
+ self . sweeper_state . lock ( ) . unwrap ( ) . persistent . outputs . clone ( )
455
456
}
456
457
457
458
/// Gets the latest best block which was connected either via the [`Listen`] or
458
459
/// [`Confirm`] interfaces.
459
460
pub fn current_best_block ( & self ) -> BestBlock {
460
- self . sweeper_state . lock ( ) . unwrap ( ) . best_block
461
+ self . sweeper_state . lock ( ) . unwrap ( ) . persistent . best_block
461
462
}
462
463
463
464
/// Regenerates and broadcasts the spending transaction for any outputs that are pending. This method will be a
@@ -505,8 +506,9 @@ where
505
506
{
506
507
let sweeper_state = self . sweeper_state . lock ( ) . unwrap ( ) ;
507
508
508
- let cur_height = sweeper_state. best_block . height ;
509
- let has_respends = sweeper_state. outputs . iter ( ) . any ( |o| filter_fn ( o, cur_height) ) ;
509
+ let cur_height = sweeper_state. persistent . best_block . height ;
510
+ let has_respends =
511
+ sweeper_state. persistent . outputs . iter ( ) . any ( |o| filter_fn ( o, cur_height) ) ;
510
512
if !has_respends {
511
513
return Ok ( ( ) ) ;
512
514
}
@@ -520,10 +522,11 @@ where
520
522
{
521
523
let mut sweeper_state = self . sweeper_state . lock ( ) . unwrap ( ) ;
522
524
523
- let cur_height = sweeper_state. best_block . height ;
524
- let cur_hash = sweeper_state. best_block . block_hash ;
525
+ let cur_height = sweeper_state. persistent . best_block . height ;
526
+ let cur_hash = sweeper_state. persistent . best_block . block_hash ;
525
527
526
528
let respend_descriptors: Vec < & SpendableOutputDescriptor > = sweeper_state
529
+ . persistent
527
530
. outputs
528
531
. iter ( )
529
532
. filter ( |o| filter_fn ( * o, cur_height) )
@@ -536,7 +539,11 @@ where
536
539
}
537
540
538
541
let spending_tx = self
539
- . spend_outputs ( & sweeper_state, & respend_descriptors, change_destination_script)
542
+ . spend_outputs (
543
+ & sweeper_state. persistent ,
544
+ & respend_descriptors,
545
+ change_destination_script,
546
+ )
540
547
. map_err ( |e| {
541
548
log_error ! ( self . logger, "Error spending outputs: {:?}" , e) ;
542
549
} ) ?;
@@ -550,7 +557,7 @@ where
550
557
// As we didn't modify the state so far, the same filter_fn yields the same elements as
551
558
// above.
552
559
let respend_outputs =
553
- sweeper_state. outputs . iter_mut ( ) . filter ( |o| filter_fn ( & * * o, cur_height) ) ;
560
+ sweeper_state. persistent . outputs . iter_mut ( ) . filter ( |o| filter_fn ( & * * o, cur_height) ) ;
554
561
for output_info in respend_outputs {
555
562
if let Some ( filter) = self . chain_data_source . as_ref ( ) {
556
563
let watched_output = output_info. to_watched_output ( cur_hash) ;
@@ -571,10 +578,10 @@ where
571
578
}
572
579
573
580
fn prune_confirmed_outputs ( & self , sweeper_state : & mut SweeperState ) {
574
- let cur_height = sweeper_state. best_block . height ;
581
+ let cur_height = sweeper_state. persistent . best_block . height ;
575
582
576
583
// Prune all outputs that have sufficient depth by now.
577
- sweeper_state. outputs . retain ( |o| {
584
+ sweeper_state. persistent . outputs . retain ( |o| {
578
585
if let Some ( confirmation_height) = o. status . confirmation_height ( ) {
579
586
// We wait at least `PRUNE_DELAY_BLOCKS` as before that
580
587
// `Event::SpendableOutputs` from lingering monitors might get replayed.
@@ -590,7 +597,7 @@ where
590
597
} ) ;
591
598
}
592
599
593
- fn persist_state ( & self , sweeper_state : & SweeperState ) -> Result < ( ) , io:: Error > {
600
+ fn persist_state ( & self , sweeper_state : & PersistentSweeperState ) -> Result < ( ) , io:: Error > {
594
601
self . kv_store
595
602
. write (
596
603
OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE ,
@@ -612,7 +619,7 @@ where
612
619
}
613
620
614
621
fn spend_outputs (
615
- & self , sweeper_state : & SweeperState , descriptors : & [ & SpendableOutputDescriptor ] ,
622
+ & self , sweeper_state : & PersistentSweeperState , descriptors : & [ & SpendableOutputDescriptor ] ,
616
623
change_destination_script : ScriptBuf ,
617
624
) -> Result < Transaction , ( ) > {
618
625
let tx_feerate =
@@ -635,7 +642,7 @@ where
635
642
) {
636
643
let confirmation_hash = header. block_hash ( ) ;
637
644
for ( _, tx) in txdata {
638
- for output_info in sweeper_state. outputs . iter_mut ( ) {
645
+ for output_info in sweeper_state. persistent . outputs . iter_mut ( ) {
639
646
if output_info. is_spent_in ( * tx) {
640
647
output_info. status . confirmed ( confirmation_hash, height, ( * tx) . clone ( ) )
641
648
}
@@ -646,7 +653,7 @@ where
646
653
fn best_block_updated_internal (
647
654
& self , sweeper_state : & mut SweeperState , header : & Header , height : u32 ,
648
655
) {
649
- sweeper_state. best_block = BestBlock :: new ( header. block_hash ( ) , height) ;
656
+ sweeper_state. persistent . best_block = BestBlock :: new ( header. block_hash ( ) , height) ;
650
657
self . prune_confirmed_outputs ( sweeper_state) ;
651
658
}
652
659
}
@@ -666,9 +673,9 @@ where
666
673
& self , header : & Header , txdata : & chain:: transaction:: TransactionData , height : u32 ,
667
674
) {
668
675
let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
669
- assert_eq ! ( state_lock. best_block. block_hash, header. prev_blockhash,
676
+ assert_eq ! ( state_lock. persistent . best_block. block_hash, header. prev_blockhash,
670
677
"Blocks must be connected in chain-order - the connected header must build on the last connected header" ) ;
671
- assert_eq ! ( state_lock. best_block. height, height - 1 ,
678
+ assert_eq ! ( state_lock. persistent . best_block. height, height - 1 ,
672
679
"Blocks must be connected in chain-order - the connected block height must be one greater than the previous height" ) ;
673
680
674
681
self . transactions_confirmed_internal ( & mut * state_lock, header, txdata, height) ;
@@ -685,13 +692,13 @@ where
685
692
let new_height = height - 1 ;
686
693
let block_hash = header. block_hash ( ) ;
687
694
688
- assert_eq ! ( state_lock. best_block. block_hash, block_hash,
695
+ assert_eq ! ( state_lock. persistent . best_block. block_hash, block_hash,
689
696
"Blocks must be disconnected in chain-order - the disconnected header must be the last connected header" ) ;
690
- assert_eq ! ( state_lock. best_block. height, height,
697
+ assert_eq ! ( state_lock. persistent . best_block. height, height,
691
698
"Blocks must be disconnected in chain-order - the disconnected block must have the correct height" ) ;
692
- state_lock. best_block = BestBlock :: new ( header. prev_blockhash , new_height) ;
699
+ state_lock. persistent . best_block = BestBlock :: new ( header. prev_blockhash , new_height) ;
693
700
694
- for output_info in state_lock. outputs . iter_mut ( ) {
701
+ for output_info in state_lock. persistent . outputs . iter_mut ( ) {
695
702
if output_info. status . confirmation_hash ( ) == Some ( block_hash) {
696
703
debug_assert_eq ! ( output_info. status. confirmation_height( ) , Some ( height) ) ;
697
704
output_info. status . unconfirmed ( ) ;
@@ -730,6 +737,7 @@ where
730
737
731
738
// Get what height was unconfirmed.
732
739
let unconf_height = state_lock
740
+ . persistent
733
741
. outputs
734
742
. iter ( )
735
743
. find ( |o| o. status . latest_spending_tx ( ) . map ( |tx| tx. compute_txid ( ) ) == Some ( * txid) )
@@ -738,6 +746,7 @@ where
738
746
if let Some ( unconf_height) = unconf_height {
739
747
// Unconfirm all >= this height.
740
748
state_lock
749
+ . persistent
741
750
. outputs
742
751
. iter_mut ( )
743
752
. filter ( |o| o. status . confirmation_height ( ) >= Some ( unconf_height) )
@@ -760,6 +769,7 @@ where
760
769
fn get_relevant_txids ( & self ) -> Vec < ( Txid , u32 , Option < BlockHash > ) > {
761
770
let state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
762
771
state_lock
772
+ . persistent
763
773
. outputs
764
774
. iter ( )
765
775
. filter_map ( |o| match o. status {
@@ -779,13 +789,18 @@ where
779
789
}
780
790
}
781
791
782
- #[ derive( Debug , Clone ) ]
792
+ #[ derive( Debug ) ]
783
793
struct SweeperState {
794
+ persistent : PersistentSweeperState ,
795
+ }
796
+
797
+ #[ derive( Debug , Clone ) ]
798
+ struct PersistentSweeperState {
784
799
outputs : Vec < TrackedSpendableOutput > ,
785
800
best_block : BestBlock ,
786
801
}
787
802
788
- impl_writeable_tlv_based ! ( SweeperState , {
803
+ impl_writeable_tlv_based ! ( PersistentSweeperState , {
789
804
( 0 , outputs, required_vec) ,
790
805
( 2 , best_block, required) ,
791
806
} ) ;
@@ -831,7 +846,7 @@ where
831
846
kv_store,
832
847
logger,
833
848
) = args;
834
- let state = SweeperState :: read ( reader) ?;
849
+ let state = PersistentSweeperState :: read ( reader) ?;
835
850
let best_block = state. best_block ;
836
851
837
852
if let Some ( filter) = chain_data_source. as_ref ( ) {
@@ -841,7 +856,7 @@ where
841
856
}
842
857
}
843
858
844
- let sweeper_state = Mutex :: new ( state) ;
859
+ let sweeper_state = Mutex :: new ( SweeperState { persistent : state } ) ;
845
860
Ok ( Self {
846
861
sweeper_state,
847
862
pending_sweep : AtomicBool :: new ( false ) ,
@@ -880,7 +895,7 @@ where
880
895
kv_store,
881
896
logger,
882
897
) = args;
883
- let state = SweeperState :: read ( reader) ?;
898
+ let state = PersistentSweeperState :: read ( reader) ?;
884
899
let best_block = state. best_block ;
885
900
886
901
if let Some ( filter) = chain_data_source. as_ref ( ) {
@@ -890,7 +905,7 @@ where
890
905
}
891
906
}
892
907
893
- let sweeper_state = Mutex :: new ( state) ;
908
+ let sweeper_state = Mutex :: new ( SweeperState { persistent : state } ) ;
894
909
Ok ( (
895
910
best_block,
896
911
OutputSweeper {
0 commit comments