@@ -1138,14 +1138,18 @@ impl<L: Deref<Target = u64>, HT: Deref<Target = HistoricalLiquidityTracker>, T:
1138
1138
DirectedChannelLiquidity < L , HT , T > {
1139
1139
/// Returns a liquidity penalty for routing the given HTLC `amount_msat` through the channel in
1140
1140
/// 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) ;
1142
1146
let available_capacity = self . capacity_msat ;
1143
1147
let max_liquidity_msat = self . max_liquidity_msat ( ) ;
1144
1148
let min_liquidity_msat = core:: cmp:: min ( self . min_liquidity_msat ( ) , max_liquidity_msat) ;
1145
1149
1146
- let mut res = if amount_msat <= min_liquidity_msat {
1150
+ let mut res = if total_inflight_amount_msat <= min_liquidity_msat {
1147
1151
0
1148
- } else if amount_msat >= max_liquidity_msat {
1152
+ } else if total_inflight_amount_msat >= max_liquidity_msat {
1149
1153
// Equivalent to hitting the else clause below with the amount equal to the effective
1150
1154
// capacity and without any certainty on the liquidity upper bound, plus the
1151
1155
// impossibility penalty.
@@ -1155,8 +1159,10 @@ DirectedChannelLiquidity< L, HT, T> {
1155
1159
score_params. liquidity_penalty_amount_multiplier_msat )
1156
1160
. saturating_add ( score_params. considered_impossible_penalty_msat )
1157
1161
} 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
+ ) ;
1160
1166
if denominator - numerator < denominator / PRECISION_LOWER_BOUND_DENOMINATOR {
1161
1167
// If the failure probability is < 1.5625% (as 1 - numerator/denominator < 1/64),
1162
1168
// don't bother trying to use the log approximation as it gets too noisy to be
@@ -1171,7 +1177,7 @@ DirectedChannelLiquidity< L, HT, T> {
1171
1177
}
1172
1178
} ;
1173
1179
1174
- if amount_msat >= available_capacity {
1180
+ if total_inflight_amount_msat >= available_capacity {
1175
1181
// We're trying to send more than the capacity, use a max penalty.
1176
1182
res = res. saturating_add ( Self :: combined_penalty_msat ( amount_msat,
1177
1183
NEGATIVE_LOG10_UPPER_BOUND * 2048 ,
@@ -1195,8 +1201,10 @@ DirectedChannelLiquidity< L, HT, T> {
1195
1201
// If we don't have any valid points (or, once decayed, we have less than a full
1196
1202
// point), redo the non-historical calculation with no liquidity bounds tracked and
1197
1203
// 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
+ ) ;
1200
1208
let negative_log10_times_2048 =
1201
1209
log_approx:: negative_log10_times_2048 ( numerator, denominator) ;
1202
1210
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
1353
1361
_ => { } ,
1354
1362
}
1355
1363
1356
- let amount_msat = usage. amount_msat . saturating_add ( usage. inflight_htlc_msat ) ;
1357
1364
let capacity_msat = usage. effective_capacity . as_msat ( ) ;
1358
1365
self . channel_liquidities
1359
1366
. get ( scid)
1360
1367
. unwrap_or ( & ChannelLiquidity :: new ( Duration :: ZERO ) )
1361
1368
. 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)
1363
1370
. saturating_add ( anti_probing_penalty_msat)
1364
1371
. saturating_add ( base_penalty_msat)
1365
1372
}
@@ -3269,7 +3276,7 @@ mod tests {
3269
3276
short_channel_id : 42 ,
3270
3277
} ) ;
3271
3278
3272
- assert_eq ! ( scorer. channel_penalty_msat( & candidate, usage, & params) , 2050 ) ;
3279
+ assert_eq ! ( scorer. channel_penalty_msat( & candidate, usage, & params) , 2048 ) ;
3273
3280
3274
3281
let usage = ChannelUsage {
3275
3282
amount_msat : 1 ,
0 commit comments