Skip to content

Commit 2157773

Browse files
committed
Switch error hop iteration to Peekable
We currently check whether our hop is the last in the path by accessing the hops vector by the next index. However, once we start handling Trampoline hops that will become inadequate. Instead, we switch it to check whether there is a subsequent element in the iterator.
1 parent 700d142 commit 2157773

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,8 @@ where
987987
.expect("Route we used spontaneously grew invalid keys in the middle of it?");
988988

989989
// Handle packed channel/node updates for passing back for the route handler
990-
for (route_hop_idx, (route_hop_option, shared_secret)) in onion_keys.into_iter().enumerate() {
990+
let mut iterator = onion_keys.into_iter().peekable();
991+
while let Some((route_hop_option, shared_secret)) = iterator.next() {
991992
let route_hop = match route_hop_option.as_ref() {
992993
Some(hop) => hop,
993994
None => {
@@ -1008,13 +1009,13 @@ where
10081009
// from the current hop (i.e., the next hop's inbound channel).
10091010
let num_blinded_hops = path.blinded_tail.as_ref().map_or(0, |bt| bt.hops.len());
10101011
// For 1-hop blinded paths, the final `path.hops` entry is the recipient.
1011-
is_from_final_node = route_hop_idx + 1 == path.hops.len() && num_blinded_hops <= 1;
1012+
is_from_final_node = iterator.peek().is_none() && num_blinded_hops <= 1;
10121013
let failing_route_hop = if is_from_final_node {
10131014
route_hop
10141015
} else {
1015-
match path.hops.get(route_hop_idx + 1) {
1016-
Some(hop) => hop,
1017-
None => {
1016+
match iterator.peek() {
1017+
Some((Some(hop), _)) => hop,
1018+
_ => {
10181019
// The failing hop is within a multi-hop blinded path.
10191020
#[cfg(not(test))]
10201021
{

0 commit comments

Comments
 (0)