Skip to content

Commit 5c38da2

Browse files
committed
Debug assert that forwarded-claimed HTLCs are claimed backwards
1 parent 6febc16 commit 5c38da2

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use crate::sync::{Mutex, LockTestExt};
7070
/// updates (e.g. ones during which there are hundreds of HTLCs pending on the commitment
7171
/// transaction), a single update may reach upwards of 1 MiB in serialized size.
7272
#[cfg_attr(any(test, fuzzing, feature = "_test_utils"), derive(PartialEq, Eq))]
73-
#[derive(Clone)]
73+
#[derive(Clone, Debug)]
7474
#[must_use]
7575
pub struct ChannelMonitorUpdate {
7676
pub(crate) updates: Vec<ChannelMonitorUpdateStep>,
@@ -489,7 +489,7 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
489489
);
490490

491491
#[cfg_attr(any(test, fuzzing, feature = "_test_utils"), derive(PartialEq, Eq))]
492-
#[derive(Clone)]
492+
#[derive(Clone, Debug)]
493493
pub(crate) enum ChannelMonitorUpdateStep {
494494
LatestHolderCommitmentTXInfo {
495495
commitment_tx: HolderCommitmentTransaction,

lightning/src/ln/chan_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp2
437437
/// channel basepoints via the new function, or they were obtained via
438438
/// CommitmentTransaction.trust().keys() because we trusted the source of the
439439
/// pre-calculated keys.
440-
#[derive(PartialEq, Eq, Clone)]
440+
#[derive(PartialEq, Eq, Clone, Debug)]
441441
pub struct TxCreationKeys {
442442
/// The broadcaster's per-commitment public key which was used to derive the other keys.
443443
pub per_commitment_point: PublicKey,
@@ -949,7 +949,7 @@ impl<'a> DirectedChannelTransactionParameters<'a> {
949949
/// Information needed to build and sign a holder's commitment transaction.
950950
///
951951
/// The transaction is only signed once we are ready to broadcast.
952-
#[derive(Clone)]
952+
#[derive(Clone, Debug)]
953953
pub struct HolderCommitmentTransaction {
954954
inner: CommitmentTransaction,
955955
/// Our counterparty's signature for the transaction
@@ -1052,7 +1052,7 @@ impl HolderCommitmentTransaction {
10521052
}
10531053

10541054
/// A pre-built Bitcoin commitment transaction and its txid.
1055-
#[derive(Clone)]
1055+
#[derive(Clone, Debug)]
10561056
pub struct BuiltCommitmentTransaction {
10571057
/// The commitment transaction
10581058
pub transaction: Transaction,
@@ -1215,7 +1215,7 @@ impl<'a> TrustedClosingTransaction<'a> {
12151215
///
12161216
/// This class can be used inside a signer implementation to generate a signature given the relevant
12171217
/// secret key.
1218-
#[derive(Clone)]
1218+
#[derive(Clone, Debug)]
12191219
pub struct CommitmentTransaction {
12201220
commitment_number: u64,
12211221
to_broadcaster_value_sat: u64,

lightning/src/ln/channel.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ enum UpdateFulfillFetch {
394394
}
395395

396396
/// The return type of get_update_fulfill_htlc_and_commit.
397+
#[derive(Debug, PartialEq)]
397398
pub enum UpdateFulfillCommitFetch<'a> {
398399
/// Indicates the HTLC fulfill is new, and either generated an update_fulfill message, placed
399400
/// it in the holding cell, or re-generated the update_fulfill message after the same claim was

lightning/src/ln/channelmanager.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub(super) enum HTLCForwardInfo {
161161
}
162162

163163
/// Tracks the inbound corresponding to an outbound HTLC
164-
#[derive(Clone, Hash, PartialEq, Eq)]
164+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
165165
pub(crate) struct HTLCPreviousHopData {
166166
// Note that this may be an outbound SCID alias for the associated channel.
167167
short_channel_id: u64,
@@ -233,7 +233,7 @@ impl Readable for InterceptId {
233233
}
234234
}
235235

236-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
236+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
237237
/// Uniquely describes an HTLC by its source. Just the guaranteed-unique subset of [`HTLCSource`].
238238
pub(crate) enum SentHTLCId {
239239
PreviousHopData { short_channel_id: u64, htlc_id: u64 },
@@ -264,7 +264,7 @@ impl_writeable_tlv_based_enum!(SentHTLCId,
264264

265265
/// Tracks the inbound corresponding to an outbound HTLC
266266
#[allow(clippy::derive_hash_xor_eq)] // Our Hash is faithful to the data, we just don't have SecretKey::hash
267-
#[derive(Clone, PartialEq, Eq)]
267+
#[derive(Clone, Debug, PartialEq, Eq)]
268268
pub(crate) enum HTLCSource {
269269
PreviousHopData(HTLCPreviousHopData),
270270
OutboundRoute {
@@ -7753,6 +7753,17 @@ where
77537753
false
77547754
} else { true }
77557755
});
7756+
#[cfg(debug_assertions)] {
7757+
if let Some(preimage) = preimage_opt {
7758+
if let Some((node_id, chan_id)) = short_to_chan_info.get(&prev_hop_data.short_channel_id) {
7759+
let chan = peer_channels.get_mut(node_id).unwrap().get_mut(chan_id).unwrap();
7760+
assert_eq!(
7761+
chan.get_update_fulfill_htlc_and_commit(prev_hop_data.htlc_id, preimage, &args.logger),
7762+
UpdateFulfillCommitFetch::DuplicateClaim {},
7763+
"Forwarded HTLCs which were previously claimed absolutely must have been claimed backwards");
7764+
}
7765+
}
7766+
}
77567767
},
77577768
HTLCSource::OutboundRoute { payment_id, session_priv, path, .. } => {
77587769
if let Some(preimage) = preimage_opt {

0 commit comments

Comments
 (0)