Avoid retrying over recently failed channels
[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::chain::keysinterface::{KeysManager, KeysInterface};
24 //! # use lightning::util::logger::{Logger, Record};
25 //! # use secp256k1::key::PublicKey;
26 //! #
27 //! # struct FakeLogger {};
28 //! # impl Logger for FakeLogger {
29 //! #     fn log(&self, record: &Record) { unimplemented!() }
30 //! # }
31 //! # fn find_scored_route(payer: PublicKey, route_params: RouteParameters, network_graph: NetworkGraph) {
32 //! # let logger = FakeLogger {};
33 //! #
34 //! // Use the default channel penalties.
35 //! let params = ProbabilisticScoringParameters::default();
36 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
37 //!
38 //! // Or use custom channel penalties.
39 //! let params = ProbabilisticScoringParameters {
40 //!     liquidity_penalty_multiplier_msat: 2 * 1000,
41 //!     ..ProbabilisticScoringParameters::default()
42 //! };
43 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
44 //! # let random_seed_bytes = [42u8; 32];
45 //!
46 //! let route = find_route(&payer, &route_params, &network_graph, None, &logger, &scorer, &random_seed_bytes);
47 //! # }
48 //! ```
49 //!
50 //! # Note
51 //!
52 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
53 //! different types and thus is undefined.
54 //!
55 //! [`find_route`]: crate::routing::router::find_route
56
57 use ln::msgs::DecodeError;
58 use routing::network_graph::{NetworkGraph, NodeId};
59 use routing::router::RouteHop;
60 use util::ser::{Readable, ReadableArgs, Writeable, Writer};
61
62 use prelude::*;
63 use core::cell::{RefCell, RefMut};
64 use core::ops::{Deref, DerefMut};
65 use core::time::Duration;
66 use io::{self, Read};
67 use sync::{Mutex, MutexGuard};
68
69 /// We define Score ever-so-slightly differently based on whether we are being built for C bindings
70 /// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
71 /// no problem - you move a `Score` that implements `Writeable` into a `Mutex`, lock it, and now
72 /// you have the original, concrete, `Score` type, which presumably implements `Writeable`.
73 ///
74 /// For C users, once you've moved the `Score` into a `LockableScore` all you have after locking it
75 /// is an opaque trait object with an opaque pointer with no type info. Users could take the unsafe
76 /// approach of blindly casting that opaque pointer to a concrete type and calling `Writeable` from
77 /// there, but other languages downstream of the C bindings (e.g. Java) can't even do that.
78 /// Instead, we really want `Score` and `LockableScore` to implement `Writeable` directly, which we
79 /// do here by defining `Score` differently for `cfg(c_bindings)`.
80 macro_rules! define_score { ($($supertrait: path)*) => {
81 /// An interface used to score payment channels for path finding.
82 ///
83 ///     Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
84 pub trait Score $(: $supertrait)* {
85         /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
86         /// given channel in the direction from `source` to `target`.
87         ///
88         /// The channel's capacity (less any other MPP parts that are also being considered for use in
89         /// the same payment) is given by `capacity_msat`. It may be determined from various sources
90         /// such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
91         /// [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
92         /// Thus, implementations should be overflow-safe.
93         fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &NodeId, target: &NodeId) -> u64;
94
95         /// Handles updating channel penalties after failing to route through a channel.
96         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64);
97
98         /// Handles updating channel penalties after successfully routing along a path.
99         fn payment_path_successful(&mut self, path: &[&RouteHop]);
100 }
101
102 impl<S: Score, T: DerefMut<Target=S> $(+ $supertrait)*> Score for T {
103         fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &NodeId, target: &NodeId) -> u64 {
104                 self.deref().channel_penalty_msat(short_channel_id, send_amt_msat, capacity_msat, source, target)
105         }
106
107         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
108                 self.deref_mut().payment_path_failed(path, short_channel_id)
109         }
110
111         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
112                 self.deref_mut().payment_path_successful(path)
113         }
114 }
115 } }
116
117 #[cfg(c_bindings)]
118 define_score!(Writeable);
119 #[cfg(not(c_bindings))]
120 define_score!();
121
122 /// A scorer that is accessed under a lock.
123 ///
124 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
125 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
126 /// implementations. Internal locking would be detrimental to route finding performance and could
127 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
128 ///
129 /// [`find_route`]: crate::routing::router::find_route
130 pub trait LockableScore<'a> {
131         /// The locked [`Score`] type.
132         type Locked: 'a + Score;
133
134         /// Returns the locked scorer.
135         fn lock(&'a self) -> Self::Locked;
136 }
137
138 /// (C-not exported)
139 impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
140         type Locked = MutexGuard<'a, T>;
141
142         fn lock(&'a self) -> MutexGuard<'a, T> {
143                 Mutex::lock(self).unwrap()
144         }
145 }
146
147 impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
148         type Locked = RefMut<'a, T>;
149
150         fn lock(&'a self) -> RefMut<'a, T> {
151                 self.borrow_mut()
152         }
153 }
154
155 #[cfg(c_bindings)]
156 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
157 pub struct MultiThreadedLockableScore<S: Score> {
158         score: Mutex<S>,
159 }
160 #[cfg(c_bindings)]
161 /// (C-not exported)
162 impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore<T> {
163         type Locked = MutexGuard<'a, T>;
164
165         fn lock(&'a self) -> MutexGuard<'a, T> {
166                 Mutex::lock(&self.score).unwrap()
167         }
168 }
169
170 #[cfg(c_bindings)]
171 impl<T: Score> MultiThreadedLockableScore<T> {
172         /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
173         pub fn new(score: T) -> Self {
174                 MultiThreadedLockableScore { score: Mutex::new(score) }
175         }
176 }
177
178 #[cfg(c_bindings)]
179 /// (C-not exported)
180 impl<'a, T: Writeable> Writeable for RefMut<'a, T> {
181         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
182                 T::write(&**self, writer)
183         }
184 }
185
186 #[cfg(c_bindings)]
187 /// (C-not exported)
188 impl<'a, S: Writeable> Writeable for MutexGuard<'a, S> {
189         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
190                 S::write(&**self, writer)
191         }
192 }
193
194 #[derive(Clone)]
195 /// [`Score`] implementation that uses a fixed penalty.
196 pub struct FixedPenaltyScorer {
197         penalty_msat: u64,
198 }
199
200 impl FixedPenaltyScorer {
201         /// Creates a new scorer using `penalty_msat`.
202         pub fn with_penalty(penalty_msat: u64) -> Self {
203                 Self { penalty_msat }
204         }
205 }
206
207 impl Score for FixedPenaltyScorer {
208         fn channel_penalty_msat(&self, _: u64, _: u64, _: u64, _: &NodeId, _: &NodeId) -> u64 {
209                 self.penalty_msat
210         }
211
212         fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
213
214         fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
215 }
216
217 impl Writeable for FixedPenaltyScorer {
218         #[inline]
219         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
220                 write_tlv_fields!(w, {});
221                 Ok(())
222         }
223 }
224
225 impl ReadableArgs<u64> for FixedPenaltyScorer {
226         #[inline]
227         fn read<R: Read>(r: &mut R, penalty_msat: u64) -> Result<Self, DecodeError> {
228                 read_tlv_fields!(r, {});
229                 Ok(Self { penalty_msat })
230         }
231 }
232
233 /// [`Score`] implementation that provides reasonable default behavior.
234 ///
235 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
236 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
237 ///
238 /// See [module-level documentation] for usage and [`ScoringParameters`] for customization.
239 ///
240 /// # Note
241 ///
242 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
243 /// behavior.
244 ///
245 /// [module-level documentation]: crate::routing::scoring
246 #[deprecated(
247         since = "0.0.105",
248         note = "ProbabilisticScorer should be used instead of Scorer.",
249 )]
250 pub type Scorer = ScorerUsingTime::<ConfiguredTime>;
251
252 #[cfg(not(feature = "no-std"))]
253 type ConfiguredTime = std::time::Instant;
254 #[cfg(feature = "no-std")]
255 type ConfiguredTime = time::Eternity;
256
257 // Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
258 // doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
259 // methods at all.
260
261 /// [`Score`] implementation.
262 ///
263 /// (C-not exported) generally all users should use the [`Scorer`] type alias.
264 pub struct ScorerUsingTime<T: Time> {
265         params: ScoringParameters,
266         // TODO: Remove entries of closed channels.
267         channel_failures: HashMap<u64, ChannelFailure<T>>,
268 }
269
270 #[derive(Clone)]
271 /// Parameters for configuring [`Scorer`].
272 pub struct ScoringParameters {
273         /// A fixed penalty in msats to apply to each channel.
274         ///
275         /// Default value: 500 msat
276         pub base_penalty_msat: u64,
277
278         /// A penalty in msats to apply to a channel upon failing to relay a payment.
279         ///
280         /// This accumulates for each failure but may be reduced over time based on
281         /// [`failure_penalty_half_life`] or when successfully routing through a channel.
282         ///
283         /// Default value: 1,024,000 msat
284         ///
285         /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
286         pub failure_penalty_msat: u64,
287
288         /// When the amount being sent over a channel is this many 1024ths of the total channel
289         /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
290         ///
291         /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
292         ///
293         /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
294         pub overuse_penalty_start_1024th: u16,
295
296         /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
297         /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
298         ///
299         /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
300         ///                to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
301         ///
302         /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
303         pub overuse_penalty_msat_per_1024th: u64,
304
305         /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
306         /// cut in half.
307         ///
308         /// Successfully routing through a channel will immediately cut the penalty in half as well.
309         ///
310         /// Default value: 1 hour
311         ///
312         /// # Note
313         ///
314         /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
315         /// never decay.
316         ///
317         /// [`failure_penalty_msat`]: Self::failure_penalty_msat
318         pub failure_penalty_half_life: Duration,
319 }
320
321 impl_writeable_tlv_based!(ScoringParameters, {
322         (0, base_penalty_msat, required),
323         (1, overuse_penalty_start_1024th, (default_value, 128)),
324         (2, failure_penalty_msat, required),
325         (3, overuse_penalty_msat_per_1024th, (default_value, 20)),
326         (4, failure_penalty_half_life, required),
327 });
328
329 /// Accounting for penalties against a channel for failing to relay any payments.
330 ///
331 /// Penalties decay over time, though accumulate as more failures occur.
332 struct ChannelFailure<T: Time> {
333         /// Accumulated penalty in msats for the channel as of `last_updated`.
334         undecayed_penalty_msat: u64,
335
336         /// Last time the channel either failed to route or successfully routed a payment. Used to decay
337         /// `undecayed_penalty_msat`.
338         last_updated: T,
339 }
340
341 impl<T: Time> ScorerUsingTime<T> {
342         /// Creates a new scorer using the given scoring parameters.
343         pub fn new(params: ScoringParameters) -> Self {
344                 Self {
345                         params,
346                         channel_failures: HashMap::new(),
347                 }
348         }
349 }
350
351 impl<T: Time> ChannelFailure<T> {
352         fn new(failure_penalty_msat: u64) -> Self {
353                 Self {
354                         undecayed_penalty_msat: failure_penalty_msat,
355                         last_updated: T::now(),
356                 }
357         }
358
359         fn add_penalty(&mut self, failure_penalty_msat: u64, half_life: Duration) {
360                 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) + failure_penalty_msat;
361                 self.last_updated = T::now();
362         }
363
364         fn reduce_penalty(&mut self, half_life: Duration) {
365                 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) >> 1;
366                 self.last_updated = T::now();
367         }
368
369         fn decayed_penalty_msat(&self, half_life: Duration) -> u64 {
370                 self.last_updated.elapsed().as_secs()
371                         .checked_div(half_life.as_secs())
372                         .and_then(|decays| self.undecayed_penalty_msat.checked_shr(decays as u32))
373                         .unwrap_or(0)
374         }
375 }
376
377 impl<T: Time> Default for ScorerUsingTime<T> {
378         fn default() -> Self {
379                 Self::new(ScoringParameters::default())
380         }
381 }
382
383 impl Default for ScoringParameters {
384         fn default() -> Self {
385                 Self {
386                         base_penalty_msat: 500,
387                         failure_penalty_msat: 1024 * 1000,
388                         failure_penalty_half_life: Duration::from_secs(3600),
389                         overuse_penalty_start_1024th: 1024 / 8,
390                         overuse_penalty_msat_per_1024th: 20,
391                 }
392         }
393 }
394
395 impl<T: Time> Score for ScorerUsingTime<T> {
396         fn channel_penalty_msat(
397                 &self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, _source: &NodeId, _target: &NodeId
398         ) -> u64 {
399                 let failure_penalty_msat = self.channel_failures
400                         .get(&short_channel_id)
401                         .map_or(0, |value| value.decayed_penalty_msat(self.params.failure_penalty_half_life));
402
403                 let mut penalty_msat = self.params.base_penalty_msat + failure_penalty_msat;
404                 let send_1024ths = send_amt_msat.checked_mul(1024).unwrap_or(u64::max_value()) / capacity_msat;
405                 if send_1024ths > self.params.overuse_penalty_start_1024th as u64 {
406                         penalty_msat = penalty_msat.checked_add(
407                                         (send_1024ths - self.params.overuse_penalty_start_1024th as u64)
408                                         .checked_mul(self.params.overuse_penalty_msat_per_1024th).unwrap_or(u64::max_value()))
409                                 .unwrap_or(u64::max_value());
410                 }
411
412                 penalty_msat
413         }
414
415         fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
416                 let failure_penalty_msat = self.params.failure_penalty_msat;
417                 let half_life = self.params.failure_penalty_half_life;
418                 self.channel_failures
419                         .entry(short_channel_id)
420                         .and_modify(|failure| failure.add_penalty(failure_penalty_msat, half_life))
421                         .or_insert_with(|| ChannelFailure::new(failure_penalty_msat));
422         }
423
424         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
425                 let half_life = self.params.failure_penalty_half_life;
426                 for hop in path.iter() {
427                         self.channel_failures
428                                 .entry(hop.short_channel_id)
429                                 .and_modify(|failure| failure.reduce_penalty(half_life));
430                 }
431         }
432 }
433
434 impl<T: Time> Writeable for ScorerUsingTime<T> {
435         #[inline]
436         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
437                 self.params.write(w)?;
438                 self.channel_failures.write(w)?;
439                 write_tlv_fields!(w, {});
440                 Ok(())
441         }
442 }
443
444 impl<T: Time> Readable for ScorerUsingTime<T> {
445         #[inline]
446         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
447                 let res = Ok(Self {
448                         params: Readable::read(r)?,
449                         channel_failures: Readable::read(r)?,
450                 });
451                 read_tlv_fields!(r, {});
452                 res
453         }
454 }
455
456 impl<T: Time> Writeable for ChannelFailure<T> {
457         #[inline]
458         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
459                 let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
460                 write_tlv_fields!(w, {
461                         (0, self.undecayed_penalty_msat, required),
462                         (2, duration_since_epoch, required),
463                 });
464                 Ok(())
465         }
466 }
467
468 impl<T: Time> Readable for ChannelFailure<T> {
469         #[inline]
470         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
471                 let mut undecayed_penalty_msat = 0;
472                 let mut duration_since_epoch = Duration::from_secs(0);
473                 read_tlv_fields!(r, {
474                         (0, undecayed_penalty_msat, required),
475                         (2, duration_since_epoch, required),
476                 });
477                 Ok(Self {
478                         undecayed_penalty_msat,
479                         last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
480                 })
481         }
482 }
483
484 /// [`Score`] implementation using channel success probability distributions.
485 ///
486 /// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
487 /// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
488 /// distributions are defined based on knowledge learned from successful and unsuccessful attempts.
489 /// Then the negative `log10` of the success probability is used to determine the cost of routing a
490 /// specific HTLC amount through a channel.
491 ///
492 /// Knowledge about channel liquidity balances takes the form of upper and lower bounds on the
493 /// possible liquidity. Certainty of the bounds is decreased over time using a decay function. See
494 /// [`ProbabilisticScoringParameters`] for details.
495 ///
496 /// Since the scorer aims to learn the current channel liquidity balances, it works best for nodes
497 /// with high payment volume or that actively probe the [`NetworkGraph`]. Nodes with low payment
498 /// volume are more likely to experience failed payment paths, which would need to be retried.
499 ///
500 /// # Note
501 ///
502 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
503 /// behavior.
504 ///
505 /// [1]: https://arxiv.org/abs/2107.05322
506 pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, ConfiguredTime>;
507
508 /// Probabilistic [`Score`] implementation.
509 ///
510 /// (C-not exported) generally all users should use the [`ProbabilisticScorer`] type alias.
511 pub struct ProbabilisticScorerUsingTime<G: Deref<Target = NetworkGraph>, T: Time> {
512         params: ProbabilisticScoringParameters,
513         network_graph: G,
514         // TODO: Remove entries of closed channels.
515         channel_liquidities: HashMap<u64, ChannelLiquidity<T>>,
516 }
517
518 /// Parameters for configuring [`ProbabilisticScorer`].
519 ///
520 /// Used to configure a base penalty and a liquidity penalty, the sum of which is the channel
521 /// penalty (i.e., the amount in msats willing to be paid to avoid routing through the channel).
522 #[derive(Clone, Copy)]
523 pub struct ProbabilisticScoringParameters {
524         /// A fixed penalty in msats to apply to each channel.
525         ///
526         /// Default value: 500 msat
527         pub base_penalty_msat: u64,
528
529         /// A multiplier used in conjunction with the negative `log10` of the channel's success
530         /// probability for a payment to determine the liquidity penalty.
531         ///
532         /// The penalty is based in part on the knowledge learned from prior successful and unsuccessful
533         /// payments. This knowledge is decayed over time based on [`liquidity_offset_half_life`]. The
534         /// penalty is effectively limited to `2 * liquidity_penalty_multiplier_msat` (corresponding to
535         /// lower bounding the success probability to `0.01`) when the amount falls within the
536         /// uncertainty bounds of the channel liquidity balance. Amounts above the upper bound will
537         /// result in a `u64::max_value` penalty, however.
538         ///
539         /// Default value: 40,000 msat
540         ///
541         /// [`liquidity_offset_half_life`]: Self::liquidity_offset_half_life
542         pub liquidity_penalty_multiplier_msat: u64,
543
544         /// The time required to elapse before any knowledge learned about channel liquidity balances is
545         /// cut in half.
546         ///
547         /// The bounds are defined in terms of offsets and are initially zero. Increasing the offsets
548         /// gives tighter bounds on the channel liquidity balance. Thus, halving the offsets decreases
549         /// the certainty of the channel liquidity balance.
550         ///
551         /// Default value: 1 hour
552         ///
553         /// # Note
554         ///
555         /// When built with the `no-std` feature, time will never elapse. Therefore, the channel
556         /// liquidity knowledge will never decay except when the bounds cross.
557         pub liquidity_offset_half_life: Duration,
558 }
559
560 /// Accounting for channel liquidity balance uncertainty.
561 ///
562 /// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
563 /// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
564 /// offset fields gives the opposite direction.
565 struct ChannelLiquidity<T: Time> {
566         /// Lower channel liquidity bound in terms of an offset from zero.
567         min_liquidity_offset_msat: u64,
568
569         /// Upper channel liquidity bound in terms of an offset from the effective capacity.
570         max_liquidity_offset_msat: u64,
571
572         /// Time when the liquidity bounds were last modified.
573         last_updated: T,
574 }
575
576 /// A snapshot of [`ChannelLiquidity`] in one direction assuming a certain channel capacity and
577 /// decayed with a given half life.
578 struct DirectedChannelLiquidity<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> {
579         min_liquidity_offset_msat: L,
580         max_liquidity_offset_msat: L,
581         capacity_msat: u64,
582         last_updated: U,
583         now: T,
584         half_life: Duration,
585 }
586
587 impl<G: Deref<Target = NetworkGraph>, T: Time> ProbabilisticScorerUsingTime<G, T> {
588         /// Creates a new scorer using the given scoring parameters for sending payments from a node
589         /// through a network graph.
590         pub fn new(params: ProbabilisticScoringParameters, network_graph: G) -> Self {
591                 Self {
592                         params,
593                         network_graph,
594                         channel_liquidities: HashMap::new(),
595                 }
596         }
597
598         #[cfg(test)]
599         fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity<T>) -> Self {
600                 assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
601                 self
602         }
603 }
604
605 impl Default for ProbabilisticScoringParameters {
606         fn default() -> Self {
607                 Self {
608                         base_penalty_msat: 500,
609                         liquidity_penalty_multiplier_msat: 40_000,
610                         liquidity_offset_half_life: Duration::from_secs(3600),
611                 }
612         }
613 }
614
615 impl<T: Time> ChannelLiquidity<T> {
616         #[inline]
617         fn new() -> Self {
618                 Self {
619                         min_liquidity_offset_msat: 0,
620                         max_liquidity_offset_msat: 0,
621                         last_updated: T::now(),
622                 }
623         }
624
625         /// Returns a view of the channel liquidity directed from `source` to `target` assuming
626         /// `capacity_msat`.
627         fn as_directed(
628                 &self, source: &NodeId, target: &NodeId, capacity_msat: u64, half_life: Duration
629         ) -> DirectedChannelLiquidity<&u64, T, &T> {
630                 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
631                         (&self.min_liquidity_offset_msat, &self.max_liquidity_offset_msat)
632                 } else {
633                         (&self.max_liquidity_offset_msat, &self.min_liquidity_offset_msat)
634                 };
635
636                 DirectedChannelLiquidity {
637                         min_liquidity_offset_msat,
638                         max_liquidity_offset_msat,
639                         capacity_msat,
640                         last_updated: &self.last_updated,
641                         now: T::now(),
642                         half_life,
643                 }
644         }
645
646         /// Returns a mutable view of the channel liquidity directed from `source` to `target` assuming
647         /// `capacity_msat`.
648         fn as_directed_mut(
649                 &mut self, source: &NodeId, target: &NodeId, capacity_msat: u64, half_life: Duration
650         ) -> DirectedChannelLiquidity<&mut u64, T, &mut T> {
651                 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
652                         (&mut self.min_liquidity_offset_msat, &mut self.max_liquidity_offset_msat)
653                 } else {
654                         (&mut self.max_liquidity_offset_msat, &mut self.min_liquidity_offset_msat)
655                 };
656
657                 DirectedChannelLiquidity {
658                         min_liquidity_offset_msat,
659                         max_liquidity_offset_msat,
660                         capacity_msat,
661                         last_updated: &mut self.last_updated,
662                         now: T::now(),
663                         half_life,
664                 }
665         }
666 }
667
668 impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiquidity<L, T, U> {
669         /// Returns a penalty for routing the given HTLC `amount_msat` through the channel in this
670         /// direction.
671         fn penalty_msat(&self, amount_msat: u64, liquidity_penalty_multiplier_msat: u64) -> u64 {
672                 let max_penalty_msat = liquidity_penalty_multiplier_msat.saturating_mul(2);
673                 let max_liquidity_msat = self.max_liquidity_msat();
674                 let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
675                 if amount_msat <= min_liquidity_msat {
676                         0
677                 } else if amount_msat >= max_liquidity_msat {
678                         if amount_msat > max_liquidity_msat {
679                                 u64::max_value()
680                         } else if max_liquidity_msat != self.capacity_msat {
681                                 // Avoid using the failed channel on retry.
682                                 u64::max_value()
683                         } else {
684                                 max_penalty_msat
685                         }
686                 } else {
687                         let numerator = (max_liquidity_msat - amount_msat).saturating_add(1);
688                         let denominator = (max_liquidity_msat - min_liquidity_msat).saturating_add(1);
689                         let penalty_msat = approx::negative_log10_times_1024(numerator, denominator)
690                                 .saturating_mul(liquidity_penalty_multiplier_msat) / 1024;
691                         // Upper bound the penalty to ensure some channel is selected.
692                         penalty_msat.min(max_penalty_msat)
693                 }
694         }
695
696         /// Returns the lower bound of the channel liquidity balance in this direction.
697         fn min_liquidity_msat(&self) -> u64 {
698                 self.decayed_offset_msat(*self.min_liquidity_offset_msat)
699         }
700
701         /// Returns the upper bound of the channel liquidity balance in this direction.
702         fn max_liquidity_msat(&self) -> u64 {
703                 self.capacity_msat
704                         .checked_sub(self.decayed_offset_msat(*self.max_liquidity_offset_msat))
705                         .unwrap_or(0)
706         }
707
708         fn decayed_offset_msat(&self, offset_msat: u64) -> u64 {
709                 self.now.duration_since(*self.last_updated).as_secs()
710                         .checked_div(self.half_life.as_secs())
711                         .and_then(|decays| offset_msat.checked_shr(decays as u32))
712                         .unwrap_or(0)
713         }
714 }
715
716 impl<L: DerefMut<Target = u64>, T: Time, U: DerefMut<Target = T>> DirectedChannelLiquidity<L, T, U> {
717         /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
718         fn failed_at_channel(&mut self, amount_msat: u64) {
719                 if amount_msat < self.max_liquidity_msat() {
720                         self.set_max_liquidity_msat(amount_msat);
721                 }
722         }
723
724         /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
725         fn failed_downstream(&mut self, amount_msat: u64) {
726                 if amount_msat > self.min_liquidity_msat() {
727                         self.set_min_liquidity_msat(amount_msat);
728                 }
729         }
730
731         /// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
732         fn successful(&mut self, amount_msat: u64) {
733                 let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
734                 self.set_max_liquidity_msat(max_liquidity_msat);
735         }
736
737         /// Adjusts the lower bound of the channel liquidity balance in this direction.
738         fn set_min_liquidity_msat(&mut self, amount_msat: u64) {
739                 *self.min_liquidity_offset_msat = amount_msat;
740                 *self.max_liquidity_offset_msat = if amount_msat > self.max_liquidity_msat() {
741                         0
742                 } else {
743                         self.decayed_offset_msat(*self.max_liquidity_offset_msat)
744                 };
745                 *self.last_updated = self.now;
746         }
747
748         /// Adjusts the upper bound of the channel liquidity balance in this direction.
749         fn set_max_liquidity_msat(&mut self, amount_msat: u64) {
750                 *self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
751                 *self.min_liquidity_offset_msat = if amount_msat < self.min_liquidity_msat() {
752                         0
753                 } else {
754                         self.decayed_offset_msat(*self.min_liquidity_offset_msat)
755                 };
756                 *self.last_updated = self.now;
757         }
758 }
759
760 impl<G: Deref<Target = NetworkGraph>, T: Time> Score for ProbabilisticScorerUsingTime<G, T> {
761         fn channel_penalty_msat(
762                 &self, short_channel_id: u64, amount_msat: u64, capacity_msat: u64, source: &NodeId,
763                 target: &NodeId
764         ) -> u64 {
765                 let liquidity_penalty_multiplier_msat = self.params.liquidity_penalty_multiplier_msat;
766                 let liquidity_offset_half_life = self.params.liquidity_offset_half_life;
767                 self.channel_liquidities
768                         .get(&short_channel_id)
769                         .unwrap_or(&ChannelLiquidity::new())
770                         .as_directed(source, target, capacity_msat, liquidity_offset_half_life)
771                         .penalty_msat(amount_msat, liquidity_penalty_multiplier_msat)
772                         .saturating_add(self.params.base_penalty_msat)
773         }
774
775         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
776                 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
777                 let liquidity_offset_half_life = self.params.liquidity_offset_half_life;
778                 let network_graph = self.network_graph.read_only();
779                 for hop in path {
780                         let target = NodeId::from_pubkey(&hop.pubkey);
781                         let channel_directed_from_source = network_graph.channels()
782                                 .get(&hop.short_channel_id)
783                                 .and_then(|channel| channel.as_directed_to(&target));
784
785                         // Only score announced channels.
786                         if let Some((channel, source)) = channel_directed_from_source {
787                                 let capacity_msat = channel.effective_capacity().as_msat();
788                                 if hop.short_channel_id == short_channel_id {
789                                         self.channel_liquidities
790                                                 .entry(hop.short_channel_id)
791                                                 .or_insert_with(ChannelLiquidity::new)
792                                                 .as_directed_mut(source, &target, capacity_msat, liquidity_offset_half_life)
793                                                 .failed_at_channel(amount_msat);
794                                         break;
795                                 }
796
797                                 self.channel_liquidities
798                                         .entry(hop.short_channel_id)
799                                         .or_insert_with(ChannelLiquidity::new)
800                                         .as_directed_mut(source, &target, capacity_msat, liquidity_offset_half_life)
801                                         .failed_downstream(amount_msat);
802                         }
803                 }
804         }
805
806         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
807                 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
808                 let liquidity_offset_half_life = self.params.liquidity_offset_half_life;
809                 let network_graph = self.network_graph.read_only();
810                 for hop in path {
811                         let target = NodeId::from_pubkey(&hop.pubkey);
812                         let channel_directed_from_source = network_graph.channels()
813                                 .get(&hop.short_channel_id)
814                                 .and_then(|channel| channel.as_directed_to(&target));
815
816                         // Only score announced channels.
817                         if let Some((channel, source)) = channel_directed_from_source {
818                                 let capacity_msat = channel.effective_capacity().as_msat();
819                                 self.channel_liquidities
820                                         .entry(hop.short_channel_id)
821                                         .or_insert_with(ChannelLiquidity::new)
822                                         .as_directed_mut(source, &target, capacity_msat, liquidity_offset_half_life)
823                                         .successful(amount_msat);
824                         }
825                 }
826         }
827 }
828
829 mod approx {
830         const BITS: u32 = 64;
831         const HIGHEST_BIT: u32 = BITS - 1;
832         const LOWER_BITS: u32 = 4;
833         const LOWER_BITS_BOUND: u64 = 1 << LOWER_BITS;
834         const LOWER_BITMASK: u64 = (1 << LOWER_BITS) - 1;
835
836         /// Look-up table for `log10(x) * 1024` where row `i` is used for each `x` having `i` as the
837         /// most significant bit. The next 4 bits of `x`, if applicable, are used for the second index.
838         const LOG10_TIMES_1024: [[u16; LOWER_BITS_BOUND as usize]; BITS as usize] = [
839                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
840                 [308, 308, 308, 308, 308, 308, 308, 308, 489, 489, 489, 489, 489, 489, 489, 489],
841                 [617, 617, 617, 617, 716, 716, 716, 716, 797, 797, 797, 797, 865, 865, 865, 865],
842                 [925, 925, 977, 977, 1024, 1024, 1066, 1066, 1105, 1105, 1141, 1141, 1174, 1174, 1204, 1204],
843                 [1233, 1260, 1285, 1309, 1332, 1354, 1375, 1394, 1413, 1431, 1449, 1466, 1482, 1497, 1513, 1527],
844                 [1541, 1568, 1594, 1618, 1641, 1662, 1683, 1703, 1722, 1740, 1757, 1774, 1790, 1806, 1821, 1835],
845                 [1850, 1876, 1902, 1926, 1949, 1970, 1991, 2011, 2030, 2048, 2065, 2082, 2098, 2114, 2129, 2144],
846                 [2158, 2185, 2210, 2234, 2257, 2279, 2299, 2319, 2338, 2356, 2374, 2390, 2407, 2422, 2437, 2452],
847                 [2466, 2493, 2518, 2542, 2565, 2587, 2608, 2627, 2646, 2665, 2682, 2699, 2715, 2731, 2746, 2760],
848                 [2774, 2801, 2827, 2851, 2874, 2895, 2916, 2936, 2955, 2973, 2990, 3007, 3023, 3039, 3054, 3068],
849                 [3083, 3110, 3135, 3159, 3182, 3203, 3224, 3244, 3263, 3281, 3298, 3315, 3331, 3347, 3362, 3377],
850                 [3391, 3418, 3443, 3467, 3490, 3512, 3532, 3552, 3571, 3589, 3607, 3623, 3640, 3655, 3670, 3685],
851                 [3699, 3726, 3751, 3775, 3798, 3820, 3841, 3860, 3879, 3898, 3915, 3932, 3948, 3964, 3979, 3993],
852                 [4007, 4034, 4060, 4084, 4107, 4128, 4149, 4169, 4188, 4206, 4223, 4240, 4256, 4272, 4287, 4301],
853                 [4316, 4343, 4368, 4392, 4415, 4436, 4457, 4477, 4496, 4514, 4531, 4548, 4564, 4580, 4595, 4610],
854                 [4624, 4651, 4676, 4700, 4723, 4745, 4765, 4785, 4804, 4822, 4840, 4857, 4873, 4888, 4903, 4918],
855                 [4932, 4959, 4984, 5009, 5031, 5053, 5074, 5093, 5112, 5131, 5148, 5165, 5181, 5197, 5212, 5226],
856                 [5240, 5267, 5293, 5317, 5340, 5361, 5382, 5402, 5421, 5439, 5456, 5473, 5489, 5505, 5520, 5534],
857                 [5549, 5576, 5601, 5625, 5648, 5670, 5690, 5710, 5729, 5747, 5764, 5781, 5797, 5813, 5828, 5843],
858                 [5857, 5884, 5909, 5933, 5956, 5978, 5998, 6018, 6037, 6055, 6073, 6090, 6106, 6121, 6136, 6151],
859                 [6165, 6192, 6217, 6242, 6264, 6286, 6307, 6326, 6345, 6364, 6381, 6398, 6414, 6430, 6445, 6459],
860                 [6473, 6500, 6526, 6550, 6573, 6594, 6615, 6635, 6654, 6672, 6689, 6706, 6722, 6738, 6753, 6767],
861                 [6782, 6809, 6834, 6858, 6881, 6903, 6923, 6943, 6962, 6980, 6998, 7014, 7030, 7046, 7061, 7076],
862                 [7090, 7117, 7142, 7166, 7189, 7211, 7231, 7251, 7270, 7288, 7306, 7323, 7339, 7354, 7369, 7384],
863                 [7398, 7425, 7450, 7475, 7497, 7519, 7540, 7560, 7578, 7597, 7614, 7631, 7647, 7663, 7678, 7692],
864                 [7706, 7733, 7759, 7783, 7806, 7827, 7848, 7868, 7887, 7905, 7922, 7939, 7955, 7971, 7986, 8001],
865                 [8015, 8042, 8067, 8091, 8114, 8136, 8156, 8176, 8195, 8213, 8231, 8247, 8263, 8279, 8294, 8309],
866                 [8323, 8350, 8375, 8399, 8422, 8444, 8464, 8484, 8503, 8521, 8539, 8556, 8572, 8587, 8602, 8617],
867                 [8631, 8658, 8684, 8708, 8730, 8752, 8773, 8793, 8811, 8830, 8847, 8864, 8880, 8896, 8911, 8925],
868                 [8939, 8966, 8992, 9016, 9039, 9060, 9081, 9101, 9120, 9138, 9155, 9172, 9188, 9204, 9219, 9234],
869                 [9248, 9275, 9300, 9324, 9347, 9369, 9389, 9409, 9428, 9446, 9464, 9480, 9497, 9512, 9527, 9542],
870                 [9556, 9583, 9608, 9632, 9655, 9677, 9698, 9717, 9736, 9754, 9772, 9789, 9805, 9820, 9835, 9850],
871                 [9864, 9891, 9917, 9941, 9963, 9985, 10006, 10026, 10044, 10063, 10080, 10097, 10113, 10129, 10144, 10158],
872                 [10172, 10199, 10225, 10249, 10272, 10293, 10314, 10334, 10353, 10371, 10388, 10405, 10421, 10437, 10452, 10467],
873                 [10481, 10508, 10533, 10557, 10580, 10602, 10622, 10642, 10661, 10679, 10697, 10713, 10730, 10745, 10760, 10775],
874                 [10789, 10816, 10841, 10865, 10888, 10910, 10931, 10950, 10969, 10987, 11005, 11022, 11038, 11053, 11068, 11083],
875                 [11097, 11124, 11150, 11174, 11196, 11218, 11239, 11259, 11277, 11296, 11313, 11330, 11346, 11362, 11377, 11391],
876                 [11405, 11432, 11458, 11482, 11505, 11526, 11547, 11567, 11586, 11604, 11621, 11638, 11654, 11670, 11685, 11700],
877                 [11714, 11741, 11766, 11790, 11813, 11835, 11855, 11875, 11894, 11912, 11930, 11946, 11963, 11978, 11993, 12008],
878                 [12022, 12049, 12074, 12098, 12121, 12143, 12164, 12183, 12202, 12220, 12238, 12255, 12271, 12286, 12301, 12316],
879                 [12330, 12357, 12383, 12407, 12429, 12451, 12472, 12492, 12511, 12529, 12546, 12563, 12579, 12595, 12610, 12624],
880                 [12638, 12665, 12691, 12715, 12738, 12759, 12780, 12800, 12819, 12837, 12854, 12871, 12887, 12903, 12918, 12933],
881                 [12947, 12974, 12999, 13023, 13046, 13068, 13088, 13108, 13127, 13145, 13163, 13179, 13196, 13211, 13226, 13241],
882                 [13255, 13282, 13307, 13331, 13354, 13376, 13397, 13416, 13435, 13453, 13471, 13488, 13504, 13519, 13535, 13549],
883                 [13563, 13590, 13616, 13640, 13662, 13684, 13705, 13725, 13744, 13762, 13779, 13796, 13812, 13828, 13843, 13857],
884                 [13871, 13898, 13924, 13948, 13971, 13992, 14013, 14033, 14052, 14070, 14087, 14104, 14120, 14136, 14151, 14166],
885                 [14180, 14207, 14232, 14256, 14279, 14301, 14321, 14341, 14360, 14378, 14396, 14412, 14429, 14444, 14459, 14474],
886                 [14488, 14515, 14540, 14564, 14587, 14609, 14630, 14649, 14668, 14686, 14704, 14721, 14737, 14752, 14768, 14782],
887                 [14796, 14823, 14849, 14873, 14895, 14917, 14938, 14958, 14977, 14995, 15012, 15029, 15045, 15061, 15076, 15090],
888                 [15104, 15131, 15157, 15181, 15204, 15225, 15246, 15266, 15285, 15303, 15320, 15337, 15353, 15369, 15384, 15399],
889                 [15413, 15440, 15465, 15489, 15512, 15534, 15554, 15574, 15593, 15611, 15629, 15645, 15662, 15677, 15692, 15707],
890                 [15721, 15748, 15773, 15797, 15820, 15842, 15863, 15882, 15901, 15919, 15937, 15954, 15970, 15985, 16001, 16015],
891                 [16029, 16056, 16082, 16106, 16128, 16150, 16171, 16191, 16210, 16228, 16245, 16262, 16278, 16294, 16309, 16323],
892                 [16337, 16364, 16390, 16414, 16437, 16458, 16479, 16499, 16518, 16536, 16553, 16570, 16586, 16602, 16617, 16632],
893                 [16646, 16673, 16698, 16722, 16745, 16767, 16787, 16807, 16826, 16844, 16862, 16878, 16895, 16910, 16925, 16940],
894                 [16954, 16981, 17006, 17030, 17053, 17075, 17096, 17115, 17134, 17152, 17170, 17187, 17203, 17218, 17234, 17248],
895                 [17262, 17289, 17315, 17339, 17361, 17383, 17404, 17424, 17443, 17461, 17478, 17495, 17511, 17527, 17542, 17556],
896                 [17571, 17597, 17623, 17647, 17670, 17691, 17712, 17732, 17751, 17769, 17786, 17803, 17819, 17835, 17850, 17865],
897                 [17879, 17906, 17931, 17955, 17978, 18000, 18020, 18040, 18059, 18077, 18095, 18111, 18128, 18143, 18158, 18173],
898                 [18187, 18214, 18239, 18263, 18286, 18308, 18329, 18348, 18367, 18385, 18403, 18420, 18436, 18452, 18467, 18481],
899                 [18495, 18522, 18548, 18572, 18595, 18616, 18637, 18657, 18676, 18694, 18711, 18728, 18744, 18760, 18775, 18789],
900                 [18804, 18830, 18856, 18880, 18903, 18924, 18945, 18965, 18984, 19002, 19019, 19036, 19052, 19068, 19083, 19098],
901                 [19112, 19139, 19164, 19188, 19211, 19233, 19253, 19273, 19292, 19310, 19328, 19344, 19361, 19376, 19391, 19406],
902                 [19420, 19447, 19472, 19496, 19519, 19541, 19562, 19581, 19600, 19619, 19636, 19653, 19669, 19685, 19700, 19714],
903         ];
904
905         /// Approximate `log10(numerator / denominator) * 1024` using a look-up table.
906         #[inline]
907         pub fn negative_log10_times_1024(numerator: u64, denominator: u64) -> u64 {
908                 // Multiply the -1 through to avoid needing to use signed numbers.
909                 (log10_times_1024(denominator) - log10_times_1024(numerator)) as u64
910         }
911
912         #[inline]
913         fn log10_times_1024(x: u64) -> u16 {
914                 debug_assert_ne!(x, 0);
915                 let most_significant_bit = HIGHEST_BIT - x.leading_zeros();
916                 let lower_bits = (x >> most_significant_bit.saturating_sub(LOWER_BITS)) & LOWER_BITMASK;
917                 LOG10_TIMES_1024[most_significant_bit as usize][lower_bits as usize]
918         }
919
920         #[cfg(test)]
921         mod tests {
922                 use super::*;
923
924                 #[test]
925                 fn prints_negative_log10_times_1024_lookup_table() {
926                         for msb in 0..BITS {
927                                 for i in 0..LOWER_BITS_BOUND {
928                                         let x = ((LOWER_BITS_BOUND + i) << (HIGHEST_BIT - LOWER_BITS)) >> (HIGHEST_BIT - msb);
929                                         let log10_times_1024 = ((x as f64).log10() * 1024.0).round() as u16;
930                                         assert_eq!(log10_times_1024, LOG10_TIMES_1024[msb as usize][i as usize]);
931
932                                         if i % LOWER_BITS_BOUND == 0 {
933                                                 print!("\t\t[{}, ", log10_times_1024);
934                                         } else if i % LOWER_BITS_BOUND == LOWER_BITS_BOUND - 1 {
935                                                 println!("{}],", log10_times_1024);
936                                         } else {
937                                                 print!("{}, ", log10_times_1024);
938                                         }
939                                 }
940                         }
941                 }
942         }
943 }
944
945 impl<G: Deref<Target = NetworkGraph>, T: Time> Writeable for ProbabilisticScorerUsingTime<G, T> {
946         #[inline]
947         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
948                 write_tlv_fields!(w, {
949                         (0, self.channel_liquidities, required)
950                 });
951                 Ok(())
952         }
953 }
954
955 impl<G: Deref<Target = NetworkGraph>, T: Time>
956 ReadableArgs<(ProbabilisticScoringParameters, G)> for ProbabilisticScorerUsingTime<G, T> {
957         #[inline]
958         fn read<R: Read>(
959                 r: &mut R, args: (ProbabilisticScoringParameters, G)
960         ) -> Result<Self, DecodeError> {
961                 let (params, network_graph) = args;
962                 let mut channel_liquidities = HashMap::new();
963                 read_tlv_fields!(r, {
964                         (0, channel_liquidities, required)
965                 });
966                 Ok(Self {
967                         params,
968                         network_graph,
969                         channel_liquidities,
970                 })
971         }
972 }
973
974 impl<T: Time> Writeable for ChannelLiquidity<T> {
975         #[inline]
976         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
977                 let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
978                 write_tlv_fields!(w, {
979                         (0, self.min_liquidity_offset_msat, required),
980                         (2, self.max_liquidity_offset_msat, required),
981                         (4, duration_since_epoch, required),
982                 });
983                 Ok(())
984         }
985 }
986
987 impl<T: Time> Readable for ChannelLiquidity<T> {
988         #[inline]
989         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
990                 let mut min_liquidity_offset_msat = 0;
991                 let mut max_liquidity_offset_msat = 0;
992                 let mut duration_since_epoch = Duration::from_secs(0);
993                 read_tlv_fields!(r, {
994                         (0, min_liquidity_offset_msat, required),
995                         (2, max_liquidity_offset_msat, required),
996                         (4, duration_since_epoch, required),
997                 });
998                 Ok(Self {
999                         min_liquidity_offset_msat,
1000                         max_liquidity_offset_msat,
1001                         last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
1002                 })
1003         }
1004 }
1005
1006 pub(crate) mod time {
1007         use core::ops::Sub;
1008         use core::time::Duration;
1009         /// A measurement of time.
1010         pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
1011                 /// Returns an instance corresponding to the current moment.
1012                 fn now() -> Self;
1013
1014                 /// Returns the amount of time elapsed since `self` was created.
1015                 fn elapsed(&self) -> Duration;
1016
1017                 /// Returns the amount of time passed between `earlier` and `self`.
1018                 fn duration_since(&self, earlier: Self) -> Duration;
1019
1020                 /// Returns the amount of time passed since the beginning of [`Time`].
1021                 ///
1022                 /// Used during (de-)serialization.
1023                 fn duration_since_epoch() -> Duration;
1024         }
1025
1026         /// A state in which time has no meaning.
1027         #[derive(Clone, Copy, Debug, PartialEq, Eq)]
1028         pub struct Eternity;
1029
1030         #[cfg(not(feature = "no-std"))]
1031         impl Time for std::time::Instant {
1032                 fn now() -> Self {
1033                         std::time::Instant::now()
1034                 }
1035
1036                 fn duration_since(&self, earlier: Self) -> Duration {
1037                         self.duration_since(earlier)
1038                 }
1039
1040                 fn duration_since_epoch() -> Duration {
1041                         use std::time::SystemTime;
1042                         SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
1043                 }
1044
1045                 fn elapsed(&self) -> Duration {
1046                         std::time::Instant::elapsed(self)
1047                 }
1048         }
1049
1050         impl Time for Eternity {
1051                 fn now() -> Self {
1052                         Self
1053                 }
1054
1055                 fn duration_since(&self, _earlier: Self) -> Duration {
1056                         Duration::from_secs(0)
1057                 }
1058
1059                 fn duration_since_epoch() -> Duration {
1060                         Duration::from_secs(0)
1061                 }
1062
1063                 fn elapsed(&self) -> Duration {
1064                         Duration::from_secs(0)
1065                 }
1066         }
1067
1068         impl Sub<Duration> for Eternity {
1069                 type Output = Self;
1070
1071                 fn sub(self, _other: Duration) -> Self {
1072                         self
1073                 }
1074         }
1075 }
1076
1077 pub(crate) use self::time::Time;
1078
1079 #[cfg(test)]
1080 mod tests {
1081         use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime, Time};
1082         use super::time::Eternity;
1083
1084         use ln::features::{ChannelFeatures, NodeFeatures};
1085         use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
1086         use routing::scoring::Score;
1087         use routing::network_graph::{NetworkGraph, NodeId};
1088         use routing::router::RouteHop;
1089         use util::ser::{Readable, ReadableArgs, Writeable};
1090
1091         use bitcoin::blockdata::constants::genesis_block;
1092         use bitcoin::hashes::Hash;
1093         use bitcoin::hashes::sha256d::Hash as Sha256dHash;
1094         use bitcoin::network::constants::Network;
1095         use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
1096         use core::cell::Cell;
1097         use core::ops::Sub;
1098         use core::time::Duration;
1099         use io;
1100
1101         // `Time` tests
1102
1103         /// Time that can be advanced manually in tests.
1104         #[derive(Clone, Copy, Debug, PartialEq, Eq)]
1105         struct SinceEpoch(Duration);
1106
1107         impl SinceEpoch {
1108                 thread_local! {
1109                         static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
1110                 }
1111
1112                 fn advance(duration: Duration) {
1113                         Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
1114                 }
1115         }
1116
1117         impl Time for SinceEpoch {
1118                 fn now() -> Self {
1119                         Self(Self::duration_since_epoch())
1120                 }
1121
1122                 fn duration_since(&self, earlier: Self) -> Duration {
1123                         self.0 - earlier.0
1124                 }
1125
1126                 fn duration_since_epoch() -> Duration {
1127                         Self::ELAPSED.with(|elapsed| elapsed.get())
1128                 }
1129
1130                 fn elapsed(&self) -> Duration {
1131                         Self::duration_since_epoch() - self.0
1132                 }
1133         }
1134
1135         impl Sub<Duration> for SinceEpoch {
1136                 type Output = Self;
1137
1138                 fn sub(self, other: Duration) -> Self {
1139                         Self(self.0 - other)
1140                 }
1141         }
1142
1143         #[test]
1144         fn time_passes_when_advanced() {
1145                 let now = SinceEpoch::now();
1146                 assert_eq!(now.elapsed(), Duration::from_secs(0));
1147
1148                 SinceEpoch::advance(Duration::from_secs(1));
1149                 SinceEpoch::advance(Duration::from_secs(1));
1150
1151                 let elapsed = now.elapsed();
1152                 let later = SinceEpoch::now();
1153
1154                 assert_eq!(elapsed, Duration::from_secs(2));
1155                 assert_eq!(later - elapsed, now);
1156         }
1157
1158         #[test]
1159         fn time_never_passes_in_an_eternity() {
1160                 let now = Eternity::now();
1161                 let elapsed = now.elapsed();
1162                 let later = Eternity::now();
1163
1164                 assert_eq!(now.elapsed(), Duration::from_secs(0));
1165                 assert_eq!(later - elapsed, now);
1166         }
1167
1168         // `Scorer` tests
1169
1170         /// A scorer for testing with time that can be manually advanced.
1171         type Scorer = ScorerUsingTime::<SinceEpoch>;
1172
1173         fn source_privkey() -> SecretKey {
1174                 SecretKey::from_slice(&[42; 32]).unwrap()
1175         }
1176
1177         fn target_privkey() -> SecretKey {
1178                 SecretKey::from_slice(&[43; 32]).unwrap()
1179         }
1180
1181         fn source_pubkey() -> PublicKey {
1182                 let secp_ctx = Secp256k1::new();
1183                 PublicKey::from_secret_key(&secp_ctx, &source_privkey())
1184         }
1185
1186         fn target_pubkey() -> PublicKey {
1187                 let secp_ctx = Secp256k1::new();
1188                 PublicKey::from_secret_key(&secp_ctx, &target_privkey())
1189         }
1190
1191         fn source_node_id() -> NodeId {
1192                 NodeId::from_pubkey(&source_pubkey())
1193         }
1194
1195         fn target_node_id() -> NodeId {
1196                 NodeId::from_pubkey(&target_pubkey())
1197         }
1198
1199         #[test]
1200         fn penalizes_without_channel_failures() {
1201                 let scorer = Scorer::new(ScoringParameters {
1202                         base_penalty_msat: 1_000,
1203                         failure_penalty_msat: 512,
1204                         failure_penalty_half_life: Duration::from_secs(1),
1205                         overuse_penalty_start_1024th: 1024,
1206                         overuse_penalty_msat_per_1024th: 0,
1207                 });
1208                 let source = source_node_id();
1209                 let target = target_node_id();
1210                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1211
1212                 SinceEpoch::advance(Duration::from_secs(1));
1213                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1214         }
1215
1216         #[test]
1217         fn accumulates_channel_failure_penalties() {
1218                 let mut scorer = Scorer::new(ScoringParameters {
1219                         base_penalty_msat: 1_000,
1220                         failure_penalty_msat: 64,
1221                         failure_penalty_half_life: Duration::from_secs(10),
1222                         overuse_penalty_start_1024th: 1024,
1223                         overuse_penalty_msat_per_1024th: 0,
1224                 });
1225                 let source = source_node_id();
1226                 let target = target_node_id();
1227                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1228
1229                 scorer.payment_path_failed(&[], 42);
1230                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1231
1232                 scorer.payment_path_failed(&[], 42);
1233                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1234
1235                 scorer.payment_path_failed(&[], 42);
1236                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_192);
1237         }
1238
1239         #[test]
1240         fn decays_channel_failure_penalties_over_time() {
1241                 let mut scorer = Scorer::new(ScoringParameters {
1242                         base_penalty_msat: 1_000,
1243                         failure_penalty_msat: 512,
1244                         failure_penalty_half_life: Duration::from_secs(10),
1245                         overuse_penalty_start_1024th: 1024,
1246                         overuse_penalty_msat_per_1024th: 0,
1247                 });
1248                 let source = source_node_id();
1249                 let target = target_node_id();
1250                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1251
1252                 scorer.payment_path_failed(&[], 42);
1253                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1254
1255                 SinceEpoch::advance(Duration::from_secs(9));
1256                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1257
1258                 SinceEpoch::advance(Duration::from_secs(1));
1259                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1260
1261                 SinceEpoch::advance(Duration::from_secs(10 * 8));
1262                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_001);
1263
1264                 SinceEpoch::advance(Duration::from_secs(10));
1265                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1266
1267                 SinceEpoch::advance(Duration::from_secs(10));
1268                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1269         }
1270
1271         #[test]
1272         fn decays_channel_failure_penalties_without_shift_overflow() {
1273                 let mut scorer = Scorer::new(ScoringParameters {
1274                         base_penalty_msat: 1_000,
1275                         failure_penalty_msat: 512,
1276                         failure_penalty_half_life: Duration::from_secs(10),
1277                         overuse_penalty_start_1024th: 1024,
1278                         overuse_penalty_msat_per_1024th: 0,
1279                 });
1280                 let source = source_node_id();
1281                 let target = target_node_id();
1282                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1283
1284                 scorer.payment_path_failed(&[], 42);
1285                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1286
1287                 // An unchecked right shift 64 bits or more in ChannelFailure::decayed_penalty_msat would
1288                 // cause an overflow.
1289                 SinceEpoch::advance(Duration::from_secs(10 * 64));
1290                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1291
1292                 SinceEpoch::advance(Duration::from_secs(10));
1293                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1294         }
1295
1296         #[test]
1297         fn accumulates_channel_failure_penalties_after_decay() {
1298                 let mut scorer = Scorer::new(ScoringParameters {
1299                         base_penalty_msat: 1_000,
1300                         failure_penalty_msat: 512,
1301                         failure_penalty_half_life: Duration::from_secs(10),
1302                         overuse_penalty_start_1024th: 1024,
1303                         overuse_penalty_msat_per_1024th: 0,
1304                 });
1305                 let source = source_node_id();
1306                 let target = target_node_id();
1307                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1308
1309                 scorer.payment_path_failed(&[], 42);
1310                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1311
1312                 SinceEpoch::advance(Duration::from_secs(10));
1313                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1314
1315                 scorer.payment_path_failed(&[], 42);
1316                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_768);
1317
1318                 SinceEpoch::advance(Duration::from_secs(10));
1319                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_384);
1320         }
1321
1322         #[test]
1323         fn reduces_channel_failure_penalties_after_success() {
1324                 let mut scorer = Scorer::new(ScoringParameters {
1325                         base_penalty_msat: 1_000,
1326                         failure_penalty_msat: 512,
1327                         failure_penalty_half_life: Duration::from_secs(10),
1328                         overuse_penalty_start_1024th: 1024,
1329                         overuse_penalty_msat_per_1024th: 0,
1330                 });
1331                 let source = source_node_id();
1332                 let target = target_node_id();
1333                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1334
1335                 scorer.payment_path_failed(&[], 42);
1336                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1337
1338                 SinceEpoch::advance(Duration::from_secs(10));
1339                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1340
1341                 let hop = RouteHop {
1342                         pubkey: PublicKey::from_slice(target.as_slice()).unwrap(),
1343                         node_features: NodeFeatures::known(),
1344                         short_channel_id: 42,
1345                         channel_features: ChannelFeatures::known(),
1346                         fee_msat: 1,
1347                         cltv_expiry_delta: 18,
1348                 };
1349                 scorer.payment_path_successful(&[&hop]);
1350                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1351
1352                 SinceEpoch::advance(Duration::from_secs(10));
1353                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1354         }
1355
1356         #[test]
1357         fn restores_persisted_channel_failure_penalties() {
1358                 let mut scorer = Scorer::new(ScoringParameters {
1359                         base_penalty_msat: 1_000,
1360                         failure_penalty_msat: 512,
1361                         failure_penalty_half_life: Duration::from_secs(10),
1362                         overuse_penalty_start_1024th: 1024,
1363                         overuse_penalty_msat_per_1024th: 0,
1364                 });
1365                 let source = source_node_id();
1366                 let target = target_node_id();
1367
1368                 scorer.payment_path_failed(&[], 42);
1369                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1370
1371                 SinceEpoch::advance(Duration::from_secs(10));
1372                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1373
1374                 scorer.payment_path_failed(&[], 43);
1375                 assert_eq!(scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1376
1377                 let mut serialized_scorer = Vec::new();
1378                 scorer.write(&mut serialized_scorer).unwrap();
1379
1380                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1381                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1382                 assert_eq!(deserialized_scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1383         }
1384
1385         #[test]
1386         fn decays_persisted_channel_failure_penalties() {
1387                 let mut scorer = Scorer::new(ScoringParameters {
1388                         base_penalty_msat: 1_000,
1389                         failure_penalty_msat: 512,
1390                         failure_penalty_half_life: Duration::from_secs(10),
1391                         overuse_penalty_start_1024th: 1024,
1392                         overuse_penalty_msat_per_1024th: 0,
1393                 });
1394                 let source = source_node_id();
1395                 let target = target_node_id();
1396
1397                 scorer.payment_path_failed(&[], 42);
1398                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1399
1400                 let mut serialized_scorer = Vec::new();
1401                 scorer.write(&mut serialized_scorer).unwrap();
1402
1403                 SinceEpoch::advance(Duration::from_secs(10));
1404
1405                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1406                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1407
1408                 SinceEpoch::advance(Duration::from_secs(10));
1409                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1410         }
1411
1412         #[test]
1413         fn charges_per_1024th_penalty() {
1414                 let scorer = Scorer::new(ScoringParameters {
1415                         base_penalty_msat: 0,
1416                         failure_penalty_msat: 0,
1417                         failure_penalty_half_life: Duration::from_secs(0),
1418                         overuse_penalty_start_1024th: 256,
1419                         overuse_penalty_msat_per_1024th: 100,
1420                 });
1421                 let source = source_node_id();
1422                 let target = target_node_id();
1423
1424                 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 1_024_000, &source, &target), 0);
1425                 assert_eq!(scorer.channel_penalty_msat(42, 256_999, 1_024_000, &source, &target), 0);
1426                 assert_eq!(scorer.channel_penalty_msat(42, 257_000, 1_024_000, &source, &target), 100);
1427                 assert_eq!(scorer.channel_penalty_msat(42, 258_000, 1_024_000, &source, &target), 200);
1428                 assert_eq!(scorer.channel_penalty_msat(42, 512_000, 1_024_000, &source, &target), 256 * 100);
1429         }
1430
1431         // `ProbabilisticScorer` tests
1432
1433         /// A probabilistic scorer for testing with time that can be manually advanced.
1434         type ProbabilisticScorer<'a> = ProbabilisticScorerUsingTime::<&'a NetworkGraph, SinceEpoch>;
1435
1436         fn sender_privkey() -> SecretKey {
1437                 SecretKey::from_slice(&[41; 32]).unwrap()
1438         }
1439
1440         fn recipient_privkey() -> SecretKey {
1441                 SecretKey::from_slice(&[45; 32]).unwrap()
1442         }
1443
1444         fn sender_pubkey() -> PublicKey {
1445                 let secp_ctx = Secp256k1::new();
1446                 PublicKey::from_secret_key(&secp_ctx, &sender_privkey())
1447         }
1448
1449         fn recipient_pubkey() -> PublicKey {
1450                 let secp_ctx = Secp256k1::new();
1451                 PublicKey::from_secret_key(&secp_ctx, &recipient_privkey())
1452         }
1453
1454         fn sender_node_id() -> NodeId {
1455                 NodeId::from_pubkey(&sender_pubkey())
1456         }
1457
1458         fn recipient_node_id() -> NodeId {
1459                 NodeId::from_pubkey(&recipient_pubkey())
1460         }
1461
1462         fn network_graph() -> NetworkGraph {
1463                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1464                 let mut network_graph = NetworkGraph::new(genesis_hash);
1465                 add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
1466                 add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
1467
1468                 network_graph
1469         }
1470
1471         fn add_channel(
1472                 network_graph: &mut NetworkGraph, short_channel_id: u64, node_1_key: SecretKey,
1473                 node_2_key: SecretKey
1474         ) {
1475                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1476                 let node_1_secret = &SecretKey::from_slice(&[39; 32]).unwrap();
1477                 let node_2_secret = &SecretKey::from_slice(&[40; 32]).unwrap();
1478                 let secp_ctx = Secp256k1::new();
1479                 let unsigned_announcement = UnsignedChannelAnnouncement {
1480                         features: ChannelFeatures::known(),
1481                         chain_hash: genesis_hash,
1482                         short_channel_id,
1483                         node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_key),
1484                         node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_key),
1485                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_secret),
1486                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_secret),
1487                         excess_data: Vec::new(),
1488                 };
1489                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1490                 let signed_announcement = ChannelAnnouncement {
1491                         node_signature_1: secp_ctx.sign(&msghash, &node_1_key),
1492                         node_signature_2: secp_ctx.sign(&msghash, &node_2_key),
1493                         bitcoin_signature_1: secp_ctx.sign(&msghash, &node_1_secret),
1494                         bitcoin_signature_2: secp_ctx.sign(&msghash, &node_2_secret),
1495                         contents: unsigned_announcement,
1496                 };
1497                 let chain_source: Option<&::util::test_utils::TestChainSource> = None;
1498                 network_graph.update_channel_from_announcement(
1499                         &signed_announcement, &chain_source, &secp_ctx).unwrap();
1500                 update_channel(network_graph, short_channel_id, node_1_key, 0);
1501                 update_channel(network_graph, short_channel_id, node_2_key, 1);
1502         }
1503
1504         fn update_channel(
1505                 network_graph: &mut NetworkGraph, short_channel_id: u64, node_key: SecretKey, flags: u8
1506         ) {
1507                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1508                 let secp_ctx = Secp256k1::new();
1509                 let unsigned_update = UnsignedChannelUpdate {
1510                         chain_hash: genesis_hash,
1511                         short_channel_id,
1512                         timestamp: 100,
1513                         flags,
1514                         cltv_expiry_delta: 18,
1515                         htlc_minimum_msat: 0,
1516                         htlc_maximum_msat: OptionalField::Present(1_000),
1517                         fee_base_msat: 1,
1518                         fee_proportional_millionths: 0,
1519                         excess_data: Vec::new(),
1520                 };
1521                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
1522                 let signed_update = ChannelUpdate {
1523                         signature: secp_ctx.sign(&msghash, &node_key),
1524                         contents: unsigned_update,
1525                 };
1526                 network_graph.update_channel(&signed_update, &secp_ctx).unwrap();
1527         }
1528
1529         fn payment_path_for_amount(amount_msat: u64) -> Vec<RouteHop> {
1530                 vec![
1531                         RouteHop {
1532                                 pubkey: source_pubkey(),
1533                                 node_features: NodeFeatures::known(),
1534                                 short_channel_id: 41,
1535                                 channel_features: ChannelFeatures::known(),
1536                                 fee_msat: 1,
1537                                 cltv_expiry_delta: 18,
1538                         },
1539                         RouteHop {
1540                                 pubkey: target_pubkey(),
1541                                 node_features: NodeFeatures::known(),
1542                                 short_channel_id: 42,
1543                                 channel_features: ChannelFeatures::known(),
1544                                 fee_msat: 2,
1545                                 cltv_expiry_delta: 18,
1546                         },
1547                         RouteHop {
1548                                 pubkey: recipient_pubkey(),
1549                                 node_features: NodeFeatures::known(),
1550                                 short_channel_id: 43,
1551                                 channel_features: ChannelFeatures::known(),
1552                                 fee_msat: amount_msat,
1553                                 cltv_expiry_delta: 18,
1554                         },
1555                 ]
1556         }
1557
1558         #[test]
1559         fn liquidity_bounds_directed_from_lowest_node_id() {
1560                 let last_updated = SinceEpoch::now();
1561                 let network_graph = network_graph();
1562                 let params = ProbabilisticScoringParameters::default();
1563                 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1564                         .with_channel(42,
1565                                 ChannelLiquidity {
1566                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100, last_updated
1567                                 })
1568                         .with_channel(43,
1569                                 ChannelLiquidity {
1570                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100, last_updated
1571                                 });
1572                 let source = source_node_id();
1573                 let target = target_node_id();
1574                 let recipient = recipient_node_id();
1575                 assert!(source > target);
1576                 assert!(target < recipient);
1577
1578                 // Update minimum liquidity.
1579
1580                 let liquidity_offset_half_life = scorer.params.liquidity_offset_half_life;
1581                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1582                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1583                 assert_eq!(liquidity.min_liquidity_msat(), 100);
1584                 assert_eq!(liquidity.max_liquidity_msat(), 300);
1585
1586                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1587                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1588                 assert_eq!(liquidity.min_liquidity_msat(), 700);
1589                 assert_eq!(liquidity.max_liquidity_msat(), 900);
1590
1591                 scorer.channel_liquidities.get_mut(&42).unwrap()
1592                         .as_directed_mut(&source, &target, 1_000, liquidity_offset_half_life)
1593                         .set_min_liquidity_msat(200);
1594
1595                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1596                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1597                 assert_eq!(liquidity.min_liquidity_msat(), 200);
1598                 assert_eq!(liquidity.max_liquidity_msat(), 300);
1599
1600                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1601                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1602                 assert_eq!(liquidity.min_liquidity_msat(), 700);
1603                 assert_eq!(liquidity.max_liquidity_msat(), 800);
1604
1605                 // Update maximum liquidity.
1606
1607                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1608                         .as_directed(&target, &recipient, 1_000, liquidity_offset_half_life);
1609                 assert_eq!(liquidity.min_liquidity_msat(), 700);
1610                 assert_eq!(liquidity.max_liquidity_msat(), 900);
1611
1612                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1613                         .as_directed(&recipient, &target, 1_000, liquidity_offset_half_life);
1614                 assert_eq!(liquidity.min_liquidity_msat(), 100);
1615                 assert_eq!(liquidity.max_liquidity_msat(), 300);
1616
1617                 scorer.channel_liquidities.get_mut(&43).unwrap()
1618                         .as_directed_mut(&target, &recipient, 1_000, liquidity_offset_half_life)
1619                         .set_max_liquidity_msat(200);
1620
1621                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1622                         .as_directed(&target, &recipient, 1_000, liquidity_offset_half_life);
1623                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1624                 assert_eq!(liquidity.max_liquidity_msat(), 200);
1625
1626                 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1627                         .as_directed(&recipient, &target, 1_000, liquidity_offset_half_life);
1628                 assert_eq!(liquidity.min_liquidity_msat(), 800);
1629                 assert_eq!(liquidity.max_liquidity_msat(), 1000);
1630         }
1631
1632         #[test]
1633         fn resets_liquidity_upper_bound_when_crossed_by_lower_bound() {
1634                 let last_updated = SinceEpoch::now();
1635                 let network_graph = network_graph();
1636                 let params = ProbabilisticScoringParameters::default();
1637                 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1638                         .with_channel(42,
1639                                 ChannelLiquidity {
1640                                         min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400, last_updated
1641                                 });
1642                 let source = source_node_id();
1643                 let target = target_node_id();
1644                 assert!(source > target);
1645
1646                 // Check initial bounds.
1647                 let liquidity_offset_half_life = scorer.params.liquidity_offset_half_life;
1648                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1649                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1650                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1651                 assert_eq!(liquidity.max_liquidity_msat(), 800);
1652
1653                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1654                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1655                 assert_eq!(liquidity.min_liquidity_msat(), 200);
1656                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1657
1658                 // Reset from source to target.
1659                 scorer.channel_liquidities.get_mut(&42).unwrap()
1660                         .as_directed_mut(&source, &target, 1_000, liquidity_offset_half_life)
1661                         .set_min_liquidity_msat(900);
1662
1663                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1664                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1665                 assert_eq!(liquidity.min_liquidity_msat(), 900);
1666                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1667
1668                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1669                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1670                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1671                 assert_eq!(liquidity.max_liquidity_msat(), 100);
1672
1673                 // Reset from target to source.
1674                 scorer.channel_liquidities.get_mut(&42).unwrap()
1675                         .as_directed_mut(&target, &source, 1_000, liquidity_offset_half_life)
1676                         .set_min_liquidity_msat(400);
1677
1678                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1679                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1680                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1681                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1682
1683                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1684                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1685                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1686                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1687         }
1688
1689         #[test]
1690         fn resets_liquidity_lower_bound_when_crossed_by_upper_bound() {
1691                 let last_updated = SinceEpoch::now();
1692                 let network_graph = network_graph();
1693                 let params = ProbabilisticScoringParameters::default();
1694                 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1695                         .with_channel(42,
1696                                 ChannelLiquidity {
1697                                         min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400, last_updated
1698                                 });
1699                 let source = source_node_id();
1700                 let target = target_node_id();
1701                 assert!(source > target);
1702
1703                 // Check initial bounds.
1704                 let liquidity_offset_half_life = scorer.params.liquidity_offset_half_life;
1705                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1706                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1707                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1708                 assert_eq!(liquidity.max_liquidity_msat(), 800);
1709
1710                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1711                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1712                 assert_eq!(liquidity.min_liquidity_msat(), 200);
1713                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1714
1715                 // Reset from source to target.
1716                 scorer.channel_liquidities.get_mut(&42).unwrap()
1717                         .as_directed_mut(&source, &target, 1_000, liquidity_offset_half_life)
1718                         .set_max_liquidity_msat(300);
1719
1720                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1721                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1722                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1723                 assert_eq!(liquidity.max_liquidity_msat(), 300);
1724
1725                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1726                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1727                 assert_eq!(liquidity.min_liquidity_msat(), 700);
1728                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1729
1730                 // Reset from target to source.
1731                 scorer.channel_liquidities.get_mut(&42).unwrap()
1732                         .as_directed_mut(&target, &source, 1_000, liquidity_offset_half_life)
1733                         .set_max_liquidity_msat(600);
1734
1735                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1736                         .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1737                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1738                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1739
1740                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1741                         .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1742                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1743                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1744         }
1745
1746         #[test]
1747         fn increased_penalty_nearing_liquidity_upper_bound() {
1748                 let network_graph = network_graph();
1749                 let params = ProbabilisticScoringParameters {
1750                         base_penalty_msat: 0, liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1751                 };
1752                 let scorer = ProbabilisticScorer::new(params, &network_graph);
1753                 let source = source_node_id();
1754                 let target = target_node_id();
1755
1756                 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024_000, &source, &target), 0);
1757                 assert_eq!(scorer.channel_penalty_msat(42, 10_240, 1_024_000, &source, &target), 14);
1758                 assert_eq!(scorer.channel_penalty_msat(42, 102_400, 1_024_000, &source, &target), 43);
1759                 assert_eq!(scorer.channel_penalty_msat(42, 1_024_000, 1_024_000, &source, &target), 2_000);
1760
1761                 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 58);
1762                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 125);
1763                 assert_eq!(scorer.channel_penalty_msat(42, 374, 1_024, &source, &target), 204);
1764                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 301);
1765                 assert_eq!(scorer.channel_penalty_msat(42, 640, 1_024, &source, &target), 426);
1766                 assert_eq!(scorer.channel_penalty_msat(42, 768, 1_024, &source, &target), 602);
1767                 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), 903);
1768         }
1769
1770         #[test]
1771         fn constant_penalty_outside_liquidity_bounds() {
1772                 let last_updated = SinceEpoch::now();
1773                 let network_graph = network_graph();
1774                 let params = ProbabilisticScoringParameters {
1775                         base_penalty_msat: 0, liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1776                 };
1777                 let scorer = ProbabilisticScorer::new(params, &network_graph)
1778                         .with_channel(42,
1779                                 ChannelLiquidity {
1780                                         min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40, last_updated
1781                                 });
1782                 let source = source_node_id();
1783                 let target = target_node_id();
1784
1785                 assert_eq!(scorer.channel_penalty_msat(42, 39, 100, &source, &target), 0);
1786                 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 0);
1787                 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), u64::max_value());
1788                 assert_eq!(scorer.channel_penalty_msat(42, 61, 100, &source, &target), u64::max_value());
1789         }
1790
1791         #[test]
1792         fn does_not_further_penalize_own_channel() {
1793                 let network_graph = network_graph();
1794                 let params = ProbabilisticScoringParameters {
1795                         base_penalty_msat: 0, liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1796                 };
1797                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1798                 let sender = sender_node_id();
1799                 let source = source_node_id();
1800                 let failed_path = payment_path_for_amount(500);
1801                 let successful_path = payment_path_for_amount(200);
1802
1803                 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1804
1805                 scorer.payment_path_failed(&failed_path.iter().collect::<Vec<_>>(), 41);
1806                 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1807
1808                 scorer.payment_path_successful(&successful_path.iter().collect::<Vec<_>>());
1809                 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1810         }
1811
1812         #[test]
1813         fn sets_liquidity_lower_bound_on_downstream_failure() {
1814                 let network_graph = network_graph();
1815                 let params = ProbabilisticScoringParameters {
1816                         base_penalty_msat: 0, liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1817                 };
1818                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1819                 let source = source_node_id();
1820                 let target = target_node_id();
1821                 let path = payment_path_for_amount(500);
1822
1823                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 128);
1824                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1825                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 601);
1826
1827                 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 43);
1828
1829                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 0);
1830                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 0);
1831                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 300);
1832         }
1833
1834         #[test]
1835         fn sets_liquidity_upper_bound_on_failure() {
1836                 let network_graph = network_graph();
1837                 let params = ProbabilisticScoringParameters {
1838                         base_penalty_msat: 0, liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1839                 };
1840                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1841                 let source = source_node_id();
1842                 let target = target_node_id();
1843                 let path = payment_path_for_amount(500);
1844
1845                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 128);
1846                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1847                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 601);
1848
1849                 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 42);
1850
1851                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 300);
1852                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), u64::max_value());
1853                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), u64::max_value());
1854         }
1855
1856         #[test]
1857         fn reduces_liquidity_upper_bound_along_path_on_success() {
1858                 let network_graph = network_graph();
1859                 let params = ProbabilisticScoringParameters {
1860                         base_penalty_msat: 0, liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1861                 };
1862                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1863                 let sender = sender_node_id();
1864                 let source = source_node_id();
1865                 let target = target_node_id();
1866                 let recipient = recipient_node_id();
1867                 let path = payment_path_for_amount(500);
1868
1869                 assert_eq!(scorer.channel_penalty_msat(41, 250, 1_000, &sender, &source), 128);
1870                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 128);
1871                 assert_eq!(scorer.channel_penalty_msat(43, 250, 1_000, &target, &recipient), 128);
1872
1873                 scorer.payment_path_successful(&path.iter().collect::<Vec<_>>());
1874
1875                 assert_eq!(scorer.channel_penalty_msat(41, 250, 1_000, &sender, &source), 128);
1876                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 300);
1877                 assert_eq!(scorer.channel_penalty_msat(43, 250, 1_000, &target, &recipient), 300);
1878         }
1879
1880         #[test]
1881         fn decays_liquidity_bounds_over_time() {
1882                 let network_graph = network_graph();
1883                 let params = ProbabilisticScoringParameters {
1884                         base_penalty_msat: 0,
1885                         liquidity_penalty_multiplier_msat: 1_000,
1886                         liquidity_offset_half_life: Duration::from_secs(10),
1887                 };
1888                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1889                 let source = source_node_id();
1890                 let target = target_node_id();
1891
1892                 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1893                 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1894
1895                 scorer.payment_path_failed(&payment_path_for_amount(768).iter().collect::<Vec<_>>(), 42);
1896                 scorer.payment_path_failed(&payment_path_for_amount(128).iter().collect::<Vec<_>>(), 43);
1897
1898                 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 0);
1899                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 97);
1900                 assert_eq!(scorer.channel_penalty_msat(42, 768, 1_024, &source, &target), 1_409);
1901                 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), u64::max_value());
1902
1903                 SinceEpoch::advance(Duration::from_secs(9));
1904                 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 0);
1905                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 97);
1906                 assert_eq!(scorer.channel_penalty_msat(42, 768, 1_024, &source, &target), 1_409);
1907                 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), u64::max_value());
1908
1909                 SinceEpoch::advance(Duration::from_secs(1));
1910                 assert_eq!(scorer.channel_penalty_msat(42, 64, 1_024, &source, &target), 0);
1911                 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 34);
1912                 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), 1_773);
1913                 assert_eq!(scorer.channel_penalty_msat(42, 960, 1_024, &source, &target), u64::max_value());
1914
1915                 // Fully decay liquidity lower bound.
1916                 SinceEpoch::advance(Duration::from_secs(10 * 7));
1917                 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1918                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1_024, &source, &target), 0);
1919                 assert_eq!(scorer.channel_penalty_msat(42, 1_023, 1_024, &source, &target), 2_000);
1920                 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1921
1922                 // Fully decay liquidity upper bound.
1923                 SinceEpoch::advance(Duration::from_secs(10));
1924                 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1925                 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1926
1927                 SinceEpoch::advance(Duration::from_secs(10));
1928                 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1929                 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1930         }
1931
1932         #[test]
1933         fn decays_liquidity_bounds_without_shift_overflow() {
1934                 let network_graph = network_graph();
1935                 let params = ProbabilisticScoringParameters {
1936                         base_penalty_msat: 0,
1937                         liquidity_penalty_multiplier_msat: 1_000,
1938                         liquidity_offset_half_life: Duration::from_secs(10),
1939                 };
1940                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1941                 let source = source_node_id();
1942                 let target = target_node_id();
1943                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 125);
1944
1945                 scorer.payment_path_failed(&payment_path_for_amount(512).iter().collect::<Vec<_>>(), 42);
1946                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 274);
1947
1948                 // An unchecked right shift 64 bits or more in DirectedChannelLiquidity::decayed_offset_msat
1949                 // would cause an overflow.
1950                 SinceEpoch::advance(Duration::from_secs(10 * 64));
1951                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 125);
1952
1953                 SinceEpoch::advance(Duration::from_secs(10));
1954                 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 125);
1955         }
1956
1957         #[test]
1958         fn restricts_liquidity_bounds_after_decay() {
1959                 let network_graph = network_graph();
1960                 let params = ProbabilisticScoringParameters {
1961                         base_penalty_msat: 0,
1962                         liquidity_penalty_multiplier_msat: 1_000,
1963                         liquidity_offset_half_life: Duration::from_secs(10),
1964                 };
1965                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1966                 let source = source_node_id();
1967                 let target = target_node_id();
1968
1969                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 301);
1970
1971                 // More knowledge gives higher confidence (256, 768), meaning a lower penalty.
1972                 scorer.payment_path_failed(&payment_path_for_amount(768).iter().collect::<Vec<_>>(), 42);
1973                 scorer.payment_path_failed(&payment_path_for_amount(256).iter().collect::<Vec<_>>(), 43);
1974                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 274);
1975
1976                 // Decaying knowledge gives less confidence (128, 896), meaning a higher penalty.
1977                 SinceEpoch::advance(Duration::from_secs(10));
1978                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 301);
1979
1980                 // Reducing the upper bound gives more confidence (128, 832) that the payment amount (512)
1981                 // is closer to the upper bound, meaning a higher penalty.
1982                 scorer.payment_path_successful(&payment_path_for_amount(64).iter().collect::<Vec<_>>());
1983                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 342);
1984
1985                 // Increasing the lower bound gives more confidence (256, 832) that the payment amount (512)
1986                 // is closer to the lower bound, meaning a lower penalty.
1987                 scorer.payment_path_failed(&payment_path_for_amount(256).iter().collect::<Vec<_>>(), 43);
1988                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 255);
1989
1990                 // Further decaying affects the lower bound more than the upper bound (128, 928).
1991                 SinceEpoch::advance(Duration::from_secs(10));
1992                 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 284);
1993         }
1994
1995         #[test]
1996         fn restores_persisted_liquidity_bounds() {
1997                 let network_graph = network_graph();
1998                 let params = ProbabilisticScoringParameters {
1999                         base_penalty_msat: 0,
2000                         liquidity_penalty_multiplier_msat: 1_000,
2001                         liquidity_offset_half_life: Duration::from_secs(10),
2002                 };
2003                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
2004                 let source = source_node_id();
2005                 let target = target_node_id();
2006
2007                 scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
2008                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), u64::max_value());
2009
2010                 SinceEpoch::advance(Duration::from_secs(10));
2011                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 472);
2012
2013                 scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
2014                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
2015
2016                 let mut serialized_scorer = Vec::new();
2017                 scorer.write(&mut serialized_scorer).unwrap();
2018
2019                 let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
2020                 let deserialized_scorer =
2021                         <ProbabilisticScorer>::read(&mut serialized_scorer, (params, &network_graph)).unwrap();
2022                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
2023         }
2024
2025         #[test]
2026         fn decays_persisted_liquidity_bounds() {
2027                 let network_graph = network_graph();
2028                 let params = ProbabilisticScoringParameters {
2029                         base_penalty_msat: 0,
2030                         liquidity_penalty_multiplier_msat: 1_000,
2031                         liquidity_offset_half_life: Duration::from_secs(10),
2032                 };
2033                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
2034                 let source = source_node_id();
2035                 let target = target_node_id();
2036
2037                 scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
2038                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), u64::max_value());
2039
2040                 let mut serialized_scorer = Vec::new();
2041                 scorer.write(&mut serialized_scorer).unwrap();
2042
2043                 SinceEpoch::advance(Duration::from_secs(10));
2044
2045                 let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
2046                 let deserialized_scorer =
2047                         <ProbabilisticScorer>::read(&mut serialized_scorer, (params, &network_graph)).unwrap();
2048                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 472);
2049
2050                 scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
2051                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
2052
2053                 SinceEpoch::advance(Duration::from_secs(10));
2054                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 371);
2055         }
2056
2057         #[test]
2058         fn adds_base_penalty_to_liquidity_penalty() {
2059                 let network_graph = network_graph();
2060                 let source = source_node_id();
2061                 let target = target_node_id();
2062
2063                 let params = ProbabilisticScoringParameters {
2064                         base_penalty_msat: 0, liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
2065                 };
2066                 let scorer = ProbabilisticScorer::new(params, &network_graph);
2067                 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 58);
2068
2069                 let params = ProbabilisticScoringParameters {
2070                         base_penalty_msat: 500, liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
2071                 };
2072                 let scorer = ProbabilisticScorer::new(params, &network_graph);
2073                 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 558);
2074         }
2075
2076         #[test]
2077         fn calculates_log10_without_overflowing_u64_max_value() {
2078                 let network_graph = network_graph();
2079                 let source = source_node_id();
2080                 let target = target_node_id();
2081
2082                 let params = ProbabilisticScoringParameters {
2083                         base_penalty_msat: 0, ..Default::default()
2084                 };
2085                 let scorer = ProbabilisticScorer::new(params, &network_graph);
2086                 assert_eq!(
2087                         scorer.channel_penalty_msat(42, u64::max_value(), u64::max_value(), &source, &target),
2088                         80_000,
2089                 );
2090         }
2091 }