Skip to content

Commit 848afc3

Browse files
committed
Make the ProbabilisticScorer impossibility penalty configurable
When we consider sending an HTLC over a given channel impossible due to our current knowledge of the channel's liquidity, we currently always assign a penalty of `u64::max_value()`. However, because we now refuse to retry a payment along the same path in the router itself, we can now make this value configurable. This allows users to have a relatively high knowledge decay interval without the side-effect of refusing to try the only available path in cases where a channel is intermittently available.
1 parent b42f470 commit 848afc3

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

lightning/src/routing/scoring.rs

+34-19
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,19 @@ pub struct ProbabilisticScoringParameters {
374374
///
375375
/// Default value: 250 msat
376376
pub anti_probing_penalty_msat: u64,
377+
378+
/// This penalty is applied when the amount we're attempting to send over a channel exceeds our
379+
/// current estimate of the liquidity available on a channel.
380+
///
381+
/// Note that in this case the liquidity penalties are still included to ensure such channels
382+
/// which may have enough liquidity are scored worse than channels which we do not believe have
383+
/// enough.
384+
///
385+
/// Note if you wish to avoid creating paths with such channels entirely, a penalty of
386+
/// `u64::max_value()` will guarantee that.
387+
///
388+
/// Default value: `u64::max_value()`
389+
pub considered_impossible_penalty_msat: u64,
377390
}
378391

379392
/// Accounting for channel liquidity balance uncertainty.
@@ -492,6 +505,7 @@ impl ProbabilisticScoringParameters {
492505
amount_penalty_multiplier_msat: 0,
493506
banned_nodes: HashSet::new(),
494507
anti_probing_penalty_msat: 0,
508+
considered_impossible_penalty_msat: 0,
495509
}
496510
}
497511

@@ -513,6 +527,7 @@ impl Default for ProbabilisticScoringParameters {
513527
amount_penalty_multiplier_msat: 256,
514528
banned_nodes: HashSet::new(),
515529
anti_probing_penalty_msat: 250,
530+
considered_impossible_penalty_msat: u64::max_value(),
516531
}
517532
}
518533
}
@@ -590,17 +605,12 @@ impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiqui
590605
if amount_msat <= min_liquidity_msat {
591606
0
592607
} else if amount_msat >= max_liquidity_msat {
593-
if amount_msat > max_liquidity_msat {
594-
u64::max_value()
595-
} else if max_liquidity_msat != self.capacity_msat {
596-
// Avoid using the failed channel on retry.
597-
u64::max_value()
598-
} else {
599-
// Equivalent to hitting the else clause below with the amount equal to the
600-
// effective capacity and without any certainty on the liquidity upper bound.
601-
let negative_log10_times_2048 = NEGATIVE_LOG10_UPPER_BOUND * 2048;
602-
self.combined_penalty_msat(amount_msat, negative_log10_times_2048, params)
603-
}
608+
// Equivalent to hitting the else clause below with the amount equal to the effective
609+
// capacity and without any certainty on the liquidity upper bound, plus the
610+
// impossibility penalty.
611+
let negative_log10_times_2048 = NEGATIVE_LOG10_UPPER_BOUND * 2048;
612+
self.combined_penalty_msat(amount_msat, negative_log10_times_2048, params)
613+
.saturating_add(params.considered_impossible_penalty_msat)
604614
} else {
605615
let numerator = (max_liquidity_msat - amount_msat).saturating_add(1);
606616
let denominator = (max_liquidity_msat - min_liquidity_msat).saturating_add(1);
@@ -1574,7 +1584,7 @@ mod tests {
15741584
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 0);
15751585
let usage = ChannelUsage { amount_msat: 102_400, ..usage };
15761586
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 47);
1577-
let usage = ChannelUsage { amount_msat: 1_024_000, ..usage };
1587+
let usage = ChannelUsage { amount_msat: 1_023_999, ..usage };
15781588
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 2_000);
15791589

