Skip to content

Commit 4ea18e3

Browse files
committed
Fix overflow in ProbabilisticScorer
When a routing hint is given in an invoice, the effective capacity of the channel is assumed to be infinite (i.e., u64::max_value) if the hop is private. Adding 1 to this in the success probability calculation will cause an overflow and ultimately an `index out of bounds panic` in log10_times_1024. This was not an issue with using log10 because the use of f64 would give infinite which casts to 0 for u64.
1 parent 5b36449 commit 4ea18e3

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lightning/src/routing/scoring.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,8 @@ impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiqui
673673
} else if amount_msat <= min_liquidity_msat {
674674
0
675675
} else {
676-
let numerator = max_liquidity_msat + 1 - amount_msat;
677-
let denominator = max_liquidity_msat + 1 - min_liquidity_msat;
676+
let numerator = (max_liquidity_msat - amount_msat).saturating_add(1);
677+
let denominator = (max_liquidity_msat - min_liquidity_msat).saturating_add(1);
678678
approx::negative_log10_times_1024(numerator, denominator)
679679
.saturating_mul(liquidity_penalty_multiplier_msat) / 1024
680680
}
@@ -2061,4 +2061,20 @@ mod tests {
20612061
let scorer = ProbabilisticScorer::new(params, &network_graph);
20622062
assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 1085);
20632063
}
2064+
2065+
#[test]
2066+
fn calculates_log10_without_overflowing_u64_max_value() {
2067+
let network_graph = network_graph();
2068+
let source = source_node_id();
2069+
let target = target_node_id();
2070+
2071+
let params = ProbabilisticScoringParameters {
2072+
base_penalty_msat: 0, ..Default::default()
2073+
};
2074+
let scorer = ProbabilisticScorer::new(params, &network_graph);
2075+
assert_eq!(
2076+
scorer.channel_penalty_msat(42, u64::max_value(), u64::max_value(), &source, &target),
2077+
20_000,
2078+
);
2079+
}
20642080
}

0 commit comments

Comments
 (0)