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