Skip to content

Commit a769cba

Browse files
committed
Track pending update_add_htlcs in ChannelManager for later processing
We plan to decode the onions of these `update_add_htlc`s as part of the HTLC forwarding flow (i.e., `process_pending_htlc_forwards`), so we'll need to track them per-channel at the `ChannelManager` level.
1 parent 45063ec commit a769cba

File tree

2 files changed

+68
-16
lines changed

2 files changed

+68
-16
lines changed

lightning/src/ln/channel.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,7 @@ pub(super) struct MonitorRestoreUpdates {
10691069
pub accepted_htlcs: Vec<(PendingHTLCInfo, u64)>,
10701070
pub failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
10711071
pub finalized_claimed_htlcs: Vec<HTLCSource>,
1072+
pub pending_update_adds: Vec<msgs::UpdateAddHTLC>,
10721073
pub funding_broadcastable: Option<Transaction>,
10731074
pub channel_ready: Option<msgs::ChannelReady>,
10741075
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
@@ -5248,13 +5249,16 @@ impl<SP: Deref> Channel<SP> where
52485249
mem::swap(&mut failed_htlcs, &mut self.context.monitor_pending_failures);
52495250
let mut finalized_claimed_htlcs = Vec::new();
52505251
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
5252+
let mut pending_update_adds = Vec::new();
5253+
mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
52515254

52525255
if self.context.channel_state.is_peer_disconnected() {
52535256
self.context.monitor_pending_revoke_and_ack = false;
52545257
self.context.monitor_pending_commitment_signed = false;
52555258
return MonitorRestoreUpdates {
52565259
raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
5257-
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, channel_ready, announcement_sigs
5260+
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, pending_update_adds,
5261+
funding_broadcastable, channel_ready, announcement_sigs
52585262
};
52595263
}
52605264

@@ -5276,7 +5280,8 @@ impl<SP: Deref> Channel<SP> where
52765280
if commitment_update.is_some() { "a" } else { "no" }, if raa.is_some() { "an" } else { "no" },
52775281
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
52785282
MonitorRestoreUpdates {
5279-
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, channel_ready, announcement_sigs
5283+
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
5284+
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs
52805285
}
52815286
}
52825287

lightning/src/ln/channelmanager.rs

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,8 @@ where
11781178
// | |
11791179
// | |__`pending_intercepted_htlcs`
11801180
// |
1181+
// |__`decode_update_add_htlcs`
1182+
// |
11811183
// |__`per_peer_state`
11821184
// |
11831185
// |__`pending_inbound_payments`
@@ -1268,6 +1270,18 @@ where
12681270
/// See `ChannelManager` struct-level documentation for lock order requirements.
12691271
pending_intercepted_htlcs: Mutex<HashMap<InterceptId, PendingAddHTLCInfo>>,
12701272

