From c4947acaec808ff3568e3c168ac942d9cfcdb9b0 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 10 Apr 2023 22:54:48 +0000 Subject: [PATCH] Correctly apply penalty bounds on the per-amount penalties When we attempt to score a channel which has a success probability very low, we may have a log well above our cut-off of two. For the liquidity penalties this works great, we bound it by `NEGATIVE_LOG10_UPPER_BOUND` and `min` the two scores. For the amount liquidity penalty we didn't do any `min`ing at all. This fix is to min the log itself first and then reuse the min'd log in both calculations. --- lightning/src/routing/scoring.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 40715c846..29650d371 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -1123,15 +1123,15 @@ impl, BRT: Deref, /// Computes the liquidity penalty from the penalty multipliers. #[inline(always)] - fn combined_penalty_msat(amount_msat: u64, negative_log10_times_2048: u64, + fn combined_penalty_msat(amount_msat: u64, mut negative_log10_times_2048: u64, liquidity_penalty_multiplier_msat: u64, liquidity_penalty_amount_multiplier_msat: u64, ) -> u64 { - let liquidity_penalty_msat = { - // Upper bound the liquidity penalty to ensure some channel is selected. - let multiplier_msat = liquidity_penalty_multiplier_msat; - let max_penalty_msat = multiplier_msat.saturating_mul(NEGATIVE_LOG10_UPPER_BOUND); - (negative_log10_times_2048.saturating_mul(multiplier_msat) / 2048).min(max_penalty_msat) - }; + negative_log10_times_2048 = + negative_log10_times_2048.min(NEGATIVE_LOG10_UPPER_BOUND * 2048); + + // Upper bound the liquidity penalty to ensure some channel is selected. + let liquidity_penalty_msat = negative_log10_times_2048 + .saturating_mul(liquidity_penalty_multiplier_msat) / 2048; let amount_penalty_msat = negative_log10_times_2048 .saturating_mul(liquidity_penalty_amount_multiplier_msat) .saturating_mul(amount_msat) / 2048 / AMOUNT_PENALTY_DIVISOR; -- 2.39.5