Skip to content

Commit 3c60ffa

Browse files
TheBlueMattadi2011
authored andcommitted
Prefer higher-value, shorter equal-cost paths when routing
This likely only impacts very rare edge cases, but if we have two equal-cost paths, we should likely prefer ones which contribute more value (avoiding cases where we use paths which are amount-limited but equal fee to higher-amount paths) and then paths with fewer hops (which may complete faster). It does make test behavior more robust against router changes, which comes in handy over the coming commits.
1 parent af60d48 commit 3c60ffa

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

lightning/src/routing/router.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,10 @@ struct RouteGraphNode {
12111211

12121212
impl cmp::Ord for RouteGraphNode {
12131213
fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
1214-
other.score.cmp(&self.score).then_with(|| other.node_counter.cmp(&self.node_counter))
1214+
other.score.cmp(&self.score)
1215+
.then_with(|| self.value_contribution_msat.cmp(&other.value_contribution_msat))
1216+
.then_with(|| other.path_length_to_node.cmp(&self.path_length_to_node))
1217+
.then_with(|| other.node_counter.cmp(&self.node_counter))
12151218
}
12161219
}
12171220

@@ -1841,11 +1844,7 @@ struct PathBuildingHop<'a> {
18411844
/// The value will be actually deducted from the counterparty balance on the previous link.
18421845
hop_use_fee_msat: u64,
18431846

1844-
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
1845-
// In tests, we apply further sanity checks on cases where we skip nodes we already processed
1846-
// to ensure it is specifically in cases where the fee has gone down because of a decrease in
1847-
// value_contribution_msat, which requires tracking it here. See comments below where it is
1848-
// used for more info.
1847+
/// The quantity of funds we're willing to route over this channel
18491848
value_contribution_msat: u64,
18501849
}
18511850

@@ -1866,9 +1865,7 @@ impl<'a> core::fmt::Debug for PathBuildingHop<'a> {
18661865
.field("total_fee_msat - (next_hops_fee_msat + hop_use_fee_msat)", &(&self.total_fee_msat.saturating_sub(self.next_hops_fee_msat).saturating_sub(self.hop_use_fee_msat)))
18671866
.field("path_penalty_msat", &self.path_penalty_msat)
18681867
.field("path_htlc_minimum_msat", &self.path_htlc_minimum_msat)
1869-
.field("cltv_expiry_delta", &self.candidate.cltv_expiry_delta());
1870-
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
1871-
let debug_struct = debug_struct
1868+
.field("cltv_expiry_delta", &self.candidate.cltv_expiry_delta())
18721869
.field("value_contribution_msat", &self.value_contribution_msat);
18731870
debug_struct.finish()
18741871
}
@@ -2578,7 +2575,6 @@ where L::Target: Logger {
25782575
path_penalty_msat: u64::max_value(),
25792576
was_processed: false,
25802577
is_first_hop_target: false,
2581-
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
25822578
value_contribution_msat,
25832579
});
25842580
dist_entry.as_mut().unwrap()
@@ -2654,8 +2650,11 @@ where L::Target: Logger {
26542650
.saturating_add(old_entry.path_penalty_msat);
26552651
let new_cost = cmp::max(total_fee_msat, path_htlc_minimum_msat)
26562652
.saturating_add(path_penalty_msat);
2653+
let should_replace =
2654+
new_cost < old_cost
2655+
|| (new_cost == old_cost && old_entry.value_contribution_msat < value_contribution_msat);
26572656

2658-
if !old_entry.was_processed && new_cost < old_cost {
2657+
if !old_entry.was_processed && should_replace {
26592658
let new_graph_node = RouteGraphNode {
26602659
node_id: src_node_id,
26612660
node_counter: src_node_counter,
@@ -2672,10 +2671,7 @@ where L::Target: Logger {
26722671
old_entry.fee_msat = 0; // This value will be later filled with hop_use_fee_msat of the following channel
26732672
old_entry.path_htlc_minimum_msat = path_htlc_minimum_msat;
26742673
old_entry.path_penalty_msat = path_penalty_msat;
2675-
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
2676-
{
2677-
old_entry.value_contribution_msat = value_contribution_msat;
2678-
}
2674+
old_entry.value_contribution_msat = value_contribution_msat;
26792675
hop_contribution_amt_msat = Some(value_contribution_msat);
26802676
} else if old_entry.was_processed && new_cost < old_cost {
26812677
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
@@ -2846,7 +2842,6 @@ where L::Target: Logger {
28462842
path_penalty_msat: u64::max_value(),
28472843
was_processed: false,
28482844
is_first_hop_target: true,
2849-
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
28502845
value_contribution_msat: 0,
28512846
});
28522847
}

0 commit comments

Comments
 (0)