Skip to content

Commit 9437642

Browse files
committed
Move to a constant for "bucket one" in the scoring buckets
Scoring buckets are stored as fixed point ints, with a 5-bit fractional part (i.e. a value of 1.0 is stored as "32"). Now that we also have 32 buckets, this leads to the codebase having many references to 32 which could reasonably be confused for each other. Thus, we add a constant here for the value 1.0 in our fixed-point scheme.
1 parent f7f524f commit 9437642

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lightning/src/routing/scoring.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,10 @@ mod bucketed_history {
16841684
buckets: [u16; 32],
16851685
}
16861686

1687+
/// Buckets are stored in fixed point numbers with a 5 bit fractional part. Thus, the value
1688+
/// "one" is 32, or this constant.
1689+
pub const BUCKET_FIXED_POINT_ONE: u16 = 32;
1690+
16871691
impl HistoricalBucketRangeTracker {
16881692
pub(super) fn new() -> Self { Self { buckets: [0; 32] } }
16891693
pub(super) fn track_datapoint(&mut self, liquidity_offset_msat: u64, capacity_msat: u64) {
@@ -1714,7 +1718,7 @@ mod bucketed_history {
17141718
*e = ((*e as u32) * 2047 / 2048) as u16;
17151719
}
17161720
let bucket = pos_to_bucket(pos);
1717-
self.buckets[bucket] = self.buckets[bucket].saturating_add(32);
1721+
self.buckets[bucket] = self.buckets[bucket].saturating_add(BUCKET_FIXED_POINT_ONE);
17181722
}
17191723
}
17201724
/// Decay all buckets by the given number of half-lives. Used to more aggressively remove old
@@ -1768,7 +1772,8 @@ mod bucketed_history {
17681772

17691773
// If the total valid points is smaller than 1.0 (i.e. 32 in our fixed-point scheme),
17701774
// treat it as if we were fully decayed.
1771-
if total_valid_points_tracked.checked_shr(required_decays).unwrap_or(0) < 32*32 {
1775+
const FULLY_DECAYED: u16 = BUCKET_FIXED_POINT_ONE * BUCKET_FIXED_POINT_ONE;
1776+
if total_valid_points_tracked.checked_shr(required_decays).unwrap_or(0) < FULLY_DECAYED.into() {
17721777
return None;
17731778
}
17741779

@@ -1806,7 +1811,7 @@ mod bucketed_history {
18061811
let mut highest_max_bucket_with_points = 0; // The highest max-bucket with any data
18071812
let mut total_max_points = 0; // Total points in max-buckets to consider
18081813
for (max_idx, max_bucket) in self.max_liquidity_offset_history.buckets.iter().enumerate() {
1809-
if *max_bucket >= 32 {
1814+
if *max_bucket >= BUCKET_FIXED_POINT_ONE {
18101815
highest_max_bucket_with_points = cmp::max(highest_max_bucket_with_points, max_idx);
18111816
}
18121817
total_max_points += *max_bucket as u64;

0 commit comments

Comments
 (0)