Skip to content

Commit 86976e0

Browse files
committed
Don't rely on calculate_success_probability* to handle amt > cap
Currently we let an `htlc_amount >= channel_capacity` pass through from `penalty_msat` to `calculate_success_probability_times_billion`, but only if its only marginally bigger (less than 65/64ths). This is fine as `calculate_success_probability_times_billion` handles bogus values just fine (it will always return a zero probability in such cases). However, this is risky, and in fact breaks in the coming commits, so instead check it before ever calling through to the historical bucket probability calculations.
1 parent bada713 commit 86976e0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lightning/src/routing/scoring.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
10351035
/// Returns a liquidity penalty for routing the given HTLC `amount_msat` through the channel in
10361036
/// this direction.
10371037
fn penalty_msat(&self, amount_msat: u64, score_params: &ProbabilisticScoringFeeParameters) -> u64 {
1038+
let available_capacity = self.available_capacity();
10381039
let max_liquidity_msat = self.max_liquidity_msat();
10391040
let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
10401041

@@ -1066,6 +1067,15 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
10661067
}
10671068
};
10681069

1070+
if amount_msat >= available_capacity {
1071+
// We're trying to send more than the capacity, use a max penalty.
1072+
res = res.saturating_add(Self::combined_penalty_msat(amount_msat,
1073+
NEGATIVE_LOG10_UPPER_BOUND * 2048,
1074+
score_params.historical_liquidity_penalty_multiplier_msat,
1075+
score_params.historical_liquidity_penalty_amount_multiplier_msat));
1076+
return res;
1077+
}
1078+
10691079
if score_params.historical_liquidity_penalty_multiplier_msat != 0 ||
10701080
score_params.historical_liquidity_penalty_amount_multiplier_msat != 0 {
10711081
let payment_amt_64th_bucket = if amount_msat < u64::max_value() / 64 {
@@ -1130,17 +1140,20 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
11301140
}
11311141

11321142
/// Returns the lower bound of the channel liquidity balance in this direction.
1143+
#[inline(always)]
11331144
fn min_liquidity_msat(&self) -> u64 {
11341145
self.decayed_offset_msat(*self.min_liquidity_offset_msat)
11351146
}
11361147

11371148
/// Returns the upper bound of the channel liquidity balance in this direction.
1149+
#[inline(always)]
11381150
fn max_liquidity_msat(&self) -> u64 {
11391151
self.available_capacity()
11401152
.saturating_sub(self.decayed_offset_msat(*self.max_liquidity_offset_msat))
11411153
}
11421154

11431155
/// Returns the capacity minus the in-flight HTLCs in this direction.
1156+
#[inline(always)]
11441157
fn available_capacity(&self) -> u64 {
11451158
self.capacity_msat.saturating_sub(self.inflight_htlc_msat)
11461159
}
@@ -2858,12 +2871,14 @@ mod tests {
28582871
assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
28592872
Some(([0; 8], [0; 8])));
28602873

2861-
let usage = ChannelUsage {
2874+
let mut usage = ChannelUsage {
28622875
amount_msat: 100,
28632876
inflight_htlc_msat: 1024,
28642877
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_024 },
28652878
};
28662879
scorer.payment_path_failed(&payment_path_for_amount(1), 42);
2880+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage, &params), 2048);
2881+
usage.inflight_htlc_msat = 0;
28672882
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage, &params), 409);
28682883

28692884
let usage = ChannelUsage {

0 commit comments

Comments
 (0)