Skip to content

Commit 0b38b39

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 60ddd5d commit 0b38b39

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

lightning/src/ln/channel.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ pub(super) struct MonitorRestoreUpdates {
10721072
pub accepted_htlcs: Vec<(PendingHTLCInfo, u64)>,
10731073
pub failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
10741074
pub finalized_claimed_htlcs: Vec<HTLCSource>,
1075+
pub pending_update_adds: Vec<msgs::UpdateAddHTLC>,
10751076
pub funding_broadcastable: Option<Transaction>,
10761077
pub channel_ready: Option<msgs::ChannelReady>,
10771078
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
@@ -5251,13 +5252,16 @@ impl<SP: Deref> Channel<SP> where
52515252
mem::swap(&mut failed_htlcs, &mut self.context.monitor_pending_failures);
52525253
let mut finalized_claimed_htlcs = Vec::new();
52535254
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
5255+
let mut pending_update_adds = Vec::new();
5256+
mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
52545257

52555258
if self.context.channel_state.is_peer_disconnected() {
52565259
self.context.monitor_pending_revoke_and_ack = false;
52575260
self.context.monitor_pending_commitment_signed = false;
52585261
return MonitorRestoreUpdates {
52595262
raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
5260-
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, channel_ready, announcement_sigs
5263+
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, pending_update_adds,
5264+
funding_broadcastable, channel_ready, announcement_sigs
52615265
};
52625266
}
52635267

@@ -5279,7 +5283,8 @@ impl<SP: Deref> Channel<SP> where
52795283
if commitment_update.is_some() { "a" } else { "no" }, if raa.is_some() { "an" } else { "no" },
52805284
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
52815285
MonitorRestoreUpdates {
5282-
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, channel_ready, announcement_sigs
5286+
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
5287+
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs
52835288
}
52845289
}
52855290

lightning/src/ln/channelmanager.rs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,8 @@ where
11811181
// | |
11821182
// | |__`pending_intercepted_htlcs`
11831183
// |
1184+
// |__`decode_update_add_htlcs`
1185+
// |
11841186
// |__`per_peer_state`
11851187
// |
11861188
// |__`pending_inbound_payments`
@@ -1271,6 +1273,18 @@ where
12711273
/// See `ChannelManager` struct-level documentation for lock order requirements.
12721274
pending_intercepted_htlcs: Mutex<HashMap<InterceptId, PendingAddHTLCInfo>>,
12731275

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

