Skip to content

Commit 24731d4

Browse files
joostjagerTheBlueMatt
authored andcommitted
fix historical liquidity bucket decay
The formula for applying half lives was incorrect. Test coverage added. Relatively straightforward merge conflicts (code added in 311a083 which was not included neighbored new code added) fixed in: * lightning/src/routing/scoring.rs
1 parent f80d82e commit 24731d4

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

lightning/src/routing/scoring.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,13 +1795,21 @@ mod bucketed_history {
17951795
self.buckets[bucket] = self.buckets[bucket].saturating_add(BUCKET_FIXED_POINT_ONE);
17961796
}
17971797
}
1798+
1799+
/// Applies decay at the given half-life to all buckets.
1800+
fn decay(&mut self, half_lives: f64) {
1801+
let factor = (1024.0 * powf64(0.5, half_lives)) as u64;
1802+
for bucket in self.buckets.iter_mut() {
1803+
*bucket = ((*bucket as u64) * factor / 1024) as u16;
1804+
}
1805+
}
17981806
}
17991807

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

18031811
#[derive(Clone, Copy)]
1804-
#[repr(C)] // Force the fields in memory to be in the order we specify.
1812+
#[repr(C)]// Force the fields in memory to be in the order we specify.
18051813
pub(super) struct HistoricalLiquidityTracker {
18061814
// This struct sits inside a `(u64, ChannelLiquidity)` in memory, and we first read the
18071815
// liquidity offsets in `ChannelLiquidity` when calculating the non-historical score. This
@@ -1849,13 +1857,8 @@ mod bucketed_history {
18491857
}
18501858

18511859
pub(super) fn decay_buckets(&mut self, half_lives: f64) {
1852-
let divisor = powf64(2048.0, half_lives) as u64;
1853-
for bucket in self.min_liquidity_offset_history.buckets.iter_mut() {
1854-
*bucket = ((*bucket as u64) * 1024 / divisor) as u16;
1855-
}
1856-
for bucket in self.max_liquidity_offset_history.buckets.iter_mut() {
1857-
*bucket = ((*bucket as u64) * 1024 / divisor) as u16;
1858-
}
1860+
self.min_liquidity_offset_history.decay(half_lives);
1861+
self.max_liquidity_offset_history.decay(half_lives);
18591862
self.recalculate_valid_point_count();
18601863
}
18611864

@@ -2049,6 +2052,33 @@ mod bucketed_history {
20492052
Some((cumulative_success_prob * (1024.0 * 1024.0 * 1024.0)) as u64)
20502053
}
20512054
}
2055+
2056+
#[cfg(test)]
2057+
mod tests {
2058+
use super::HistoricalBucketRangeTracker;
2059+
2060+
#[test]
2061+
fn historical_liquidity_bucket_decay() {
2062+
let mut bucket = HistoricalBucketRangeTracker::new();
2063+
bucket.track_datapoint(100, 1000);
2064+
assert_eq!(
2065+
bucket.buckets,
2066+
[
2067+
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,
2068+
0, 0, 0, 0, 0, 0, 0
2069+
]
2070+
);
2071+
2072+
bucket.decay(2.0);
2073+
assert_eq!(
2074+
bucket.buckets,
2075+
[
2076+
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,
2077+
0, 0, 0, 0, 0, 0, 0
2078+
]
2079+
);
2080+
}
2081+
}
20522082
}
20532083
use bucketed_history::{LegacyHistoricalBucketRangeTracker, HistoricalBucketRangeTracker, DirectedHistoricalLiquidityTracker, HistoricalLiquidityTracker};
20542084

0 commit comments

Comments
 (0)