Skip to content

Commit d3e9cd0

Browse files
Dedup data in InboundUpdateAdd::Forwarded::hop_data
Previously, the InboundUpdateAdd::Forwarded enum variant contained an HTLCPreviousHopData, which had a lot of fields that were redundant with the outer InboundHTLCOutput/Channel structs. Here we dedup those fields, which is important because the pending InboundUpdateAdds are persisted whenever the ChannelManager is persisted.
1 parent 07b3def commit d3e9cd0

2 files changed

Lines changed: 56 additions & 15 deletions

File tree

lightning/src/ln/channel.rs

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ use crate::ln::channel_state::{
5050
OutboundHTLCDetails, OutboundHTLCStateDetails,
5151
};
5252
use crate::ln::channelmanager::{
53-
self, ChannelReadyOrder, FundingConfirmedMessage, HTLCFailureMsg, HTLCPreviousHopData,
54-
HTLCSource, OpenChannelMessage, PaymentClaimDetails, PendingHTLCInfo, PendingHTLCStatus,
55-
RAACommitmentOrder, SentHTLCId, BREAKDOWN_TIMEOUT, MAX_LOCAL_BREAKDOWN_TIMEOUT,
56-
MIN_CLTV_EXPIRY_DELTA,
53+
self, BlindedFailure, ChannelReadyOrder, FundingConfirmedMessage, HTLCFailureMsg,
54+
HTLCPreviousHopData, HTLCSource, OpenChannelMessage, PaymentClaimDetails, PendingHTLCInfo,
55+
PendingHTLCStatus, RAACommitmentOrder, SentHTLCId, BREAKDOWN_TIMEOUT,
56+
MAX_LOCAL_BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA,
5757
};
5858
use crate::ln::funding::{FundingTxInput, SpliceContribution};
5959
use crate::ln::interactivetxs::{
@@ -320,12 +320,16 @@ enum InboundUpdateAdd {
320320
WithOnion { update_add_htlc: msgs::UpdateAddHTLC },
321321
/// This inbound HTLC is a forward that was irrevocably committed to the outbound edge, allowing
322322
/// its onion to be pruned and no longer persisted.
323+
///
324+
/// Contains data that is useful if we need to fail or claim this HTLC backwards after a restart
325+
/// and it's missing in the outbound edge.
323326
Forwarded {
324-
/// Useful if we need to fail or claim this HTLC backwards after restart, if it's missing in the
325-
/// outbound edge.
326-
hop_data: HTLCPreviousHopData,
327-
/// Useful if we need to claim this HTLC backwards after a restart and it's missing in the
328-
/// outbound edge, to generate an accurate [`Event::PaymentForwarded`].
327+
incoming_packet_shared_secret: [u8; 32],
328+
phantom_shared_secret: Option<[u8; 32]>,
329+
trampoline_shared_secret: Option<[u8; 32]>,
330+
blinded_failure: Option<BlindedFailure>,
331+
/// Useful for generating an accurate [`Event::PaymentForwarded`], if we need to claim this
332+
/// HTLC post-restart.
329333
///
330334
/// [`Event::PaymentForwarded`]: crate::events::Event::PaymentForwarded
331335
outbound_amt_msat: u64,
@@ -341,8 +345,11 @@ impl_writeable_tlv_based_enum_upgradable!(InboundUpdateAdd,
341345
},
342346
(2, Legacy) => {},
343347
(4, Forwarded) => {
344-
(0, hop_data, required),
348+
(0, incoming_packet_shared_secret, required),
345349
(2, outbound_amt_msat, required),
350+
(4, phantom_shared_secret, option),
351+
(6, trampoline_shared_secret, option),
352+
(8, blinded_failure, option),
346353
},
347354
);
348355

@@ -7927,14 +7934,42 @@ where
79277934
})
79287935
};
79297936