1273+
/// SCID/SCID Alias -> pending `update_add_htlc`s to decode.
1274+
///
1275+
/// Note that because we may have an SCID Alias as the key we can have two entries per channel,
1276+
/// though in practice we probably won't be receiving HTLCs for a channel both via the alias
1277+
/// and via the classic SCID.
1278+
///
1279+
/// Note that no consistency guarantees are made about the existence of a channel with the
1280+
/// `short_channel_id` here, nor the `channel_id` in `UpdateAddHTLC`!
1281+
///
1282+
/// See `ChannelManager` struct-level documentation for lock order requirements.
1283+
decode_update_add_htlcs: Mutex<HashMap<u64, Vec<msgs::UpdateAddHTLC>>>,
1284+
12711285
/// The sets of payments which are claimable or currently being claimed. See
12721286
/// [`ClaimablePayments`]' individual field docs for more info.
12731287
///
@@ -2235,9 +2249,9 @@ macro_rules! handle_monitor_update_completion {
22352249
let update_actions = $peer_state.monitor_update_blocked_actions
22362250
.remove(&$chan.context.channel_id()).unwrap_or(Vec::new());
22372251

2238-
let htlc_forwards = $self.handle_channel_resumption(
2252+
let (htlc_forwards, decode_update_add_htlcs) = $self.handle_channel_resumption(
22392253
&mut $peer_state.pending_msg_events, $chan, updates.raa,
2240-
updates.commitment_update, updates.order, updates.accepted_htlcs,
2254+
updates.commitment_update, updates.order, updates.accepted_htlcs, updates.pending_update_adds,
22412255
updates.funding_broadcastable, updates.channel_ready,
22422256
updates.announcement_sigs);
22432257
if let Some(upd) = channel_update {
@@ -2298,6 +2312,9 @@ macro_rules! handle_monitor_update_completion {
22982312
if let Some(forwards) = htlc_forwards {
22992313
$self.forward_htlcs(&mut [forwards][..]);
23002314
}
2315+
if let Some(decode) = decode_update_add_htlcs {
2316+
$self.decode_update_add_htlcs(decode);
2317+
}
23012318
$self.finalize_claims(updates.finalized_claimed_htlcs);
23022319
for failure in updates.failed_htlcs.drain(..) {
23032320
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
@@ -2474,6 +2491,7 @@ where
24742491
pending_inbound_payments: Mutex::new(new_hash_map()),
24752492
pending_outbound_payments: OutboundPayments::new(),
24762493
forward_htlcs: Mutex::new(new_hash_map()),
2494+
decode_update_add_htlcs: Mutex::new(new_hash_map()),
24772495
claimable_payments: Mutex::new(ClaimablePayments { claimable_payments: new_hash_map(), pending_claiming_payments: new_hash_map() }),
24782496
pending_intercepted_htlcs: Mutex::new(new_hash_map()),
24792497
outpoint_to_peer: Mutex::new(new_hash_map()),
@@ -5922,24 +5940,31 @@ where
59225940
fn handle_channel_resumption(&self, pending_msg_events: &mut Vec<MessageSendEvent>,
59235941
channel: &mut Channel<SP>, raa: Option<msgs::RevokeAndACK>,
59245942
commitment_update: Option<msgs::CommitmentUpdate>, order: RAACommitmentOrder,
5925-
pending_forwards: Vec<(PendingHTLCInfo, u64)>, funding_broadcastable: Option<Transaction>,
5943+
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
5944+
funding_broadcastable: Option<Transaction>,
59265945
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>)
5927-
-> Option<(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)> {
5946+
-> (Option<(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
59285947
let logger = WithChannelContext::from(&self.logger, &channel.context);
5929-
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {}broadcasting funding, {} channel ready, {} announcement",
5948+
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement",
59305949
&channel.context.channel_id(),
59315950
if raa.is_some() { "an" } else { "no" },
5932-
if commitment_update.is_some() { "a" } else { "no" }, pending_forwards.len(),
5951+
if commitment_update.is_some() { "a" } else { "no" },
5952+
pending_forwards.len(), pending_update_adds.len(),
59335953
if funding_broadcastable.is_some() { "" } else { "not " },
59345954
if channel_ready.is_some() { "sending" } else { "without" },
59355955
if announcement_sigs.is_some() { "sending" } else { "without" });
59365956

5937-
let mut htlc_forwards = None;
5938-
59395957
let counterparty_node_id = channel.context.get_counterparty_node_id();
5958+
let short_channel_id = channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
5959+
5960+
let mut htlc_forwards = None;
59405961
if !pending_forwards.is_empty() {
5941-
htlc_forwards = Some((channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias()),
5942-
channel.context.get_funding_txo().unwrap(), channel.context.channel_id(), channel.context.get_user_id(), pending_forwards));
5962+
htlc_forwards = Some((short_channel_id, channel.context.get_funding_txo().unwrap(),
5963+
channel.context.channel_id(), channel.context.get_user_id(), pending_forwards));
5964+
}
5965+
let mut decode_update_add_htlcs = None;
5966+
if !pending_update_adds.is_empty() {
5967+
decode_update_add_htlcs = Some((short_channel_id, pending_update_adds));
59435968
}
59445969

59455970
if let Some(msg) = channel_ready {
@@ -5990,7 +6015,7 @@ where
59906015
emit_channel_ready_event!(pending_events, channel);
59916016
}
59926017

5993-
htlc_forwards
6018+
(htlc_forwards, decode_update_add_htlcs)
59946019
}
59956020

59966021
fn channel_monitor_updated(&self, funding_txo: &OutPoint, channel_id: &ChannelId, highest_applied_update_id: u64, counterparty_node_id: Option<&PublicKey>) {
@@ -6953,6 +6978,17 @@ where
69536978
}
69546979
}
69556980

6981+
fn decode_update_add_htlcs(&self, update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {
6982+
let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
6983+
let scid = update_add_htlcs.0;
6984+
for update_add_htlc in update_add_htlcs.1 {
6985+
match decode_update_add_htlcs.entry(scid) {
6986+
hash_map::Entry::Occupied(mut e) => { e.get_mut().push(update_add_htlc); },
6987+
hash_map::Entry::Vacant(e) => { e.insert(vec![update_add_htlc]); },
6988+
}
6989+
}
6990+
}
6991+
69566992
#[inline]
69576993
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) {
69586994
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 {
@@ -7289,10 +7325,10 @@ where
72897325
}
72907326
}
72917327
let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
7292-
let htlc_forwards = self.handle_channel_resumption(
7328+
let (htlc_forwards, decode_update_add_htlcs) = self.handle_channel_resumption(
72937329
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.order,
7294-
Vec::new(), None, responses.channel_ready, responses.announcement_sigs);
7295-
debug_assert!(htlc_forwards.is_none());
7330+
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs);
7331+
debug_assert!(htlc_forwards.is_none() && decode_update_add_htlcs.is_none());
72967332
if let Some(upd) = channel_update {
72977333
peer_state.pending_msg_events.push(upd);
72987334
}
@@ -10146,6 +10182,12 @@ where
1014610182
}
1014710183
}
1014810184

10185+
let mut decode_update_add_htlcs_opt = None;
10186+
let decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
10187+
if !decode_update_add_htlcs.is_empty() {
10188+
decode_update_add_htlcs_opt = Some(decode_update_add_htlcs);
10189+
}
10190+
1014910191
let per_peer_state = self.per_peer_state.write().unwrap();
1015010192

1015110193
let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
@@ -10297,6 +10339,7 @@ where
1029710339
(10, in_flight_monitor_updates, option),
1029810340
(11, self.probing_cookie_secret, required),
1029910341
(13, htlc_onion_fields, optional_vec),
10342+
(14, decode_update_add_htlcs_opt, option),
1030010343
});
1030110344

