Skip to content

Commit 3e14896

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 6b5df57 commit 3e14896

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lightning/src/ln/onion_utils.rs

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

990990
// Handle packed channel/node updates for passing back for the route handler
991-
for (route_hop_idx, (route_hop_option, shared_secret)) in onion_keys.into_iter().enumerate() {
991+
let mut iterator = onion_keys.into_iter().peekable();
992+
while let Some((route_hop_option, shared_secret)) = iterator.next() {
992993
let route_hop = match route_hop_option.as_ref() {
993994
Some(hop) => hop,
994995
None => {
@@ -1008,13 +1009,16 @@ where
10081009
// The failing hop includes either the inbound channel to the recipient or the outbound channel
10091010
// from the current hop (i.e., the next hop's inbound channel).
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+
// In our case that means that if we're on the last iteration, and there is no more than one
1013+
// blinded hop, the current iteration references the last non-blinded hop.
1014+
let next_hop = iterator.peek();
1015+
is_from_final_node = next_hop.is_none() && num_blinded_hops <= 1;
10121016
let failing_route_hop = if is_from_final_node {
10131017
route_hop
10141018
} else {
1015-
match path.hops.get(route_hop_idx + 1) {
1016-
Some(hop) => hop,
1017-
None => {
1019+
match next_hop {
1020+
Some((Some(hop), _)) => hop,
1021+
_ => {
10181022
// The failing hop is within a multi-hop blinded path.
10191023
#[cfg(not(test))]
10201024
{

0 commit comments

Comments
 (0)