15801590
let usage = ChannelUsage {
@@ -1604,6 +1614,7 @@ mod tests {
16041614
let network_graph = network_graph(&logger);
16051615
let params = ProbabilisticScoringParameters {
16061616
liquidity_penalty_multiplier_msat: 1_000,
1617+
considered_impossible_penalty_msat: u64::max_value(),
16071618
..ProbabilisticScoringParameters::zero_penalty()
16081619
};
16091620
let scorer = ProbabilisticScorer::new(params, &network_graph, &logger)
@@ -1695,6 +1706,7 @@ mod tests {
16951706
let network_graph = network_graph(&logger);
16961707
let params = ProbabilisticScoringParameters {
16971708
liquidity_penalty_multiplier_msat: 1_000,
1709+
considered_impossible_penalty_msat: u64::max_value(),
16981710
..ProbabilisticScoringParameters::zero_penalty()
16991711
};
17001712
let mut scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
@@ -1761,6 +1773,7 @@ mod tests {
17611773
let params = ProbabilisticScoringParameters {
17621774
liquidity_penalty_multiplier_msat: 1_000,
17631775
liquidity_offset_half_life: Duration::from_secs(10),
1776+
considered_impossible_penalty_msat: u64::max_value(),
17641777
..ProbabilisticScoringParameters::zero_penalty()
17651778
};
17661779
let mut scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
@@ -1770,10 +1783,10 @@ mod tests {
17701783
let usage = ChannelUsage {
17711784
amount_msat: 0,
17721785
inflight_htlc_msat: 0,
1773-
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: Some(1_000) },
1786+
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: Some(1_024) },
17741787
};
17751788
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 0);
1776-
let usage = ChannelUsage { amount_msat: 1_024, ..usage };
1789+
let usage = ChannelUsage { amount_msat: 1_023, ..usage };
17771790
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 2_000);
17781791

17791792
scorer.payment_path_failed(&payment_path_for_amount(768).iter().collect::<Vec<_>>(), 42);
@@ -1817,20 +1830,20 @@ mod tests {
18171830
let usage = ChannelUsage { amount_msat: 1_023, ..usage };
18181831
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 2_000);
18191832
let usage = ChannelUsage { amount_msat: 1_024, ..usage };
1820-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 2_000);
1833+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
18211834

18221835
// Fully decay liquidity upper bound.
18231836
SinceEpoch::advance(Duration::from_secs(10));
18241837
let usage = ChannelUsage { amount_msat: 0, ..usage };
18251838
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 0);
18261839
let usage = ChannelUsage { amount_msat: 1_024, ..usage };
1827-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 2_000);
1840+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
18281841

18291842
SinceEpoch::advance(Duration::from_secs(10));
18301843
let usage = ChannelUsage { amount_msat: 0, ..usage };
18311844
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 0);
18321845
let usage = ChannelUsage { amount_msat: 1_024, ..usage };
1833-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 2_000);
1846+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
18341847
}
18351848

18361849
#[test]
@@ -1915,6 +1928,7 @@ mod tests {
19151928
let params = ProbabilisticScoringParameters {
19161929
liquidity_penalty_multiplier_msat: 1_000,
19171930
liquidity_offset_half_life: Duration::from_secs(10),
1931+
considered_impossible_penalty_msat: u64::max_value(),
19181932
..ProbabilisticScoringParameters::zero_penalty()
19191933
};
19201934
let mut scorer = ProbabilisticScorer::new(params.clone(), &network_graph, &logger);
@@ -1951,6 +1965,7 @@ mod tests {
19511965
let params = ProbabilisticScoringParameters {
19521966
liquidity_penalty_multiplier_msat: 1_000,
19531967
liquidity_offset_half_life: Duration::from_secs(10),
1968+
considered_impossible_penalty_msat: u64::max_value(),
19541969
..ProbabilisticScoringParameters::zero_penalty()
19551970
};
19561971
let mut scorer = ProbabilisticScorer::new(params.clone(), &network_graph, &logger);
@@ -2110,11 +2125,11 @@ mod tests {
21102125
};
21112126

21122127
let params = ProbabilisticScoringParameters {
2113-
liquidity_penalty_multiplier_msat: 40_000,
2128+
considered_impossible_penalty_msat: 42_420,
21142129
..ProbabilisticScoringParameters::zero_penalty()
21152130
};
21162131
let scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
2117-
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 80_000);
2132+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 42_420);
21182133
}
21192134

21202135
#[test]

0 commit comments

Comments
 (0)