Skip to content

Commit 90474d2

Browse files
committed
Minor refactor for sort / add, and some nits.
- `sort_by_key` to `sort_unstable_by_key` - `checked_add() .. max_value()` to `saturating_add()` - Some typos and nits
1 parent d501a20 commit 90474d2

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

lightning/src/routing/router.rs

+18-24
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,9 @@ struct RouteGraphNode {
332332
impl cmp::Ord for RouteGraphNode {
333333
fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
334334
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);
337336
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);
340338
other_score.cmp(&self_score).then_with(|| other.node_id.cmp(&self.node_id))
341339
}
342340
}
@@ -840,7 +838,7 @@ where L::Target: Logger {
840838
.entry(short_channel_id)
841839
.or_insert_with(|| $candidate.effective_capacity().as_msat());
842840

843-
// It is tricky to substract $next_hops_fee_msat from available liquidity here.
841+
// It is tricky to subtract $next_hops_fee_msat from available liquidity here.
844842
// It may be misleading because we might later choose to reduce the value transferred
845843
// over these channels, and the channel which was insufficient might become sufficient.
846844
// Worst case: we drop a good channel here because it can't cover the high following
@@ -880,8 +878,7 @@ where L::Target: Logger {
880878
.checked_sub(2*MEDIAN_HOP_CLTV_EXPIRY_DELTA)
881879
.unwrap_or(payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta);
882880
let hop_total_cltv_delta = ($next_hops_cltv_delta as u32)
883-
.checked_add($candidate.cltv_expiry_delta())
884-
.unwrap_or(u32::max_value());
881+
.saturating_add($candidate.cltv_expiry_delta());
885882
let doesnt_exceed_cltv_delta_limit = hop_total_cltv_delta <= max_total_cltv_expiry_delta;
886883

887884
let value_contribution_msat = cmp::min(available_value_contribution_msat, $next_hops_value_contribution);
@@ -988,9 +985,9 @@ where L::Target: Logger {
988985
}
989986
}
990987

991-
let path_penalty_msat = $next_hops_path_penalty_msat.checked_add(
992-
scorer.channel_penalty_msat(short_channel_id, amount_to_transfer_over_msat, *available_liquidity_msat,
993-
&$src_node_id, &$dest_node_id)).unwrap_or_else(|| u64::max_value());
988+
let path_penalty_msat = $next_hops_path_penalty_msat.saturating_add(
989+
scorer.channel_penalty_msat(short_channel_id, amount_to_transfer_over_msat,
990+
*available_liquidity_msat, &$src_node_id, &$dest_node_id));
994991
let new_graph_node = RouteGraphNode {
995992
node_id: $src_node_id,
996993
lowest_fee_to_peer_through_node: total_fee_msat,
@@ -1018,11 +1015,9 @@ where L::Target: Logger {
10181015
// the fees included in $next_hops_path_htlc_minimum_msat, but also
10191016
// can't use something that may decrease on future hops.
10201017
let old_cost = cmp::max(old_entry.total_fee_msat, old_entry.path_htlc_minimum_msat)
1021-
.checked_add(old_entry.path_penalty_msat)
1022-
.unwrap_or_else(|| u64::max_value());
1018+
.saturating_add(old_entry.path_penalty_msat);
10231019
let new_cost = cmp::max(total_fee_msat, path_htlc_minimum_msat)
1024-
.checked_add(path_penalty_msat)
1025-
.unwrap_or_else(|| u64::max_value());
1020+
.saturating_add(path_penalty_msat);
10261021

10271022
if !old_entry.was_processed && new_cost < old_cost {
10281023
targets.push(new_graph_node);
@@ -1206,12 +1201,10 @@ where L::Target: Logger {
12061201
.unwrap_or_else(|| CandidateRouteHop::PrivateHop { hint: hop });
12071202
let capacity_msat = candidate.effective_capacity().as_msat();
12081203
aggregate_next_hops_path_penalty_msat = aggregate_next_hops_path_penalty_msat
1209-
.checked_add(scorer.channel_penalty_msat(hop.short_channel_id, final_value_msat, capacity_msat, &source, &target))
1210-
.unwrap_or_else(|| u64::max_value());
1204+
.saturating_add(scorer.channel_penalty_msat(hop.short_channel_id, final_value_msat, capacity_msat, &source, &target));
12111205

12121206
aggregate_next_hops_cltv_delta = aggregate_next_hops_cltv_delta
1213-
.checked_add(hop.cltv_expiry_delta as u32)
1214-
.unwrap_or_else(|| u32::max_value());
1207+
.saturating_add(hop.cltv_expiry_delta as u32);
12151208

12161209
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) {
12171210
// If this hop was not used then there is no use checking the preceding hops
@@ -1449,7 +1442,7 @@ where L::Target: Logger {
14491442
}
14501443

14511444
// Sort by total fees and take the best paths.
1452-
payment_paths.sort_by_key(|path| path.get_total_fee_paid_msat());
1445+
payment_paths.sort_unstable_by_key(|path| path.get_total_fee_paid_msat());
14531446
if payment_paths.len() > 50 {
14541447
payment_paths.truncate(50);
14551448
}
@@ -1517,13 +1510,14 @@ where L::Target: Logger {
15171510
assert!(cur_route.len() > 0);
15181511

15191512
// Step (8).
1520-
// Now, substract the overpaid value from the most-expensive path.
1513+
// Now, subtract the overpaid value from the most-expensive path.
15211514
// TODO: this could also be optimized by also sorting by feerate_per_sat_routed,
15221515
// so that the sender pays less fees overall. And also htlc_minimum_msat.
1523-
cur_route.sort_by_key(|path| { path.hops.iter().map(|hop| hop.0.candidate.fees().proportional_millionths as u64).sum::<u64>() });
1516+
cur_route.sort_unstable_by_key(|path| { path.hops.iter().map(|hop| hop.0.candidate.fees().proportional_millionths as u64).sum::<u64>() });
15241517
let expensive_payment_path = cur_route.first_mut().unwrap();
1525-
// We already dropped all the small channels above, meaning all the
1526-
// remaining channels are larger than remaining overpaid_value_msat.
1518+
1519+
// We already dropped all the small value paths above, meaning all the
1520+
// remaining paths are larger than remaining overpaid_value_msat.
15271521
// Thus, this can't be negative.
15281522
let expensive_path_new_value_msat = expensive_payment_path.get_value_msat() - overpaid_value_msat;
15291523
expensive_payment_path.update_value_and_recompute_fees(expensive_path_new_value_msat);
@@ -1535,7 +1529,7 @@ where L::Target: Logger {
15351529

15361530
// Step (9).
15371531
// Select the best route by lowest total fee.
1538-
drawn_routes.sort_by_key(|paths| paths.iter().map(|path| path.get_total_fee_paid_msat()).sum::<u64>());
1532+
drawn_routes.sort_unstable_by_key(|paths| paths.iter().map(|path| path.get_total_fee_paid_msat()).sum::<u64>());
15391533
let mut selected_paths = Vec::<Vec<Result<RouteHop, LightningError>>>::new();
15401534
for payment_path in drawn_routes.first().unwrap() {
15411535
let mut path = payment_path.hops.iter().map(|(payment_hop, node_features)| {

0 commit comments

Comments
 (0)