Skip to content

Commit 113f9cb

Browse files
committed
Pass constructed PendingAddHTLCInfo to chanman forward_htlcs
We jump through some hoops in order to pass a small list of objects to `forward_htlcs` on a per-channel basis rather than per-HTLC. Then, `forward_htlcs` builds a `PendingAddHTLCInfo` for each HTLC for insertion. Worse, in some `forward_htlcs` callsites we're actually starting with a `PendingAddHTLCInfo`, converting it to a tuple, then back inside `forward_htlcs`. Instead, here we just pass a list of built `PendingAddHTLCInfo`s to `forward_htlcs`, cleaning up a good bit of code and even avoiding an allocation of the HTLCs vec in many cases.
1 parent 3497b59 commit 113f9cb

1 file changed

Lines changed: 57 additions & 96 deletions

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 57 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ pub(super) enum PendingHTLCStatus {
449449
pub(super) struct PendingAddHTLCInfo {
450450
pub(super) forward_info: PendingHTLCInfo,
451451

452-
// These fields are produced in `forward_htlcs()` and consumed in
452+
// These fields are set before calling `forward_htlcs()` and consumed in
453453
// `process_pending_htlc_forwards()` for constructing the
454454
// `HTLCSource::PreviousHopData` for failed and forwarded
455455
// HTLCs.
@@ -766,10 +766,6 @@ impl_writeable_tlv_based_enum!(SentHTLCId,
766766
},
767767
);
768768

769-
// (src_outbound_scid_alias, src_counterparty_node_id, src_funding_outpoint, src_chan_id, src_user_chan_id)
770-
type PerSourcePendingForward =
771-
(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>);
772-
773769
type FailedHTLCForward = (HTLCSource, PaymentHash, HTLCFailReason, HTLCHandlingFailureType);
774770

775771
mod fuzzy_channelmanager {
@@ -1421,7 +1417,7 @@ enum PostMonitorUpdateChanResume {
14211417
user_channel_id: u128,
14221418
unbroadcasted_batch_funding_txid: Option<Txid>,
14231419
update_actions: Vec<MonitorUpdateCompletionAction>,
1424-
htlc_forwards: Option<PerSourcePendingForward>,
1420+
htlc_forwards: Vec<PendingAddHTLCInfo>,
14251421
decode_update_add_htlcs: Option<(u64, Vec<msgs::UpdateAddHTLC>)>,
14261422
finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
14271423
failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
@@ -6798,15 +6794,16 @@ impl<
67986794
..payment.forward_info
67996795
};
68006796

6801-
let mut per_source_pending_forward = [(
6802-
payment.prev_outbound_scid_alias,
6803-
payment.prev_counterparty_node_id,
6804-
payment.prev_funding_outpoint,
6805-
payment.prev_channel_id,
6806-
payment.prev_user_channel_id,
6807-
vec![(pending_htlc_info, payment.prev_htlc_id)],
6808-
)];
6809-
self.forward_htlcs(&mut per_source_pending_forward);
6797+
let forward = [PendingAddHTLCInfo {
6798+
prev_outbound_scid_alias: payment.prev_outbound_scid_alias,
6799+
prev_htlc_id: payment.prev_htlc_id,
6800+
prev_counterparty_node_id: payment.prev_counterparty_node_id,
6801+
prev_channel_id: payment.prev_channel_id,
6802+
prev_funding_outpoint: payment.prev_funding_outpoint,
6803+
prev_user_channel_id: payment.prev_user_channel_id,
6804+
forward_info: pending_htlc_info,
6805+
}];
6806+
self.forward_htlcs(forward);
68106807
Ok(())
68116808
}
68126809

@@ -7037,7 +7034,7 @@ impl<
70377034
next_packet_details_opt.map(|d| d.next_packet_pubkey),
70387035
) {
70397036
Ok(info) => {
7040-
let to_pending_add = |info| PendingAddHTLCInfo {
7037+
let pending_add = PendingAddHTLCInfo {
70417038
prev_outbound_scid_alias: incoming_scid_alias,
70427039
prev_counterparty_node_id: incoming_counterparty_node_id,
70437040
prev_funding_outpoint: incoming_funding_txo,
@@ -7059,7 +7056,7 @@ impl<
70597056
Some(incoming_channel_id),
70607057
Some(update_add_htlc.payment_hash),
70617058
);
7062-
if info.routing.should_hold_htlc() {
7059+
if pending_add.forward_info.routing.should_hold_htlc() {
70637060
let mut held_htlcs = self.pending_intercepted_htlcs.lock().unwrap();
70647061
let intercept_id = intercept_id();
70657062
match held_htlcs.entry(intercept_id) {
@@ -7068,7 +7065,6 @@ impl<
70687065
logger,
70697066
"Intercepted held HTLC with id {intercept_id}, holding until the recipient is online"
70707067
);
7071-
let pending_add = to_pending_add(info);
70727068
entry.insert(pending_add);
70737069
},
70747070
hash_map::Entry::Occupied(_) => {
@@ -7085,7 +7081,6 @@ impl<
70857081
self.pending_intercepted_htlcs.lock().unwrap();
70867082
match pending_intercepts.entry(intercept_id) {
70877083
hash_map::Entry::Vacant(entry) => {
7088-
let pending_add = to_pending_add(info);
70897084
if let Ok(intercept_ev) =
70907085
create_htlc_intercepted_event(intercept_id, &pending_add)
70917086
{
@@ -7125,7 +7120,7 @@ impl<
71257120
},
71267121
}
71277122
} else {
7128-
htlc_forwards.push((info, update_add_htlc.htlc_id))
7123+
htlc_forwards.push(pending_add);
71297124
}
71307125
},
71317126
Err(inbound_err) => {
@@ -7145,15 +7140,7 @@ impl<
71457140

71467141
// Process all of the forwards and failures for the channel in which the HTLCs were
71477142
// proposed to as a batch.
7148-
let pending_forwards = (
7149-
incoming_scid_alias,
7150-
incoming_counterparty_node_id,
7151-
incoming_funding_txo,
7152-
incoming_channel_id,
7153-
incoming_user_channel_id,
7154-
htlc_forwards,
7155-
);
7156-
self.forward_htlcs(&mut [pending_forwards]);
7143+
self.forward_htlcs(htlc_forwards);
71577144
for (htlc_fail, failure_type, failure_reason) in htlc_fails.drain(..) {
71587145
let failure = match htlc_fail {
71597146
HTLCFailureMsg::Relay(fail_htlc) => HTLCForwardInfo::FailHTLC {
@@ -7247,7 +7234,7 @@ impl<
72477234

72487235
let mut new_events = VecDeque::new();
72497236
let mut failed_forwards = Vec::new();
7250-
let mut phantom_receives: Vec<PerSourcePendingForward> = Vec::new();
7237+
let mut phantom_receives: Vec<PendingAddHTLCInfo> = Vec::new();
72517238
let mut forward_htlcs = new_hash_map();
72527239
mem::swap(&mut forward_htlcs, &mut self.forward_htlcs.lock().unwrap());
72537240

@@ -7294,7 +7281,7 @@ impl<
72947281
None,
72957282
);
72967283
}
7297-
self.forward_htlcs(&mut phantom_receives);
7284+
self.forward_htlcs(phantom_receives);
72987285

72997286
if self.check_free_holding_cells() {
73007287
should_persist = NotifyOption::DoPersist;
@@ -7314,7 +7301,7 @@ impl<
73147301
fn forwarding_channel_not_found(
73157302
&self, forward_infos: impl Iterator<Item = HTLCForwardInfo>, short_chan_id: u64,
73167303
forwarding_counterparty: Option<PublicKey>, failed_forwards: &mut Vec<FailedHTLCForward>,
7317-
phantom_receives: &mut Vec<PerSourcePendingForward>,
7304+
phantom_receives: &mut Vec<PendingAddHTLCInfo>,
73187305
) {
73197306
for forward_info in forward_infos {
73207307
match forward_info {
@@ -7436,14 +7423,15 @@ impl<
74367423
current_height,
74377424
);
74387425
match create_res {
7439-
Ok(info) => phantom_receives.push((
7426+
Ok(info) => phantom_receives.push(PendingAddHTLCInfo {
7427+
forward_info: info,
74407428
prev_outbound_scid_alias,
7429+
prev_htlc_id,
74417430
prev_counterparty_node_id,
7442-
prev_funding_outpoint,
74437431
prev_channel_id,
7432+
prev_funding_outpoint,
74447433
prev_user_channel_id,
7445-
vec![(info, prev_htlc_id)],
7446-
)),
7434+
}),
74477435
Err(InboundHTLCErr { reason, err_data, msg }) => {
74487436
failure_handler(
74497437
msg,
@@ -7495,7 +7483,7 @@ impl<
74957483
fn process_forward_htlcs(
74967484
&self, short_chan_id: u64, pending_forwards: &mut Vec<HTLCForwardInfo>,
74977485
failed_forwards: &mut Vec<FailedHTLCForward>,
7498-
phantom_receives: &mut Vec<PerSourcePendingForward>,
7486+
phantom_receives: &mut Vec<PendingAddHTLCInfo>,
74997487
) {
75007488
let mut forwarding_counterparty = None;
75017489

@@ -9572,8 +9560,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
95729560
fn post_monitor_update_unlock(
95739561
&self, channel_id: ChannelId, counterparty_node_id: PublicKey, funding_txo: OutPoint,
95749562
user_channel_id: u128, unbroadcasted_batch_funding_txid: Option<Txid>,
9575-
update_actions: Vec<MonitorUpdateCompletionAction>,
9576-
htlc_forwards: Option<PerSourcePendingForward>,
9563+
update_actions: Vec<MonitorUpdateCompletionAction>, htlc_forwards: Vec<PendingAddHTLCInfo>,
95779564
decode_update_add_htlcs: Option<(u64, Vec<msgs::UpdateAddHTLC>)>,
95789565
finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
95799566
failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
@@ -9634,9 +9621,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
96349621

96359622
self.handle_monitor_update_completion_actions(update_actions);
96369623

9637-
if let Some(forwards) = htlc_forwards {
9638-
self.forward_htlcs(&mut [forwards][..]);
9639-
}
9624+
self.forward_htlcs(htlc_forwards);
96409625
if let Some(decode) = decode_update_add_htlcs {
96419626
self.push_decode_update_add_htlcs(decode);
96429627
}
@@ -10263,7 +10248,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1026310248
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
1026410249
tx_signatures: Option<msgs::TxSignatures>, tx_abort: Option<msgs::TxAbort>,
1026510250
channel_ready_order: ChannelReadyOrder,
10266-
) -> (Option<(u64, PublicKey, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
10251+
) -> (Vec<PendingAddHTLCInfo>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
1026710252
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
1026810253
log_trace!(logger, "Handling channel resumption with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement, {} tx_signatures, {} tx_abort",
1026910254
if raa.is_some() { "an" } else { "no" },
@@ -10279,13 +10264,19 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1027910264
let counterparty_node_id = channel.context.get_counterparty_node_id();
1028010265
let outbound_scid_alias = channel.context.outbound_scid_alias();
1028110266

10282-
let mut htlc_forwards = None;
10267+
let mut htlc_forwards = Vec::new();
1028310268
if !pending_forwards.is_empty() {
10284-
htlc_forwards = Some((
10285-
outbound_scid_alias, channel.context.get_counterparty_node_id(),
10286-
channel.funding.get_funding_txo().unwrap(), channel.context.channel_id(),
10287-
channel.context.get_user_id(), pending_forwards
10288-
));
10269+
htlc_forwards = pending_forwards.into_iter().map(|(forward_info, prev_htlc_id)| {
10270+
PendingAddHTLCInfo {
10271+
forward_info,
10272+
prev_outbound_scid_alias: outbound_scid_alias,
10273+
prev_htlc_id,
10274+
prev_counterparty_node_id: counterparty_node_id,
10275+
prev_channel_id: channel.context.channel_id(),
10276+
prev_funding_outpoint: channel.funding.get_funding_txo().unwrap(),
10277+
prev_user_channel_id: channel.context.get_user_id(),
10278+
}
10279+
}).collect();
1028910280
}
1029010281
let mut decode_update_add_htlcs = None;
1029110282
if !pending_update_adds.is_empty() {
@@ -12130,44 +12121,22 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1213012121
}
1213112122

1213212123
#[inline]
12133-
fn forward_htlcs(&self, per_source_pending_forwards: &mut [PerSourcePendingForward]) {
12134-
for &mut (
12135-
prev_outbound_scid_alias,
12136-
prev_counterparty_node_id,
12137-
prev_funding_outpoint,
12138-
prev_channel_id,
12139-
prev_user_channel_id,
12140-
ref mut pending_forwards,
12141-
) in per_source_pending_forwards
12142-
{
12143-
if !pending_forwards.is_empty() {
12144-
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
12145-
let scid = match forward_info.routing {
12146-
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
12147-
PendingHTLCRouting::TrampolineForward { .. }
12148-
| PendingHTLCRouting::Receive { .. }
12149-
| PendingHTLCRouting::ReceiveKeysend { .. } => 0,
12150-
};
12151-
12152-
let pending_add = PendingAddHTLCInfo {
12153-
prev_outbound_scid_alias,
12154-
prev_counterparty_node_id,
12155-
prev_funding_outpoint,
12156-
prev_channel_id,
12157-
prev_htlc_id,
12158-
prev_user_channel_id,
12159-
forward_info,
12160-
};
12124+
fn forward_htlcs<I: IntoIterator<Item = PendingAddHTLCInfo>>(&self, pending_forwards: I) {
12125+
for htlc in pending_forwards.into_iter() {
12126+
let scid = match htlc.forward_info.routing {
12127+
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
12128+
PendingHTLCRouting::TrampolineForward { .. }
12129+
| PendingHTLCRouting::Receive { .. }
12130+
| PendingHTLCRouting::ReceiveKeysend { .. } => 0,
12131+
};
1216112132

12162-
match self.forward_htlcs.lock().unwrap().entry(scid) {
12163-
hash_map::Entry::Occupied(mut entry) => {
12164-
entry.get_mut().push(HTLCForwardInfo::AddHTLC(pending_add));
12165-
},
12166-
hash_map::Entry::Vacant(entry) => {
12167-
entry.insert(vec![HTLCForwardInfo::AddHTLC(pending_add)]);
12168-
},
12169-
}
12170-
}
12133+
match self.forward_htlcs.lock().unwrap().entry(scid) {
12134+
hash_map::Entry::Occupied(mut entry) => {
12135+
entry.get_mut().push(HTLCForwardInfo::AddHTLC(htlc));
12136+
},
12137+
hash_map::Entry::Vacant(entry) => {
12138+
entry.insert(vec![HTLCForwardInfo::AddHTLC(htlc)]);
12139+
},
1217112140
}
1217212141
}
1217312142
}
@@ -12502,7 +12471,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1250212471
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs,
1250312472
responses.tx_signatures, responses.tx_abort, responses.channel_ready_order,
1250412473
);
12505-
debug_assert!(htlc_forwards.is_none());
12474+
debug_assert!(htlc_forwards.is_empty());
1250612475
debug_assert!(decode_update_add_htlcs.is_none());
1250712476
if let Some(upd) = channel_update {
1250812477
peer_state.pending_msg_events.push(upd);
@@ -16563,15 +16532,7 @@ impl<
1656316532
},
1656416533
}
1656516534
} else {
16566-
let mut per_source_pending_forward = [(
16567-
htlc.prev_outbound_scid_alias,
16568-
htlc.prev_counterparty_node_id,
16569-
htlc.prev_funding_outpoint,
16570-
htlc.prev_channel_id,
16571-
htlc.prev_user_channel_id,
16572-
vec![(htlc.forward_info, htlc.prev_htlc_id)],
16573-
)];
16574-
self.forward_htlcs(&mut per_source_pending_forward);
16535+
self.forward_htlcs([htlc]);
1657516536
}
1657616537
},
1657716538
_ => return,

0 commit comments

Comments
 (0)