Probabilistic channel scoring
[rust-lightning] / lightning / src / routing / scoring.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Utilities for scoring payment channels.
11 //!
12 //! [`ProbabilisticScorer`] may be given to [`find_route`] to score payment channels during path
13 //! finding when a custom [`Score`] implementation is not needed.
14 //!
15 //! # Example
16 //!
17 //! ```
18 //! # extern crate secp256k1;
19 //! #
20 //! # use lightning::routing::network_graph::NetworkGraph;
21 //! # use lightning::routing::router::{RouteParameters, find_route};
22 //! # use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters, Scorer, ScoringParameters};
23 //! # use lightning::util::logger::{Logger, Record};
24 //! # use secp256k1::key::PublicKey;
25 //! #
26 //! # struct FakeLogger {};
27 //! # impl Logger for FakeLogger {
28 //! #     fn log(&self, record: &Record) { unimplemented!() }
29 //! # }
30 //! # fn find_scored_route(payer: PublicKey, route_params: RouteParameters, network_graph: NetworkGraph) {
31 //! # let logger = FakeLogger {};
32 //! #
33 //! // Use the default channel penalties.
34 //! let params = ProbabilisticScoringParameters::default();
35 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
36 //!
37 //! // Or use custom channel penalties.
38 //! let params = ProbabilisticScoringParameters {
39 //!     liquidity_penalty_multiplier_msat: 2 * 1000,
40 //!     ..ProbabilisticScoringParameters::default()
41 //! };
42 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
43 //!
44 //! let route = find_route(&payer, &route_params, &network_graph, None, &logger, &scorer);
45 //! # }
46 //! ```
47 //!
48 //! # Note
49 //!
50 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
51 //! different types and thus is undefined.
52 //!
53 //! [`find_route`]: crate::routing::router::find_route
54
55 use ln::msgs::DecodeError;
56 use routing::network_graph::{NetworkGraph, NodeId};
57 use routing::router::RouteHop;
58 use util::ser::{Readable, ReadableArgs, Writeable, Writer};
59
60 use prelude::*;
61 use core::cell::{RefCell, RefMut};
62 use core::ops::{Deref, DerefMut};
63 use core::time::Duration;
64 use io::{self, Read};
65 use sync::{Mutex, MutexGuard};
66
67 /// We define Score ever-so-slightly differently based on whether we are being built for C bindings
68 /// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
69 /// no problem - you move a `Score` that implements `Writeable` into a `Mutex`, lock it, and now
70 /// you have the original, concrete, `Score` type, which presumably implements `Writeable`.
71 ///
72 /// For C users, once you've moved the `Score` into a `LockableScore` all you have after locking it
73 /// is an opaque trait object with an opaque pointer with no type info. Users could take the unsafe
74 /// approach of blindly casting that opaque pointer to a concrete type and calling `Writeable` from
75 /// there, but other languages downstream of the C bindings (e.g. Java) can't even do that.
76 /// Instead, we really want `Score` and `LockableScore` to implement `Writeable` directly, which we
77 /// do here by defining `Score` differently for `cfg(c_bindings)`.
78 macro_rules! define_score { ($($supertrait: path)*) => {
79 /// An interface used to score payment channels for path finding.
80 ///
81 ///     Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
82 pub trait Score $(: $supertrait)* {
83         /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
84         /// given channel in the direction from `source` to `target`.
85         ///
86         /// The channel's capacity (less any other MPP parts that are also being considered for use in
87         /// the same payment) is given by `capacity_msat`. It may be determined from various sources
88         /// such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
89         /// [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
90         /// Thus, implementations should be overflow-safe.
91         fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &NodeId, target: &NodeId) -> u64;
92
93         /// Handles updating channel penalties after failing to route through a channel.
94         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64);
95
96         /// Handles updating channel penalties after successfully routing along a path.
97         fn payment_path_successful(&mut self, path: &[&RouteHop]);
98 }
99
100 impl<S: Score, T: DerefMut<Target=S> $(+ $supertrait)*> Score for T {
101         fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &NodeId, target: &NodeId) -> u64 {
102                 self.deref().channel_penalty_msat(short_channel_id, send_amt_msat, capacity_msat, source, target)
103         }
104
105         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
106                 self.deref_mut().payment_path_failed(path, short_channel_id)
107         }
108
109         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
110                 self.deref_mut().payment_path_successful(path)
111         }
112 }
113 } }
114
115 #[cfg(c_bindings)]
116 define_score!(Writeable);
117 #[cfg(not(c_bindings))]
118 define_score!();
119
120 /// A scorer that is accessed under a lock.
121 ///
122 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
123 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
124 /// implementations. Internal locking would be detrimental to route finding performance and could
125 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
126 ///
127 /// [`find_route`]: crate::routing::router::find_route
128 pub trait LockableScore<'a> {
129         /// The locked [`Score`] type.
130         type Locked: 'a + Score;
131
132         /// Returns the locked scorer.
133         fn lock(&'a self) -> Self::Locked;
134 }
135
136 /// (C-not exported)
137 impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
138         type Locked = MutexGuard<'a, T>;
139
140         fn lock(&'a self) -> MutexGuard<'a, T> {
141                 Mutex::lock(self).unwrap()
142         }
143 }
144
145 impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
146         type Locked = RefMut<'a, T>;
147
148         fn lock(&'a self) -> RefMut<'a, T> {
149                 self.borrow_mut()
150         }
151 }
152
153 #[cfg(c_bindings)]
154 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
155 pub struct MultiThreadedLockableScore<S: Score> {
156         score: Mutex<S>,
157 }
158 #[cfg(c_bindings)]
159 /// (C-not exported)
160 impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore<T> {
161         type Locked = MutexGuard<'a, T>;
162
163         fn lock(&'a self) -> MutexGuard<'a, T> {
164                 Mutex::lock(&self.score).unwrap()
165         }
166 }
167
168 #[cfg(c_bindings)]
169 impl<T: Score> MultiThreadedLockableScore<T> {
170         /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
171         pub fn new(score: T) -> Self {
172                 MultiThreadedLockableScore { score: Mutex::new(score) }
173         }
174 }
175
176 #[cfg(c_bindings)]
177 /// (C-not exported)
178 impl<'a, T: Writeable> Writeable for RefMut<'a, T> {
179         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
180                 T::write(&**self, writer)
181         }
182 }
183
184 #[cfg(c_bindings)]
185 /// (C-not exported)
186 impl<'a, S: Writeable> Writeable for MutexGuard<'a, S> {
187         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
188                 S::write(&**self, writer)
189         }
190 }
191
192 /// [`Score`] implementation that provides reasonable default behavior.
193 ///
194 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
195 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
196 ///
197 /// See [module-level documentation] for usage.
198 ///
199 /// [module-level documentation]: crate::routing::scoring
200 #[cfg(not(feature = "no-std"))]
201 pub type Scorer = ScorerUsingTime::<std::time::Instant>;
202 /// [`Score`] implementation that provides reasonable default behavior.
203 ///
204 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
205 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
206 ///
207 /// See [module-level documentation] for usage and [`ScoringParameters`] for customization.
208 ///
209 /// [module-level documentation]: crate::routing::scoring
210 #[cfg(feature = "no-std")]
211 pub type Scorer = ScorerUsingTime::<time::Eternity>;
212
213 // Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
214 // doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
215 // methods at all.
216
217 /// [`Score`] implementation.
218 ///
219 /// See [`Scorer`] for details.
220 ///
221 /// # Note
222 ///
223 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
224 /// behavior.
225 ///
226 /// (C-not exported) generally all users should use the [`Scorer`] type alias.
227 pub struct ScorerUsingTime<T: Time> {
228         params: ScoringParameters,
229         // TODO: Remove entries of closed channels.
230         channel_failures: HashMap<u64, ChannelFailure<T>>,
231 }
232
233 /// Parameters for configuring [`Scorer`].
234 pub struct ScoringParameters {
235         /// A fixed penalty in msats to apply to each channel.
236         ///
237         /// Default value: 500 msat
238         pub base_penalty_msat: u64,
239
240         /// A penalty in msats to apply to a channel upon failing to relay a payment.
241         ///
242         /// This accumulates for each failure but may be reduced over time based on
243         /// [`failure_penalty_half_life`] or when successfully routing through a channel.
244         ///
245         /// Default value: 1,024,000 msat
246         ///
247         /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
248         pub failure_penalty_msat: u64,
249
250         /// When the amount being sent over a channel is this many 1024ths of the total channel
251         /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
252         ///
253         /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
254         ///
255         /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
256         pub overuse_penalty_start_1024th: u16,
257
258         /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
259         /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
260         ///
261         /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
262         ///                to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
263         ///
264         /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
265         pub overuse_penalty_msat_per_1024th: u64,
266
267         /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
268         /// cut in half.
269         ///
270         /// Successfully routing through a channel will immediately cut the penalty in half as well.
271         ///
272         /// # Note
273         ///
274         /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
275         /// never decay.
276         ///
277         /// [`failure_penalty_msat`]: Self::failure_penalty_msat
278         pub failure_penalty_half_life: Duration,
279 }
280
281 impl_writeable_tlv_based!(ScoringParameters, {
282         (0, base_penalty_msat, required),
283         (1, overuse_penalty_start_1024th, (default_value, 128)),
284         (2, failure_penalty_msat, required),
285         (3, overuse_penalty_msat_per_1024th, (default_value, 20)),
286         (4, failure_penalty_half_life, required),
287 });
288
289 /// Accounting for penalties against a channel for failing to relay any payments.
290 ///
291 /// Penalties decay over time, though accumulate as more failures occur.
292 struct ChannelFailure<T: Time> {
293         /// Accumulated penalty in msats for the channel as of `last_updated`.
294         undecayed_penalty_msat: u64,
295
296         /// Last time the channel either failed to route or successfully routed a payment. Used to decay
297         /// `undecayed_penalty_msat`.
298         last_updated: T,
299 }
300
301 impl<T: Time> ScorerUsingTime<T> {
302         /// Creates a new scorer using the given scoring parameters.
303         pub fn new(params: ScoringParameters) -> Self {
304                 Self {
305                         params,
306                         channel_failures: HashMap::new(),
307                 }
308         }
309
310         /// Creates a new scorer using `penalty_msat` as a fixed channel penalty.
311         #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
312         pub fn with_fixed_penalty(penalty_msat: u64) -> Self {
313                 Self::new(ScoringParameters {
314                         base_penalty_msat: penalty_msat,
315                         failure_penalty_msat: 0,
316                         failure_penalty_half_life: Duration::from_secs(0),
317                         overuse_penalty_start_1024th: 1024,
318                         overuse_penalty_msat_per_1024th: 0,
319                 })
320         }
321 }
322
323 impl<T: Time> ChannelFailure<T> {
324         fn new(failure_penalty_msat: u64) -> Self {
325                 Self {
326                         undecayed_penalty_msat: failure_penalty_msat,
327                         last_updated: T::now(),
328                 }
329         }
330
331         fn add_penalty(&mut self, failure_penalty_msat: u64, half_life: Duration) {
332                 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) + failure_penalty_msat;
333                 self.last_updated = T::now();
334         }
335
336         fn reduce_penalty(&mut self, half_life: Duration) {
337                 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) >> 1;
338                 self.last_updated = T::now();
339         }
340
341         fn decayed_penalty_msat(&self, half_life: Duration) -> u64 {
342                 self.last_updated.elapsed().as_secs()
343                         .checked_div(half_life.as_secs())
344                         .and_then(|decays| self.undecayed_penalty_msat.checked_shr(decays as u32))
345                         .unwrap_or(0)
346         }
347 }
348
349 impl<T: Time> Default for ScorerUsingTime<T> {
350         fn default() -> Self {
351                 Self::new(ScoringParameters::default())
352         }
353 }
354
355 impl Default for ScoringParameters {
356         fn default() -> Self {
357                 Self {
358                         base_penalty_msat: 500,
359                         failure_penalty_msat: 1024 * 1000,
360                         failure_penalty_half_life: Duration::from_secs(3600),
361                         overuse_penalty_start_1024th: 1024 / 8,
362                         overuse_penalty_msat_per_1024th: 20,
363                 }
364         }
365 }
366
367 impl<T: Time> Score for ScorerUsingTime<T> {
368         fn channel_penalty_msat(
369                 &self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, _source: &NodeId, _target: &NodeId
370         ) -> u64 {
371                 let failure_penalty_msat = self.channel_failures
372                         .get(&short_channel_id)
373                         .map_or(0, |value| value.decayed_penalty_msat(self.params.failure_penalty_half_life));
374
375                 let mut penalty_msat = self.params.base_penalty_msat + failure_penalty_msat;
376                 let send_1024ths = send_amt_msat.checked_mul(1024).unwrap_or(u64::max_value()) / capacity_msat;
377                 if send_1024ths > self.params.overuse_penalty_start_1024th as u64 {
378                         penalty_msat = penalty_msat.checked_add(
379                                         (send_1024ths - self.params.overuse_penalty_start_1024th as u64)
380                                         .checked_mul(self.params.overuse_penalty_msat_per_1024th).unwrap_or(u64::max_value()))
381                                 .unwrap_or(u64::max_value());
382                 }
383
384                 penalty_msat
385         }
386
387         fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
388                 let failure_penalty_msat = self.params.failure_penalty_msat;
389                 let half_life = self.params.failure_penalty_half_life;
390                 self.channel_failures
391                         .entry(short_channel_id)
392                         .and_modify(|failure| failure.add_penalty(failure_penalty_msat, half_life))
393                         .or_insert_with(|| ChannelFailure::new(failure_penalty_msat));
394         }
395
396         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
397                 let half_life = self.params.failure_penalty_half_life;
398                 for hop in path.iter() {
399                         self.channel_failures
400                                 .entry(hop.short_channel_id)
401                                 .and_modify(|failure| failure.reduce_penalty(half_life));
402                 }
403         }
404 }
405
406 impl<T: Time> Writeable for ScorerUsingTime<T> {
407         #[inline]
408         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
409                 self.params.write(w)?;
410                 self.channel_failures.write(w)?;
411                 write_tlv_fields!(w, {});
412                 Ok(())
413         }
414 }
415
416 impl<T: Time> Readable for ScorerUsingTime<T> {
417         #[inline]
418         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
419                 let res = Ok(Self {
420                         params: Readable::read(r)?,
421                         channel_failures: Readable::read(r)?,
422                 });
423                 read_tlv_fields!(r, {});
424                 res
425         }
426 }
427
428 impl<T: Time> Writeable for ChannelFailure<T> {
429         #[inline]
430         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
431                 let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
432                 write_tlv_fields!(w, {
433                         (0, self.undecayed_penalty_msat, required),
434                         (2, duration_since_epoch, required),
435                 });
436                 Ok(())
437         }
438 }
439
440 impl<T: Time> Readable for ChannelFailure<T> {
441         #[inline]
442         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
443                 let mut undecayed_penalty_msat = 0;
444                 let mut duration_since_epoch = Duration::from_secs(0);
445                 read_tlv_fields!(r, {
446                         (0, undecayed_penalty_msat, required),
447                         (2, duration_since_epoch, required),
448                 });
449                 Ok(Self {
450                         undecayed_penalty_msat,
451                         last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
452                 })
453         }
454 }
455
456 /// [`Score`] implementation using channel success probability distributions.
457 ///
458 /// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
459 /// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
460 /// distributions are defined based on knowledge learned from successful and unsuccessful attempts.
461 /// Then the negative `log10` of the success probability is used to determine the cost of routing a
462 /// specific HTLC amount through a channel.
463 ///
464 /// [1]: https://arxiv.org/abs/2107.05322
465 pub struct ProbabilisticScorer<G: Deref<Target = NetworkGraph>> {
466         params: ProbabilisticScoringParameters,
467         network_graph: G,
468         // TODO: Remove entries of closed channels.
469         channel_liquidities: HashMap<u64, ChannelLiquidity>,
470 }
471
472 /// Parameters for configuring [`ProbabilisticScorer`].
473 pub struct ProbabilisticScoringParameters {
474         /// A penalty applied after multiplying by the negative `log10` of the channel's success
475         /// probability for a payment.
476         ///
477         /// The success probability is determined by the effective channel capacity, the payment amount,
478         /// and knowledge learned from prior successful and unsuccessful payments. The lower bound of
479         /// the success probability is 0.01, effectively limiting the penalty to the range
480         /// `0..=2*liquidity_penalty_multiplier_msat`.
481         ///
482         /// Default value: 10,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         /// Lower channel liquidity bound in terms of an offset from zero.
497         min_liquidity_offset_msat: u64,
498
499         /// Upper channel liquidity bound in terms of an offset from the effective capacity.
500         max_liquidity_offset_msat: u64,
501 }
502
503 /// A view of [`ChannelLiquidity`] in one direction assuming a certain channel capacity.
504 struct DirectedChannelLiquidity<L: Deref<Target = u64>> {
505         min_liquidity_offset_msat: L,
506         max_liquidity_offset_msat: L,
507         capacity_msat: u64,
508 }
509
510 impl<G: Deref<Target = NetworkGraph>> ProbabilisticScorer<G> {
511         /// Creates a new scorer using the given scoring parameters for sending payments from a node
512         /// through a network graph.
513         pub fn new(params: ProbabilisticScoringParameters, network_graph: G) -> Self {
514                 Self {
515                         params,
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: 10_000,
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                 }.max(0.01) // Lower bound the success probability to ensure some channel is selected.
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                 let liquidity_penalty_multiplier_msat = self.params.liquidity_penalty_multiplier_msat;
656                 let success_probability = self.channel_liquidities
657                         .get(&short_channel_id)
658                         .unwrap_or(&ChannelLiquidity::new())
659                         .as_directed(source, target, capacity_msat)
660                         .success_probability(amount_msat);
661                 // NOTE: If success_probability is ever changed to return 0.0, log10 is undefined so return
662                 // u64::max_value instead.
663                 debug_assert!(success_probability > core::f64::EPSILON);
664                 (-(success_probability.log10()) * liquidity_penalty_multiplier_msat as f64) as u64
665         }
666
667         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
668                 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
669                 let network_graph = self.network_graph.read_only();
670                 for hop in path {
671                         let target = NodeId::from_pubkey(&hop.pubkey);
672                         let channel_directed_from_source = network_graph.channels()
673                                 .get(&hop.short_channel_id)
674                                 .and_then(|channel| channel.as_directed_to(&target));
675
676                         // Only score announced channels.
677                         if let Some((channel, source)) = channel_directed_from_source {
678                                 let capacity_msat = channel.effective_capacity().as_msat();
679                                 if hop.short_channel_id == short_channel_id {
680                                         self.channel_liquidities
681                                                 .entry(hop.short_channel_id)
682                                                 .or_insert_with(ChannelLiquidity::new)
683                                                 .as_directed_mut(source, &target, capacity_msat)
684                                                 .failed_at_channel(amount_msat);
685                                         break;
686                                 }
687
688                                 self.channel_liquidities
689                                         .entry(hop.short_channel_id)
690                                         .or_insert_with(ChannelLiquidity::new)
691                                         .as_directed_mut(source, &target, capacity_msat)
692                                         .failed_downstream(amount_msat);
693                         }
694                 }
695         }
696
697         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
698                 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
699                 let network_graph = self.network_graph.read_only();
700                 for hop in path {
701                         let target = NodeId::from_pubkey(&hop.pubkey);
702                         let channel_directed_from_source = network_graph.channels()
703                                 .get(&hop.short_channel_id)
704                                 .and_then(|channel| channel.as_directed_to(&target));
705
706                         // Only score announced channels.
707                         if let Some((channel, source)) = channel_directed_from_source {
708                                 let capacity_msat = channel.effective_capacity().as_msat();
709                                 self.channel_liquidities
710                                         .entry(hop.short_channel_id)
711                                         .or_insert_with(ChannelLiquidity::new)
712                                         .as_directed_mut(source, &target, capacity_msat)
713                                         .successful(amount_msat);
714                         }
715                 }
716         }
717 }
718
719 impl<G: Deref<Target = NetworkGraph>> Writeable for ProbabilisticScorer<G> {
720         #[inline]
721         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
722                 write_tlv_fields!(w, {
723                         (0, self.channel_liquidities, required)
724                 });
725                 Ok(())
726         }
727 }
728
729 impl<G: Deref<Target = NetworkGraph>> ReadableArgs<(ProbabilisticScoringParameters, G)>
730 for ProbabilisticScorer<G> {
731         #[inline]
732         fn read<R: Read>(
733                 r: &mut R, args: (ProbabilisticScoringParameters, G)
734         ) -> Result<Self, DecodeError> {
735                 let (params, network_graph) = args;
736                 let mut channel_liquidities = HashMap::new();
737                 read_tlv_fields!(r, {
738                         (0, channel_liquidities, required)
739                 });
740                 Ok(Self {
741                         params,
742                         network_graph,
743                         channel_liquidities,
744                 })
745         }
746 }
747
748 impl Writeable for ChannelLiquidity {
749         #[inline]
750         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
751                 write_tlv_fields!(w, {
752                         (0, self.min_liquidity_offset_msat, required),
753                         (2, self.max_liquidity_offset_msat, required),
754                 });
755                 Ok(())
756         }
757 }
758
759 impl Readable for ChannelLiquidity {
760         #[inline]
761         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
762                 let mut min_liquidity_offset_msat = 0;
763                 let mut max_liquidity_offset_msat = 0;
764                 read_tlv_fields!(r, {
765                         (0, min_liquidity_offset_msat, required),
766                         (2, max_liquidity_offset_msat, required),
767                 });
768                 Ok(Self {
769                         min_liquidity_offset_msat,
770                         max_liquidity_offset_msat
771                 })
772         }
773 }
774
775 pub(crate) mod time {
776         use core::ops::Sub;
777         use core::time::Duration;
778         /// A measurement of time.
779         pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
780                 /// Returns an instance corresponding to the current moment.
781                 fn now() -> Self;
782
783                 /// Returns the amount of time elapsed since `self` was created.
784                 fn elapsed(&self) -> Duration;
785
786                 /// Returns the amount of time passed since the beginning of [`Time`].
787                 ///
788                 /// Used during (de-)serialization.
789                 fn duration_since_epoch() -> Duration;
790         }
791
792         /// A state in which time has no meaning.
793         #[derive(Debug, PartialEq, Eq)]
794         pub struct Eternity;
795
796         #[cfg(not(feature = "no-std"))]
797         impl Time for std::time::Instant {
798                 fn now() -> Self {
799                         std::time::Instant::now()
800                 }
801
802                 fn duration_since_epoch() -> Duration {
803                         use std::time::SystemTime;
804                         SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
805                 }
806
807                 fn elapsed(&self) -> Duration {
808                         std::time::Instant::elapsed(self)
809                 }
810         }
811
812         impl Time for Eternity {
813                 fn now() -> Self {
814                         Self
815                 }
816
817                 fn duration_since_epoch() -> Duration {
818                         Duration::from_secs(0)
819                 }
820
821                 fn elapsed(&self) -> Duration {
822                         Duration::from_secs(0)
823                 }
824         }
825
826         impl Sub<Duration> for Eternity {
827                 type Output = Self;
828
829                 fn sub(self, _other: Duration) -> Self {
830                         self
831                 }
832         }
833 }
834
835 pub(crate) use self::time::Time;
836
837 #[cfg(test)]
838 mod tests {
839         use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorer, ScoringParameters, ScorerUsingTime, Time};
840         use super::time::Eternity;
841
842         use ln::features::{ChannelFeatures, NodeFeatures};
843         use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
844         use routing::scoring::Score;
845         use routing::network_graph::{NetworkGraph, NodeId};
846         use routing::router::RouteHop;
847         use util::ser::{Readable, Writeable};
848
849         use bitcoin::blockdata::constants::genesis_block;
850         use bitcoin::hashes::Hash;
851         use bitcoin::hashes::sha256d::Hash as Sha256dHash;
852         use bitcoin::network::constants::Network;
853         use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
854         use core::cell::Cell;
855         use core::ops::Sub;
856         use core::time::Duration;
857         use io;
858
859         // `Time` tests
860
861         /// Time that can be advanced manually in tests.
862         #[derive(Debug, PartialEq, Eq)]
863         struct SinceEpoch(Duration);
864
865         impl SinceEpoch {
866                 thread_local! {
867                         static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
868                 }
869
870                 fn advance(duration: Duration) {
871                         Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
872                 }
873         }
874
875         impl Time for SinceEpoch {
876                 fn now() -> Self {
877                         Self(Self::duration_since_epoch())
878                 }
879
880                 fn duration_since_epoch() -> Duration {
881                         Self::ELAPSED.with(|elapsed| elapsed.get())
882                 }
883
884                 fn elapsed(&self) -> Duration {
885                         Self::duration_since_epoch() - self.0
886                 }
887         }
888
889         impl Sub<Duration> for SinceEpoch {
890                 type Output = Self;
891
892                 fn sub(self, other: Duration) -> Self {
893                         Self(self.0 - other)
894                 }
895         }
896
897         #[test]
898         fn time_passes_when_advanced() {
899                 let now = SinceEpoch::now();
900                 assert_eq!(now.elapsed(), Duration::from_secs(0));
901
902                 SinceEpoch::advance(Duration::from_secs(1));
903                 SinceEpoch::advance(Duration::from_secs(1));
904
905                 let elapsed = now.elapsed();
906                 let later = SinceEpoch::now();
907
908                 assert_eq!(elapsed, Duration::from_secs(2));
909                 assert_eq!(later - elapsed, now);
910         }
911
912         #[test]
913         fn time_never_passes_in_an_eternity() {
914                 let now = Eternity::now();
915                 let elapsed = now.elapsed();
916                 let later = Eternity::now();
917
918                 assert_eq!(now.elapsed(), Duration::from_secs(0));
919                 assert_eq!(later - elapsed, now);
920         }
921
922         // `Scorer` tests
923
924         /// A scorer for testing with time that can be manually advanced.
925         type Scorer = ScorerUsingTime::<SinceEpoch>;
926
927         fn source_privkey() -> SecretKey {
928                 SecretKey::from_slice(&[42; 32]).unwrap()
929         }
930
931         fn target_privkey() -> SecretKey {
932                 SecretKey::from_slice(&[43; 32]).unwrap()
933         }
934
935         fn source_pubkey() -> PublicKey {
936                 let secp_ctx = Secp256k1::new();
937                 PublicKey::from_secret_key(&secp_ctx, &source_privkey())
938         }
939
940         fn target_pubkey() -> PublicKey {
941                 let secp_ctx = Secp256k1::new();
942                 PublicKey::from_secret_key(&secp_ctx, &target_privkey())
943         }
944
945         fn source_node_id() -> NodeId {
946                 NodeId::from_pubkey(&source_pubkey())
947         }
948
949         fn target_node_id() -> NodeId {
950                 NodeId::from_pubkey(&target_pubkey())
951         }
952
953         #[test]
954         fn penalizes_without_channel_failures() {
955                 let scorer = Scorer::new(ScoringParameters {
956                         base_penalty_msat: 1_000,
957                         failure_penalty_msat: 512,
958                         failure_penalty_half_life: Duration::from_secs(1),
959                         overuse_penalty_start_1024th: 1024,
960                         overuse_penalty_msat_per_1024th: 0,
961                 });
962                 let source = source_node_id();
963                 let target = target_node_id();
964                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
965
966                 SinceEpoch::advance(Duration::from_secs(1));
967                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
968         }
969
970         #[test]
971         fn accumulates_channel_failure_penalties() {
972                 let mut scorer = Scorer::new(ScoringParameters {
973                         base_penalty_msat: 1_000,
974                         failure_penalty_msat: 64,
975                         failure_penalty_half_life: Duration::from_secs(10),
976                         overuse_penalty_start_1024th: 1024,
977                         overuse_penalty_msat_per_1024th: 0,
978                 });
979                 let source = source_node_id();
980                 let target = target_node_id();
981                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
982
983                 scorer.payment_path_failed(&[], 42);
984                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
985
986                 scorer.payment_path_failed(&[], 42);
987                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
988
989                 scorer.payment_path_failed(&[], 42);
990                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_192);
991         }
992
993         #[test]
994         fn decays_channel_failure_penalties_over_time() {
995                 let mut scorer = Scorer::new(ScoringParameters {
996                         base_penalty_msat: 1_000,
997                         failure_penalty_msat: 512,
998                         failure_penalty_half_life: Duration::from_secs(10),
999                         overuse_penalty_start_1024th: 1024,
1000                         overuse_penalty_msat_per_1024th: 0,
1001                 });
1002                 let source = source_node_id();
1003                 let target = target_node_id();
1004                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1005
1006                 scorer.payment_path_failed(&[], 42);
1007                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1008
1009                 SinceEpoch::advance(Duration::from_secs(9));
1010                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1011
1012                 SinceEpoch::advance(Duration::from_secs(1));
1013                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1014
1015                 SinceEpoch::advance(Duration::from_secs(10 * 8));
1016                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_001);
1017
1018                 SinceEpoch::advance(Duration::from_secs(10));
1019                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1020
1021                 SinceEpoch::advance(Duration::from_secs(10));
1022                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1023         }
1024
1025         #[test]
1026         fn decays_channel_failure_penalties_without_shift_overflow() {
1027                 let mut scorer = Scorer::new(ScoringParameters {
1028                         base_penalty_msat: 1_000,
1029                         failure_penalty_msat: 512,
1030                         failure_penalty_half_life: Duration::from_secs(10),
1031                         overuse_penalty_start_1024th: 1024,
1032                         overuse_penalty_msat_per_1024th: 0,
1033                 });
1034                 let source = source_node_id();
1035                 let target = target_node_id();
1036                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1037
1038                 scorer.payment_path_failed(&[], 42);
1039                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1040
1041                 // An unchecked right shift 64 bits or more in ChannelFailure::decayed_penalty_msat would
1042                 // cause an overflow.
1043                 SinceEpoch::advance(Duration::from_secs(10 * 64));
1044                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1045
1046                 SinceEpoch::advance(Duration::from_secs(10));
1047                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1048         }
1049
1050         #[test]
1051         fn accumulates_channel_failure_penalties_after_decay() {
1052                 let mut scorer = Scorer::new(ScoringParameters {
1053                         base_penalty_msat: 1_000,
1054                         failure_penalty_msat: 512,
1055                         failure_penalty_half_life: Duration::from_secs(10),
1056                         overuse_penalty_start_1024th: 1024,
1057                         overuse_penalty_msat_per_1024th: 0,
1058                 });
1059                 let source = source_node_id();
1060                 let target = target_node_id();
1061                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1062
1063                 scorer.payment_path_failed(&[], 42);
1064                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1065
1066                 SinceEpoch::advance(Duration::from_secs(10));
1067                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1068
1069                 scorer.payment_path_failed(&[], 42);
1070                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_768);
1071
1072                 SinceEpoch::advance(Duration::from_secs(10));
1073                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_384);
1074         }
1075
1076         #[test]
1077         fn reduces_channel_failure_penalties_after_success() {
1078                 let mut scorer = Scorer::new(ScoringParameters {
1079                         base_penalty_msat: 1_000,
1080                         failure_penalty_msat: 512,
1081                         failure_penalty_half_life: Duration::from_secs(10),
1082                         overuse_penalty_start_1024th: 1024,
1083                         overuse_penalty_msat_per_1024th: 0,
1084                 });
1085                 let source = source_node_id();
1086                 let target = target_node_id();
1087                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1088
1089                 scorer.payment_path_failed(&[], 42);
1090                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1091
1092                 SinceEpoch::advance(Duration::from_secs(10));
1093                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1094
1095                 let hop = RouteHop {
1096                         pubkey: PublicKey::from_slice(target.as_slice()).unwrap(),
1097                         node_features: NodeFeatures::known(),
1098                         short_channel_id: 42,
1099                         channel_features: ChannelFeatures::known(),
1100                         fee_msat: 1,
1101                         cltv_expiry_delta: 18,
1102                 };
1103                 scorer.payment_path_successful(&[&hop]);
1104                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1105
1106                 SinceEpoch::advance(Duration::from_secs(10));
1107                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1108         }
1109
1110         #[test]
1111         fn restores_persisted_channel_failure_penalties() {
1112                 let mut scorer = Scorer::new(ScoringParameters {
1113                         base_penalty_msat: 1_000,
1114                         failure_penalty_msat: 512,
1115                         failure_penalty_half_life: Duration::from_secs(10),
1116                         overuse_penalty_start_1024th: 1024,
1117                         overuse_penalty_msat_per_1024th: 0,
1118                 });
1119                 let source = source_node_id();
1120                 let target = target_node_id();
1121
1122                 scorer.payment_path_failed(&[], 42);
1123                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1124
1125                 SinceEpoch::advance(Duration::from_secs(10));
1126                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1127
1128                 scorer.payment_path_failed(&[], 43);
1129                 assert_eq!(scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1130
1131                 let mut serialized_scorer = Vec::new();
1132                 scorer.write(&mut serialized_scorer).unwrap();
1133
1134                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1135                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1136                 assert_eq!(deserialized_scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1137         }
1138
1139         #[test]
1140         fn decays_persisted_channel_failure_penalties() {
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
1151                 scorer.payment_path_failed(&[], 42);
1152                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1153
1154                 let mut serialized_scorer = Vec::new();
1155                 scorer.write(&mut serialized_scorer).unwrap();
1156
1157                 SinceEpoch::advance(Duration::from_secs(10));
1158
1159                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1160                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1161
1162                 SinceEpoch::advance(Duration::from_secs(10));
1163                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1164         }
1165
1166         #[test]
1167         fn charges_per_1024th_penalty() {
1168                 let scorer = Scorer::new(ScoringParameters {
1169                         base_penalty_msat: 0,
1170                         failure_penalty_msat: 0,
1171                         failure_penalty_half_life: Duration::from_secs(0),
1172                         overuse_penalty_start_1024th: 256,
1173                         overuse_penalty_msat_per_1024th: 100,
1174                 });
1175                 let source = source_node_id();
1176                 let target = target_node_id();
1177
1178                 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 1_024_000, &source, &target), 0);
1179                 assert_eq!(scorer.channel_penalty_msat(42, 256_999, 1_024_000, &source, &target), 0);
1180                 assert_eq!(scorer.channel_penalty_msat(42, 257_000, 1_024_000, &source, &target), 100);
1181                 assert_eq!(scorer.channel_penalty_msat(42, 258_000, 1_024_000, &source, &target), 200);
1182                 assert_eq!(scorer.channel_penalty_msat(42, 512_000, 1_024_000, &source, &target), 256 * 100);
1183         }
1184
1185         // `ProbabilisticScorer` tests
1186
1187         fn sender_privkey() -> SecretKey {
1188                 SecretKey::from_slice(&[41; 32]).unwrap()
1189         }
1190
1191         fn recipient_privkey() -> SecretKey {
1192                 SecretKey::from_slice(&[45; 32]).unwrap()
1193         }
1194
1195         fn sender_pubkey() -> PublicKey {
1196                 let secp_ctx = Secp256k1::new();
1197                 PublicKey::from_secret_key(&secp_ctx, &sender_privkey())
1198         }
1199
1200         fn recipient_pubkey() -> PublicKey {
1201                 let secp_ctx = Secp256k1::new();
1202                 PublicKey::from_secret_key(&secp_ctx, &recipient_privkey())
1203         }
1204
1205         fn sender_node_id() -> NodeId {
1206                 NodeId::from_pubkey(&sender_pubkey())
1207         }
1208
1209         fn recipient_node_id() -> NodeId {
1210                 NodeId::from_pubkey(&recipient_pubkey())
1211         }
1212
1213         fn network_graph() -> NetworkGraph {
1214                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1215                 let mut network_graph = NetworkGraph::new(genesis_hash);
1216                 add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
1217                 add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
1218
1219                 network_graph
1220         }
1221
1222         fn add_channel(
1223                 network_graph: &mut NetworkGraph, short_channel_id: u64, node_1_key: SecretKey,
1224                 node_2_key: SecretKey
1225         ) {
1226                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1227                 let node_1_secret = &SecretKey::from_slice(&[39; 32]).unwrap();
1228                 let node_2_secret = &SecretKey::from_slice(&[40; 32]).unwrap();
1229                 let secp_ctx = Secp256k1::new();
1230                 let unsigned_announcement = UnsignedChannelAnnouncement {
1231                         features: ChannelFeatures::known(),
1232                         chain_hash: genesis_hash,
1233                         short_channel_id,
1234                         node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_key),
1235                         node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_key),
1236                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_secret),
1237                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_secret),
1238                         excess_data: Vec::new(),
1239                 };
1240                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1241                 let signed_announcement = ChannelAnnouncement {
1242                         node_signature_1: secp_ctx.sign(&msghash, &node_1_key),
1243                         node_signature_2: secp_ctx.sign(&msghash, &node_2_key),
1244                         bitcoin_signature_1: secp_ctx.sign(&msghash, &node_1_secret),
1245                         bitcoin_signature_2: secp_ctx.sign(&msghash, &node_2_secret),
1246                         contents: unsigned_announcement,
1247                 };
1248                 let chain_source: Option<&::util::test_utils::TestChainSource> = None;
1249                 network_graph.update_channel_from_announcement(
1250                         &signed_announcement, &chain_source, &secp_ctx).unwrap();
1251                 update_channel(network_graph, short_channel_id, node_1_key, 0);
1252                 update_channel(network_graph, short_channel_id, node_2_key, 1);
1253         }
1254
1255         fn update_channel(
1256                 network_graph: &mut NetworkGraph, short_channel_id: u64, node_key: SecretKey, flags: u8
1257         ) {
1258                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1259                 let secp_ctx = Secp256k1::new();
1260                 let unsigned_update = UnsignedChannelUpdate {
1261                         chain_hash: genesis_hash,
1262                         short_channel_id,
1263                         timestamp: 100,
1264                         flags,
1265                         cltv_expiry_delta: 18,
1266                         htlc_minimum_msat: 0,
1267                         htlc_maximum_msat: OptionalField::Present(1_000),
1268                         fee_base_msat: 1,
1269                         fee_proportional_millionths: 0,
1270                         excess_data: Vec::new(),
1271                 };
1272                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
1273                 let signed_update = ChannelUpdate {
1274                         signature: secp_ctx.sign(&msghash, &node_key),
1275                         contents: unsigned_update,
1276                 };
1277                 network_graph.update_channel(&signed_update, &secp_ctx).unwrap();
1278         }
1279
1280         fn payment_path_for_amount(amount_msat: u64) -> Vec<RouteHop> {
1281                 vec![
1282                         RouteHop {
1283                                 pubkey: source_pubkey(),
1284                                 node_features: NodeFeatures::known(),
1285                                 short_channel_id: 41,
1286                                 channel_features: ChannelFeatures::known(),
1287                                 fee_msat: 1,
1288                                 cltv_expiry_delta: 18,
1289                         },
1290                         RouteHop {
1291                                 pubkey: target_pubkey(),
1292                                 node_features: NodeFeatures::known(),
1293                                 short_channel_id: 42,
1294                                 channel_features: ChannelFeatures::known(),
1295                                 fee_msat: 2,
1296                                 cltv_expiry_delta: 18,
1297                         },
1298                         RouteHop {
1299                                 pubkey: recipient_pubkey(),
1300                                 node_features: NodeFeatures::known(),
1301                                 short_channel_id: 43,
1302                                 channel_features: ChannelFeatures::known(),
1303                                 fee_msat: amount_msat,
1304                                 cltv_expiry_delta: 18,
1305                         },
1306                 ]
1307         }
1308
1309         #[test]
1310         fn liquidity_bounds_directed_from_lowest_node_id() {
1311                 let network_graph = network_graph();
1312                 let params = ProbabilisticScoringParameters::default();
1313                 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1314                         .with_channel(42,
1315                                 ChannelLiquidity {
1316                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1317                                 })
1318                         .with_channel(43,
1319                                 ChannelLiquidity {
1320                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1321                                 });
1322                 let source = source_node_id();
1323                 let target = target_node_id();
1324                 let recipient = recipient_node_id();
1325
1326                 let liquidity = scorer.channel_liquidities.get_mut(&42).unwrap();
1327                 assert!(source > target);
1328                 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 100);
1329                 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1330                 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1331                 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 900);
1332
1333                 liquidity.as_directed_mut(&source, &target, 1_000).set_min_liquidity_msat(200);
1334                 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 200);
1335                 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1336                 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1337                 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 800);
1338
1339                 let liquidity = scorer.channel_liquidities.get_mut(&43).unwrap();
1340                 assert!(target < recipient);
1341                 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 700);
1342                 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 900);
1343                 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 100);
1344                 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 300);
1345
1346                 liquidity.as_directed_mut(&target, &recipient, 1_000).set_max_liquidity_msat(200);
1347                 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 0);
1348                 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 200);
1349                 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 800);
1350                 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 1000);
1351         }
1352
1353         #[test]
1354         fn resets_liquidity_upper_bound_when_crossed_by_lower_bound() {
1355                 let network_graph = network_graph();
1356                 let params = ProbabilisticScoringParameters::default();
1357                 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1358                         .with_channel(42,
1359                                 ChannelLiquidity {
1360                                         min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400
1361                                 });
1362                 let source = source_node_id();
1363                 let target = target_node_id();
1364                 assert!(source > target);
1365
1366                 // Check initial bounds.
1367                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1368                         .as_directed(&source, &target, 1_000);
1369                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1370                 assert_eq!(liquidity.max_liquidity_msat(), 800);
1371
1372                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1373                         .as_directed(&target, &source, 1_000);
1374                 assert_eq!(liquidity.min_liquidity_msat(), 200);
1375                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1376
1377                 // Reset from source to target.
1378                 scorer.channel_liquidities.get_mut(&42).unwrap()
1379                         .as_directed_mut(&source, &target, 1_000)
1380                         .set_min_liquidity_msat(900);
1381
1382                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1383                         .as_directed(&source, &target, 1_000);
1384                 assert_eq!(liquidity.min_liquidity_msat(), 900);
1385                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1386
1387                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1388                         .as_directed(&target, &source, 1_000);
1389                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1390                 assert_eq!(liquidity.max_liquidity_msat(), 100);
1391
1392                 // Reset from target to source.
1393                 scorer.channel_liquidities.get_mut(&42).unwrap()
1394                         .as_directed_mut(&target, &source, 1_000)
1395                         .set_min_liquidity_msat(400);
1396
1397                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1398                         .as_directed(&source, &target, 1_000);
1399                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1400                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1401
1402                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1403                         .as_directed(&target, &source, 1_000);
1404                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1405                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1406         }
1407
1408         #[test]
1409         fn resets_liquidity_lower_bound_when_crossed_by_upper_bound() {
1410                 let network_graph = network_graph();
1411                 let params = ProbabilisticScoringParameters::default();
1412                 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1413                         .with_channel(42,
1414                                 ChannelLiquidity {
1415                                         min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400
1416                                 });
1417                 let source = source_node_id();
1418                 let target = target_node_id();
1419                 assert!(source > target);
1420
1421                 // Check initial bounds.
1422                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1423                         .as_directed(&source, &target, 1_000);
1424                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1425                 assert_eq!(liquidity.max_liquidity_msat(), 800);
1426
1427                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1428                         .as_directed(&target, &source, 1_000);
1429                 assert_eq!(liquidity.min_liquidity_msat(), 200);
1430                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1431
1432                 // Reset from source to target.
1433                 scorer.channel_liquidities.get_mut(&42).unwrap()
1434                         .as_directed_mut(&source, &target, 1_000)
1435                         .set_max_liquidity_msat(300);
1436
1437                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1438                         .as_directed(&source, &target, 1_000);
1439                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1440                 assert_eq!(liquidity.max_liquidity_msat(), 300);
1441
1442                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1443                         .as_directed(&target, &source, 1_000);
1444                 assert_eq!(liquidity.min_liquidity_msat(), 700);
1445                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1446
1447                 // Reset from target to source.
1448                 scorer.channel_liquidities.get_mut(&42).unwrap()
1449                         .as_directed_mut(&target, &source, 1_000)
1450                         .set_max_liquidity_msat(600);
1451
1452                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1453                         .as_directed(&source, &target, 1_000);
1454                 assert_eq!(liquidity.min_liquidity_msat(), 400);
1455                 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1456
1457                 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1458                         .as_directed(&target, &source, 1_000);
1459                 assert_eq!(liquidity.min_liquidity_msat(), 0);
1460                 assert_eq!(liquidity.max_liquidity_msat(), 600);
1461         }
1462
1463         #[test]
1464         fn increased_penalty_nearing_liquidity_upper_bound() {
1465                 let network_graph = network_graph();
1466                 let params = ProbabilisticScoringParameters {
1467                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1468                 };
1469                 let scorer = ProbabilisticScorer::new(params, &network_graph);
1470                 let source = source_node_id();
1471                 let target = target_node_id();
1472
1473                 assert_eq!(scorer.channel_penalty_msat(42, 100, 100_000, &source, &target), 0);
1474                 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 100_000, &source, &target), 4);
1475                 assert_eq!(scorer.channel_penalty_msat(42, 10_000, 100_000, &source, &target), 45);
1476                 assert_eq!(scorer.channel_penalty_msat(42, 100_000, 100_000, &source, &target), 2_000);
1477
1478                 assert_eq!(scorer.channel_penalty_msat(42, 125, 1_000, &source, &target), 57);
1479                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1480                 assert_eq!(scorer.channel_penalty_msat(42, 375, 1_000, &source, &target), 203);
1481                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1482                 assert_eq!(scorer.channel_penalty_msat(42, 625, 1_000, &source, &target), 425);
1483                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1484                 assert_eq!(scorer.channel_penalty_msat(42, 875, 1_000, &source, &target), 900);
1485         }
1486
1487         #[test]
1488         fn constant_penalty_outside_liquidity_bounds() {
1489                 let network_graph = network_graph();
1490                 let params = ProbabilisticScoringParameters {
1491                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1492                 };
1493                 let scorer = ProbabilisticScorer::new(params, &network_graph)
1494                         .with_channel(42,
1495                                 ChannelLiquidity { min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40 });
1496                 let source = source_node_id();
1497                 let target = target_node_id();
1498
1499                 assert_eq!(scorer.channel_penalty_msat(42, 39, 100, &source, &target), 0);
1500                 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 0);
1501                 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 2_000);
1502                 assert_eq!(scorer.channel_penalty_msat(42, 61, 100, &source, &target), 2_000);
1503         }
1504
1505         #[test]
1506         fn does_not_further_penalize_own_channel() {
1507                 let network_graph = network_graph();
1508                 let params = ProbabilisticScoringParameters {
1509                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1510                 };
1511                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1512                 let sender = sender_node_id();
1513                 let source = source_node_id();
1514                 let failed_path = payment_path_for_amount(500);
1515                 let successful_path = payment_path_for_amount(200);
1516
1517                 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1518
1519                 scorer.payment_path_failed(&failed_path.iter().collect::<Vec<_>>(), 41);
1520                 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1521
1522                 scorer.payment_path_successful(&successful_path.iter().collect::<Vec<_>>());
1523                 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1524         }
1525
1526         #[test]
1527         fn sets_liquidity_lower_bound_on_downstream_failure() {
1528                 let network_graph = network_graph();
1529                 let params = ProbabilisticScoringParameters {
1530                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1531                 };
1532                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1533                 let source = source_node_id();
1534                 let target = target_node_id();
1535                 let path = payment_path_for_amount(500);
1536
1537                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1538                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1539                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1540
1541                 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 43);
1542
1543                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 0);
1544                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 0);
1545                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 300);
1546         }
1547
1548         #[test]
1549         fn sets_liquidity_upper_bound_on_failure() {
1550                 let network_graph = network_graph();
1551                 let params = ProbabilisticScoringParameters {
1552                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1553                 };
1554                 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1555                 let source = source_node_id();
1556                 let target = target_node_id();
1557                 let path = payment_path_for_amount(500);
1558
1559                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1560                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1561                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1562
1563                 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 42);
1564
1565                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 300);
1566                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2_000);
1567                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 2_000);
1568         }
1569
1570         #[test]
1571         fn reduces_liquidity_upper_bound_along_path_on_success() {
1572                 let network_graph = network_graph();
1573                 let params = ProbabilisticScoringParameters {
1574                         liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1575                 };
1576                 let mut scorer = ProbabilisticScorer::new(params, &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), 124);
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), 124);
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 }