Skip to content

Commit 30e1922

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 4930af9 commit 30e1922

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

lightning/src/routing/scoring.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,10 @@ impl<T: Time> ChannelLiquidity<T> {
701701
/// probabilities.
702702
const NEGATIVE_LOG10_UPPER_BOUND: u64 = 2;
703703

704+
/// The rough cutoff at which our precision falls off and we should stop bothering to try to log a
705+
/// ratio, as X in 1/X.
706+
const PRECISION_LOWER_BOUND_DENOMINATOR: u64 = approx::LOWER_BITS_BOUND;
707+
704708
/// The divisor used when computing the amount penalty.
705709
const AMOUNT_PENALTY_DIVISOR: u64 = 1 << 20;
706710

@@ -727,9 +731,16 @@ impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiqui
727731
} else {
728732
let numerator = (max_liquidity_msat - amount_msat).saturating_add(1);
729733
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)
734+
if amount_msat - min_liquidity_msat < denominator / PRECISION_LOWER_BOUND_DENOMINATOR {
735+
// If the failure probability is < 1.5625% (as 1 - numerator/denominator < 1/64),
736+
// don't bother trying to use the log approximation as it gets too noisy to be
737+
// particularly helpful, instead just round down to 0 and return the base penalty.
738+
params.base_penalty_msat
739+
} else {
740+
let negative_log10_times_2048 =
741+
approx::negative_log10_times_2048(numerator, denominator);
742+
self.combined_penalty_msat(amount_msat, negative_log10_times_2048, params)
743+
}
733744
}
734745
}
735746

@@ -889,7 +900,7 @@ mod approx {
889900
const BITS: u32 = 64;
890901
const HIGHEST_BIT: u32 = BITS - 1;
891902
const LOWER_BITS: u32 = 6;
892-
const LOWER_BITS_BOUND: u64 = 1 << LOWER_BITS;
903+
pub(super) const LOWER_BITS_BOUND: u64 = 1 << LOWER_BITS;
893904
const LOWER_BITMASK: u64 = (1 << LOWER_BITS) - 1;
894905

895906
/// Look-up table for `log10(x) * 2048` where row `i` is used for each `x` having `i` as the
@@ -2007,8 +2018,8 @@ mod tests {
20072018
let source = source_node_id();
20082019
let target = target_node_id();
20092020

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);
2021+
assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024_000, &source, &target), 0);
2022+
assert_eq!(scorer.channel_penalty_msat(42, 10_240, 1_024_000, &source, &target), 0);
20122023
assert_eq!(scorer.channel_penalty_msat(42, 102_400, 1_024_000, &source, &target), 47);
20132024
assert_eq!(scorer.channel_penalty_msat(42, 1_024_000, 1_024_000, &source, &target), 2_000);
20142025

@@ -2329,11 +2340,11 @@ mod tests {
23292340
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 3_950_000_000, &source, &target), 1223);
23302341
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 4_950_000_000, &source, &target), 877);
23312342
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);
2343+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 6_950_000_000, &source, &target), 500);
2344+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 7_450_000_000, &source, &target), 500);
2345+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 7_950_000_000, &source, &target), 500);
2346+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 8_950_000_000, &source, &target), 500);
2347+
assert_eq!(scorer.channel_penalty_msat(42, 100_000_000, 9_950_000_000, &source, &target), 500);
23372348
}
23382349

23392350
#[test]

0 commit comments

Comments
 (0)