7937+
let prev_outbound_scid_alias = self.context.outbound_scid_alias();
7938+
let user_channel_id = self.context.get_user_id();
7939+
let channel_id = self.context.channel_id();
7940+
let outpoint = self.funding_outpoint();
7941+
let counterparty_node_id = self.context.get_counterparty_node_id();
7942+
79307943
self.context.pending_inbound_htlcs.iter().filter_map(move |htlc| match &htlc.state {
79317944
InboundHTLCState::Committed {
7932-
update_add_htlc: InboundUpdateAdd::Forwarded { hop_data, outbound_amt_msat },
7945+
update_add_htlc:
7946+
InboundUpdateAdd::Forwarded {
7947+
incoming_packet_shared_secret,
7948+
phantom_shared_secret,
7949+
trampoline_shared_secret,
7950+
blinded_failure,
7951+
outbound_amt_msat,
7952+
},
79337953
} => {
79347954
if htlc_resolution_in_holding_cell(htlc.htlc_id) {
79357955
return None;
79367956
}
7937-
Some((htlc.payment_hash, hop_data.clone(), *outbound_amt_msat))
7957+
// The reconstructed `HTLCPreviousHopData` is used to fail or claim the HTLC backwards
7958+
// post-restart, if it is missing in the outbound edge.
7959+
let hop_data = HTLCPreviousHopData {
7960+
prev_outbound_scid_alias,
7961+
user_channel_id: Some(user_channel_id),
7962+
htlc_id: htlc.htlc_id,
7963+
incoming_packet_shared_secret: *incoming_packet_shared_secret,
7964+
phantom_shared_secret: *phantom_shared_secret,
7965+
trampoline_shared_secret: *trampoline_shared_secret,
7966+
blinded_failure: *blinded_failure,
7967+
channel_id,
7968+
outpoint,
7969+
counterparty_node_id: Some(counterparty_node_id),
7970+
cltv_expiry: Some(htlc.cltv_expiry),
7971+
};
7972+
Some((htlc.payment_hash, hop_data, *outbound_amt_msat))
79387973
},
79397974
_ => None,
79407975
})
@@ -7984,12 +8019,18 @@ where
79848019
/// This inbound HTLC was irrevocably forwarded to the outbound edge, so we no longer need to
79858020
/// persist its onion.
79868021
pub(super) fn prune_inbound_htlc_onion(
7987-
&mut self, htlc_id: u64, hop_data: HTLCPreviousHopData, outbound_amt_msat: u64,
8022+
&mut self, htlc_id: u64, hop_data: &HTLCPreviousHopData, outbound_amt_msat: u64,
79888023
) {
79898024
for htlc in self.context.pending_inbound_htlcs.iter_mut() {
79908025
if htlc.htlc_id == htlc_id {
79918026
if let InboundHTLCState::Committed { ref mut update_add_htlc } = htlc.state {
7992-
*update_add_htlc = InboundUpdateAdd::Forwarded { hop_data, outbound_amt_msat };
8027+
*update_add_htlc = InboundUpdateAdd::Forwarded {
8028+
incoming_packet_shared_secret: hop_data.incoming_packet_shared_secret,
8029+
phantom_shared_secret: hop_data.phantom_shared_secret,
8030+
trampoline_shared_secret: hop_data.trampoline_shared_secret,
8031+
blinded_failure: hop_data.blinded_failure,
8032+
outbound_amt_msat,
8033+
};
79938034
return;
79948035
}
79958036
}

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10161,7 +10161,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1016110161
if let Some(chan) =
1016210162
peer_state.channel_by_id.get_mut(&source.channel_id).and_then(|c| c.as_funded_mut())
1016310163
{
10164-
chan.prune_inbound_htlc_onion(source.htlc_id, source, outbound_amt_msat);
10164+
chan.prune_inbound_htlc_onion(source.htlc_id, &source, outbound_amt_msat);
1016510165
}
1016610166
}
1016710167
}

0 commit comments

Comments
 (0)