@@ -701,6 +701,10 @@ impl<T: Time> ChannelLiquidity<T> {
701
701
/// probabilities.
702
702
const NEGATIVE_LOG10_UPPER_BOUND : u64 = 2 ;
703
703
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
+
704
708
/// The divisor used when computing the amount penalty.
705
709
const AMOUNT_PENALTY_DIVISOR : u64 = 1 << 20 ;
706
710
@@ -727,9 +731,16 @@ impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiqui
727
731
} else {
728
732
let numerator = ( max_liquidity_msat - amount_msat) . saturating_add ( 1 ) ;
729
733
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
+ }
733
744
}
734
745
}
735
746
@@ -889,7 +900,7 @@ mod approx {
889
900
const BITS : u32 = 64 ;
890
901
const HIGHEST_BIT : u32 = BITS - 1 ;
891
902
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 ;
893
904
const LOWER_BITMASK : u64 = ( 1 << LOWER_BITS ) - 1 ;
894
905
895
906
/// 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 {
2007
2018
let source = source_node_id ( ) ;
2008
2019
let target = target_node_id ( ) ;
2009
2020
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 ) ;
2012
2023
assert_eq ! ( scorer. channel_penalty_msat( 42 , 102_400 , 1_024_000 , & source, & target) , 47 ) ;
2013
2024
assert_eq ! ( scorer. channel_penalty_msat( 42 , 1_024_000 , 1_024_000 , & source, & target) , 2_000 ) ;
2014
2025
@@ -2329,11 +2340,11 @@ mod tests {
2329
2340
assert_eq ! ( scorer. channel_penalty_msat( 42 , 100_000_000 , 3_950_000_000 , & source, & target) , 1223 ) ;
2330
2341
assert_eq ! ( scorer. channel_penalty_msat( 42 , 100_000_000 , 4_950_000_000 , & source, & target) , 877 ) ;
2331
2342
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 ) ;
2337
2348
}
2338
2349
2339
2350
#[ test]
0 commit comments