Skip to content

Commit f9cdf93

Browse files
committed
Select best route by lowest total cost
Selecting only by fees rather than cost (fees plus penalty) may result in preferring higher cost routes over lower cost ones.
1 parent 30b6873 commit f9cdf93

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lightning/src/routing/router.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,10 @@ impl<'a> PaymentPath<'a> {
511511
return result;
512512
}
513513

514+
fn get_cost_msat(&self) -> u64 {
515+
self.get_total_fee_paid_msat().saturating_add(self.get_path_penalty_msat())
516+
}
517+
514518
// If the amount transferred by the path is updated, the fees should be adjusted. Any other way
515519
// to change fees may result in an inconsistency.
516520
//
@@ -1502,9 +1506,8 @@ where L::Target: Logger {
15021506
// prefer lower cost paths.
15031507
cur_route.sort_unstable_by(|a, b| {
15041508
a.get_value_msat().cmp(&b.get_value_msat())
1505-
// Reverse ordering for fees, so we drop higher-fee paths first
1506-
.then_with(|| b.get_total_fee_paid_msat().saturating_add(b.get_path_penalty_msat())
1507-
.cmp(&a.get_total_fee_paid_msat().saturating_add(a.get_path_penalty_msat())))
1509+
// Reverse ordering for cost, so we drop higher-cost paths first
1510+
.then_with(|| b.get_cost_msat().cmp(&a.get_cost_msat()))
15081511
});
15091512

15101513
// We should make sure that at least 1 path left.
@@ -1548,8 +1551,8 @@ where L::Target: Logger {
15481551
}
15491552

15501553
// Step (9).
1551-
// Select the best route by lowest total fee.
1552-
drawn_routes.sort_unstable_by_key(|paths| paths.iter().map(|path| path.get_total_fee_paid_msat()).sum::<u64>());
1554+
// Select the best route by lowest total cost.
1555+
drawn_routes.sort_unstable_by_key(|paths| paths.iter().map(|path| path.get_cost_msat()).sum::<u64>());
15531556
let mut selected_paths = Vec::<Vec<Result<RouteHop, LightningError>>>::new();
15541557
for payment_path in drawn_routes.first().unwrap() {
15551558
let mut path = payment_path.hops.iter().map(|(payment_hop, node_features)| {

0 commit comments

Comments
 (0)