Skip to content

Commit 27b7347

Browse files
committed
Separate sweeper persistent state
Prepare for adding runtime state while avoiding the _unused serialization macro config.
1 parent 55426d7 commit 27b7347

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

lightning/src/util/sweep.rs

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ where
382382
output_spender: O, change_destination_source: D, kv_store: K, logger: L,
383383
) -> Self {
384384
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 } });
386387
Self {
387388
sweeper_state,
388389
pending_sweep: AtomicBool::new(false),
@@ -437,12 +438,12 @@ where
437438
},
438439
};
439440

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() {
442443
continue;
443444
}
444445

445-
state_lock.outputs.push(output_info);
446+
state_lock.persistent.outputs.push(output_info);
446447
}
447448
self.persist_state(&*state_lock).await.map_err(|e| {
448449
log_error!(self.logger, "Error persisting OutputSweeper: {:?}", e);
@@ -451,13 +452,13 @@ where
451452

452453
/// Returns a list of the currently tracked spendable outputs.
453454
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()
455456
}
456457

457458
/// Gets the latest best block which was connected either via the [`Listen`] or
458459
/// [`Confirm`] interfaces.
459460
pub fn current_best_block(&self) -> BestBlock {
460-
self.sweeper_state.lock().unwrap().best_block
461+
self.sweeper_state.lock().unwrap().persistent.best_block
461462
}
462463

463464
/// Regenerates and broadcasts the spending transaction for any outputs that are pending. This method will be a
@@ -505,8 +506,9 @@ where
505506
{
506507
let sweeper_state = self.sweeper_state.lock().unwrap();
507508

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));
510512
if !has_respends {
511513
return Ok(());
512514
}
@@ -520,10 +522,11 @@ where
520522
{
521523
let mut sweeper_state = self.sweeper_state.lock().unwrap();
522524

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;
525527

526528
let respend_descriptors: Vec<&SpendableOutputDescriptor> = sweeper_state
529+
.persistent
527530
.outputs
528531
.iter()
529532
.filter(|o| filter_fn(*o, cur_height))
@@ -536,7 +539,11 @@ where
536539
}
537540

