@@ -1968,13 +1968,21 @@ mod bucketed_history {
19681968 * bucket = ( * bucket + other. buckets [ index] ) / 2 ;
19691969 }
19701970 }
1971+
1972+ /// Applies decay at the given half-life to all buckets.
1973+ fn decay ( & mut self , half_lives : f64 ) {
1974+ let factor = ( 1024.0 * powf64 ( 0.5 , half_lives) ) as u64 ;
1975+ for bucket in self . buckets . iter_mut ( ) {
1976+ * bucket = ( ( * bucket as u64 ) * factor / 1024 ) as u16 ;
1977+ }
1978+ }
19711979 }
19721980
19731981 impl_writeable_tlv_based ! ( HistoricalBucketRangeTracker , { ( 0 , buckets, required) } ) ;
19741982 impl_writeable_tlv_based ! ( LegacyHistoricalBucketRangeTracker , { ( 0 , buckets, required) } ) ;
19751983
19761984 #[ derive( Clone , Copy ) ]
1977- #[ repr( C ) ] // Force the fields in memory to be in the order we specify.
1985+ #[ repr( C ) ] // Force the fields in memory to be in the order we specify.
19781986 pub ( super ) struct HistoricalLiquidityTracker {
19791987 // This struct sits inside a `(u64, ChannelLiquidity)` in memory, and we first read the
19801988 // liquidity offsets in `ChannelLiquidity` when calculating the non-historical score. This
@@ -2022,13 +2030,8 @@ mod bucketed_history {
20222030 }
20232031
20242032 pub ( super ) fn decay_buckets ( & mut self , half_lives : f64 ) {
2025- let divisor = powf64 ( 2048.0 , half_lives) as u64 ;
2026- for bucket in self . min_liquidity_offset_history . buckets . iter_mut ( ) {
2027- * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
2028- }
2029- for bucket in self . max_liquidity_offset_history . buckets . iter_mut ( ) {
2030- * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
2031- }
2033+ self . min_liquidity_offset_history . decay ( half_lives) ;
2034+ self . max_liquidity_offset_history . decay ( half_lives) ;
20322035 self . recalculate_valid_point_count ( ) ;
20332036 }
20342037
@@ -2267,6 +2270,28 @@ mod bucketed_history {
22672270 ) ;
22682271 }
22692272
2273+ #[ test]
2274+ fn historical_liquidity_bucket_decay ( ) {
2275+ let mut bucket = HistoricalBucketRangeTracker :: new ( ) ;
2276+ bucket. track_datapoint ( 100 , 1000 ) ;
2277+ assert_eq ! (
2278+ bucket. buckets,
2279+ [
2280+ 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 ,
2281+ 0 , 0 , 0 , 0 , 0 , 0 , 0
2282+ ]
2283+ ) ;
2284+
2285+ bucket. decay ( 2.0 ) ;
2286+ assert_eq ! (
2287+ bucket. buckets,
2288+ [
2289+ 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 ,
2290+ 0 , 0 , 0 , 0 , 0 , 0 , 0
2291+ ]
2292+ ) ;
2293+ }
2294+
22702295 #[ test]
22712296 fn historical_liquidity_tracker_merge ( ) {
22722297 let params = ProbabilisticScoringFeeParameters :: default ( ) ;
0 commit comments