2241-
let htlc_forwards = $self.handle_channel_resumption(
2255+
let (htlc_forwards, decode_update_add_htlcs) = $self.handle_channel_resumption(
22422256
&mut $peer_state.pending_msg_events, $chan, updates.raa,
2243-
updates.commitment_update, updates.order, updates.accepted_htlcs,
2257+
updates.commitment_update, updates.order, updates.accepted_htlcs, updates.pending_update_adds,
22442258
updates.funding_broadcastable, updates.channel_ready,
22452259
updates.announcement_sigs);
22462260
if let Some(upd) = channel_update {
@@ -2301,6 +2315,9 @@ macro_rules! handle_monitor_update_completion {
23012315
if let Some(forwards) = htlc_forwards {
23022316
$self.forward_htlcs(&mut [forwards][..]);
23032317
}
2318+
if let Some(decode) = decode_update_add_htlcs {
2319+
$self.push_decode_update_add_htlcs(decode);
2320+
}
23042321
$self.finalize_claims(updates.finalized_claimed_htlcs);
23052322
for failure in updates.failed_htlcs.drain(..) {
23062323
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
@@ -2477,6 +2494,7 @@ where
24772494
pending_inbound_payments: Mutex::new(new_hash_map()),
24782495
pending_outbound_payments: OutboundPayments::new(),
24792496
forward_htlcs: Mutex::new(new_hash_map()),
2497+
decode_update_add_htlcs: Mutex::new(new_hash_map()),
24802498
claimable_payments: Mutex::new(ClaimablePayments { claimable_payments: new_hash_map(), pending_claiming_payments: new_hash_map() }),
24812499
pending_intercepted_htlcs: Mutex::new(new_hash_map()),
24822500
outpoint_to_peer: Mutex::new(new_hash_map()),
@@ -5929,24 +5947,31 @@ where
59295947
fn handle_channel_resumption(&self, pending_msg_events: &mut Vec<MessageSendEvent>,
59305948
channel: &mut Channel<SP>, raa: Option<msgs::RevokeAndACK>,
59315949
commitment_update: Option<msgs::CommitmentUpdate>, order: RAACommitmentOrder,
5932-
pending_forwards: Vec<(PendingHTLCInfo, u64)>, funding_broadcastable: Option<Transaction>,
5950+
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
5951+
funding_broadcastable: Option<Transaction>,
59335952
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>)
5934-
-> Option<(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)> {
5953+
-> (Option<(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
59355954
let logger = WithChannelContext::from(&self.logger, &channel.context);
5936-
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {}broadcasting funding, {} channel ready, {} announcement",
5955+
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement",
59375956
&channel.context.channel_id(),
59385957
if raa.is_some() { "an" } else { "no" },
5939-
if commitment_update.is_some() { "a" } else { "no" }, pending_forwards.len(),
5958+
if commitment_update.is_some() { "a" } else { "no" },
5959+
pending_forwards.len(), pending_update_adds.len(),
59405960
if funding_broadcastable.is_some() { "" } else { "not " },
59415961
if channel_ready.is_some() { "sending" } else { "without" },
59425962
if announcement_sigs.is_some() { "sending" } else { "without" });
59435963

5944-
let mut htlc_forwards = None;
5945-
59465964
let counterparty_node_id = channel.context.get_counterparty_node_id();
5965+
let short_channel_id = channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
5966+
5967+
let mut htlc_forwards = None;
59475968
if !pending_forwards.is_empty() {
5948-
htlc_forwards = Some((channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias()),
5949-
channel.context.get_funding_txo().unwrap(), channel.context.channel_id(), channel.context.get_user_id(), pending_forwards));
5969+
htlc_forwards = Some((short_channel_id, channel.context.get_funding_txo().unwrap(),
5970+
channel.context.channel_id(), channel.context.get_user_id(), pending_forwards));
5971+
}
5972+
let mut decode_update_add_htlcs = None;
5973+
if !pending_update_adds.is_empty() {
5974+
decode_update_add_htlcs = Some((short_channel_id, pending_update_adds));
59505975
}
59515976

59525977
if let Some(msg) = channel_ready {
@@ -5997,7 +6022,7 @@ where
59976022
emit_channel_ready_event!(pending_events, channel);
59986023
}
59996024

6000-
htlc_forwards
6025+
(htlc_forwards, decode_update_add_htlcs)
60016026
}
60026027

60036028
fn channel_monitor_updated(&self, funding_txo: &OutPoint, channel_id: &ChannelId, highest_applied_update_id: u64, counterparty_node_id: Option<&PublicKey>) {
@@ -6971,6 +6996,15 @@ where
69716996
}
69726997
}
69736998

6999+
fn push_decode_update_add_htlcs(&self, mut update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {
7000+
let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
7001+
let scid = update_add_htlcs.0;
7002+
match decode_update_add_htlcs.entry(scid) {
7003+
hash_map::Entry::Occupied(mut e) => { e.get_mut().append(&mut update_add_htlcs.1); },
7004+
hash_map::Entry::Vacant(e) => { e.insert(update_add_htlcs.1); },
7005+
}
7006+
}
7007+
69747008
#[inline]
69757009
fn forward_htlcs(&self, per_source_pending_forwards: &mut [(u64, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)]) {
69767010
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 {
@@ -7307,10 +7341,11 @@ where
73077341
}
73087342
}
73097343
let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
7310-
let htlc_forwards = self.handle_channel_resumption(
7344+
let (htlc_forwards, decode_update_add_htlcs) = self.handle_channel_resumption(
73117345
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.order,
7312-
Vec::new(), None, responses.channel_ready, responses.announcement_sigs);
7346+
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs);
73137347
debug_assert!(htlc_forwards.is_none());
7348+
debug_assert!(decode_update_add_htlcs.is_none());
73147349
if let Some(upd) = channel_update {
73157350
peer_state.pending_msg_events.push(upd);
73167351
}
@@ -10192,6 +10227,12 @@ where
1019210227
}
1019310228
}
1019410229

10230+
let mut decode_update_add_htlcs_opt = None;
10231+
let decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
10232+
if !decode_update_add_htlcs.is_empty() {
10233+
decode_update_add_htlcs_opt = Some(decode_update_add_htlcs);
10234+
}
10235+
1019510236
let per_peer_state = self.per_peer_state.write().unwrap();
1019610237

1019710238
let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
@@ -10343,6 +10384,7 @@ where
1034310384
(10, in_flight_monitor_updates, option),
1034410385
(11, self.probing_cookie_secret, required),
1034510386
(13, htlc_onion_fields, optional_vec),
10387+
(14, decode_update_add_htlcs_opt, option),
1034610388
});
1034710389

1034810390
Ok(())
@@ -10808,6 +10850,7 @@ where
1080810850
let mut monitor_update_blocked_actions_per_peer: Option<Vec<(_, BTreeMap<_, Vec<_>>)>> = Some(Vec::new());
1080910851
let mut events_override = None;
1081010852
let mut in_flight_monitor_updates: Option<HashMap<(PublicKey, OutPoint), Vec<ChannelMonitorUpdate>>> = None;
10853+
let mut decode_update_add_htlcs: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> = None;
1081110854
read_tlv_fields!(reader, {
1081210855
(1, pending_outbound_payments_no_retry, option),
1081310856
(2, pending_intercepted_htlcs, option),
@@ -10821,7 +10864,9 @@ where
1082110864
(10, in_flight_monitor_updates, option),
1082210865
(11, probing_cookie_secret, option),
1082310866
(13, claimable_htlc_onion_fields, optional_vec),
10867+
(14, decode_update_add_htlcs, option),
1082410868
});
10869+
let decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
1082510870
if fake_scid_rand_bytes.is_none() {
1082610871
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
1082710872
}
@@ -11356,6 +11401,7 @@ where
1135611401
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
1135711402

1135811403
forward_htlcs: Mutex::new(forward_htlcs),
11404+
decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs),
1135911405
claimable_payments: Mutex::new(ClaimablePayments { claimable_payments, pending_claiming_payments: pending_claiming_payments.unwrap() }),
1136011406
outbound_scid_aliases: Mutex::new(outbound_scid_aliases),
1136111407
outpoint_to_peer: Mutex::new(outpoint_to_peer),

0 commit comments

Comments
 (0)