@@ -1095,44 +1095,39 @@ 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 ( ) } ;
1109- 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- }
1103+ let nontrampolines =
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 trampolines = 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 ( ) ;
1135- while let Some ( ( route_hop_idx, ( route_hop_option, shared_secret) ) ) = iterator . next ( ) {
1129+ let mut iter = nontrampolines . chain ( trampolines . into_iter ( ) . flatten ( ) ) . enumerate ( ) . peekable ( ) ;
1130+ while let Some ( ( route_hop_idx, ( route_hop_option, shared_secret) ) ) = iter . next ( ) {
11361131 let route_hop = match route_hop_option. as_ref ( ) {
11371132 Some ( hop) => hop,
11381133 None => {
@@ -1154,7 +1149,7 @@ where
11541149 // For 1-hop blinded paths, the final `ErrorHop` entry is the recipient.
11551150 // In our case that means that if we're on the last iteration, and there is no more than one
11561151 // blinded hop, the current iteration references the last non-blinded hop.
1157- let next_hop = iterator . peek ( ) ;
1152+ let next_hop = iter . peek ( ) ;
11581153 is_from_final_non_blinded_node = next_hop. is_none ( ) && num_blinded_hops <= 1 ;
11591154 let failing_route_hop = if is_from_final_non_blinded_node {
11601155 route_hop
0 commit comments