Skip to content

Commit 24158d5

Browse files
committed
Track the cp node_id which originated an HTLC in the HTLCSource
Because we track pending `ChannelMonitorUpdate`s per-peer, we really need to know what peer an HTLC came from when we go to claim it on-chain, allowing us to look up any blocked actions in the `PeerState`. While we could do this by moving the blocked actions to some global "closed-channel update queue", its simpler to do it this way. While this trades off `ChannelMonitorUpdate` size somewhat (the `HTLCSource` is included in many of them), which we should be sensitive to, it will also allow us to (eventually) remove the SCID -> peer + channel_id lookups we do when claiming or failing today, which are somewhat annoying to deal with.
1 parent f267ffe commit 24158d5

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

lightning/src/ln/channelmanager.rs

+43-17
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ pub(super) struct PendingAddHTLCInfo {
306306
// Note that this may be an outbound SCID alias for the associated channel.
307307
prev_short_channel_id: u64,
308308
prev_htlc_id: u64,
309+
prev_counterparty_node_id: Option<PublicKey>,
309310
prev_channel_id: ChannelId,
310311
prev_funding_outpoint: OutPoint,
311312
prev_user_channel_id: u128,
@@ -349,9 +350,10 @@ pub(crate) struct HTLCPreviousHopData {
349350
blinded_failure: Option<BlindedFailure>,
350351
channel_id: ChannelId,
351352

352-
// This field is consumed by `claim_funds_from_hop()` when updating a force-closed backwards
353+
// These fields are consumed by `claim_funds_from_hop()` when updating a force-closed backwards
353354
// channel with a preimage provided by the forward channel.
354355
outpoint: OutPoint,
356+
counterparty_node_id: Option<PublicKey>,
355357
}
356358

357359
enum OnionPayload {
@@ -4663,6 +4665,7 @@ where
46634665

46644666
let mut per_source_pending_forward = [(
46654667
payment.prev_short_channel_id,
4668+
payment.prev_counterparty_node_id,
46664669
payment.prev_funding_outpoint,
46674670
payment.prev_channel_id,
46684671
payment.prev_user_channel_id,
@@ -4693,6 +4696,7 @@ where
46934696
user_channel_id: Some(payment.prev_user_channel_id),
46944697
outpoint: payment.prev_funding_outpoint,
46954698
channel_id: payment.prev_channel_id,
4699+
counterparty_node_id: payment.prev_counterparty_node_id,
46964700
htlc_id: payment.prev_htlc_id,
46974701
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
46984702
phantom_shared_secret: None,
@@ -4822,8 +4826,10 @@ where
48224826

48234827
// Process all of the forwards and failures for the channel in which the HTLCs were
48244828
// proposed to as a batch.
4825-
let pending_forwards = (incoming_scid, incoming_funding_txo, incoming_channel_id,
4826-
incoming_user_channel_id, htlc_forwards.drain(..).collect());
4829+
let pending_forwards = (
4830+
incoming_scid, Some(incoming_counterparty_node_id), incoming_funding_txo,
4831+
incoming_channel_id, incoming_user_channel_id, htlc_forwards.drain(..).collect()
4832+
);
48274833
self.forward_htlcs_without_forward_event(&mut [pending_forwards]);
48284834
for (htlc_fail, htlc_destination) in htlc_fails.drain(..) {
48294835
let failure = match htlc_fail {
@@ -4857,7 +4863,7 @@ where
48574863

48584864
let mut new_events = VecDeque::new();
48594865
let mut failed_forwards = Vec::new();
4860-
let mut phantom_receives: Vec<(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)> = Vec::new();
4866+
let mut phantom_receives: Vec<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)> = Vec::new();
48614867
{
48624868
let mut forward_htlcs = new_hash_map();
48634869
mem::swap(&mut forward_htlcs, &mut self.forward_htlcs.lock().unwrap());
@@ -4871,7 +4877,7 @@ where
48714877
match forward_info {
48724878
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
48734879
prev_short_channel_id, prev_htlc_id, prev_channel_id, prev_funding_outpoint,
4874-
prev_user_channel_id, forward_info: PendingHTLCInfo {
4880+
prev_user_channel_id, prev_counterparty_node_id, forward_info: PendingHTLCInfo {
48754881
routing, incoming_shared_secret, payment_hash, outgoing_amt_msat,
48764882
outgoing_cltv_value, ..
48774883
}
@@ -4886,6 +4892,7 @@ where
48864892
user_channel_id: Some(prev_user_channel_id),
48874893
channel_id: prev_channel_id,
48884894
outpoint: prev_funding_outpoint,
4895+
counterparty_node_id: prev_counterparty_node_id,
48894896
htlc_id: prev_htlc_id,
48904897
incoming_packet_shared_secret: incoming_shared_secret,
48914898
phantom_shared_secret: $phantom_ss,
@@ -4948,7 +4955,10 @@ where
49484955
outgoing_cltv_value, Some(phantom_shared_secret), false, None,
49494956
current_height, self.default_configuration.accept_mpp_keysend)
49504957
{
4951-
Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)])),
4958+
Ok(info) => phantom_receives.push((
4959+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
4960+
prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)]
4961+
)),
49524962
Err(InboundHTLCErr { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
49534963
}
49544964
},
@@ -4994,7 +5004,7 @@ where
49945004
let queue_fail_htlc_res = match forward_info {
49955005
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
49965006
prev_short_channel_id, prev_htlc_id, prev_channel_id, prev_funding_outpoint,
4997-
prev_user_channel_id, forward_info: PendingHTLCInfo {
5007+
prev_user_channel_id, prev_counterparty_node_id, forward_info: PendingHTLCInfo {
49985008
incoming_shared_secret, payment_hash, outgoing_amt_msat, outgoing_cltv_value,
49995009
routing: PendingHTLCRouting::Forward {
50005010
onion_packet, blinded, ..
@@ -5006,6 +5016,7 @@ where
50065016
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
50075017
short_channel_id: prev_short_channel_id,
50085018
user_channel_id: Some(prev_user_channel_id),
5019+
counterparty_node_id: prev_counterparty_node_id,
50095020
channel_id: prev_channel_id,
50105021
outpoint: prev_funding_outpoint,
50115022
htlc_id: prev_htlc_id,
@@ -5079,7 +5090,7 @@ where
50795090
match forward_info {
50805091
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
50815092
prev_short_channel_id, prev_htlc_id, prev_channel_id, prev_funding_outpoint,
5082-
prev_user_channel_id, forward_info: PendingHTLCInfo {
5093+
prev_user_channel_id, prev_counterparty_node_id, forward_info: PendingHTLCInfo {
50835094
routing, incoming_shared_secret, payment_hash, incoming_amt_msat, outgoing_amt_msat,
50845095
skimmed_fee_msat, ..
50855096
}
@@ -5117,6 +5128,7 @@ where
51175128
prev_hop: HTLCPreviousHopData {
51185129
short_channel_id: prev_short_channel_id,
51195130
user_channel_id: Some(prev_user_channel_id),
5131+
counterparty_node_id: prev_counterparty_node_id,
51205132
channel_id: prev_channel_id,
51215133
outpoint: prev_funding_outpoint,
51225134
htlc_id: prev_htlc_id,
@@ -5149,6 +5161,7 @@ where
51495161
failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
51505162
short_channel_id: $htlc.prev_hop.short_channel_id,
51515163
user_channel_id: $htlc.prev_hop.user_channel_id,
5164+
counterparty_node_id: $htlc.prev_hop.counterparty_node_id,
51525165
channel_id: prev_channel_id,
51535166
outpoint: prev_funding_outpoint,
51545167
htlc_id: $htlc.prev_hop.htlc_id,
@@ -6517,7 +6530,7 @@ where
65176530
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
65186531
funding_broadcastable: Option<Transaction>,
65196532
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>)
6520-
-> (Option<(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
6533+
-> (Option<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
65216534
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
65226535
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement",
65236536
&channel.context.channel_id(),
@@ -6533,8 +6546,11 @@ where
65336546

65346547
let mut htlc_forwards = None;
65356548
if !pending_forwards.is_empty() {
6536-
htlc_forwards = Some((short_channel_id, channel.context.get_funding_txo().unwrap(),
6537-
channel.context.channel_id(), channel.context.get_user_id(), pending_forwards));
6549+
htlc_forwards = Some((
6550+
short_channel_id, Some(channel.context.get_counterparty_node_id()),
6551+
channel.context.get_funding_txo().unwrap(), channel.context.channel_id(),
6552+
channel.context.get_user_id(), pending_forwards
6553+
));
65386554
}
65396555
let mut decode_update_add_htlcs = None;
65406556
if !pending_update_adds.is_empty() {
@@ -7577,15 +7593,15 @@ where
75777593
}
75787594

75797595
#[inline]
7580-
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) {
7596+
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) {
75817597
let push_forward_event = self.forward_htlcs_without_forward_event(per_source_pending_forwards);
75827598
if push_forward_event { self.push_pending_forwards_ev() }
75837599
}
75847600

75857601
#[inline]
7586-
fn forward_htlcs_without_forward_event(&self, per_source_pending_forwards: &mut [(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) -> bool {
7602+
fn forward_htlcs_without_forward_event(&self, per_source_pending_forwards: &mut [(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) -> bool {
75877603
let mut push_forward_event = false;
7588-
for &mut (prev_short_channel_id, prev_funding_outpoint, prev_channel_id, prev_user_channel_id, ref mut pending_forwards) in per_source_pending_forwards {
7604+
for &mut (prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint, prev_channel_id, prev_user_channel_id, ref mut pending_forwards) in per_source_pending_forwards {
75897605
let mut new_intercept_events = VecDeque::new();
75907606
let mut failed_intercept_forwards = Vec::new();
75917607
if !pending_forwards.is_empty() {
@@ -7604,7 +7620,9 @@ where
76047620
match forward_htlcs.entry(scid) {
76057621
hash_map::Entry::Occupied(mut entry) => {
76067622
entry.get_mut().push(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
7607-
prev_short_channel_id, prev_funding_outpoint, prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info }));
7623+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
7624+
prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info
7625+
}));
76087626
},
76097627
hash_map::Entry::Vacant(entry) => {
76107628
if !is_our_scid && forward_info.incoming_amt_msat.is_some() &&
@@ -7622,14 +7640,17 @@ where
76227640
intercept_id
76237641
}, None));
76247642
entry.insert(PendingAddHTLCInfo {
7625-
prev_short_channel_id, prev_funding_outpoint, prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info });
7643+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
7644+
prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info
7645+
});
76267646
},
76277647
hash_map::Entry::Occupied(_) => {
76287648
let logger = WithContext::from(&self.logger, None, Some(prev_channel_id), Some(forward_info.payment_hash));
76297649
log_info!(logger, "Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}", scid);
76307650
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
76317651
short_channel_id: prev_short_channel_id,
76327652
user_channel_id: Some(prev_user_channel_id),
7653+
counterparty_node_id: prev_counterparty_node_id,
76337654
outpoint: prev_funding_outpoint,
76347655
channel_id: prev_channel_id,
76357656
htlc_id: prev_htlc_id,
@@ -7649,7 +7670,9 @@ where
76497670
// payments are being processed.
76507671
push_forward_event |= forward_htlcs_empty && decode_update_add_htlcs_empty;
76517672
entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
7652-
prev_short_channel_id, prev_funding_outpoint, prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info })));
7673+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
7674+
prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info
7675+
})));
76537676
}
76547677
}
76557678
}
@@ -9438,6 +9461,7 @@ where
94389461
htlc_id: htlc.prev_htlc_id,
94399462
incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
94409463
phantom_shared_secret: None,
9464+
counterparty_node_id: htlc.prev_counterparty_node_id,
94419465
outpoint: htlc.prev_funding_outpoint,
94429466
channel_id: htlc.prev_channel_id,
94439467
blinded_failure: htlc.forward_info.routing.blinded_failure(),
@@ -10533,6 +10557,7 @@ impl_writeable_tlv_based!(HTLCPreviousHopData, {
1053310557
// Note that by the time we get past the required read for type 2 above, outpoint will be
1053410558
// filled in, so we can safely unwrap it here.
1053510559
(9, channel_id, (default_value, ChannelId::v1_from_funding_outpoint(outpoint.0.unwrap()))),
10560+
(11, counterparty_node_id, option),
1053610561
});
1053710562

1053810563
impl Writeable for ClaimableHTLC {
@@ -10689,6 +10714,7 @@ impl_writeable_tlv_based!(PendingAddHTLCInfo, {
1068910714
// Note that by the time we get past the required read for type 6 above, prev_funding_outpoint will be
1069010715
// filled in, so we can safely unwrap it here.
1069110716
(7, prev_channel_id, (default_value, ChannelId::v1_from_funding_outpoint(prev_funding_outpoint.0.unwrap()))),
10717+
(9, prev_counterparty_node_id, option),
1069210718
});
1069310719

1069410720
impl Writeable for HTLCForwardInfo {

0 commit comments

Comments
 (0)