Skip to content

Commit d26733b

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 7641516 commit d26733b

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

Diff for: lightning/src/ln/onion_utils.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -1095,43 +1095,38 @@ where
10951095
}
10961096
}
10971097

1098-
let (num_blinded_hops, num_trampoline_hops) =
1099-
path.blinded_tail.as_ref().map_or((0, 0), |bt| (bt.hops.len(), bt.trampoline_hops.len()));
1100-
1101-
// We are first collecting all the unblinded `RouteHop`s inside `onion_keys`. Then, if applicable,
1102-
// we will add all the `TrampolineHop`s, and finally, the blinded hops.
1103-
let mut onion_keys =
1104-
Vec::with_capacity(path.hops.len() + num_trampoline_hops + num_blinded_hops);
1098+
let num_blinded_hops = path.blinded_tail.as_ref().map_or(0, |bt| bt.hops.len());
11051099

11061100
// if we have Trampoline hops, the blinded hops are part of the inner Trampoline onion
1107-
let nontrampoline_bp =
1101+
let nontrampoline_bt =
11081102
if path.has_trampoline_hops() { None } else { path.blinded_tail.as_ref() };
11091103
let nontrampoline_hops =
1110-
construct_onion_keys_generic(secp_ctx, &path.hops, nontrampoline_bp, outer_session_priv);
1111-
for (shared_secret, _, _, route_hop_option, _) in nontrampoline_hops {
1112-
onion_keys.push((route_hop_option.map(|rh| ErrorHop::RouteHop(rh)), shared_secret));
1113-
}
1104+
construct_onion_keys_generic(secp_ctx, &path.hops, nontrampoline_bt, outer_session_priv)
1105+
.map(|(shared_secret, _, _, route_hop_option, _)| {
1106+
(route_hop_option.map(|rh| ErrorHop::RouteHop(rh)), shared_secret)
1107+
});
11141108

1115-
if path.has_trampoline_hops() {
1109+
let trampoline_hops = if path.has_trampoline_hops() {
11161110
// Trampoline hops are part of the blinded tail, so this can never panic
11171111
let blinded_tail = path.blinded_tail.as_ref();
11181112
let hops = &blinded_tail.unwrap().trampoline_hops;
11191113
let inner_session_priv =
11201114
inner_session_priv.expect("Trampoline hops always have an inner session priv");
1121-
let trampoline_hops =
1122-
construct_onion_keys_generic(secp_ctx, hops, blinded_tail, inner_session_priv);
1123-
for (shared_secret, _, _, trampoline_hop_option, _) in trampoline_hops {
1124-
onion_keys
1125-
.push((trampoline_hop_option.map(|th| ErrorHop::TrampolineHop(th)), shared_secret));
1126-
}
1127-
}
1115+
Some(construct_onion_keys_generic(secp_ctx, hops, blinded_tail, inner_session_priv).map(
1116+
|(shared_secret, _, _, route_hop_option, _)| {
1117+
(route_hop_option.map(|tram_hop| ErrorHop::TrampolineHop(tram_hop)), shared_secret)
1118+
},
1119+
))
1120+
} else {
1121+
None
1122+
};
11281123

11291124
// In the best case, paths can be up to 27 hops. But attribution data can only be conveyed back to the sender from
11301125
// the first 20 hops. Determine the number of hops to be used for attribution data.
11311126
let attributable_hop_count = usize::min(path.hops.len(), MAX_HOPS);
11321127

11331128
// Handle packed channel/node updates for passing back for the route handler
1134-
let mut iterator = onion_keys.into_iter().enumerate().peekable();
1129+
let mut iterator = nontrampoline_hops.chain(trampoline_hops.into_iter().flatten()).enumerate().peekable();
11351130
while let Some((route_hop_idx, (route_hop_option, shared_secret))) = iterator.next() {
11361131
let route_hop = match route_hop_option.as_ref() {
11371132
Some(hop) => hop,

0 commit comments

Comments
 (0)