]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Move max penalty cap in ProbabilisticScorer
authorJeffrey Czyz <jkczyz@gmail.com>
Fri, 25 Mar 2022 14:35:35 +0000 (09:35 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 25 Mar 2022 14:35:35 +0000 (09:35 -0500)
This reduce a branch in the 0 and u6::max_value cases.

lightning/src/routing/scoring.rs

index d9d0bee3ca59c23ddf61898d0d5599962fac9f06..36e1333b943b2a43b79e73365c660a2a6718f453 100644 (file)
@@ -666,20 +666,21 @@ impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiqui
        /// Returns a penalty for routing the given HTLC `amount_msat` through the channel in this
        /// direction.
        fn penalty_msat(&self, amount_msat: u64, liquidity_penalty_multiplier_msat: u64) -> u64 {
+               let max_penalty_msat = liquidity_penalty_multiplier_msat.saturating_mul(2);
                let max_liquidity_msat = self.max_liquidity_msat();
                let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
                if amount_msat > max_liquidity_msat {
-                       u64::max_value()
+                       max_penalty_msat
                } else if amount_msat <= min_liquidity_msat {
                        0
                } else {
                        let numerator = (max_liquidity_msat - amount_msat).saturating_add(1);
                        let denominator = (max_liquidity_msat - min_liquidity_msat).saturating_add(1);
-                       approx::negative_log10_times_1024(numerator, denominator)
-                               .saturating_mul(liquidity_penalty_multiplier_msat) / 1024
+                       let penalty_msat = approx::negative_log10_times_1024(numerator, denominator)
+                               .saturating_mul(liquidity_penalty_multiplier_msat) / 1024;
+                       // Upper bound the penalty to ensure some channel is selected.
+                       penalty_msat.min(max_penalty_msat)
                }
-               // Upper bound the penalty to ensure some channel is selected.
-               .min(2 * liquidity_penalty_multiplier_msat)
        }
 
        /// Returns the lower bound of the channel liquidity balance in this direction.