Skip to content

Commit b39aef6

Browse files
committed
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 e46489d commit b39aef6

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
@@ -1179,7 +1179,10 @@ struct RouteGraphNode {
11791179

11801180
impl cmp::Ord for RouteGraphNode {
11811181
fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
1182-
other.score.cmp(&self.score).then_with(|| other.node_counter.cmp(&self.node_counter))
1182+
other.score.cmp(&self.score)
1183+
.then_with(|| self.value_contribution_msat.cmp(&other.value_contribution_msat))
1184+
.then_with(|| other.path_length_to_node.cmp(&self.path_length_to_node))
1185+
.then_with(|| other.node_counter.cmp(&self.node_counter))
11831186
}
11841187
}
11851188

@@ -1809,11 +1812,7 @@ struct PathBuildingHop<'a> {
18091812
/// The value will be actually deducted from the counterparty balance on the previous link.
18101813
hop_use_fee_msat: u64,
18111814

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

@@ -1834,9 +1833,7 @@ impl<'a> core::fmt::Debug for PathBuildingHop<'a> {
18341833
.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)))
18351834
.field("path_penalty_msat", &self.path_penalty_msat)
18361835
.field("path_htlc_minimum_msat", &self.path_htlc_minimum_msat)
1837-
.field("cltv_expiry_delta", &self.candidate.cltv_expiry_delta());
1838-
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
1839-
let debug_struct = debug_struct
1836+
.field("cltv_expiry_delta", &self.candidate.cltv_expiry_delta())
18401837
.field("value_contribution_msat", &self.value_contribution_msat);
18411838
debug_struct.finish()
18421839
}
@@ -2546,7 +2543,6 @@ where L::Target: Logger {
25462543
path_penalty_msat: u64::max_value(),
25472544
was_processed: false,
25482545
is_first_hop_target: false,
2549-
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
25502546
value_contribution_msat,
25512547
});
25522548
dist_entry.as_mut().unwrap()
@@ -2622,8 +2618,11 @@ where L::Target: Logger {
26222618
.saturating_add(old_entry.path_penalty_msat);
26232619
let new_cost = cmp::max(total_fee_msat, path_htlc_minimum_msat)
26242620
.saturating_add(path_penalty_msat);
2621+
let should_replace =
2622+
new_cost < old_cost
2623+
|| (new_cost == old_cost && old_entry.value_contribution_msat < value_contribution_msat);
26252624

2626-
if !old_entry.was_processed && new_cost < old_cost {
2625+
if !old_entry.was_processed && should_replace {
26272626
let new_graph_node = RouteGraphNode {
26282627
node_id: src_node_id,
26292628
node_counter: src_node_counter,
@@ -2640,10 +2639,7 @@ where L::Target: Logger {
26402639
old_entry.fee_msat = 0; // This value will be later filled with hop_use_fee_msat of the following channel
26412640
old_entry.path_htlc_minimum_msat = path_htlc_minimum_msat;
26422641
old_entry.path_penalty_msat = path_penalty_msat;
2643-
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
2644-
{
2645-
old_entry.value_contribution_msat = value_contribution_msat;
2646-
}
2642+
old_entry.value_contribution_msat = value_contribution_msat;
26472643
hop_contribution_amt_msat = Some(value_contribution_msat);
26482644
} else if old_entry.was_processed && new_cost < old_cost {
26492645
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
@@ -2814,7 +2810,6 @@ where L::Target: Logger {
28142810
path_penalty_msat: u64::max_value(),
28152811
was_processed: false,
28162812
is_first_hop_target: true,
2817-
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
28182813
value_contribution_msat: 0,
28192814
});
28202815
}

0 commit comments

Comments
 (0)