Skip to content

Commit dc01b0e

Browse files
committed
[router] Calc min-to-node fees based on min value, not est value
When walking the network graph to calculate a route, we always calculate the minimum fee which is required to make one further hop. This avoids some extra hop processing at the end of each path selection loop (saving around 10% runtime in our benchmarks). However, if we use the real value which we expect to send over a channel in that calculation, we may find an alternate path to the same node which is more expensive but capacity-constrained, resulting in us considering it cheaper as the relative fee paid will be lower. Instead, we can use the `minimal_value_contribution_msat`, which is a constant through an entire path finding iteration, as the amount, preventing any basis change in the relative fee paid.
1 parent 31859bf commit dc01b0e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lightning/src/routing/router.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -630,21 +630,22 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
630630
Some(fee_msat) => {
631631
hop_use_fee_msat = fee_msat;
632632
total_fee_msat += hop_use_fee_msat;
633-
if let Some(prev_hop_fee_msat) = compute_fees(total_fee_msat + amount_to_transfer_over_msat,
634-
old_entry.src_lowest_inbound_fees) {
635-
if let Some(incremented_total_fee_msat) = total_fee_msat.checked_add(prev_hop_fee_msat) {
636-
total_fee_msat = incremented_total_fee_msat;
637-
}
638-
else {
639-
// max_value means we'll always fail
640-
// the old_entry.total_fee_msat > total_fee_msat check
633+
// When calculating the lowest inbound fees to a node, we
634+
// calculate fees here not based on the actual value we think
635+
// will flow over this channel, but on the minimum value that
636+
// we'll accept flowing over it. Otherwise we may later find a
637+
// different path to the source node that is more expensive,
638+
// but which we consider to be cheaper because we are capacity
639+
// constrained and the relative fee becomes lower.
640+
match compute_fees(minimal_value_contribution_msat, old_entry.src_lowest_inbound_fees)
641+
.map(|a| a.checked_add(total_fee_msat)) {
642+
Some(Some(v)) => {
643+
total_fee_msat = v;
644+
},
645+
_ => {
641646
total_fee_msat = u64::max_value();
642647
}
643-
} else {
644-
// max_value means we'll always fail
645-
// the old_entry.total_fee_msat > total_fee_msat check
646-
total_fee_msat = u64::max_value();
647-
}
648+
};
648649
}
649650
}
650651
}

0 commit comments

Comments
 (0)