Skip to content

Commit 308affa

Browse files
committed
Claim HTLCs with preimage from currently confirmed commitment
This ensures that we correctly handle the cases covered by the tests added in the previous commit. Namely, that we claim the HTLCs from the currently confirmed commitment, rather than always claiming from the latest or previous counterparty commitment if we've seen either confirm onchain at a prior point.
1 parent 0cb6720 commit 308affa

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,18 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
25032503
{
25042504
self.payment_preimages.insert(payment_hash.clone(), payment_preimage.clone());
25052505

2506+
let confirmed_spend_txid = self.funding_spend_confirmed.or_else(|| {
2507+
self.onchain_events_awaiting_threshold_conf.iter().find_map(|event| match event.event {
2508+
OnchainEvent::FundingSpendConfirmation { .. } => Some(event.txid),
2509+
_ => None,
2510+
})
2511+
});
2512+
let confirmed_spend_txid = if let Some(txid) = confirmed_spend_txid {
2513+
txid
2514+
} else {
2515+
return;
2516+
};
2517+
25062518
// If the channel is force closed, try to claim the output from this preimage.
25072519
// First check if a counterparty commitment transaction has been broadcasted:
25082520
macro_rules! claim_htlcs {
@@ -2512,14 +2524,24 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
25122524
}
25132525
}
25142526
if let Some(txid) = self.current_counterparty_commitment_txid {
2515-
if let Some(commitment_number) = self.counterparty_commitment_txn_on_chain.get(&txid) {
2516-
claim_htlcs!(*commitment_number, txid);
2527+
if txid == confirmed_spend_txid {
2528+
if let Some(commitment_number) = self.counterparty_commitment_txn_on_chain.get(&txid) {
2529+
claim_htlcs!(*commitment_number, txid);
2530+
} else {
2531+
debug_assert!(false);
2532+
log_error!(self.logger, "Detected counterparty commitment tx on-chain without tracking commitment number");
2533+
}
25172534
return;
25182535
}
25192536
}
25202537
if let Some(txid) = self.prev_counterparty_commitment_txid {
2521-
if let Some(commitment_number) = self.counterparty_commitment_txn_on_chain.get(&txid) {
2522-
claim_htlcs!(*commitment_number, txid);
2538+
if txid == confirmed_spend_txid {
2539+
if let Some(commitment_number) = self.counterparty_commitment_txn_on_chain.get(&txid) {
2540+
claim_htlcs!(*commitment_number, txid);
2541+
} else {
2542+
debug_assert!(false);
2543+
log_error!(self.logger, "Detected counterparty commitment tx on-chain without tracking commitment number");
2544+
}
25232545
return;
25242546
}
25252547
}
@@ -2530,13 +2552,22 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
25302552
// *we* sign a holder commitment transaction, not when e.g. a watchtower broadcasts one of our
25312553
// holder commitment transactions.
25322554
if self.broadcasted_holder_revokable_script.is_some() {
2533-
// Assume that the broadcasted commitment transaction confirmed in the current best
2534-
// block. Even if not, its a reasonable metric for the bump criteria on the HTLC
2535-
// transactions.
2536-
let (claim_reqs, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, self.best_block.height());
2537-
self.onchain_tx_handler.update_claims_view_from_requests(claim_reqs, self.best_block.height(), self.best_block.height(), broadcaster, fee_estimator, logger);
2538-
if let Some(ref tx) = self.prev_holder_signed_commitment_tx {
2539-
let (claim_reqs, _) = self.get_broadcasted_holder_claims(&tx, self.best_block.height());
2555+
let holder_commitment_tx = if self.current_holder_commitment_tx.txid == confirmed_spend_txid {
2556+
Some(&self.current_holder_commitment_tx)
2557+
} else if let Some(prev_holder_commitment_tx) = &self.prev_holder_signed_commitment_tx {
2558+
if prev_holder_commitment_tx.txid == confirmed_spend_txid {
2559+
Some(prev_holder_commitment_tx)
2560+
} else {
2561+
None
2562+
}
2563+
} else {
2564+
None
2565+
};
2566+
if let Some(holder_commitment_tx) = holder_commitment_tx {
2567+
// Assume that the broadcasted commitment transaction confirmed in the current best
2568+
// block. Even if not, its a reasonable metric for the bump criteria on the HTLC
2569+
// transactions.
2570+
let (claim_reqs, _) = self.get_broadcasted_holder_claims(&holder_commitment_tx, self.best_block.height());
25402571
self.onchain_tx_handler.update_claims_view_from_requests(claim_reqs, self.best_block.height(), self.best_block.height(), broadcaster, fee_estimator, logger);
25412572
}
25422573
}

0 commit comments

Comments
 (0)