Skip to content

Commit a8aeaed

Browse files
committed
Router: Ensure used liquidity is always limited by hop's capacity
Previously, when recomputing fees for bottleneck hops, we might allow the tracked used liquidity values to surpass the actual hop capacity, which is bogus. Here, we assert we'd always limit the amount spend on a hop by its capacity. Found by a `fuzz` test hitting the related `debug_assert`.
1 parent 86308e1 commit a8aeaed

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lightning/src/routing/router.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,13 +3113,19 @@ where L::Target: Logger {
31133113
// on the same liquidity in future paths.
31143114
let mut prevented_redundant_path_selection = false;
31153115
for (hop, _) in payment_path.hops.iter() {
3116-
let spent_on_hop_msat = value_contribution_msat + hop.next_hops_fee_msat;
3116+
let hop_capacity = hop.candidate.effective_capacity();
3117+
let hop_max_msat = max_htlc_from_capacity(hop_capacity, channel_saturation_pow_half);
3118+
let spent_on_hop_msat = value_contribution_msat
3119+
.saturating_add(hop.next_hops_fee_msat)
3120+
.min(hop_max_msat);
31173121
let used_liquidity_msat = used_liquidities
31183122
.entry(hop.candidate.id())
3119-
.and_modify(|used_liquidity_msat| *used_liquidity_msat += spent_on_hop_msat)
3123+
.and_modify(|used_liquidity_msat| {
3124+
*used_liquidity_msat = used_liquidity_msat
3125+
.saturating_add(spent_on_hop_msat)
3126+
.min(hop_max_msat)
3127+
})
31203128
.or_insert(spent_on_hop_msat);
3121-
let hop_capacity = hop.candidate.effective_capacity();
3122-
let hop_max_msat = max_htlc_from_capacity(hop_capacity, channel_saturation_pow_half);
31233129
if *used_liquidity_msat == hop_max_msat {
31243130
// If this path used all of this channel's available liquidity, we know
31253131
// this path will not be selected again in the next loop iteration.

0 commit comments

Comments
 (0)