Skip to content

Commit 0b725f2

Browse files
committed
Lower-bound the log approximation and stop using it > ~98.5%
When we start getting a numerator and divisor particularly close to each other, the log approximation starts to get very noisy. In order to avoid applying scores that are basically noise (and can range upwards of 2x the default per-hop penalty), simply consider such cases as having a success probability of 100%.
1 parent 067753a commit 0b725f2

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

lightning/src/routing/scoring.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,16 @@ impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiqui
727727
} else {
728728
let numerator = (max_liquidity_msat - amount_msat).saturating_add(1);
729729
let denominator = (max_liquidity_msat - min_liquidity_msat).saturating_add(1);
730-
let negative_log10_times_2048 =
731-
approx::negative_log10_times_2048(numerator, denominator);
732-
self.combined_penalty_msat(amount_msat, negative_log10_times_2048, params)
730+
if amount_msat - min_liquidity_msat < denominator / 64 {
731+
// If the failure probability is < 1.5625%, don't bother trying to use the log
732+
// approximation as it gets too noisy to be particularly helpful, instead just
733+
// round down to 0 and return the base penalty.
734+
params.base_penalty_msat
735+
} else {
736+
let negative_log10_times_2048 =
737+
approx::negative_log10_times_2048(numerator, denominator);
738+
self.combined_penalty_msat(amount_msat, negative_log10_times_2048, params)
739+
}
733740
}
734741
}
735742

@@ -2007,8 +2014,8 @@ mod tests {
20072014
let source = source_node_id();
20082015
let target = target_node_id();
20092016

2010-
assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024_000, &source, &target), 3);
2011-
assert_eq!(scorer.channel_penalty_msat(42, 10_240, 1_024_000, &source, &target), 6);
2017+
assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024_000, &source, &target), 0);
2018+
assert_eq!(scorer.channel_penalty_msat(42, 10_240, 1_024_000, &source, &target), 0);
20122019
assert_eq!(scorer.channel_penalty_msat(42, 102_400, 1_024_000, &source, &target), 47);
20132020
assert_eq!(scorer.channel_penalty_msat(42, 1_024_000, 1_024_000, &source, &target), 2_000);
20142021

@@ -2329,11 +2336,11 @@ mod tests {
23292336
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 3_950_000_000, &source, &target), 1223);
23302337
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 4_950_000_000, &source, &target), 877);
23312338
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 5_950_000_000, &source, &target), 845);
2332-
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 6_950_000_000, &source, &target), 782);
2333-
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 7_450_000_000, &source, &target), 1002);
2334-
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 7_950_000_000, &source, &target), 970);
2335-
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 8_950_000_000, &source, &target), 907);
2336-
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 9_950_000_000, &source, &target), 877);
2339+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 6_950_000_000, &source, &target), 500);
2340+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 7_450_000_000, &source, &target), 500);
2341+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 7_950_000_000, &source, &target), 500);
2342+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 8_950_000_000, &source, &target), 500);
2343+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 9_950_000_000, &source, &target), 500);
23372344
}
23382345

23392346
#[test]

0 commit comments

Comments
 (0)