@@ -1824,6 +1824,28 @@ impl<'a> core::fmt::Debug for PathBuildingHop<'a> {
1824
1824
}
1825
1825
}
1826
1826
1827
+
1828
+ #[ derive( Default ) ]
1829
+ struct NextHopsData {
1830
+ fee_msat : u64 ,
1831
+ value_contribution : u64 ,
1832
+ path_htlc_minimum_msat : u64 ,
1833
+ path_penalty_msat : u64 ,
1834
+ cltv_delta : u32 ,
1835
+ path_length : u8 ,
1836
+ }
1837
+
1838
+ impl NextHopsData {
1839
+ /// Return [`NextHopsData`] with all default values (zeros), except a
1840
+ /// `value_contribution`.
1841
+ fn with_value_contribution ( contribution : u64 ) -> Self {
1842
+ Self {
1843
+ value_contribution : contribution,
1844
+ ..Default :: default ( )
1845
+ }
1846
+ }
1847
+ }
1848
+
1827
1849
// Instantiated with a list of hops with correct data in them collected during path finding,
1828
1850
// an instance of this struct should be further modified only via given methods.
1829
1851
#[ derive( Clone ) ]
@@ -2395,9 +2417,7 @@ where L::Target: Logger {
2395
2417
// $next_hops_fee_msat represents the fees paid for using all the channels *after* this one,
2396
2418
// since that value has to be transferred over this channel.
2397
2419
// Returns the contribution amount of $candidate if the channel caused an update to `targets`.
2398
- ( $candidate: expr, $next_hops_fee_msat: expr,
2399
- $next_hops_value_contribution: expr, $next_hops_path_htlc_minimum_msat: expr,
2400
- $next_hops_path_penalty_msat: expr, $next_hops_cltv_delta: expr, $next_hops_path_length: expr ) => { {
2420
+ ( $candidate: expr, $next_hops_data: expr ) => { {
2401
2421
add_entry_internal(
2402
2422
& params,
2403
2423
channel_saturation_pow_half,
@@ -2411,12 +2431,7 @@ where L::Target: Logger {
2411
2431
scorer,
2412
2432
score_params,
2413
2433
$candidate,
2414
- $next_hops_fee_msat,
2415
- $next_hops_value_contribution,
2416
- $next_hops_path_htlc_minimum_msat,
2417
- $next_hops_path_penalty_msat,
2418
- $next_hops_cltv_delta,
2419
- $next_hops_path_length,
2434
+ $next_hops_data,
2420
2435
)
2421
2436
} }
2422
2437
}
@@ -2461,10 +2476,13 @@ where L::Target: Logger {
2461
2476
details, payer_node_id: & our_node_id, payer_node_counter,
2462
2477
target_node_counter: $node. node_counter,
2463
2478
} ) ;
2464
- add_entry!( & candidate, fee_to_target_msat,
2465
- $next_hops_value_contribution,
2466
- next_hops_path_htlc_minimum_msat, next_hops_path_penalty_msat,
2467
- $next_hops_cltv_delta, $next_hops_path_length) ;
2479
+ add_entry!( & candidate, & NextHopsData {
2480
+ fee_msat: fee_to_target_msat,
2481
+ value_contribution: $next_hops_value_contribution,
2482
+ path_htlc_minimum_msat: next_hops_path_htlc_minimum_msat,
2483
+ path_penalty_msat: next_hops_path_penalty_msat,
2484
+ cltv_delta: $next_hops_cltv_delta,
2485
+ path_length: $next_hops_path_length } ) ;
2468
2486
}
2469
2487
}
2470
2488
@@ -2485,12 +2503,13 @@ where L::Target: Logger {
2485
2503
info: directed_channel,
2486
2504
short_channel_id: * chan_id,
2487
2505
} ) ;
2488
- add_entry!( & candidate,
2489
- fee_to_target_msat,
2490
- $next_hops_value_contribution,
2491
- next_hops_path_htlc_minimum_msat,
2492
- next_hops_path_penalty_msat,
2493
- $next_hops_cltv_delta, $next_hops_path_length) ;
2506
+ add_entry!( & candidate, & NextHopsData {
2507
+ fee_msat: fee_to_target_msat,
2508
+ value_contribution: $next_hops_value_contribution,
2509
+ path_htlc_minimum_msat: next_hops_path_htlc_minimum_msat,
2510
+ path_penalty_msat: next_hops_path_penalty_msat,
2511
+ cltv_delta: $next_hops_cltv_delta,
2512
+ path_length: $next_hops_path_length } ) ;
2494
2513
}
2495
2514
}
2496
2515
}
@@ -2522,8 +2541,8 @@ where L::Target: Logger {
2522
2541
details, payer_node_id : & our_node_id, payer_node_counter,
2523
2542
target_node_counter : payee_node_counter,
2524
2543
} ) ;
2525
- let added = add_entry ! ( & candidate, 0 , path_value_msat ,
2526
- 0 , 0u64 , 0 , 0 ) . is_some ( ) ;
2544
+ let added = add_entry ! ( & candidate,
2545
+ & NextHopsData :: with_value_contribution ( path_value_msat ) ) . is_some ( ) ;
2527
2546
log_trace ! ( logger, "{} direct route to payee via {}" ,
2528
2547
if added { "Added" } else { "Skipped" } , LoggedCandidateHop ( & candidate) ) ;
2529
2548
}
@@ -2566,8 +2585,7 @@ where L::Target: Logger {
2566
2585
CandidateRouteHop :: Blinded ( BlindedPathCandidate { source_node_counter, source_node_id, hint, hint_idx } )
2567
2586
} ;
2568
2587
let mut path_contribution_msat = path_value_msat;
2569
- if let Some ( hop_used_msat) = add_entry ! ( & candidate,
2570
- 0 , path_contribution_msat, 0 , 0_u64 , 0 , 0 )
2588
+ if let Some ( hop_used_msat) = add_entry ! ( & candidate, & NextHopsData :: with_value_contribution( path_contribution_msat) )
2571
2589
{
2572
2590
path_contribution_msat = hop_used_msat;
2573
2591
} else { continue }
@@ -2586,8 +2604,13 @@ where L::Target: Logger {
2586
2604
} ;
2587
2605
let path_min = candidate. htlc_minimum_msat ( ) . saturating_add (
2588
2606
compute_fees_saturating ( candidate. htlc_minimum_msat ( ) , candidate. fees ( ) ) ) ;
2589
- add_entry ! ( & first_hop_candidate, blinded_path_fee, path_contribution_msat, path_min,
2590
- 0_u64 , candidate. cltv_expiry_delta( ) , 0 ) ;
2607
+ add_entry ! ( & first_hop_candidate, & NextHopsData {
2608
+ fee_msat: blinded_path_fee,
2609
+ value_contribution: path_contribution_msat,
2610
+ path_htlc_minimum_msat: path_min,
2611
+ path_penalty_msat: 0 ,
2612
+ cltv_delta: candidate. cltv_expiry_delta( ) ,
2613
+ path_length: 0 } ) ;
2591
2614
}
2592
2615
}
2593
2616
}
@@ -2609,12 +2632,7 @@ where L::Target: Logger {
2609
2632
let prev_hop_iter = core:: iter:: once ( & maybe_dummy_payee_pk) . chain (
2610
2633
route. 0 . iter ( ) . skip ( 1 ) . rev ( ) . map ( |hop| & hop. src_node_id ) ) ;
2611
2634
let mut hop_used = true ;
2612
- let mut aggregate_next_hops_fee_msat: u64 = 0 ;
2613
- let mut aggregate_next_hops_path_htlc_minimum_msat: u64 = 0 ;
2614
- let mut aggregate_next_hops_path_penalty_msat: u64 = 0 ;
2615
- let mut aggregate_next_hops_cltv_delta: u32 = 0 ;
2616
- let mut aggregate_next_hops_path_length: u8 = 0 ;
2617
- let mut aggregate_path_contribution_msat = path_value_msat;
2635
+ let mut aggregate_next_hops = NextHopsData :: with_value_contribution ( path_value_msat) ;
2618
2636
2619
2637
for ( idx, ( hop, prev_hop_id) ) in hop_iter. zip ( prev_hop_iter) . enumerate ( ) {
2620
2638
let ( target, private_target_node_counter) =
@@ -2645,12 +2663,8 @@ where L::Target: Logger {
2645
2663
target_node_counter : * private_target_node_counter,
2646
2664
} ) ) ;
2647
2665
2648
- if let Some ( hop_used_msat) = add_entry ! ( & candidate,
2649
- aggregate_next_hops_fee_msat, aggregate_path_contribution_msat,
2650
- aggregate_next_hops_path_htlc_minimum_msat, aggregate_next_hops_path_penalty_msat,
2651
- aggregate_next_hops_cltv_delta, aggregate_next_hops_path_length)
2652
- {
2653
- aggregate_path_contribution_msat = hop_used_msat;
2666
+ if let Some ( hop_used_msat) = add_entry ! ( & candidate, & aggregate_next_hops) {
2667
+ aggregate_next_hops. value_contribution = hop_used_msat;
2654
2668
} else {
2655
2669
// If this hop was not used then there is no use checking the preceding
2656
2670
// hops in the RouteHint. We can break by just searching for a direct
@@ -2662,20 +2676,20 @@ where L::Target: Logger {
2662
2676
. get ( & candidate. id ( ) ) . copied ( )
2663
2677
. unwrap_or ( 0 ) ;
2664
2678
let channel_usage = ChannelUsage {
2665
- amount_msat : final_value_msat + aggregate_next_hops_fee_msat ,
2679
+ amount_msat : final_value_msat + aggregate_next_hops . fee_msat ,
2666
2680
inflight_htlc_msat : used_liquidity_msat,
2667
2681
effective_capacity : candidate. effective_capacity ( ) ,
2668
2682
} ;
2669
2683
let channel_penalty_msat = scorer. channel_penalty_msat (
2670
2684
& candidate, channel_usage, score_params
2671
2685
) ;
2672
- aggregate_next_hops_path_penalty_msat = aggregate_next_hops_path_penalty_msat
2686
+ aggregate_next_hops . path_penalty_msat = aggregate_next_hops . path_penalty_msat
2673
2687
. saturating_add ( channel_penalty_msat) ;
2674
2688
2675
- aggregate_next_hops_cltv_delta = aggregate_next_hops_cltv_delta
2689
+ aggregate_next_hops . cltv_delta = aggregate_next_hops . cltv_delta
2676
2690
. saturating_add ( hop. cltv_expiry_delta as u32 ) ;
2677
2691
2678
- aggregate_next_hops_path_length = aggregate_next_hops_path_length
2692
+ aggregate_next_hops . path_length = aggregate_next_hops . path_length
2679
2693
. saturating_add ( 1 ) ;
2680
2694
2681
2695
// Searching for a direct channel between last checked hop and first_hop_targets
@@ -2688,10 +2702,7 @@ where L::Target: Logger {
2688
2702
details, payer_node_id : & our_node_id, payer_node_counter,
2689
2703
target_node_counter : * peer_node_counter,
2690
2704
} ) ;
2691
- add_entry ! ( & first_hop_candidate,
2692
- aggregate_next_hops_fee_msat, aggregate_path_contribution_msat,
2693
- aggregate_next_hops_path_htlc_minimum_msat, aggregate_next_hops_path_penalty_msat,
2694
- aggregate_next_hops_cltv_delta, aggregate_next_hops_path_length) ;
2705
+ add_entry ! ( & first_hop_candidate, & aggregate_next_hops) ;
2695
2706
}
2696
2707
}
2697
2708
@@ -2704,15 +2715,15 @@ where L::Target: Logger {
2704
2715
// for the last node in the RouteHint. We need to just add the fees to
2705
2716
// route through the current node so that the preceding node (next iteration)
2706
2717
// can use it.
2707
- let hops_fee = compute_fees ( aggregate_next_hops_fee_msat + final_value_msat, hop. fees )
2708
- . map_or ( None , |inc| inc. checked_add ( aggregate_next_hops_fee_msat ) ) ;
2709
- aggregate_next_hops_fee_msat = if let Some ( val) = hops_fee { val } else { break ; } ;
2718
+ let hops_fee = compute_fees ( aggregate_next_hops . fee_msat + final_value_msat, hop. fees )
2719
+ . map_or ( None , |inc| inc. checked_add ( aggregate_next_hops . fee_msat ) ) ;
2720
+ aggregate_next_hops . fee_msat = if let Some ( val) = hops_fee { val } else { break ; } ;
2710
2721
2711
2722
// The next channel will need to relay this channel's min_htlc *plus* the fees taken by
2712
2723
// this route hint's source node to forward said min over this channel.
2713
- aggregate_next_hops_path_htlc_minimum_msat = {
2724
+ aggregate_next_hops . path_htlc_minimum_msat = {
2714
2725
let curr_htlc_min = cmp:: max (
2715
- candidate. htlc_minimum_msat ( ) , aggregate_next_hops_path_htlc_minimum_msat
2726
+ candidate. htlc_minimum_msat ( ) , aggregate_next_hops . path_htlc_minimum_msat
2716
2727
) ;
2717
2728
let curr_htlc_min_fee = if let Some ( val) = compute_fees ( curr_htlc_min, hop. fees ) { val } else { break } ;
2718
2729
if let Some ( min) = curr_htlc_min. checked_add ( curr_htlc_min_fee) { min } else { break }
@@ -2737,13 +2748,7 @@ where L::Target: Logger {
2737
2748
details, payer_node_id : & our_node_id, payer_node_counter,
2738
2749
target_node_counter : * peer_node_counter,
2739
2750
} ) ;
2740
- add_entry ! ( & first_hop_candidate,
2741
- aggregate_next_hops_fee_msat,
2742
- aggregate_path_contribution_msat,
2743
- aggregate_next_hops_path_htlc_minimum_msat,
2744
- aggregate_next_hops_path_penalty_msat,
2745
- aggregate_next_hops_cltv_delta,
2746
- aggregate_next_hops_path_length) ;
2751
+ add_entry ! ( & first_hop_candidate, & aggregate_next_hops) ;
2747
2752
}
2748
2753
}
2749
2754
}
@@ -3136,12 +3141,7 @@ fn add_entry_internal<'a, L: Deref, S: ScoreLookUp>(
3136
3141
score_params : & S :: ScoreParams ,
3137
3142
// original add_entry params:
3138
3143
candidate : & CandidateRouteHop < ' a > ,
3139
- next_hops_fee_msat : u64 ,
3140
- next_hops_value_contribution : u64 ,
3141
- next_hops_path_htlc_minimum_msat : u64 ,
3142
- next_hops_path_penalty_msat : u64 ,
3143
- next_hops_cltv_delta : u32 ,
3144
- next_hops_path_length : u8 ,
3144
+ next_hops : & NextHopsData ,
3145
3145
) -> Option < u64 >
3146
3146
where
3147
3147
L :: Target : Logger ,
@@ -3166,7 +3166,7 @@ where
3166
3166
// fees caused by one expensive channel, but then this channel could have been used
3167
3167
// if the amount being transferred over this path is lower.
3168
3168
// We do this for now, but this is a subject for removal.
3169
- if let Some ( mut available_value_contribution_msat) = htlc_maximum_msat. checked_sub ( next_hops_fee_msat ) {
3169
+ if let Some ( mut available_value_contribution_msat) = htlc_maximum_msat. checked_sub ( next_hops . fee_msat ) {
3170
3170
let used_liquidity_msat = used_liquidities
3171
3171
. get ( & candidate. id ( ) )
3172
3172
. map_or ( 0 , |used_liquidity_msat| {
@@ -3178,7 +3178,7 @@ where
3178
3178
// Verify the liquidity offered by this channel complies to the minimal contribution.
3179
3179
let contributes_sufficient_value = available_value_contribution_msat >= params. minimal_value_contribution_msat ;
3180
3180
// Do not consider candidate hops that would exceed the maximum path length.
3181
- let path_length_to_node = next_hops_path_length
3181
+ let path_length_to_node = next_hops . path_length
3182
3182
+ if candidate. blinded_hint_idx ( ) . is_some ( ) { 0 } else { 1 } ;
3183
3183
let exceeds_max_path_length = path_length_to_node > params. max_path_length ;
3184
3184
@@ -3189,27 +3189,27 @@ where
3189
3189
let max_total_cltv_expiry_delta = ( params. payment . max_total_cltv_expiry_delta - params. final_cltv_expiry_delta )
3190
3190
. checked_sub ( 2 * MEDIAN_HOP_CLTV_EXPIRY_DELTA )
3191
3191
. unwrap_or ( params. payment . max_total_cltv_expiry_delta - params. final_cltv_expiry_delta ) ;
3192
- let hop_total_cltv_delta = ( next_hops_cltv_delta as u32 )
3192
+ let hop_total_cltv_delta = ( next_hops . cltv_delta as u32 )
3193
3193
. saturating_add ( candidate. cltv_expiry_delta ( ) ) ;
3194
3194
let exceeds_cltv_delta_limit = hop_total_cltv_delta > max_total_cltv_expiry_delta;
3195
3195
3196
- let value_contribution_msat = cmp:: min ( available_value_contribution_msat, next_hops_value_contribution ) ;
3196
+ let value_contribution_msat = cmp:: min ( available_value_contribution_msat, next_hops . value_contribution ) ;
3197
3197
// Includes paying fees for the use of the following channels.
3198
- let amount_to_transfer_over_msat: u64 = match value_contribution_msat. checked_add ( next_hops_fee_msat ) {
3198
+ let amount_to_transfer_over_msat: u64 = match value_contribution_msat. checked_add ( next_hops . fee_msat ) {
3199
3199
Some ( result) => result,
3200
3200
// Can't overflow due to how the values were computed right above.
3201
3201
None => unreachable ! ( ) ,
3202
3202
} ;
3203
3203
#[ allow( unused_comparisons) ] // next_hops_path_htlc_minimum_msat is 0 in some calls so rustc complains
3204
3204
let over_path_minimum_msat = amount_to_transfer_over_msat >= candidate. htlc_minimum_msat ( ) &&
3205
- amount_to_transfer_over_msat >= next_hops_path_htlc_minimum_msat ;
3205
+ amount_to_transfer_over_msat >= next_hops . path_htlc_minimum_msat ;
3206
3206
3207
3207
#[ allow( unused_comparisons) ] // next_hops_path_htlc_minimum_msat is 0 in some calls so rustc complains
3208
3208
let may_overpay_to_meet_path_minimum_msat =
3209
3209
( amount_to_transfer_over_msat < candidate. htlc_minimum_msat ( ) &&
3210
3210
params. recommended_value_msat >= candidate. htlc_minimum_msat ( ) ) ||
3211
- ( amount_to_transfer_over_msat < next_hops_path_htlc_minimum_msat &&
3212
- params. recommended_value_msat >= next_hops_path_htlc_minimum_msat ) ;
3211
+ ( amount_to_transfer_over_msat < next_hops . path_htlc_minimum_msat &&
3212
+ params. recommended_value_msat >= next_hops . path_htlc_minimum_msat ) ;
3213
3213
3214
3214
let payment_failed_on_this_channel = match scid_opt {
3215
3215
Some ( scid) => params. payment . previously_failed_channels . contains ( & scid) ,
@@ -3277,7 +3277,7 @@ where
3277
3277
// payment path (upstream to the payee). To avoid that, we recompute
3278
3278
// path fees knowing the final path contribution after constructing it.
3279
3279
let curr_min = cmp:: max (
3280
- next_hops_path_htlc_minimum_msat , candidate. htlc_minimum_msat ( )
3280
+ next_hops . path_htlc_minimum_msat , candidate. htlc_minimum_msat ( )
3281
3281
) ;
3282
3282
let path_htlc_minimum_msat = compute_fees_saturating ( curr_min, candidate. fees ( ) )
3283
3283
. saturating_add ( curr_min) ;
@@ -3317,7 +3317,7 @@ where
3317
3317
3318
3318
if should_process {
3319
3319
let mut hop_use_fee_msat = 0 ;
3320
- let mut total_fee_msat: u64 = next_hops_fee_msat ;
3320
+ let mut total_fee_msat: u64 = next_hops . fee_msat ;
3321
3321
3322
3322
// Ignore hop_use_fee_msat for channel-from-us as we assume all channels-from-us
3323
3323
// will have the same effective-fee
@@ -3352,7 +3352,7 @@ where
3352
3352
scorer. channel_penalty_msat ( candidate,
3353
3353
channel_usage,
3354
3354
score_params) ;
3355
- let path_penalty_msat = next_hops_path_penalty_msat
3355
+ let path_penalty_msat = next_hops . path_penalty_msat
3356
3356
. saturating_add ( channel_penalty_msat) ;
3357
3357
3358
3358
// Update the way of reaching candidate.source()
@@ -3386,7 +3386,7 @@ where
3386
3386
path_length_to_node,
3387
3387
} ;
3388
3388
targets. push ( new_graph_node) ;
3389
- old_entry. next_hops_fee_msat = next_hops_fee_msat ;
3389
+ old_entry. next_hops_fee_msat = next_hops . fee_msat ;
3390
3390
old_entry. hop_use_fee_msat = hop_use_fee_msat;
3391
3391
old_entry. total_fee_msat = total_fee_msat;
3392
3392
old_entry. candidate = candidate. clone ( ) ;
0 commit comments