@@ -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,12 +438,12 @@ 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
448
self . persist_state ( & * state_lock) . await . map_err ( |e| {
448
449
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
@@ -451,13 +452,13 @@ where
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.
@@ -596,7 +603,7 @@ where
596
603
OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE ,
597
604
OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE ,
598
605
OUTPUT_SWEEPER_PERSISTENCE_KEY ,
599
- & sweeper_state. encode ( ) ,
606
+ & sweeper_state. persistent . encode ( ) ,
600
607
)
601
608
. await
602
609
. map_err ( |e| {
@@ -613,7 +620,7 @@ where
613
620
}
614
621
615
622
fn spend_outputs (
616
- & self , sweeper_state : & SweeperState , descriptors : & [ & SpendableOutputDescriptor ] ,
623
+ & self , sweeper_state : & PersistentSweeperState , descriptors : & [ & SpendableOutputDescriptor ] ,
617
624
change_destination_script : ScriptBuf ,
618
625
) -> Result < Transaction , ( ) > {
619
626
let tx_feerate =
@@ -636,7 +643,7 @@ where
636
643
) {
637
644
let confirmation_hash = header. block_hash ( ) ;
638
645
for ( _, tx) in txdata {
639
- for output_info in sweeper_state. outputs . iter_mut ( ) {
646
+ for output_info in sweeper_state. persistent . outputs . iter_mut ( ) {
640
647
if output_info. is_spent_in ( * tx) {
641
648
output_info. status . confirmed ( confirmation_hash, height, ( * tx) . clone ( ) )
642
649
}
@@ -647,7 +654,7 @@ where
647
654
fn best_block_updated_internal (
648
655
& self , sweeper_state : & mut SweeperState , header : & Header , height : u32 ,
649
656
) {
650
- sweeper_state. best_block = BestBlock :: new ( header. block_hash ( ) , height) ;
657
+ sweeper_state. persistent . best_block = BestBlock :: new ( header. block_hash ( ) , height) ;
651
658
self . prune_confirmed_outputs ( sweeper_state) ;
652
659
}
653
660
}
@@ -667,9 +674,9 @@ where
667
674
& self , header : & Header , txdata : & chain:: transaction:: TransactionData , height : u32 ,
668
675
) {
669
676
let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
670
- assert_eq ! ( state_lock. best_block. block_hash, header. prev_blockhash,
677
+ assert_eq ! ( state_lock. persistent . best_block. block_hash, header. prev_blockhash,
671
678
"Blocks must be connected in chain-order - the connected header must build on the last connected header" ) ;
672
- assert_eq ! ( state_lock. best_block. height, height - 1 ,
679
+ assert_eq ! ( state_lock. persistent . best_block. height, height - 1 ,
673
680
"Blocks must be connected in chain-order - the connected block height must be one greater than the previous height" ) ;
674
681
675
682
self . transactions_confirmed_internal ( & mut * state_lock, header, txdata, height) ;
@@ -686,13 +693,13 @@ where
686
693
let new_height = height - 1 ;
687
694
let block_hash = header. block_hash ( ) ;
688
695
689
- assert_eq ! ( state_lock. best_block. block_hash, block_hash,
696
+ assert_eq ! ( state_lock. persistent . best_block. block_hash, block_hash,
690
697
"Blocks must be disconnected in chain-order - the disconnected header must be the last connected header" ) ;
691
- assert_eq ! ( state_lock. best_block. height, height,
698
+ assert_eq ! ( state_lock. persistent . best_block. height, height,
692
699
"Blocks must be disconnected in chain-order - the disconnected block must have the correct height" ) ;
693
- state_lock. best_block = BestBlock :: new ( header. prev_blockhash , new_height) ;
700
+ state_lock. persistent . best_block = BestBlock :: new ( header. prev_blockhash , new_height) ;
694
701
695
- for output_info in state_lock. outputs . iter_mut ( ) {
702
+ for output_info in state_lock. persistent . outputs . iter_mut ( ) {
696
703
if output_info. status . confirmation_hash ( ) == Some ( block_hash) {
697
704
debug_assert_eq ! ( output_info. status. confirmation_height( ) , Some ( height) ) ;
698
705
output_info. status . unconfirmed ( ) ;
@@ -731,6 +738,7 @@ where
731
738
732
739
// Get what height was unconfirmed.
733
740
let unconf_height = state_lock
741
+ . persistent
734
742
. outputs
735
743
. iter ( )
736
744
. find ( |o| o. status . latest_spending_tx ( ) . map ( |tx| tx. compute_txid ( ) ) == Some ( * txid) )
@@ -739,6 +747,7 @@ where
739
747
if let Some ( unconf_height) = unconf_height {
740
748
// Unconfirm all >= this height.
741
749
state_lock
750
+ . persistent
742
751
. outputs
743
752
. iter_mut ( )
744
753
. filter ( |o| o. status . confirmation_height ( ) >= Some ( unconf_height) )
@@ -761,6 +770,7 @@ where
761
770
fn get_relevant_txids ( & self ) -> Vec < ( Txid , u32 , Option < BlockHash > ) > {
762
771
let state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
763
772
state_lock
773
+ . persistent
764
774
. outputs
765
775
. iter ( )
766
776
. filter_map ( |o| match o. status {
@@ -780,13 +790,18 @@ where
780
790
}
781
791
}
782
792
783
- #[ derive( Debug , Clone ) ]
793
+ #[ derive( Debug ) ]
784
794
struct SweeperState {
795
+ persistent : PersistentSweeperState ,
796
+ }
797
+
798
+ #[ derive( Debug , Clone ) ]
799
+ struct PersistentSweeperState {
785
800
outputs : Vec < TrackedSpendableOutput > ,
786
801
best_block : BestBlock ,
787
802
}
788
803
789
- impl_writeable_tlv_based ! ( SweeperState , {
804
+ impl_writeable_tlv_based ! ( PersistentSweeperState , {
790
805
( 0 , outputs, required_vec) ,
791
806
( 2 , best_block, required) ,
792
807
} ) ;
@@ -832,7 +847,7 @@ where
832
847
kv_store,
833
848
logger,
834
849
) = args;
835
- let state = SweeperState :: read ( reader) ?;
850
+ let state = PersistentSweeperState :: read ( reader) ?;
836
851
let best_block = state. best_block ;
837
852
838
853
if let Some ( filter) = chain_data_source. as_ref ( ) {
@@ -842,7 +857,7 @@ where
842
857
}
843
858
}
844
859
845
- let sweeper_state = Mutex :: new ( state) ;
860
+ let sweeper_state = Mutex :: new ( SweeperState { persistent : state } ) ;
846
861
Ok ( Self {
847
862
sweeper_state,
848
863
pending_sweep : AtomicBool :: new ( false ) ,
@@ -881,7 +896,7 @@ where
881
896
kv_store,
882
897
logger,
883
898
) = args;
884
- let state = SweeperState :: read ( reader) ?;
899
+ let state = PersistentSweeperState :: read ( reader) ?;
885
900
let best_block = state. best_block ;
886
901
887
902
if let Some ( filter) = chain_data_source. as_ref ( ) {
@@ -891,7 +906,7 @@ where
891
906
}
892
907
}
893
908
894
- let sweeper_state = Mutex :: new ( state) ;
909
+ let sweeper_state = Mutex :: new ( SweeperState { persistent : state } ) ;
895
910
Ok ( (
896
911
best_block,
897
912
OutputSweeper {
0 commit comments