Skip to content

Commit 866588c

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 866588c

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

lightning/src/routing/router.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7392,9 +7392,12 @@ mod tests {
73927392
.unwrap();
73937393
let random_seed_bytes = [42; 32];
73947394

7395-
// 100,000 sats is less than the available liquidity on each channel, set above.
7395+
// 75,000 sats is less than the available liquidity on each channel, set above, when
7396+
// applying max_channel_saturation_power_of_half. This value also ensures the cost of paths
7397+
// considered when applying max_channel_saturation_power_of_half is less than the cost of
7398+
// those when it is not applied.
73967399
let route_params = RouteParameters::from_payment_params_and_value(
7397-
payment_params, 100_000_000);
7400+
payment_params, 75_000_000);
73987401
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
73997402
Arc::clone(&logger), &scorer, &ProbabilisticScoringFeeParameters::default(), &random_seed_bytes).unwrap();
74007403
assert_eq!(route.paths.len(), 2);

lightning/src/routing/scoring.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -524,14 +524,14 @@ pub struct ProbabilisticScoringFeeParameters {
524524
/// [`liquidity_offset_half_life`]: ProbabilisticScoringDecayParameters::liquidity_offset_half_life
525525
pub liquidity_penalty_multiplier_msat: u64,
526526

527-
/// A multiplier used in conjunction with the total amount flowing over a channel and the
528-
/// negative `log10` of the channel's success probability for the payment, as determined by our
529-
/// latest estimates of the channel's liquidity, to determine the amount penalty.
527+
/// A multiplier used in conjunction with the payment amount and the negative `log10` of the
528+
/// channel's success probability for the total amount flowing over a channel, as determined by
529+
/// our latest estimates of the channel's liquidity, to determine the amount penalty.
530530
///
531531
/// The purpose of the amount penalty is to avoid having fees dominate the channel cost (i.e.,
532532
/// fees plus penalty) for large payments. The penalty is computed as the product of this
533-
/// multiplier and `2^20`ths of the amount flowing over this channel, weighted by the negative
534-
/// `log10` of the success probability.
533+
/// multiplier and `2^20`ths of the payment amount, weighted by the negative `log10` of the
534+
/// success probability.
535535
///
536536
/// `-log10(success_probability) * liquidity_penalty_amount_multiplier_msat * amount_msat / 2^20`
537537
///
@@ -560,15 +560,14 @@ pub struct ProbabilisticScoringFeeParameters {
560560
/// [`liquidity_penalty_multiplier_msat`]: Self::liquidity_penalty_multiplier_msat
561561
pub historical_liquidity_penalty_multiplier_msat: u64,
562562

563-
/// A multiplier used in conjunction with the total amount flowing over a channel and the
564-
/// negative `log10` of the channel's success probability for the payment, as determined based
565-
/// on the history of our estimates of the channel's available liquidity, to determine a
563+
/// A multiplier used in conjunction with the payment amount and the negative `log10` of the
564+
/// channel's success probability for the total amount flowing over a channel, as determined
565+
/// based on the history of our estimates of the channel's available liquidity, to determine a
566566
/// penalty.
567567
///
568568
/// The purpose of the amount penalty is to avoid having fees dominate the channel cost for
569569
/// large payments. The penalty is computed as the product of this multiplier and `2^20`ths
570-
/// of the amount flowing over this channel, weighted by the negative `log10` of the success
571-
/// probability.
570+
/// of the payment amount, weighted by the negative `log10` of the success probability.
572571
///
573572
/// This penalty is similar to [`liquidity_penalty_amount_multiplier_msat`], however, instead
574573
/// of using only our latest estimate for the current liquidity available in the channel, it
@@ -1138,14 +1137,18 @@ impl<L: Deref<Target = u64>, HT: Deref<Target = HistoricalLiquidityTracker>, T:
11381137
DirectedChannelLiquidity< L, HT, T> {
11391138
/// Returns a liquidity penalty for routing the given HTLC `amount_msat` through the channel in
11401139
/// this direction.
1141-
fn penalty_msat(&self, amount_msat: u64, score_params: &ProbabilisticScoringFeeParameters) -> u64 {
1140+
fn penalty_msat(
1141+
&self, amount_msat: u64, inflight_htlc_msat: u64,
1142+
score_params: &ProbabilisticScoringFeeParameters,
1143+
) -> u64 {
1144+
let total_inflight_amount_msat = amount_msat.saturating_add(inflight_htlc_msat);
11421145
let available_capacity = self.capacity_msat;
11431146
let max_liquidity_msat = self.max_liquidity_msat();
11441147
let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
11451148

1146-
let mut res = if amount_msat <= min_liquidity_msat {
1149+
let mut res = if total_inflight_amount_msat <= min_liquidity_msat {
11471150
0
1148-
} else if amount_msat >= max_liquidity_msat {
1151+
} else if total_inflight_amount_msat >= max_liquidity_msat {
11491152
// Equivalent to hitting the else clause below with the amount equal to the effective
11501153
// capacity and without any certainty on the liquidity upper bound, plus the
11511154
// impossibility penalty.
@@ -1155,8 +1158,10 @@ DirectedChannelLiquidity< L, HT, T> {
11551158
score_params.liquidity_penalty_amount_multiplier_msat)
11561159
.saturating_add(score_params.considered_impossible_penalty_msat)
11571160
} else {
1158-
let (numerator, denominator) = success_probability(amount_msat,
1159-
min_liquidity_msat, max_liquidity_msat, available_capacity, score_params, false);
1161+
let (numerator, denominator) = success_probability(
1162+
total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat,
1163+
available_capacity, score_params, false,
1164+
);
11601165
if denominator - numerator < denominator / PRECISION_LOWER_BOUND_DENOMINATOR {
11611166
// If the failure probability is < 1.5625% (as 1 - numerator/denominator < 1/64),
11621167
// don't bother trying to use the log approximation as it gets too noisy to be
@@ -1171,7 +1176,7 @@ DirectedChannelLiquidity< L, HT, T> {
11711176
}
11721177
};
11731178

1174-
if amount_msat >= available_capacity {
1179+
if total_inflight_amount_msat >= available_capacity {
11751180
// We're trying to send more than the capacity, use a max penalty.
11761181
res = res.saturating_add(Self::combined_penalty_msat(amount_msat,
11771182
NEGATIVE_LOG10_UPPER_BOUND * 2048,
@@ -1184,7 +1189,8 @@ DirectedChannelLiquidity< L, HT, T> {
11841189
score_params.historical_liquidity_penalty_amount_multiplier_msat != 0 {
11851190
if let Some(cumulative_success_prob_times_billion) = self.liquidity_history
11861191
.calculate_success_probability_times_billion(
1187-
score_params, amount_msat, self.capacity_msat)
1192+
score_params, total_inflight_amount_msat, self.capacity_msat
1193+
)
11881194
{
11891195
let historical_negative_log10_times_2048 =
11901196
log_approx::negative_log10_times_2048(cumulative_success_prob_times_billion + 1, 1024 * 1024 * 1024);
@@ -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)