1030210345
Ok(())
@@ -10762,6 +10805,7 @@ where
1076210805
let mut monitor_update_blocked_actions_per_peer: Option<Vec<(_, BTreeMap<_, Vec<_>>)>> = Some(Vec::new());
1076310806
let mut events_override = None;
1076410807
let mut in_flight_monitor_updates: Option<HashMap<(PublicKey, OutPoint), Vec<ChannelMonitorUpdate>>> = None;
10808+
let mut decode_update_add_htlcs: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> = None;
1076510809
read_tlv_fields!(reader, {
1076610810
(1, pending_outbound_payments_no_retry, option),
1076710811
(2, pending_intercepted_htlcs, option),
@@ -10775,7 +10819,9 @@ where
1077510819
(10, in_flight_monitor_updates, option),
1077610820
(11, probing_cookie_secret, option),
1077710821
(13, claimable_htlc_onion_fields, optional_vec),
10822+
(14, decode_update_add_htlcs, option),
1077810823
});
10824+
let decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or(new_hash_map());
1077910825
if fake_scid_rand_bytes.is_none() {
1078010826
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
1078110827
}
@@ -11310,6 +11356,7 @@ where
1131011356
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
1131111357

1131211358
forward_htlcs: Mutex::new(forward_htlcs),
11359+
decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs),
1131311360
claimable_payments: Mutex::new(ClaimablePayments { claimable_payments, pending_claiming_payments: pending_claiming_payments.unwrap() }),
1131411361
outbound_scid_aliases: Mutex::new(outbound_scid_aliases),
1131511362
outpoint_to_peer: Mutex::new(outpoint_to_peer),

0 commit comments

Comments
 (0)