Skip to content

Commit 2f58110

Browse files
committed
Track spent WatchedOutputs and re-add if unconfirmed
Previously, we would track a spending transaction but wouldn't account for it being reorged out of the chain, in which case we wouldn't monitor the `WatchedOutput`s until they'd be reloaded on restart. Here, we keep any `WatchedOutput`s around until their spends are sufficiently confirmed and only prune them after `ANTI_REORG_DELAY`.
1 parent 2c9dbb9 commit 2f58110

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lightning-transaction-sync/src/common.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use lightning::chain::{Confirm, WatchedOutput};
2+
use lightning::chain::channelmonitor::ANTI_REORG_DELAY;
23
use bitcoin::{Txid, BlockHash, Transaction, OutPoint};
34
use bitcoin::block::Header;
45

@@ -13,6 +14,9 @@ pub(crate) struct SyncState {
1314
// Outputs that were previously processed, but must not be forgotten yet as
1415
// as we still need to monitor any spends on-chain.
1516
pub watched_outputs: HashMap<OutPoint, WatchedOutput>,
17+
// Outputs for which we previously saw a spend on-chain but kept around until the spends reach
18+
// sufficient depth.
19+
pub outputs_spends_pending_threshold_conf: Vec<(Txid, u32, OutPoint, WatchedOutput)>,
1620
// The tip hash observed during our last sync.
1721
pub last_sync_hash: Option<BlockHash>,
1822
// Indicates whether we need to resync, e.g., after encountering an error.
@@ -24,6 +28,7 @@ impl SyncState {
2428
Self {
2529
watched_transactions: HashSet::new(),
2630
watched_outputs: HashMap::new(),
31+
outputs_spends_pending_threshold_conf: Vec::new(),
2732
last_sync_hash: None,
2833
pending_sync: false,
2934
}
@@ -38,6 +43,17 @@ impl SyncState {
3843
}
3944

4045
self.watched_transactions.insert(txid);
46+
47+
// If a previously-confirmed output spend is unconfirmed, re-add the watched output to
48+
// the tracking map.
49+
self.outputs_spends_pending_threshold_conf.retain(|(conf_txid, _, prev_outpoint, output)| {
50+
if txid == *conf_txid {
51+
self.watched_outputs.insert(*prev_outpoint, output.clone());
52+
false
53+
} else {
54+
true
55+
}
56+
})
4157
}
4258
}
4359

@@ -57,10 +73,18 @@ impl SyncState {
5773
self.watched_transactions.remove(&ctx.tx.txid());
5874

5975
for input in &ctx.tx.input {
60-
self.watched_outputs.remove(&input.previous_output);
76+
if let Some(output) = self.watched_outputs.remove(&input.previous_output) {
77+
self.outputs_spends_pending_threshold_conf.push((ctx.tx.txid(), ctx.block_height, input.previous_output, output));
78+
}
6179
}
6280
}
6381
}
82+
83+
pub fn prune_output_spends(&mut self, cur_height: u32) {
84+
self.outputs_spends_pending_threshold_conf.retain(|(_, conf_height, _, _)| {
85+
cur_height < conf_height + ANTI_REORG_DELAY - 1
86+
});
87+
}
6488
}
6589

6690

lightning-transaction-sync/src/electrum.rs

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ where
157157
for c in &confirmables {
158158
c.best_block_updated(&tip_header, tip_height);
159159
}
160+
161+
// Prune any sufficiently confirmed output spends
162+
sync_state.prune_output_spends(tip_height);
160163
}
161164

162165
match self.get_confirmed_transactions(&sync_state) {

lightning-transaction-sync/src/esplora.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ where
153153
}
154154
}
155155

156-
match maybe_await!(self.sync_best_block_updated(&confirmables, &tip_hash)) {
156+
match maybe_await!(self.sync_best_block_updated(&confirmables, &mut sync_state, &tip_hash)) {
157157
Ok(()) => {}
158158
Err(InternalError::Inconsistency) => {
159159
// Immediately restart syncing when we encounter any inconsistencies.
@@ -238,7 +238,7 @@ where
238238

239239
#[maybe_async]
240240
fn sync_best_block_updated(
241-
&self, confirmables: &Vec<&(dyn Confirm + Sync + Send)>, tip_hash: &BlockHash,
241+
&self, confirmables: &Vec<&(dyn Confirm + Sync + Send)>, sync_state: &mut SyncState, tip_hash: &BlockHash,
242242
) -> Result<(), InternalError> {
243243

244244
// Inform the interface of the new block.
@@ -249,6 +249,9 @@ where
249249
for c in confirmables {
250250
c.best_block_updated(&tip_header, tip_height);
251251
}
252+
253+
// Prune any sufficiently confirmed output spends
254+
sync_state.prune_output_spends(tip_height);
252255
}
253256
} else {
254257
return Err(InternalError::Inconsistency);

0 commit comments

Comments
 (0)