Skip to content

Commit

Permalink
fix historical liquidity bucket decay
Browse files Browse the repository at this point in the history
The formula for applying half lives was incorrect. Test coverage added.
  • Loading branch information
joostjager committed Feb 3, 2025
1 parent 761fc52 commit 055177c
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions lightning/src/routing/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1968,13 +1968,21 @@ mod bucketed_history {
*bucket = (*bucket + other.buckets[index]) / 2;
}
}

/// Applies decay at the given half-life to all buckets.
fn decay(&mut self, half_lives: f64) {
let factor = (1024.0 * powf64(0.5, half_lives)) as u64;
for bucket in self.buckets.iter_mut() {
*bucket = ((*bucket as u64) * factor / 1024) as u16;
}
}
}

impl_writeable_tlv_based!(HistoricalBucketRangeTracker, { (0, buckets, required) });
impl_writeable_tlv_based!(LegacyHistoricalBucketRangeTracker, { (0, buckets, required) });

#[derive(Clone, Copy)]
#[repr(C)] // Force the fields in memory to be in the order we specify.
#[repr(C)]// Force the fields in memory to be in the order we specify.
pub(super) struct HistoricalLiquidityTracker {
// This struct sits inside a `(u64, ChannelLiquidity)` in memory, and we first read the
// liquidity offsets in `ChannelLiquidity` when calculating the non-historical score. This
Expand Down Expand Up @@ -2022,13 +2030,8 @@ mod bucketed_history {
}

pub(super) fn decay_buckets(&mut self, half_lives: f64) {
let divisor = powf64(2048.0, half_lives) as u64;
for bucket in self.min_liquidity_offset_history.buckets.iter_mut() {
*bucket = ((*bucket as u64) * 1024 / divisor) as u16;
}
for bucket in self.max_liquidity_offset_history.buckets.iter_mut() {
*bucket = ((*bucket as u64) * 1024 / divisor) as u16;
}
self.min_liquidity_offset_history.decay(half_lives);
self.max_liquidity_offset_history.decay(half_lives);
self.recalculate_valid_point_count();
}

Expand Down Expand Up @@ -2267,6 +2270,28 @@ mod bucketed_history {
);
}

#[test]
fn historical_liquidity_bucket_decay() {
let mut bucket = HistoricalBucketRangeTracker::new();
bucket.track_datapoint(100, 1000);
assert_eq!(
bucket.buckets,
[
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,
0, 0, 0, 0, 0, 0, 0
]
);

bucket.decay(2.0);
assert_eq!(
bucket.buckets,
[
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,
0, 0, 0, 0, 0, 0, 0
]
);
}

#[test]
fn historical_liquidity_tracker_merge() {
let params = ProbabilisticScoringFeeParameters::default();
Expand Down

0 comments on commit 055177c

Please sign in to comment.