@@ -332,11 +332,9 @@ struct RouteGraphNode {
332
332
impl cmp:: Ord for RouteGraphNode {
333
333
fn cmp ( & self , other : & RouteGraphNode ) -> cmp:: Ordering {
334
334
let other_score = cmp:: max ( other. lowest_fee_to_peer_through_node , other. path_htlc_minimum_msat )
335
- . checked_add ( other. path_penalty_msat )
336
- . unwrap_or_else ( || u64:: max_value ( ) ) ;
335
+ . saturating_add ( other. path_penalty_msat ) ;
337
336
let self_score = cmp:: max ( self . lowest_fee_to_peer_through_node , self . path_htlc_minimum_msat )
338
- . checked_add ( self . path_penalty_msat )
339
- . unwrap_or_else ( || u64:: max_value ( ) ) ;
337
+ . saturating_add ( self . path_penalty_msat ) ;
340
338
other_score. cmp ( & self_score) . then_with ( || other. node_id . cmp ( & self . node_id ) )
341
339
}
342
340
}
@@ -837,7 +835,7 @@ where L::Target: Logger {
837
835
. entry( short_channel_id)
838
836
. or_insert_with( || $candidate. effective_capacity( ) . as_msat( ) ) ;
839
837
840
- // It is tricky to substract $next_hops_fee_msat from available liquidity here.
838
+ // It is tricky to subtract $next_hops_fee_msat from available liquidity here.
841
839
// It may be misleading because we might later choose to reduce the value transferred
842
840
// over these channels, and the channel which was insufficient might become sufficient.
843
841
// Worst case: we drop a good channel here because it can't cover the high following
@@ -877,8 +875,7 @@ where L::Target: Logger {
877
875
. checked_sub( 2 * MEDIAN_HOP_CLTV_EXPIRY_DELTA )
878
876
. unwrap_or( payment_params. max_total_cltv_expiry_delta - final_cltv_expiry_delta) ;
879
877
let hop_total_cltv_delta = ( $next_hops_cltv_delta as u32 )
880
- . checked_add( $candidate. cltv_expiry_delta( ) )
881
- . unwrap_or( u32 :: max_value( ) ) ;
878
+ . saturating_add( $candidate. cltv_expiry_delta( ) ) ;
882
879
let doesnt_exceed_cltv_delta_limit = hop_total_cltv_delta <= max_total_cltv_expiry_delta;
883
880
884
881
let value_contribution_msat = cmp:: min( available_value_contribution_msat, $next_hops_value_contribution) ;
@@ -985,9 +982,9 @@ where L::Target: Logger {
985
982
}
986
983
}
987
984
988
- let path_penalty_msat = $next_hops_path_penalty_msat. checked_add (
989
- scorer. channel_penalty_msat( short_channel_id, amount_to_transfer_over_msat, * available_liquidity_msat ,
990
- & $src_node_id, & $dest_node_id) ) . unwrap_or_else ( || u64 :: max_value ( ) ) ;
985
+ let path_penalty_msat = $next_hops_path_penalty_msat. saturating_add (
986
+ scorer. channel_penalty_msat( short_channel_id, amount_to_transfer_over_msat,
987
+ * available_liquidity_msat , & $src_node_id, & $dest_node_id) ) ;
991
988
let new_graph_node = RouteGraphNode {
992
989
node_id: $src_node_id,
993
990
lowest_fee_to_peer_through_node: total_fee_msat,
@@ -1015,11 +1012,9 @@ where L::Target: Logger {
1015
1012
// the fees included in $next_hops_path_htlc_minimum_msat, but also
1016
1013
// can't use something that may decrease on future hops.
1017
1014
let old_cost = cmp:: max( old_entry. total_fee_msat, old_entry. path_htlc_minimum_msat)
1018
- . checked_add( old_entry. path_penalty_msat)
1019
- . unwrap_or_else( || u64 :: max_value( ) ) ;
1015
+ . saturating_add( old_entry. path_penalty_msat) ;
1020
1016
let new_cost = cmp:: max( total_fee_msat, path_htlc_minimum_msat)
1021
- . checked_add( path_penalty_msat)
1022
- . unwrap_or_else( || u64 :: max_value( ) ) ;
1017
+ . saturating_add( path_penalty_msat) ;
1023
1018
1024
1019
if !old_entry. was_processed && new_cost < old_cost {
1025
1020
targets. push( new_graph_node) ;
@@ -1203,12 +1198,10 @@ where L::Target: Logger {
1203
1198
. unwrap_or_else ( || CandidateRouteHop :: PrivateHop { hint : hop } ) ;
1204
1199
let capacity_msat = candidate. effective_capacity ( ) . as_msat ( ) ;
1205
1200
aggregate_next_hops_path_penalty_msat = aggregate_next_hops_path_penalty_msat
1206
- . checked_add ( scorer. channel_penalty_msat ( hop. short_channel_id , final_value_msat, capacity_msat, & source, & target) )
1207
- . unwrap_or_else ( || u64:: max_value ( ) ) ;
1201
+ . saturating_add ( scorer. channel_penalty_msat ( hop. short_channel_id , final_value_msat, capacity_msat, & source, & target) ) ;
1208
1202
1209
1203
aggregate_next_hops_cltv_delta = aggregate_next_hops_cltv_delta
1210
- . checked_add ( hop. cltv_expiry_delta as u32 )
1211
- . unwrap_or_else ( || u32:: max_value ( ) ) ;
1204
+ . saturating_add ( hop. cltv_expiry_delta as u32 ) ;
1212
1205
1213
1206
if !add_entry ! ( candidate, source, target, aggregate_next_hops_fee_msat, path_value_msat, aggregate_next_hops_path_htlc_minimum_msat, aggregate_next_hops_path_penalty_msat, aggregate_next_hops_cltv_delta) {
1214
1207
// If this hop was not used then there is no use checking the preceding hops
@@ -1446,7 +1439,7 @@ where L::Target: Logger {
1446
1439
}
1447
1440
1448
1441
// Sort by total fees and take the best paths.
1449
- payment_paths. sort_by_key ( |path| path. get_total_fee_paid_msat ( ) ) ;
1442
+ payment_paths. sort_unstable_by_key ( |path| path. get_total_fee_paid_msat ( ) ) ;
1450
1443
if payment_paths. len ( ) > 50 {
1451
1444
payment_paths. truncate ( 50 ) ;
1452
1445
}
@@ -1514,13 +1507,14 @@ where L::Target: Logger {
1514
1507
assert ! ( cur_route. len( ) > 0 ) ;
1515
1508
1516
1509
// Step (8).
1517
- // Now, substract the overpaid value from the most-expensive path.
1510
+ // Now, subtract the overpaid value from the most-expensive path.
1518
1511
// TODO: this could also be optimized by also sorting by feerate_per_sat_routed,
1519
1512
// so that the sender pays less fees overall. And also htlc_minimum_msat.
1520
- cur_route. sort_by_key ( |path| { path. hops . iter ( ) . map ( |hop| hop. 0 . candidate . fees ( ) . proportional_millionths as u64 ) . sum :: < u64 > ( ) } ) ;
1513
+ cur_route. sort_unstable_by_key ( |path| { path. hops . iter ( ) . map ( |hop| hop. 0 . candidate . fees ( ) . proportional_millionths as u64 ) . sum :: < u64 > ( ) } ) ;
1521
1514
let expensive_payment_path = cur_route. first_mut ( ) . unwrap ( ) ;
1522
- // We already dropped all the small channels above, meaning all the
1523
- // remaining channels are larger than remaining overpaid_value_msat.
1515
+
1516
+ // We already dropped all the small value paths above, meaning all the
1517
+ // remaining paths are larger than remaining overpaid_value_msat.
1524
1518
// Thus, this can't be negative.
1525
1519
let expensive_path_new_value_msat = expensive_payment_path. get_value_msat ( ) - overpaid_value_msat;
1526
1520
expensive_payment_path. update_value_and_recompute_fees ( expensive_path_new_value_msat) ;
@@ -1532,7 +1526,7 @@ where L::Target: Logger {
1532
1526
1533
1527
// Step (9).
1534
1528
// Select the best route by lowest total fee.
1535
- drawn_routes. sort_by_key ( |paths| paths. iter ( ) . map ( |path| path. get_total_fee_paid_msat ( ) ) . sum :: < u64 > ( ) ) ;
1529
+ drawn_routes. sort_unstable_by_key ( |paths| paths. iter ( ) . map ( |path| path. get_total_fee_paid_msat ( ) ) . sum :: < u64 > ( ) ) ;
1536
1530
let mut selected_paths = Vec :: < Vec < Result < RouteHop , LightningError > > > :: new ( ) ;
1537
1531
for payment_path in drawn_routes. first ( ) . unwrap ( ) {
1538
1532
let mut path = payment_path. hops . iter ( ) . map ( |( payment_hop, node_features) | {
0 commit comments