Skip to content

Commit c77c1ed

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 7f22871 commit c77c1ed

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

lightning/src/ln/onion_utils.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -1025,39 +1025,34 @@ where
10251025
}
10261026
}
10271027

1028-
let (num_blinded_hops, num_trampoline_hops) =
1029-
path.blinded_tail.as_ref().map_or((0, 0), |bt| (bt.hops.len(), bt.trampoline_hops.len()));
1030-
1031-
// We are first collecting all the unblinded `RouteHop`s inside `onion_keys`. Then, if applicable,
1032-
// we will add all the `TrampolineHop`s, and finally, the blinded hops.
1033-
let mut onion_keys =
1034-
Vec::with_capacity(path.hops.len() + num_trampoline_hops + num_blinded_hops);
1028+
let num_blinded_hops = path.blinded_tail.as_ref().map_or(0, |bt| bt.hops.len());
10351029

10361030
// if we have Trampoline hops, the blinded hops are part of the inner Trampoline onion
10371031
let nontrampoline_bp =
10381032
if path.has_trampoline_hops() { None } else { path.blinded_tail.as_ref() };
10391033
let nontrampoline_hops =
1040-
construct_onion_keys_generic(secp_ctx, &path.hops, nontrampoline_bp, outer_session_priv);
1041-
for (shared_secret, _, _, route_hop_option, _) in nontrampoline_hops {
1042-
onion_keys.push((route_hop_option.map(|rh| ErrorHop::RouteHop(rh)), shared_secret));
1043-
}
1034+
construct_onion_keys_generic(secp_ctx, &path.hops, nontrampoline_bp, outer_session_priv)
1035+
.map(|(shared_secret, _, _, route_hop_option, _)| {
1036+
(route_hop_option.map(|rh| ErrorHop::RouteHop(rh)), shared_secret)
1037+
});
10441038

1045-
if path.has_trampoline_hops() {
1039+
let trampoline_hops = if path.has_trampoline_hops() {
10461040
// Trampoline hops are part of the blinded tail, so this can never panic
10471041
let blinded_tail = path.blinded_tail.as_ref();
10481042
let hops = &blinded_tail.unwrap().trampoline_hops;
10491043
let inner_session_priv =
10501044
inner_session_priv.expect("Trampoline hops always have an inner session priv");
1051-
let trampoline_hops =
1052-
construct_onion_keys_generic(secp_ctx, hops, blinded_tail, inner_session_priv);
1053-
for (shared_secret, _, _, trampoline_hop_option, _) in trampoline_hops {
1054-
onion_keys
1055-
.push((trampoline_hop_option.map(|th| ErrorHop::TrampolineHop(th)), shared_secret));
1056-
}
1057-
}
1045+
Some(construct_onion_keys_generic(secp_ctx, hops, blinded_tail, inner_session_priv).map(
1046+
|(shared_secret, _, _, route_hop_option, _)| {
1047+
(route_hop_option.map(|th| ErrorHop::TrampolineHop(th)), shared_secret)
1048+
},
1049+
))
1050+
} else {
1051+
None
1052+
};
10581053

10591054
// Handle packed channel/node updates for passing back for the route handler
1060-
let mut iterator = onion_keys.into_iter().peekable();
1055+
let mut iterator = nontrampoline_hops.chain(trampoline_hops.into_iter().flatten()).peekable();
10611056
while let Some((route_hop_option, shared_secret)) = iterator.next() {
10621057
let route_hop = match route_hop_option.as_ref() {
10631058
Some(hop) => hop,

0 commit comments

Comments
 (0)