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