Skip to content

Commit ea42df4

Browse files
committed
Prune HTLC sources from pending funding scopes
We only need to track the HTLC sources for the previous and current counterparty commitments.
1 parent c40d1e8 commit ea42df4

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,26 +3399,35 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33993399

34003400
// Prune HTLCs from the previous counterparty commitment tx so we don't generate failure/fulfill
34013401
// events for now-revoked/fulfilled HTLCs.
3402-
if let Some(txid) = self.funding.prev_counterparty_commitment_txid.take() {
3403-
if self.funding.current_counterparty_commitment_txid.unwrap() != txid {
3404-
let cur_claimables = self.funding.counterparty_claimable_outpoints.get(
3405-
&self.funding.current_counterparty_commitment_txid.unwrap()).unwrap();
3406-
for (_, ref source_opt) in self.funding.counterparty_claimable_outpoints.get(&txid).unwrap() {
3407-
if let Some(source) = source_opt {
3408-
if !cur_claimables.iter()
3409-
.any(|(_, cur_source_opt)| cur_source_opt == source_opt)
3410-
{
3411-
self.counterparty_fulfilled_htlcs.remove(&SentHTLCId::from_source(source));
3402+
let mut removed_fulfilled_htlcs = false;
3403+
let prune_htlc_sources = |funding: &mut FundingScope| {
3404+
if let Some(txid) = funding.prev_counterparty_commitment_txid.take() {
3405+
if funding.current_counterparty_commitment_txid.unwrap() != txid {
3406+
let cur_claimables = funding.counterparty_claimable_outpoints.get(
3407+
&funding.current_counterparty_commitment_txid.unwrap()).unwrap();
3408+
// We only need to remove fulfilled HTLCs once for the first `FundingScope` we
3409+
// come across since all `FundingScope`s share the same set of HTLC sources.
3410+
if !removed_fulfilled_htlcs {
3411+
for (_, ref source_opt) in funding.counterparty_claimable_outpoints.get(&txid).unwrap() {
3412+
if let Some(source) = source_opt {
3413+
if !cur_claimables.iter()
3414+
.any(|(_, cur_source_opt)| cur_source_opt == source_opt)
3415+
{
3416+
self.counterparty_fulfilled_htlcs.remove(&SentHTLCId::from_source(source));
3417+
}
3418+
}
34123419
}
3420+
removed_fulfilled_htlcs = true;
34133421
}
3422+
for &mut (_, ref mut source_opt) in funding.counterparty_claimable_outpoints.get_mut(&txid).unwrap() {
3423+
*source_opt = None;
3424+
}
3425+
} else {
3426+
assert!(cfg!(fuzzing), "Commitment txids are unique outside of fuzzing, where hashes can collide");
34143427
}
3415-
for &mut (_, ref mut source_opt) in self.funding.counterparty_claimable_outpoints.get_mut(&txid).unwrap() {
3416-
*source_opt = None;
3417-
}
3418-
} else {
3419-
assert!(cfg!(fuzzing), "Commitment txids are unique outside of fuzzing, where hashes can collide");
34203428
}
3421-
}
3429+
};
3430+
core::iter::once(&mut self.funding).chain(&mut self.pending_funding).for_each(prune_htlc_sources);
34223431

34233432
if !self.payment_preimages.is_empty() {
34243433
let min_idx = self.get_min_seen_secret();

0 commit comments

Comments
 (0)