@@ -531,7 +531,10 @@ pub struct ProbabilisticScoringParameters {
531
531
///
532
532
/// The penalty is based in part by the knowledge learned from prior successful and unsuccessful
533
533
/// payments. This knowledge is decayed over time based on [`liquidity_offset_half_life`]. The
534
- /// penalty is effectively limited to `2 * liquidity_penalty_multiplier_msat`.
534
+ /// penalty is effectively limited to `2 * liquidity_penalty_multiplier_msat` (corresponding to
535
+ /// lower bounding the success probability to `0.01`) when the amount falls within the
536
+ /// uncertainty bounds of the channel liquidity balance. Amounts above the upper bound will
537
+ /// result in a `u64::max_value` penalty, however.
535
538
///
536
539
/// Default value: 40,000 msat
537
540
///
@@ -666,19 +669,22 @@ impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiqui
666
669
/// Returns a penalty for routing the given HTLC `amount_msat` through the channel in this
667
670
/// direction.
668
671
fn penalty_msat ( & self , amount_msat : u64 , liquidity_penalty_multiplier_msat : u64 ) -> u64 {
669
- let max_penalty_msat = liquidity_penalty_multiplier_msat. saturating_mul ( 2 ) ;
670
672
let max_liquidity_msat = self . max_liquidity_msat ( ) ;
671
673
let min_liquidity_msat = core:: cmp:: min ( self . min_liquidity_msat ( ) , max_liquidity_msat) ;
672
- if amount_msat > max_liquidity_msat {
673
- max_penalty_msat
674
- } else if amount_msat <= min_liquidity_msat {
674
+ if amount_msat <= min_liquidity_msat {
675
675
0
676
+ } else if amount_msat > max_liquidity_msat {
677
+ u64:: max_value ( )
678
+ } else if amount_msat == max_liquidity_msat && max_liquidity_msat != self . capacity_msat {
679
+ // Avoid using the failed channel on retry.
680
+ u64:: max_value ( )
676
681
} else {
677
682
let numerator = ( max_liquidity_msat - amount_msat) . saturating_add ( 1 ) ;
678
683
let denominator = ( max_liquidity_msat - min_liquidity_msat) . saturating_add ( 1 ) ;
679
684
let penalty_msat = approx:: negative_log10_times_1024 ( numerator, denominator)
680
685
. saturating_mul ( liquidity_penalty_multiplier_msat) / 1024 ;
681
686
// Upper bound the penalty to ensure some channel is selected.
687
+ let max_penalty_msat = liquidity_penalty_multiplier_msat. saturating_mul ( 2 ) ;
682
688
penalty_msat. min ( max_penalty_msat)
683
689
}
684
690
}
@@ -1774,8 +1780,8 @@ mod tests {
1774
1780
1775
1781
assert_eq ! ( scorer. channel_penalty_msat( 42 , 39 , 100 , & source, & target) , 0 ) ;
1776
1782
assert_ne ! ( scorer. channel_penalty_msat( 42 , 50 , 100 , & source, & target) , 0 ) ;
1777
- assert_ne ! ( scorer. channel_penalty_msat( 42 , 50 , 100 , & source, & target) , 2_000 ) ;
1778
- assert_eq ! ( scorer. channel_penalty_msat( 42 , 61 , 100 , & source, & target) , 2_000 ) ;
1783
+ assert_ne ! ( scorer. channel_penalty_msat( 42 , 50 , 100 , & source, & target) , u64 :: max_value ( ) ) ;
1784
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , 61 , 100 , & source, & target) , u64 :: max_value ( ) ) ;
1779
1785
}
1780
1786
1781
1787
#[ test]
@@ -1839,8 +1845,8 @@ mod tests {
1839
1845
scorer. payment_path_failed ( & path. iter ( ) . collect :: < Vec < _ > > ( ) , 42 ) ;
1840
1846
1841
1847
assert_eq ! ( scorer. channel_penalty_msat( 42 , 250 , 1_000 , & source, & target) , 300 ) ;
1842
- assert_eq ! ( scorer. channel_penalty_msat( 42 , 500 , 1_000 , & source, & target) , 2_000 ) ;
1843
- assert_eq ! ( scorer. channel_penalty_msat( 42 , 750 , 1_000 , & source, & target) , 2_000 ) ;
1848
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , 500 , 1_000 , & source, & target) , u64 :: max_value ( ) ) ;
1849
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , 750 , 1_000 , & source, & target) , u64 :: max_value ( ) ) ;
1844
1850
}
1845
1851
1846
1852
#[ test]
@@ -1888,19 +1894,19 @@ mod tests {
1888
1894
assert_eq ! ( scorer. channel_penalty_msat( 42 , 128 , 1_024 , & source, & target) , 0 ) ;
1889
1895
assert_eq ! ( scorer. channel_penalty_msat( 42 , 256 , 1_024 , & source, & target) , 97 ) ;
1890
1896
assert_eq ! ( scorer. channel_penalty_msat( 42 , 768 , 1_024 , & source, & target) , 1_409 ) ;
1891
- assert_eq ! ( scorer. channel_penalty_msat( 42 , 896 , 1_024 , & source, & target) , 2_000 ) ;
1897
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , 896 , 1_024 , & source, & target) , u64 :: max_value ( ) ) ;
1892
1898
1893
1899
SinceEpoch :: advance ( Duration :: from_secs ( 9 ) ) ;
1894
1900
assert_eq ! ( scorer. channel_penalty_msat( 42 , 128 , 1_024 , & source, & target) , 0 ) ;
1895
1901
assert_eq ! ( scorer. channel_penalty_msat( 42 , 256 , 1_024 , & source, & target) , 97 ) ;
1896
1902
assert_eq ! ( scorer. channel_penalty_msat( 42 , 768 , 1_024 , & source, & target) , 1_409 ) ;
1897
- assert_eq ! ( scorer. channel_penalty_msat( 42 , 896 , 1_024 , & source, & target) , 2_000 ) ;
1903
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , 896 , 1_024 , & source, & target) , u64 :: max_value ( ) ) ;
1898
1904
1899
1905
SinceEpoch :: advance ( Duration :: from_secs ( 1 ) ) ;
1900
1906
assert_eq ! ( scorer. channel_penalty_msat( 42 , 64 , 1_024 , & source, & target) , 0 ) ;
1901
1907
assert_eq ! ( scorer. channel_penalty_msat( 42 , 128 , 1_024 , & source, & target) , 34 ) ;
1902
1908
assert_eq ! ( scorer. channel_penalty_msat( 42 , 896 , 1_024 , & source, & target) , 1_773 ) ;
1903
- assert_eq ! ( scorer. channel_penalty_msat( 42 , 960 , 1_024 , & source, & target) , 2_000 ) ;
1909
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , 960 , 1_024 , & source, & target) , u64 :: max_value ( ) ) ;
1904
1910
1905
1911
// Fully decay liquidity lower bound.
1906
1912
SinceEpoch :: advance ( Duration :: from_secs ( 10 * 7 ) ) ;
@@ -1995,7 +2001,7 @@ mod tests {
1995
2001
let target = target_node_id ( ) ;
1996
2002
1997
2003
scorer. payment_path_failed ( & payment_path_for_amount ( 500 ) . iter ( ) . collect :: < Vec < _ > > ( ) , 42 ) ;
1998
- assert_eq ! ( scorer. channel_penalty_msat( 42 , 500 , 1_000 , & source, & target) , 2_000 ) ;
2004
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , 500 , 1_000 , & source, & target) , u64 :: max_value ( ) ) ;
1999
2005
2000
2006
SinceEpoch :: advance ( Duration :: from_secs ( 10 ) ) ;
2001
2007
assert_eq ! ( scorer. channel_penalty_msat( 42 , 500 , 1_000 , & source, & target) , 472 ) ;
@@ -2025,7 +2031,7 @@ mod tests {
2025
2031
let target = target_node_id ( ) ;
2026
2032
2027
2033
scorer. payment_path_failed ( & payment_path_for_amount ( 500 ) . iter ( ) . collect :: < Vec < _ > > ( ) , 42 ) ;
2028
- assert_eq ! ( scorer. channel_penalty_msat( 42 , 500 , 1_000 , & source, & target) , 2_000 ) ;
2034
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , 500 , 1_000 , & source, & target) , u64 :: max_value ( ) ) ;
2029
2035
2030
2036
let mut serialized_scorer = Vec :: new ( ) ;
2031
2037
scorer. write ( & mut serialized_scorer) . unwrap ( ) ;
0 commit comments