Skip to content

Commit 453ed11

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 1c6ce8e commit 453ed11

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 {
@@ -4692,6 +4694,7 @@ where
46924694

46934695
let mut per_source_pending_forward = [(
46944696
payment.prev_short_channel_id,
4697+
payment.prev_counterparty_node_id,
46954698
payment.prev_funding_outpoint,
46964699
payment.prev_channel_id,
46974700
payment.prev_user_channel_id,
@@ -4722,6 +4725,7 @@ where
47224725
user_channel_id: Some(payment.prev_user_channel_id),
47234726
outpoint: payment.prev_funding_outpoint,
47244727
channel_id: payment.prev_channel_id,
4728+
counterparty_node_id: payment.prev_counterparty_node_id,
47254729
htlc_id: payment.prev_htlc_id,
47264730
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
47274731
phantom_shared_secret: None,
@@ -4851,8 +4855,10 @@ where
48514855

48524856
// Process all of the forwards and failures for the channel in which the HTLCs were
48534857
// proposed to as a batch.
4854-
let pending_forwards = (incoming_scid, incoming_funding_txo, incoming_channel_id,
4855-
incoming_user_channel_id, htlc_forwards.drain(..).collect());
4858+
let pending_forwards = (
4859+
incoming_scid, Some(incoming_counterparty_node_id), incoming_funding_txo,
4860+
incoming_channel_id, incoming_user_channel_id, htlc_forwards.drain(..).collect()
4861+
);
48564862
self.forward_htlcs_without_forward_event(&mut [pending_forwards]);
48574863
for (htlc_fail, htlc_destination) in htlc_fails.drain(..) {
48584864
let failure = match htlc_fail {
@@ -4886,7 +4892,7 @@ where
48864892

48874893
let mut new_events = VecDeque::new();
48884894
let mut failed_forwards = Vec::new();
4889-
let mut phantom_receives: Vec<(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)> = Vec::new();
4895+
let mut phantom_receives: Vec<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)> = Vec::new();
48904896
{
48914897
let mut forward_htlcs = new_hash_map();
48924898
mem::swap(&mut forward_htlcs, &mut self.forward_htlcs.lock().unwrap());
@@ -4900,7 +4906,7 @@ where
49004906
match forward_info {
49014907
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
49024908
prev_short_channel_id, prev_htlc_id, prev_channel_id, prev_funding_outpoint,
4903-
prev_user_channel_id, forward_info: PendingHTLCInfo {
4909+
prev_user_channel_id, prev_counterparty_node_id, forward_info: PendingHTLCInfo {
49044910
routing, incoming_shared_secret, payment_hash, outgoing_amt_msat,
49054911
outgoing_cltv_value, ..
49064912
}
@@ -4915,6 +4921,7 @@ where
49154921
user_channel_id: Some(prev_user_channel_id),
49164922
channel_id: prev_channel_id,
49174923
outpoint: prev_funding_outpoint,
4924+
counterparty_node_id: prev_counterparty_node_id,
49184925
htlc_id: prev_htlc_id,
49194926
incoming_packet_shared_secret: incoming_shared_secret,
49204927
phantom_shared_secret: $phantom_ss,
@@ -4977,7 +4984,10 @@ where
49774984
outgoing_cltv_value, Some(phantom_shared_secret), false, None,
49784985
current_height, self.default_configuration.accept_mpp_keysend)
49794986
{
4980-
Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)])),
4987+
Ok(info) => phantom_receives.push((
4988+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
4989+
prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)]
4990+
)),
49814991
Err(InboundHTLCErr { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
49824992
}
49834993
},
@@ -5022,7 +5032,7 @@ where
50225032
let queue_fail_htlc_res = match forward_info {
50235033
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
50245034
prev_short_channel_id, prev_htlc_id, prev_channel_id, prev_funding_outpoint,
5025-
prev_user_channel_id, forward_info: PendingHTLCInfo {
5035+
prev_user_channel_id, prev_counterparty_node_id, forward_info: PendingHTLCInfo {
50265036
incoming_shared_secret, payment_hash, outgoing_amt_msat, outgoing_cltv_value,
50275037
routing: PendingHTLCRouting::Forward {
50285038
ref onion_packet, blinded, ..
@@ -5032,6 +5042,7 @@ where
50325042
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
50335043
short_channel_id: prev_short_channel_id,
50345044
user_channel_id: Some(prev_user_channel_id),
5045+
counterparty_node_id: prev_counterparty_node_id,
50355046
channel_id: prev_channel_id,
50365047
outpoint: prev_funding_outpoint,
50375048
htlc_id: prev_htlc_id,
@@ -5160,7 +5171,7 @@ where
51605171
match forward_info {
51615172
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
51625173
prev_short_channel_id, prev_htlc_id, prev_channel_id, prev_funding_outpoint,
5163-
prev_user_channel_id, forward_info: PendingHTLCInfo {
5174+
prev_user_channel_id, prev_counterparty_node_id, forward_info: PendingHTLCInfo {
51645175
routing, incoming_shared_secret, payment_hash, incoming_amt_msat, outgoing_amt_msat,
51655176
skimmed_fee_msat, ..
51665177
}
@@ -5198,6 +5209,7 @@ where
51985209
prev_hop: HTLCPreviousHopData {
51995210
short_channel_id: prev_short_channel_id,
52005211
user_channel_id: Some(prev_user_channel_id),
5212+
counterparty_node_id: prev_counterparty_node_id,
52015213
channel_id: prev_channel_id,
52025214
outpoint: prev_funding_outpoint,
52035215
htlc_id: prev_htlc_id,
@@ -5230,6 +5242,7 @@ where
52305242
failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
52315243
short_channel_id: $htlc.prev_hop.short_channel_id,
52325244
user_channel_id: $htlc.prev_hop.user_channel_id,
5245+
counterparty_node_id: $htlc.prev_hop.counterparty_node_id,
52335246
channel_id: prev_channel_id,
52345247
outpoint: prev_funding_outpoint,
52355248
htlc_id: $htlc.prev_hop.htlc_id,
@@ -6586,7 +6599,7 @@ where
65866599
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
65876600
funding_broadcastable: Option<Transaction>,
65886601
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>)
6589-
-> (Option<(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
6602+
-> (Option<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
65906603
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
65916604
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement",
65926605
&channel.context.channel_id(),
@@ -6602,8 +6615,11 @@ where
66026615

66036616
let mut htlc_forwards = None;
66046617
if !pending_forwards.is_empty() {
6605-
htlc_forwards = Some((short_channel_id, channel.context.get_funding_txo().unwrap(),
6606-
channel.context.channel_id(), channel.context.get_user_id(), pending_forwards));
6618+
htlc_forwards = Some((
6619+
short_channel_id, Some(channel.context.get_counterparty_node_id()),
6620+
channel.context.get_funding_txo().unwrap(), channel.context.channel_id(),
6621+
channel.context.get_user_id(), pending_forwards
6622+
));
66076623
}
66086624
let mut decode_update_add_htlcs = None;
66096625
if !pending_update_adds.is_empty() {
@@ -7646,15 +7662,15 @@ where
76467662
}
76477663

76487664
#[inline]
7649-
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) {
7665+
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) {
76507666
let push_forward_event = self.forward_htlcs_without_forward_event(per_source_pending_forwards);
76517667
if push_forward_event { self.push_pending_forwards_ev() }
76527668
}
76537669

76547670
#[inline]
7655-
fn forward_htlcs_without_forward_event(&self, per_source_pending_forwards: &mut [(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) -> bool {
7671+
fn forward_htlcs_without_forward_event(&self, per_source_pending_forwards: &mut [(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) -> bool {
76567672
let mut push_forward_event = false;
7657-
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 {
7673+
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 {
76587674
let mut new_intercept_events = VecDeque::new();
76597675
let mut failed_intercept_forwards = Vec::new();
76607676
if !pending_forwards.is_empty() {
@@ -7673,7 +7689,9 @@ where
76737689
match forward_htlcs.entry(scid) {
76747690
hash_map::Entry::Occupied(mut entry) => {
76757691
entry.get_mut().push(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
7676-
prev_short_channel_id, prev_funding_outpoint, prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info }));
7692+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
7693+
prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info
7694+
}));
76777695
},
76787696
hash_map::Entry::Vacant(entry) => {
76797697
if !is_our_scid && forward_info.incoming_amt_msat.is_some() &&
@@ -7691,14 +7709,17 @@ where
76917709
intercept_id
76927710
}, None));
76937711
entry.insert(PendingAddHTLCInfo {
7694-
prev_short_channel_id, prev_funding_outpoint, prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info });
7712+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
7713+
prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info
7714+
});
76957715
},
76967716
hash_map::Entry::Occupied(_) => {
76977717
let logger = WithContext::from(&self.logger, None, Some(prev_channel_id), Some(forward_info.payment_hash));
76987718
log_info!(logger, "Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}", scid);
76997719
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
77007720
short_channel_id: prev_short_channel_id,
77017721
user_channel_id: Some(prev_user_channel_id),
7722+
counterparty_node_id: prev_counterparty_node_id,
77027723
outpoint: prev_funding_outpoint,
77037724
channel_id: prev_channel_id,
77047725
htlc_id: prev_htlc_id,
@@ -7718,7 +7739,9 @@ where
77187739
// payments are being processed.
77197740
push_forward_event |= forward_htlcs_empty && decode_update_add_htlcs_empty;
77207741
entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
7721-
prev_short_channel_id, prev_funding_outpoint, prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info })));
7742+
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
7743+
prev_channel_id, prev_htlc_id, prev_user_channel_id, forward_info
7744+
})));
77227745
}
77237746
}
77247747
}
@@ -9507,6 +9530,7 @@ where
95079530
htlc_id: htlc.prev_htlc_id,
95089531
incoming_packet_shared_secret: htlc.forward_info.incoming_shared_secret,
95099532
phantom_shared_secret: None,
9533+
counterparty_node_id: htlc.prev_counterparty_node_id,
95109534
outpoint: htlc.prev_funding_outpoint,
95119535
channel_id: htlc.prev_channel_id,
95129536
blinded_failure: htlc.forward_info.routing.blinded_failure(),
@@ -10616,6 +10640,7 @@ impl_writeable_tlv_based!(HTLCPreviousHopData, {
1061610640
// Note that by the time we get past the required read for type 2 above, outpoint will be
1061710641
// filled in, so we can safely unwrap it here.
1061810642
(9, channel_id, (default_value, ChannelId::v1_from_funding_outpoint(outpoint.0.unwrap()))),
10643+
(11, counterparty_node_id, option),
1061910644
});
1062010645

1062110646
impl Writeable for ClaimableHTLC {
@@ -10772,6 +10797,7 @@ impl_writeable_tlv_based!(PendingAddHTLCInfo, {
1077210797
// Note that by the time we get past the required read for type 6 above, prev_funding_outpoint will be
1077310798
// filled in, so we can safely unwrap it here.
1077410799
(7, prev_channel_id, (default_value, ChannelId::v1_from_funding_outpoint(prev_funding_outpoint.0.unwrap()))),
10800+
(9, prev_counterparty_node_id, option),
1077510801
});
1077610802

1077710803
impl Writeable for HTLCForwardInfo {

0 commit comments

Comments
 (0)