@@ -725,7 +725,7 @@ impl HistoricalMinMaxBuckets<'_> {
725
725
726
726
#[ inline]
727
727
fn calculate_success_probability_times_billion < T : Time > (
728
- & self , now : T , last_updated : T , half_life : Duration , payment_amt_64th_bucket : u8 )
728
+ & self , now : T , last_updated : T , half_life : Duration , amount_msat : u64 , capacity_msat : u64 )
729
729
-> Option < u64 > {
730
730
// If historical penalties are enabled, calculate the penalty by walking the set of
731
731
// historical liquidity bucket (min, max) combinations (where min_idx < max_idx) and, for
@@ -748,6 +748,20 @@ impl HistoricalMinMaxBuckets<'_> {
748
748
// less than 1/16th of a channel's capacity, or 1/8th if we used the top of the bucket.
749
749
let mut total_valid_points_tracked = 0 ;
750
750
751
+ let payment_amt_64th_bucket: u8 = if amount_msat < u64:: max_value ( ) / 64 {
752
+ ( amount_msat * 64 / capacity_msat. saturating_add ( 1 ) )
753
+ . try_into ( ) . unwrap_or ( 65 )
754
+ } else {
755
+ // Only use 128-bit arithmetic when multiplication will overflow to avoid 128-bit
756
+ // division. This branch should only be hit in fuzz testing since the amount would
757
+ // need to be over 2.88 million BTC in practice.
758
+ ( ( amount_msat as u128 ) * 64 / ( capacity_msat as u128 ) . saturating_add ( 1 ) )
759
+ . try_into ( ) . unwrap_or ( 65 )
760
+ } ;
761
+ #[ cfg( not( fuzzing) ) ]
762
+ debug_assert ! ( payment_amt_64th_bucket <= 64 ) ;
763
+ if payment_amt_64th_bucket >= 64 { return None ; }
764
+
751
765
// Check if all our buckets are zero, once decayed and treat it as if we had no data. We
752
766
// don't actually use the decayed buckets, though, as that would lose precision.
753
767
let ( decayed_min_buckets, decayed_max_buckets, required_decays) =
@@ -1078,26 +1092,13 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
1078
1092
1079
1093
if score_params. historical_liquidity_penalty_multiplier_msat != 0 ||
1080
1094
score_params. historical_liquidity_penalty_amount_multiplier_msat != 0 {
1081
- let payment_amt_64th_bucket = if amount_msat < u64:: max_value ( ) / 64 {
1082
- amount_msat * 64 / self . capacity_msat . saturating_add ( 1 )
1083
- } else {
1084
- // Only use 128-bit arithmetic when multiplication will overflow to avoid 128-bit
1085
- // division. This branch should only be hit in fuzz testing since the amount would
1086
- // need to be over 2.88 million BTC in practice.
1087
- ( ( amount_msat as u128 ) * 64 / ( self . capacity_msat as u128 ) . saturating_add ( 1 ) )
1088
- . try_into ( ) . unwrap_or ( 65 )
1089
- } ;
1090
- #[ cfg( not( fuzzing) ) ]
1091
- debug_assert ! ( payment_amt_64th_bucket <= 64 ) ;
1092
- if payment_amt_64th_bucket > 64 { return res; }
1093
-
1094
1095
let buckets = HistoricalMinMaxBuckets {
1095
1096
min_liquidity_offset_history : & self . min_liquidity_offset_history ,
1096
1097
max_liquidity_offset_history : & self . max_liquidity_offset_history ,
1097
1098
} ;
1098
1099
if let Some ( cumulative_success_prob_times_billion) = buckets
1099
1100
. calculate_success_probability_times_billion ( self . now , * self . last_updated ,
1100
- self . decay_params . historical_no_updates_half_life , payment_amt_64th_bucket as u8 )
1101
+ self . decay_params . historical_no_updates_half_life , amount_msat , self . capacity_msat )
1101
1102
{
1102
1103
let historical_negative_log10_times_2048 = approx:: negative_log10_times_2048 ( cumulative_success_prob_times_billion + 1 , 1024 * 1024 * 1024 ) ;
1103
1104
res = res. saturating_add ( Self :: combined_penalty_msat ( amount_msat,
0 commit comments