@@ -1963,13 +1963,21 @@ mod bucketed_history {
1963
1963
* bucket = ( * bucket + other. buckets [ index] ) / 2 ;
1964
1964
}
1965
1965
}
1966
+
1967
+ /// Applies decay at the given half-life to all buckets.
1968
+ fn decay ( & mut self , half_lives : f64 ) {
1969
+ let factor = ( 1024.0 * powf64 ( 0.5 , half_lives) ) as u64 ;
1970
+ for bucket in self . buckets . iter_mut ( ) {
1971
+ * bucket = ( ( * bucket as u64 ) * factor / 1024 ) as u16 ;
1972
+ }
1973
+ }
1966
1974
}
1967
1975
1968
1976
impl_writeable_tlv_based ! ( HistoricalBucketRangeTracker , { ( 0 , buckets, required) } ) ;
1969
1977
impl_writeable_tlv_based ! ( LegacyHistoricalBucketRangeTracker , { ( 0 , buckets, required) } ) ;
1970
1978
1971
1979
#[ derive( Clone , Copy ) ]
1972
- #[ repr( C ) ] // Force the fields in memory to be in the order we specify.
1980
+ #[ repr( C ) ] // Force the fields in memory to be in the order we specify.
1973
1981
pub ( super ) struct HistoricalLiquidityTracker {
1974
1982
// This struct sits inside a `(u64, ChannelLiquidity)` in memory, and we first read the
1975
1983
// liquidity offsets in `ChannelLiquidity` when calculating the non-historical score. This
@@ -2017,13 +2025,8 @@ mod bucketed_history {
2017
2025
}
2018
2026
2019
2027
pub ( super ) fn decay_buckets ( & mut self , half_lives : f64 ) {
2020
- let divisor = powf64 ( 2048.0 , half_lives) as u64 ;
2021
- for bucket in self . min_liquidity_offset_history . buckets . iter_mut ( ) {
2022
- * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
2023
- }
2024
- for bucket in self . max_liquidity_offset_history . buckets . iter_mut ( ) {
2025
- * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
2026
- }
2028
+ self . min_liquidity_offset_history . decay ( half_lives) ;
2029
+ self . max_liquidity_offset_history . decay ( half_lives) ;
2027
2030
self . recalculate_valid_point_count ( ) ;
2028
2031
}
2029
2032
@@ -2262,6 +2265,28 @@ mod bucketed_history {
2262
2265
) ;
2263
2266
}
2264
2267
2268
+ #[ test]
2269
+ fn historical_liquidity_bucket_decay ( ) {
2270
+ let mut bucket = HistoricalBucketRangeTracker :: new ( ) ;
2271
+ bucket. track_datapoint ( 100 , 1000 ) ;
2272
+ assert_eq ! (
2273
+ bucket. buckets,
2274
+ [
2275
+ 0u16 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 32 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
2276
+ 0 , 0 , 0 , 0 , 0 , 0 , 0
2277
+ ]
2278
+ ) ;
2279
+
2280
+ bucket. decay ( 2.0 ) ;
2281
+ assert_eq ! (
2282
+ bucket. buckets,
2283
+ [
2284
+ 0u16 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 8 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
2285
+ 0 , 0 , 0 , 0 , 0 , 0 , 0
2286
+ ]
2287
+ ) ;
2288
+ }
2289
+
2265
2290
#[ test]
2266
2291
fn historical_liquidity_tracker_merge ( ) {
2267
2292
let params = ProbabilisticScoringFeeParameters :: default ( ) ;
0 commit comments