Skip to content

Commit 6863ebd

Browse files
committed
Pre-Trampoline optimizations
We don't need to recalculate the blinded hop count on each iteration, and clarify the meaning of `is_from_final_hop` to mean that it refers to all non-blinded hops, including Trampoline.
1 parent 7641788 commit 6863ebd

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ where
967967
let mut htlc_msat = *first_hop_htlc_msat;
968968
let mut _error_code_ret = None;
969969
let mut _error_packet_ret = None;
970-
let mut is_from_final_node = false;
970+
let mut is_from_final_non_blinded_node = false;
971971

972972
const BADONION: u16 = 0x8000;
973973
const PERM: u16 = 0x4000;
@@ -1010,6 +1010,8 @@ where
10101010
)
10111011
.expect("Route we used spontaneously grew invalid keys in the middle of it?");
10121012

1013+
let num_blinded_hops = path.blinded_tail.as_ref().map_or(0, |bt| bt.hops.len());
1014+
10131015
// Handle packed channel/node updates for passing back for the route handler
10141016
let mut iterator = onion_keys.into_iter().peekable();
10151017
while let Some((route_hop_option, shared_secret)) = iterator.next() {
@@ -1031,10 +1033,9 @@ where
10311033

10321034
// The failing hop includes either the inbound channel to the recipient or the outbound channel
10331035
// from the current hop (i.e., the next hop's inbound channel).
1034-
let num_blinded_hops = path.blinded_tail.as_ref().map_or(0, |bt| bt.hops.len());
10351036
// For 1-hop blinded paths, the final `path.hops` entry is the recipient.
1036-
is_from_final_node = iterator.peek().is_none() && num_blinded_hops <= 1;
1037-
let failing_route_hop = if is_from_final_node {
1037+
is_from_final_non_blinded_node = iterator.peek().is_none() && num_blinded_hops <= 1;
1038+
let failing_route_hop = if is_from_final_non_blinded_node {
10381039
route_hop
10391040
} else {
10401041
match iterator.peek() {
@@ -1099,7 +1100,7 @@ where
10991100
res = Some(FailureLearnings {
11001101
network_update,
11011102
short_channel_id,
1102-
payment_failed_permanently: is_from_final_node,
1103+
payment_failed_permanently: is_from_final_non_blinded_node,
11031104
failed_within_blinded_path: false,
11041105
});
11051106
break;
@@ -1121,7 +1122,7 @@ where
11211122
res = Some(FailureLearnings {
11221123
network_update,
11231124
short_channel_id,
1124-
payment_failed_permanently: is_from_final_node,
1125+
payment_failed_permanently: is_from_final_non_blinded_node,
11251126
failed_within_blinded_path: false,
11261127
});
11271128
break;
@@ -1138,7 +1139,7 @@ where
11381139
let payment_failed = match error_code & 0xff {
11391140
15 | 16 | 17 | 18 | 19 | 23 => true,
11401141
_ => false,
1141-
} && is_from_final_node; // PERM bit observed below even if this error is from the intermediate nodes
1142+
} && is_from_final_non_blinded_node; // PERM bit observed below even if this error is from the intermediate nodes
11421143

11431144
let mut network_update = None;
11441145
let mut short_channel_id = None;
@@ -1223,7 +1224,7 @@ where
12231224
res = Some(FailureLearnings {
12241225
network_update,
12251226
short_channel_id,
1226-
payment_failed_permanently: error_code & PERM == PERM && is_from_final_node,
1227+
payment_failed_permanently: error_code & PERM == PERM && is_from_final_non_blinded_node,
12271228
failed_within_blinded_path: false,
12281229
});
12291230

@@ -1282,7 +1283,7 @@ where
12821283
DecodedOnionFailure {
12831284
network_update: None,
12841285
short_channel_id: None,
1285-
payment_failed_permanently: is_from_final_node,
1286+
payment_failed_permanently: is_from_final_non_blinded_node,
12861287
failed_within_blinded_path: false,
12871288
#[cfg(any(test, feature = "_test_utils"))]
12881289
onion_error_code: None,

0 commit comments

Comments
 (0)