Skip to content

Commit 34d9290

Browse files
committed
f dry up bounds checking
1 parent 977ea33 commit 34d9290

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

lightning/src/routing/scoring.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,17 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
706706
let amt = directed_info.effective_capacity().as_msat();
707707
let dir_liq = liq.as_directed(source, target, 0, amt, self.decay_params);
708708

709-
let (min_buckets, max_buckets, _, _) = dir_liq.liquidity_history
709+
let decayed_buckets = dir_liq.liquidity_history
710710
.get_decayed_buckets(now, *dir_liq.last_updated,
711711
self.decay_params.historical_no_updates_half_life);
712712

713+
let (min_buckets, max_buckets, _, _) =
714+
if let Some(buckets) = decayed_buckets { buckets } else {
715+
// If the buckets, once decayed, end up being zero, print them out
716+
// as zeros.
717+
([0; 32], [0; 32], 0, 0)
718+
};
719+
713720
log_debug!(self.logger, core::concat!(
714721
"Liquidity from {} to {} via {} is in the range ({}, {}).\n",
715722
"\tHistorical min liquidity bucket relative probabilities:\n",
@@ -806,13 +813,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
806813
dir_liq.liquidity_history.get_decayed_buckets(
807814
dir_liq.now, *dir_liq.last_updated,
808815
self.decay_params.historical_no_updates_half_life
809-
);
810-
811-
// If the total valid points is smaller than 1.0 (i.e. 32 in our fixed-point
812-
// scheme), treat it as if we were fully decayed.
813-
if valid_points.checked_shr(required_decays).unwrap_or(0) < 32*32 {
814-
return None;
815-
}
816+
)?;
816817

817818
// Note that the liquidity buckets are an offset from the edge, so we inverse
818819
// the max order to get the probabilities from zero.
@@ -1744,7 +1745,7 @@ mod bucketed_history {
17441745
impl<D: Deref<Target = HistoricalBucketRangeTracker>> HistoricalMinMaxBuckets<D> {
17451746
#[inline]
17461747
pub(super) fn get_decayed_buckets<T: Time>(&self, now: T, last_updated: T, half_life: Duration)
1747-
-> ([u16; 32], [u16; 32], u64, u32) {
1748+
-> Option<([u16; 32], [u16; 32], u64, u32)> {
17481749
let required_decays = now.duration_since(last_updated).as_secs()
17491750
.checked_div(half_life.as_secs())
17501751
.map_or(u32::max_value(), |decays| cmp::min(decays, u32::max_value() as u64) as u32);
@@ -1756,11 +1757,17 @@ mod bucketed_history {
17561757
}
17571758
}
17581759

1760+
// If the total valid points is smaller than 1.0 (i.e. 32 in our fixed-point scheme),
1761+
// treat it as if we were fully decayed.
1762+
if total_valid_points_tracked.checked_shr(required_decays).unwrap_or(0) < 32*32 {
1763+
return None;
1764+
}
1765+
17591766
let mut min_buckets = *self.min_liquidity_offset_history;
17601767
min_buckets.time_decay_data(required_decays);
17611768
let mut max_buckets = *self.max_liquidity_offset_history;
17621769
max_buckets.time_decay_data(required_decays);
1763-
(min_buckets.buckets, max_buckets.buckets, total_valid_points_tracked, required_decays)
1770+
Some((min_buckets.buckets, max_buckets.buckets, total_valid_points_tracked, required_decays))
17641771
}
17651772

17661773
#[inline]
@@ -1778,13 +1785,7 @@ mod bucketed_history {
17781785
// Check if all our buckets are zero, once decayed and treat it as if we had no data. We
17791786
// don't actually use the decayed buckets, though, as that would lose precision.
17801787
let (decayed_min_buckets, decayed_max_buckets, total_valid_points_tracked, required_decays)
1781-
= self.get_decayed_buckets(now, last_updated, half_life);
1782-
1783-
// If the total valid points is smaller than 1.0 (i.e. 32 in our fixed-point scheme), treat
1784-
// it as if we were fully decayed.
1785-
if total_valid_points_tracked.checked_shr(required_decays).unwrap_or(0) < 32*32 {
1786-
return None;
1787-
}
1788+
= self.get_decayed_buckets(now, last_updated, half_life)?;
17881789

17891790
let mut cumulative_success_prob_times_billion = 0;
17901791
// Special-case the 0th min bucket - it generally means we failed a payment, so only

0 commit comments

Comments
 (0)