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 //! [`Scorer`] may be given to [`find_route`] to score payment channels during path finding when a
13 //! 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::{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, params: RouteParameters, network_graph: NetworkGraph) {
31 //! # let logger = FakeLogger {};
32 //! #
33 //! // Use the default channel penalties.
34 //! let scorer = Scorer::default();
35 //!
36 //! // Or use custom channel penalties.
37 //! let scorer = Scorer::new(ScoringParameters {
38 //!     base_penalty_msat: 1000,
39 //!     failure_penalty_msat: 2 * 1024 * 1000,
40 //!     ..ScoringParameters::default()
41 //! });
42 //!
43 //! let route = find_route(&payer, &params, &network_graph, None, &logger, &scorer);
44 //! # }
45 //! ```
46 //!
47 //! # Note
48 //!
49 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
50 //! different types and thus is undefined.
51 //!
52 //! [`find_route`]: crate::routing::router::find_route
53
54 use bitcoin::secp256k1::key::PublicKey;
55
56 use ln::msgs::DecodeError;
57 use routing::network_graph::{EffectiveCapacity, NetworkGraph, NodeId};
58 use routing::router::RouteHop;
59 use util::ser::{Readable, ReadableArgs, Writeable, Writer};
60
61 use prelude::*;
62 use core::cell::{RefCell, RefMut};
63 use core::ops::{Deref, DerefMut};
64 use core::time::Duration;
65 use io::{self, Read};
66 use sync::{Mutex, MutexGuard};
67
68 /// We define Score ever-so-slightly differently based on whether we are being built for C bindings
69 /// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
70 /// no problem - you move a `Score` that implements `Writeable` into a `Mutex`, lock it, and now
71 /// you have the original, concrete, `Score` type, which presumably implements `Writeable`.
72 ///
73 /// For C users, once you've moved the `Score` into a `LockableScore` all you have after locking it
74 /// is an opaque trait object with an opaque pointer with no type info. Users could take the unsafe
75 /// approach of blindly casting that opaque pointer to a concrete type and calling `Writeable` from
76 /// there, but other languages downstream of the C bindings (e.g. Java) can't even do that.
77 /// Instead, we really want `Score` and `LockableScore` to implement `Writeable` directly, which we
78 /// do here by defining `Score` differently for `cfg(c_bindings)`.
79 macro_rules! define_score { ($($supertrait: path)*) => {
80 /// An interface used to score payment channels for path finding.
81 ///
82 ///     Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
83 pub trait Score $(: $supertrait)* {
84         /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
85         /// given channel in the direction from `source` to `target`.
86         ///
87         /// The channel's capacity (less any other MPP parts that are also being considered for use in
88         /// the same payment) is given by `capacity_msat`. It may be determined from various sources
89         /// such as a chain data, network gossip, or invoice hints, the latter indicating sufficient
90         /// capacity (i.e., near [`u64::max_value`]). 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 log 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         node_id: NodeId,
468         network_graph: G,
469         // TODO: Remove entries of closed channels.
470         channel_liquidities: HashMap<u64, ChannelLiquidity>,
471 }
472
473 /// Parameters for configuring [`ProbabilisticScorer`].
474 pub struct ProbabilisticScoringParameters;
475
476 impl_writeable_tlv_based!(ProbabilisticScoringParameters, {
477 });
478
479 /// Accounting for channel liquidity balance uncertainty.
480 ///
481 /// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
482 /// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
483 /// offset fields gives the opposite direction.
484 struct ChannelLiquidity {
485         min_liquidity_offset_msat: u64,
486         max_liquidity_offset_msat: u64,
487 }
488
489 /// A view of [`ChannelLiquidity`] in one direction assuming a certain channel capacity.
490 struct DirectedChannelLiquidity<L: Deref<Target = u64>> {
491         min_liquidity_offset_msat: L,
492         max_liquidity_offset_msat: L,
493         capacity_msat: u64,
494 }
495
496 /// The likelihood of an event occurring.
497 enum Probability {
498         Zero,
499         One,
500         Ratio { numerator: u64, denominator: u64 },
501 }
502
503 impl<G: Deref<Target = NetworkGraph>> ProbabilisticScorer<G> {
504         /// Creates a new scorer using the given scoring parameters for sending payments from a node
505         /// through a network graph.
506         pub fn new(
507                 _params: ProbabilisticScoringParameters, node_pubkey: PublicKey, network_graph: G
508         ) -> Self {
509                 Self {
510                         _params,
511                         node_id: NodeId::from_pubkey(&node_pubkey),
512                         network_graph,
513                         channel_liquidities: HashMap::new(),
514                 }
515         }
516
517         #[cfg(test)]
518         fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity) -> Self {
519                 assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
520                 self
521         }
522 }
523
524 impl Default for ProbabilisticScoringParameters {
525         fn default() -> Self {
526                 Self
527         }
528 }
529
530 impl ChannelLiquidity {
531         #[inline]
532         fn new() -> Self {
533                 Self {
534                         min_liquidity_offset_msat: 0,
535                         max_liquidity_offset_msat: 0,
536                 }
537         }
538
539         /// Returns a view of the channel liquidity directed from `source` to `target` assuming
540         /// `capacity_msat`.
541         fn as_directed(
542                 &self, source: &NodeId, target: &NodeId, capacity_msat: u64
543         ) -> DirectedChannelLiquidity<&u64> {
544                 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
545                         (&self.min_liquidity_offset_msat, &self.max_liquidity_offset_msat)
546                 } else {
547                         (&self.max_liquidity_offset_msat, &self.min_liquidity_offset_msat)
548                 };
549
550                 DirectedChannelLiquidity {
551                         min_liquidity_offset_msat,
552                         max_liquidity_offset_msat,
553                         capacity_msat,
554                 }
555         }
556
557         /// Returns a mutable view of the channel liquidity directed from `source` to `target` assuming
558         /// `capacity_msat`.
559         fn as_directed_mut(
560                 &mut self, source: &NodeId, target: &NodeId, capacity_msat: u64
561         ) -> DirectedChannelLiquidity<&mut u64> {
562                 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
563                         (&mut self.min_liquidity_offset_msat, &mut self.max_liquidity_offset_msat)
564                 } else {
565                         (&mut self.max_liquidity_offset_msat, &mut self.min_liquidity_offset_msat)
566                 };
567
568                 DirectedChannelLiquidity {
569                         min_liquidity_offset_msat,
570                         max_liquidity_offset_msat,
571                         capacity_msat,
572                 }
573         }
574 }
575
576 impl<L: Deref<Target = u64>> DirectedChannelLiquidity<L> {
577         /// Returns the success probability of routing the given HTLC `amount_msat` through the channel
578         /// in this direction.
579         fn success_probability(&self, amount_msat: u64) -> Probability {
580                 let max_liquidity_msat = self.max_liquidity_msat();
581                 let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
582                 if amount_msat > max_liquidity_msat {
583                         Probability::Zero
584                 } else if amount_msat < min_liquidity_msat {
585                         Probability::One
586                 } else {
587                         let numerator = max_liquidity_msat + 1 - amount_msat;
588                         let denominator = max_liquidity_msat + 1 - min_liquidity_msat;
589                         if numerator == denominator {
590                                 Probability::One
591                         } else {
592                                 Probability::Ratio { numerator, denominator }
593                         }
594                 }
595         }
596
597         /// Returns the lower bound of the channel liquidity balance in this direction.
598         fn min_liquidity_msat(&self) -> u64 {
599                 *self.min_liquidity_offset_msat
600         }
601
602         /// Returns the upper bound of the channel liquidity balance in this direction.
603         fn max_liquidity_msat(&self) -> u64 {
604                 self.capacity_msat.checked_sub(*self.max_liquidity_offset_msat).unwrap_or(0)
605         }
606 }
607
608 impl<L: DerefMut<Target = u64>> DirectedChannelLiquidity<L> {
609         /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
610         fn failed_at_channel(&mut self, amount_msat: u64) {
611                 if amount_msat < self.max_liquidity_msat() {
612                         self.set_max_liquidity_msat(amount_msat);
613                 }
614         }
615
616         /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
617         fn failed_downstream(&mut self, amount_msat: u64) {
618                 if amount_msat > self.min_liquidity_msat() {
619                         self.set_min_liquidity_msat(amount_msat);
620                 }
621         }
622
623         /// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
624         fn successful(&mut self, amount_msat: u64) {
625                 let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
626                 self.set_max_liquidity_msat(max_liquidity_msat);
627         }
628
629         /// Adjusts the lower bound of the channel liquidity balance in this direction.
630         fn set_min_liquidity_msat(&mut self, amount_msat: u64) {
631                 *self.min_liquidity_offset_msat = amount_msat;
632
633                 if amount_msat > self.max_liquidity_msat() {
634                         *self.max_liquidity_offset_msat = 0;
635                 }
636         }
637
638         /// Adjusts the upper bound of the channel liquidity balance in this direction.
639         fn set_max_liquidity_msat(&mut self, amount_msat: u64) {
640                 *self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
641
642                 if amount_msat < self.min_liquidity_msat() {
643                         *self.min_liquidity_offset_msat = 0;
644                 }
645         }
646 }
647
648 impl<G: Deref<Target = NetworkGraph>> Score for ProbabilisticScorer<G> {
649         fn channel_penalty_msat(
650                 &self, short_channel_id: u64, amount_msat: u64, capacity_msat: u64, source: &NodeId,
651                 target: &NodeId
652         ) -> u64 {
653                 if *source == self.node_id || *target == self.node_id {
654                         return 0;
655                 }
656
657                 let success_probability = self.channel_liquidities
658                         .get(&short_channel_id)
659                         .unwrap_or(&ChannelLiquidity::new())
660                         .as_directed(source, target, capacity_msat)
661                         .success_probability(amount_msat);
662                 match success_probability {
663                         Probability::Zero => u64::max_value(),
664                         Probability::One => 0,
665                         Probability::Ratio { numerator, denominator } => {
666                                 let success_probability = numerator as f64 / denominator as f64;
667                                 (-(success_probability.log10()) * amount_msat as f64) as u64
668                         },
669                 }
670         }
671
672         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
673                 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
674                 let network_graph = self.network_graph.read_only();
675                 let hop_sources = core::iter::once(self.node_id)
676                         .chain(path.iter().map(|hop| NodeId::from_pubkey(&hop.pubkey)));
677                 for (source, hop) in hop_sources.zip(path.iter()) {
678                         let target = NodeId::from_pubkey(&hop.pubkey);
679                         if source == self.node_id || target == self.node_id {
680                                 continue;
681                         }
682
683                         let capacity_msat = network_graph.channels()
684                                 .get(&hop.short_channel_id)
685                                 .and_then(|channel| channel.as_directed_to(&target).map(|d| d.effective_capacity()))
686                                 .unwrap_or(EffectiveCapacity::Unknown)
687                                 .as_msat();
688
689                         if hop.short_channel_id == short_channel_id {
690                                 self.channel_liquidities
691                                         .entry(hop.short_channel_id)
692                                         .or_insert_with(|| ChannelLiquidity::new())
693                                         .as_directed_mut(&source, &target, capacity_msat)
694                                         .failed_at_channel(amount_msat);
695                                 break;
696                         }
697
698                         self.channel_liquidities
699                                 .entry(hop.short_channel_id)
700                                 .or_insert_with(|| ChannelLiquidity::new())
701                                 .as_directed_mut(&source, &target, capacity_msat)
702                                 .failed_downstream(amount_msat);
703                 }
704         }
705
706         fn payment_path_successful(&mut self, path: &[&RouteHop]) {
707                 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
708                 let network_graph = self.network_graph.read_only();
709                 let hop_sources = core::iter::once(self.node_id)
710                         .chain(path.iter().map(|hop| NodeId::from_pubkey(&hop.pubkey)));
711                 for (source, hop) in hop_sources.zip(path.iter()) {
712                         let target = NodeId::from_pubkey(&hop.pubkey);
713                         if source == self.node_id || target == self.node_id {
714                                 continue;
715                         }
716
717                         let capacity_msat = network_graph.channels()
718                                 .get(&hop.short_channel_id)
719                                 .and_then(|channel| channel.as_directed_to(&target).map(|d| d.effective_capacity()))
720                                 .unwrap_or(EffectiveCapacity::Unknown)
721                                 .as_msat();
722
723                         self.channel_liquidities
724                                 .entry(hop.short_channel_id)
725                                 .or_insert_with(|| ChannelLiquidity::new())
726                                 .as_directed_mut(&source, &target, capacity_msat)
727                                 .successful(amount_msat);
728                 }
729         }
730 }
731
732 impl<G: Deref<Target = NetworkGraph>> Writeable for ProbabilisticScorer<G> {
733         #[inline]
734         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
735                 self._params.write(w)?;
736                 self.node_id.write(w)?;
737                 self.channel_liquidities.write(w)?;
738                 write_tlv_fields!(w, {});
739                 Ok(())
740         }
741 }
742
743 impl<G: Deref<Target = NetworkGraph>> ReadableArgs<G> for ProbabilisticScorer<G> {
744         #[inline]
745         fn read<R: Read>(r: &mut R, args: G) -> Result<Self, DecodeError> {
746                 let res = Ok(Self {
747                         _params: Readable::read(r)?,
748                         node_id: Readable::read(r)?,
749                         network_graph: args,
750                         channel_liquidities: Readable::read(r)?,
751                 });
752                 read_tlv_fields!(r, {});
753                 res
754         }
755 }
756
757 impl Writeable for ChannelLiquidity {
758         #[inline]
759         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
760                 write_tlv_fields!(w, {
761                         (0, self.min_liquidity_offset_msat, required),
762                         (2, self.max_liquidity_offset_msat, required),
763                 });
764                 Ok(())
765         }
766 }
767
768 impl Readable for ChannelLiquidity {
769         #[inline]
770         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
771                 let mut min_liquidity_offset_msat = 0;
772                 let mut max_liquidity_offset_msat = 0;
773                 read_tlv_fields!(r, {
774                         (0, min_liquidity_offset_msat, required),
775                         (2, max_liquidity_offset_msat, required),
776                 });
777                 Ok(Self {
778                         min_liquidity_offset_msat,
779                         max_liquidity_offset_msat
780                 })
781         }
782 }
783
784 pub(crate) mod time {
785         use core::ops::Sub;
786         use core::time::Duration;
787         /// A measurement of time.
788         pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
789                 /// Returns an instance corresponding to the current moment.
790                 fn now() -> Self;
791
792                 /// Returns the amount of time elapsed since `self` was created.
793                 fn elapsed(&self) -> Duration;
794
795                 /// Returns the amount of time passed since the beginning of [`Time`].
796                 ///
797                 /// Used during (de-)serialization.
798                 fn duration_since_epoch() -> Duration;
799         }
800
801         /// A state in which time has no meaning.
802         #[derive(Debug, PartialEq, Eq)]
803         pub struct Eternity;
804
805         #[cfg(not(feature = "no-std"))]
806         impl Time for std::time::Instant {
807                 fn now() -> Self {
808                         std::time::Instant::now()
809                 }
810
811                 fn duration_since_epoch() -> Duration {
812                         use std::time::SystemTime;
813                         SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
814                 }
815
816                 fn elapsed(&self) -> Duration {
817                         std::time::Instant::elapsed(self)
818                 }
819         }
820
821         impl Time for Eternity {
822                 fn now() -> Self {
823                         Self
824                 }
825
826                 fn duration_since_epoch() -> Duration {
827                         Duration::from_secs(0)
828                 }
829
830                 fn elapsed(&self) -> Duration {
831                         Duration::from_secs(0)
832                 }
833         }
834
835         impl Sub<Duration> for Eternity {
836                 type Output = Self;
837
838                 fn sub(self, _other: Duration) -> Self {
839                         self
840                 }
841         }
842 }
843
844 pub(crate) use self::time::Time;
845
846 #[cfg(test)]
847 mod tests {
848         use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorer, ScoringParameters, ScorerUsingTime, Time};
849         use super::time::Eternity;
850
851         use ln::features::{ChannelFeatures, NodeFeatures};
852         use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
853         use routing::scoring::Score;
854         use routing::network_graph::{NetworkGraph, NodeId};
855         use routing::router::RouteHop;
856         use util::ser::{Readable, Writeable};
857
858         use bitcoin::blockdata::constants::genesis_block;
859         use bitcoin::hashes::Hash;
860         use bitcoin::hashes::sha256d::Hash as Sha256dHash;
861         use bitcoin::network::constants::Network;
862         use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
863         use core::cell::Cell;
864         use core::ops::Sub;
865         use core::time::Duration;
866         use io;
867
868         // `Time` tests
869
870         /// Time that can be advanced manually in tests.
871         #[derive(Debug, PartialEq, Eq)]
872         struct SinceEpoch(Duration);
873
874         impl SinceEpoch {
875                 thread_local! {
876                         static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
877                 }
878
879                 fn advance(duration: Duration) {
880                         Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
881                 }
882         }
883
884         impl Time for SinceEpoch {
885                 fn now() -> Self {
886                         Self(Self::duration_since_epoch())
887                 }
888
889                 fn duration_since_epoch() -> Duration {
890                         Self::ELAPSED.with(|elapsed| elapsed.get())
891                 }
892
893                 fn elapsed(&self) -> Duration {
894                         Self::duration_since_epoch() - self.0
895                 }
896         }
897
898         impl Sub<Duration> for SinceEpoch {
899                 type Output = Self;
900
901                 fn sub(self, other: Duration) -> Self {
902                         Self(self.0 - other)
903                 }
904         }
905
906         #[test]
907         fn time_passes_when_advanced() {
908                 let now = SinceEpoch::now();
909                 assert_eq!(now.elapsed(), Duration::from_secs(0));
910
911                 SinceEpoch::advance(Duration::from_secs(1));
912                 SinceEpoch::advance(Duration::from_secs(1));
913
914                 let elapsed = now.elapsed();
915                 let later = SinceEpoch::now();
916
917                 assert_eq!(elapsed, Duration::from_secs(2));
918                 assert_eq!(later - elapsed, now);
919         }
920
921         #[test]
922         fn time_never_passes_in_an_eternity() {
923                 let now = Eternity::now();
924                 let elapsed = now.elapsed();
925                 let later = Eternity::now();
926
927                 assert_eq!(now.elapsed(), Duration::from_secs(0));
928                 assert_eq!(later - elapsed, now);
929         }
930
931         // `Scorer` tests
932
933         /// A scorer for testing with time that can be manually advanced.
934         type Scorer = ScorerUsingTime::<SinceEpoch>;
935
936         fn source_privkey() -> SecretKey {
937                 SecretKey::from_slice(&[42; 32]).unwrap()
938         }
939
940         fn target_privkey() -> SecretKey {
941                 SecretKey::from_slice(&[43; 32]).unwrap()
942         }
943
944         fn source_pubkey() -> PublicKey {
945                 let secp_ctx = Secp256k1::new();
946                 PublicKey::from_secret_key(&secp_ctx, &source_privkey())
947         }
948
949         fn target_pubkey() -> PublicKey {
950                 let secp_ctx = Secp256k1::new();
951                 PublicKey::from_secret_key(&secp_ctx, &target_privkey())
952         }
953
954         fn source_node_id() -> NodeId {
955                 NodeId::from_pubkey(&source_pubkey())
956         }
957
958         fn target_node_id() -> NodeId {
959                 NodeId::from_pubkey(&target_pubkey())
960         }
961
962         #[test]
963         fn penalizes_without_channel_failures() {
964                 let scorer = Scorer::new(ScoringParameters {
965                         base_penalty_msat: 1_000,
966                         failure_penalty_msat: 512,
967                         failure_penalty_half_life: Duration::from_secs(1),
968                         overuse_penalty_start_1024th: 1024,
969                         overuse_penalty_msat_per_1024th: 0,
970                 });
971                 let source = source_node_id();
972                 let target = target_node_id();
973                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
974
975                 SinceEpoch::advance(Duration::from_secs(1));
976                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
977         }
978
979         #[test]
980         fn accumulates_channel_failure_penalties() {
981                 let mut scorer = Scorer::new(ScoringParameters {
982                         base_penalty_msat: 1_000,
983                         failure_penalty_msat: 64,
984                         failure_penalty_half_life: Duration::from_secs(10),
985                         overuse_penalty_start_1024th: 1024,
986                         overuse_penalty_msat_per_1024th: 0,
987                 });
988                 let source = source_node_id();
989                 let target = target_node_id();
990                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
991
992                 scorer.payment_path_failed(&[], 42);
993                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
994
995                 scorer.payment_path_failed(&[], 42);
996                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
997
998                 scorer.payment_path_failed(&[], 42);
999                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_192);
1000         }
1001
1002         #[test]
1003         fn decays_channel_failure_penalties_over_time() {
1004                 let mut scorer = Scorer::new(ScoringParameters {
1005                         base_penalty_msat: 1_000,
1006                         failure_penalty_msat: 512,
1007                         failure_penalty_half_life: Duration::from_secs(10),
1008                         overuse_penalty_start_1024th: 1024,
1009                         overuse_penalty_msat_per_1024th: 0,
1010                 });
1011                 let source = source_node_id();
1012                 let target = target_node_id();
1013                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1014
1015                 scorer.payment_path_failed(&[], 42);
1016                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1017
1018                 SinceEpoch::advance(Duration::from_secs(9));
1019                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1020
1021                 SinceEpoch::advance(Duration::from_secs(1));
1022                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1023
1024                 SinceEpoch::advance(Duration::from_secs(10 * 8));
1025                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_001);
1026
1027                 SinceEpoch::advance(Duration::from_secs(10));
1028                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1029
1030                 SinceEpoch::advance(Duration::from_secs(10));
1031                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1032         }
1033
1034         #[test]
1035         fn decays_channel_failure_penalties_without_shift_overflow() {
1036                 let mut scorer = Scorer::new(ScoringParameters {
1037                         base_penalty_msat: 1_000,
1038                         failure_penalty_msat: 512,
1039                         failure_penalty_half_life: Duration::from_secs(10),
1040                         overuse_penalty_start_1024th: 1024,
1041                         overuse_penalty_msat_per_1024th: 0,
1042                 });
1043                 let source = source_node_id();
1044                 let target = target_node_id();
1045                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1046
1047                 scorer.payment_path_failed(&[], 42);
1048                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1049
1050                 // An unchecked right shift 64 bits or more in ChannelFailure::decayed_penalty_msat would
1051                 // cause an overflow.
1052                 SinceEpoch::advance(Duration::from_secs(10 * 64));
1053                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1054
1055                 SinceEpoch::advance(Duration::from_secs(10));
1056                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1057         }
1058
1059         #[test]
1060         fn accumulates_channel_failure_penalties_after_decay() {
1061                 let mut scorer = Scorer::new(ScoringParameters {
1062                         base_penalty_msat: 1_000,
1063                         failure_penalty_msat: 512,
1064                         failure_penalty_half_life: Duration::from_secs(10),
1065                         overuse_penalty_start_1024th: 1024,
1066                         overuse_penalty_msat_per_1024th: 0,
1067                 });
1068                 let source = source_node_id();
1069                 let target = target_node_id();
1070                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1071
1072                 scorer.payment_path_failed(&[], 42);
1073                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1074
1075                 SinceEpoch::advance(Duration::from_secs(10));
1076                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1077
1078                 scorer.payment_path_failed(&[], 42);
1079                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_768);
1080
1081                 SinceEpoch::advance(Duration::from_secs(10));
1082                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_384);
1083         }
1084
1085         #[test]
1086         fn reduces_channel_failure_penalties_after_success() {
1087                 let mut scorer = Scorer::new(ScoringParameters {
1088                         base_penalty_msat: 1_000,
1089                         failure_penalty_msat: 512,
1090                         failure_penalty_half_life: Duration::from_secs(10),
1091                         overuse_penalty_start_1024th: 1024,
1092                         overuse_penalty_msat_per_1024th: 0,
1093                 });
1094                 let source = source_node_id();
1095                 let target = target_node_id();
1096                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1097
1098                 scorer.payment_path_failed(&[], 42);
1099                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1100
1101                 SinceEpoch::advance(Duration::from_secs(10));
1102                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1103
1104                 let hop = RouteHop {
1105                         pubkey: PublicKey::from_slice(target.as_slice()).unwrap(),
1106                         node_features: NodeFeatures::known(),
1107                         short_channel_id: 42,
1108                         channel_features: ChannelFeatures::known(),
1109                         fee_msat: 1,
1110                         cltv_expiry_delta: 18,
1111                 };
1112                 scorer.payment_path_successful(&[&hop]);
1113                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1114
1115                 SinceEpoch::advance(Duration::from_secs(10));
1116                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1117         }
1118
1119         #[test]
1120         fn restores_persisted_channel_failure_penalties() {
1121                 let mut scorer = Scorer::new(ScoringParameters {
1122                         base_penalty_msat: 1_000,
1123                         failure_penalty_msat: 512,
1124                         failure_penalty_half_life: Duration::from_secs(10),
1125                         overuse_penalty_start_1024th: 1024,
1126                         overuse_penalty_msat_per_1024th: 0,
1127                 });
1128                 let source = source_node_id();
1129                 let target = target_node_id();
1130
1131                 scorer.payment_path_failed(&[], 42);
1132                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1133
1134                 SinceEpoch::advance(Duration::from_secs(10));
1135                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1136
1137                 scorer.payment_path_failed(&[], 43);
1138                 assert_eq!(scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1139
1140                 let mut serialized_scorer = Vec::new();
1141                 scorer.write(&mut serialized_scorer).unwrap();
1142
1143                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1144                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1145                 assert_eq!(deserialized_scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1146         }
1147
1148         #[test]
1149         fn decays_persisted_channel_failure_penalties() {
1150                 let mut scorer = Scorer::new(ScoringParameters {
1151                         base_penalty_msat: 1_000,
1152                         failure_penalty_msat: 512,
1153                         failure_penalty_half_life: Duration::from_secs(10),
1154                         overuse_penalty_start_1024th: 1024,
1155                         overuse_penalty_msat_per_1024th: 0,
1156                 });
1157                 let source = source_node_id();
1158                 let target = target_node_id();
1159
1160                 scorer.payment_path_failed(&[], 42);
1161                 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1162
1163                 let mut serialized_scorer = Vec::new();
1164                 scorer.write(&mut serialized_scorer).unwrap();
1165
1166                 SinceEpoch::advance(Duration::from_secs(10));
1167
1168                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1169                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1170
1171                 SinceEpoch::advance(Duration::from_secs(10));
1172                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1173         }
1174
1175         #[test]
1176         fn charges_per_1024th_penalty() {
1177                 let scorer = Scorer::new(ScoringParameters {
1178                         base_penalty_msat: 0,
1179                         failure_penalty_msat: 0,
1180                         failure_penalty_half_life: Duration::from_secs(0),
1181                         overuse_penalty_start_1024th: 256,
1182                         overuse_penalty_msat_per_1024th: 100,
1183                 });
1184                 let source = source_node_id();
1185                 let target = target_node_id();
1186
1187                 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 1_024_000, &source, &target), 0);
1188                 assert_eq!(scorer.channel_penalty_msat(42, 256_999, 1_024_000, &source, &target), 0);
1189                 assert_eq!(scorer.channel_penalty_msat(42, 257_000, 1_024_000, &source, &target), 100);
1190                 assert_eq!(scorer.channel_penalty_msat(42, 258_000, 1_024_000, &source, &target), 200);
1191                 assert_eq!(scorer.channel_penalty_msat(42, 512_000, 1_024_000, &source, &target), 256 * 100);
1192         }
1193
1194         // `ProbabilisticScorer` tests
1195
1196         fn sender_privkey() -> SecretKey {
1197                 SecretKey::from_slice(&[41; 32]).unwrap()
1198         }
1199
1200         fn recipient_privkey() -> SecretKey {
1201                 SecretKey::from_slice(&[45; 32]).unwrap()
1202         }
1203
1204         fn sender_pubkey() -> PublicKey {
1205                 let secp_ctx = Secp256k1::new();
1206                 PublicKey::from_secret_key(&secp_ctx, &sender_privkey())
1207         }
1208
1209         fn recipient_pubkey() -> PublicKey {
1210                 let secp_ctx = Secp256k1::new();
1211                 PublicKey::from_secret_key(&secp_ctx, &recipient_privkey())
1212         }
1213
1214         fn sender_node_id() -> NodeId {
1215                 NodeId::from_pubkey(&sender_pubkey())
1216         }
1217
1218         fn recipient_node_id() -> NodeId {
1219                 NodeId::from_pubkey(&recipient_pubkey())
1220         }
1221
1222         fn network_graph() -> NetworkGraph {
1223                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1224                 let mut network_graph = NetworkGraph::new(genesis_hash);
1225                 add_channel(&mut network_graph, 41, sender_privkey(), source_privkey());
1226                 add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
1227                 add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
1228
1229                 network_graph
1230         }
1231
1232         fn add_channel(
1233                 network_graph: &mut NetworkGraph, short_channel_id: u64, node_1_key: SecretKey,
1234                 node_2_key: SecretKey
1235         ) {
1236                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1237                 let node_1_secret = &SecretKey::from_slice(&[39; 32]).unwrap();
1238                 let node_2_secret = &SecretKey::from_slice(&[40; 32]).unwrap();
1239                 let secp_ctx = Secp256k1::new();
1240                 let unsigned_announcement = UnsignedChannelAnnouncement {
1241                         features: ChannelFeatures::known(),
1242                         chain_hash: genesis_hash,
1243                         short_channel_id,
1244                         node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_key),
1245                         node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_key),
1246                         bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_secret),
1247                         bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_secret),
1248                         excess_data: Vec::new(),
1249                 };
1250                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1251                 let signed_announcement = ChannelAnnouncement {
1252                         node_signature_1: secp_ctx.sign(&msghash, &node_1_key),
1253                         node_signature_2: secp_ctx.sign(&msghash, &node_2_key),
1254                         bitcoin_signature_1: secp_ctx.sign(&msghash, &node_1_secret),
1255                         bitcoin_signature_2: secp_ctx.sign(&msghash, &node_2_secret),
1256                         contents: unsigned_announcement,
1257                 };
1258                 let chain_source: Option<&::util::test_utils::TestChainSource> = None;
1259                 network_graph.update_channel_from_announcement(
1260                         &signed_announcement, &chain_source, &secp_ctx).unwrap();
1261                 update_channel(network_graph, short_channel_id, node_1_key, 0);
1262                 update_channel(network_graph, short_channel_id, node_2_key, 1);
1263         }
1264
1265         fn update_channel(
1266                 network_graph: &mut NetworkGraph, short_channel_id: u64, node_key: SecretKey, flags: u8
1267         ) {
1268                 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1269                 let secp_ctx = Secp256k1::new();
1270                 let unsigned_update = UnsignedChannelUpdate {
1271                         chain_hash: genesis_hash,
1272                         short_channel_id,
1273                         timestamp: 100,
1274                         flags,
1275                         cltv_expiry_delta: 18,
1276                         htlc_minimum_msat: 0,
1277                         htlc_maximum_msat: OptionalField::Present(1_000),
1278                         fee_base_msat: 1,
1279                         fee_proportional_millionths: 0,
1280                         excess_data: Vec::new(),
1281                 };
1282                 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
1283                 let signed_update = ChannelUpdate {
1284                         signature: secp_ctx.sign(&msghash, &node_key),
1285                         contents: unsigned_update,
1286                 };
1287                 network_graph.update_channel(&signed_update, &secp_ctx).unwrap();
1288         }
1289
1290         fn payment_path(amount_msat: u64) -> Vec<RouteHop> {
1291                 vec![
1292                         RouteHop {
1293                                 pubkey: source_pubkey(),
1294                                 node_features: NodeFeatures::known(),
1295                                 short_channel_id: 41,
1296                                 channel_features: ChannelFeatures::known(),
1297                                 fee_msat: 1,
1298                                 cltv_expiry_delta: 18,
1299                         },
1300                         RouteHop {
1301                                 pubkey: target_pubkey(),
1302                                 node_features: NodeFeatures::known(),
1303                                 short_channel_id: 42,
1304                                 channel_features: ChannelFeatures::known(),
1305                                 fee_msat: 2,
1306                                 cltv_expiry_delta: 18,
1307                         },
1308                         RouteHop {
1309                                 pubkey: recipient_pubkey(),
1310                                 node_features: NodeFeatures::known(),
1311                                 short_channel_id: 43,
1312                                 channel_features: ChannelFeatures::known(),
1313                                 fee_msat: amount_msat,
1314                                 cltv_expiry_delta: 18,
1315                         },
1316                 ]
1317         }
1318
1319         #[test]
1320         fn liquidity_bounds_directed_from_lowest_node_id() {
1321                 let network_graph = network_graph();
1322                 let params = ProbabilisticScoringParameters::default();
1323                 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1324                         .with_channel(42,
1325                                 ChannelLiquidity {
1326                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1327                                 })
1328                         .with_channel(43,
1329                                 ChannelLiquidity {
1330                                         min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1331                                 });
1332                 let source = source_node_id();
1333                 let target = target_node_id();
1334                 let recipient = recipient_node_id();
1335
1336                 let liquidity = scorer.channel_liquidities.get_mut(&42).unwrap();
1337                 assert!(source > target);
1338                 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 100);
1339                 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1340                 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1341                 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 900);
1342
1343                 liquidity.as_directed_mut(&source, &target, 1_000).set_min_liquidity_msat(200);
1344                 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 200);
1345                 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1346                 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1347                 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 800);
1348
1349                 let liquidity = scorer.channel_liquidities.get_mut(&43).unwrap();
1350                 assert!(target < recipient);
1351                 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 700);
1352                 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 900);
1353                 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 100);
1354                 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 300);
1355
1356                 liquidity.as_directed_mut(&target, &recipient, 1_000).set_max_liquidity_msat(200);
1357                 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 0);
1358                 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 200);
1359                 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 800);
1360                 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 1000);
1361         }
1362
1363         #[test]
1364         fn increased_penalty_nearing_liquidity_upper_bound() {
1365                 let network_graph = network_graph();
1366                 let params = ProbabilisticScoringParameters::default();
1367                 let scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph);
1368                 let source = source_node_id();
1369                 let target = target_node_id();
1370
1371                 assert_eq!(scorer.channel_penalty_msat(42, 100, 100_000, &source, &target), 0);
1372                 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 100_000, &source, &target), 4);
1373                 assert_eq!(scorer.channel_penalty_msat(42, 10_000, 100_000, &source, &target), 457);
1374                 assert_eq!(scorer.channel_penalty_msat(42, 100_000, 100_000, &source, &target), 500_000);
1375
1376                 assert_eq!(scorer.channel_penalty_msat(42, 125, 1_000, &source, &target), 7);
1377                 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 31);
1378                 assert_eq!(scorer.channel_penalty_msat(42, 375, 1_000, &source, &target), 76);
1379                 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 150);
1380                 assert_eq!(scorer.channel_penalty_msat(42, 625, 1_000, &source, &target), 265);
1381                 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 450);
1382                 assert_eq!(scorer.channel_penalty_msat(42, 875, 1_000, &source, &target), 787);
1383         }
1384
1385         #[test]
1386         fn constant_penalty_outside_liquidity_bounds() {
1387                 let network_graph = network_graph();
1388                 let params = ProbabilisticScoringParameters::default();
1389                 let scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1390                         .with_channel(42,
1391                                 ChannelLiquidity { min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40 });
1392                 let source = source_node_id();
1393                 let target = target_node_id();
1394
1395                 assert_eq!(scorer.channel_penalty_msat(42, 39, 100, &source, &target), 0);
1396                 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 0);
1397                 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), u64::max_value());
1398                 assert_eq!(scorer.channel_penalty_msat(42, 61, 100, &source, &target), u64::max_value());
1399         }
1400
1401         #[test]
1402         fn reduces_liquidity_upper_bound_on_success() {
1403                 let network_graph = network_graph();
1404                 let params = ProbabilisticScoringParameters::default();
1405                 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1406                         .with_channel(42,
1407                                 ChannelLiquidity { min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 0 })
1408                         .with_channel(43,
1409                                 ChannelLiquidity { min_liquidity_offset_msat: 0, max_liquidity_offset_msat: 400 });
1410                 let sender = sender_node_id();
1411                 let source = source_node_id();
1412                 let target = target_node_id();
1413                 let recipient = recipient_node_id();
1414                 let path = payment_path(200);
1415
1416                 assert_eq!(scorer.channel_penalty_msat(41, 200, 1_000, &sender, &source), 0);
1417                 assert_eq!(scorer.channel_penalty_msat(42, 200, 1_000, &source, &target), 94);
1418                 assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 35);
1419
1420                 scorer.payment_path_successful(&path.iter().collect::<Vec<_>>());
1421
1422                 assert_eq!(scorer.channel_penalty_msat(41, 200, 1_000, &sender, &source), 0);
1423                 assert_eq!(scorer.channel_penalty_msat(42, 200, 1_000, &source, &target), u64::max_value());
1424                 assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 59);
1425         }
1426
1427         // TODO: Add more test coverage
1428 }