XXX: Stuff we should revert but
[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 [`Score`] implementation is not needed.
14 //!
15 //! # Example
16 //!
17 //! ```
18 //! # extern crate secp256k1;
19 //! #
20 //! # use lightning::routing::network_graph::NetworkGraph;
21 //! # use lightning::routing::router::{RouteParameters, find_route};
22 //! # use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters, Scorer, ScoringParameters};
23 //! # use lightning::util::logger::{Logger, Record};
24 //! # use secp256k1::key::PublicKey;
25 //! #
26 //! # struct FakeLogger {};
27 //! # impl Logger for FakeLogger {
28 //! #     fn log(&self, record: &Record) { unimplemented!() }
29 //! # }
30 //! # fn find_scored_route(payer: PublicKey, route_params: RouteParameters, network_graph: NetworkGraph) {
31 //! # let logger = FakeLogger {};
32 //! #
33 //! // Use the default channel penalties.
34 //! let params = ProbabilisticScoringParameters::default();
35 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
36 //!
37 //! // Or use custom channel penalties.
38 //! let params = ProbabilisticScoringParameters {
39 //!     liquidity_penalty_multiplier_msat: 2 * 1000,
40 //!     ..ProbabilisticScoringParameters::default()
41 //! };
42 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
43 //!
44 //! let route = find_route(&payer, &route_params, &network_graph, None, &logger, &scorer);
45 //! # }
46 //! ```
47 //!
48 //! # Note
49 //!
50 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
51 //! different types and thus is undefined.
52 //!
53 //! [`find_route`]: crate::routing::router::find_route
54
55 use ln::msgs::DecodeError;
56 use routing::network_graph::{NetworkGraph, NodeId};
57 use routing::router::RouteHop;
58 use util::ser::{Readable, ReadableArgs, Writeable, Writer};
59
60 use prelude::*;
61 use core::cell::{RefCell, RefMut};
62 use core::ops::{Deref, DerefMut};
63 use core::time::Duration;
64 use io::{self, Read};
65 use sync::{Mutex, MutexGuard};
66
67 /// We define Score ever-so-slightly differently based on whether we are being built for C bindings
68 /// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
69 /// no problem - you move a `Score` that implements `Writeable` into a `Mutex`, lock it, and now
70 /// you have the original, concrete, `Score` type, which presumably implements `Writeable`.
71 ///
72 /// For C users, once you've moved the `Score` into a `LockableScore` all you have after locking it
73 /// is an opaque trait object with an opaque pointer with no type info. Users could take the unsafe
74 /// approach of blindly casting that opaque pointer to a concrete type and calling `Writeable` from
75 /// there, but other languages downstream of the C bindings (e.g. Java) can't even do that.
76 /// Instead, we really want `Score` and `LockableScore` to implement `Writeable` directly, which we
77 /// do here by defining `Score` differently for `cfg(c_bindings)`.
78 macro_rules! define_score { ($($supertrait: path)*) => {
79 /// An interface used to score payment channels for path finding.
80 ///
81 ///     Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
82 pub trait Score $(: $supertrait)* {
83         /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
84         /// given channel in the direction from `source` to `target`.
85         ///
86         /// The channel's capacity (less any other MPP parts that are also being considered for use in
87         /// the same payment) is given by `capacity_msat`. It may be determined from various sources
88         /// such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
89         /// [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
90         /// Thus, implementations should be overflow-safe.
91         fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &NodeId, target: &NodeId) -> u64;
92
93         /// Handles updating channel penalties after failing to route through a channel.
94         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64);
95
96         /// Handles updating channel penalties after successfully routing along a path.
97         fn payment_path_successful(&mut self, path: &[&RouteHop]);
98 }
99
100 impl<S: Score, T: DerefMut<Target=S> $(+ $supertrait)*> Score for T {
101         fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &NodeId, target: &NodeId) -> u64 {
102                 self.deref().channel_penalty_msat(short_channel_id, send_amt_msat, capacity_msat, source, target)
103         }
104
105         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
106                 self.deref_mut().payment_path_failed(path, short_channel_id)
107         }
108
109         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
110                 self.deref_mut().payment_path_successful(path)
111         }
112 }
113 } }
114
115 #[cfg(c_bindings)]
116 define_score!(Writeable);
117 #[cfg(not(c_bindings))]
118 define_score!();
119
120 /// A scorer that is accessed under a lock.
121 ///
122 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
123 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
124 /// implementations. Internal locking would be detrimental to route finding performance and could
125 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
126 ///
127 /// [`find_route`]: crate::routing::router::find_route
128 pub trait LockableScore<'a> {
129         /// The locked [`Score`] type.
130         type Locked: 'a + Score;
131
132         /// Returns the locked scorer.
133         fn lock(&'a self) -> Self::Locked;
134 }
135
136 /// (C-not exported)
137 impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
138         type Locked = MutexGuard<'a, T>;
139
140         fn lock(&'a self) -> MutexGuard<'a, T> {
141                 Mutex::lock(self).unwrap()
142         }
143 }
144
145 impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
146         type Locked = RefMut<'a, T>;
147
148         fn lock(&'a self) -> RefMut<'a, T> {
149                 self.borrow_mut()
150         }
151 }
152
153 #[cfg(c_bindings)]
154 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
155 pub struct MultiThreadedLockableScore<S: Score> {
156         score: Mutex<S>,
157 }
158 #[cfg(c_bindings)]
159 /// (C-not exported)
160 impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore<T> {
161         type Locked = MutexGuard<'a, T>;
162
163         fn lock(&'a self) -> MutexGuard<'a, T> {
164                 Mutex::lock(&self.score).unwrap()
165         }
166 }
167
168 #[cfg(c_bindings)]
169 impl<T: Score> MultiThreadedLockableScore<T> {
170         /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
171         pub fn new(score: T) -> Self {
172                 MultiThreadedLockableScore { score: Mutex::new(score) }
173         }
174 }
175
176 #[cfg(c_bindings)]
177 /// (C-not exported)
178 impl<'a, T: Writeable> Writeable for RefMut<'a, T> {
179         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
180                 T::write(&**self, writer)
181         }
182 }
183
184 #[cfg(c_bindings)]
185 /// (C-not exported)
186 impl<'a, S: Writeable> Writeable for MutexGuard<'a, S> {
187         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
188                 S::write(&**self, writer)
189         }
190 }
191
192 /// [`Score`] implementation that uses a fixed penalty.
193 pub struct FixedPenaltyScorer {
194         penalty_msat: u64,
195 }
196
197 impl_writeable_tlv_based!(FixedPenaltyScorer, {
198         (0, penalty_msat, required),
199 });
200
201 impl FixedPenaltyScorer {
202         /// Creates a new scorer using `penalty_msat`.
203         pub fn with_penalty(penalty_msat: u64) -> Self {
204                 Self { penalty_msat }
205         }
206 }
207
208 impl Score for FixedPenaltyScorer {
209         fn channel_penalty_msat(&self, _: u64, _: u64, _: u64, _: &NodeId, _: &NodeId) -> u64 {
210                 self.penalty_msat
211         }
212
213         fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
214
215         fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
216 }
217
218 /// [`Score`] implementation that provides reasonable default behavior.
219 ///
220 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
221 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
222 ///
223 /// See [module-level documentation] for usage and [`ScoringParameters`] for customization.
224 ///
225 /// # Note
226 ///
227 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
228 /// behavior.
229 ///
230 /// [module-level documentation]: crate::routing::scoring
231 #[deprecated(
232         since = "0.0.105",
233         note = "ProbabilisticScorer should be used instead of Scorer.",
234 )]
235 #[cfg(not(feature = "no-std"))]
236 pub type Scorer = ScorerUsingTime::<std::time::Instant>;
237 #[cfg(feature = "no-std")]
238 pub type Scorer = ScorerUsingTime::<time::Eternity>;
239
240 // Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
241 // doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
242 // methods at all.
243
244 /// [`Score`] implementation.
245 ///
246 /// (C-not exported) generally all users should use the [`Scorer`] type alias.
247 #[doc(hidden)]
248 pub struct ScorerUsingTime<T: Time> {
249         params: ScoringParameters,
250         // TODO: Remove entries of closed channels.
251         channel_failures: HashMap<u64, ChannelFailure<T>>,
252 }
253
254 /// Parameters for configuring [`Scorer`].
255 pub struct ScoringParameters {
256         /// A fixed penalty in msats to apply to each channel.
257         ///
258         /// Default value: 500 msat
259         pub base_penalty_msat: u64,
260
261         /// A penalty in msats to apply to a channel upon failing to relay a payment.
262         ///
263         /// This accumulates for each failure but may be reduced over time based on
264         /// [`failure_penalty_half_life`] or when successfully routing through a channel.
265         ///
266         /// Default value: 1,024,000 msat
267         ///
268         /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
269         pub failure_penalty_msat: u64,
270
271         /// When the amount being sent over a channel is this many 1024ths of the total channel
272         /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
273         ///
274         /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
275         ///
276         /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
277         pub overuse_penalty_start_1024th: u16,
278
279         /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
280         /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
281         ///
282         /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
283         ///                to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
284         ///
285         /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
286         pub overuse_penalty_msat_per_1024th: u64,
287
288         /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
289         /// cut in half.
290         ///
291         /// Successfully routing through a channel will immediately cut the penalty in half as well.
292         ///
293         /// Default value: 1 hour
294         ///
295         /// # Note
296         ///
297         /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
298         /// never decay.
299         ///
300         /// [`failure_penalty_msat`]: Self::failure_penalty_msat
301         pub failure_penalty_half_life: Duration,
302 }
303
304 impl_writeable_tlv_based!(ScoringParameters, {
305         (0, base_penalty_msat, required),
306         (1, overuse_penalty_start_1024th, (default_value, 128)),
307         (2, failure_penalty_msat, required),
308         (3, overuse_penalty_msat_per_1024th, (default_value, 20)),
309         (4, failure_penalty_half_life, required),
310 });
311
312 /// Accounting for penalties against a channel for failing to relay any payments.
313 ///
314 /// Penalties decay over time, though accumulate as more failures occur.
315 struct ChannelFailure<T: Time> {
316         /// Accumulated penalty in msats for the channel as of `last_updated`.
317         undecayed_penalty_msat: u64,
318
319         /// Last time the channel either failed to route or successfully routed a payment. Used to decay
320         /// `undecayed_penalty_msat`.
321         last_updated: T,
322 }
323
324 impl<T: Time> ScorerUsingTime<T> {
325         /// Creates a new scorer using the given scoring parameters.
326         pub fn new(params: ScoringParameters) -> Self {
327                 Self {
328                         params,
329                         channel_failures: HashMap::new(),
330                 }
331         }
332 }
333
334 impl<T: Time> ChannelFailure<T> {
335         fn new(failure_penalty_msat: u64) -> Self {
336                 Self {
337                         undecayed_penalty_msat: failure_penalty_msat,
338                         last_updated: T::now(),
339                 }
340         }
341
342         fn add_penalty(&mut self, failure_penalty_msat: u64, half_life: Duration) {
343                 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) + failure_penalty_msat;
344                 self.last_updated = T::now();
345         }
346
347         fn reduce_penalty(&mut self, half_life: Duration) {
348                 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) >> 1;
349                 self.last_updated = T::now();
350         }
351
352         fn decayed_penalty_msat(&self, half_life: Duration) -> u64 {
353                 self.last_updated.elapsed().as_secs()
354                         .checked_div(half_life.as_secs())
355                         .and_then(|decays| self.undecayed_penalty_msat.checked_shr(decays as u32))
356                         .unwrap_or(0)
357         }
358 }
359
360 impl<T: Time> Default for ScorerUsingTime<T> {
361         fn default() -> Self {
362                 Self::new(ScoringParameters::default())
363         }
364 }
365
366 impl Default for ScoringParameters {
367         fn default() -> Self {
368                 Self {
369                         base_penalty_msat: 500,
370                         failure_penalty_msat: 1024 * 1000,
371                         failure_penalty_half_life: Duration::from_secs(3600),
372                         overuse_penalty_start_1024th: 1024 / 8,
373                         overuse_penalty_msat_per_1024th: 20,
374                 }
375         }
376 }
377
378 impl<T: Time> Score for ScorerUsingTime<T> {
379         fn channel_penalty_msat(
380                 &self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, _source: &NodeId, _target: &NodeId
381         ) -> u64 {
382                 let failure_penalty_msat = self.channel_failures
383                         .get(&short_channel_id)
384                         .map_or(0, |value| value.decayed_penalty_msat(self.params.failure_penalty_half_life));
385
386                 let mut penalty_msat = self.params.base_penalty_msat + failure_penalty_msat;
387                 let send_1024ths = send_amt_msat.checked_mul(1024).unwrap_or(u64::max_value()) / capacity_msat;
388                 if send_1024ths > self.params.overuse_penalty_start_1024th as u64 {
389                         penalty_msat = penalty_msat.checked_add(
390                                         (send_1024ths - self.params.overuse_penalty_start_1024th as u64)
391                                         .checked_mul(self.params.overuse_penalty_msat_per_1024th).unwrap_or(u64::max_value()))
392                                 .unwrap_or(u64::max_value());
393                 }
394
395                 penalty_msat
396         }
397
398         fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
399                 let failure_penalty_msat = self.params.failure_penalty_msat;
400                 let half_life = self.params.failure_penalty_half_life;
401                 self.channel_failures
402                         .entry(short_channel_id)
403                         .and_modify(|failure| failure.add_penalty(failure_penalty_msat, half_life))
404                         .or_insert_with(|| ChannelFailure::new(failure_penalty_msat));
405         }
406
407         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
408                 let half_life = self.params.failure_penalty_half_life;
409                 for hop in path.iter() {
410                         self.channel_failures
411                                 .entry(hop.short_channel_id)
412                                 .and_modify(|failure| failure.reduce_penalty(half_life));
413                 }
414         }
415 }
416
417 impl<T: Time> Writeable for ScorerUsingTime<T> {
418         #[inline]
419         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
420                 self.params.write(w)?;
421                 self.channel_failures.write(w)?;
422                 write_tlv_fields!(w, {});
423                 Ok(())
424         }
425 }
426
427 impl<T: Time> Readable for ScorerUsingTime<T> {
428         #[inline]
429         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
430                 let res = Ok(Self {
431                         params: Readable::read(r)?,
432                         channel_failures: Readable::read(r)?,
433                 });
434                 read_tlv_fields!(r, {});
435                 res
436         }
437 }
438
439 impl<T: Time> Writeable for ChannelFailure<T> {
440         #[inline]
441         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
442                 let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
443                 write_tlv_fields!(w, {
444                         (0, self.undecayed_penalty_msat, required),
445                         (2, duration_since_epoch, required),
446                 });
447                 Ok(())
448         }
449 }
450
451 impl<T: Time> Readable for ChannelFailure<T> {
452         #[inline]
453         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
454                 let mut undecayed_penalty_msat = 0;
455                 let mut duration_since_epoch = Duration::from_secs(0);
456                 read_tlv_fields!(r, {
457                         (0, undecayed_penalty_msat, required),
458                         (2, duration_since_epoch, required),
459                 });
460                 Ok(Self {
461                         undecayed_penalty_msat,
462                         last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
463                 })
464         }
465 }
466
467 /// [`Score`] implementation using channel success probability distributions.
468 ///
469 /// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
470 /// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
471 /// distributions are defined based on knowledge learned from successful and unsuccessful attempts.
472 /// Then the negative `log10` of the success probability is used to determine the cost of routing a
473 /// specific HTLC amount through a channel.
474 ///
475 /// Knowledge about channel liquidity balances takes the form of upper and lower bounds on the
476 /// possible liquidity. Certainty of the bounds is decreased over time using a decay function. See
477 /// [`ProbabilisticScoringParameters`] for details.
478 ///
479 /// Since the scorer aims to learn the current channel liquidity balances, it works best for nodes
480 /// with high payment volume or that actively probe the [`NetworkGraph`]. Nodes with low payment
481 /// volume are more likely to experience failed payment paths, which would need to be retried.
482 ///
483 /// # Note
484 ///
485 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
486 /// behavior.
487 ///
488 /// [1]: https://arxiv.org/abs/2107.05322
489 #[cfg(not(feature = "no-std"))]
490 pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, std::time::Instant>;
491 #[cfg(feature = "no-std")]
492 pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, time::Eternity>;
493
494 /// Probabilistic [`Score`] implementation.
495 ///
496 /// (C-not exported) generally all users should use the [`ProbabilisticScorer`] type alias.
497 #[doc(hidden)]
498 pub struct ProbabilisticScorerUsingTime<G: Deref<Target = NetworkGraph>, T: Time> {
499         params: ProbabilisticScoringParameters,
500         network_graph: G,
501         // TODO: Remove entries of closed channels.
502         channel_liquidities: HashMap<u64, ChannelLiquidity<T>>,
503 }
504
505 /// Parameters for configuring [`ProbabilisticScorer`].
506 #[derive(Clone, Copy)]
507 pub struct ProbabilisticScoringParameters {
508         /// A multiplier used to determine the amount in msats willing to be paid to avoid routing
509         /// through a channel, as per multiplying by the negative `log10` of the channel's success
510         /// probability for a payment.
511         ///
512         /// The success probability is determined by the effective channel capacity, the payment amount,
513         /// and knowledge learned from prior successful and unsuccessful payments. The lower bound of
514         /// the success probability is 0.01, effectively limiting the penalty to the range
515         /// `0..=2*liquidity_penalty_multiplier_msat`. The knowledge learned is decayed over time based
516         /// on [`liquidity_offset_half_life`].
517         ///
518         /// Default value: 10,000 msat
519         ///
520         /// [`liquidity_offset_half_life`]: Self::liquidity_offset_half_life
521         pub liquidity_penalty_multiplier_msat: u64,
522
523         /// The time required to elapse before any knowledge learned about channel liquidity balances is
524         /// cut in half.
525         ///
526         /// The bounds are defined in terms of offsets and are initially zero. Increasing the offsets
527         /// gives tighter bounds on the channel liquidity balance. Thus, halving the offsets decreases
528         /// the certainty of the channel liquidity balance.
529         ///
530         /// Default value: 1 hour
531         ///
532         /// # Note
533         ///
534         /// When built with the `no-std` feature, time will never elapse. Therefore, the channel
535         /// liquidity knowledge will never decay except when the bounds cross.
536         pub liquidity_offset_half_life: Duration,
537 }
538
539 impl_writeable_tlv_based!(ProbabilisticScoringParameters, {
540         (0, liquidity_penalty_multiplier_msat, required),
541         (2, liquidity_offset_half_life, required),
542 });
543
544 /// Accounting for channel liquidity balance uncertainty.
545 ///
546 /// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
547 /// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
548 /// offset fields gives the opposite direction.
549 struct ChannelLiquidity<T: Time> {
550         /// Lower channel liquidity bound in terms of an offset from zero.
551         min_liquidity_offset_msat: u64,
552
553         /// Upper channel liquidity bound in terms of an offset from the effective capacity.
554         max_liquidity_offset_msat: u64,
555
556         /// Time when the liquidity bounds were last modified.
557         last_updated: T,
558 }
559
560 /// A snapshot of [`ChannelLiquidity`] in one direction assuming a certain channel capacity and
561 /// decayed with a given half life.
562 struct DirectedChannelLiquidity<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> {
563         min_liquidity_offset_msat: L,
564         max_liquidity_offset_msat: L,
565         capacity_msat: u64,
566         last_updated: U,
567         now: T,
568         half_life: Duration,
569 }
570
571 impl<G: Deref<Target = NetworkGraph>, T: Time> ProbabilisticScorerUsingTime<G, T> {
572         /// Creates a new scorer using the given scoring parameters for sending payments from a node
573         /// through a network graph.
574         pub fn new(params: ProbabilisticScoringParameters, network_graph: G) -> Self {
575                 Self {
576                         params,
577                         network_graph,
578                         channel_liquidities: HashMap::new(),
579                 }
580         }
581
582         #[cfg(test)]
583         fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity<T>) -> Self {
584                 assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
585                 self
586         }
587 }
588
589 impl Default for ProbabilisticScoringParameters {
590         fn default() -> Self {
591                 Self {
592                         liquidity_penalty_multiplier_msat: 10_000,
593                         liquidity_offset_half_life: Duration::from_secs(3600),
594                 }
595         }
596 }
597
598 impl<T: Time> ChannelLiquidity<T> {
599         #[inline]
600         fn new() -> Self {
601                 Self {
602                         min_liquidity_offset_msat: 0,
603                         max_liquidity_offset_msat: 0,
604                         last_updated: T::now(),
605                 }
606         }
607
608         /// Returns a view of the channel liquidity directed from `source` to `target` assuming
609         /// `capacity_msat`.
610         fn as_directed(
611                 &self, source: &NodeId, target: &NodeId, capacity_msat: u64, half_life: Duration
612         ) -> DirectedChannelLiquidity<&u64, T, &T> {
613                 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
614                         (&self.min_liquidity_offset_msat, &self.max_liquidity_offset_msat)
615                 } else {
616                         (&self.max_liquidity_offset_msat, &self.min_liquidity_offset_msat)
617                 };
618
619                 DirectedChannelLiquidity {
620                         min_liquidity_offset_msat,
621                         max_liquidity_offset_msat,
622                         capacity_msat,
623                         last_updated: &self.last_updated,
624                         now: T::now(),
625                         half_life,
626                 }
627         }
628
629         /// Returns a mutable view of the channel liquidity directed from `source` to `target` assuming
630         /// `capacity_msat`.
631         fn as_directed_mut(
632                 &mut self, source: &NodeId, target: &NodeId, capacity_msat: u64, half_life: Duration
633         ) -> DirectedChannelLiquidity<&mut u64, T, &mut T> {
634                 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
635                         (&mut self.min_liquidity_offset_msat, &mut self.max_liquidity_offset_msat)
636                 } else {
637                         (&mut self.max_liquidity_offset_msat, &mut self.min_liquidity_offset_msat)
638                 };
639
640                 DirectedChannelLiquidity {
641                         min_liquidity_offset_msat,
642                         max_liquidity_offset_msat,
643                         capacity_msat,
644                         last_updated: &mut self.last_updated,
645                         now: T::now(),
646                         half_life,
647                 }
648         }
649 }
650
651 impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiquidity<L, T, U> {
652         /// Returns the success probability of routing the given HTLC `amount_msat` through the channel
653         /// in this direction.
654         fn success_probability(&self, amount_msat: u64) -> f64 {
655                 let max_liquidity_msat = self.max_liquidity_msat();
656                 let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
657                 if amount_msat > max_liquidity_msat {
658                         0.0
659                 } else if amount_msat <= min_liquidity_msat {
660                         1.0
661                 } else {
662                         let numerator = max_liquidity_msat + 1 - amount_msat;
663                         let denominator = max_liquidity_msat + 1 - min_liquidity_msat;
664                         numerator as f64 / denominator as f64
665                 }.max(0.01) // Lower bound the success probability to ensure some channel is selected.
666         }
667
668         /// Returns the lower bound of the channel liquidity balance in this direction.
669         fn min_liquidity_msat(&self) -> u64 {
670                 self.decayed_offset_msat(*self.min_liquidity_offset_msat)
671         }
672
673         /// Returns the upper bound of the channel liquidity balance in this direction.
674         fn max_liquidity_msat(&self) -> u64 {
675                 self.capacity_msat
676                         .checked_sub(self.decayed_offset_msat(*self.max_liquidity_offset_msat))
677                         .unwrap_or(0)
678         }
679
680         fn decayed_offset_msat(&self, offset_msat: u64) -> u64 {
681                 self.now.duration_since(*self.last_updated).as_secs()
682                         .checked_div(self.half_life.as_secs())
683                         .and_then(|decays| offset_msat.checked_shr(decays as u32))
684                         .unwrap_or(0)
685         }
686 }
687
688 impl<L: DerefMut<Target = u64>, T: Time, U: DerefMut<Target = T>> DirectedChannelLiquidity<L, T, U> {
689         /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
690         fn failed_at_channel(&mut self, amount_msat: u64) {
691                 if amount_msat < self.max_liquidity_msat() {
692                         self.set_max_liquidity_msat(amount_msat);
693                 }
694         }
695
696         /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
697         fn failed_downstream(&mut self, amount_msat: u64) {
698                 if amount_msat > self.min_liquidity_msat() {
699                         self.set_min_liquidity_msat(amount_msat);
700                 }
701         }
702
703         /// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
704         fn successful(&mut self, amount_msat: u64) {
705                 let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
706                 self.set_max_liquidity_msat(max_liquidity_msat);
707         }
708
709         /// Adjusts the lower bound of the channel liquidity balance in this direction.
710         fn set_min_liquidity_msat(&mut self, amount_msat: u64) {
711                 *self.min_liquidity_offset_msat = amount_msat;
712                 *self.max_liquidity_offset_msat = if amount_msat > self.max_liquidity_msat() {
713                         0
714                 } else {
715                         self.decayed_offset_msat(*self.max_liquidity_offset_msat)
716                 };
717                 *self.last_updated = self.now;
718         }
719
720         /// Adjusts the upper bound of the channel liquidity balance in this direction.
721         fn set_max_liquidity_msat(&mut self, amount_msat: u64) {
722                 *self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
723                 *self.min_liquidity_offset_msat = if amount_msat < self.min_liquidity_msat() {
724                         0
725                 } else {
726                         self.decayed_offset_msat(*self.min_liquidity_offset_msat)
727                 };
728                 *self.last_updated = self.now;
729         }
730 }
731
732 impl<G: Deref<Target = NetworkGraph>, T: Time> Score for ProbabilisticScorerUsingTime<G, T> {
733         fn channel_penalty_msat(
734                 &self, short_channel_id: u64, amount_msat: u64, capacity_msat: u64, source: &NodeId,
735                 target: &NodeId
736         ) -> u64 {
737                 let liquidity_penalty_multiplier_msat = self.params.liquidity_penalty_multiplier_msat;
738                 let liquidity_offset_half_life = self.params.liquidity_offset_half_life;
739                 let success_probability = self.channel_liquidities
740                         .get(&short_channel_id)
741                         .unwrap_or(&ChannelLiquidity::new())
742                         .as_directed(source, target, capacity_msat, liquidity_offset_half_life)
743                         .success_probability(amount_msat);
744                 // NOTE: If success_probability is ever changed to return 0.0, log10 is undefined so return
745                 // u64::max_value instead.
746                 debug_assert!(success_probability > core::f64::EPSILON);
747                 (-(success_probability.log10()) * liquidity_penalty_multiplier_msat as f64) as u64
748         }
749
750         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
751                 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
752                 let liquidity_offset_half_life = self.params.liquidity_offset_half_life;
753                 let network_graph = self.network_graph.read_only();
754                 for hop in path {
755                         let target = NodeId::from_pubkey(&hop.pubkey);
756                         let channel_directed_from_source = network_graph.channels()
757                                 .get(&hop.short_channel_id)
758                                 .and_then(|channel| channel.as_directed_to(&target));
759
760                         // Only score announced channels.
761                         if let Some((channel, source)) = channel_directed_from_source {
762                                 let capacity_msat = channel.effective_capacity().as_msat();
763                                 if hop.short_channel_id == short_channel_id {
764                                         self.channel_liquidities
765                                                 .entry(hop.short_channel_id)
766                                                 .or_insert_with(ChannelLiquidity::new)
767                                                 .as_directed_mut(source, &target, capacity_msat, liquidity_offset_half_life)
768                                                 .failed_at_channel(amount_msat);
769                                         break;
770                                 }
771
772                                 self.channel_liquidities
773                                         .entry(hop.short_channel_id)
774                                         .or_insert_with(ChannelLiquidity::new)
775                                         .as_directed_mut(source, &target, capacity_msat, liquidity_offset_half_life)
776                                         .failed_downstream(amount_msat);
777                         }
778                 }
779         }
780
781         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
782                 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
783                 let liquidity_offset_half_life = self.params.liquidity_offset_half_life;
784                 let network_graph = self.network_graph.read_only();
785                 for hop in path {
786                         let target = NodeId::from_pubkey(&hop.pubkey);
787                         let channel_directed_from_source = network_graph.channels()
788                                 .get(&hop.short_channel_id)
789                                 .and_then(|channel| channel.as_directed_to(&target));
790
791                         // Only score announced channels.
792                         if let Some((channel, source)) = channel_directed_from_source {
793                                 let capacity_msat = channel.effective_capacity().as_msat();
794                                 self.channel_liquidities
795                                         .entry(hop.short_channel_id)
796                                         .or_insert_with(ChannelLiquidity::new)
797                                         .as_directed_mut(source, &target, capacity_msat, liquidity_offset_half_life)
798                                         .successful(amount_msat);
799                         }
800                 }
801         }
802 }
803
804 impl<G: Deref<Target = NetworkGraph>, T: Time> Writeable for ProbabilisticScorerUsingTime<G, T> {
805         #[inline]
806         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
807                 write_tlv_fields!(w, {
808                         (0, self.channel_liquidities, required)
809                 });
810                 Ok(())
811         }
812 }
813
814 impl<G, T> ReadableArgs<(ProbabilisticScoringParameters, G)> for ProbabilisticScorerUsingTime<G, T>
815 where
816         G: Deref<Target = NetworkGraph>,
817         T: Time,
818 {
819         #[inline]
820         fn read<R: Read>(
821                 r: &mut R, args: (ProbabilisticScoringParameters, G)
822         ) -> Result<Self, DecodeError> {
823                 let (params, network_graph) = args;
824                 let mut channel_liquidities = HashMap::new();
825                 read_tlv_fields!(r, {
826                         (0, channel_liquidities, required)
827                 });
828                 Ok(Self {
829                         params,
830                         network_graph,
831                         channel_liquidities,
832                 })
833         }
834 }
835
836 impl<T: Time> Writeable for ChannelLiquidity<T> {
837         #[inline]
838         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
839                 let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
840                 write_tlv_fields!(w, {
841                         (0, self.min_liquidity_offset_msat, required),
842                         (2, self.max_liquidity_offset_msat, required),
843                         (4, duration_since_epoch, required),
844                 });
845                 Ok(())
846         }
847 }
848
849 impl<T: Time> Readable for ChannelLiquidity<T> {
850         #[inline]
851         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
852                 let mut min_liquidity_offset_msat = 0;
853                 let mut max_liquidity_offset_msat = 0;
854                 let mut duration_since_epoch = Duration::from_secs(0);
855                 read_tlv_fields!(r, {
856                         (0, min_liquidity_offset_msat, required),
857                         (2, max_liquidity_offset_msat, required),
858                         (4, duration_since_epoch, required),
859                 });
860                 Ok(Self {
861                         min_liquidity_offset_msat,
862                         max_liquidity_offset_msat,
863                         last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
864                 })
865         }
866 }
867
868 pub(crate) mod time {
869         use core::ops::Sub;
870         use core::time::Duration;
871         /// A measurement of time.
872         pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
873                 /// Returns an instance corresponding to the current moment.
874                 fn now() -> Self;
875
876                 /// Returns the amount of time elapsed since `self` was created.
877                 fn elapsed(&self) -> Duration;
878
879                 /// Returns the amount of time passed between `earlier` and `self`.
880                 fn duration_since(&self, earlier: Self) -> Duration;
881
882                 /// Returns the amount of time passed since the beginning of [`Time`].
883                 ///
884                 /// Used during (de-)serialization.
885                 fn duration_since_epoch() -> Duration;
886         }
887
888         /// A state in which time has no meaning.
889         #[derive(Clone, Copy, Debug, PartialEq, Eq)]
890         pub struct Eternity;
891
892         #[cfg(not(feature = "no-std"))]
893         impl Time for std::time::Instant {
894                 fn now() -> Self {
895                         std::time::Instant::now()
896                 }
897
898                 fn duration_since(&self, earlier: Self) -> Duration {
899                         self.duration_since(earlier)
900                 }
901
902                 fn duration_since_epoch() -> Duration {
903                         use std::time::SystemTime;
904                         SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
905                 }
906
907                 fn elapsed(&self) -> Duration {
908                         std::time::Instant::elapsed(self)
909                 }
910         }
911
912         impl Time for Eternity {
913                 fn now() -> Self {
914                         Self
915                 }
916
917                 fn duration_since(&self, _earlier: Self) -> Duration {
918                         Duration::from_secs(0)
919                 }
920
921                 fn duration_since_epoch() -> Duration {
922                         Duration::from_secs(0)
923                 }
924
925                 fn elapsed(&self) -> Duration {
926                         Duration::from_secs(0)
927                 }
928         }
929
930         impl Sub<Duration> for Eternity {
931                 type Output = Self;
932
933                 fn sub(self, _other: Duration) -> Self {
934                         self
935                 }
936         }
937 }
938
939 pub(crate) use self::time::Time;
940
941 #[cfg(test)]
942 mod tests {
943         use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime, Time};
944         use super::time::Eternity;
945
946         use ln::features::{ChannelFeatures, NodeFeatures};
947         use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
948         use routing::scoring::Score;
949         use routing::network_graph::{NetworkGraph, NodeId};
950         use routing::router::RouteHop;
951         use util::ser::{Readable, ReadableArgs, Writeable};
952
953         use bitcoin::blockdata::constants::genesis_block;
954         use bitcoin::hashes::Hash;
955         use bitcoin::hashes::sha256d::Hash as Sha256dHash;
956         use bitcoin::network::constants::Network;
957         use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
958         use core::cell::Cell;
959         use core::ops::Sub;
960         use core::time::Duration;
961         use io;
962
963         // `Time` tests
964
965         /// Time that can be advanced manually in tests.
966         #[derive(Clone, Copy, Debug, PartialEq, Eq)]
967         struct SinceEpoch(Duration);
968
969         impl SinceEpoch {
970                 thread_local! {
971                         static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
972                 }
973
974                 fn advance(duration: Duration) {
975                         Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
976                 }
977         }
978
979         impl Time for SinceEpoch {
980                 fn now() -> Self {
981                         Self(Self::duration_since_epoch())
982                 }
983
984                 fn duration_since(&self, earlier: Self) -> Duration {
985                         self.0 - earlier.0
986                 }
987
988                 fn duration_since_epoch() -> Duration {
989                         Self::ELAPSED.with(|elapsed| elapsed.get())
990                 }
991
992                 fn elapsed(&self) -> Duration {
993                         Self::duration_since_epoch() - self.0
994                 }
995         }
996
997         impl Sub<Duration> for SinceEpoch {
998                 type Output = Self;
999
1000                 fn sub(self, other: Duration) -> Self {
1001                         Self(self.0 - other)
1002                 }
1003         }
1004
1005         #[test]
1006         fn time_passes_when_advanced() {
1007                 let now = SinceEpoch::now();
1008                 assert_eq!(now.elapsed(), Duration::from_secs(0));
1009
1010                 SinceEpoch::advance(Duration::from_secs(1));
1011                 SinceEpoch::advance(Duration::from_secs(1));
1012
1013                 let elapsed = now.elapsed();
1014                 let later = SinceEpoch::now();
1015
1016                 assert_eq!(elapsed, Duration::from_secs(2));
1017                 assert_eq!(later - elapsed, now);
1018         }
1019
1020         #[test]
1021         fn time_never_passes_in_an_eternity() {
1022                 let now = Eternity::now();
1023                 let elapsed = now.elapsed();
1024                 let later = Eternity::now();
1025
1026                 assert_eq!(now.elapsed(), Duration::from_secs(0));
1027                 assert_eq!(later - elapsed, now);
1028         }
1029
1030         // `Scorer` tests
1031
1032         /// A scorer for testing with time that can be manually advanced.
1033         type Scorer = ScorerUsingTime::<SinceEpoch>;
1034
1035         fn source_privkey() -> SecretKey {
1036                 SecretKey::from_slice(&[42; 32]).unwrap()
1037         }
1038
1039         fn target_privkey() -> SecretKey {
1040                 SecretKey::from_slice(&[43; 32]).unwrap()
1041         }
1042
1043         fn source_pubkey() -> PublicKey {
1044                 let secp_ctx = Secp256k1::new();
1045                 PublicKey::from_secret_key(&secp_ctx, &source_privkey())
1046         }
1047
1048         fn target_pubkey() -> PublicKey {
1049                 let secp_ctx = Secp256k1::new();
1050                 PublicKey::from_secret_key(&secp_ctx, &target_privkey())
1051         }
1052
1053         fn source_node_id() -> NodeId {
1054                 NodeId::from_pubkey(&source_pubkey())
1055         }
1056
1057         fn target_node_id() -> NodeId {
1058                 NodeId::from_pubkey(&target_pubkey())
1059         }
1060
1061         #[test]
1062         fn penalizes_without_channel_failures() {
1063                 let scorer = Scorer::new(ScoringParameters {
1064                         base_penalty_msat: 1_000,
1065                         failure_penalty_msat: 512,
1066                         failure_penalty_half_life: Duration::from_secs(1),
1067                         overuse_penalty_start_1024th: 1024,
1068                         overuse_penalty_msat_per_1024th: 0,
1069                 });
1070                 let source = source_node_id();
1071                 let target = target_node_id();
1072                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1073
1074                 SinceEpoch::advance(Duration::from_secs(1));
1075                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1076         }
1077
1078         #[test]
1079         fn accumulates_channel_failure_penalties() {
1080                 let mut scorer = Scorer::new(ScoringParameters {
1081                         base_penalty_msat: 1_000,
1082                         failure_penalty_msat: 64,
1083                         failure_penalty_half_life: Duration::from_secs(10),
1084                         overuse_penalty_start_1024th: 1024,
1085                         overuse_penalty_msat_per_1024th: 0,
1086                 });
1087                 let source = source_node_id();
1088                 let target = target_node_id();
1089                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1090
1091                 scorer.payment_path_failed(&[], 42);
1092                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1093
1094                 scorer.payment_path_failed(&[], 42);
1095                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1096
1097                 scorer.payment_path_failed(&[], 42);
1098                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_192);
1099         }
1100
1101         #[test]
1102         fn decays_channel_failure_penalties_over_time() {
1103                 let mut scorer = Scorer::new(ScoringParameters {
1104                         base_penalty_msat: 1_000,
1105                         failure_penalty_msat: 512,
1106                         failure_penalty_half_life: Duration::from_secs(10),
1107                         overuse_penalty_start_1024th: 1024,
1108                         overuse_penalty_msat_per_1024th: 0,
1109                 });
1110                 let source = source_node_id();
1111                 let target = target_node_id();
1112                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1113
1114                 scorer.payment_path_failed(&[], 42);
1115                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1116
1117                 SinceEpoch::advance(Duration::from_secs(9));
1118                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1119
1120                 SinceEpoch::advance(Duration::from_secs(1));
1121                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1122
1123                 SinceEpoch::advance(Duration::from_secs(10 * 8));
1124                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_001);
1125
1126                 SinceEpoch::advance(Duration::from_secs(10));
1127                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1128
1129                 SinceEpoch::advance(Duration::from_secs(10));
1130                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1131         }
1132
1133         #[test]
1134         fn decays_channel_failure_penalties_without_shift_overflow() {
1135                 let mut scorer = Scorer::new(ScoringParameters {
1136                         base_penalty_msat: 1_000,
1137                         failure_penalty_msat: 512,
1138                         failure_penalty_half_life: Duration::from_secs(10),
1139                         overuse_penalty_start_1024th: 1024,
1140                         overuse_penalty_msat_per_1024th: 0,
1141                 });
1142                 let source = source_node_id();
1143                 let target = target_node_id();
1144                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1145
1146                 scorer.payment_path_failed(&[], 42);
1147                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1148
1149                 // An unchecked right shift 64 bits or more in ChannelFailure::decayed_penalty_msat would
1150                 // cause an overflow.
1151                 SinceEpoch::advance(Duration::from_secs(10 * 64));
1152                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1153
1154                 SinceEpoch::advance(Duration::from_secs(10));
1155                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1156         }
1157
1158         #[test]
1159         fn accumulates_channel_failure_penalties_after_decay() {
1160                 let mut scorer = Scorer::new(ScoringParameters {
1161                         base_penalty_msat: 1_000,
1162                         failure_penalty_msat: 512,
1163                         failure_penalty_half_life: Duration::from_secs(10),
1164                         overuse_penalty_start_1024th: 1024,
1165                         overuse_penalty_msat_per_1024th: 0,
1166                 });
1167                 let source = source_node_id();
1168                 let target = target_node_id();
1169                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1170
1171                 scorer.payment_path_failed(&[], 42);
1172                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1173
1174                 SinceEpoch::advance(Duration::from_secs(10));
1175                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1176
1177                 scorer.payment_path_failed(&[], 42);
1178                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_768);
1179
1180                 SinceEpoch::advance(Duration::from_secs(10));
1181                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_384);
1182         }
1183
1184         #[test]
1185         fn reduces_channel_failure_penalties_after_success() {
1186                 let mut scorer = Scorer::new(ScoringParameters {
1187                         base_penalty_msat: 1_000,
1188                         failure_penalty_msat: 512,
1189                         failure_penalty_half_life: Duration::from_secs(10),
1190                         overuse_penalty_start_1024th: 1024,
1191                         overuse_penalty_msat_per_1024th: 0,
1192                 });
1193                 let source = source_node_id();
1194                 let target = target_node_id();
1195                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1196
1197                 scorer.payment_path_failed(&[], 42);
1198                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1199
1200                 SinceEpoch::advance(Duration::from_secs(10));
1201                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1202
1203                 let hop = RouteHop {
1204                         pubkey: PublicKey::from_slice(target.as_slice()).unwrap(),
1205                         node_features: NodeFeatures::known(),
1206                         short_channel_id: 42,
1207                         channel_features: ChannelFeatures::known(),
1208                         fee_msat: 1,
1209                         cltv_expiry_delta: 18,
1210                 };
1211                 scorer.payment_path_successful(&[&hop]);
1212                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1213
1214                 SinceEpoch::advance(Duration::from_secs(10));
1215                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1216         }
1217
1218         #[test]
1219         fn restores_persisted_channel_failure_penalties() {
1220                 let mut scorer = Scorer::new(ScoringParameters {
1221                         base_penalty_msat: 1_000,
1222                         failure_penalty_msat: 512,
1223                         failure_penalty_half_life: Duration::from_secs(10),
1224                         overuse_penalty_start_1024th: 1024,
1225                         overuse_penalty_msat_per_1024th: 0,
1226                 });
1227                 let source = source_node_id();
1228                 let target = target_node_id();
1229
1230                 scorer.payment_path_failed(&[], 42);
1231                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1232
1233                 SinceEpoch::advance(Duration::from_secs(10));
1234                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1235
1236                 scorer.payment_path_failed(&[], 43);
1237                 assert_eq!(scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1238
1239                 let mut serialized_scorer = Vec::new();
1240                 scorer.write(&mut serialized_scorer).unwrap();
1241
1242                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1243                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1244                 assert_eq!(deserialized_scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1245         }
1246
1247         #[test]
1248         fn decays_persisted_channel_failure_penalties() {
1249                 let mut scorer = Scorer::new(ScoringParameters {
1250                         base_penalty_msat: 1_000,
1251                         failure_penalty_msat: 512,
1252                         failure_penalty_half_life: Duration::from_secs(10),
1253                         overuse_penalty_start_1024th: 1024,
1254                         overuse_penalty_msat_per_1024th: 0,
1255                 });
1256                 let source = source_node_id();
1257                 let target = target_node_id();
1258
1259                 scorer.payment_path_failed(&[], 42);
1260                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1261
1262                 let mut serialized_scorer = Vec::new();
1263                 scorer.write(&mut serialized_scorer).unwrap();
1264
1265                 SinceEpoch::advance(Duration::from_secs(10));
1266
1267                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1268                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1269
1270                 SinceEpoch::advance(Duration::from_secs(10));
1271                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1272         }
1273
1274         #[test]
1275         fn charges_per_1024th_penalty() {
1276                 let scorer = Scorer::new(ScoringParameters {
1277                         base_penalty_msat: 0,
1278                         failure_penalty_msat: 0,
1279                         failure_penalty_half_life: Duration::from_secs(0),
1280                         overuse_penalty_start_1024th: 256,
1281                         overuse_penalty_msat_per_1024th: 100,
1282                 });
1283                 let source = source_node_id();
1284                 let target = target_node_id();
1285
1286                 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 1_024_000, &source, &target), 0);
1287                 assert_eq!(scorer.channel_penalty_msat(42, 256_999, 1_024_000, &source, &target), 0);
1288                 assert_eq!(scorer.channel_penalty_msat(42, 257_000, 1_024_000, &source, &target), 100);
1289                 assert_eq!(scorer.channel_penalty_msat(42, 258_000, 1_024_000, &source, &target), 200);
1290                 assert_eq!(scorer.channel_penalty_msat(42, 512_000, 1_024_000, &source, &target), 256 * 100);
1291         }
1292
1293         // `ProbabilisticScorer` tests
1294
1295         /// A probabilistic scorer for testing with time that can be manually advanced.
1296         type ProbabilisticScorer<'a> = ProbabilisticScorerUsingTime::<&'a NetworkGraph, SinceEpoch>;
1297
1298         fn sender_privkey() -> SecretKey {
1299                 SecretKey::from_slice(&[41; 32]).unwrap()
1300         }
1301
1302         fn recipient_privkey() -> SecretKey {
1303                 SecretKey::from_slice(&[45; 32]).unwrap()
1304         }
1305
1306         fn sender_pubkey() -> PublicKey {
1307                 let secp_ctx = Secp256k1::new();
1308                 PublicKey::from_secret_key(&secp_ctx, &sender_privkey())
1309         }
1310
1311         fn recipient_pubkey() -> PublicKey {
1312                 let secp_ctx = Secp256k1::new();
1313                 PublicKey::from_secret_key(&secp_ctx, &recipient_privkey())
1314         }
1315
1316         fn sender_node_id() -> NodeId {
1317                 NodeId::from_pubkey(&sender_pubkey())
1318         }
1319
1320         fn recipient_node_id() -> NodeId {
1321                 NodeId::from_pubkey(&recipient_pubkey())
1322         }
1323
1324         fn network_graph() -> NetworkGraph {
1325                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1326                 let mut network_graph = NetworkGraph::new(genesis_hash);
1327                 add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
1328                 add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
1329
1330                 network_graph
1331         }
1332
1333         fn add_channel(
1334                 network_graph: &mut NetworkGraph, short_channel_id: u64, node_1_key: SecretKey,
1335                 node_2_key: SecretKey
1336         ) {
1337                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1338                 let node_1_secret = &SecretKey::from_slice(&[39; 32]).unwrap();
1339                 let node_2_secret = &SecretKey::from_slice(&[40; 32]).unwrap();
1340                 let secp_ctx = Secp256k1::new();
1341                 let unsigned_announcement = UnsignedChannelAnnouncement {
1342                         features: ChannelFeatures::known(),
1343                         chain_hash: genesis_hash,
1344                         short_channel_id,
1345                         node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_key),
1346                         node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_key),
1347                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_secret),
1348                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_secret),
1349                         excess_data: Vec::new(),
1350                 };
1351                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1352                 let signed_announcement = ChannelAnnouncement {
1353                         node_signature_1: secp_ctx.sign(&msghash, &node_1_key),
1354                         node_signature_2: secp_ctx.sign(&msghash, &node_2_key),
1355                         bitcoin_signature_1: secp_ctx.sign(&msghash, &node_1_secret),
1356                         bitcoin_signature_2: secp_ctx.sign(&msghash, &node_2_secret),
1357                         contents: unsigned_announcement,
1358                 };
1359                 let chain_source: Option<&::util::test_utils::TestChainSource> = None;
1360                 network_graph.update_channel_from_announcement(
1361                         &signed_announcement, &chain_source, &secp_ctx).unwrap();
1362                 update_channel(network_graph, short_channel_id, node_1_key, 0);
1363                 update_channel(network_graph, short_channel_id, node_2_key, 1);
1364         }
1365
1366         fn update_channel(
1367                 network_graph: &mut NetworkGraph, short_channel_id: u64, node_key: SecretKey, flags: u8
1368         ) {
1369                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1370                 let secp_ctx = Secp256k1::new();
1371                 let unsigned_update = UnsignedChannelUpdate {
1372                         chain_hash: genesis_hash,
1373                         short_channel_id,
1374                         timestamp: 100,
1375                         flags,
1376                         cltv_expiry_delta: 18,
1377                         htlc_minimum_msat: 0,
1378                         htlc_maximum_msat: OptionalField::Present(1_000),
1379                         fee_base_msat: 1,
1380                         fee_proportional_millionths: 0,
1381                         excess_data: Vec::new(),
1382                 };
1383                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
1384                 let signed_update = ChannelUpdate {
1385                         signature: secp_ctx.sign(&msghash, &node_key),
1386                         contents: unsigned_update,
1387                 };
1388                 network_graph.update_channel(&signed_update, &secp_ctx).unwrap();
1389         }
1390
1391         fn payment_path_for_amount(amount_msat: u64) -> Vec<RouteHop> {
1392                 vec![
1393                         RouteHop {
1394                                 pubkey: source_pubkey(),
1395                                 node_features: NodeFeatures::known(),
1396                                 short_channel_id: 41,
1397                                 channel_features: ChannelFeatures::known(),
1398                                 fee_msat: 1,
1399                                 cltv_expiry_delta: 18,
1400                         },
1401                         RouteHop {
1402                                 pubkey: target_pubkey(),
1403                                 node_features: NodeFeatures::known(),
1404                                 short_channel_id: 42,
1405                                 channel_features: ChannelFeatures::known(),
1406                                 fee_msat: 2,
1407                                 cltv_expiry_delta: 18,
1408                         },
1409                         RouteHop {
1410                                 pubkey: recipient_pubkey(),
1411                                 node_features: NodeFeatures::known(),
1412                                 short_channel_id: 43,
1413                                 channel_features: ChannelFeatures::known(),
1414                                 fee_msat: amount_msat,
1415                                 cltv_expiry_delta: 18,
1416                         },
1417                 ]
1418         }
1419
1420         #[test]
1421         fn liquidity_bounds_directed_from_lowest_node_id() {
1422                 let last_updated = SinceEpoch::now();
1423                 let network_graph = network_graph();
1424                 let params = ProbabilisticScoringParameters::default();
1425                 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1426                         .with_channel(42,
1427                                 ChannelLiquidity {
1428                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100, last_updated
1429                                 })
1430                         .with_channel(43,
1431                                 ChannelLiquidity {
1432                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100, last_updated
1433                                 });
1434                 let source = source_node_id();
1435                 let target = target_node_id();
1436                 let recipient = recipient_node_id();
1437                 assert!(source > target);
1438                 assert!(target < recipient);
1439
1440                 // Update minimum liquidity.
1441
1442                 let liquidity_offset_half_life = scorer.params.liquidity_offset_half_life;
1443                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1444                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1445                 assert_eq!(liquidity.min_liquidity_msat(), 100);
1446                 assert_eq!(liquidity.max_liquidity_msat(), 300);
1447
1448                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1449                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1450                 assert_eq!(liquidity.min_liquidity_msat(), 700);
1451                 assert_eq!(liquidity.max_liquidity_msat(), 900);
1452
1453                 scorer.channel_liquidities.get_mut(&42).unwrap()
1454                         .as_directed_mut(&source, &target, 1_000, liquidity_offset_half_life)
1455                         .set_min_liquidity_msat(200);
1456
1457                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1458                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1459                 assert_eq!(liquidity.min_liquidity_msat(), 200);
1460                 assert_eq!(liquidity.max_liquidity_msat(), 300);
1461
1462                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1463                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1464                 assert_eq!(liquidity.min_liquidity_msat(), 700);
1465                 assert_eq!(liquidity.max_liquidity_msat(), 800);
1466
1467                 // Update maximum liquidity.
1468
1469                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1470                         .as_directed(&target, &recipient, 1_000, liquidity_offset_half_life);
1471                 assert_eq!(liquidity.min_liquidity_msat(), 700);
1472                 assert_eq!(liquidity.max_liquidity_msat(), 900);
1473
1474                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1475                         .as_directed(&recipient, &target, 1_000, liquidity_offset_half_life);
1476                 assert_eq!(liquidity.min_liquidity_msat(), 100);
1477                 assert_eq!(liquidity.max_liquidity_msat(), 300);
1478
1479                 scorer.channel_liquidities.get_mut(&43).unwrap()
1480                         .as_directed_mut(&target, &recipient, 1_000, liquidity_offset_half_life)
1481                         .set_max_liquidity_msat(200);
1482
1483                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1484                         .as_directed(&target, &recipient, 1_000, liquidity_offset_half_life);
1485                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1486                 assert_eq!(liquidity.max_liquidity_msat(), 200);
1487
1488                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1489                         .as_directed(&recipient, &target, 1_000, liquidity_offset_half_life);
1490                 assert_eq!(liquidity.min_liquidity_msat(), 800);
1491                 assert_eq!(liquidity.max_liquidity_msat(), 1000);
1492         }
1493
1494         #[test]
1495         fn resets_liquidity_upper_bound_when_crossed_by_lower_bound() {
1496                 let last_updated = SinceEpoch::now();
1497                 let network_graph = network_graph();
1498                 let params = ProbabilisticScoringParameters::default();
1499                 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1500                         .with_channel(42,
1501                                 ChannelLiquidity {
1502                                         min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400, last_updated
1503                                 });
1504                 let source = source_node_id();
1505                 let target = target_node_id();
1506                 assert!(source > target);
1507
1508                 // Check initial bounds.
1509                 let liquidity_offset_half_life = scorer.params.liquidity_offset_half_life;
1510                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1511                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1512                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1513                 assert_eq!(liquidity.max_liquidity_msat(), 800);
1514
1515                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1516                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1517                 assert_eq!(liquidity.min_liquidity_msat(), 200);
1518                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1519
1520                 // Reset from source to target.
1521                 scorer.channel_liquidities.get_mut(&42).unwrap()
1522                         .as_directed_mut(&source, &target, 1_000, liquidity_offset_half_life)
1523                         .set_min_liquidity_msat(900);
1524
1525                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1526                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1527                 assert_eq!(liquidity.min_liquidity_msat(), 900);
1528                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1529
1530                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1531                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1532                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1533                 assert_eq!(liquidity.max_liquidity_msat(), 100);
1534
1535                 // Reset from target to source.
1536                 scorer.channel_liquidities.get_mut(&42).unwrap()
1537                         .as_directed_mut(&target, &source, 1_000, liquidity_offset_half_life)
1538                         .set_min_liquidity_msat(400);
1539
1540                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1541                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1542                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1543                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1544
1545                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1546                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1547                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1548                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1549         }
1550
1551         #[test]
1552         fn resets_liquidity_lower_bound_when_crossed_by_upper_bound() {
1553                 let last_updated = SinceEpoch::now();
1554                 let network_graph = network_graph();
1555                 let params = ProbabilisticScoringParameters::default();
1556                 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1557                         .with_channel(42,
1558                                 ChannelLiquidity {
1559                                         min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400, last_updated
1560                                 });
1561                 let source = source_node_id();
1562                 let target = target_node_id();
1563                 assert!(source > target);
1564
1565                 // Check initial bounds.
1566                 let liquidity_offset_half_life = scorer.params.liquidity_offset_half_life;
1567                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1568                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1569                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1570                 assert_eq!(liquidity.max_liquidity_msat(), 800);
1571
1572                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1573                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1574                 assert_eq!(liquidity.min_liquidity_msat(), 200);
1575                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1576
1577                 // Reset from source to target.
1578                 scorer.channel_liquidities.get_mut(&42).unwrap()
1579                         .as_directed_mut(&source, &target, 1_000, liquidity_offset_half_life)
1580                         .set_max_liquidity_msat(300);
1581
1582                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1583                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1584                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1585                 assert_eq!(liquidity.max_liquidity_msat(), 300);
1586
1587                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1588                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1589                 assert_eq!(liquidity.min_liquidity_msat(), 700);
1590                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1591
1592                 // Reset from target to source.
1593                 scorer.channel_liquidities.get_mut(&42).unwrap()
1594                         .as_directed_mut(&target, &source, 1_000, liquidity_offset_half_life)
1595                         .set_max_liquidity_msat(600);
1596
1597                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1598                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1599                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1600                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1601
1602                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1603                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1604                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1605                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1606         }
1607
1608         #[test]
1609         fn increased_penalty_nearing_liquidity_upper_bound() {
1610                 let network_graph = network_graph();
1611                 let params = ProbabilisticScoringParameters {
1612                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1613                 };
1614                 let scorer = ProbabilisticScorer::new(params, &network_graph);
1615                 let source = source_node_id();
1616                 let target = target_node_id();
1617
1618                 assert_eq!(scorer.channel_penalty_msat(42, 100, 100_000, &source, &target), 0);
1619                 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 100_000, &source, &target), 4);
1620                 assert_eq!(scorer.channel_penalty_msat(42, 10_000, 100_000, &source, &target), 45);
1621                 assert_eq!(scorer.channel_penalty_msat(42, 100_000, 100_000, &source, &target), 2_000);
1622
1623                 assert_eq!(scorer.channel_penalty_msat(42, 125, 1_000, &source, &target), 57);
1624                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1625                 assert_eq!(scorer.channel_penalty_msat(42, 375, 1_000, &source, &target), 203);
1626                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1627                 assert_eq!(scorer.channel_penalty_msat(42, 625, 1_000, &source, &target), 425);
1628                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1629                 assert_eq!(scorer.channel_penalty_msat(42, 875, 1_000, &source, &target), 900);
1630         }
1631
1632         #[test]
1633         fn constant_penalty_outside_liquidity_bounds() {
1634                 let last_updated = SinceEpoch::now();
1635                 let network_graph = network_graph();
1636                 let params = ProbabilisticScoringParameters {
1637                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1638                 };
1639                 let scorer = ProbabilisticScorer::new(params, &network_graph)
1640                         .with_channel(42,
1641                                 ChannelLiquidity {
1642                                         min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40, last_updated
1643                                 });
1644                 let source = source_node_id();
1645                 let target = target_node_id();
1646
1647                 assert_eq!(scorer.channel_penalty_msat(42, 39, 100, &source, &target), 0);
1648                 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 0);
1649                 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 2_000);
1650                 assert_eq!(scorer.channel_penalty_msat(42, 61, 100, &source, &target), 2_000);
1651         }
1652
1653         #[test]
1654         fn does_not_further_penalize_own_channel() {
1655                 let network_graph = network_graph();
1656                 let params = ProbabilisticScoringParameters {
1657                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1658                 };
1659                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1660                 let sender = sender_node_id();
1661                 let source = source_node_id();
1662                 let failed_path = payment_path_for_amount(500);
1663                 let successful_path = payment_path_for_amount(200);
1664
1665                 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1666
1667                 scorer.payment_path_failed(&failed_path.iter().collect::<Vec<_>>(), 41);
1668                 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1669
1670                 scorer.payment_path_successful(&successful_path.iter().collect::<Vec<_>>());
1671                 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1672         }
1673
1674         #[test]
1675         fn sets_liquidity_lower_bound_on_downstream_failure() {
1676                 let network_graph = network_graph();
1677                 let params = ProbabilisticScoringParameters {
1678                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1679                 };
1680                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1681                 let source = source_node_id();
1682                 let target = target_node_id();
1683                 let path = payment_path_for_amount(500);
1684
1685                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1686                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1687                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1688
1689                 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 43);
1690
1691                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 0);
1692                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 0);
1693                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 300);
1694         }
1695
1696         #[test]
1697         fn sets_liquidity_upper_bound_on_failure() {
1698                 let network_graph = network_graph();
1699                 let params = ProbabilisticScoringParameters {
1700                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1701                 };
1702                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1703                 let source = source_node_id();
1704                 let target = target_node_id();
1705                 let path = payment_path_for_amount(500);
1706
1707                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1708                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1709                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1710
1711                 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 42);
1712
1713                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 300);
1714                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2_000);
1715                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 2_000);
1716         }
1717
1718         #[test]
1719         fn reduces_liquidity_upper_bound_along_path_on_success() {
1720                 let network_graph = network_graph();
1721                 let params = ProbabilisticScoringParameters {
1722                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1723                 };
1724                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1725                 let sender = sender_node_id();
1726                 let source = source_node_id();
1727                 let target = target_node_id();
1728                 let recipient = recipient_node_id();
1729                 let path = payment_path_for_amount(500);
1730
1731                 assert_eq!(scorer.channel_penalty_msat(41, 250, 1_000, &sender, &source), 124);
1732                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1733                 assert_eq!(scorer.channel_penalty_msat(43, 250, 1_000, &target, &recipient), 124);
1734
1735                 scorer.payment_path_successful(&path.iter().collect::<Vec<_>>());
1736
1737                 assert_eq!(scorer.channel_penalty_msat(41, 250, 1_000, &sender, &source), 124);
1738                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 300);
1739                 assert_eq!(scorer.channel_penalty_msat(43, 250, 1_000, &target, &recipient), 300);
1740         }
1741
1742         #[test]
1743         fn decays_liquidity_bounds_over_time() {
1744                 let network_graph = network_graph();
1745                 let params = ProbabilisticScoringParameters {
1746                         liquidity_penalty_multiplier_msat: 1_000,
1747                         liquidity_offset_half_life: Duration::from_secs(10),
1748                 };
1749                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1750                 let source = source_node_id();
1751                 let target = target_node_id();
1752
1753                 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1754                 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1755
1756                 scorer.payment_path_failed(&payment_path_for_amount(768).iter().collect::<Vec<_>>(), 42);
1757                 scorer.payment_path_failed(&payment_path_for_amount(128).iter().collect::<Vec<_>>(), 43);
1758
1759                 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 0);
1760                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 92);
1761                 assert_eq!(scorer.channel_penalty_msat(42, 768, 1_024, &source, &target), 1_424);
1762                 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), 2_000);
1763
1764                 SinceEpoch::advance(Duration::from_secs(9));
1765                 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 0);
1766                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 92);
1767                 assert_eq!(scorer.channel_penalty_msat(42, 768, 1_024, &source, &target), 1_424);
1768                 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), 2_000);
1769
1770                 SinceEpoch::advance(Duration::from_secs(1));
1771                 assert_eq!(scorer.channel_penalty_msat(42, 64, 1_024, &source, &target), 0);
1772                 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 34);
1773                 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), 1_812);
1774                 assert_eq!(scorer.channel_penalty_msat(42, 960, 1_024, &source, &target), 2_000);
1775
1776                 // Fully decay liquidity lower bound.
1777                 SinceEpoch::advance(Duration::from_secs(10 * 7));
1778                 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1779                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1_024, &source, &target), 0);
1780                 assert_eq!(scorer.channel_penalty_msat(42, 1_023, 1_024, &source, &target), 2_000);
1781                 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1782
1783                 // Fully decay liquidity upper bound.
1784                 SinceEpoch::advance(Duration::from_secs(10));
1785                 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1786                 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1787
1788                 SinceEpoch::advance(Duration::from_secs(10));
1789                 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1790                 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1791         }
1792
1793         #[test]
1794         fn decays_liquidity_bounds_without_shift_overflow() {
1795                 let network_graph = network_graph();
1796                 let params = ProbabilisticScoringParameters {
1797                         liquidity_penalty_multiplier_msat: 1_000,
1798                         liquidity_offset_half_life: Duration::from_secs(10),
1799                 };
1800                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1801                 let source = source_node_id();
1802                 let target = target_node_id();
1803                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 124);
1804
1805                 scorer.payment_path_failed(&payment_path_for_amount(512).iter().collect::<Vec<_>>(), 42);
1806                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 281);
1807
1808                 // An unchecked right shift 64 bits or more in DirectedChannelLiquidity::decayed_offset_msat
1809                 // would cause an overflow.
1810                 SinceEpoch::advance(Duration::from_secs(10 * 64));
1811                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 124);
1812
1813                 SinceEpoch::advance(Duration::from_secs(10));
1814                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 124);
1815         }
1816
1817         #[test]
1818         fn restricts_liquidity_bounds_after_decay() {
1819                 let network_graph = network_graph();
1820                 let params = ProbabilisticScoringParameters {
1821                         liquidity_penalty_multiplier_msat: 1_000,
1822                         liquidity_offset_half_life: Duration::from_secs(10),
1823                 };
1824                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1825                 let source = source_node_id();
1826                 let target = target_node_id();
1827
1828                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 300);
1829
1830                 // More knowledge gives higher confidence (256, 768), meaning a lower penalty.
1831                 scorer.payment_path_failed(&payment_path_for_amount(768).iter().collect::<Vec<_>>(), 42);
1832                 scorer.payment_path_failed(&payment_path_for_amount(256).iter().collect::<Vec<_>>(), 43);
1833                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 281);
1834
1835                 // Decaying knowledge gives less confidence (128, 896), meaning a higher penalty.
1836                 SinceEpoch::advance(Duration::from_secs(10));
1837                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 293);
1838
1839                 // Reducing the upper bound gives more confidence (128, 832) that the payment amount (512)
1840                 // is closer to the upper bound, meaning a higher penalty.
1841                 scorer.payment_path_successful(&payment_path_for_amount(64).iter().collect::<Vec<_>>());
1842                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 333);
1843
1844                 // Increasing the lower bound gives more confidence (256, 832) that the payment amount (512)
1845                 // is closer to the lower bound, meaning a lower penalty.
1846                 scorer.payment_path_failed(&payment_path_for_amount(256).iter().collect::<Vec<_>>(), 43);
1847                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 247);
1848
1849                 // Further decaying affects the lower bound more than the upper bound (128, 928).
1850                 SinceEpoch::advance(Duration::from_secs(10));
1851                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 280);
1852         }
1853
1854         #[test]
1855         fn restores_persisted_liquidity_bounds() {
1856                 let network_graph = network_graph();
1857                 let params = ProbabilisticScoringParameters {
1858                         liquidity_penalty_multiplier_msat: 1_000,
1859                         liquidity_offset_half_life: Duration::from_secs(10),
1860                 };
1861                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1862                 let source = source_node_id();
1863                 let target = target_node_id();
1864
1865                 scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
1866                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2_000);
1867
1868                 SinceEpoch::advance(Duration::from_secs(10));
1869                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 475);
1870
1871                 scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
1872                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1873
1874                 let mut serialized_scorer = Vec::new();
1875                 scorer.write(&mut serialized_scorer).unwrap();
1876
1877                 let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
1878                 let deserialized_scorer =
1879                         <ProbabilisticScorer>::read(&mut serialized_scorer, (params, &network_graph)).unwrap();
1880                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1881         }
1882
1883         #[test]
1884         fn decays_persisted_liquidity_bounds() {
1885                 let network_graph = network_graph();
1886                 let params = ProbabilisticScoringParameters {
1887                         liquidity_penalty_multiplier_msat: 1_000,
1888                         liquidity_offset_half_life: Duration::from_secs(10),
1889                 };
1890                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1891                 let source = source_node_id();
1892                 let target = target_node_id();
1893
1894                 scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
1895                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2_000);
1896
1897                 let mut serialized_scorer = Vec::new();
1898                 scorer.write(&mut serialized_scorer).unwrap();
1899
1900                 SinceEpoch::advance(Duration::from_secs(10));
1901
1902                 let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
1903                 let deserialized_scorer =
1904                         <ProbabilisticScorer>::read(&mut serialized_scorer, (params, &network_graph)).unwrap();
1905                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 475);
1906
1907                 scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
1908                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1909
1910                 SinceEpoch::advance(Duration::from_secs(10));
1911                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 367);
1912         }
1913 }