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