@@ -1007,11 +1007,25 @@ const BASE_AMOUNT_PENALTY_DIVISOR: u64 = 1 << 30;
1007
1007
/// (recently) seen an HTLC successfully complete over this channel.
1008
1008
#[ inline( always) ]
1009
1009
fn success_probability (
1010
- amount_msat : u64 , min_liquidity_msat : u64 , max_liquidity_msat : u64 , _capacity_msat : u64 ,
1011
- _params : & ProbabilisticScoringFeeParameters , _min_zero_implies_no_successes : bool ,
1010
+ amount_msat : u64 , min_liquidity_msat : u64 , max_liquidity_msat : u64 , capacity_msat : u64 ,
1011
+ _params : & ProbabilisticScoringFeeParameters , min_zero_implies_no_successes : bool ,
1012
1012
) -> ( u64 , u64 ) {
1013
+ debug_assert ! ( min_liquidity_msat <= amount_msat) ;
1014
+ debug_assert ! ( amount_msat < max_liquidity_msat) ;
1015
+ debug_assert ! ( max_liquidity_msat <= capacity_msat) ;
1016
+
1013
1017
let numerator = max_liquidity_msat - amount_msat;
1014
- let denominator = ( max_liquidity_msat - min_liquidity_msat) . saturating_add ( 1 ) ;
1018
+ let mut denominator = ( max_liquidity_msat - min_liquidity_msat) . saturating_add ( 1 ) ;
1019
+ if min_zero_implies_no_successes && min_liquidity_msat == 0 &&
1020
+ denominator < u64:: max_value ( ) / 21
1021
+ {
1022
+ // If we have no knowledge of the channel, scale probability down by ~75%
1023
+ // Note that we prefer to increase the denominator rather than decrease the numerator as
1024
+ // the denominator is more likely to be larger and thus provide greater precision. This is
1025
+ // mostly an overoptimization but makes a large difference in tests.
1026
+ denominator = denominator * 21 / 16
1027
+ }
1028
+
1015
1029
( numerator, denominator)
1016
1030
}
1017
1031
@@ -2956,47 +2970,47 @@ mod tests {
2956
2970
inflight_htlc_msat : 0 ,
2957
2971
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 950_000_000 , htlc_maximum_msat : 1_000 } ,
2958
2972
} ;
2959
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 4375 ) ;
2973
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 6262 ) ;
2960
2974
let usage = ChannelUsage {
2961
2975
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 1_950_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2962
2976
} ;
2963
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 2739 ) ;
2977
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 4634 ) ;
2964
2978
let usage = ChannelUsage {
2965
2979
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 2_950_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2966
2980
} ;
2967
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 2236 ) ;
2981
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 4186 ) ;
2968
2982
let usage = ChannelUsage {
2969
2983
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 3_950_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2970
2984
} ;
2971
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 1983 ) ;
2985
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 3909 ) ;
2972
2986
let usage = ChannelUsage {
2973
2987
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 4_950_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2974
2988
} ;
2975
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 1637 ) ;
2989
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 3556 ) ;
2976
2990
let usage = ChannelUsage {
2977
2991
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 5_950_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2978
2992
} ;
2979
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 1606 ) ;
2993
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 3533 ) ;
2980
2994
let usage = ChannelUsage {
2981
2995
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 6_950_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2982
2996
} ;
2983
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 1331 ) ;
2997
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 3172 ) ;
2984
2998
let usage = ChannelUsage {
2985
2999
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 7_450_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2986
3000
} ;
2987
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 1387 ) ;
3001
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 3211 ) ;
2988
3002
let usage = ChannelUsage {
2989
3003
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 7_950_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2990
3004
} ;
2991
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 1379 ) ;
3005
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 3243 ) ;
2992
3006
let usage = ChannelUsage {
2993
3007
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 8_950_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2994
3008
} ;
2995
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 1363 ) ;
3009
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 3297 ) ;
2996
3010
let usage = ChannelUsage {
2997
3011
effective_capacity : EffectiveCapacity :: Total { capacity_msat : 9_950_000_000 , htlc_maximum_msat : 1_000 } , ..usage
2998
3012
} ;
2999
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 1355 ) ;
3013
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 3250 ) ;
3000
3014
}
3001
3015
3002
3016
#[ test]
@@ -3161,15 +3175,15 @@ mod tests {
3161
3175
} ;
3162
3176
3163
3177
// With no historical data the normal liquidity penalty calculation is used.
3164
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 47 ) ;
3178
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 168 ) ;
3165
3179
assert_eq ! ( scorer. historical_estimated_channel_liquidity_probabilities( 42 , & target) ,
3166
3180
None ) ;
3167
3181
assert_eq ! ( scorer. historical_estimated_payment_success_probability( 42 , & target, 42 , & params) ,
3168
3182
None ) ;
3169
3183
3170
3184
scorer. payment_path_failed ( & payment_path_for_amount ( 1 ) , 42 ) ;
3171
3185
assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 2048 ) ;
3172
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage_1, & params) , 128 ) ;
3186
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage_1, & params) , 249 ) ;
3173
3187
// The "it failed" increment is 32, where the probability should lie several buckets into
3174
3188
// the first octile.
3175
3189
assert_eq ! ( scorer. historical_estimated_channel_liquidity_probabilities( 42 , & target) ,
@@ -3183,7 +3197,7 @@ mod tests {
3183
3197
// Even after we tell the scorer we definitely have enough available liquidity, it will
3184
3198
// still remember that there was some failure in the past, and assign a non-0 penalty.
3185
3199
scorer. payment_path_failed ( & payment_path_for_amount ( 1000 ) , 43 ) ;
3186
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 32 ) ;
3200
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 105 ) ;
3187
3201
// The first points should be decayed just slightly and the last bucket has a new point.
3188
3202
assert_eq ! ( scorer. historical_estimated_channel_liquidity_probabilities( 42 , & target) ,
3189
3203
Some ( ( [ 31 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 32 , 0 , 0 , 0 , 0 , 0 ] ,
@@ -3193,17 +3207,17 @@ mod tests {
3193
3207
// simply check bounds here.
3194
3208
let five_hundred_prob =
3195
3209
scorer. historical_estimated_payment_success_probability ( 42 , & target, 500 , & params) . unwrap ( ) ;
3196
- assert ! ( five_hundred_prob > 0.66 ) ;
3197
- assert ! ( five_hundred_prob < 0.68 ) ;
3210
+ assert ! ( five_hundred_prob > 0.59 ) ;
3211
+ assert ! ( five_hundred_prob < 0.60 ) ;
3198
3212
let one_prob =
3199
3213
scorer. historical_estimated_payment_success_probability ( 42 , & target, 1 , & params) . unwrap ( ) ;
3200
- assert ! ( one_prob < 1.0 ) ;
3201
- assert ! ( one_prob > 0.95 ) ;
3214
+ assert ! ( one_prob < 0.85 ) ;
3215
+ assert ! ( one_prob > 0.84 ) ;
3202
3216
3203
3217
// Advance the time forward 16 half-lives (which the docs claim will ensure all data is
3204
3218
// gone), and check that we're back to where we started.
3205
3219
SinceEpoch :: advance ( Duration :: from_secs ( 10 * 16 ) ) ;
3206
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 47 ) ;
3220
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 168 ) ;
3207
3221
// Once fully decayed we still have data, but its all-0s. In the future we may remove the
3208
3222
// data entirely instead.
3209
3223
assert_eq ! ( scorer. historical_estimated_channel_liquidity_probabilities( 42 , & target) ,
@@ -3367,9 +3381,9 @@ mod tests {
3367
3381
inflight_htlc_msat : 0 ,
3368
3382
effective_capacity : EffectiveCapacity :: Total { capacity_msat, htlc_maximum_msat : capacity_msat } ,
3369
3383
} ;
3370
- // With no historical data the normal liquidity penalty calculation is used, which in this
3371
- // case is diminuitively low .
3372
- assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 0 ) ;
3384
+ // With no historical data the normal liquidity penalty calculation is used, which results
3385
+ // in a success probability of ~75% .
3386
+ assert_eq ! ( scorer. channel_penalty_msat( 42 , & source, & target, usage, & params) , 1269 ) ;
3373
3387
assert_eq ! ( scorer. historical_estimated_channel_liquidity_probabilities( 42 , & target) ,
3374
3388
None ) ;
3375
3389
assert_eq ! ( scorer. historical_estimated_payment_success_probability( 42 , & target, 42 , & params) ,
0 commit comments