Skip to content

Commit 3f70b5e

Browse files
committed
Check for duplicate HTLC events having matured
1 parent 8a84f9a commit 3f70b5e

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,31 +2067,56 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20672067
claimable_outpoints.append(&mut new_outpoints);
20682068
}
20692069

2070+
// Find which on-chain events have reached their confirmation threshold.
20702071
let onchain_events_waiting_threshold_conf =
20712072
self.onchain_events_waiting_threshold_conf.drain(..).collect::<Vec<_>>();
2073+
let mut onchain_events_reaching_threshold_conf = Vec::new();
20722074
for entry in onchain_events_waiting_threshold_conf {
20732075
if entry.has_reached_confirmation_threshold(height) {
2074-
match entry.event {
2075-
OnchainEvent::HTLCUpdate { htlc_update } => {
2076-
log_trace!(logger, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!((htlc_update.1).0));
2077-
self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
2078-
payment_hash: htlc_update.1,
2079-
payment_preimage: None,
2080-
source: htlc_update.0,
2081-
}));
2082-
},
2083-
OnchainEvent::MaturingOutput { descriptor } => {
2084-
log_trace!(logger, "Descriptor {} has got enough confirmations to be passed upstream", log_spendable!(descriptor));
2085-
self.pending_events.push(Event::SpendableOutputs {
2086-
outputs: vec![descriptor]
2087-
});
2088-
}
2089-
}
2076+
onchain_events_reaching_threshold_conf.push(entry);
20902077
} else {
20912078
self.onchain_events_waiting_threshold_conf.push(entry);
20922079
}
20932080
}
20942081

2082+
// Used to check for duplicate HTLC resolutions.
2083+
#[cfg(debug_assertions)]
2084+
let unconfirmed_htlcs: Vec<_> = self.onchain_events_waiting_threshold_conf
2085+
.iter()
2086+
.filter_map(|entry| match &entry.event {
2087+
OnchainEvent::HTLCUpdate { htlc_update } => Some(htlc_update.0.clone()),
2088+
OnchainEvent::MaturingOutput { .. } => None,
2089+
})
2090+
.collect();
2091+
#[cfg(debug_assertions)]
2092+
let mut confirmed_htlcs = Vec::new();
2093+
2094+
// Produce actionable events from on-chain events having reached their threshold.
2095+
for entry in onchain_events_reaching_threshold_conf.drain(..) {
2096+
match entry.event {
2097+
OnchainEvent::HTLCUpdate { htlc_update } => {
2098+
// Check for duplicate HTLC resolutions.
2099+
debug_assert!(unconfirmed_htlcs.iter().find(|&htlc| htlc == &htlc_update.0).is_none());
2100+
debug_assert!(confirmed_htlcs.iter().find(|&htlc| htlc == &htlc_update.0).is_none());
2101+
#[cfg(debug_assertions)]
2102+
confirmed_htlcs.push(htlc_update.0.clone());
2103+
2104+
log_trace!(logger, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!((htlc_update.1).0));
2105+
self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
2106+
payment_hash: htlc_update.1,
2107+
payment_preimage: None,
2108+
source: htlc_update.0,
2109+
}));
2110+
},
2111+
OnchainEvent::MaturingOutput { descriptor } => {
2112+
log_trace!(logger, "Descriptor {} has got enough confirmations to be passed upstream", log_spendable!(descriptor));
2113+
self.pending_events.push(Event::SpendableOutputs {
2114+
outputs: vec![descriptor]
2115+
});
2116+
}
2117+
}
2118+
}
2119+
20952120
self.onchain_tx_handler.update_claims_view(&txn_matched, claimable_outpoints, Some(height), &&*broadcaster, &&*fee_estimator, &&*logger);
20962121
self.last_block_hash = block_hash;
20972122

0 commit comments

Comments
 (0)