Skip to content

Commit 4c03823

Browse files
committed
Set correct counterparty_spendable_height on c.p. revoked HTLCs
If the counterparty broadcasts a revoked transaction with offered HTLCs, the output is not immediately pinnable as the counterparty cannot claim the HTLC until the CLTV expires and they use an HTLC-Timeout path. Here we fix the `counterparty_spendable_height` value we set on counterparty revoked HTLC claims to match reality. Note that because we still consider these outputs `Pinnable` the value is not used. In the next commit we'll start making them `Unpinnable` which will actually change behavior. Note that when upgrading we have to wipe the `counterparty_spendable_height` value for non-offered HTLCs as otherwise we'd consider them `Unpinnable` when they are, in fact, `Pinnable`.
1 parent 8239cfd commit 4c03823

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3552,11 +3552,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35523552
return (claimable_outpoints, to_counterparty_output_info);
35533553
}
35543554
let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone(), &self.onchain_tx_handler.channel_transaction_parameters.channel_type_features);
3555+
let counterparty_spendable_height = if htlc.offered {
3556+
htlc.cltv_expiry
3557+
} else {
3558+
height
3559+
};
35553560
let justice_package = PackageTemplate::build_package(
35563561
commitment_txid,
35573562
transaction_output_index,
35583563
PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp),
3559-
htlc.cltv_expiry,
3564+
counterparty_spendable_height,
35603565
);
35613566
claimable_outpoints.push(justice_package);
35623567
}

lightning/src/chain/package.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,10 +771,13 @@ pub struct PackageTemplate {
771771
/// Block height at which our counterparty can potentially claim this output as well (assuming
772772
/// they have the keys or information required to do so).
773773
///
774-
/// This is used primarily by external consumers to decide when an output becomes "pinnable"
775-
/// because the counterparty can potentially spend it. It is also used internally by
774+
/// This is used primarily by to decide when an output becomes "pinnable" because the
775+
/// counterparty can potentially spend it. It is also used internally by
776776
/// [`Self::get_height_timer`] to identify when an output must be claimed by, depending on the
777777
/// type of output.
778+
///
779+
/// Note that for revoked counterparty HTLC outputs the value may be zero in some cases where
780+
/// we upgraded from LDK 0.1 or prior.
778781
counterparty_spendable_height: u32,
779782
// Cache of package feerate committed at previous (re)broadcast. If bumping resources
780783
// (either claimed output value or external utxo), it will keep increasing until holder
@@ -1218,6 +1221,18 @@ impl Readable for PackageTemplate {
12181221
(4, _height_original, option), // Written with a dummy value since 0.1
12191222
(6, height_timer, option),
12201223
});
1224+
for (_, input) in &inputs {
1225+
if let PackageSolvingData::RevokedHTLCOutput(RevokedHTLCOutput { htlc, .. }) = input {
1226+
// LDK versions through 0.1 set the wrong counterparty_spendable_height for
1227+
// non-offered revoked HTLCs (ie HTLCs we sent to our counterparty which they can
1228+
// claim with a preimage immediately). Here we detect this and reset the value to
1229+
// zero, as the value is unused except for merging decisions which doesn't care
1230+
// about any values below the current height.
1231+
if !htlc.offered && htlc.cltv_expiry == counterparty_spendable_height {
1232+
counterparty_spendable_height = 0;
1233+
}
1234+
}
1235+
}
12211236
Ok(PackageTemplate {
12221237
inputs,
12231238
malleability,

0 commit comments

Comments
 (0)