Skip to content

Commit 06333e1

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 81b7187 commit 06333e1

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
@@ -1690,6 +1690,10 @@ mod bucketed_history {
16901690
buckets: [u16; 32],
16911691
}
16921692

1693+
/// Buckets are stored in fixed point numbers with a 5 bit fractional part. Thus, the value
1694+
/// "one" is 32, or this constant.
1695+
pub const BUCKET_FIXED_POINT_ONE: u16 = 32;
1696+
16931697
impl HistoricalBucketRangeTracker {
16941698
pub(super) fn new() -> Self { Self { buckets: [0; 32] } }
16951699
pub(super) fn track_datapoint(&mut self, liquidity_offset_msat: u64, capacity_msat: u64) {
@@ -1720,7 +1724,7 @@ mod bucketed_history {
17201724
*e = ((*e as u32) * 2047 / 2048) as u16;
17211725
}
17221726
let bucket = pos_to_bucket(pos);
1723-
self.buckets[bucket] = self.buckets[bucket].saturating_add(32);
1727+
self.buckets[bucket] = self.buckets[bucket].saturating_add(BUCKET_FIXED_POINT_ONE);
17241728
}
17251729
}
17261730
/// Decay all buckets by the given number of half-lives. Used to more aggressively remove old
@@ -1764,7 +1768,8 @@ mod bucketed_history {
17641768

17651769
// If the total valid points is smaller than 1.0 (i.e. 32 in our fixed-point scheme),
17661770
// treat it as if we were fully decayed.
1767-
if total_valid_points_tracked.checked_shr(required_decays).unwrap_or(0) < 32*32 {
1771+
const FULLY_DECAYED: u16 = BUCKET_FIXED_POINT_ONE * BUCKET_FIXED_POINT_ONE;
1772+
if total_valid_points_tracked.checked_shr(required_decays).unwrap_or(0) < FULLY_DECAYED.into() {
17681773
return None;
17691774
}
17701775

@@ -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)