@@ -1977,13 +1977,21 @@ mod bucketed_history {
19771977 * bucket = ( ( * bucket as u32 + * other_bucket as u32 ) / 2 ) as u16 ;
19781978 }
19791979 }
1980+
1981+ /// Applies decay at the given half-life to all buckets.
1982+ fn decay ( & mut self , half_lives : f64 ) {
1983+ let factor = ( 1024.0 * powf64 ( 0.5 , half_lives) ) as u64 ;
1984+ for bucket in self . buckets . iter_mut ( ) {
1985+ * bucket = ( ( * bucket as u64 ) * factor / 1024 ) as u16 ;
1986+ }
1987+ }
19801988 }
19811989
19821990 impl_writeable_tlv_based ! ( HistoricalBucketRangeTracker , { ( 0 , buckets, required) } ) ;
19831991 impl_writeable_tlv_based ! ( LegacyHistoricalBucketRangeTracker , { ( 0 , buckets, required) } ) ;
19841992
19851993 #[ derive( Clone , Copy ) ]
1986- #[ repr( C ) ] // Force the fields in memory to be in the order we specify.
1994+ #[ repr( C ) ] // Force the fields in memory to be in the order we specify.
19871995 pub ( super ) struct HistoricalLiquidityTracker {
19881996 // This struct sits inside a `(u64, ChannelLiquidity)` in memory, and we first read the
19891997 // liquidity offsets in `ChannelLiquidity` when calculating the non-historical score. This
@@ -2031,13 +2039,8 @@ mod bucketed_history {
20312039 }
20322040
20332041 pub ( super ) fn decay_buckets ( & mut self , half_lives : f64 ) {
2034- let divisor = powf64 ( 2048.0 , half_lives) as u64 ;
2035- for bucket in self . min_liquidity_offset_history . buckets . iter_mut ( ) {
2036- * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
2037- }
2038- for bucket in self . max_liquidity_offset_history . buckets . iter_mut ( ) {
2039- * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
2040- }
2042+ self . min_liquidity_offset_history . decay ( half_lives) ;
2043+ self . max_liquidity_offset_history . decay ( half_lives) ;
20412044 self . recalculate_valid_point_count ( ) ;
20422045 }
20432046
@@ -2276,6 +2279,28 @@ mod bucketed_history {
22762279 ) ;
22772280 }
22782281
2282+ #[ test]
2283+ fn historical_liquidity_bucket_decay ( ) {
2284+ let mut bucket = HistoricalBucketRangeTracker :: new ( ) ;
2285+ bucket. track_datapoint ( 100 , 1000 ) ;
2286+ assert_eq ! (
2287+ bucket. buckets,
2288+ [
2289+ 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 ,
2290+ 0 , 0 , 0 , 0 , 0 , 0 , 0
2291+ ]
2292+ ) ;
2293+
2294+ bucket. decay ( 2.0 ) ;
2295+ assert_eq ! (
2296+ bucket. buckets,
2297+ [
2298+ 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 ,
2299+ 0 , 0 , 0 , 0 , 0 , 0 , 0
2300+ ]
2301+ ) ;
2302+ }
2303+
22792304 #[ test]
22802305 fn historical_liquidity_tracker_merge ( ) {
22812306 let params = ProbabilisticScoringFeeParameters :: default ( ) ;
0 commit comments