@@ -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,32 @@ where
437
438
} ,
438
439
} ;
439
440
440
- if state_lock. outputs . iter ( ) . find ( |o| o. descriptor == output_info. descriptor ) . is_some ( )
441
+ if state_lock
442
+ . persistent
443
+ . outputs
444
+ . iter ( )
445
+ . find ( |o| o. descriptor == output_info. descriptor )
446
+ . is_some ( )
441
447
{
442
448
continue ;
443
449
}
444
450
445
- state_lock. outputs . push ( output_info) ;
451
+ state_lock. persistent . outputs . push ( output_info) ;
446
452
}
447
- self . persist_state ( & * state_lock) . map_err ( |e| {
453
+ self . persist_state ( & state_lock) . map_err ( |e| {
448
454
log_error ! ( self . logger, "Error persisting OutputSweeper: {:?}" , e) ;
449
455
} )
450
456
}
451
457
452
458
/// Returns a list of the currently tracked spendable outputs.
453
459
pub fn tracked_spendable_outputs ( & self ) -> Vec < TrackedSpendableOutput > {
454
- self . sweeper_state . lock ( ) . unwrap ( ) . outputs . clone ( )
460
+ self . sweeper_state . lock ( ) . unwrap ( ) . persistent . outputs . clone ( )
455
461
}
456
462
457
463
/// Gets the latest best block which was connected either via the [`Listen`] or
458
464
/// [`Confirm`] interfaces.
459
465
pub fn current_best_block ( & self ) -> BestBlock {
460
- self . sweeper_state . lock ( ) . unwrap ( ) . best_block
466
+ self . sweeper_state . lock ( ) . unwrap ( ) . persistent . best_block
461
467
}
462
468
463
469
/// Regenerates and broadcasts the spending transaction for any outputs that are pending. This method will be a
@@ -505,8 +511,9 @@ where
505
511
{
506
512
let sweeper_state = self . sweeper_state . lock ( ) . unwrap ( ) ;
507
513
508
- let cur_height = sweeper_state. best_block . height ;
509
- let has_respends = sweeper_state. outputs . iter ( ) . any ( |o| filter_fn ( o, cur_height) ) ;
514
+ let cur_height = sweeper_state. persistent . best_block . height ;
515
+ let has_respends =
516
+ sweeper_state. persistent . outputs . iter ( ) . any ( |o| filter_fn ( o, cur_height) ) ;
510
517
if !has_respends {
511
518
return Ok ( ( ) ) ;
512
519
}
@@ -520,10 +527,11 @@ where
520
527
{
521
528
let mut sweeper_state = self . sweeper_state . lock ( ) . unwrap ( ) ;
522
529
523
- let cur_height = sweeper_state. best_block . height ;
524
- let cur_hash = sweeper_state. best_block . block_hash ;
530
+ let cur_height = sweeper_state. persistent . best_block . height ;
531
+ let cur_hash = sweeper_state. persistent . best_block . block_hash ;
525
532
526
533
let respend_descriptors: Vec < & SpendableOutputDescriptor > = sweeper_state
534
+ . persistent
527
535
. outputs
528
536
. iter ( )
529
537
. filter ( |o| filter_fn ( * o, cur_height) )
@@ -536,7 +544,11 @@ where
536
544
}
537
545
538
546
let spending_tx = self
539
- . spend_outputs ( & sweeper_state, & respend_descriptors, change_destination_script)
547
+ . spend_outputs (
548
+ & sweeper_state. persistent ,
549
+ & respend_descriptors,
550
+ change_destination_script,
551
+ )
540
552
. map_err ( |e| {
541
553
log_error ! ( self . logger, "Error spending outputs: {:?}" , e) ;
542
554
} ) ?;
@@ -550,7 +562,7 @@ where
550
562
// As we didn't modify the state so far, the same filter_fn yields the same elements as
551
563
// above.
552
564
let respend_outputs =
553
- sweeper_state. outputs . iter_mut ( ) . filter ( |o| filter_fn ( & * * o, cur_height) ) ;
565
+ sweeper_state. persistent . outputs . iter_mut ( ) . filter ( |o| filter_fn ( & * * o, cur_height) ) ;
554
566
for output_info in respend_outputs {
555
567
if let Some ( filter) = self . chain_data_source . as_ref ( ) {
556
568
let watched_output = output_info. to_watched_output ( cur_hash) ;
@@ -571,10 +583,10 @@ where
571
583
}
572
584
573
585
fn prune_confirmed_outputs ( & self , sweeper_state : & mut SweeperState ) {
574
- let cur_height = sweeper_state. best_block . height ;
586
+ let cur_height = sweeper_state. persistent . best_block . height ;
575
587
576
588
// Prune all outputs that have sufficient depth by now.
577
- sweeper_state. outputs . retain ( |o| {
589
+ sweeper_state. persistent . outputs . retain ( |o| {
578
590
if let Some ( confirmation_height) = o. status . confirmation_height ( ) {
579
591
// We wait at least `PRUNE_DELAY_BLOCKS` as before that
580
592
// `Event::SpendableOutputs` from lingering monitors might get replayed.
@@ -596,7 +608,7 @@ where
596
608
OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE ,
597
609
OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE ,
598
610
OUTPUT_SWEEPER_PERSISTENCE_KEY ,
599
- & sweeper_state. encode ( ) ,
611
+ & sweeper_state. persistent . encode ( ) ,
600
612
)
601
613
. map_err ( |e| {
602
614
log_error ! (
@@ -612,7 +624,7 @@ where
612
624
}
613
625
614
626
fn spend_outputs (
615
- & self , sweeper_state : & SweeperState , descriptors : & [ & SpendableOutputDescriptor ] ,
627
+ & self , sweeper_state : & PersistentSweeperState , descriptors : & [ & SpendableOutputDescriptor ] ,
616
628
change_destination_script : ScriptBuf ,
617
629
) -> Result < Transaction , ( ) > {
618
630
let tx_feerate =
@@ -635,7 +647,7 @@ where
635
647
) {
636
648
let confirmation_hash = header. block_hash ( ) ;
637
649
for ( _, tx) in txdata {
638
- for output_info in sweeper_state. outputs . iter_mut ( ) {
650
+ for output_info in sweeper_state. persistent . outputs . iter_mut ( ) {
639
651
if output_info. is_spent_in ( * tx) {
640
652
output_info. status . confirmed ( confirmation_hash, height, ( * tx) . clone ( ) )
641
653
}
@@ -646,7 +658,7 @@ where
646
658
fn best_block_updated_internal (
647
659
& self , sweeper_state : & mut SweeperState , header : & Header , height : u32 ,
648
660
) {
649
- sweeper_state. best_block = BestBlock :: new ( header. block_hash ( ) , height) ;
661
+ sweeper_state. persistent . best_block = BestBlock :: new ( header. block_hash ( ) , height) ;
650
662
self . prune_confirmed_outputs ( sweeper_state) ;
651
663
}
652
664
}
@@ -666,9 +678,9 @@ where
666
678
& self , header : & Header , txdata : & chain:: transaction:: TransactionData , height : u32 ,
667
679
) {
668
680
let mut state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
669
- assert_eq ! ( state_lock. best_block. block_hash, header. prev_blockhash,
681
+ assert_eq ! ( state_lock. persistent . best_block. block_hash, header. prev_blockhash,
670
682
"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 ,
683
+ assert_eq ! ( state_lock. persistent . best_block. height, height - 1 ,
672
684
"Blocks must be connected in chain-order - the connected block height must be one greater than the previous height" ) ;
673
685
674
686
self . transactions_confirmed_internal ( & mut * state_lock, header, txdata, height) ;
@@ -685,13 +697,13 @@ where
685
697
let new_height = height - 1 ;
686
698
let block_hash = header. block_hash ( ) ;
687
699
688
- assert_eq ! ( state_lock. best_block. block_hash, block_hash,
700
+ assert_eq ! ( state_lock. persistent . best_block. block_hash, block_hash,
689
701
"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,
702
+ assert_eq ! ( state_lock. persistent . best_block. height, height,
691
703
"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) ;
704
+ state_lock. persistent . best_block = BestBlock :: new ( header. prev_blockhash , new_height) ;
693
705
694
- for output_info in state_lock. outputs . iter_mut ( ) {
706
+ for output_info in state_lock. persistent . outputs . iter_mut ( ) {
695
707
if output_info. status . confirmation_hash ( ) == Some ( block_hash) {
696
708
debug_assert_eq ! ( output_info. status. confirmation_height( ) , Some ( height) ) ;
697
709
output_info. status . unconfirmed ( ) ;
@@ -730,6 +742,7 @@ where
730
742
731
743
// Get what height was unconfirmed.
732
744
let unconf_height = state_lock
745
+ . persistent
733
746
. outputs
734
747
. iter ( )
735
748
. find ( |o| o. status . latest_spending_tx ( ) . map ( |tx| tx. compute_txid ( ) ) == Some ( * txid) )
@@ -738,6 +751,7 @@ where
738
751
if let Some ( unconf_height) = unconf_height {
739
752
// Unconfirm all >= this height.
740
753
state_lock
754
+ . persistent
741
755
. outputs
742
756
. iter_mut ( )
743
757
. filter ( |o| o. status . confirmation_height ( ) >= Some ( unconf_height) )
@@ -760,6 +774,7 @@ where
760
774
fn get_relevant_txids ( & self ) -> Vec < ( Txid , u32 , Option < BlockHash > ) > {
761
775
let state_lock = self . sweeper_state . lock ( ) . unwrap ( ) ;
762
776
state_lock
777
+ . persistent
763
778
. outputs
764
779
. iter ( )
765
780
. filter_map ( |o| match o. status {
@@ -779,13 +794,18 @@ where
779
794
}
780
795
}
781
796
782
- #[ derive( Debug , Clone ) ]
797
+ #[ derive( Debug ) ]
783
798
struct SweeperState {
799
+ persistent : PersistentSweeperState ,
800
+ }
801
+
802
+ #[ derive( Debug , Clone ) ]
803
+ struct PersistentSweeperState {
784
804
outputs : Vec < TrackedSpendableOutput > ,
785
805
best_block : BestBlock ,
786
806
}
787
807
788
- impl_writeable_tlv_based ! ( SweeperState , {
808
+ impl_writeable_tlv_based ! ( PersistentSweeperState , {
789
809
( 0 , outputs, required_vec) ,
790
810
( 2 , best_block, required) ,
791
811
} ) ;
@@ -831,7 +851,7 @@ where
831
851
kv_store,
832
852
logger,
833
853
) = args;
834
- let state = SweeperState :: read ( reader) ?;
854
+ let state = PersistentSweeperState :: read ( reader) ?;
835
855
let best_block = state. best_block ;
836
856
837
857
if let Some ( filter) = chain_data_source. as_ref ( ) {
@@ -841,7 +861,7 @@ where
841
861
}
842
862
}
843
863
844
- let sweeper_state = Mutex :: new ( state) ;
864
+ let sweeper_state = Mutex :: new ( SweeperState { persistent : state } ) ;
845
865
Ok ( Self {
846
866
sweeper_state,
847
867
pending_sweep : AtomicBool :: new ( false ) ,
@@ -880,7 +900,7 @@ where
880
900
kv_store,
881
901
logger,
882
902
) = args;
883
- let state = SweeperState :: read ( reader) ?;
903
+ let state = PersistentSweeperState :: read ( reader) ?;
884
904
let best_block = state. best_block ;
885
905
886
906
if let Some ( filter) = chain_data_source. as_ref ( ) {
@@ -890,7 +910,7 @@ where
890
910
}
891
911
}
892
912
893
- let sweeper_state = Mutex :: new ( state) ;
913
+ let sweeper_state = Mutex :: new ( SweeperState { persistent : state } ) ;
894
914
Ok ( (
895
915
best_block,
896
916
OutputSweeper {
0 commit comments