Skip to content

Commit

Permalink
Skip calculation steps when the liquidity penalty multipliers are 0
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheBlueMatt committed Dec 19, 2024
1 parent 098cd32 commit 829dd26
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions lightning/src/routing/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,35 +1236,42 @@ DirectedChannelLiquidity< L, HT, T> {
let max_liquidity_msat = self.max_liquidity_msat();
let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);

let mut res = if total_inflight_amount_msat <= min_liquidity_msat {
0
} else if total_inflight_amount_msat >= max_liquidity_msat {
// Equivalent to hitting the else clause below with the amount equal to the effective
// capacity and without any certainty on the liquidity upper bound, plus the
// impossibility penalty.
let negative_log10_times_2048 = NEGATIVE_LOG10_UPPER_BOUND * 2048;
Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
score_params.liquidity_penalty_multiplier_msat,
score_params.liquidity_penalty_amount_multiplier_msat)
.saturating_add(score_params.considered_impossible_penalty_msat)
} else {
let (numerator, denominator) = success_probability(
total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat,
available_capacity, score_params, false,
);
if denominator - numerator < denominator / PRECISION_LOWER_BOUND_DENOMINATOR {
// If the failure probability is < 1.5625% (as 1 - numerator/denominator < 1/64),
// don't bother trying to use the log approximation as it gets too noisy to be
// particularly helpful, instead just round down to 0.
0
let mut res = 0;
if score_params.liquidity_penalty_multiplier_msat != 0 ||
score_params.liquidity_penalty_amount_multiplier_msat != 0 {
if total_inflight_amount_msat <= min_liquidity_msat {
// If the in-flight is less than the minimum liquidity estimate, we don't assign a
// liquidity penalty at all (as the success probability is 100%).
} else if total_inflight_amount_msat >= max_liquidity_msat {
// Equivalent to hitting the else clause below with the amount equal to the effective
// capacity and without any certainty on the liquidity upper bound, plus the
// impossibility penalty.
let negative_log10_times_2048 = NEGATIVE_LOG10_UPPER_BOUND * 2048;
res = Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
score_params.liquidity_penalty_multiplier_msat,
score_params.liquidity_penalty_amount_multiplier_msat);
} else {
let negative_log10_times_2048 =
log_approx::negative_log10_times_2048(numerator, denominator);
Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
score_params.liquidity_penalty_multiplier_msat,
score_params.liquidity_penalty_amount_multiplier_msat)
let (numerator, denominator) = success_probability(
total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat,
available_capacity, score_params, false,
);
if denominator - numerator < denominator / PRECISION_LOWER_BOUND_DENOMINATOR {
// If the failure probability is < 1.5625% (as 1 - numerator/denominator < 1/64),
// don't bother trying to use the log approximation as it gets too noisy to be
// particularly helpful, instead just round down to 0.
} else {
let negative_log10_times_2048 =
log_approx::negative_log10_times_2048(numerator, denominator);
res = Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
score_params.liquidity_penalty_multiplier_msat,
score_params.liquidity_penalty_amount_multiplier_msat);
}
}
};
}

if total_inflight_amount_msat >= max_liquidity_msat {
res = res.saturating_add(score_params.considered_impossible_penalty_msat);
}

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

0 comments on commit 829dd26

Please sign in to comment.