Skip to content

Commit 5eb0a34

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 e93622b commit 5eb0a34

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lightning/src/chain/channelmonitor.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3556,11 +3556,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35563556
return (claimable_outpoints, to_counterparty_output_info);
35573557
}
35583558
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);
3559+
let counterparty_spendable_height = if htlc.offered {
3560+
htlc.cltv_expiry
3561+
} else {
3562+
height
3563+
};
35593564
let justice_package = PackageTemplate::build_package(
35603565
commitment_txid,
35613566
transaction_output_index,
35623567
PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp),
3563-
htlc.cltv_expiry,
3568+
counterparty_spendable_height,
35643569
);
35653570
claimable_outpoints.push(justice_package);
35663571
}

lightning/src/chain/package.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -771,10 +771,12 @@ 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
776-
/// [`Self::get_height_timer`] to identify when an output must be claimed by, depending on the
777-
/// type of output.
774+
/// This is used primarily to decide when an output becomes "pinnable" because the counterparty
775+
/// can potentially spend it. It is also used internally by [`Self::get_height_timer`] to
776+
/// identify when an output must be claimed by, depending on the type of output.
777+
///
778+
/// Note that for revoked counterparty HTLC outputs the value may be zero in some cases where
779+
/// we upgraded from LDK 0.1 or prior.
778780
counterparty_spendable_height: u32,
779781
// Cache of package feerate committed at previous (re)broadcast. If bumping resources
780782
// (either claimed output value or external utxo), it will keep increasing until holder
@@ -1218,6 +1220,18 @@ impl Readable for PackageTemplate {
12181220
(4, _height_original, option), // Written with a dummy value since 0.1
12191221
(6, height_timer, option),
12201222
});
1223+
for (_, input) in &inputs {
1224+
if let PackageSolvingData::RevokedHTLCOutput(RevokedHTLCOutput { htlc, .. }) = input {
1225+
// LDK versions through 0.1 set the wrong counterparty_spendable_height for
1226+
// non-offered revoked HTLCs (ie HTLCs we sent to our counterparty which they can
1227+
// claim with a preimage immediately). Here we detect this and reset the value to
1228+
// zero, as the value is unused except for merging decisions which doesn't care
1229+
// about any values below the current height.
1230+
if !htlc.offered && htlc.cltv_expiry == counterparty_spendable_height {
1231+
counterparty_spendable_height = 0;
1232+
}
1233+
}
1234+
}
12211235
Ok(PackageTemplate {
12221236
inputs,
12231237
malleability,

0 commit comments

Comments
 (0)