Skip to content

Commit 7cfd9a0

Browse files
committed
Debug assert that forwarded-claimed HTLCs are claimed backwards
1 parent 1d94baf commit 7cfd9a0

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

lightning/src/chain/channelmonitor.rs

+2-2
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

+4-4
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/channelmanager.rs

+15-3
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 {
@@ -7821,6 +7821,18 @@ where
78217821
false
78227822
} else { true }
78237823
});
7824+
#[cfg(debug_assertions)] {
7825+
if let Some(preimage) = preimage_opt {
7826+
if let Some((node_id, chan_id)) = short_to_chan_info.get(&prev_hop_data.short_channel_id) {
7827+
let chan = peer_channels.get_mut(node_id).unwrap().get_mut(chan_id).unwrap();
7828+
if let UpdateFulfillCommitFetch::DuplicateClaim {} =
7829+
chan.get_update_fulfill_htlc_and_commit(prev_hop_data.htlc_id, preimage, &args.logger) {
7830+
} else {
7831+
panic!("Forwarded HTLCs which were previously claimed absolutely must have been claimed backwards");
7832+
}
7833+
}
7834+
}
7835+
}
78247836
},
78257837
HTLCSource::OutboundRoute { payment_id, session_priv, path, .. } => {
78267838
if let Some(preimage) = preimage_opt {

0 commit comments

Comments
 (0)