Skip to content

Commit 8f7cbc3

Browse files
committed
Don't over-penalize channels with inflight HTLCs
Commit df52da7 modified ProbabilisticScorer to apply some penalty amount multipliers (e.g., liquidity_penalty_amount_multiplier_msat) to the total amount flowing over the channel (i.e., including inflight HTLCs), not just the payment in question. This led to over-penalizing in-use channels. Instead, only apply the total amount when calculating success probability.
1 parent 605952c commit 8f7cbc3

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

lightning/src/routing/scoring.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,14 +1138,18 @@ impl<L: Deref<Target = u64>, HT: Deref<Target = HistoricalLiquidityTracker>, T:
11381138
DirectedChannelLiquidity< L, HT, T> {
11391139
/// Returns a liquidity penalty for routing the given HTLC `amount_msat` through the channel in
11401140
/// this direction.
1141-
fn penalty_msat(&self, amount_msat: u64, score_params: &ProbabilisticScoringFeeParameters) -> u64 {
1141+
fn penalty_msat(
1142+
&self, amount_msat: u64, inflight_htlc_msat: u64,
1143+
score_params: &ProbabilisticScoringFeeParameters,
1144+
) -> u64 {
1145+
let total_inflight_amount_msat = amount_msat.saturating_add(inflight_htlc_msat);
11421146
let available_capacity = self.capacity_msat;
11431147
let max_liquidity_msat = self.max_liquidity_msat();
11441148
let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
11451149

1146-
let mut res = if amount_msat <= min_liquidity_msat {
1150+
let mut res = if total_inflight_amount_msat <= min_liquidity_msat {
11471151
0
1148-
} else if amount_msat >= max_liquidity_msat {
1152+
} else if total_inflight_amount_msat >= max_liquidity_msat {
11491153
// Equivalent to hitting the else clause below with the amount equal to the effective
11501154
// capacity and without any certainty on the liquidity upper bound, plus the
11511155
// impossibility penalty.
@@ -1155,8 +1159,10 @@ DirectedChannelLiquidity< L, HT, T> {
11551159
score_params.liquidity_penalty_amount_multiplier_msat)
11561160
.saturating_add(score_params.considered_impossible_penalty_msat)
11571161
} else {
1158-
let (numerator, denominator) = success_probability(amount_msat,
1159-
min_liquidity_msat, max_liquidity_msat, available_capacity, score_params, false);
1162+
let (numerator, denominator) = success_probability(
1163+
total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat,
1164+
available_capacity, score_params, false,
1165+
);
11601166
if denominator - numerator < denominator / PRECISION_LOWER_BOUND_DENOMINATOR {
11611167
// If the failure probability is < 1.5625% (as 1 - numerator/denominator < 1/64),
11621168
// don't bother trying to use the log approximation as it gets too noisy to be
@@ -1171,7 +1177,7 @@ DirectedChannelLiquidity< L, HT, T> {
11711177
}
11721178
};
11731179

1174-
if amount_msat >= available_capacity {
1180+
if total_inflight_amount_msat >= available_capacity {
11751181
// We're trying to send more than the capacity, use a max penalty.
11761182
res = res.saturating_add(Self::combined_penalty_msat(amount_msat,
11771183
NEGATIVE_LOG10_UPPER_BOUND * 2048,
@@ -1195,8 +1201,10 @@ DirectedChannelLiquidity< L, HT, T> {
11951201
// If we don't have any valid points (or, once decayed, we have less than a full
11961202
// point), redo the non-historical calculation with no liquidity bounds tracked and
11971203
// the historical penalty multipliers.
1198-
let (numerator, denominator) = success_probability(amount_msat, 0,
1199-
available_capacity, available_capacity, score_params, true);
1204+
let (numerator, denominator) = success_probability(
1205+
total_inflight_amount_msat, 0, available_capacity, available_capacity,
1206+
score_params, true,
1207+
);
12001208
let negative_log10_times_2048 =
12011209
log_approx::negative_log10_times_2048(numerator, denominator);
12021210
res = res.saturating_add(Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
@@ -1353,13 +1361,12 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ScoreLookUp for Probabilistic
13531361
_ => {},
13541362
}
13551363

1356-
let amount_msat = usage.amount_msat.saturating_add(usage.inflight_htlc_msat);
13571364
let capacity_msat = usage.effective_capacity.as_msat();
13581365
self.channel_liquidities
13591366
.get(scid)
13601367
.unwrap_or(&ChannelLiquidity::new(Duration::ZERO))
13611368
.as_directed(&source, &target, capacity_msat)
1362-
.penalty_msat(amount_msat, score_params)
1369+
.penalty_msat(usage.amount_msat, usage.inflight_htlc_msat, score_params)
13631370
.saturating_add(anti_probing_penalty_msat)
13641371
.saturating_add(base_penalty_msat)
13651372
}
@@ -3269,7 +3276,7 @@ mod tests {
32693276
short_channel_id: 42,
32703277
});
32713278

3272-
assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 2050);
3279+
assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 2048);
32733280

32743281
let usage = ChannelUsage {
32753282
amount_msat: 1,

0 commit comments

Comments
 (0)