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