Correctly apply penalty bounds on the per-amount penalties
authorMatt Corallo <git@bluematt.me>
Mon, 10 Apr 2023 22:54:48 +0000 (22:54 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 23 Aug 2023 21:15:11 +0000 (21:15 +0000)
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

index 40715c84631d2dacb4c606cfce9cd46492be03b7..29650d371526bb90e52ec3855b6188798bd48258 100644 (file)
@@ -1123,15 +1123,15 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
 
        /// 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;