Skip to content

Commit 82b2a09

Browse files
committed
Use iterators directly in process_onion_failure_inner
Now that `create_onion_keys_generic` returns an iterator rather than relying on a callback, making `process_onion_failure_inner` chain the non-trampoline and trampoline iterators is trivial. We do so here, avoiding the extra vec allocation.
1 parent fde4124 commit 82b2a09

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,39 +1041,34 @@ where
10411041
}
10421042
}
10431043

1044-
let (num_blinded_hops, num_trampoline_hops) =
1045-
path.blinded_tail.as_ref().map_or((0, 0), |bt| (bt.hops.len(), bt.trampoline_hops.len()));
1046-
1047-
// We are first collecting all the unblinded `RouteHop`s inside `onion_keys`. Then, if applicable,
1048-
// we will add all the `TrampolineHop`s, and finally, the blinded hops.
1049-
let mut onion_keys =
1050-
Vec::with_capacity(path.hops.len() + num_trampoline_hops + num_blinded_hops);
1044+
let num_blinded_hops = path.blinded_tail.as_ref().map_or(0, |bt| bt.hops.len());
10511045

10521046
// if we have Trampoline hops, the blinded hops are part of the inner Trampoline onion
10531047
let nontrampoline_bp =
10541048
if path.has_trampoline_hops() { None } else { path.blinded_tail.as_ref() };
10551049
let nontrampoline_hops =
1056-
construct_onion_keys_generic(secp_ctx, &path.hops, nontrampoline_bp, outer_session_priv);
1057-
for (shared_secret, _, _, route_hop_option, _) in nontrampoline_hops {
1058-
onion_keys.push((route_hop_option.map(|rh| ErrorHop::RouteHop(rh)), shared_secret));
1059-
}
1050+
construct_onion_keys_generic(secp_ctx, &path.hops, nontrampoline_bp, outer_session_priv)
1051+
.map(|(shared_secret, _, _, route_hop_option, _)| {
1052+
(route_hop_option.map(|rh| ErrorHop::RouteHop(rh)), shared_secret)
1053+
});
10601054

1061-
if path.has_trampoline_hops() {
1055+
let trampoline_hops = if path.has_trampoline_hops() {
10621056
// Trampoline hops are part of the blinded tail, so this can never panic
10631057
let blinded_tail = path.blinded_tail.as_ref();
10641058
let hops = &blinded_tail.unwrap().trampoline_hops;
10651059
let inner_session_priv =
10661060
inner_session_priv.expect("Trampoline hops always have an inner session priv");
1067-
let trampoline_hops =
1068-
construct_onion_keys_generic(secp_ctx, hops, blinded_tail, inner_session_priv);
1069-
for (shared_secret, _, _, trampoline_hop_option, _) in trampoline_hops {
1070-
onion_keys
1071-
.push((trampoline_hop_option.map(|th| ErrorHop::TrampolineHop(th)), shared_secret));
1072-
}
1073-
}
1061+
Some(construct_onion_keys_generic(secp_ctx, hops, blinded_tail, inner_session_priv).map(
1062+
|(shared_secret, _, _, route_hop_option, _)| {
1063+
(route_hop_option.map(|th| ErrorHop::TrampolineHop(th)), shared_secret)
1064+
},
1065+
))
1066+
} else {
1067+
None
1068+
};
10741069

10751070
// Handle packed channel/node updates for passing back for the route handler
1076-
let mut iterator = onion_keys.into_iter().peekable();
1071+
let mut iterator = nontrampoline_hops.chain(trampoline_hops.into_iter().flatten()).peekable();
10771072
while let Some((route_hop_option, shared_secret)) = iterator.next() {
10781073
let route_hop = match route_hop_option.as_ref() {
10791074
Some(hop) => hop,

0 commit comments

Comments
 (0)