From 568731008ec858dc8865b4e72f2911d3e52400c8 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 9 Apr 2023 04:43:23 +0000 Subject: [PATCH] Find payment bucket in calculate_success_probability_times_billion This simply moves code which will simplify the next commit somewhat. --- lightning/src/routing/scoring.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 29650d371..5b8c28cab 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -725,7 +725,7 @@ impl HistoricalMinMaxBuckets<'_> { #[inline] fn calculate_success_probability_times_billion( - &self, now: T, last_updated: T, half_life: Duration, payment_amt_64th_bucket: u8) + &self, now: T, last_updated: T, half_life: Duration, amount_msat: u64, capacity_msat: u64) -> Option { // If historical penalties are enabled, calculate the penalty by walking the set of // historical liquidity bucket (min, max) combinations (where min_idx < max_idx) and, for @@ -748,6 +748,20 @@ impl HistoricalMinMaxBuckets<'_> { // less than 1/16th of a channel's capacity, or 1/8th if we used the top of the bucket. let mut total_valid_points_tracked = 0; + let payment_amt_64th_bucket: u8 = if amount_msat < u64::max_value() / 64 { + (amount_msat * 64 / capacity_msat.saturating_add(1)) + .try_into().unwrap_or(65) + } else { + // Only use 128-bit arithmetic when multiplication will overflow to avoid 128-bit + // division. This branch should only be hit in fuzz testing since the amount would + // need to be over 2.88 million BTC in practice. + ((amount_msat as u128) * 64 / (capacity_msat as u128).saturating_add(1)) + .try_into().unwrap_or(65) + }; + #[cfg(not(fuzzing))] + debug_assert!(payment_amt_64th_bucket <= 64); + if payment_amt_64th_bucket >= 64 { return None; } + // Check if all our buckets are zero, once decayed and treat it as if we had no data. We // don't actually use the decayed buckets, though, as that would lose precision. let (decayed_min_buckets, decayed_max_buckets, required_decays) = @@ -1078,26 +1092,13 @@ impl, BRT: Deref, if score_params.historical_liquidity_penalty_multiplier_msat != 0 || score_params.historical_liquidity_penalty_amount_multiplier_msat != 0 { - let payment_amt_64th_bucket = if amount_msat < u64::max_value() / 64 { - amount_msat * 64 / self.capacity_msat.saturating_add(1) - } else { - // Only use 128-bit arithmetic when multiplication will overflow to avoid 128-bit - // division. This branch should only be hit in fuzz testing since the amount would - // need to be over 2.88 million BTC in practice. - ((amount_msat as u128) * 64 / (self.capacity_msat as u128).saturating_add(1)) - .try_into().unwrap_or(65) - }; - #[cfg(not(fuzzing))] - debug_assert!(payment_amt_64th_bucket <= 64); - if payment_amt_64th_bucket > 64 { return res; } - let buckets = HistoricalMinMaxBuckets { min_liquidity_offset_history: &self.min_liquidity_offset_history, max_liquidity_offset_history: &self.max_liquidity_offset_history, }; if let Some(cumulative_success_prob_times_billion) = buckets .calculate_success_probability_times_billion(self.now, *self.last_updated, - self.decay_params.historical_no_updates_half_life, payment_amt_64th_bucket as u8) + self.decay_params.historical_no_updates_half_life, amount_msat, self.capacity_msat) { let historical_negative_log10_times_2048 = approx::negative_log10_times_2048(cumulative_success_prob_times_billion + 1, 1024 * 1024 * 1024); res = res.saturating_add(Self::combined_penalty_msat(amount_msat, -- 2.39.5