Skip to content

Commit 9c1159c

Browse files
tnullTheBlueMatt
authored andcommitted
OutputSweeper: Delay pruning until monitors have likely been archived
Previously, we would prune tracked descriptors once we see a spend hit `ANTI_REORG_DELAY = 6` confirmations. However, this could lead to a scenario where lingering `ChannelMonitor`s waiting to be archived would still regenerate and replay `Event::SpendableOutput`s, i.e., we would re-add the same (now unspendable due to be actually being already spent) outputs again after having intially pruned them. Here, we therefore keep the tracked descriptors around for longer, in particular at least `ARCHIVAL_DELAY_BLOCKS + ANTI_REORG_DELAY = 4038` confirmations, at which point we assume the lingering monitors to have been likely archived, and it's 'safe' for us to also forget about the descriptors.
1 parent 99347b4 commit 9c1159c

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lightning-background-processor/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ mod tests {
10991099
SCORER_PERSISTENCE_SECONDARY_NAMESPACE,
11001100
};
11011101
use lightning::util::ser::Writeable;
1102-
use lightning::util::sweep::{OutputSpendStatus, OutputSweeper};
1102+
use lightning::util::sweep::{OutputSpendStatus, OutputSweeper, PRUNE_DELAY_BLOCKS};
11031103
use lightning::util::test_utils;
11041104
use lightning::{get_event, get_event_msg};
11051105
use lightning_persister::fs_store::FilesystemStore;
@@ -2282,8 +2282,8 @@ mod tests {
22822282
}
22832283

22842284
// Check we stop tracking the spendable outputs when one of the txs reaches
2285-
// ANTI_REORG_DELAY confirmations.
2286-
confirm_transaction_depth(&mut nodes[0], &sweep_tx_0, ANTI_REORG_DELAY);
2285+
// PRUNE_DELAY_BLOCKS confirmations.
2286+
confirm_transaction_depth(&mut nodes[0], &sweep_tx_0, PRUNE_DELAY_BLOCKS);
22872287
assert_eq!(nodes[0].sweeper.tracked_spendable_outputs().len(), 0);
22882288

22892289
if !std::thread::panicking() {

lightning/src/util/sweep.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! sweeping them.
1010
1111
use crate::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
12-
use crate::chain::channelmonitor::ANTI_REORG_DELAY;
12+
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, ARCHIVAL_DELAY_BLOCKS};
1313
use crate::chain::{self, BestBlock, Confirm, Filter, Listen, WatchedOutput};
1414
use crate::io;
1515
use crate::ln::msgs::DecodeError;
@@ -32,6 +32,9 @@ use bitcoin::{BlockHash, Transaction, Txid};
3232

3333
use core::ops::Deref;
3434

35+
/// The number of blocks we wait before we prune the tracked spendable outputs.
36+
pub const PRUNE_DELAY_BLOCKS: u32 = ARCHIVAL_DELAY_BLOCKS + ANTI_REORG_DELAY;
37+
3538
/// The state of a spendable output currently tracked by an [`OutputSweeper`].
3639
#[derive(Clone, Debug, PartialEq, Eq)]
3740
pub struct TrackedSpendableOutput {
@@ -101,7 +104,11 @@ pub enum OutputSpendStatus {
101104
latest_spending_tx: Transaction,
102105
},
103106
/// A transaction spending the output has been confirmed on-chain but will be tracked until it
104-
/// reaches [`ANTI_REORG_DELAY`] confirmations.
107+
/// reaches at least [`PRUNE_DELAY_BLOCKS`] confirmations to ensure [`Event::SpendableOutputs`]
108+
/// stemming from lingering [`ChannelMonitor`]s can safely be replayed.
109+
///
110+
/// [`Event::SpendableOutputs`]: crate::events::Event::SpendableOutputs
111+
/// [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
105112
PendingThresholdConfirmations {
106113
/// The hash of the chain tip when we first broadcast a transaction spending this output.
107114
first_broadcast_hash: BlockHash,
@@ -524,7 +531,9 @@ where
524531
// Prune all outputs that have sufficient depth by now.
525532
sweeper_state.outputs.retain(|o| {
526533
if let Some(confirmation_height) = o.status.confirmation_height() {
527-
if cur_height >= confirmation_height + ANTI_REORG_DELAY - 1 {
534+
// We wait at least `PRUNE_DELAY_BLOCKS` as before that
535+
// `Event::SpendableOutputs` from lingering monitors might get replayed.
536+
if cur_height >= confirmation_height + PRUNE_DELAY_BLOCKS - 1 {
528537
log_debug!(self.logger,
529538
"Pruning swept output as sufficiently confirmed via spend in transaction {:?}. Pruned descriptor: {:?}",
530539
o.status.latest_spending_tx().map(|t| t.compute_txid()), o.descriptor

0 commit comments

Comments
 (0)