538541
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+
)
540547
.map_err(|e| {
541548
log_error!(self.logger, "Error spending outputs: {:?}", e);
542549
})?;
@@ -550,7 +557,7 @@ where
550557
// As we didn't modify the state so far, the same filter_fn yields the same elements as
551558
// above.
552559
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));
554561
for output_info in respend_outputs {
555562
if let Some(filter) = self.chain_data_source.as_ref() {
556563
let watched_output = output_info.to_watched_output(cur_hash);
@@ -571,10 +578,10 @@ where
571578
}
572579

573580
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;
575582

576583
// Prune all outputs that have sufficient depth by now.
577-
sweeper_state.outputs.retain(|o| {
584+
sweeper_state.persistent.outputs.retain(|o| {
578585
if let Some(confirmation_height) = o.status.confirmation_height() {
579586
// We wait at least `PRUNE_DELAY_BLOCKS` as before that
580587
// `Event::SpendableOutputs` from lingering monitors might get replayed.
@@ -596,7 +603,7 @@ where
596603
OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE,
597604
OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE,
598605
OUTPUT_SWEEPER_PERSISTENCE_KEY,
599-
&sweeper_state.encode(),
606+
&sweeper_state.persistent.encode(),
600607
)
601608
.await
602609
.map_err(|e| {
@@ -613,7 +620,7 @@ where
613620
}
614621

615622
fn spend_outputs(
616-
&self, sweeper_state: &SweeperState, descriptors: &[&SpendableOutputDescriptor],
623+
&self, sweeper_state: &PersistentSweeperState, descriptors: &[&SpendableOutputDescriptor],
617624
change_destination_script: ScriptBuf,
618625
) -> Result<Transaction, ()> {
619626
let tx_feerate =
@@ -636,7 +643,7 @@ where
636643
) {
637644
let confirmation_hash = header.block_hash();
638645
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() {
640647
if output_info.is_spent_in(*tx) {
641648
output_info.status.confirmed(confirmation_hash, height, (*tx).clone())
642649
}
@@ -647,7 +654,7 @@ where
647654
fn best_block_updated_internal(
648655
&self, sweeper_state: &mut SweeperState, header: &Header, height: u32,
649656
) {
650-
sweeper_state.best_block = BestBlock::new(header.block_hash(), height);
657+
sweeper_state.persistent.best_block = BestBlock::new(header.block_hash(), height);
651658
self.prune_confirmed_outputs(sweeper_state);
652659
}
653660
}
@@ -667,9 +674,9 @@ where
667674
&self, header: &Header, txdata: &chain::transaction::TransactionData, height: u32,
668675
) {
669676
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,
671678
"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,
673680
"Blocks must be connected in chain-order - the connected block height must be one greater than the previous height");
674681

675682
self.transactions_confirmed_internal(&mut *state_lock, header, txdata, height);
@@ -686,13 +693,13 @@ where
686693
let new_height = height - 1;
687694
let block_hash = header.block_hash();
688695

689-
assert_eq!(state_lock.best_block.block_hash, block_hash,
696+
assert_eq!(state_lock.persistent.best_block.block_hash, block_hash,
690697
"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,
692699
"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);
694701

695-
for output_info in state_lock.outputs.iter_mut() {
702+
for output_info in state_lock.persistent.outputs.iter_mut() {
696703
if output_info.status.confirmation_hash() == Some(block_hash) {
697704
debug_assert_eq!(output_info.status.confirmation_height(), Some(height));
698705
output_info.status.unconfirmed();
@@ -731,6 +738,7 @@ where
731738

732739
// Get what height was unconfirmed.
733740
let unconf_height = state_lock
741+
.persistent
734742
.outputs
735743
.iter()
736744
.find(|o| o.status.latest_spending_tx().map(|tx| tx.compute_txid()) == Some(*txid))
@@ -739,6 +747,7 @@ where
739747
if let Some(unconf_height) = unconf_height {
740748
// Unconfirm all >= this height.
741749
state_lock
750+
.persistent
742751
.outputs
743752
.iter_mut()
744753
.filter(|o| o.status.confirmation_height() >= Some(unconf_height))
@@ -761,6 +770,7 @@ where
761770
fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
762771
let state_lock = self.sweeper_state.lock().unwrap();
763772
state_lock
773+
.persistent
764774
.outputs
765775
.iter()
766776
.filter_map(|o| match o.status {
@@ -780,13 +790,18 @@ where
780790
}
781791
}
782792

783-
#[derive(Debug, Clone)]
793+
#[derive(Debug)]
784794
struct SweeperState {
795+
persistent: PersistentSweeperState,
796+
}
797+
798+
#[derive(Debug, Clone)]
799+
struct PersistentSweeperState {
785800
outputs: Vec<TrackedSpendableOutput>,
786801
best_block: BestBlock,
787802
}
788803

789-
impl_writeable_tlv_based!(SweeperState, {
804+
impl_writeable_tlv_based!(PersistentSweeperState, {
790805
(0, outputs, required_vec),
791806
(2, best_block, required),
792807
});
@@ -832,7 +847,7 @@ where
832847
kv_store,
833848
logger,
834849
) = args;
835-
let state = SweeperState::read(reader)?;
850+
let state = PersistentSweeperState::read(reader)?;
836851
let best_block = state.best_block;
837852

838853
if let Some(filter) = chain_data_source.as_ref() {
@@ -842,7 +857,7 @@ where
842857
}
843858
}
844859

845-
let sweeper_state = Mutex::new(state);
860+
let sweeper_state = Mutex::new(SweeperState { persistent: state });
846861
Ok(Self {
847862
sweeper_state,
848863
pending_sweep: AtomicBool::new(false),
@@ -881,7 +896,7 @@ where
881896
kv_store,
882897
logger,
883898
) = args;
884-
let state = SweeperState::read(reader)?;
899+
let state = PersistentSweeperState::read(reader)?;
885900
let best_block = state.best_block;
886901

887902
if let Some(filter) = chain_data_source.as_ref() {
@@ -891,7 +906,7 @@ where
891906
}
892907
}
893908

894-
let sweeper_state = Mutex::new(state);
909+
let sweeper_state = Mutex::new(SweeperState { persistent: state });
895910
Ok((
896911
best_block,
897912
OutputSweeper {

0 commit comments

Comments
 (0)