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