Skip to content

Commit 6582f29

Browse files
committed
Skip calculation steps when the liquidity penalty multipliers are 0
If the liquidity penalty multipliers in the scoring config are both 0 (as is now the default), the corresponding liquiditiy penalties will be 0. Thus, we should avoid doing the work to calculate them if we're ultimately just gonna get a value of zero anyway, which we do here.
1 parent e659002 commit 6582f29

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

lightning/src/routing/scoring.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,35 +1237,42 @@ DirectedChannelLiquidity< L, HT, T> {
12371237
let max_liquidity_msat = self.max_liquidity_msat();
12381238
let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
12391239

1240-
let mut res = if total_inflight_amount_msat <= min_liquidity_msat {
1241-
0
1242-
} else if total_inflight_amount_msat >= max_liquidity_msat {
1243-
// Equivalent to hitting the else clause below with the amount equal to the effective
1244-
// capacity and without any certainty on the liquidity upper bound, plus the
1245-
// impossibility penalty.
1246-
let negative_log10_times_2048 = NEGATIVE_LOG10_UPPER_BOUND * 2048;
1247-
Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
1248-
score_params.liquidity_penalty_multiplier_msat,
1249-
score_params.liquidity_penalty_amount_multiplier_msat)
1250-
.saturating_add(score_params.considered_impossible_penalty_msat)
1251-
} else {
1252-
let (numerator, denominator) = success_probability(
1253-
total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat,
1254-
available_capacity, score_params, false,
1255-
);
1256-
if denominator - numerator < denominator / PRECISION_LOWER_BOUND_DENOMINATOR {
1257-
// If the failure probability is < 1.5625% (as 1 - numerator/denominator < 1/64),
1258-
// don't bother trying to use the log approximation as it gets too noisy to be
1259-
// particularly helpful, instead just round down to 0.
1260-
0
1240+
let mut res = 0;
1241+
if score_params.liquidity_penalty_multiplier_msat != 0 ||
1242+
score_params.liquidity_penalty_amount_multiplier_msat != 0 {
1243+
if total_inflight_amount_msat <= min_liquidity_msat {
1244+
// If the in-flight is less than the minimum liquidity estimate, we don't assign a
1245+
// liquidity penalty at all (as the success probability is 100%).
1246+
} else if total_inflight_amount_msat >= max_liquidity_msat {
1247+
// Equivalent to hitting the else clause below with the amount equal to the effective
1248+
// capacity and without any certainty on the liquidity upper bound, plus the
1249+
// impossibility penalty.
1250+
let negative_log10_times_2048 = NEGATIVE_LOG10_UPPER_BOUND * 2048;
1251+
res = Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
1252+
score_params.liquidity_penalty_multiplier_msat,
1253+
score_params.liquidity_penalty_amount_multiplier_msat);
12611254
} else {
1262-
let negative_log10_times_2048 =
1263-
log_approx::negative_log10_times_2048(numerator, denominator);
1264-
Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
1265-
score_params.liquidity_penalty_multiplier_msat,
1266-
score_params.liquidity_penalty_amount_multiplier_msat)
1255+
let (numerator, denominator) = success_probability(
1256+
total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat,
1257+
available_capacity, score_params, false,
1258+
);
1259+
if denominator - numerator < denominator / PRECISION_LOWER_BOUND_DENOMINATOR {
1260+
// If the failure probability is < 1.5625% (as 1 - numerator/denominator < 1/64),
1261+
// don't bother trying to use the log approximation as it gets too noisy to be
1262+
// particularly helpful, instead just round down to 0.
1263+
} else {
1264+
let negative_log10_times_2048 =
1265+
log_approx::negative_log10_times_2048(numerator, denominator);
1266+
res = Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
1267+
score_params.liquidity_penalty_multiplier_msat,
1268+
score_params.liquidity_penalty_amount_multiplier_msat);
1269+
}
12671270
}
1268-
};
1271+
}
1272+
1273+
if total_inflight_amount_msat >= max_liquidity_msat {
1274+
res = res.saturating_add(score_params.considered_impossible_penalty_msat);
1275+
}
12691276

12701277
if total_inflight_amount_msat >= available_capacity {
12711278
// We're trying to send more than the capacity, use a max penalty.

0 commit comments

Comments
 (0)