Skip to content

Commit a58b81a

Browse files
committed
DRY up historical bucket_idx calculation
1 parent 10cfe5c commit a58b81a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

lightning/src/routing/scoring.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ struct HistoricalBucketRangeTracker {
550550

551551
impl HistoricalBucketRangeTracker {
552552
fn new() -> Self { Self { buckets: [0; 8] } }
553-
fn track_datapoint(&mut self, bucket_idx: u8) {
553+
fn track_datapoint(&mut self, liquidity_offset_msat: u64, capacity_msat: u64) {
554554
// We have 8 leaky buckets for min and max liquidity. Each bucket tracks the amount of time
555555
// we spend in each bucket as a 16-bit fixed-point number with a 5 bit fractional part.
556556
//
@@ -571,6 +571,12 @@ impl HistoricalBucketRangeTracker {
571571
//
572572
// The constants were picked experimentally, selecting a decay amount that restricts us
573573
// from overflowing buckets without having to cap them manually.
574+
575+
// Ensure the bucket index is in the range [0, 7], even if the liquidity offset is zero or
576+
// the channel's capacity, though the second should generally never happen.
577+
debug_assert!(liquidity_offset_msat <= capacity_msat);
578+
let bucket_idx: u8 = (liquidity_offset_msat.saturating_sub(1) * 8 / capacity_msat)
579+
.try_into().unwrap_or(32); // 32 is bogus for 8 buckets, and will be ignored
574580
debug_assert!(bucket_idx < 8);
575581
if bucket_idx < 8 {
576582
for e in self.buckets.iter_mut() {
@@ -1151,18 +1157,12 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
11511157
self.min_liquidity_offset_history.time_decay_data(half_lives);
11521158
self.max_liquidity_offset_history.time_decay_data(half_lives);
11531159

1154-
debug_assert!(*self.min_liquidity_offset_msat <= self.capacity_msat);
11551160
self.min_liquidity_offset_history.track_datapoint(
1156-
// Ensure the bucket index we pass is in the range [0, 7], even if the liquidity offset
1157-
// is zero or the channel's capacity, though the second should generally never happen.
1158-
(self.min_liquidity_offset_msat.saturating_sub(1) * 8 / self.capacity_msat)
1159-
.try_into().unwrap_or(32)); // 32 is bogus for 8 buckets, and will be ignored
1160-
debug_assert!(*self.max_liquidity_offset_msat <= self.capacity_msat);
1161+
*self.min_liquidity_offset_msat, self.capacity_msat
1162+
);
11611163
self.max_liquidity_offset_history.track_datapoint(
1162-
// Ensure the bucket index we pass is in the range [0, 7], even if the liquidity offset
1163-
// is zero or the channel's capacity, though the second should generally never happen.
1164-
(self.max_liquidity_offset_msat.saturating_sub(1) * 8 / self.capacity_msat)
1165-
.try_into().unwrap_or(32)); // 32 is bogus for 8 buckets, and will be ignored
1164+
*self.max_liquidity_offset_msat, self.capacity_msat
1165+
);
11661166
}
11671167

11681168
/// Adjusts the lower bound of the channel liquidity balance in this direction.

0 commit comments

Comments
 (0)