Skip to content

Commit aaf458d

Browse files
committed
Update HTLC transaction detection from revoked counterparty commitments
Previously, this method assumed that all HTLC transactions have 1 input and 1 output, with the sole input having a witness of 5 elements. This will no longer be the case for HTLC transactions on channels with anchors outputs since additional inputs and outputs can be attached to them to allow fee bumping.
1 parent 69b1095 commit aaf458d

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

lightning/src/chain/channelmonitor.rs

+35-16
Original file line numberDiff line numberDiff line change
@@ -2646,12 +2646,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
26462646
}
26472647

26482648
/// Attempts to claim a counterparty HTLC-Success/HTLC-Timeout's outputs using the revocation key
2649-
fn check_spend_counterparty_htlc<L: Deref>(&mut self, tx: &Transaction, commitment_number: u64, height: u32, logger: &L) -> (Vec<PackageTemplate>, Option<TransactionOutputs>) where L::Target: Logger {
2650-
let htlc_txid = tx.txid();
2651-
if tx.input.len() != 1 || tx.output.len() != 1 || tx.input[0].witness.len() != 5 {
2652-
return (Vec::new(), None)
2653-
}
2654-
2649+
fn check_spend_counterparty_htlc<L: Deref>(
2650+
&mut self, tx: &Transaction, commitment_number: u64, commitment_txid: &Txid, height: u32, logger: &L
2651+
) -> (Vec<PackageTemplate>, Option<TransactionOutputs>) where L::Target: Logger {
26552652
macro_rules! ignore_error {
26562653
( $thing : expr ) => {
26572654
match $thing {
@@ -2665,12 +2662,32 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
26652662
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
26662663
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
26672664

2668-
log_error!(logger, "Got broadcast of revoked counterparty HTLC transaction, spending {}:{}", htlc_txid, 0);
2669-
let revk_outp = RevokedOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, tx.output[0].value, self.counterparty_commitment_params.on_counterparty_tx_csv);
2670-
let justice_package = PackageTemplate::build_package(htlc_txid, 0, PackageSolvingData::RevokedOutput(revk_outp), height + self.counterparty_commitment_params.on_counterparty_tx_csv as u32, true, height);
2671-
let claimable_outpoints = vec!(justice_package);
2672-
let outputs = vec![(0, tx.output[0].clone())];
2673-
(claimable_outpoints, Some((htlc_txid, outputs)))
2665+
let htlc_txid = tx.txid();
2666+
let mut claimable_outpoints = vec![];
2667+
let mut outputs_to_watch = None;
2668+
for (idx, input) in tx.input.iter().enumerate() {
2669+
// HTLC transactions always spend an output on the commitment transaction with a witness
2670+
// of 5 elements. The HTLC input will always have a corresponding output at the same
2671+
// index within the transaction.
2672+
if input.previous_output.txid == *commitment_txid && input.witness.len() == 5 && tx.output.get(idx).is_some() {
2673+
log_error!(logger, "Got broadcast of revoked counterparty HTLC transaction, spending {}:{}", htlc_txid, idx);
2674+
let revk_outp = RevokedOutput::build(
2675+
per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
2676+
self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key,
2677+
tx.output[0].value, self.counterparty_commitment_params.on_counterparty_tx_csv
2678+
);
2679+
let justice_package = PackageTemplate::build_package(
2680+
htlc_txid, idx as u32, PackageSolvingData::RevokedOutput(revk_outp),
2681+
height + self.counterparty_commitment_params.on_counterparty_tx_csv as u32, true, height
2682+
);
2683+
claimable_outpoints.push(justice_package);
2684+
if outputs_to_watch.is_none() {
2685+
outputs_to_watch = Some((htlc_txid, vec![]));
2686+
}
2687+
outputs_to_watch.as_mut().unwrap().1.push((idx as u32, tx.output[idx].clone()));
2688+
}
2689+
}
2690+
(claimable_outpoints, outputs_to_watch)
26742691
}
26752692

26762693
// Returns (1) `PackageTemplate`s that can be given to the OnchainTxHandler, so that the handler can
@@ -2908,9 +2925,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29082925
for tx in &txn_matched {
29092926
if tx.input.len() == 1 {
29102927
// Assuming our keys were not leaked (in which case we're screwed no matter what),
2911-
// commitment transactions and HTLC transactions will all only ever have one input,
2912-
// which is an easy way to filter out any potential non-matching txn for lazy
2913-
// filters.
2928+
// commitment transactions and HTLC transactions will all only ever have one input
2929+
// (except for HTLC transactions for channels with anchor outputs), which is an easy
2930+
// way to filter out any potential non-matching txn for lazy filters.
29142931
let prevout = &tx.input[0].previous_output;
29152932
if prevout.txid == self.funding_info.0.txid && prevout.vout == self.funding_info.0.index as u32 {
29162933
let mut balance_spendable_csv = None;
@@ -2951,7 +2968,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29512968
});
29522969
} else {
29532970
if let Some(&commitment_number) = self.counterparty_commitment_txn_on_chain.get(&prevout.txid) {
2954-
let (mut new_outpoints, new_outputs_option) = self.check_spend_counterparty_htlc(&tx, commitment_number, height, &logger);
2971+
let (mut new_outpoints, new_outputs_option) = self.check_spend_counterparty_htlc(
2972+
&tx, commitment_number, &prevout.txid, height, &logger
2973+
);
29552974
claimable_outpoints.append(&mut new_outpoints);
29562975
if let Some(new_outputs) = new_outputs_option {
29572976
watch_outputs.push(new_outputs);

0 commit comments

Comments
 (0)