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