Skip to content

Commit 7c56f21

Browse files
committed
Cancel claims signed by a remote ChannelMonitor when reorging
In `ChannelMonitorImpl::cancel_prev_commitment_claims` we need to cancel any claims against a removed commitment transaction. We were checking if `holder_tx_signed` before checking if either the current or previous holder commitment transaction had pending claims against it, but (a) there's no need to do this, there's not a big performance cost to just always trying to remove claims and (b) we can't actually rely on `holder_tx_signed`. `holder_tx_signed` being set doesn't necessarily imply that the `ChannelMonitor` was persisted (i.e. it may simply be lost in a poorly-timed restart) but we also (somewhat theoretically) allow for multiple copies of a `ChannelMonitor` to exist, and a different one could have signed the commitment transaction which was confirmed (and then unconfirmed). Thus, we simply remove the additional check here.
1 parent 9d2449a commit 7c56f21

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3874,35 +3874,32 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38743874
}
38753875
}
38763876
}
3877-
if self.holder_tx_signed {
3878-
// If we've signed, we may have broadcast either commitment (prev or current), and
3879-
// attempted to claim from it immediately without waiting for a confirmation.
3880-
if self.current_holder_commitment_tx.txid != *confirmed_commitment_txid {
3877+
// Cancel any pending claims for any holder commitments in case they had previously
3878+
// confirmed or been signed (in which case we will start attempting to claim without
3879+
// waiting for confirmation).
3880+
if self.current_holder_commitment_tx.txid != *confirmed_commitment_txid {
3881+
log_trace!(logger, "Canceling claims for previously broadcast holder commitment {}",
3882+
self.current_holder_commitment_tx.txid);
3883+
let mut outpoint = BitcoinOutPoint { txid: self.current_holder_commitment_tx.txid, vout: 0 };
3884+
for (htlc, _, _) in &self.current_holder_commitment_tx.htlc_outputs {
3885+
if let Some(vout) = htlc.transaction_output_index {
3886+
outpoint.vout = vout;
3887+
self.onchain_tx_handler.abandon_claim(&outpoint);
3888+
}
3889+
}
3890+
}
3891+
if let Some(prev_holder_commitment_tx) = &self.prev_holder_signed_commitment_tx {
3892+
if prev_holder_commitment_tx.txid != *confirmed_commitment_txid {
38813893
log_trace!(logger, "Canceling claims for previously broadcast holder commitment {}",
3882-
self.current_holder_commitment_tx.txid);
3883-
let mut outpoint = BitcoinOutPoint { txid: self.current_holder_commitment_tx.txid, vout: 0 };
3884-
for (htlc, _, _) in &self.current_holder_commitment_tx.htlc_outputs {
3894+
prev_holder_commitment_tx.txid);
3895+
let mut outpoint = BitcoinOutPoint { txid: prev_holder_commitment_tx.txid, vout: 0 };
3896+
for (htlc, _, _) in &prev_holder_commitment_tx.htlc_outputs {
38853897
if let Some(vout) = htlc.transaction_output_index {
38863898
outpoint.vout = vout;
38873899
self.onchain_tx_handler.abandon_claim(&outpoint);
38883900
}
38893901
}
38903902
}
3891-
if let Some(prev_holder_commitment_tx) = &self.prev_holder_signed_commitment_tx {
3892-
if prev_holder_commitment_tx.txid != *confirmed_commitment_txid {
3893-
log_trace!(logger, "Canceling claims for previously broadcast holder commitment {}",
3894-
prev_holder_commitment_tx.txid);
3895-
let mut outpoint = BitcoinOutPoint { txid: prev_holder_commitment_tx.txid, vout: 0 };
3896-
for (htlc, _, _) in &prev_holder_commitment_tx.htlc_outputs {
3897-
if let Some(vout) = htlc.transaction_output_index {
3898-
outpoint.vout = vout;
3899-
self.onchain_tx_handler.abandon_claim(&outpoint);
3900-
}
3901-
}
3902-
}
3903-
}
3904-
} else {
3905-
// No previous claim.
39063903
}
39073904
}
39083905

0 commit comments

Comments
 (0)