Inline point count divide
[rust-lightning] / lightning / src / routing / scoring.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Utilities for scoring payment channels.
11 //!
12 //! [`ProbabilisticScorer`] may be given to [`find_route`] to score payment channels during path
13 //! finding when a custom [`ScoreLookUp`] implementation is not needed.
14 //!
15 //! # Example
16 //!
17 //! ```
18 //! # extern crate bitcoin;
19 //! #
20 //! # use lightning::routing::gossip::NetworkGraph;
21 //! # use lightning::routing::router::{RouteParameters, find_route};
22 //! # use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters};
23 //! # use lightning::sign::KeysManager;
24 //! # use lightning::util::logger::{Logger, Record};
25 //! # use bitcoin::secp256k1::PublicKey;
26 //! #
27 //! # struct FakeLogger {};
28 //! # impl Logger for FakeLogger {
29 //! #     fn log(&self, record: Record) { unimplemented!() }
30 //! # }
31 //! # fn find_scored_route(payer: PublicKey, route_params: RouteParameters, network_graph: NetworkGraph<&FakeLogger>) {
32 //! # let logger = FakeLogger {};
33 //! #
34 //! // Use the default channel penalties.
35 //! let params = ProbabilisticScoringFeeParameters::default();
36 //! let decay_params = ProbabilisticScoringDecayParameters::default();
37 //! let scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
38 //!
39 //! // Or use custom channel penalties.
40 //! let params = ProbabilisticScoringFeeParameters {
41 //!     liquidity_penalty_multiplier_msat: 2 * 1000,
42 //!     ..ProbabilisticScoringFeeParameters::default()
43 //! };
44 //! let decay_params = ProbabilisticScoringDecayParameters::default();
45 //! let scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
46 //! # let random_seed_bytes = [42u8; 32];
47 //!
48 //! let route = find_route(&payer, &route_params, &network_graph, None, &logger, &scorer, &params, &random_seed_bytes);
49 //! # }
50 //! ```
51 //!
52 //! # Note
53 //!
54 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
55 //! different types and thus is undefined.
56 //!
57 //! [`find_route`]: crate::routing::router::find_route
58
59 use crate::ln::msgs::DecodeError;
60 use crate::routing::gossip::{EffectiveCapacity, NetworkGraph, NodeId};
61 use crate::routing::router::{Path, CandidateRouteHop};
62 use crate::routing::log_approx;
63 use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
64 use crate::util::logger::Logger;
65
66 use crate::prelude::*;
67 use core::{cmp, fmt};
68 use core::cell::{RefCell, RefMut, Ref};
69 use core::convert::TryInto;
70 use core::ops::{Deref, DerefMut};
71 use core::time::Duration;
72 use crate::io::{self, Read};
73 use crate::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
74
75 /// We define Score ever-so-slightly differently based on whether we are being built for C bindings
76 /// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
77 /// no problem - you move a `Score` that implements `Writeable` into a `Mutex`, lock it, and now
78 /// you have the original, concrete, `Score` type, which presumably implements `Writeable`.
79 ///
80 /// For C users, once you've moved the `Score` into a `LockableScore` all you have after locking it
81 /// is an opaque trait object with an opaque pointer with no type info. Users could take the unsafe
82 /// approach of blindly casting that opaque pointer to a concrete type and calling `Writeable` from
83 /// there, but other languages downstream of the C bindings (e.g. Java) can't even do that.
84 /// Instead, we really want `Score` and `LockableScore` to implement `Writeable` directly, which we
85 /// do here by defining `Score` differently for `cfg(c_bindings)`.
86 macro_rules! define_score { ($($supertrait: path)*) => {
87 /// An interface used to score payment channels for path finding.
88 ///
89 /// `ScoreLookUp` is used to determine the penalty for a given channel.
90 ///
91 /// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
92 pub trait ScoreLookUp {
93         /// A configurable type which should contain various passed-in parameters for configuring the scorer,
94         /// on a per-routefinding-call basis through to the scorer methods,
95         /// which are used to determine the parameters for the suitability of channels for use.
96         type ScoreParams;
97         /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
98         /// given channel in the direction from `source` to `target`.
99         ///
100         /// The channel's capacity (less any other MPP parts that are also being considered for use in
101         /// the same payment) is given by `capacity_msat`. It may be determined from various sources
102         /// such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
103         /// [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
104         /// Thus, implementations should be overflow-safe.
105         fn channel_penalty_msat(
106                 &self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams
107         ) -> u64;
108 }
109
110 /// `ScoreUpdate` is used to update the scorer's internal state after a payment attempt.
111 pub trait ScoreUpdate {
112         /// Handles updating channel penalties after failing to route through a channel.
113         fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration);
114
115         /// Handles updating channel penalties after successfully routing along a path.
116         fn payment_path_successful(&mut self, path: &Path, duration_since_epoch: Duration);
117
118         /// Handles updating channel penalties after a probe over the given path failed.
119         fn probe_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration);
120
121         /// Handles updating channel penalties after a probe over the given path succeeded.
122         fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration);
123
124         /// Scorers may wish to reduce their certainty of channel liquidity information over time.
125         /// Thus, this method is provided to allow scorers to observe the passage of time - the holder
126         /// of this object should call this method regularly (generally via the
127         /// `lightning-background-processor` crate).
128         fn decay_liquidity_certainty(&mut self, duration_since_epoch: Duration);
129 }
130
131 /// A trait which can both lookup and update routing channel penalty scores.
132 ///
133 /// This is used in places where both bounds are required and implemented for all types which
134 /// implement [`ScoreLookUp`] and [`ScoreUpdate`].
135 ///
136 /// Bindings users may need to manually implement this for their custom scoring implementations.
137 pub trait Score : ScoreLookUp + ScoreUpdate $(+ $supertrait)* {}
138
139 #[cfg(not(c_bindings))]
140 impl<T: ScoreLookUp + ScoreUpdate $(+ $supertrait)*> Score for T {}
141
142 #[cfg(not(c_bindings))]
143 impl<S: ScoreLookUp, T: Deref<Target=S>> ScoreLookUp for T {
144         type ScoreParams = S::ScoreParams;
145         fn channel_penalty_msat(
146                 &self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams
147         ) -> u64 {
148                 self.deref().channel_penalty_msat(candidate, usage, score_params)
149         }
150 }
151
152 #[cfg(not(c_bindings))]
153 impl<S: ScoreUpdate, T: DerefMut<Target=S>> ScoreUpdate for T {
154         fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration) {
155                 self.deref_mut().payment_path_failed(path, short_channel_id, duration_since_epoch)
156         }
157
158         fn payment_path_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
159                 self.deref_mut().payment_path_successful(path, duration_since_epoch)
160         }
161
162         fn probe_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration) {
163                 self.deref_mut().probe_failed(path, short_channel_id, duration_since_epoch)
164         }
165
166         fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
167                 self.deref_mut().probe_successful(path, duration_since_epoch)
168         }
169
170         fn decay_liquidity_certainty(&mut self, duration_since_epoch: Duration) {
171                 self.deref_mut().decay_liquidity_certainty(duration_since_epoch)
172         }
173 }
174 } }
175
176 #[cfg(c_bindings)]
177 define_score!(Writeable);
178
179 #[cfg(not(c_bindings))]
180 define_score!();
181
182 /// A scorer that is accessed under a lock.
183 ///
184 /// Needed so that calls to [`ScoreLookUp::channel_penalty_msat`] in [`find_route`] can be made while
185 /// having shared ownership of a scorer but without requiring internal locking in [`ScoreUpdate`]
186 /// implementations. Internal locking would be detrimental to route finding performance and could
187 /// result in [`ScoreLookUp::channel_penalty_msat`] returning a different value for the same channel.
188 ///
189 /// [`find_route`]: crate::routing::router::find_route
190 pub trait LockableScore<'a> {
191         /// The [`ScoreUpdate`] type.
192         type ScoreUpdate: 'a + ScoreUpdate;
193         /// The [`ScoreLookUp`] type.
194         type ScoreLookUp: 'a + ScoreLookUp;
195
196         /// The write locked [`ScoreUpdate`] type.
197         type WriteLocked: DerefMut<Target = Self::ScoreUpdate> + Sized;
198
199         /// The read locked [`ScoreLookUp`] type.
200         type ReadLocked: Deref<Target = Self::ScoreLookUp> + Sized;
201
202         /// Returns read locked scorer.
203         fn read_lock(&'a self) -> Self::ReadLocked;
204
205         /// Returns write locked scorer.
206         fn write_lock(&'a self) -> Self::WriteLocked;
207 }
208
209 /// Refers to a scorer that is accessible under lock and also writeable to disk
210 ///
211 /// We need this trait to be able to pass in a scorer to `lightning-background-processor` that will enable us to
212 /// use the Persister to persist it.
213 pub trait WriteableScore<'a>: LockableScore<'a> + Writeable {}
214
215 #[cfg(not(c_bindings))]
216 impl<'a, T> WriteableScore<'a> for T where T: LockableScore<'a> + Writeable {}
217 #[cfg(not(c_bindings))]
218 impl<'a, T: Score + 'a> LockableScore<'a> for Mutex<T> {
219         type ScoreUpdate = T;
220         type ScoreLookUp = T;
221
222         type WriteLocked = MutexGuard<'a, Self::ScoreUpdate>;
223         type ReadLocked = MutexGuard<'a, Self::ScoreLookUp>;
224
225         fn read_lock(&'a self) -> Self::ReadLocked {
226                 Mutex::lock(self).unwrap()
227         }
228
229         fn write_lock(&'a self) -> Self::WriteLocked {
230                 Mutex::lock(self).unwrap()
231         }
232 }
233
234 #[cfg(not(c_bindings))]
235 impl<'a, T: Score + 'a> LockableScore<'a> for RefCell<T> {
236         type ScoreUpdate = T;
237         type ScoreLookUp = T;
238
239         type WriteLocked = RefMut<'a, Self::ScoreUpdate>;
240         type ReadLocked = Ref<'a, Self::ScoreLookUp>;
241
242         fn write_lock(&'a self) -> Self::WriteLocked {
243                 self.borrow_mut()
244         }
245
246         fn read_lock(&'a self) -> Self::ReadLocked {
247                 self.borrow()
248         }
249 }
250
251 #[cfg(not(c_bindings))]
252 impl<'a, T: Score + 'a> LockableScore<'a> for RwLock<T> {
253         type ScoreUpdate = T;
254         type ScoreLookUp = T;
255
256         type WriteLocked = RwLockWriteGuard<'a, Self::ScoreLookUp>;
257         type ReadLocked = RwLockReadGuard<'a, Self::ScoreUpdate>;
258
259         fn read_lock(&'a self) -> Self::ReadLocked {
260                 RwLock::read(self).unwrap()
261         }
262
263         fn write_lock(&'a self) -> Self::WriteLocked {
264                 RwLock::write(self).unwrap()
265         }
266 }
267
268 #[cfg(c_bindings)]
269 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
270 pub struct MultiThreadedLockableScore<T: Score> {
271         score: RwLock<T>,
272 }
273
274 #[cfg(c_bindings)]
275 impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore<T> {
276         type ScoreUpdate = T;
277         type ScoreLookUp = T;
278         type WriteLocked = MultiThreadedScoreLockWrite<'a, Self::ScoreUpdate>;
279         type ReadLocked = MultiThreadedScoreLockRead<'a, Self::ScoreLookUp>;
280
281         fn read_lock(&'a self) -> Self::ReadLocked {
282                 MultiThreadedScoreLockRead(self.score.read().unwrap())
283         }
284
285         fn write_lock(&'a self) -> Self::WriteLocked {
286                 MultiThreadedScoreLockWrite(self.score.write().unwrap())
287         }
288 }
289
290 #[cfg(c_bindings)]
291 impl<T: Score> Writeable for MultiThreadedLockableScore<T> {
292         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
293                 self.score.read().unwrap().write(writer)
294         }
295 }
296
297 #[cfg(c_bindings)]
298 impl<'a, T: Score + 'a> WriteableScore<'a> for MultiThreadedLockableScore<T> {}
299
300 #[cfg(c_bindings)]
301 impl<T: Score> MultiThreadedLockableScore<T> {
302         /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
303         pub fn new(score: T) -> Self {
304                 MultiThreadedLockableScore { score: RwLock::new(score) }
305         }
306 }
307
308 #[cfg(c_bindings)]
309 /// A locked `MultiThreadedLockableScore`.
310 pub struct MultiThreadedScoreLockRead<'a, T: Score>(RwLockReadGuard<'a, T>);
311
312 #[cfg(c_bindings)]
313 /// A locked `MultiThreadedLockableScore`.
314 pub struct MultiThreadedScoreLockWrite<'a, T: Score>(RwLockWriteGuard<'a, T>);
315
316 #[cfg(c_bindings)]
317 impl<'a, T: 'a + Score> Deref for MultiThreadedScoreLockRead<'a, T> {
318         type Target = T;
319
320         fn deref(&self) -> &Self::Target {
321                 self.0.deref()
322         }
323 }
324
325 #[cfg(c_bindings)]
326 impl<'a, T: Score> ScoreLookUp for MultiThreadedScoreLockRead<'a, T> {
327         type ScoreParams = T::ScoreParams;
328         fn channel_penalty_msat(&self, candidate:&CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams
329         ) -> u64 {
330                 self.0.channel_penalty_msat(candidate, usage, score_params)
331         }
332 }
333
334 #[cfg(c_bindings)]
335 impl<'a, T: Score> Writeable for MultiThreadedScoreLockWrite<'a, T> {
336         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
337                 self.0.write(writer)
338         }
339 }
340
341 #[cfg(c_bindings)]
342 impl<'a, T: 'a + Score> Deref for MultiThreadedScoreLockWrite<'a, T> {
343         type Target = T;
344
345         fn deref(&self) -> &Self::Target {
346                 self.0.deref()
347         }
348 }
349
350 #[cfg(c_bindings)]
351 impl<'a, T: 'a + Score> DerefMut for MultiThreadedScoreLockWrite<'a, T> {
352         fn deref_mut(&mut self) -> &mut Self::Target {
353                 self.0.deref_mut()
354         }
355 }
356
357 #[cfg(c_bindings)]
358 impl<'a, T: Score> ScoreUpdate for MultiThreadedScoreLockWrite<'a, T> {
359         fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration) {
360                 self.0.payment_path_failed(path, short_channel_id, duration_since_epoch)
361         }
362
363         fn payment_path_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
364                 self.0.payment_path_successful(path, duration_since_epoch)
365         }
366
367         fn probe_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration) {
368                 self.0.probe_failed(path, short_channel_id, duration_since_epoch)
369         }
370
371         fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
372                 self.0.probe_successful(path, duration_since_epoch)
373         }
374
375         fn decay_liquidity_certainty(&mut self, duration_since_epoch: Duration) {
376                 self.0.decay_liquidity_certainty(duration_since_epoch)
377         }
378 }
379
380
381 /// Proposed use of a channel passed as a parameter to [`ScoreLookUp::channel_penalty_msat`].
382 #[derive(Clone, Copy, Debug, PartialEq)]
383 pub struct ChannelUsage {
384         /// The amount to send through the channel, denominated in millisatoshis.
385         pub amount_msat: u64,
386
387         /// Total amount, denominated in millisatoshis, already allocated to send through the channel
388         /// as part of a multi-path payment.
389         pub inflight_htlc_msat: u64,
390
391         /// The effective capacity of the channel.
392         pub effective_capacity: EffectiveCapacity,
393 }
394
395 #[derive(Clone)]
396 /// [`ScoreLookUp`] implementation that uses a fixed penalty.
397 pub struct FixedPenaltyScorer {
398         penalty_msat: u64,
399 }
400
401 impl FixedPenaltyScorer {
402         /// Creates a new scorer using `penalty_msat`.
403         pub fn with_penalty(penalty_msat: u64) -> Self {
404                 Self { penalty_msat }
405         }
406 }
407
408 impl ScoreLookUp for FixedPenaltyScorer {
409         type ScoreParams = ();
410         fn channel_penalty_msat(&self, _: &CandidateRouteHop, _: ChannelUsage, _score_params: &Self::ScoreParams) -> u64 {
411                 self.penalty_msat
412         }
413 }
414
415 impl ScoreUpdate for FixedPenaltyScorer {
416         fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64, _duration_since_epoch: Duration) {}
417
418         fn payment_path_successful(&mut self, _path: &Path, _duration_since_epoch: Duration) {}
419
420         fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64, _duration_since_epoch: Duration) {}
421
422         fn probe_successful(&mut self, _path: &Path, _duration_since_epoch: Duration) {}
423
424         fn decay_liquidity_certainty(&mut self, _duration_since_epoch: Duration) {}
425 }
426
427 impl Writeable for FixedPenaltyScorer {
428         #[inline]
429         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
430                 write_tlv_fields!(w, {});
431                 Ok(())
432         }
433 }
434
435 impl ReadableArgs<u64> for FixedPenaltyScorer {
436         #[inline]
437         fn read<R: Read>(r: &mut R, penalty_msat: u64) -> Result<Self, DecodeError> {
438                 read_tlv_fields!(r, {});
439                 Ok(Self { penalty_msat })
440         }
441 }
442
443 /// [`ScoreLookUp`] implementation using channel success probability distributions.
444 ///
445 /// Channels are tracked with upper and lower liquidity bounds - when an HTLC fails at a channel,
446 /// we learn that the upper-bound on the available liquidity is lower than the amount of the HTLC.
447 /// When a payment is forwarded through a channel (but fails later in the route), we learn the
448 /// lower-bound on the channel's available liquidity must be at least the value of the HTLC.
449 ///
450 /// These bounds are then used to determine a success probability using the formula from
451 /// *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
452 /// and Stefan Richter [[1]] (i.e. `(upper_bound - payment_amount) / (upper_bound - lower_bound)`).
453 ///
454 /// This probability is combined with the [`liquidity_penalty_multiplier_msat`] and
455 /// [`liquidity_penalty_amount_multiplier_msat`] parameters to calculate a concrete penalty in
456 /// milli-satoshis. The penalties, when added across all hops, have the property of being linear in
457 /// terms of the entire path's success probability. This allows the router to directly compare
458 /// penalties for different paths. See the documentation of those parameters for the exact formulas.
459 ///
460 /// The liquidity bounds are decayed by halving them every [`liquidity_offset_half_life`].
461 ///
462 /// Further, we track the history of our upper and lower liquidity bounds for each channel,
463 /// allowing us to assign a second penalty (using [`historical_liquidity_penalty_multiplier_msat`]
464 /// and [`historical_liquidity_penalty_amount_multiplier_msat`]) based on the same probability
465 /// formula, but using the history of a channel rather than our latest estimates for the liquidity
466 /// bounds.
467 ///
468 /// [1]: https://arxiv.org/abs/2107.05322
469 /// [`liquidity_penalty_multiplier_msat`]: ProbabilisticScoringFeeParameters::liquidity_penalty_multiplier_msat
470 /// [`liquidity_penalty_amount_multiplier_msat`]: ProbabilisticScoringFeeParameters::liquidity_penalty_amount_multiplier_msat
471 /// [`liquidity_offset_half_life`]: ProbabilisticScoringDecayParameters::liquidity_offset_half_life
472 /// [`historical_liquidity_penalty_multiplier_msat`]: ProbabilisticScoringFeeParameters::historical_liquidity_penalty_multiplier_msat
473 /// [`historical_liquidity_penalty_amount_multiplier_msat`]: ProbabilisticScoringFeeParameters::historical_liquidity_penalty_amount_multiplier_msat
474 pub struct ProbabilisticScorer<G: Deref<Target = NetworkGraph<L>>, L: Deref>
475 where L::Target: Logger {
476         decay_params: ProbabilisticScoringDecayParameters,
477         network_graph: G,
478         logger: L,
479         channel_liquidities: HashMap<u64, ChannelLiquidity>,
480 }
481
482 /// Parameters for configuring [`ProbabilisticScorer`].
483 ///
484 /// Used to configure base, liquidity, and amount penalties, the sum of which comprises the channel
485 /// penalty (i.e., the amount in msats willing to be paid to avoid routing through the channel).
486 ///
487 /// The penalty applied to any channel by the [`ProbabilisticScorer`] is the sum of each of the
488 /// parameters here.
489 #[derive(Clone)]
490 pub struct ProbabilisticScoringFeeParameters {
491         /// A fixed penalty in msats to apply to each channel.
492         ///
493         /// Default value: 500 msat
494         pub base_penalty_msat: u64,
495
496         /// A multiplier used with the total amount flowing over a channel to calculate a fixed penalty
497         /// applied to each channel, in excess of the [`base_penalty_msat`].
498         ///
499         /// The purpose of the amount penalty is to avoid having fees dominate the channel cost (i.e.,
500         /// fees plus penalty) for large payments. The penalty is computed as the product of this
501         /// multiplier and `2^30`ths of the total amount flowing over a channel (i.e. the payment
502         /// amount plus the amount of any other HTLCs flowing we sent over the same channel).
503         ///
504         /// ie `base_penalty_amount_multiplier_msat * amount_msat / 2^30`
505         ///
506         /// Default value: 8,192 msat
507         ///
508         /// [`base_penalty_msat`]: Self::base_penalty_msat
509         pub base_penalty_amount_multiplier_msat: u64,
510
511         /// A multiplier used in conjunction with the negative `log10` of the channel's success
512         /// probability for a payment, as determined by our latest estimates of the channel's
513         /// liquidity, to determine the liquidity penalty.
514         ///
515         /// The penalty is based in part on the knowledge learned from prior successful and unsuccessful
516         /// payments. This knowledge is decayed over time based on [`liquidity_offset_half_life`]. The
517         /// penalty is effectively limited to `2 * liquidity_penalty_multiplier_msat` (corresponding to
518         /// lower bounding the success probability to `0.01`) when the amount falls within the
519         /// uncertainty bounds of the channel liquidity balance. Amounts above the upper bound will
520         /// result in a `u64::max_value` penalty, however.
521         ///
522         /// `-log10(success_probability) * liquidity_penalty_multiplier_msat`
523         ///
524         /// Default value: 30,000 msat
525         ///
526         /// [`liquidity_offset_half_life`]: ProbabilisticScoringDecayParameters::liquidity_offset_half_life
527         pub liquidity_penalty_multiplier_msat: u64,
528
529         /// A multiplier used in conjunction with the total amount flowing over a channel and the
530         /// negative `log10` of the channel's success probability for the payment, as determined by our
531         /// latest estimates of the channel's liquidity, to determine the amount penalty.
532         ///
533         /// The purpose of the amount penalty is to avoid having fees dominate the channel cost (i.e.,
534         /// fees plus penalty) for large payments. The penalty is computed as the product of this
535         /// multiplier and `2^20`ths of the amount flowing over this channel, weighted by the negative
536         /// `log10` of the success probability.
537         ///
538         /// `-log10(success_probability) * liquidity_penalty_amount_multiplier_msat * amount_msat / 2^20`
539         ///
540         /// In practice, this means for 0.1 success probability (`-log10(0.1) == 1`) each `2^20`th of
541         /// the amount will result in a penalty of the multiplier. And, as the success probability
542         /// decreases, the negative `log10` weighting will increase dramatically. For higher success
543         /// probabilities, the multiplier will have a decreasing effect as the negative `log10` will
544         /// fall below `1`.
545         ///
546         /// Default value: 192 msat
547         pub liquidity_penalty_amount_multiplier_msat: u64,
548
549         /// A multiplier used in conjunction with the negative `log10` of the channel's success
550         /// probability for the payment, as determined based on the history of our estimates of the
551         /// channel's available liquidity, to determine a penalty.
552         ///
553         /// This penalty is similar to [`liquidity_penalty_multiplier_msat`], however, instead of using
554         /// only our latest estimate for the current liquidity available in the channel, it estimates
555         /// success probability based on the estimated liquidity available in the channel through
556         /// history. Specifically, every time we update our liquidity bounds on a given channel, we
557         /// track which of several buckets those bounds fall into, exponentially decaying the
558         /// probability of each bucket as new samples are added.
559         ///
560         /// Default value: 10,000 msat
561         ///
562         /// [`liquidity_penalty_multiplier_msat`]: Self::liquidity_penalty_multiplier_msat
563         pub historical_liquidity_penalty_multiplier_msat: u64,
564
565         /// A multiplier used in conjunction with the total amount flowing over a channel and the
566         /// negative `log10` of the channel's success probability for the payment, as determined based
567         /// on the history of our estimates of the channel's available liquidity, to determine a
568         /// penalty.
569         ///
570         /// The purpose of the amount penalty is to avoid having fees dominate the channel cost for
571         /// large payments. The penalty is computed as the product of this multiplier and `2^20`ths
572         /// of the amount flowing over this channel, weighted by the negative `log10` of the success
573         /// probability.
574         ///
575         /// This penalty is similar to [`liquidity_penalty_amount_multiplier_msat`], however, instead
576         /// of using only our latest estimate for the current liquidity available in the channel, it
577         /// estimates success probability based on the estimated liquidity available in the channel
578         /// through history. Specifically, every time we update our liquidity bounds on a given
579         /// channel, we track which of several buckets those bounds fall into, exponentially decaying
580         /// the probability of each bucket as new samples are added.
581         ///
582         /// Default value: 64 msat
583         ///
584         /// [`liquidity_penalty_amount_multiplier_msat`]: Self::liquidity_penalty_amount_multiplier_msat
585         pub historical_liquidity_penalty_amount_multiplier_msat: u64,
586
587         /// Manual penalties used for the given nodes. Allows to set a particular penalty for a given
588         /// node. Note that a manual penalty of `u64::max_value()` means the node would not ever be
589         /// considered during path finding.
590         ///
591         /// This is not exported to bindings users
592         pub manual_node_penalties: HashMap<NodeId, u64>,
593
594         /// This penalty is applied when `htlc_maximum_msat` is equal to or larger than half of the
595         /// channel's capacity, (ie. htlc_maximum_msat >= 0.5 * channel_capacity) which makes us
596         /// prefer nodes with a smaller `htlc_maximum_msat`. We treat such nodes preferentially
597         /// as this makes balance discovery attacks harder to execute, thereby creating an incentive
598         /// to restrict `htlc_maximum_msat` and improve privacy.
599         ///
600         /// Default value: 250 msat
601         pub anti_probing_penalty_msat: u64,
602
603         /// This penalty is applied when the total amount flowing over a channel exceeds our current
604         /// estimate of the channel's available liquidity. The total amount is the amount of the
605         /// current HTLC plus any HTLCs which we've sent over the same channel.
606         ///
607         /// Note that in this case all other penalties, including the
608         /// [`liquidity_penalty_multiplier_msat`] and [`liquidity_penalty_amount_multiplier_msat`]-based
609         /// penalties, as well as the [`base_penalty_msat`] and the [`anti_probing_penalty_msat`], if
610         /// applicable, are still included in the overall penalty.
611         ///
612         /// If you wish to avoid creating paths with such channels entirely, setting this to a value of
613         /// `u64::max_value()` will guarantee that.
614         ///
615         /// Default value: 1_0000_0000_000 msat (1 Bitcoin)
616         ///
617         /// [`liquidity_penalty_multiplier_msat`]: Self::liquidity_penalty_multiplier_msat
618         /// [`liquidity_penalty_amount_multiplier_msat`]: Self::liquidity_penalty_amount_multiplier_msat
619         /// [`base_penalty_msat`]: Self::base_penalty_msat
620         /// [`anti_probing_penalty_msat`]: Self::anti_probing_penalty_msat
621         pub considered_impossible_penalty_msat: u64,
622
623         /// In order to calculate most of the scores above, we must first convert a lower and upper
624         /// bound on the available liquidity in a channel into the probability that we think a payment
625         /// will succeed. That probability is derived from a Probability Density Function for where we
626         /// think the liquidity in a channel likely lies, given such bounds.
627         ///
628         /// If this flag is set, that PDF is simply a constant - we assume that the actual available
629         /// liquidity in a channel is just as likely to be at any point between our lower and upper
630         /// bounds.
631         ///
632         /// If this flag is *not* set, that PDF is `(x - 0.5*capacity) ^ 2`. That is, we use an
633         /// exponential curve which expects the liquidity of a channel to lie "at the edges". This
634         /// matches experimental results - most routing nodes do not aggressively rebalance their
635         /// channels and flows in the network are often unbalanced, leaving liquidity usually
636         /// unavailable.
637         ///
638         /// Thus, for the "best" routes, leave this flag `false`. However, the flag does imply a number
639         /// of floating-point multiplications in the hottest routing code, which may lead to routing
640         /// performance degradation on some machines.
641         ///
642         /// Default value: false
643         pub linear_success_probability: bool,
644 }
645
646 impl Default for ProbabilisticScoringFeeParameters {
647         fn default() -> Self {
648                 Self {
649                         base_penalty_msat: 500,
650                         base_penalty_amount_multiplier_msat: 8192,
651                         liquidity_penalty_multiplier_msat: 30_000,
652                         liquidity_penalty_amount_multiplier_msat: 192,
653                         manual_node_penalties: HashMap::new(),
654                         anti_probing_penalty_msat: 250,
655                         considered_impossible_penalty_msat: 1_0000_0000_000,
656                         historical_liquidity_penalty_multiplier_msat: 10_000,
657                         historical_liquidity_penalty_amount_multiplier_msat: 64,
658                         linear_success_probability: false,
659                 }
660         }
661 }
662
663 impl ProbabilisticScoringFeeParameters {
664         /// Marks the node with the given `node_id` as banned,
665         /// i.e it will be avoided during path finding.
666         pub fn add_banned(&mut self, node_id: &NodeId) {
667                 self.manual_node_penalties.insert(*node_id, u64::max_value());
668         }
669
670         /// Marks all nodes in the given list as banned, i.e.,
671         /// they will be avoided during path finding.
672         pub fn add_banned_from_list(&mut self, node_ids: Vec<NodeId>) {
673                 for id in node_ids {
674                         self.manual_node_penalties.insert(id, u64::max_value());
675                 }
676         }
677
678         /// Removes the node with the given `node_id` from the list of nodes to avoid.
679         pub fn remove_banned(&mut self, node_id: &NodeId) {
680                 self.manual_node_penalties.remove(node_id);
681         }
682
683         /// Sets a manual penalty for the given node.
684         pub fn set_manual_penalty(&mut self, node_id: &NodeId, penalty: u64) {
685                 self.manual_node_penalties.insert(*node_id, penalty);
686         }
687
688         /// Removes the node with the given `node_id` from the list of manual penalties.
689         pub fn remove_manual_penalty(&mut self, node_id: &NodeId) {
690                 self.manual_node_penalties.remove(node_id);
691         }
692
693         /// Clears the list of manual penalties that are applied during path finding.
694         pub fn clear_manual_penalties(&mut self) {
695                 self.manual_node_penalties = HashMap::new();
696         }
697 }
698
699 #[cfg(test)]
700 impl ProbabilisticScoringFeeParameters {
701         fn zero_penalty() -> Self {
702                 Self {
703                         base_penalty_msat: 0,
704                         base_penalty_amount_multiplier_msat: 0,
705                         liquidity_penalty_multiplier_msat: 0,
706                         liquidity_penalty_amount_multiplier_msat: 0,
707                         historical_liquidity_penalty_multiplier_msat: 0,
708                         historical_liquidity_penalty_amount_multiplier_msat: 0,
709                         manual_node_penalties: HashMap::new(),
710                         anti_probing_penalty_msat: 0,
711                         considered_impossible_penalty_msat: 0,
712                         linear_success_probability: true,
713                 }
714         }
715 }
716
717 /// Parameters for configuring [`ProbabilisticScorer`].
718 ///
719 /// Used to configure decay parameters that are static throughout the lifetime of the scorer.
720 /// these decay parameters affect the score of the channel penalty and are not changed on a
721 /// per-route penalty cost call.
722 #[derive(Copy, Clone)]
723 pub struct ProbabilisticScoringDecayParameters {
724         /// If we aren't learning any new datapoints for a channel, the historical liquidity bounds
725         /// tracking can simply live on with increasingly stale data. Instead, when a channel has not
726         /// seen a liquidity estimate update for this amount of time, the historical datapoints are
727         /// decayed by half.
728         /// For an example of historical_no_updates_half_life being used see [`historical_estimated_channel_liquidity_probabilities`]
729         ///
730         /// Note that after 16 or more half lives all historical data will be completely gone.
731         ///
732         /// Default value: 14 days
733         ///
734         /// [`historical_estimated_channel_liquidity_probabilities`]: ProbabilisticScorer::historical_estimated_channel_liquidity_probabilities
735         pub historical_no_updates_half_life: Duration,
736
737         /// Whenever this amount of time elapses since the last update to a channel's liquidity bounds,
738         /// the distance from the bounds to "zero" is cut in half. In other words, the lower-bound on
739         /// the available liquidity is halved and the upper-bound moves half-way to the channel's total
740         /// capacity.
741         ///
742         /// Because halving the liquidity bounds grows the uncertainty on the channel's liquidity,
743         /// the penalty for an amount within the new bounds may change. See the [`ProbabilisticScorer`]
744         /// struct documentation for more info on the way the liquidity bounds are used.
745         ///
746         /// For example, if the channel's capacity is 1 million sats, and the current upper and lower
747         /// liquidity bounds are 200,000 sats and 600,000 sats, after this amount of time the upper
748         /// and lower liquidity bounds will be decayed to 100,000 and 800,000 sats.
749         ///
750         /// Default value: 6 hours
751         ///
752         /// # Note
753         ///
754         /// When built with the `no-std` feature, time will never elapse. Therefore, the channel
755         /// liquidity knowledge will never decay except when the bounds cross.
756         pub liquidity_offset_half_life: Duration,
757 }
758
759 impl Default for ProbabilisticScoringDecayParameters {
760         fn default() -> Self {
761                 Self {
762                         liquidity_offset_half_life: Duration::from_secs(6 * 60 * 60),
763                         historical_no_updates_half_life: Duration::from_secs(60 * 60 * 24 * 14),
764                 }
765         }
766 }
767
768 #[cfg(test)]
769 impl ProbabilisticScoringDecayParameters {
770         fn zero_penalty() -> Self {
771                 Self {
772                         liquidity_offset_half_life: Duration::from_secs(6 * 60 * 60),
773                         historical_no_updates_half_life: Duration::from_secs(60 * 60 * 24 * 14),
774                 }
775         }
776 }
777
778 /// Accounting for channel liquidity balance uncertainty.
779 ///
780 /// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
781 /// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
782 /// offset fields gives the opposite direction.
783 #[repr(C)] // Force the fields in memory to be in the order we specify
784 struct ChannelLiquidity {
785         /// Lower channel liquidity bound in terms of an offset from zero.
786         min_liquidity_offset_msat: u64,
787
788         /// Upper channel liquidity bound in terms of an offset from the effective capacity.
789         max_liquidity_offset_msat: u64,
790
791         liquidity_history: HistoricalLiquidityTracker,
792
793         /// Time when the liquidity bounds were last modified as an offset since the unix epoch.
794         last_updated: Duration,
795
796         /// Time when the historical liquidity bounds were last modified as an offset against the unix
797         /// epoch.
798         offset_history_last_updated: Duration,
799 }
800
801 // Check that the liquidity HashMap's entries sit on round cache lines.
802 //
803 // Specifically, the first cache line will have the key, the liquidity offsets, and the total
804 // points tracked in the historical tracker.
805 //
806 // The next two cache lines will have the historical points, which we only access last during
807 // scoring, followed by the last_updated `Duration`s (which we do not need during scoring).
808 const _LIQUIDITY_MAP_SIZING_CHECK: usize = 192 - ::core::mem::size_of::<(u64, ChannelLiquidity)>();
809 const _LIQUIDITY_MAP_SIZING_CHECK_2: usize = ::core::mem::size_of::<(u64, ChannelLiquidity)>() - 192;
810
811 /// A snapshot of [`ChannelLiquidity`] in one direction assuming a certain channel capacity and
812 /// decayed with a given half life.
813 struct DirectedChannelLiquidity<L: Deref<Target = u64>, HT: Deref<Target = HistoricalLiquidityTracker>, T: Deref<Target = Duration>> {
814         min_liquidity_offset_msat: L,
815         max_liquidity_offset_msat: L,
816         liquidity_history: DirectedHistoricalLiquidityTracker<HT>,
817         capacity_msat: u64,
818         last_updated: T,
819         offset_history_last_updated: T,
820 }
821
822 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> where L::Target: Logger {
823         /// Creates a new scorer using the given scoring parameters for sending payments from a node
824         /// through a network graph.
825         pub fn new(decay_params: ProbabilisticScoringDecayParameters, network_graph: G, logger: L) -> Self {
826                 Self {
827                         decay_params,
828                         network_graph,
829                         logger,
830                         channel_liquidities: HashMap::new(),
831                 }
832         }
833
834         #[cfg(test)]
835         fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity) -> Self {
836                 assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
837                 self
838         }
839
840         /// Dump the contents of this scorer into the configured logger.
841         ///
842         /// Note that this writes roughly one line per channel for which we have a liquidity estimate,
843         /// which may be a substantial amount of log output.
844         pub fn debug_log_liquidity_stats(&self) {
845                 let graph = self.network_graph.read_only();
846                 for (scid, liq) in self.channel_liquidities.iter() {
847                         if let Some(chan_debug) = graph.channels().get(scid) {
848                                 let log_direction = |source, target| {
849                                         if let Some((directed_info, _)) = chan_debug.as_directed_to(target) {
850                                                 let amt = directed_info.effective_capacity().as_msat();
851                                                 let dir_liq = liq.as_directed(source, target, amt);
852
853                                                 let min_buckets = &dir_liq.liquidity_history.min_liquidity_offset_history_buckets();
854                                                 let max_buckets = &dir_liq.liquidity_history.max_liquidity_offset_history_buckets();
855
856                                                 log_debug!(self.logger, core::concat!(
857                                                         "Liquidity from {} to {} via {} is in the range ({}, {}).\n",
858                                                         "\tHistorical min liquidity bucket relative probabilities:\n",
859                                                         "\t\t{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}\n",
860                                                         "\tHistorical max liquidity bucket relative probabilities:\n",
861                                                         "\t\t{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}"),
862                                                         source, target, scid, dir_liq.min_liquidity_msat(), dir_liq.max_liquidity_msat(),
863                                                         min_buckets[ 0], min_buckets[ 1], min_buckets[ 2], min_buckets[ 3],
864                                                         min_buckets[ 4], min_buckets[ 5], min_buckets[ 6], min_buckets[ 7],
865                                                         min_buckets[ 8], min_buckets[ 9], min_buckets[10], min_buckets[11],
866                                                         min_buckets[12], min_buckets[13], min_buckets[14], min_buckets[15],
867                                                         min_buckets[16], min_buckets[17], min_buckets[18], min_buckets[19],
868                                                         min_buckets[20], min_buckets[21], min_buckets[22], min_buckets[23],
869                                                         min_buckets[24], min_buckets[25], min_buckets[26], min_buckets[27],
870                                                         min_buckets[28], min_buckets[29], min_buckets[30], min_buckets[31],
871                                                         // Note that the liquidity buckets are an offset from the edge, so we
872                                                         // inverse the max order to get the probabilities from zero.
873                                                         max_buckets[31], max_buckets[30], max_buckets[29], max_buckets[28],
874                                                         max_buckets[27], max_buckets[26], max_buckets[25], max_buckets[24],
875                                                         max_buckets[23], max_buckets[22], max_buckets[21], max_buckets[20],
876                                                         max_buckets[19], max_buckets[18], max_buckets[17], max_buckets[16],
877                                                         max_buckets[15], max_buckets[14], max_buckets[13], max_buckets[12],
878                                                         max_buckets[11], max_buckets[10], max_buckets[ 9], max_buckets[ 8],
879                                                         max_buckets[ 7], max_buckets[ 6], max_buckets[ 5], max_buckets[ 4],
880                                                         max_buckets[ 3], max_buckets[ 2], max_buckets[ 1], max_buckets[ 0]);
881                                         } else {
882                                                 log_debug!(self.logger, "No amount known for SCID {} from {:?} to {:?}", scid, source, target);
883                                         }
884                                 };
885
886                                 log_direction(&chan_debug.node_one, &chan_debug.node_two);
887                                 log_direction(&chan_debug.node_two, &chan_debug.node_one);
888                         } else {
889                                 log_debug!(self.logger, "No network graph entry for SCID {}", scid);
890                         }
891                 }
892         }
893
894         /// Query the estimated minimum and maximum liquidity available for sending a payment over the
895         /// channel with `scid` towards the given `target` node.
896         pub fn estimated_channel_liquidity_range(&self, scid: u64, target: &NodeId) -> Option<(u64, u64)> {
897                 let graph = self.network_graph.read_only();
898
899                 if let Some(chan) = graph.channels().get(&scid) {
900                         if let Some(liq) = self.channel_liquidities.get(&scid) {
901                                 if let Some((directed_info, source)) = chan.as_directed_to(target) {
902                                         let amt = directed_info.effective_capacity().as_msat();
903                                         let dir_liq = liq.as_directed(source, target, amt);
904                                         return Some((dir_liq.min_liquidity_msat(), dir_liq.max_liquidity_msat()));
905                                 }
906                         }
907                 }
908                 None
909         }
910
911         /// Query the historical estimated minimum and maximum liquidity available for sending a
912         /// payment over the channel with `scid` towards the given `target` node.
913         ///
914         /// Returns two sets of 32 buckets. The first set describes the lower-bound liquidity history,
915         /// the second set describes the upper-bound liquidity history. Each bucket describes the
916         /// relative frequency at which we've seen a liquidity bound in the bucket's range relative to
917         /// the channel's total capacity, on an arbitrary scale. Because the values are slowly decayed,
918         /// more recent data points are weighted more heavily than older datapoints.
919         ///
920         /// Note that the range of each bucket varies by its location to provide more granular results
921         /// at the edges of a channel's capacity, where it is more likely to sit.
922         ///
923         /// When scoring, the estimated probability that an upper-/lower-bound lies in a given bucket
924         /// is calculated by dividing that bucket's value with the total value of all buckets.
925         ///
926         /// For example, using a lower bucket count for illustrative purposes, a value of
927         /// `[0, 0, 0, ..., 0, 32]` indicates that we believe the probability of a bound being very
928         /// close to the channel's capacity to be 100%, and have never (recently) seen it in any other
929         /// bucket. A value of `[31, 0, 0, ..., 0, 0, 32]` indicates we've seen the bound being both
930         /// in the top and bottom bucket, and roughly with similar (recent) frequency.
931         ///
932         /// Because the datapoints are decayed slowly over time, values will eventually return to
933         /// `Some(([0; 32], [0; 32]))` or `None` if no data remains for a channel.
934         ///
935         /// In order to fetch a single success probability from the buckets provided here, as used in
936         /// the scoring model, see [`Self::historical_estimated_payment_success_probability`].
937         pub fn historical_estimated_channel_liquidity_probabilities(&self, scid: u64, target: &NodeId)
938         -> Option<([u16; 32], [u16; 32])> {
939                 let graph = self.network_graph.read_only();
940
941                 if let Some(chan) = graph.channels().get(&scid) {
942                         if let Some(liq) = self.channel_liquidities.get(&scid) {
943                                 if let Some((directed_info, source)) = chan.as_directed_to(target) {
944                                         let amt = directed_info.effective_capacity().as_msat();
945                                         let dir_liq = liq.as_directed(source, target, amt);
946
947                                         let min_buckets = *dir_liq.liquidity_history.min_liquidity_offset_history_buckets();
948                                         let mut max_buckets = *dir_liq.liquidity_history.max_liquidity_offset_history_buckets();
949
950                                         // Note that the liquidity buckets are an offset from the edge, so we inverse
951                                         // the max order to get the probabilities from zero.
952                                         max_buckets.reverse();
953                                         return Some((min_buckets, max_buckets));
954                                 }
955                         }
956                 }
957                 None
958         }
959
960         /// Query the probability of payment success sending the given `amount_msat` over the channel
961         /// with `scid` towards the given `target` node, based on the historical estimated liquidity
962         /// bounds.
963         ///
964         /// These are the same bounds as returned by
965         /// [`Self::historical_estimated_channel_liquidity_probabilities`] (but not those returned by
966         /// [`Self::estimated_channel_liquidity_range`]).
967         pub fn historical_estimated_payment_success_probability(
968                 &self, scid: u64, target: &NodeId, amount_msat: u64, params: &ProbabilisticScoringFeeParameters)
969         -> Option<f64> {
970                 let graph = self.network_graph.read_only();
971
972                 if let Some(chan) = graph.channels().get(&scid) {
973                         if let Some(liq) = self.channel_liquidities.get(&scid) {
974                                 if let Some((directed_info, source)) = chan.as_directed_to(target) {
975                                         let capacity_msat = directed_info.effective_capacity().as_msat();
976                                         let dir_liq = liq.as_directed(source, target, capacity_msat);
977
978                                         return dir_liq.liquidity_history.calculate_success_probability_times_billion(
979                                                 &params, amount_msat, capacity_msat
980                                         ).map(|p| p as f64 / (1024 * 1024 * 1024) as f64);
981                                 }
982                         }
983                 }
984                 None
985         }
986 }
987
988 impl ChannelLiquidity {
989         fn new(last_updated: Duration) -> Self {
990                 Self {
991                         min_liquidity_offset_msat: 0,
992                         max_liquidity_offset_msat: 0,
993                         liquidity_history: HistoricalLiquidityTracker::new(),
994                         last_updated,
995                         offset_history_last_updated: last_updated,
996                 }
997         }
998
999         /// Returns a view of the channel liquidity directed from `source` to `target` assuming
1000         /// `capacity_msat`.
1001         fn as_directed(
1002                 &self, source: &NodeId, target: &NodeId, capacity_msat: u64,
1003         ) -> DirectedChannelLiquidity<&u64, &HistoricalLiquidityTracker, &Duration> {
1004                 let source_less_than_target = source < target;
1005                 let (min_liquidity_offset_msat, max_liquidity_offset_msat) =
1006                         if source_less_than_target {
1007                                 (&self.min_liquidity_offset_msat, &self.max_liquidity_offset_msat)
1008                         } else {
1009                                 (&self.max_liquidity_offset_msat, &self.min_liquidity_offset_msat)
1010                         };
1011
1012                 DirectedChannelLiquidity {
1013                         min_liquidity_offset_msat,
1014                         max_liquidity_offset_msat,
1015                         liquidity_history: self.liquidity_history.as_directed(source_less_than_target),
1016                         capacity_msat,
1017                         last_updated: &self.last_updated,
1018                         offset_history_last_updated: &self.offset_history_last_updated,
1019                 }
1020         }
1021
1022         /// Returns a mutable view of the channel liquidity directed from `source` to `target` assuming
1023         /// `capacity_msat`.
1024         fn as_directed_mut(
1025                 &mut self, source: &NodeId, target: &NodeId, capacity_msat: u64,
1026         ) -> DirectedChannelLiquidity<&mut u64, &mut HistoricalLiquidityTracker, &mut Duration> {
1027                 let source_less_than_target = source < target;
1028                 let (min_liquidity_offset_msat, max_liquidity_offset_msat) =
1029                         if source_less_than_target {
1030                                 (&mut self.min_liquidity_offset_msat, &mut self.max_liquidity_offset_msat)
1031                         } else {
1032                                 (&mut self.max_liquidity_offset_msat, &mut self.min_liquidity_offset_msat)
1033                         };
1034
1035                 DirectedChannelLiquidity {
1036                         min_liquidity_offset_msat,
1037                         max_liquidity_offset_msat,
1038                         liquidity_history: self.liquidity_history.as_directed_mut(source_less_than_target),
1039                         capacity_msat,
1040                         last_updated: &mut self.last_updated,
1041                         offset_history_last_updated: &mut self.offset_history_last_updated,
1042                 }
1043         }
1044
1045         fn decayed_offset(&self, offset: u64, duration_since_epoch: Duration,
1046                 decay_params: ProbabilisticScoringDecayParameters
1047         ) -> u64 {
1048                 let half_life = decay_params.liquidity_offset_half_life.as_secs_f64();
1049                 if half_life != 0.0 {
1050                         let elapsed_time = duration_since_epoch.saturating_sub(self.last_updated).as_secs_f64();
1051                         ((offset as f64) * powf64(0.5, elapsed_time / half_life)) as u64
1052                 } else {
1053                         0
1054                 }
1055         }
1056 }
1057
1058 /// Bounds `-log10` to avoid excessive liquidity penalties for payments with low success
1059 /// probabilities.
1060 const NEGATIVE_LOG10_UPPER_BOUND: u64 = 2;
1061
1062 /// The rough cutoff at which our precision falls off and we should stop bothering to try to log a
1063 /// ratio, as X in 1/X.
1064 const PRECISION_LOWER_BOUND_DENOMINATOR: u64 = log_approx::LOWER_BITS_BOUND;
1065
1066 /// The divisor used when computing the amount penalty.
1067 const AMOUNT_PENALTY_DIVISOR: u64 = 1 << 20;
1068 const BASE_AMOUNT_PENALTY_DIVISOR: u64 = 1 << 30;
1069
1070 /// Raises three `f64`s to the 3rd power, without `powi` because it requires `std` (dunno why).
1071 #[inline(always)]
1072 fn three_f64_pow_3(a: f64, b: f64, c: f64) -> (f64, f64, f64) {
1073         (a * a * a, b * b * b, c * c * c)
1074 }
1075
1076 #[inline(always)]
1077 fn linear_success_probability(
1078         amount_msat: u64, min_liquidity_msat: u64, max_liquidity_msat: u64,
1079         min_zero_implies_no_successes: bool,
1080 ) -> (u64, u64) {
1081         let (numerator, mut denominator) =
1082                 (max_liquidity_msat - amount_msat,
1083                  (max_liquidity_msat - min_liquidity_msat).saturating_add(1));
1084
1085         if min_zero_implies_no_successes && min_liquidity_msat == 0 &&
1086                 denominator < u64::max_value() / 21
1087         {
1088                 // If we have no knowledge of the channel, scale probability down by ~75%
1089                 // Note that we prefer to increase the denominator rather than decrease the numerator as
1090                 // the denominator is more likely to be larger and thus provide greater precision. This is
1091                 // mostly an overoptimization but makes a large difference in tests.
1092                 denominator = denominator * 21 / 16
1093         }
1094
1095         (numerator, denominator)
1096 }
1097
1098 #[inline(always)]
1099 fn nonlinear_success_probability(
1100         amount_msat: u64, min_liquidity_msat: u64, max_liquidity_msat: u64, capacity_msat: u64,
1101 ) -> (f64, f64) {
1102         let capacity = capacity_msat as f64;
1103         let min = (min_liquidity_msat as f64) / capacity;
1104         let max = (max_liquidity_msat as f64) / capacity;
1105         let amount = (amount_msat as f64) / capacity;
1106
1107         // Assume the channel has a probability density function of (x - 0.5)^2 for values from
1108         // 0 to 1 (where 1 is the channel's full capacity). The success probability given some
1109         // liquidity bounds is thus the integral under the curve from the amount to maximum
1110         // estimated liquidity, divided by the same integral from the minimum to the maximum
1111         // estimated liquidity bounds.
1112         //
1113         // Because the integral from x to y is simply (y - 0.5)^3 - (x - 0.5)^3, we can
1114         // calculate the cumulative density function between the min/max bounds trivially. Note
1115         // that we don't bother to normalize the CDF to total to 1, as it will come out in the
1116         // division of num / den.
1117         let (max_pow, amt_pow, min_pow) = three_f64_pow_3(max - 0.5, amount - 0.5, min - 0.5);
1118         (max_pow - amt_pow, max_pow - min_pow)
1119 }
1120
1121
1122 /// Given liquidity bounds, calculates the success probability (in the form of a numerator and
1123 /// denominator) of an HTLC. This is a key assumption in our scoring models.
1124 ///
1125 /// min_zero_implies_no_successes signals that a `min_liquidity_msat` of 0 means we've not
1126 /// (recently) seen an HTLC successfully complete over this channel.
1127 #[inline(always)]
1128 fn success_probability(
1129         amount_msat: u64, min_liquidity_msat: u64, max_liquidity_msat: u64, capacity_msat: u64,
1130         params: &ProbabilisticScoringFeeParameters, min_zero_implies_no_successes: bool,
1131 ) -> (u64, u64) {
1132         debug_assert!(min_liquidity_msat <= amount_msat);
1133         debug_assert!(amount_msat < max_liquidity_msat);
1134         debug_assert!(max_liquidity_msat <= capacity_msat);
1135
1136         if params.linear_success_probability {
1137                 return linear_success_probability(
1138                         amount_msat, min_liquidity_msat, max_liquidity_msat, min_zero_implies_no_successes
1139                 );
1140         }
1141
1142         let (num, den) = nonlinear_success_probability(
1143                 amount_msat, min_liquidity_msat, max_liquidity_msat, capacity_msat
1144         );
1145
1146         // Because our numerator and denominator max out at 0.5^3 we need to multiply them by
1147         // quite a large factor to get something useful (ideally in the 2^30 range).
1148         const BILLIONISH: f64 = 1024.0 * 1024.0 * 1024.0;
1149         let numerator = (num * BILLIONISH) as u64 + 1;
1150         let mut denominator = (den * BILLIONISH) as u64 + 1;
1151         debug_assert!(numerator <= 1 << 30, "Got large numerator ({}) from float {}.", numerator, num);
1152         debug_assert!(denominator <= 1 << 30, "Got large denominator ({}) from float {}.", denominator, den);
1153
1154         if min_zero_implies_no_successes && min_liquidity_msat == 0 &&
1155                 denominator < u64::max_value() / 21
1156         {
1157                 // If we have no knowledge of the channel, scale probability down by ~75%
1158                 // Note that we prefer to increase the denominator rather than decrease the numerator as
1159                 // the denominator is more likely to be larger and thus provide greater precision. This is
1160                 // mostly an overoptimization but makes a large difference in tests.
1161                 denominator = denominator * 21 / 16
1162         }
1163
1164         (numerator, denominator)
1165 }
1166
1167 /// Given liquidity bounds, calculates the success probability (times some value) of an HTLC. This
1168 /// is a key assumption in our scoring models.
1169 ///
1170 /// min_zero_implies_no_successes signals that a `min_liquidity_msat` of 0 means we've not
1171 /// (recently) seen an HTLC successfully complete over this channel.
1172 #[inline(always)]
1173 fn success_probability_times_value_times_billion(
1174         amount_msat: u64, min_liquidity_msat: u64, max_liquidity_msat: u64, capacity_msat: u64,
1175         params: &ProbabilisticScoringFeeParameters, min_zero_implies_no_successes: bool,
1176         value_numerator: u64, value_denominator: u64,
1177 ) -> u64 {
1178         debug_assert!(min_liquidity_msat <= amount_msat);
1179         debug_assert!(amount_msat < max_liquidity_msat);
1180         debug_assert!(max_liquidity_msat <= capacity_msat);
1181
1182         if params.linear_success_probability {
1183                 let (numerator, denominator) = linear_success_probability(
1184                         amount_msat, min_liquidity_msat, max_liquidity_msat, min_zero_implies_no_successes
1185                 );
1186                 const BILLIONISH: u64 = 1024 * 1024 * 1024;
1187                 return (value_numerator * BILLIONISH / value_denominator) * numerator / denominator;
1188         }
1189
1190         let (num, mut den) = nonlinear_success_probability(
1191                 amount_msat, min_liquidity_msat, max_liquidity_msat, capacity_msat
1192         );
1193
1194         let value = (value_numerator as f64) / (value_denominator as f64);
1195
1196         if min_zero_implies_no_successes && min_liquidity_msat == 0 {
1197                 // If we have no knowledge of the channel, scale probability down by ~75%
1198                 // Note that we prefer to increase the denominator rather than decrease the numerator as
1199                 // the denominator is more likely to be larger and thus provide greater precision. This is
1200                 // mostly an overoptimization but makes a large difference in tests.
1201                 den = den * 21.0 / 16.0
1202         }
1203
1204         const BILLIONISH: f64 = 1024.0 * 1024.0 * 1024.0;
1205         let res = (value * num / den) * BILLIONISH;
1206
1207         res as u64
1208 }
1209
1210 impl<L: Deref<Target = u64>, HT: Deref<Target = HistoricalLiquidityTracker>, T: Deref<Target = Duration>>
1211 DirectedChannelLiquidity< L, HT, T> {
1212         /// Returns a liquidity penalty for routing the given HTLC `amount_msat` through the channel in
1213         /// this direction.
1214         fn penalty_msat(&self, amount_msat: u64, score_params: &ProbabilisticScoringFeeParameters) -> u64 {
1215                 let available_capacity = self.capacity_msat;
1216                 let max_liquidity_msat = self.max_liquidity_msat();
1217                 let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
1218
1219                 let mut res = if amount_msat <= min_liquidity_msat {
1220                         0
1221                 } else if amount_msat >= max_liquidity_msat {
1222                         // Equivalent to hitting the else clause below with the amount equal to the effective
1223                         // capacity and without any certainty on the liquidity upper bound, plus the
1224                         // impossibility penalty.
1225                         let negative_log10_times_2048 = NEGATIVE_LOG10_UPPER_BOUND * 2048;
1226                         Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
1227                                         score_params.liquidity_penalty_multiplier_msat,
1228                                         score_params.liquidity_penalty_amount_multiplier_msat)
1229                                 .saturating_add(score_params.considered_impossible_penalty_msat)
1230                 } else {
1231                         let (numerator, denominator) = success_probability(amount_msat,
1232                                 min_liquidity_msat, max_liquidity_msat, available_capacity, score_params, false);
1233                         if denominator - numerator < denominator / PRECISION_LOWER_BOUND_DENOMINATOR {
1234                                 // If the failure probability is < 1.5625% (as 1 - numerator/denominator < 1/64),
1235                                 // don't bother trying to use the log approximation as it gets too noisy to be
1236                                 // particularly helpful, instead just round down to 0.
1237                                 0
1238                         } else {
1239                                 let negative_log10_times_2048 =
1240                                         log_approx::negative_log10_times_2048(numerator, denominator);
1241                                 Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
1242                                         score_params.liquidity_penalty_multiplier_msat,
1243                                         score_params.liquidity_penalty_amount_multiplier_msat)
1244                         }
1245                 };
1246
1247                 if amount_msat >= available_capacity {
1248                         // We're trying to send more than the capacity, use a max penalty.
1249                         res = res.saturating_add(Self::combined_penalty_msat(amount_msat,
1250                                 NEGATIVE_LOG10_UPPER_BOUND * 2048,
1251                                 score_params.historical_liquidity_penalty_multiplier_msat,
1252                                 score_params.historical_liquidity_penalty_amount_multiplier_msat));
1253                         return res;
1254                 }
1255
1256                 if score_params.historical_liquidity_penalty_multiplier_msat != 0 ||
1257                    score_params.historical_liquidity_penalty_amount_multiplier_msat != 0 {
1258                         if let Some(cumulative_success_prob_times_billion) = self.liquidity_history
1259                                 .calculate_success_probability_times_billion(
1260                                         score_params, amount_msat, self.capacity_msat)
1261                         {
1262                                 let historical_negative_log10_times_2048 =
1263                                         log_approx::negative_log10_times_2048(cumulative_success_prob_times_billion + 1, 1024 * 1024 * 1024);
1264                                 res = res.saturating_add(Self::combined_penalty_msat(amount_msat,
1265                                         historical_negative_log10_times_2048, score_params.historical_liquidity_penalty_multiplier_msat,
1266                                         score_params.historical_liquidity_penalty_amount_multiplier_msat));
1267                         } else {
1268                                 // If we don't have any valid points (or, once decayed, we have less than a full
1269                                 // point), redo the non-historical calculation with no liquidity bounds tracked and
1270                                 // the historical penalty multipliers.
1271                                 let (numerator, denominator) = success_probability(amount_msat, 0,
1272                                         available_capacity, available_capacity, score_params, true);
1273                                 let negative_log10_times_2048 =
1274                                         log_approx::negative_log10_times_2048(numerator, denominator);
1275                                 res = res.saturating_add(Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
1276                                         score_params.historical_liquidity_penalty_multiplier_msat,
1277                                         score_params.historical_liquidity_penalty_amount_multiplier_msat));
1278                         }
1279                 }
1280
1281                 res
1282         }
1283
1284         /// Computes the liquidity penalty from the penalty multipliers.
1285         #[inline(always)]
1286         fn combined_penalty_msat(amount_msat: u64, mut negative_log10_times_2048: u64,
1287                 liquidity_penalty_multiplier_msat: u64, liquidity_penalty_amount_multiplier_msat: u64,
1288         ) -> u64 {
1289                 negative_log10_times_2048 =
1290                         negative_log10_times_2048.min(NEGATIVE_LOG10_UPPER_BOUND * 2048);
1291
1292                 // Upper bound the liquidity penalty to ensure some channel is selected.
1293                 let liquidity_penalty_msat = negative_log10_times_2048
1294                         .saturating_mul(liquidity_penalty_multiplier_msat) / 2048;
1295                 let amount_penalty_msat = negative_log10_times_2048
1296                         .saturating_mul(liquidity_penalty_amount_multiplier_msat)
1297                         .saturating_mul(amount_msat) / 2048 / AMOUNT_PENALTY_DIVISOR;
1298
1299                 liquidity_penalty_msat.saturating_add(amount_penalty_msat)
1300         }
1301
1302         /// Returns the lower bound of the channel liquidity balance in this direction.
1303         #[inline(always)]
1304         fn min_liquidity_msat(&self) -> u64 {
1305                 *self.min_liquidity_offset_msat
1306         }
1307
1308         /// Returns the upper bound of the channel liquidity balance in this direction.
1309         #[inline(always)]
1310         fn max_liquidity_msat(&self) -> u64 {
1311                 self.capacity_msat
1312                         .saturating_sub(*self.max_liquidity_offset_msat)
1313         }
1314 }
1315
1316 impl<L: DerefMut<Target = u64>, HT: DerefMut<Target = HistoricalLiquidityTracker>, T: DerefMut<Target = Duration>>
1317 DirectedChannelLiquidity<L, HT, T> {
1318         /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
1319         fn failed_at_channel<Log: Deref>(
1320                 &mut self, amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
1321         ) where Log::Target: Logger {
1322                 let existing_max_msat = self.max_liquidity_msat();
1323                 if amount_msat < existing_max_msat {
1324                         log_debug!(logger, "Setting max liquidity of {} from {} to {}", chan_descr, existing_max_msat, amount_msat);
1325                         self.set_max_liquidity_msat(amount_msat, duration_since_epoch);
1326                 } else {
1327                         log_trace!(logger, "Max liquidity of {} is {} (already less than or equal to {})",
1328                                 chan_descr, existing_max_msat, amount_msat);
1329                 }
1330                 self.update_history_buckets(0, duration_since_epoch);
1331         }
1332
1333         /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
1334         fn failed_downstream<Log: Deref>(
1335                 &mut self, amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
1336         ) where Log::Target: Logger {
1337                 let existing_min_msat = self.min_liquidity_msat();
1338                 if amount_msat > existing_min_msat {
1339                         log_debug!(logger, "Setting min liquidity of {} from {} to {}", existing_min_msat, chan_descr, amount_msat);
1340                         self.set_min_liquidity_msat(amount_msat, duration_since_epoch);
1341                 } else {
1342                         log_trace!(logger, "Min liquidity of {} is {} (already greater than or equal to {})",
1343                                 chan_descr, existing_min_msat, amount_msat);
1344                 }
1345                 self.update_history_buckets(0, duration_since_epoch);
1346         }
1347
1348         /// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
1349         fn successful<Log: Deref>(&mut self,
1350                 amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
1351         ) where Log::Target: Logger {
1352                 let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
1353                 log_debug!(logger, "Subtracting {} from max liquidity of {} (setting it to {})", amount_msat, chan_descr, max_liquidity_msat);
1354                 self.set_max_liquidity_msat(max_liquidity_msat, duration_since_epoch);
1355                 self.update_history_buckets(amount_msat, duration_since_epoch);
1356         }
1357
1358         /// Updates the history buckets for this channel. Because the history buckets track what we now
1359         /// know about the channel's state *prior to our payment* (i.e. what we assume is "steady
1360         /// state"), we allow the caller to set an offset applied to our liquidity bounds which
1361         /// represents the amount of the successful payment we just made.
1362         fn update_history_buckets(&mut self, bucket_offset_msat: u64, duration_since_epoch: Duration) {
1363                 self.liquidity_history.track_datapoint(
1364                         *self.min_liquidity_offset_msat + bucket_offset_msat,
1365                         self.max_liquidity_offset_msat.saturating_sub(bucket_offset_msat),
1366                         self.capacity_msat,
1367                 );
1368                 *self.offset_history_last_updated = duration_since_epoch;
1369         }
1370
1371         /// Adjusts the lower bound of the channel liquidity balance in this direction.
1372         fn set_min_liquidity_msat(&mut self, amount_msat: u64, duration_since_epoch: Duration) {
1373                 *self.min_liquidity_offset_msat = amount_msat;
1374                 if amount_msat > self.max_liquidity_msat() {
1375                         *self.max_liquidity_offset_msat = 0;
1376                 }
1377                 *self.last_updated = duration_since_epoch;
1378         }
1379
1380         /// Adjusts the upper bound of the channel liquidity balance in this direction.
1381         fn set_max_liquidity_msat(&mut self, amount_msat: u64, duration_since_epoch: Duration) {
1382                 *self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
1383                 if amount_msat < *self.min_liquidity_offset_msat {
1384                         *self.min_liquidity_offset_msat = 0;
1385                 }
1386                 *self.last_updated = duration_since_epoch;
1387         }
1388 }
1389
1390 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ScoreLookUp for ProbabilisticScorer<G, L> where L::Target: Logger {
1391         type ScoreParams = ProbabilisticScoringFeeParameters;
1392         fn channel_penalty_msat(
1393                 &self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters
1394         ) -> u64 {
1395                 let (scid, target) = match candidate {
1396                         CandidateRouteHop::PublicHop { info, short_channel_id } => {
1397                                 (short_channel_id, info.target())
1398                         },
1399                         _ => return 0,
1400                 };
1401                 let source = candidate.source();
1402                 if let Some(penalty) = score_params.manual_node_penalties.get(&target) {
1403                         return *penalty;
1404                 }
1405
1406                 let base_penalty_msat = score_params.base_penalty_msat.saturating_add(
1407                         score_params.base_penalty_amount_multiplier_msat
1408                                 .saturating_mul(usage.amount_msat) / BASE_AMOUNT_PENALTY_DIVISOR);
1409
1410                 let mut anti_probing_penalty_msat = 0;
1411                 match usage.effective_capacity {
1412                         EffectiveCapacity::ExactLiquidity { liquidity_msat: amount_msat } |
1413                                 EffectiveCapacity::HintMaxHTLC { amount_msat } =>
1414                         {
1415                                 if usage.amount_msat > amount_msat {
1416                                         return u64::max_value();
1417                                 } else {
1418                                         return base_penalty_msat;
1419                                 }
1420                         },
1421                         EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat } => {
1422                                 if htlc_maximum_msat >= capacity_msat/2 {
1423                                         anti_probing_penalty_msat = score_params.anti_probing_penalty_msat;
1424                                 }
1425                         },
1426                         _ => {},
1427                 }
1428
1429                 let amount_msat = usage.amount_msat.saturating_add(usage.inflight_htlc_msat);
1430                 let capacity_msat = usage.effective_capacity.as_msat();
1431                 self.channel_liquidities
1432                         .get(&scid)
1433                         .unwrap_or(&ChannelLiquidity::new(Duration::ZERO))
1434                         .as_directed(&source, &target, capacity_msat)
1435                         .penalty_msat(amount_msat, score_params)
1436                         .saturating_add(anti_probing_penalty_msat)
1437                         .saturating_add(base_penalty_msat)
1438         }
1439 }
1440
1441 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ScoreUpdate for ProbabilisticScorer<G, L> where L::Target: Logger {
1442         fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration) {
1443                 let amount_msat = path.final_value_msat();
1444                 log_trace!(self.logger, "Scoring path through to SCID {} as having failed at {} msat", short_channel_id, amount_msat);
1445                 let network_graph = self.network_graph.read_only();
1446                 for (hop_idx, hop) in path.hops.iter().enumerate() {
1447                         let target = NodeId::from_pubkey(&hop.pubkey);
1448                         let channel_directed_from_source = network_graph.channels()
1449                                 .get(&hop.short_channel_id)
1450                                 .and_then(|channel| channel.as_directed_to(&target));
1451
1452                         let at_failed_channel = hop.short_channel_id == short_channel_id;
1453                         if at_failed_channel && hop_idx == 0 {
1454                                 log_warn!(self.logger, "Payment failed at the first hop - we do not attempt to learn channel info in such cases as we can directly observe local state.\n\tBecause we know the local state, we should generally not see failures here - this may be an indication that your channel peer on channel {} is broken and you may wish to close the channel.", hop.short_channel_id);
1455                         }
1456
1457                         // Only score announced channels.
1458                         if let Some((channel, source)) = channel_directed_from_source {
1459                                 let capacity_msat = channel.effective_capacity().as_msat();
1460                                 if at_failed_channel {
1461                                         self.channel_liquidities
1462                                                 .entry(hop.short_channel_id)
1463                                                 .or_insert_with(|| ChannelLiquidity::new(duration_since_epoch))
1464                                                 .as_directed_mut(source, &target, capacity_msat)
1465                                                 .failed_at_channel(amount_msat, duration_since_epoch,
1466                                                         format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
1467                                 } else {
1468                                         self.channel_liquidities
1469                                                 .entry(hop.short_channel_id)
1470                                                 .or_insert_with(|| ChannelLiquidity::new(duration_since_epoch))
1471                                                 .as_directed_mut(source, &target, capacity_msat)
1472                                                 .failed_downstream(amount_msat, duration_since_epoch,
1473                                                         format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
1474                                 }
1475                         } else {
1476                                 log_debug!(self.logger, "Not able to penalize channel with SCID {} as we do not have graph info for it (likely a route-hint last-hop).",
1477                                         hop.short_channel_id);
1478                         }
1479                         if at_failed_channel { break; }
1480                 }
1481         }
1482
1483         fn payment_path_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
1484                 let amount_msat = path.final_value_msat();
1485                 log_trace!(self.logger, "Scoring path through SCID {} as having succeeded at {} msat.",
1486                         path.hops.split_last().map(|(hop, _)| hop.short_channel_id).unwrap_or(0), amount_msat);
1487                 let network_graph = self.network_graph.read_only();
1488                 for hop in &path.hops {
1489                         let target = NodeId::from_pubkey(&hop.pubkey);
1490                         let channel_directed_from_source = network_graph.channels()
1491                                 .get(&hop.short_channel_id)
1492                                 .and_then(|channel| channel.as_directed_to(&target));
1493
1494                         // Only score announced channels.
1495                         if let Some((channel, source)) = channel_directed_from_source {
1496                                 let capacity_msat = channel.effective_capacity().as_msat();
1497                                 self.channel_liquidities
1498                                         .entry(hop.short_channel_id)
1499                                         .or_insert_with(|| ChannelLiquidity::new(duration_since_epoch))
1500                                         .as_directed_mut(source, &target, capacity_msat)
1501                                         .successful(amount_msat, duration_since_epoch,
1502                                                 format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
1503                         } else {
1504                                 log_debug!(self.logger, "Not able to learn for channel with SCID {} as we do not have graph info for it (likely a route-hint last-hop).",
1505                                         hop.short_channel_id);
1506                         }
1507                 }
1508         }
1509
1510         fn probe_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration) {
1511                 self.payment_path_failed(path, short_channel_id, duration_since_epoch)
1512         }
1513
1514         fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
1515                 self.payment_path_failed(path, u64::max_value(), duration_since_epoch)
1516         }
1517
1518         fn decay_liquidity_certainty(&mut self, duration_since_epoch: Duration) {
1519                 let decay_params = self.decay_params;
1520                 self.channel_liquidities.retain(|_scid, liquidity| {
1521                         liquidity.min_liquidity_offset_msat =
1522                                 liquidity.decayed_offset(liquidity.min_liquidity_offset_msat, duration_since_epoch, decay_params);
1523                         liquidity.max_liquidity_offset_msat =
1524                                 liquidity.decayed_offset(liquidity.max_liquidity_offset_msat, duration_since_epoch, decay_params);
1525                         liquidity.last_updated = duration_since_epoch;
1526
1527                         let elapsed_time =
1528                                 duration_since_epoch.saturating_sub(liquidity.offset_history_last_updated);
1529                         if elapsed_time > decay_params.historical_no_updates_half_life {
1530                                 let half_life = decay_params.historical_no_updates_half_life.as_secs_f64();
1531                                 if half_life != 0.0 {
1532                                         liquidity.liquidity_history.decay_buckets(elapsed_time.as_secs_f64() / half_life);
1533                                         liquidity.offset_history_last_updated = duration_since_epoch;
1534                                 }
1535                         }
1536                         liquidity.min_liquidity_offset_msat != 0 || liquidity.max_liquidity_offset_msat != 0 ||
1537                                 liquidity.liquidity_history.has_datapoints()
1538                 });
1539         }
1540 }
1541
1542 #[cfg(c_bindings)]
1543 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> Score for ProbabilisticScorer<G, L>
1544 where L::Target: Logger {}
1545
1546 #[cfg(feature = "std")]
1547 #[inline]
1548 fn powf64(n: f64, exp: f64) -> f64 {
1549         n.powf(exp)
1550 }
1551 #[cfg(not(feature = "std"))]
1552 fn powf64(n: f64, exp: f64) -> f64 {
1553         libm::powf(n as f32, exp as f32) as f64
1554 }
1555
1556 mod bucketed_history {
1557         use super::*;
1558
1559         // Because liquidity is often skewed heavily in one direction, we store historical state
1560         // distribution in buckets of different size. For backwards compatibility, buckets of size 1/8th
1561         // must fit evenly into the buckets here.
1562         //
1563         // The smallest bucket is 2^-14th of the channel, for each of our 32 buckets here we define the
1564         // width of the bucket in 2^14'ths of the channel. This increases exponentially until we reach
1565         // a full 16th of the channel's capacity, which is reapeated a few times for backwards
1566         // compatibility. The four middle buckets represent full octiles of the channel's capacity.
1567         //
1568         // For a 1 BTC channel, this let's us differentiate between failures in the bottom 6k sats, or
1569         // between the 12,000th sat and 24,000th sat, while only needing to store and operate on 32
1570         // buckets in total.
1571
1572         // By default u16s may not be cache-aligned, but we'd rather not have to read a third cache
1573         // line just to access it
1574         #[repr(align(128))]
1575         struct BucketStartPos([u16; 33]);
1576         impl BucketStartPos {
1577                 const fn new() -> Self {
1578                         Self([
1579                                 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 3072, 4096, 6144, 8192, 10240, 12288,
1580                                 13312, 14336, 15360, 15872, 16128, 16256, 16320, 16352, 16368, 16376, 16380, 16382, 16383, 16384,
1581                         ])
1582                 }
1583         }
1584         impl core::ops::Index<usize> for BucketStartPos {
1585                 type Output = u16;
1586                 #[inline(always)]
1587                 fn index(&self, index: usize) -> &u16 { &self.0[index] }
1588         }
1589         const BUCKET_START_POS: BucketStartPos = BucketStartPos::new();
1590
1591         const LEGACY_TO_BUCKET_RANGE: [(u8, u8); 8] = [
1592                 (0, 12), (12, 14), (14, 15), (15, 16), (16, 17), (17, 18), (18, 20), (20, 32)
1593         ];
1594
1595         const POSITION_TICKS: u16 = 1 << 14;
1596
1597         fn pos_to_bucket(pos: u16) -> usize {
1598                 for bucket in 0..32 {
1599                         if pos < BUCKET_START_POS[bucket + 1] {
1600                                 return bucket;
1601                         }
1602                 }
1603                 debug_assert!(false);
1604                 return 32;
1605         }
1606
1607         #[cfg(test)]
1608         #[test]
1609         fn check_bucket_maps() {
1610                 const BUCKET_WIDTH_IN_16384S: [u16; 32] = [
1611                         1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 1024, 1024, 2048, 2048,
1612                         2048, 2048, 1024, 1024, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 1];
1613
1614                 let mut min_size_iter = 0;
1615                 let mut legacy_bucket_iter = 0;
1616                 for (bucket, width) in BUCKET_WIDTH_IN_16384S.iter().enumerate() {
1617                         assert_eq!(BUCKET_START_POS[bucket], min_size_iter);
1618                         for i in 0..*width {
1619                                 assert_eq!(pos_to_bucket(min_size_iter + i) as usize, bucket);
1620                         }
1621                         min_size_iter += *width;
1622                         if min_size_iter % (POSITION_TICKS / 8) == 0 {
1623                                 assert_eq!(LEGACY_TO_BUCKET_RANGE[legacy_bucket_iter].1 as usize, bucket + 1);
1624                                 if legacy_bucket_iter + 1 < 8 {
1625                                         assert_eq!(LEGACY_TO_BUCKET_RANGE[legacy_bucket_iter + 1].0 as usize, bucket + 1);
1626                                 }
1627                                 legacy_bucket_iter += 1;
1628                         }
1629                 }
1630                 assert_eq!(BUCKET_START_POS[32], POSITION_TICKS);
1631                 assert_eq!(min_size_iter, POSITION_TICKS);
1632         }
1633
1634         #[inline]
1635         fn amount_to_pos(amount_msat: u64, capacity_msat: u64) -> u16 {
1636                 let pos = if amount_msat < u64::max_value() / (POSITION_TICKS as u64) {
1637                         (amount_msat * (POSITION_TICKS as u64) / capacity_msat.saturating_add(1))
1638                                 .try_into().unwrap_or(POSITION_TICKS)
1639                 } else {
1640                         // Only use 128-bit arithmetic when multiplication will overflow to avoid 128-bit
1641                         // division. This branch should only be hit in fuzz testing since the amount would
1642                         // need to be over 2.88 million BTC in practice.
1643                         ((amount_msat as u128) * (POSITION_TICKS as u128)
1644                                         / (capacity_msat as u128).saturating_add(1))
1645                                 .try_into().unwrap_or(POSITION_TICKS)
1646                 };
1647                 // If we are running in a client that doesn't validate gossip, its possible for a channel's
1648                 // capacity to change due to a `channel_update` message which, if received while a payment
1649                 // is in-flight, could cause this to fail. Thus, we only assert in test.
1650                 #[cfg(test)]
1651                 debug_assert!(pos < POSITION_TICKS);
1652                 pos
1653         }
1654
1655         /// Prior to LDK 0.0.117 we used eight buckets which were split evenly across the either
1656         /// octiles. This was changed to use 32 buckets for accuracy reasons in 0.0.117, however we
1657         /// support reading the legacy values here for backwards compatibility.
1658         pub(super) struct LegacyHistoricalBucketRangeTracker {
1659                 buckets: [u16; 8],
1660         }
1661
1662         impl LegacyHistoricalBucketRangeTracker {
1663                 pub(crate) fn into_current(&self) -> HistoricalBucketRangeTracker {
1664                         let mut buckets = [0; 32];
1665                         for (idx, legacy_bucket) in self.buckets.iter().enumerate() {
1666                                 let mut new_val = *legacy_bucket;
1667                                 let (start, end) = LEGACY_TO_BUCKET_RANGE[idx];
1668                                 new_val /= (end - start) as u16;
1669                                 for i in start..end {
1670                                         buckets[i as usize] = new_val;
1671                                 }
1672                         }
1673                         HistoricalBucketRangeTracker { buckets }
1674                 }
1675         }
1676
1677         /// Tracks the historical state of a distribution as a weighted average of how much time was spent
1678         /// in each of 32 buckets.
1679         #[derive(Clone, Copy)]
1680         pub(super) struct HistoricalBucketRangeTracker {
1681                 buckets: [u16; 32],
1682         }
1683
1684         /// Buckets are stored in fixed point numbers with a 5 bit fractional part. Thus, the value
1685         /// "one" is 32, or this constant.
1686         pub const BUCKET_FIXED_POINT_ONE: u16 = 32;
1687
1688         impl HistoricalBucketRangeTracker {
1689                 pub(super) fn new() -> Self { Self { buckets: [0; 32] } }
1690                 fn track_datapoint(&mut self, liquidity_offset_msat: u64, capacity_msat: u64) {
1691                         // We have 32 leaky buckets for min and max liquidity. Each bucket tracks the amount of time
1692                         // we spend in each bucket as a 16-bit fixed-point number with a 5 bit fractional part.
1693                         //
1694                         // Each time we update our liquidity estimate, we add 32 (1.0 in our fixed-point system) to
1695                         // the buckets for the current min and max liquidity offset positions.
1696                         //
1697                         // We then decay each bucket by multiplying by 2047/2048 (avoiding dividing by a
1698                         // non-power-of-two). This ensures we can't actually overflow the u16 - when we get to
1699                         // 63,457 adding 32 and decaying by 2047/2048 leaves us back at 63,457.
1700                         //
1701                         // In total, this allows us to track data for the last 8,000 or so payments across a given
1702                         // channel.
1703                         //
1704                         // These constants are a balance - we try to fit in 2 bytes per bucket to reduce overhead,
1705                         // and need to balance having more bits in the decimal part (to ensure decay isn't too
1706                         // non-linear) with having too few bits in the mantissa, causing us to not store very many
1707                         // datapoints.
1708                         //
1709                         // The constants were picked experimentally, selecting a decay amount that restricts us
1710                         // from overflowing buckets without having to cap them manually.
1711
1712                         let pos: u16 = amount_to_pos(liquidity_offset_msat, capacity_msat);
1713                         if pos < POSITION_TICKS {
1714                                 for e in self.buckets.iter_mut() {
1715                                         *e = ((*e as u32) * 2047 / 2048) as u16;
1716                                 }
1717                                 let bucket = pos_to_bucket(pos);
1718                                 self.buckets[bucket] = self.buckets[bucket].saturating_add(BUCKET_FIXED_POINT_ONE);
1719                         }
1720                 }
1721         }
1722
1723         impl_writeable_tlv_based!(HistoricalBucketRangeTracker, { (0, buckets, required) });
1724         impl_writeable_tlv_based!(LegacyHistoricalBucketRangeTracker, { (0, buckets, required) });
1725
1726         #[derive(Clone, Copy)]
1727         #[repr(C)] // Force the fields in memory to be in the order we specify.
1728         pub(super) struct HistoricalLiquidityTracker {
1729                 total_valid_points_tracked: u64,
1730                 min_liquidity_offset_history: HistoricalBucketRangeTracker,
1731                 max_liquidity_offset_history: HistoricalBucketRangeTracker,
1732         }
1733
1734         impl HistoricalLiquidityTracker {
1735                 pub(super) fn new() -> HistoricalLiquidityTracker {
1736                         HistoricalLiquidityTracker {
1737                                 min_liquidity_offset_history: HistoricalBucketRangeTracker::new(),
1738                                 max_liquidity_offset_history: HistoricalBucketRangeTracker::new(),
1739                                 total_valid_points_tracked: 0,
1740                         }
1741                 }
1742
1743                 pub(super) fn from_min_max(
1744                         min_liquidity_offset_history: HistoricalBucketRangeTracker,
1745                         max_liquidity_offset_history: HistoricalBucketRangeTracker,
1746                 ) -> HistoricalLiquidityTracker {
1747                         let mut res = HistoricalLiquidityTracker {
1748                                 min_liquidity_offset_history,
1749                                 max_liquidity_offset_history,
1750                                 total_valid_points_tracked: 0,
1751                         };
1752                         res.recalculate_valid_points();
1753                         res
1754                 }
1755
1756                 pub(super) fn has_datapoints(&self) -> bool {
1757                         self.min_liquidity_offset_history.buckets != [0; 32] ||
1758                                 self.max_liquidity_offset_history.buckets != [0; 32]
1759                 }
1760
1761                 pub(super) fn decay_buckets(&mut self, half_lives: f64) {
1762                         let divisor = powf64(2048.0, half_lives) as u64;
1763                         for bucket in self.min_liquidity_offset_history.buckets.iter_mut() {
1764                                 *bucket = ((*bucket as u64) * 1024 / divisor) as u16;
1765                         }
1766                         for bucket in self.max_liquidity_offset_history.buckets.iter_mut() {
1767                                 *bucket = ((*bucket as u64) * 1024 / divisor) as u16;
1768                         }
1769                         self.recalculate_valid_points();
1770                 }
1771
1772                 fn recalculate_valid_points(&mut self) {
1773                         self.total_valid_points_tracked = 0;
1774                         for (min_idx, min_bucket) in self.min_liquidity_offset_history.buckets.iter().enumerate() {
1775                                 for max_bucket in self.max_liquidity_offset_history.buckets.iter().take(32 - min_idx) {
1776                                         self.total_valid_points_tracked += (*min_bucket as u64) * (*max_bucket as u64);
1777                                 }
1778                         }
1779                 }
1780
1781                 pub(super) fn writeable_min_offset_history(&self) -> &HistoricalBucketRangeTracker {
1782                         &self.min_liquidity_offset_history
1783                 }
1784
1785                 pub(super) fn writeable_max_offset_history(&self) -> &HistoricalBucketRangeTracker {
1786                         &self.max_liquidity_offset_history
1787                 }
1788
1789                 pub(super) fn as_directed<'a>(&'a self, source_less_than_target: bool)
1790                 -> DirectedHistoricalLiquidityTracker<&'a HistoricalLiquidityTracker> {
1791                         DirectedHistoricalLiquidityTracker { source_less_than_target, tracker: self }
1792                 }
1793
1794                 pub(super) fn as_directed_mut<'a>(&'a mut self, source_less_than_target: bool)
1795                 -> DirectedHistoricalLiquidityTracker<&'a mut HistoricalLiquidityTracker> {
1796                         DirectedHistoricalLiquidityTracker { source_less_than_target, tracker: self }
1797                 }
1798         }
1799
1800         /// A set of buckets representing the history of where we've seen the minimum- and maximum-
1801         /// liquidity bounds for a given channel.
1802         pub(super) struct DirectedHistoricalLiquidityTracker<D: Deref<Target = HistoricalLiquidityTracker>> {
1803                 source_less_than_target: bool,
1804                 tracker: D,
1805         }
1806
1807         impl<D: DerefMut<Target = HistoricalLiquidityTracker>> DirectedHistoricalLiquidityTracker<D> {
1808                 pub(super) fn track_datapoint(
1809                         &mut self, min_offset_msat: u64, max_offset_msat: u64, capacity_msat: u64,
1810                 ) {
1811                         if self.source_less_than_target {
1812                                 self.tracker.min_liquidity_offset_history.track_datapoint(min_offset_msat, capacity_msat);
1813                                 self.tracker.max_liquidity_offset_history.track_datapoint(max_offset_msat, capacity_msat);
1814                         } else {
1815                                 self.tracker.max_liquidity_offset_history.track_datapoint(min_offset_msat, capacity_msat);
1816                                 self.tracker.min_liquidity_offset_history.track_datapoint(max_offset_msat, capacity_msat);
1817                         }
1818                         self.tracker.recalculate_valid_points();
1819                 }
1820         }
1821
1822         impl<D: Deref<Target = HistoricalLiquidityTracker>> DirectedHistoricalLiquidityTracker<D> {
1823                 pub(super) fn min_liquidity_offset_history_buckets(&self) -> &[u16; 32] {
1824                         if self.source_less_than_target {
1825                                 &self.tracker.min_liquidity_offset_history.buckets
1826                         } else {
1827                                 &self.tracker.max_liquidity_offset_history.buckets
1828                         }
1829                 }
1830
1831                 pub(super) fn max_liquidity_offset_history_buckets(&self) -> &[u16; 32] {
1832                         if self.source_less_than_target {
1833                                 &self.tracker.max_liquidity_offset_history.buckets
1834                         } else {
1835                                 &self.tracker.min_liquidity_offset_history.buckets
1836                         }
1837                 }
1838
1839                 #[inline]
1840                 pub(super) fn calculate_success_probability_times_billion(
1841                         &self, params: &ProbabilisticScoringFeeParameters, amount_msat: u64,
1842                         capacity_msat: u64
1843                 ) -> Option<u64> {
1844                         // If historical penalties are enabled, we try to calculate a probability of success
1845                         // given our historical distribution of min- and max-liquidity bounds in a channel.
1846                         // To do so, we walk the set of historical liquidity bucket (min, max) combinations
1847                         // (where min_idx < max_idx, as having a minimum above our maximum is an invalid
1848                         // state). For each pair, we calculate the probability as if the bucket's corresponding
1849                         // min- and max- liquidity bounds were our current liquidity bounds and then multiply
1850                         // that probability by the weight of the selected buckets.
1851                         let payment_pos = amount_to_pos(amount_msat, capacity_msat);
1852                         if payment_pos >= POSITION_TICKS { return None; }
1853
1854                         let min_liquidity_offset_history_buckets =
1855                                 self.min_liquidity_offset_history_buckets();
1856                         let max_liquidity_offset_history_buckets =
1857                                 self.max_liquidity_offset_history_buckets();
1858
1859                         let total_valid_points_tracked = self.tracker.total_valid_points_tracked;
1860                         #[cfg(debug_assertions)] {
1861                                 let mut actual_valid_points_tracked = 0;
1862                                 for (min_idx, min_bucket) in min_liquidity_offset_history_buckets.iter().enumerate() {
1863                                         for max_bucket in max_liquidity_offset_history_buckets.iter().take(32 - min_idx) {
1864                                                 actual_valid_points_tracked += (*min_bucket as u64) * (*max_bucket as u64);
1865                                         }
1866                                 }
1867                                 assert_eq!(total_valid_points_tracked, actual_valid_points_tracked);
1868                         }
1869
1870                         // If the total valid points is smaller than 1.0 (i.e. 32 in our fixed-point scheme),
1871                         // treat it as if we were fully decayed.
1872                         const FULLY_DECAYED: u16 = BUCKET_FIXED_POINT_ONE * BUCKET_FIXED_POINT_ONE;
1873                         if total_valid_points_tracked < FULLY_DECAYED.into() {
1874                                 return None;
1875                         }
1876
1877                         let mut cumulative_success_prob_times_billion = 0;
1878                         // Special-case the 0th min bucket - it generally means we failed a payment, so only
1879                         // consider the highest (i.e. largest-offset-from-max-capacity) max bucket for all
1880                         // points against the 0th min bucket. This avoids the case where we fail to route
1881                         // increasingly lower values over a channel, but treat each failure as a separate
1882                         // datapoint, many of which may have relatively high maximum-available-liquidity
1883                         // values, which will result in us thinking we have some nontrivial probability of
1884                         // routing up to that amount.
1885                         if min_liquidity_offset_history_buckets[0] != 0 {
1886                                 let mut highest_max_bucket_with_points = 0; // The highest max-bucket with any data
1887                                 let mut total_max_points = 0; // Total points in max-buckets to consider
1888                                 for (max_idx, max_bucket) in max_liquidity_offset_history_buckets.iter().enumerate() {
1889                                         if *max_bucket >= BUCKET_FIXED_POINT_ONE {
1890                                                 highest_max_bucket_with_points = cmp::max(highest_max_bucket_with_points, max_idx);
1891                                         }
1892                                         total_max_points += *max_bucket as u64;
1893                                 }
1894                                 let max_bucket_end_pos = BUCKET_START_POS[32 - highest_max_bucket_with_points] - 1;
1895                                 if payment_pos < max_bucket_end_pos {
1896                                         let bucket_points = (min_liquidity_offset_history_buckets[0] as u64) * total_max_points;
1897                                         cumulative_success_prob_times_billion += success_probability_times_value_times_billion(
1898                                                 payment_pos as u64, 0, max_bucket_end_pos as u64,
1899                                                 POSITION_TICKS as u64 - 1, params, true,
1900                                                 bucket_points, total_valid_points_tracked
1901                                         );
1902                                 }
1903                         }
1904
1905                         for (min_idx, min_bucket) in min_liquidity_offset_history_buckets.iter().enumerate().skip(1) {
1906                                 let min_bucket_start_pos = BUCKET_START_POS[min_idx];
1907                                 if payment_pos < min_bucket_start_pos {
1908                                         for (max_idx, max_bucket) in max_liquidity_offset_history_buckets.iter().enumerate().take(32 - min_idx) {
1909                                                 let max_bucket_end_pos = BUCKET_START_POS[32 - max_idx] - 1;
1910                                                 if payment_pos >= max_bucket_end_pos {
1911                                                         // Success probability 0, the payment amount may be above the max liquidity
1912                                                         break;
1913                                                 }
1914                                                 // Note that this multiply can only barely not overflow - two 16 bit ints plus
1915                                                 // 30 bits is 62 bits.
1916                                                 let bucket_prob_times_billion = ((*min_bucket as u32) * (*max_bucket as u32)) as u64
1917                                                         * 1024 * 1024 * 1024 / total_valid_points_tracked;
1918                                                 debug_assert!(bucket_prob_times_billion < u32::max_value() as u64);
1919                                                 cumulative_success_prob_times_billion += bucket_prob_times_billion;
1920                                         }
1921                                 } else {
1922                                         for (max_idx, max_bucket) in max_liquidity_offset_history_buckets.iter().enumerate().take(32 - min_idx) {
1923                                                 let max_bucket_end_pos = BUCKET_START_POS[32 - max_idx] - 1;
1924                                                 if payment_pos >= max_bucket_end_pos {
1925                                                         // Success probability 0, the payment amount may be above the max liquidity
1926                                                         break;
1927                                                 }
1928                                                 // Note that this multiply can only barely not overflow - two 16 bit ints plus
1929                                                 // 30 bits is 62 bits.
1930                                                 let bucket_points = ((*min_bucket as u32) * (*max_bucket as u32)) as u64;
1931                                                 cumulative_success_prob_times_billion += success_probability_times_value_times_billion(
1932                                                         payment_pos as u64, min_bucket_start_pos as u64,
1933                                                         max_bucket_end_pos as u64, POSITION_TICKS as u64 - 1, params, true,
1934                                                         bucket_points, total_valid_points_tracked);
1935                                         }
1936                                 }
1937                         }
1938
1939                         Some(cumulative_success_prob_times_billion)
1940                 }
1941         }
1942 }
1943 use bucketed_history::{LegacyHistoricalBucketRangeTracker, HistoricalBucketRangeTracker, DirectedHistoricalLiquidityTracker, HistoricalLiquidityTracker};
1944
1945 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> Writeable for ProbabilisticScorer<G, L> where L::Target: Logger {
1946         #[inline]
1947         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1948                 write_tlv_fields!(w, {
1949                         (0, self.channel_liquidities, required),
1950                 });
1951                 Ok(())
1952         }
1953 }
1954
1955 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref>
1956 ReadableArgs<(ProbabilisticScoringDecayParameters, G, L)> for ProbabilisticScorer<G, L> where L::Target: Logger {
1957         #[inline]
1958         fn read<R: Read>(
1959                 r: &mut R, args: (ProbabilisticScoringDecayParameters, G, L)
1960         ) -> Result<Self, DecodeError> {
1961                 let (decay_params, network_graph, logger) = args;
1962                 let mut channel_liquidities = HashMap::new();
1963                 read_tlv_fields!(r, {
1964                         (0, channel_liquidities, required),
1965                 });
1966                 Ok(Self {
1967                         decay_params,
1968                         network_graph,
1969                         logger,
1970                         channel_liquidities,
1971                 })
1972         }
1973 }
1974
1975 impl Writeable for ChannelLiquidity {
1976         #[inline]
1977         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1978                 write_tlv_fields!(w, {
1979                         (0, self.min_liquidity_offset_msat, required),
1980                         // 1 was the min_liquidity_offset_history in octile form
1981                         (2, self.max_liquidity_offset_msat, required),
1982                         // 3 was the max_liquidity_offset_history in octile form
1983                         (4, self.last_updated, required),
1984                         (5, self.liquidity_history.writeable_min_offset_history(), required),
1985                         (7, self.liquidity_history.writeable_max_offset_history(), required),
1986                         (9, self.offset_history_last_updated, required),
1987                 });
1988                 Ok(())
1989         }
1990 }
1991
1992 impl Readable for ChannelLiquidity {
1993         #[inline]
1994         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1995                 let mut min_liquidity_offset_msat = 0;
1996                 let mut max_liquidity_offset_msat = 0;
1997                 let mut legacy_min_liq_offset_history: Option<LegacyHistoricalBucketRangeTracker> = None;
1998                 let mut legacy_max_liq_offset_history: Option<LegacyHistoricalBucketRangeTracker> = None;
1999                 let mut min_liquidity_offset_history: Option<HistoricalBucketRangeTracker> = None;
2000                 let mut max_liquidity_offset_history: Option<HistoricalBucketRangeTracker> = None;
2001                 let mut last_updated = Duration::from_secs(0);
2002                 let mut offset_history_last_updated = None;
2003                 read_tlv_fields!(r, {
2004                         (0, min_liquidity_offset_msat, required),
2005                         (1, legacy_min_liq_offset_history, option),
2006                         (2, max_liquidity_offset_msat, required),
2007                         (3, legacy_max_liq_offset_history, option),
2008                         (4, last_updated, required),
2009                         (5, min_liquidity_offset_history, option),
2010                         (7, max_liquidity_offset_history, option),
2011                         (9, offset_history_last_updated, option),
2012                 });
2013
2014                 if min_liquidity_offset_history.is_none() {
2015                         if let Some(legacy_buckets) = legacy_min_liq_offset_history {
2016                                 min_liquidity_offset_history = Some(legacy_buckets.into_current());
2017                         } else {
2018                                 min_liquidity_offset_history = Some(HistoricalBucketRangeTracker::new());
2019                         }
2020                 }
2021                 if max_liquidity_offset_history.is_none() {
2022                         if let Some(legacy_buckets) = legacy_max_liq_offset_history {
2023                                 max_liquidity_offset_history = Some(legacy_buckets.into_current());
2024                         } else {
2025                                 max_liquidity_offset_history = Some(HistoricalBucketRangeTracker::new());
2026                         }
2027                 }
2028                 Ok(Self {
2029                         min_liquidity_offset_msat,
2030                         max_liquidity_offset_msat,
2031                         liquidity_history: HistoricalLiquidityTracker::from_min_max(
2032                                 min_liquidity_offset_history.unwrap(), max_liquidity_offset_history.unwrap()
2033                         ),
2034                         last_updated,
2035                         offset_history_last_updated: offset_history_last_updated.unwrap_or(last_updated),
2036                 })
2037         }
2038 }
2039
2040 #[cfg(test)]
2041 mod tests {
2042         use super::{ChannelLiquidity, HistoricalLiquidityTracker, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters, ProbabilisticScorer};
2043         use crate::blinded_path::{BlindedHop, BlindedPath};
2044         use crate::util::config::UserConfig;
2045
2046         use crate::ln::channelmanager;
2047         use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
2048         use crate::routing::gossip::{EffectiveCapacity, NetworkGraph, NodeId};
2049         use crate::routing::router::{BlindedTail, Path, RouteHop, CandidateRouteHop};
2050         use crate::routing::scoring::{ChannelUsage, ScoreLookUp, ScoreUpdate};
2051         use crate::util::ser::{ReadableArgs, Writeable};
2052         use crate::util::test_utils::{self, TestLogger};
2053
2054         use bitcoin::blockdata::constants::ChainHash;
2055         use bitcoin::hashes::Hash;
2056         use bitcoin::hashes::sha256d::Hash as Sha256dHash;
2057         use bitcoin::network::constants::Network;
2058         use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
2059         use core::time::Duration;
2060         use crate::io;
2061
2062         fn source_privkey() -> SecretKey {
2063                 SecretKey::from_slice(&[42; 32]).unwrap()
2064         }
2065
2066         fn target_privkey() -> SecretKey {
2067                 SecretKey::from_slice(&[43; 32]).unwrap()
2068         }
2069
2070         fn source_pubkey() -> PublicKey {
2071                 let secp_ctx = Secp256k1::new();
2072                 PublicKey::from_secret_key(&secp_ctx, &source_privkey())
2073         }
2074
2075         fn target_pubkey() -> PublicKey {
2076                 let secp_ctx = Secp256k1::new();
2077                 PublicKey::from_secret_key(&secp_ctx, &target_privkey())
2078         }
2079
2080         fn source_node_id() -> NodeId {
2081                 NodeId::from_pubkey(&source_pubkey())
2082         }
2083
2084         fn target_node_id() -> NodeId {
2085                 NodeId::from_pubkey(&target_pubkey())
2086         }
2087
2088         // `ProbabilisticScorer` tests
2089
2090         fn sender_privkey() -> SecretKey {
2091                 SecretKey::from_slice(&[41; 32]).unwrap()
2092         }
2093
2094         fn recipient_privkey() -> SecretKey {
2095                 SecretKey::from_slice(&[45; 32]).unwrap()
2096         }
2097
2098         fn sender_pubkey() -> PublicKey {
2099                 let secp_ctx = Secp256k1::new();
2100                 PublicKey::from_secret_key(&secp_ctx, &sender_privkey())
2101         }
2102
2103         fn recipient_pubkey() -> PublicKey {
2104                 let secp_ctx = Secp256k1::new();
2105                 PublicKey::from_secret_key(&secp_ctx, &recipient_privkey())
2106         }
2107
2108         fn recipient_node_id() -> NodeId {
2109                 NodeId::from_pubkey(&recipient_pubkey())
2110         }
2111
2112         fn network_graph(logger: &TestLogger) -> NetworkGraph<&TestLogger> {
2113                 let mut network_graph = NetworkGraph::new(Network::Testnet, logger);
2114                 add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
2115                 add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
2116
2117                 network_graph
2118         }
2119
2120         fn add_channel(
2121                 network_graph: &mut NetworkGraph<&TestLogger>, short_channel_id: u64, node_1_key: SecretKey,
2122                 node_2_key: SecretKey
2123         ) {
2124                 let genesis_hash = ChainHash::using_genesis_block(Network::Testnet);
2125                 let node_1_secret = &SecretKey::from_slice(&[39; 32]).unwrap();
2126                 let node_2_secret = &SecretKey::from_slice(&[40; 32]).unwrap();
2127                 let secp_ctx = Secp256k1::new();
2128                 let unsigned_announcement = UnsignedChannelAnnouncement {
2129                         features: channelmanager::provided_channel_features(&UserConfig::default()),
2130                         chain_hash: genesis_hash,
2131                         short_channel_id,
2132                         node_id_1: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_1_key)),
2133                         node_id_2: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_2_key)),
2134                         bitcoin_key_1: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_1_secret)),
2135                         bitcoin_key_2: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_2_secret)),
2136                         excess_data: Vec::new(),
2137                 };
2138                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
2139                 let signed_announcement = ChannelAnnouncement {
2140                         node_signature_1: secp_ctx.sign_ecdsa(&msghash, &node_1_key),
2141                         node_signature_2: secp_ctx.sign_ecdsa(&msghash, &node_2_key),
2142                         bitcoin_signature_1: secp_ctx.sign_ecdsa(&msghash, &node_1_secret),
2143                         bitcoin_signature_2: secp_ctx.sign_ecdsa(&msghash, &node_2_secret),
2144                         contents: unsigned_announcement,
2145                 };
2146                 let chain_source: Option<&crate::util::test_utils::TestChainSource> = None;
2147                 network_graph.update_channel_from_announcement(
2148                         &signed_announcement, &chain_source).unwrap();
2149                 update_channel(network_graph, short_channel_id, node_1_key, 0, 1_000, 100);
2150                 update_channel(network_graph, short_channel_id, node_2_key, 1, 0, 100);
2151         }
2152
2153         fn update_channel(
2154                 network_graph: &mut NetworkGraph<&TestLogger>, short_channel_id: u64, node_key: SecretKey,
2155                 flags: u8, htlc_maximum_msat: u64, timestamp: u32,
2156         ) {
2157                 let genesis_hash = ChainHash::using_genesis_block(Network::Testnet);
2158                 let secp_ctx = Secp256k1::new();
2159                 let unsigned_update = UnsignedChannelUpdate {
2160                         chain_hash: genesis_hash,
2161                         short_channel_id,
2162                         timestamp,
2163                         flags,
2164                         cltv_expiry_delta: 18,
2165                         htlc_minimum_msat: 0,
2166                         htlc_maximum_msat,
2167                         fee_base_msat: 1,
2168                         fee_proportional_millionths: 0,
2169                         excess_data: Vec::new(),
2170                 };
2171                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
2172                 let signed_update = ChannelUpdate {
2173                         signature: secp_ctx.sign_ecdsa(&msghash, &node_key),
2174                         contents: unsigned_update,
2175                 };
2176                 network_graph.update_channel(&signed_update).unwrap();
2177         }
2178
2179         fn path_hop(pubkey: PublicKey, short_channel_id: u64, fee_msat: u64) -> RouteHop {
2180                 let config = UserConfig::default();
2181                 RouteHop {
2182                         pubkey,
2183                         node_features: channelmanager::provided_node_features(&config),
2184                         short_channel_id,
2185                         channel_features: channelmanager::provided_channel_features(&config),
2186                         fee_msat,
2187                         cltv_expiry_delta: 18,
2188                         maybe_announced_channel: true,
2189                 }
2190         }
2191
2192         fn payment_path_for_amount(amount_msat: u64) -> Path {
2193                 Path {
2194                         hops: vec![
2195                                 path_hop(source_pubkey(), 41, 1),
2196                                 path_hop(target_pubkey(), 42, 2),
2197                                 path_hop(recipient_pubkey(), 43, amount_msat),
2198                         ], blinded_tail: None,
2199                 }
2200         }
2201
2202         #[test]
2203         fn liquidity_bounds_directed_from_lowest_node_id() {
2204                 let logger = TestLogger::new();
2205                 let last_updated = Duration::ZERO;
2206                 let offset_history_last_updated = Duration::ZERO;
2207                 let network_graph = network_graph(&logger);
2208                 let decay_params = ProbabilisticScoringDecayParameters::default();
2209                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
2210                         .with_channel(42,
2211                                 ChannelLiquidity {
2212                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100,
2213                                         last_updated, offset_history_last_updated,
2214                                         liquidity_history: HistoricalLiquidityTracker::new(),
2215                                 })
2216                         .with_channel(43,
2217                                 ChannelLiquidity {
2218                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100,
2219                                         last_updated, offset_history_last_updated,
2220                                         liquidity_history: HistoricalLiquidityTracker::new(),
2221                                 });
2222                 let source = source_node_id();
2223                 let target = target_node_id();
2224                 let recipient = recipient_node_id();
2225                 assert!(source > target);
2226                 assert!(target < recipient);
2227
2228                 // Update minimum liquidity.
2229
2230                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2231                         .as_directed(&source, &target, 1_000);
2232                 assert_eq!(liquidity.min_liquidity_msat(), 100);
2233                 assert_eq!(liquidity.max_liquidity_msat(), 300);
2234
2235                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2236                         .as_directed(&target, &source, 1_000);
2237                 assert_eq!(liquidity.min_liquidity_msat(), 700);
2238                 assert_eq!(liquidity.max_liquidity_msat(), 900);
2239
2240                 scorer.channel_liquidities.get_mut(&42).unwrap()
2241                         .as_directed_mut(&source, &target, 1_000)
2242                         .set_min_liquidity_msat(200, Duration::ZERO);
2243
2244                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2245                         .as_directed(&source, &target, 1_000);
2246                 assert_eq!(liquidity.min_liquidity_msat(), 200);
2247                 assert_eq!(liquidity.max_liquidity_msat(), 300);
2248
2249                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2250                         .as_directed(&target, &source, 1_000);
2251                 assert_eq!(liquidity.min_liquidity_msat(), 700);
2252                 assert_eq!(liquidity.max_liquidity_msat(), 800);
2253
2254                 // Update maximum liquidity.
2255
2256                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
2257                         .as_directed(&target, &recipient, 1_000);
2258                 assert_eq!(liquidity.min_liquidity_msat(), 700);
2259                 assert_eq!(liquidity.max_liquidity_msat(), 900);
2260
2261                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
2262                         .as_directed(&recipient, &target, 1_000);
2263                 assert_eq!(liquidity.min_liquidity_msat(), 100);
2264                 assert_eq!(liquidity.max_liquidity_msat(), 300);
2265
2266                 scorer.channel_liquidities.get_mut(&43).unwrap()
2267                         .as_directed_mut(&target, &recipient, 1_000)
2268                         .set_max_liquidity_msat(200, Duration::ZERO);
2269
2270                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
2271                         .as_directed(&target, &recipient, 1_000);
2272                 assert_eq!(liquidity.min_liquidity_msat(), 0);
2273                 assert_eq!(liquidity.max_liquidity_msat(), 200);
2274
2275                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
2276                         .as_directed(&recipient, &target, 1_000);
2277                 assert_eq!(liquidity.min_liquidity_msat(), 800);
2278                 assert_eq!(liquidity.max_liquidity_msat(), 1000);
2279         }
2280
2281         #[test]
2282         fn resets_liquidity_upper_bound_when_crossed_by_lower_bound() {
2283                 let logger = TestLogger::new();
2284                 let last_updated = Duration::ZERO;
2285                 let offset_history_last_updated = Duration::ZERO;
2286                 let network_graph = network_graph(&logger);
2287                 let decay_params = ProbabilisticScoringDecayParameters::default();
2288                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
2289                         .with_channel(42,
2290                                 ChannelLiquidity {
2291                                         min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400,
2292                                         last_updated, offset_history_last_updated,
2293                                         liquidity_history: HistoricalLiquidityTracker::new(),
2294                                 });
2295                 let source = source_node_id();
2296                 let target = target_node_id();
2297                 assert!(source > target);
2298
2299                 // Check initial bounds.
2300                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2301                         .as_directed(&source, &target, 1_000);
2302                 assert_eq!(liquidity.min_liquidity_msat(), 400);
2303                 assert_eq!(liquidity.max_liquidity_msat(), 800);
2304
2305                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2306                         .as_directed(&target, &source, 1_000);
2307                 assert_eq!(liquidity.min_liquidity_msat(), 200);
2308                 assert_eq!(liquidity.max_liquidity_msat(), 600);
2309
2310                 // Reset from source to target.
2311                 scorer.channel_liquidities.get_mut(&42).unwrap()
2312                         .as_directed_mut(&source, &target, 1_000)
2313                         .set_min_liquidity_msat(900, Duration::ZERO);
2314
2315                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2316                         .as_directed(&source, &target, 1_000);
2317                 assert_eq!(liquidity.min_liquidity_msat(), 900);
2318                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
2319
2320                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2321                         .as_directed(&target, &source, 1_000);
2322                 assert_eq!(liquidity.min_liquidity_msat(), 0);
2323                 assert_eq!(liquidity.max_liquidity_msat(), 100);
2324
2325                 // Reset from target to source.
2326                 scorer.channel_liquidities.get_mut(&42).unwrap()
2327                         .as_directed_mut(&target, &source, 1_000)
2328                         .set_min_liquidity_msat(400, Duration::ZERO);
2329
2330                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2331                         .as_directed(&source, &target, 1_000);
2332                 assert_eq!(liquidity.min_liquidity_msat(), 0);
2333                 assert_eq!(liquidity.max_liquidity_msat(), 600);
2334
2335                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2336                         .as_directed(&target, &source, 1_000);
2337                 assert_eq!(liquidity.min_liquidity_msat(), 400);
2338                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
2339         }
2340
2341         #[test]
2342         fn resets_liquidity_lower_bound_when_crossed_by_upper_bound() {
2343                 let logger = TestLogger::new();
2344                 let last_updated = Duration::ZERO;
2345                 let offset_history_last_updated = Duration::ZERO;
2346                 let network_graph = network_graph(&logger);
2347                 let decay_params = ProbabilisticScoringDecayParameters::default();
2348                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
2349                         .with_channel(42,
2350                                 ChannelLiquidity {
2351                                         min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400,
2352                                         last_updated, offset_history_last_updated,
2353                                         liquidity_history: HistoricalLiquidityTracker::new(),
2354                                 });
2355                 let source = source_node_id();
2356                 let target = target_node_id();
2357                 assert!(source > target);
2358
2359                 // Check initial bounds.
2360                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2361                         .as_directed(&source, &target, 1_000);
2362                 assert_eq!(liquidity.min_liquidity_msat(), 400);
2363                 assert_eq!(liquidity.max_liquidity_msat(), 800);
2364
2365                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2366                         .as_directed(&target, &source, 1_000);
2367                 assert_eq!(liquidity.min_liquidity_msat(), 200);
2368                 assert_eq!(liquidity.max_liquidity_msat(), 600);
2369
2370                 // Reset from source to target.
2371                 scorer.channel_liquidities.get_mut(&42).unwrap()
2372                         .as_directed_mut(&source, &target, 1_000)
2373                         .set_max_liquidity_msat(300, Duration::ZERO);
2374
2375                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2376                         .as_directed(&source, &target, 1_000);
2377                 assert_eq!(liquidity.min_liquidity_msat(), 0);
2378                 assert_eq!(liquidity.max_liquidity_msat(), 300);
2379
2380                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2381                         .as_directed(&target, &source, 1_000);
2382                 assert_eq!(liquidity.min_liquidity_msat(), 700);
2383                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
2384
2385                 // Reset from target to source.
2386                 scorer.channel_liquidities.get_mut(&42).unwrap()
2387                         .as_directed_mut(&target, &source, 1_000)
2388                         .set_max_liquidity_msat(600, Duration::ZERO);
2389
2390                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2391                         .as_directed(&source, &target, 1_000);
2392                 assert_eq!(liquidity.min_liquidity_msat(), 400);
2393                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
2394
2395                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
2396                         .as_directed(&target, &source, 1_000);
2397                 assert_eq!(liquidity.min_liquidity_msat(), 0);
2398                 assert_eq!(liquidity.max_liquidity_msat(), 600);
2399         }
2400
2401         #[test]
2402         fn increased_penalty_nearing_liquidity_upper_bound() {
2403                 let logger = TestLogger::new();
2404                 let network_graph = network_graph(&logger);
2405                 let params = ProbabilisticScoringFeeParameters {
2406                         liquidity_penalty_multiplier_msat: 1_000,
2407                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2408                 };
2409                 let decay_params = ProbabilisticScoringDecayParameters::default();
2410                 let scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
2411                 let source = source_node_id();
2412
2413                 let usage = ChannelUsage {
2414                         amount_msat: 1_024,
2415                         inflight_htlc_msat: 0,
2416                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: 1_000 },
2417                 };
2418                 let network_graph = network_graph.read_only();
2419                 let channel = network_graph.channel(42).unwrap();
2420                 let (info, _) = channel.as_directed_from(&source).unwrap();
2421                 let candidate = CandidateRouteHop::PublicHop {
2422                         info,
2423                         short_channel_id: 42,
2424                 };
2425                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2426                 let usage = ChannelUsage { amount_msat: 10_240, ..usage };
2427                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2428                 let usage = ChannelUsage { amount_msat: 102_400, ..usage };
2429                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 47);
2430                 let usage = ChannelUsage { amount_msat: 1_023_999, ..usage };
2431                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 2_000);
2432
2433                 let usage = ChannelUsage {
2434                         amount_msat: 128,
2435                         inflight_htlc_msat: 0,
2436                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_000 },
2437                 };
2438                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 58);
2439                 let usage = ChannelUsage { amount_msat: 256, ..usage };
2440                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 125);
2441                 let usage = ChannelUsage { amount_msat: 374, ..usage };
2442                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 198);
2443                 let usage = ChannelUsage { amount_msat: 512, ..usage };
2444                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 300);
2445                 let usage = ChannelUsage { amount_msat: 640, ..usage };
2446                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 425);
2447                 let usage = ChannelUsage { amount_msat: 768, ..usage };
2448                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 602);
2449                 let usage = ChannelUsage { amount_msat: 896, ..usage };
2450                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 902);
2451         }
2452
2453         #[test]
2454         fn constant_penalty_outside_liquidity_bounds() {
2455                 let logger = TestLogger::new();
2456                 let last_updated = Duration::ZERO;
2457                 let offset_history_last_updated = Duration::ZERO;
2458                 let network_graph = network_graph(&logger);
2459                 let params = ProbabilisticScoringFeeParameters {
2460                         liquidity_penalty_multiplier_msat: 1_000,
2461                         considered_impossible_penalty_msat: u64::max_value(),
2462                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2463                 };
2464                 let decay_params = ProbabilisticScoringDecayParameters {
2465                         ..ProbabilisticScoringDecayParameters::zero_penalty()
2466                 };
2467                 let scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
2468                         .with_channel(42,
2469                                 ChannelLiquidity {
2470                                         min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40,
2471                                         last_updated, offset_history_last_updated,
2472                                         liquidity_history: HistoricalLiquidityTracker::new(),
2473                                 });
2474                 let source = source_node_id();
2475
2476                 let usage = ChannelUsage {
2477                         amount_msat: 39,
2478                         inflight_htlc_msat: 0,
2479                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 100, htlc_maximum_msat: 1_000 },
2480                 };
2481                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2482                 let (info, _) = channel.as_directed_from(&source).unwrap();
2483                 let candidate = CandidateRouteHop::PublicHop {
2484                         info,
2485                         short_channel_id: 42,
2486                 };
2487                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2488                 let usage = ChannelUsage { amount_msat: 50, ..usage };
2489                 assert_ne!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2490                 assert_ne!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2491                 let usage = ChannelUsage { amount_msat: 61, ..usage };
2492                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2493         }
2494
2495         #[test]
2496         fn does_not_further_penalize_own_channel() {
2497                 let logger = TestLogger::new();
2498                 let network_graph = network_graph(&logger);
2499                 let params = ProbabilisticScoringFeeParameters {
2500                         liquidity_penalty_multiplier_msat: 1_000,
2501                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2502                 };
2503                 let mut scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
2504                 let source = source_node_id();
2505                 let usage = ChannelUsage {
2506                         amount_msat: 500,
2507                         inflight_htlc_msat: 0,
2508                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
2509                 };
2510                 let failed_path = payment_path_for_amount(500);
2511                 let successful_path = payment_path_for_amount(200);
2512                 let channel = &network_graph.read_only().channel(42).unwrap().to_owned();
2513                 let (info, _) = channel.as_directed_from(&source).unwrap();
2514                 let candidate = CandidateRouteHop::PublicHop {
2515                         info,
2516                         short_channel_id: 41,
2517                 };
2518
2519                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 301);
2520
2521                 scorer.payment_path_failed(&failed_path, 41, Duration::ZERO);
2522                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 301);
2523
2524                 scorer.payment_path_successful(&successful_path, Duration::ZERO);
2525                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 301);
2526         }
2527
2528         #[test]
2529         fn sets_liquidity_lower_bound_on_downstream_failure() {
2530                 let logger = TestLogger::new();
2531                 let network_graph = network_graph(&logger);
2532                 let params = ProbabilisticScoringFeeParameters {
2533                         liquidity_penalty_multiplier_msat: 1_000,
2534                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2535                 };
2536                 let mut scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
2537                 let source = source_node_id();
2538                 let path = payment_path_for_amount(500);
2539
2540                 let usage = ChannelUsage {
2541                         amount_msat: 250,
2542                         inflight_htlc_msat: 0,
2543                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
2544                 };
2545                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2546                 let (info, _) = channel.as_directed_from(&source).unwrap();
2547                 let candidate = CandidateRouteHop::PublicHop {
2548                         info,
2549                         short_channel_id: 42,
2550                 };
2551                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 128);
2552                 let usage = ChannelUsage { amount_msat: 500, ..usage };
2553                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 301);
2554                 let usage = ChannelUsage { amount_msat: 750, ..usage };
2555                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 602);
2556
2557                 scorer.payment_path_failed(&path, 43, Duration::ZERO);
2558
2559                 let usage = ChannelUsage { amount_msat: 250, ..usage };
2560                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2561                 let usage = ChannelUsage { amount_msat: 500, ..usage };
2562                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2563                 let usage = ChannelUsage { amount_msat: 750, ..usage };
2564                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 300);
2565         }
2566
2567         #[test]
2568         fn sets_liquidity_upper_bound_on_failure() {
2569                 let logger = TestLogger::new();
2570                 let network_graph = network_graph(&logger);
2571                 let params = ProbabilisticScoringFeeParameters {
2572                         liquidity_penalty_multiplier_msat: 1_000,
2573                         considered_impossible_penalty_msat: u64::max_value(),
2574                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2575                 };
2576                 let mut scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
2577                 let source = source_node_id();
2578                 let path = payment_path_for_amount(500);
2579
2580                 let usage = ChannelUsage {
2581                         amount_msat: 250,
2582                         inflight_htlc_msat: 0,
2583                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
2584                 };
2585                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2586                 let (info, _) = channel.as_directed_from(&source).unwrap();
2587                 let candidate = CandidateRouteHop::PublicHop {
2588                         info,
2589                         short_channel_id: 42,
2590                 };
2591                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 128);
2592                 let usage = ChannelUsage { amount_msat: 500, ..usage };
2593                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 301);
2594                 let usage = ChannelUsage { amount_msat: 750, ..usage };
2595                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 602);
2596
2597                 scorer.payment_path_failed(&path, 42, Duration::ZERO);
2598
2599                 let usage = ChannelUsage { amount_msat: 250, ..usage };
2600                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 300);
2601                 let usage = ChannelUsage { amount_msat: 500, ..usage };
2602                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2603                 let usage = ChannelUsage { amount_msat: 750, ..usage };
2604                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2605         }
2606
2607         #[test]
2608         fn ignores_channels_after_removed_failed_channel() {
2609                 // Previously, if we'd tried to send over a channel which was removed from the network
2610                 // graph before we call `payment_path_failed` (which is the default if the we get a "no
2611                 // such channel" error in the `InvoicePayer`), we would call `failed_downstream` on all
2612                 // channels in the route, even ones which they payment never reached. This tests to ensure
2613                 // we do not score such channels.
2614                 let secp_ctx = Secp256k1::new();
2615                 let logger = TestLogger::new();
2616                 let mut network_graph = NetworkGraph::new(Network::Testnet, &logger);
2617                 let secret_a = SecretKey::from_slice(&[42; 32]).unwrap();
2618                 let secret_b = SecretKey::from_slice(&[43; 32]).unwrap();
2619                 let secret_c = SecretKey::from_slice(&[44; 32]).unwrap();
2620                 let secret_d = SecretKey::from_slice(&[45; 32]).unwrap();
2621                 add_channel(&mut network_graph, 42, secret_a, secret_b);
2622                 // Don't add the channel from B -> C.
2623                 add_channel(&mut network_graph, 44, secret_c, secret_d);
2624
2625                 let pub_a = PublicKey::from_secret_key(&secp_ctx, &secret_a);
2626                 let pub_b = PublicKey::from_secret_key(&secp_ctx, &secret_b);
2627                 let pub_c = PublicKey::from_secret_key(&secp_ctx, &secret_c);
2628                 let pub_d = PublicKey::from_secret_key(&secp_ctx, &secret_d);
2629
2630                 let path = vec![
2631                         path_hop(pub_b, 42, 1),
2632                         path_hop(pub_c, 43, 2),
2633                         path_hop(pub_d, 44, 100),
2634                 ];
2635
2636                 let node_a = NodeId::from_pubkey(&pub_a);
2637                 let node_b = NodeId::from_pubkey(&pub_b);
2638                 let node_c = NodeId::from_pubkey(&pub_c);
2639
2640                 let params = ProbabilisticScoringFeeParameters {
2641                         liquidity_penalty_multiplier_msat: 1_000,
2642                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2643                 };
2644                 let mut scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
2645
2646                 let usage = ChannelUsage {
2647                         amount_msat: 250,
2648                         inflight_htlc_msat: 0,
2649                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
2650                 };
2651                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2652                 let (info, _) = channel.as_directed_from(&node_a).unwrap();
2653                 let candidate = CandidateRouteHop::PublicHop {
2654                         info,
2655                         short_channel_id: 42,
2656                 };
2657                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 128);
2658                 // Note that a default liquidity bound is used for B -> C as no channel exists
2659                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2660                 let (info, _) = channel.as_directed_from(&node_b).unwrap();
2661                 let candidate = CandidateRouteHop::PublicHop {
2662                         info,
2663                         short_channel_id: 43,
2664                 };
2665                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 128);
2666                 let channel = network_graph.read_only().channel(44).unwrap().to_owned();
2667                 let (info, _) = channel.as_directed_from(&node_c).unwrap();
2668                 let candidate = CandidateRouteHop::PublicHop {
2669                         info,
2670                         short_channel_id: 44,
2671                 };
2672                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 128);
2673
2674                 scorer.payment_path_failed(&Path { hops: path, blinded_tail: None }, 43, Duration::ZERO);
2675
2676                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2677                 let (info, _) = channel.as_directed_from(&node_a).unwrap();
2678                 let candidate = CandidateRouteHop::PublicHop {
2679                         info,
2680                         short_channel_id: 42,
2681                 };
2682                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 80);
2683                 // Note that a default liquidity bound is used for B -> C as no channel exists
2684                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2685                 let (info, _) = channel.as_directed_from(&node_b).unwrap();
2686                 let candidate = CandidateRouteHop::PublicHop {
2687                         info,
2688                         short_channel_id: 43,
2689                 };
2690                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 128);
2691                 let channel = network_graph.read_only().channel(44).unwrap().to_owned();
2692                 let (info, _) = channel.as_directed_from(&node_c).unwrap();
2693                 let candidate = CandidateRouteHop::PublicHop {
2694                         info,
2695                         short_channel_id: 44,
2696                 };
2697                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 128);
2698         }
2699
2700         #[test]
2701         fn reduces_liquidity_upper_bound_along_path_on_success() {
2702                 let logger = TestLogger::new();
2703                 let network_graph = network_graph(&logger);
2704                 let params = ProbabilisticScoringFeeParameters {
2705                         liquidity_penalty_multiplier_msat: 1_000,
2706                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2707                 };
2708                 let mut scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
2709                 let source = source_node_id();
2710                 let usage = ChannelUsage {
2711                         amount_msat: 250,
2712                         inflight_htlc_msat: 0,
2713                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
2714                 };
2715                 let network_graph = network_graph.read_only().channels().clone();
2716                 let channel_42 = network_graph.get(&42).unwrap();
2717                 let channel_43 = network_graph.get(&43).unwrap();
2718                 let (info, _) = channel_42.as_directed_from(&source).unwrap();
2719                 let candidate_41 = CandidateRouteHop::PublicHop {
2720                         info,
2721                         short_channel_id: 41,
2722                 };
2723                 let (info, target) = channel_42.as_directed_from(&source).unwrap();
2724                 let candidate_42 = CandidateRouteHop::PublicHop {
2725                         info,
2726                         short_channel_id: 42,
2727                 };
2728                 let (info, _) = channel_43.as_directed_from(&target).unwrap();
2729                 let candidate_43 = CandidateRouteHop::PublicHop {
2730                         info,
2731                         short_channel_id: 43,
2732                 };
2733                 assert_eq!(scorer.channel_penalty_msat(&candidate_41, usage, &params), 128);
2734                 assert_eq!(scorer.channel_penalty_msat(&candidate_42, usage, &params), 128);
2735                 assert_eq!(scorer.channel_penalty_msat(&candidate_43, usage, &params), 128);
2736
2737                 scorer.payment_path_successful(&payment_path_for_amount(500), Duration::ZERO);
2738
2739                 assert_eq!(scorer.channel_penalty_msat(&candidate_41, usage, &params), 128);
2740                 assert_eq!(scorer.channel_penalty_msat(&candidate_42, usage, &params), 300);
2741                 assert_eq!(scorer.channel_penalty_msat(&candidate_43, usage, &params), 300);
2742         }
2743
2744         #[test]
2745         fn decays_liquidity_bounds_over_time() {
2746                 let logger = TestLogger::new();
2747                 let network_graph = network_graph(&logger);
2748                 let params = ProbabilisticScoringFeeParameters {
2749                         liquidity_penalty_multiplier_msat: 1_000,
2750                         considered_impossible_penalty_msat: u64::max_value(),
2751                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2752                 };
2753                 let decay_params = ProbabilisticScoringDecayParameters {
2754                         liquidity_offset_half_life: Duration::from_secs(10),
2755                         ..ProbabilisticScoringDecayParameters::zero_penalty()
2756                 };
2757                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
2758                 let source = source_node_id();
2759
2760                 let usage = ChannelUsage {
2761                         amount_msat: 0,
2762                         inflight_htlc_msat: 0,
2763                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_024 },
2764                 };
2765                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2766                 let (info, _) = channel.as_directed_from(&source).unwrap();
2767                 let candidate = CandidateRouteHop::PublicHop {
2768                         info,
2769                         short_channel_id: 42,
2770                 };
2771                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2772                 let usage = ChannelUsage { amount_msat: 1_023, ..usage };
2773                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 2_000);
2774
2775                 scorer.payment_path_failed(&payment_path_for_amount(768), 42, Duration::ZERO);
2776                 scorer.payment_path_failed(&payment_path_for_amount(128), 43, Duration::ZERO);
2777
2778                 // Initial penalties
2779                 let usage = ChannelUsage { amount_msat: 128, ..usage };
2780                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2781                 let usage = ChannelUsage { amount_msat: 256, ..usage };
2782                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 93);
2783                 let usage = ChannelUsage { amount_msat: 768, ..usage };
2784                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 1_479);
2785                 let usage = ChannelUsage { amount_msat: 896, ..usage };
2786                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2787
2788                 // Half decay (i.e., three-quarter life)
2789                 scorer.decay_liquidity_certainty(Duration::from_secs(5));
2790                 let usage = ChannelUsage { amount_msat: 128, ..usage };
2791                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 22);
2792                 let usage = ChannelUsage { amount_msat: 256, ..usage };
2793                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 106);
2794                 let usage = ChannelUsage { amount_msat: 768, ..usage };
2795                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 921);
2796                 let usage = ChannelUsage { amount_msat: 896, ..usage };
2797                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2798
2799                 // One decay (i.e., half life)
2800                 scorer.decay_liquidity_certainty(Duration::from_secs(10));
2801                 let usage = ChannelUsage { amount_msat: 64, ..usage };
2802                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2803                 let usage = ChannelUsage { amount_msat: 128, ..usage };
2804                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 34);
2805                 let usage = ChannelUsage { amount_msat: 896, ..usage };
2806                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 1_970);
2807                 let usage = ChannelUsage { amount_msat: 960, ..usage };
2808                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2809
2810                 // Fully decay liquidity lower bound.
2811                 scorer.decay_liquidity_certainty(Duration::from_secs(10 * 8));
2812                 let usage = ChannelUsage { amount_msat: 0, ..usage };
2813                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2814                 let usage = ChannelUsage { amount_msat: 1, ..usage };
2815                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2816                 let usage = ChannelUsage { amount_msat: 1_023, ..usage };
2817                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 2_000);
2818                 let usage = ChannelUsage { amount_msat: 1_024, ..usage };
2819                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2820
2821                 // Fully decay liquidity upper bound.
2822                 scorer.decay_liquidity_certainty(Duration::from_secs(10 * 9));
2823                 let usage = ChannelUsage { amount_msat: 0, ..usage };
2824                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2825                 let usage = ChannelUsage { amount_msat: 1_024, ..usage };
2826                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2827
2828                 scorer.decay_liquidity_certainty(Duration::from_secs(10 * 10));
2829                 let usage = ChannelUsage { amount_msat: 0, ..usage };
2830                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
2831                 let usage = ChannelUsage { amount_msat: 1_024, ..usage };
2832                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2833         }
2834
2835         #[test]
2836         fn restricts_liquidity_bounds_after_decay() {
2837                 let logger = TestLogger::new();
2838                 let network_graph = network_graph(&logger);
2839                 let params = ProbabilisticScoringFeeParameters {
2840                         liquidity_penalty_multiplier_msat: 1_000,
2841                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2842                 };
2843                 let decay_params = ProbabilisticScoringDecayParameters {
2844                         liquidity_offset_half_life: Duration::from_secs(10),
2845                         ..ProbabilisticScoringDecayParameters::default()
2846                 };
2847                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
2848                 let source = source_node_id();
2849                 let usage = ChannelUsage {
2850                         amount_msat: 512,
2851                         inflight_htlc_msat: 0,
2852                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_000 },
2853                 };
2854                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2855                 let (info, _) = channel.as_directed_from(&source).unwrap();
2856                 let candidate = CandidateRouteHop::PublicHop {
2857                         info,
2858                         short_channel_id: 42,
2859                 };
2860
2861                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 300);
2862
2863                 // More knowledge gives higher confidence (256, 768), meaning a lower penalty.
2864                 scorer.payment_path_failed(&payment_path_for_amount(768), 42, Duration::ZERO);
2865                 scorer.payment_path_failed(&payment_path_for_amount(256), 43, Duration::ZERO);
2866                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 281);
2867
2868                 // Decaying knowledge gives less confidence (128, 896), meaning a higher penalty.
2869                 scorer.decay_liquidity_certainty(Duration::from_secs(10));
2870                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 291);
2871
2872                 // Reducing the upper bound gives more confidence (128, 832) that the payment amount (512)
2873                 // is closer to the upper bound, meaning a higher penalty.
2874                 scorer.payment_path_successful(&payment_path_for_amount(64), Duration::from_secs(10));
2875                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 331);
2876
2877                 // Increasing the lower bound gives more confidence (256, 832) that the payment amount (512)
2878                 // is closer to the lower bound, meaning a lower penalty.
2879                 scorer.payment_path_failed(&payment_path_for_amount(256), 43, Duration::from_secs(10));
2880                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 245);
2881
2882                 // Further decaying affects the lower bound more than the upper bound (128, 928).
2883                 scorer.decay_liquidity_certainty(Duration::from_secs(20));
2884                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 280);
2885         }
2886
2887         #[test]
2888         fn restores_persisted_liquidity_bounds() {
2889                 let logger = TestLogger::new();
2890                 let network_graph = network_graph(&logger);
2891                 let params = ProbabilisticScoringFeeParameters {
2892                         liquidity_penalty_multiplier_msat: 1_000,
2893                         considered_impossible_penalty_msat: u64::max_value(),
2894                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2895                 };
2896                 let decay_params = ProbabilisticScoringDecayParameters {
2897                         liquidity_offset_half_life: Duration::from_secs(10),
2898                         ..ProbabilisticScoringDecayParameters::default()
2899                 };
2900                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
2901                 let source = source_node_id();
2902                 let usage = ChannelUsage {
2903                         amount_msat: 500,
2904                         inflight_htlc_msat: 0,
2905                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
2906                 };
2907
2908                 scorer.payment_path_failed(&payment_path_for_amount(500), 42, Duration::ZERO);
2909                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2910                 let (info, _) = channel.as_directed_from(&source).unwrap();
2911                 let candidate = CandidateRouteHop::PublicHop {
2912                         info,
2913                         short_channel_id: 42,
2914                 };
2915                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2916
2917                 scorer.decay_liquidity_certainty(Duration::from_secs(10));
2918                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 473);
2919
2920                 scorer.payment_path_failed(&payment_path_for_amount(250), 43, Duration::from_secs(10));
2921                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 300);
2922
2923                 let mut serialized_scorer = Vec::new();
2924                 scorer.write(&mut serialized_scorer).unwrap();
2925
2926                 let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
2927                 let deserialized_scorer =
2928                         <ProbabilisticScorer<_, _>>::read(&mut serialized_scorer, (decay_params, &network_graph, &logger)).unwrap();
2929                 assert_eq!(deserialized_scorer.channel_penalty_msat(&candidate, usage, &params), 300);
2930         }
2931
2932         fn do_decays_persisted_liquidity_bounds(decay_before_reload: bool) {
2933                 let logger = TestLogger::new();
2934                 let network_graph = network_graph(&logger);
2935                 let params = ProbabilisticScoringFeeParameters {
2936                         liquidity_penalty_multiplier_msat: 1_000,
2937                         considered_impossible_penalty_msat: u64::max_value(),
2938                         ..ProbabilisticScoringFeeParameters::zero_penalty()
2939                 };
2940                 let decay_params = ProbabilisticScoringDecayParameters {
2941                         liquidity_offset_half_life: Duration::from_secs(10),
2942                         ..ProbabilisticScoringDecayParameters::zero_penalty()
2943                 };
2944                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
2945                 let source = source_node_id();
2946                 let usage = ChannelUsage {
2947                         amount_msat: 500,
2948                         inflight_htlc_msat: 0,
2949                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
2950                 };
2951
2952                 scorer.payment_path_failed(&payment_path_for_amount(500), 42, Duration::ZERO);
2953                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
2954                 let (info, _) = channel.as_directed_from(&source).unwrap();
2955                 let candidate = CandidateRouteHop::PublicHop {
2956                         info,
2957                         short_channel_id: 42,
2958                 };
2959                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
2960
2961                 if decay_before_reload {
2962                         scorer.decay_liquidity_certainty(Duration::from_secs(10));
2963                 }
2964
2965                 let mut serialized_scorer = Vec::new();
2966                 scorer.write(&mut serialized_scorer).unwrap();
2967
2968                 let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
2969                 let mut deserialized_scorer =
2970                         <ProbabilisticScorer<_, _>>::read(&mut serialized_scorer, (decay_params, &network_graph, &logger)).unwrap();
2971                 if !decay_before_reload {
2972                         scorer.decay_liquidity_certainty(Duration::from_secs(10));
2973                         deserialized_scorer.decay_liquidity_certainty(Duration::from_secs(10));
2974                 }
2975                 assert_eq!(deserialized_scorer.channel_penalty_msat(&candidate, usage, &params), 473);
2976
2977                 scorer.payment_path_failed(&payment_path_for_amount(250), 43, Duration::from_secs(10));
2978                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 300);
2979
2980                 deserialized_scorer.decay_liquidity_certainty(Duration::from_secs(20));
2981                 assert_eq!(deserialized_scorer.channel_penalty_msat(&candidate, usage, &params), 370);
2982         }
2983
2984         #[test]
2985         fn decays_persisted_liquidity_bounds() {
2986                 do_decays_persisted_liquidity_bounds(false);
2987                 do_decays_persisted_liquidity_bounds(true);
2988         }
2989
2990         #[test]
2991         fn scores_realistic_payments() {
2992                 // Shows the scores of "realistic" sends of 100k sats over channels of 1-10m sats (with a
2993                 // 50k sat reserve).
2994                 let logger = TestLogger::new();
2995                 let network_graph = network_graph(&logger);
2996                 let params = ProbabilisticScoringFeeParameters::default();
2997                 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
2998                 let source = source_node_id();
2999
3000                 let usage = ChannelUsage {
3001                         amount_msat: 100_000_000,
3002                         inflight_htlc_msat: 0,
3003                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 950_000_000, htlc_maximum_msat: 1_000 },
3004                 };
3005                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
3006                 let (info, _) = channel.as_directed_from(&source).unwrap();
3007                 let candidate = CandidateRouteHop::PublicHop {
3008                         info,
3009                         short_channel_id: 42,
3010                 };
3011                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 11497);
3012                 let usage = ChannelUsage {
3013                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_950_000_000, htlc_maximum_msat: 1_000 }, ..usage
3014                 };
3015                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 7408);
3016                 let usage = ChannelUsage {
3017                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 2_950_000_000, htlc_maximum_msat: 1_000 }, ..usage
3018                 };
3019                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 6151);
3020                 let usage = ChannelUsage {
3021                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 3_950_000_000, htlc_maximum_msat: 1_000 }, ..usage
3022                 };
3023                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 5427);
3024                 let usage = ChannelUsage {
3025                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 4_950_000_000, htlc_maximum_msat: 1_000 }, ..usage
3026                 };
3027                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 4955);
3028                 let usage = ChannelUsage {
3029                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 5_950_000_000, htlc_maximum_msat: 1_000 }, ..usage
3030                 };
3031                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 4736);
3032                 let usage = ChannelUsage {
3033                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 6_950_000_000, htlc_maximum_msat: 1_000 }, ..usage
3034                 };
3035                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 4484);
3036                 let usage = ChannelUsage {
3037                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 7_450_000_000, htlc_maximum_msat: 1_000 }, ..usage
3038                 };
3039                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 4484);
3040                 let usage = ChannelUsage {
3041                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 7_950_000_000, htlc_maximum_msat: 1_000 }, ..usage
3042                 };
3043                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 4263);
3044                 let usage = ChannelUsage {
3045                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 8_950_000_000, htlc_maximum_msat: 1_000 }, ..usage
3046                 };
3047                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 4263);
3048                 let usage = ChannelUsage {
3049                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 9_950_000_000, htlc_maximum_msat: 1_000 }, ..usage
3050                 };
3051                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 4044);
3052         }
3053
3054         #[test]
3055         fn adds_base_penalty_to_liquidity_penalty() {
3056                 let logger = TestLogger::new();
3057                 let network_graph = network_graph(&logger);
3058                 let source = source_node_id();
3059                 let usage = ChannelUsage {
3060                         amount_msat: 128,
3061                         inflight_htlc_msat: 0,
3062                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_000 },
3063                 };
3064
3065                 let params = ProbabilisticScoringFeeParameters {
3066                         liquidity_penalty_multiplier_msat: 1_000,
3067                         ..ProbabilisticScoringFeeParameters::zero_penalty()
3068                 };
3069                 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
3070                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
3071                 let (info, _) = channel.as_directed_from(&source).unwrap();
3072                 let candidate = CandidateRouteHop::PublicHop {
3073                         info,
3074                         short_channel_id: 42,
3075                 };
3076                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 58);
3077
3078                 let params = ProbabilisticScoringFeeParameters {
3079                         base_penalty_msat: 500, liquidity_penalty_multiplier_msat: 1_000,
3080                         anti_probing_penalty_msat: 0, ..ProbabilisticScoringFeeParameters::zero_penalty()
3081                 };
3082                 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
3083                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 558);
3084
3085                 let params = ProbabilisticScoringFeeParameters {
3086                         base_penalty_msat: 500, liquidity_penalty_multiplier_msat: 1_000,
3087                         base_penalty_amount_multiplier_msat: (1 << 30),
3088                         anti_probing_penalty_msat: 0, ..ProbabilisticScoringFeeParameters::zero_penalty()
3089                 };
3090
3091                 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
3092                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 558 + 128);
3093         }
3094
3095         #[test]
3096         fn adds_amount_penalty_to_liquidity_penalty() {
3097                 let logger = TestLogger::new();
3098                 let network_graph = network_graph(&logger);
3099                 let source = source_node_id();
3100                 let usage = ChannelUsage {
3101                         amount_msat: 512_000,
3102                         inflight_htlc_msat: 0,
3103                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: 1_000 },
3104                 };
3105
3106                 let params = ProbabilisticScoringFeeParameters {
3107                         liquidity_penalty_multiplier_msat: 1_000,
3108                         liquidity_penalty_amount_multiplier_msat: 0,
3109                         ..ProbabilisticScoringFeeParameters::zero_penalty()
3110                 };
3111                 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
3112                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
3113                 let (info, _) = channel.as_directed_from(&source).unwrap();
3114                 let candidate = CandidateRouteHop::PublicHop {
3115                         info,
3116                         short_channel_id: 42,
3117                 };
3118                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 300);
3119
3120                 let params = ProbabilisticScoringFeeParameters {
3121                         liquidity_penalty_multiplier_msat: 1_000,
3122                         liquidity_penalty_amount_multiplier_msat: 256,
3123                         ..ProbabilisticScoringFeeParameters::zero_penalty()
3124                 };
3125                 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
3126                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 337);
3127         }
3128
3129         #[test]
3130         fn calculates_log10_without_overflowing_u64_max_value() {
3131                 let logger = TestLogger::new();
3132                 let network_graph = network_graph(&logger);
3133                 let source = source_node_id();
3134                 let usage = ChannelUsage {
3135                         amount_msat: u64::max_value(),
3136                         inflight_htlc_msat: 0,
3137                         effective_capacity: EffectiveCapacity::Infinite,
3138                 };
3139                 let params = ProbabilisticScoringFeeParameters {
3140                         liquidity_penalty_multiplier_msat: 40_000,
3141                         ..ProbabilisticScoringFeeParameters::zero_penalty()
3142                 };
3143                 let decay_params = ProbabilisticScoringDecayParameters::zero_penalty();
3144                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
3145                 let (info, _) = channel.as_directed_from(&source).unwrap();
3146                 let candidate = CandidateRouteHop::PublicHop {
3147                         info,
3148                         short_channel_id: 42,
3149                 };
3150                 let scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
3151                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 80_000);
3152         }
3153
3154         #[test]
3155         fn accounts_for_inflight_htlc_usage() {
3156                 let logger = TestLogger::new();
3157                 let network_graph = network_graph(&logger);
3158                 let params = ProbabilisticScoringFeeParameters {
3159                         considered_impossible_penalty_msat: u64::max_value(),
3160                         ..ProbabilisticScoringFeeParameters::zero_penalty()
3161                 };
3162                 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
3163                 let source = source_node_id();
3164
3165                 let usage = ChannelUsage {
3166                         amount_msat: 750,
3167                         inflight_htlc_msat: 0,
3168                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
3169                 };
3170                 let network_graph = network_graph.read_only();
3171                 let channel = network_graph.channel(42).unwrap();
3172                 let (info, _) = channel.as_directed_from(&source).unwrap();
3173                 let candidate = CandidateRouteHop::PublicHop {
3174                         info,
3175                         short_channel_id: 42,
3176                 };
3177                 assert_ne!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
3178
3179                 let usage = ChannelUsage { inflight_htlc_msat: 251, ..usage };
3180                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
3181         }
3182
3183         #[test]
3184         fn removes_uncertainity_when_exact_liquidity_known() {
3185                 let logger = TestLogger::new();
3186                 let network_graph = network_graph(&logger);
3187                 let params = ProbabilisticScoringFeeParameters::default();
3188                 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
3189                 let source = source_node_id();
3190
3191                 let base_penalty_msat = params.base_penalty_msat;
3192                 let usage = ChannelUsage {
3193                         amount_msat: 750,
3194                         inflight_htlc_msat: 0,
3195                         effective_capacity: EffectiveCapacity::ExactLiquidity { liquidity_msat: 1_000 },
3196                 };
3197                 let network_graph = network_graph.read_only();
3198                 let channel = network_graph.channel(42).unwrap();
3199                 let (info, _) = channel.as_directed_from(&source).unwrap();
3200                 let candidate = CandidateRouteHop::PublicHop {
3201                         info,
3202                         short_channel_id: 42,
3203                 };
3204                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), base_penalty_msat);
3205
3206                 let usage = ChannelUsage { amount_msat: 1_000, ..usage };
3207                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), base_penalty_msat);
3208
3209                 let usage = ChannelUsage { amount_msat: 1_001, ..usage };
3210                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), u64::max_value());
3211         }
3212
3213         #[test]
3214         fn remembers_historical_failures() {
3215                 let logger = TestLogger::new();
3216                 let network_graph = network_graph(&logger);
3217                 let params = ProbabilisticScoringFeeParameters {
3218                         historical_liquidity_penalty_multiplier_msat: 1024,
3219                         historical_liquidity_penalty_amount_multiplier_msat: 1024,
3220                         ..ProbabilisticScoringFeeParameters::zero_penalty()
3221                 };
3222                 let decay_params = ProbabilisticScoringDecayParameters {
3223                         liquidity_offset_half_life: Duration::from_secs(60 * 60),
3224                         historical_no_updates_half_life: Duration::from_secs(10),
3225                 };
3226                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
3227                 let source = source_node_id();
3228                 let target = target_node_id();
3229
3230                 let usage = ChannelUsage {
3231                         amount_msat: 100,
3232                         inflight_htlc_msat: 0,
3233                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_024 },
3234                 };
3235                 let usage_1 = ChannelUsage {
3236                         amount_msat: 1,
3237                         inflight_htlc_msat: 0,
3238                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_024 },
3239                 };
3240
3241                 {
3242                         let network_graph = network_graph.read_only();
3243                         let channel = network_graph.channel(42).unwrap();
3244                         let (info, _) = channel.as_directed_from(&source).unwrap();
3245                         let candidate = CandidateRouteHop::PublicHop {
3246                                 info,
3247                                 short_channel_id: 42,
3248                         };
3249
3250                         // With no historical data the normal liquidity penalty calculation is used.
3251                         assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 168);
3252                 }
3253                 assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
3254                 None);
3255                 assert_eq!(scorer.historical_estimated_payment_success_probability(42, &target, 42, &params),
3256                 None);
3257
3258                 scorer.payment_path_failed(&payment_path_for_amount(1), 42, Duration::ZERO);
3259                 {
3260                         let network_graph = network_graph.read_only();
3261                         let channel = network_graph.channel(42).unwrap();
3262                         let (info, _) = channel.as_directed_from(&source).unwrap();
3263                         let candidate = CandidateRouteHop::PublicHop {
3264                                 info,
3265                                 short_channel_id: 42,
3266                         };
3267
3268                         assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 2048);
3269                         assert_eq!(scorer.channel_penalty_msat(&candidate, usage_1, &params), 249);
3270                 }
3271                 // The "it failed" increment is 32, where the probability should lie several buckets into
3272                 // the first octile.
3273                 assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
3274                         Some(([32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
3275                                 [0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])));
3276                 assert!(scorer.historical_estimated_payment_success_probability(42, &target, 1, &params)
3277                         .unwrap() > 0.35);
3278                 assert_eq!(scorer.historical_estimated_payment_success_probability(42, &target, 500, &params),
3279                         Some(0.0));
3280
3281                 // Even after we tell the scorer we definitely have enough available liquidity, it will
3282                 // still remember that there was some failure in the past, and assign a non-0 penalty.
3283                 scorer.payment_path_failed(&payment_path_for_amount(1000), 43, Duration::ZERO);
3284                 {
3285                         let network_graph = network_graph.read_only();
3286                         let channel = network_graph.channel(42).unwrap();
3287                         let (info, _) = channel.as_directed_from(&source).unwrap();
3288                         let candidate = CandidateRouteHop::PublicHop {
3289                                 info,
3290                                 short_channel_id: 42,
3291                         };
3292
3293                         assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 105);
3294                 }
3295                 // The first points should be decayed just slightly and the last bucket has a new point.
3296                 assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
3297                         Some(([31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0],
3298                                 [0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32])));
3299
3300                 // The exact success probability is a bit complicated and involves integer rounding, so we
3301                 // simply check bounds here.
3302                 let five_hundred_prob =
3303                         scorer.historical_estimated_payment_success_probability(42, &target, 500, &params).unwrap();
3304                 assert!(five_hundred_prob > 0.59);
3305                 assert!(five_hundred_prob < 0.60);
3306                 let one_prob =
3307                         scorer.historical_estimated_payment_success_probability(42, &target, 1, &params).unwrap();
3308                 assert!(one_prob < 0.85);
3309                 assert!(one_prob > 0.84);
3310
3311                 // Advance the time forward 16 half-lives (which the docs claim will ensure all data is
3312                 // gone), and check that we're back to where we started.
3313                 scorer.decay_liquidity_certainty(Duration::from_secs(10 * 16));
3314                 {
3315                         let network_graph = network_graph.read_only();
3316                         let channel = network_graph.channel(42).unwrap();
3317                         let (info, _) = channel.as_directed_from(&source).unwrap();
3318                         let candidate = CandidateRouteHop::PublicHop {
3319                                 info,
3320                                 short_channel_id: 42,
3321                         };
3322
3323                         assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 168);
3324                 }
3325                 // Once fully decayed we still have data, but its all-0s. In the future we may remove the
3326                 // data entirely instead.
3327                 assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
3328                         Some(([0; 32], [0; 32])));
3329                 assert_eq!(scorer.historical_estimated_payment_success_probability(42, &target, 1, &params), None);
3330
3331                 let mut usage = ChannelUsage {
3332                         amount_msat: 100,
3333                         inflight_htlc_msat: 1024,
3334                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_024 },
3335                 };
3336                 scorer.payment_path_failed(&payment_path_for_amount(1), 42, Duration::from_secs(10 * 16));
3337                 {
3338                         let network_graph = network_graph.read_only();
3339                         let channel = network_graph.channel(42).unwrap();
3340                         let (info, _) = channel.as_directed_from(&source).unwrap();
3341                         let candidate = CandidateRouteHop::PublicHop {
3342                                 info,
3343                                 short_channel_id: 42,
3344                         };
3345
3346                         assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 2050);
3347
3348                         let usage = ChannelUsage {
3349                                 amount_msat: 1,
3350                                 inflight_htlc_msat: 0,
3351                                 effective_capacity: EffectiveCapacity::AdvertisedMaxHTLC { amount_msat: 0 },
3352                         };
3353                         assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 2048);
3354                 }
3355
3356                 // Advance to decay all liquidity offsets to zero.
3357                 scorer.decay_liquidity_certainty(Duration::from_secs(10 * (16 + 60 * 60)));
3358
3359                 // Once even the bounds have decayed information about the channel should be removed
3360                 // entirely.
3361                 assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
3362                         None);
3363
3364                 // Use a path in the opposite direction, which have zero for htlc_maximum_msat. This will
3365                 // ensure that the effective capacity is zero to test division-by-zero edge cases.
3366                 let path = vec![
3367                         path_hop(target_pubkey(), 43, 2),
3368                         path_hop(source_pubkey(), 42, 1),
3369                         path_hop(sender_pubkey(), 41, 0),
3370                 ];
3371                 scorer.payment_path_failed(&Path { hops: path, blinded_tail: None }, 42, Duration::from_secs(10 * (16 + 60 * 60)));
3372         }
3373
3374         #[test]
3375         fn adds_anti_probing_penalty() {
3376                 let logger = TestLogger::new();
3377                 let network_graph = network_graph(&logger);
3378                 let source = source_node_id();
3379                 let params = ProbabilisticScoringFeeParameters {
3380                         anti_probing_penalty_msat: 500,
3381                         ..ProbabilisticScoringFeeParameters::zero_penalty()
3382                 };
3383                 let scorer = ProbabilisticScorer::new(ProbabilisticScoringDecayParameters::default(), &network_graph, &logger);
3384
3385                 // Check we receive no penalty for a low htlc_maximum_msat.
3386                 let usage = ChannelUsage {
3387                         amount_msat: 512_000,
3388                         inflight_htlc_msat: 0,
3389                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: 1_000 },
3390                 };
3391                 let network_graph = network_graph.read_only();
3392                 let channel = network_graph.channel(42).unwrap();
3393                 let (info, _) = channel.as_directed_from(&source).unwrap();
3394                 let candidate = CandidateRouteHop::PublicHop {
3395                         info,
3396                         short_channel_id: 42,
3397                 };
3398                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
3399
3400                 // Check we receive anti-probing penalty for htlc_maximum_msat == channel_capacity.
3401                 let usage = ChannelUsage {
3402                         amount_msat: 512_000,
3403                         inflight_htlc_msat: 0,
3404                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: 1_024_000 },
3405                 };
3406                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 500);
3407
3408                 // Check we receive anti-probing penalty for htlc_maximum_msat == channel_capacity/2.
3409                 let usage = ChannelUsage {
3410                         amount_msat: 512_000,
3411                         inflight_htlc_msat: 0,
3412                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: 512_000 },
3413                 };
3414                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 500);
3415
3416                 // Check we receive no anti-probing penalty for htlc_maximum_msat == channel_capacity/2 - 1.
3417                 let usage = ChannelUsage {
3418                         amount_msat: 512_000,
3419                         inflight_htlc_msat: 0,
3420                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: 511_999 },
3421                 };
3422                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 0);
3423         }
3424
3425         #[test]
3426         fn scores_with_blinded_path() {
3427                 // Make sure we'll account for a blinded path's final_value_msat in scoring
3428                 let logger = TestLogger::new();
3429                 let network_graph = network_graph(&logger);
3430                 let params = ProbabilisticScoringFeeParameters {
3431                         liquidity_penalty_multiplier_msat: 1_000,
3432                         ..ProbabilisticScoringFeeParameters::zero_penalty()
3433                 };
3434                 let decay_params = ProbabilisticScoringDecayParameters::default();
3435                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
3436                 let source = source_node_id();
3437                 let usage = ChannelUsage {
3438                         amount_msat: 512,
3439                         inflight_htlc_msat: 0,
3440                         effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_000 },
3441                 };
3442                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
3443                 let (info, target) = channel.as_directed_from(&source).unwrap();
3444                 let candidate = CandidateRouteHop::PublicHop {
3445                         info,
3446                         short_channel_id: 42,
3447                 };
3448                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 300);
3449
3450                 let mut path = payment_path_for_amount(768);
3451                 let recipient_hop = path.hops.pop().unwrap();
3452                 let blinded_path = BlindedPath {
3453                         introduction_node_id: path.hops.last().as_ref().unwrap().pubkey,
3454                         blinding_point: test_utils::pubkey(42),
3455                         blinded_hops: vec![
3456                                 BlindedHop { blinded_node_id: test_utils::pubkey(44), encrypted_payload: Vec::new() }
3457                         ],
3458                 };
3459                 path.blinded_tail = Some(BlindedTail {
3460                         hops: blinded_path.blinded_hops,
3461                         blinding_point: blinded_path.blinding_point,
3462                         excess_final_cltv_expiry_delta: recipient_hop.cltv_expiry_delta,
3463                         final_value_msat: recipient_hop.fee_msat,
3464                 });
3465
3466                 // Check the liquidity before and after scoring payment failures to ensure the blinded path's
3467                 // final value is taken into account.
3468                 assert!(scorer.channel_liquidities.get(&42).is_none());
3469
3470                 scorer.payment_path_failed(&path, 42, Duration::ZERO);
3471                 path.blinded_tail.as_mut().unwrap().final_value_msat = 256;
3472                 scorer.payment_path_failed(&path, 43, Duration::ZERO);
3473
3474                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
3475                         .as_directed(&source, &target, 1_000);
3476                 assert_eq!(liquidity.min_liquidity_msat(), 256);
3477                 assert_eq!(liquidity.max_liquidity_msat(), 768);
3478         }
3479
3480         #[test]
3481         fn realistic_historical_failures() {
3482                 // The motivation for the unequal sized buckets came largely from attempting to pay 10k
3483                 // sats over a one bitcoin channel. This tests that case explicitly, ensuring that we score
3484                 // properly.
3485                 let logger = TestLogger::new();
3486                 let mut network_graph = network_graph(&logger);
3487                 let params = ProbabilisticScoringFeeParameters {
3488                         historical_liquidity_penalty_multiplier_msat: 1024,
3489                         historical_liquidity_penalty_amount_multiplier_msat: 1024,
3490                         ..ProbabilisticScoringFeeParameters::zero_penalty()
3491                 };
3492                 let decay_params = ProbabilisticScoringDecayParameters {
3493                         liquidity_offset_half_life: Duration::from_secs(60 * 60),
3494                         historical_no_updates_half_life: Duration::from_secs(10),
3495                         ..ProbabilisticScoringDecayParameters::default()
3496                 };
3497
3498                 let capacity_msat = 100_000_000_000;
3499                 update_channel(&mut network_graph, 42, source_privkey(), 0, capacity_msat, 200);
3500                 update_channel(&mut network_graph, 42, target_privkey(), 1, capacity_msat, 200);
3501
3502                 let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
3503                 let source = source_node_id();
3504
3505                 let mut amount_msat = 10_000_000;
3506                 let usage = ChannelUsage {
3507                         amount_msat,
3508                         inflight_htlc_msat: 0,
3509                         effective_capacity: EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: capacity_msat },
3510                 };
3511                 let channel = network_graph.read_only().channel(42).unwrap().to_owned();
3512                 let (info, target) = channel.as_directed_from(&source).unwrap();
3513                 let candidate = CandidateRouteHop::PublicHop {
3514                         info,
3515                         short_channel_id: 42,
3516                 };
3517                 // With no historical data the normal liquidity penalty calculation is used, which results
3518                 // in a success probability of ~75%.
3519                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params), 1269);
3520                 assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
3521                         None);
3522                 assert_eq!(scorer.historical_estimated_payment_success_probability(42, &target, 42, &params),
3523                         None);
3524
3525                 // Fail to pay once, and then check the buckets and penalty.
3526                 scorer.payment_path_failed(&payment_path_for_amount(amount_msat), 42, Duration::ZERO);
3527                 // The penalty should be the maximum penalty, as the payment we're scoring is now in the
3528                 // same bucket which is the only maximum datapoint.
3529                 assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &params),
3530                         2048 + 2048 * amount_msat / super::AMOUNT_PENALTY_DIVISOR);
3531                 // The "it failed" increment is 32, which we should apply to the first upper-bound (between
3532                 // 6k sats and 12k sats).
3533                 assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
3534                         Some(([32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
3535                                 [0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])));
3536                 // The success probability estimate itself should be zero.
3537                 assert_eq!(scorer.historical_estimated_payment_success_probability(42, &target, amount_msat, &params),
3538                         Some(0.0));
3539
3540                 // Now test again with the amount in the bottom bucket.
3541                 amount_msat /= 2;
3542                 // The new amount is entirely within the only minimum bucket with score, so the probability
3543                 // we assign is 1/2.
3544                 assert_eq!(scorer.historical_estimated_payment_success_probability(42, &target, amount_msat, &params),
3545                         Some(0.5));
3546
3547                 // ...but once we see a failure, we consider the payment to be substantially less likely,
3548                 // even though not a probability of zero as we still look at the second max bucket which
3549                 // now shows 31.
3550                 scorer.payment_path_failed(&payment_path_for_amount(amount_msat), 42, Duration::ZERO);
3551                 assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
3552                         Some(([63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
3553                                 [32, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])));
3554                 assert_eq!(scorer.historical_estimated_payment_success_probability(42, &target, amount_msat, &params),
3555                         Some(0.0));
3556         }
3557 }
3558
3559 #[cfg(ldk_bench)]
3560 pub mod benches {
3561         use super::*;
3562         use criterion::Criterion;
3563         use crate::routing::router::{bench_utils, RouteHop};
3564         use crate::util::test_utils::TestLogger;
3565         use crate::ln::features::{ChannelFeatures, NodeFeatures};
3566
3567         pub fn decay_100k_channel_bounds(bench: &mut Criterion) {
3568                 let logger = TestLogger::new();
3569                 let (network_graph, mut scorer) = bench_utils::read_graph_scorer(&logger).unwrap();
3570                 let mut cur_time = Duration::ZERO;
3571                         cur_time += Duration::from_millis(1);
3572                         scorer.decay_liquidity_certainty(cur_time);
3573                 bench.bench_function("decay_100k_channel_bounds", |b| b.iter(|| {
3574                         cur_time += Duration::from_millis(1);
3575                         scorer.decay_liquidity_certainty(cur_time);
3576                 }));
3577         }
3578 }