@@ -550,7 +550,7 @@ struct HistoricalBucketRangeTracker {
550
550
551
551
impl HistoricalBucketRangeTracker {
552
552
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 ) {
554
554
// We have 8 leaky buckets for min and max liquidity. Each bucket tracks the amount of time
555
555
// we spend in each bucket as a 16-bit fixed-point number with a 5 bit fractional part.
556
556
//
@@ -571,6 +571,12 @@ impl HistoricalBucketRangeTracker {
571
571
//
572
572
// The constants were picked experimentally, selecting a decay amount that restricts us
573
573
// 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
574
580
debug_assert ! ( bucket_idx < 8 ) ;
575
581
if bucket_idx < 8 {
576
582
for e in self . buckets . iter_mut ( ) {
@@ -1151,18 +1157,12 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
1151
1157
self . min_liquidity_offset_history . time_decay_data ( half_lives) ;
1152
1158
self . max_liquidity_offset_history . time_decay_data ( half_lives) ;
1153
1159
1154
- debug_assert ! ( * self . min_liquidity_offset_msat <= self . capacity_msat) ;
1155
1160
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
+ ) ;
1161
1163
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
+ ) ;
1166
1166
}
1167
1167
1168
1168
/// Adjusts the lower bound of the channel liquidity balance in this direction.
0 commit comments