1 // This file is Copyright its original authors, visible in version control
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
10 //! Utilities for scoring payment channels.
12 //! [`ProbabilisticScorer`] may be given to [`find_route`] to score payment channels during path
13 //! finding when a custom [`Score`] implementation is not needed.
18 //! # extern crate secp256k1;
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;
26 //! # struct FakeLogger {};
27 //! # impl Logger for FakeLogger {
28 //! # fn log(&self, record: &Record) { unimplemented!() }
30 //! # fn find_scored_route(payer: PublicKey, route_params: RouteParameters, network_graph: NetworkGraph) {
31 //! # let logger = FakeLogger {};
33 //! // Use the default channel penalties.
34 //! let params = ProbabilisticScoringParameters::default();
35 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
37 //! // Or use custom channel penalties.
38 //! let params = ProbabilisticScoringParameters {
39 //! liquidity_penalty_multiplier_msat: 2 * 1000,
40 //! ..ProbabilisticScoringParameters::default()
42 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
44 //! let route = find_route(&payer, &route_params, &network_graph, None, &logger, &scorer);
50 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
51 //! different types and thus is undefined.
53 //! [`find_route`]: crate::routing::router::find_route
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};
61 use core::cell::{RefCell, RefMut};
62 use core::ops::{Deref, DerefMut};
63 use core::time::Duration;
65 use sync::{Mutex, MutexGuard};
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`.
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.
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`.
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;
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);
96 /// Handles updating channel penalties after successfully routing along a path.
97 fn payment_path_successful(&mut self, path: &[&RouteHop]);
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)
105 fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
106 self.deref_mut().payment_path_failed(path, short_channel_id)
109 fn payment_path_successful(&mut self, path: &[&RouteHop]) {
110 self.deref_mut().payment_path_successful(path)
116 define_score!(Writeable);
117 #[cfg(not(c_bindings))]
120 /// A scorer that is accessed under a lock.
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.
127 /// [`find_route`]: crate::routing::router::find_route
128 pub trait LockableScore<'a> {
129 /// The locked [`Score`] type.
130 type Locked: 'a + Score;
132 /// Returns the locked scorer.
133 fn lock(&'a self) -> Self::Locked;
137 impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
138 type Locked = MutexGuard<'a, T>;
140 fn lock(&'a self) -> MutexGuard<'a, T> {
141 Mutex::lock(self).unwrap()
145 impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
146 type Locked = RefMut<'a, T>;
148 fn lock(&'a self) -> RefMut<'a, T> {
154 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
155 pub struct MultiThreadedLockableScore<S: Score> {
160 impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore<T> {
161 type Locked = MutexGuard<'a, T>;
163 fn lock(&'a self) -> MutexGuard<'a, T> {
164 Mutex::lock(&self.score).unwrap()
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) }
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)
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)
192 /// [`Score`] implementation that uses a fixed penalty.
193 pub struct FixedPenaltyScorer {
197 impl_writeable_tlv_based!(FixedPenaltyScorer, {
198 (0, penalty_msat, required),
201 impl FixedPenaltyScorer {
202 /// Creates a new scorer using `penalty_msat`.
203 pub fn with_penalty(penalty_msat: u64) -> Self {
204 Self { penalty_msat }
208 impl Score for FixedPenaltyScorer {
209 fn channel_penalty_msat(&self, _: u64, _: u64, _: u64, _: &NodeId, _: &NodeId) -> u64 {
213 fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
215 fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
218 #[cfg(not(feature = "no-std"))]
219 /// [`Score`] implementation that provides reasonable default behavior.
221 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
222 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
224 /// See [module-level documentation] for usage and [`ScoringParameters`] for customization.
228 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
231 /// [module-level documentation]: crate::routing::scoring
234 note = "ProbabilisticScorer should be used instead of Scorer.",
236 pub type Scorer = ScorerUsingTime::<std::time::Instant>;
237 #[cfg(feature = "no-std")]
238 /// [`Score`] implementation that provides reasonable default behavior.
240 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
241 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
243 /// See [module-level documentation] for usage and [`ScoringParameters`] for customization.
247 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
250 /// [module-level documentation]: crate::routing::scoring
251 pub type Scorer = ScorerUsingTime::<time::Eternity>;
253 // Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
254 // doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
257 /// [`Score`] implementation.
259 /// (C-not exported) generally all users should use the [`Scorer`] type alias.
260 pub struct ScorerUsingTime<T: Time> {
261 params: ScoringParameters,
262 // TODO: Remove entries of closed channels.
263 channel_failures: HashMap<u64, ChannelFailure<T>>,
266 /// Parameters for configuring [`Scorer`].
267 pub struct ScoringParameters {
268 /// A fixed penalty in msats to apply to each channel.
270 /// Default value: 500 msat
271 pub base_penalty_msat: u64,
273 /// A penalty in msats to apply to a channel upon failing to relay a payment.
275 /// This accumulates for each failure but may be reduced over time based on
276 /// [`failure_penalty_half_life`] or when successfully routing through a channel.
278 /// Default value: 1,024,000 msat
280 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
281 pub failure_penalty_msat: u64,
283 /// When the amount being sent over a channel is this many 1024ths of the total channel
284 /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
286 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
288 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
289 pub overuse_penalty_start_1024th: u16,
291 /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
292 /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
294 /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
295 /// to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
297 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
298 pub overuse_penalty_msat_per_1024th: u64,
300 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
303 /// Successfully routing through a channel will immediately cut the penalty in half as well.
305 /// Default value: 1 hour
309 /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
312 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
313 pub failure_penalty_half_life: Duration,
316 impl_writeable_tlv_based!(ScoringParameters, {
317 (0, base_penalty_msat, required),
318 (1, overuse_penalty_start_1024th, (default_value, 128)),
319 (2, failure_penalty_msat, required),
320 (3, overuse_penalty_msat_per_1024th, (default_value, 20)),
321 (4, failure_penalty_half_life, required),
324 /// Accounting for penalties against a channel for failing to relay any payments.
326 /// Penalties decay over time, though accumulate as more failures occur.
327 struct ChannelFailure<T: Time> {
328 /// Accumulated penalty in msats for the channel as of `last_updated`.
329 undecayed_penalty_msat: u64,
331 /// Last time the channel either failed to route or successfully routed a payment. Used to decay
332 /// `undecayed_penalty_msat`.
336 impl<T: Time> ScorerUsingTime<T> {
337 /// Creates a new scorer using the given scoring parameters.
338 pub fn new(params: ScoringParameters) -> Self {
341 channel_failures: HashMap::new(),
346 impl<T: Time> ChannelFailure<T> {
347 fn new(failure_penalty_msat: u64) -> Self {
349 undecayed_penalty_msat: failure_penalty_msat,
350 last_updated: T::now(),
354 fn add_penalty(&mut self, failure_penalty_msat: u64, half_life: Duration) {
355 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) + failure_penalty_msat;
356 self.last_updated = T::now();
359 fn reduce_penalty(&mut self, half_life: Duration) {
360 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) >> 1;
361 self.last_updated = T::now();
364 fn decayed_penalty_msat(&self, half_life: Duration) -> u64 {
365 self.last_updated.elapsed().as_secs()
366 .checked_div(half_life.as_secs())
367 .and_then(|decays| self.undecayed_penalty_msat.checked_shr(decays as u32))
372 impl<T: Time> Default for ScorerUsingTime<T> {
373 fn default() -> Self {
374 Self::new(ScoringParameters::default())
378 impl Default for ScoringParameters {
379 fn default() -> Self {
381 base_penalty_msat: 500,
382 failure_penalty_msat: 1024 * 1000,
383 failure_penalty_half_life: Duration::from_secs(3600),
384 overuse_penalty_start_1024th: 1024 / 8,
385 overuse_penalty_msat_per_1024th: 20,
390 impl<T: Time> Score for ScorerUsingTime<T> {
391 fn channel_penalty_msat(
392 &self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, _source: &NodeId, _target: &NodeId
394 let failure_penalty_msat = self.channel_failures
395 .get(&short_channel_id)
396 .map_or(0, |value| value.decayed_penalty_msat(self.params.failure_penalty_half_life));
398 let mut penalty_msat = self.params.base_penalty_msat + failure_penalty_msat;
399 let send_1024ths = send_amt_msat.checked_mul(1024).unwrap_or(u64::max_value()) / capacity_msat;
400 if send_1024ths > self.params.overuse_penalty_start_1024th as u64 {
401 penalty_msat = penalty_msat.checked_add(
402 (send_1024ths - self.params.overuse_penalty_start_1024th as u64)
403 .checked_mul(self.params.overuse_penalty_msat_per_1024th).unwrap_or(u64::max_value()))
404 .unwrap_or(u64::max_value());
410 fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
411 let failure_penalty_msat = self.params.failure_penalty_msat;
412 let half_life = self.params.failure_penalty_half_life;
413 self.channel_failures
414 .entry(short_channel_id)
415 .and_modify(|failure| failure.add_penalty(failure_penalty_msat, half_life))
416 .or_insert_with(|| ChannelFailure::new(failure_penalty_msat));
419 fn payment_path_successful(&mut self, path: &[&RouteHop]) {
420 let half_life = self.params.failure_penalty_half_life;
421 for hop in path.iter() {
422 self.channel_failures
423 .entry(hop.short_channel_id)
424 .and_modify(|failure| failure.reduce_penalty(half_life));
429 impl<T: Time> Writeable for ScorerUsingTime<T> {
431 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
432 self.params.write(w)?;
433 self.channel_failures.write(w)?;
434 write_tlv_fields!(w, {});
439 impl<T: Time> Readable for ScorerUsingTime<T> {
441 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
443 params: Readable::read(r)?,
444 channel_failures: Readable::read(r)?,
446 read_tlv_fields!(r, {});
451 impl<T: Time> Writeable for ChannelFailure<T> {
453 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
454 let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
455 write_tlv_fields!(w, {
456 (0, self.undecayed_penalty_msat, required),
457 (2, duration_since_epoch, required),
463 impl<T: Time> Readable for ChannelFailure<T> {
465 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
466 let mut undecayed_penalty_msat = 0;
467 let mut duration_since_epoch = Duration::from_secs(0);
468 read_tlv_fields!(r, {
469 (0, undecayed_penalty_msat, required),
470 (2, duration_since_epoch, required),
473 undecayed_penalty_msat,
474 last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
479 #[cfg(not(feature = "no-std"))]
480 /// [`Score`] implementation using channel success probability distributions.
482 /// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
483 /// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
484 /// distributions are defined based on knowledge learned from successful and unsuccessful attempts.
485 /// Then the negative `log10` of the success probability is used to determine the cost of routing a
486 /// specific HTLC amount through a channel.
488 /// Knowledge about channel liquidity balances takes the form of upper and lower bounds on the
489 /// possible liquidity. Certainty of the bounds is decreased over time using a decay function. See
490 /// [`ProbabilisticScoringParameters`] for details.
492 /// Since the scorer aims to learn the current channel liquidity balances, it works best for nodes
493 /// with high payment volume or that actively probe the [`NetworkGraph`]. Nodes with low payment
494 /// volume are more likely to experience failed payment paths, which would need to be retried.
498 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
501 /// [1]: https://arxiv.org/abs/2107.05322
502 pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, std::time::Instant>;
503 #[cfg(feature = "no-std")]
504 /// [`Score`] implementation using channel success probability distributions.
506 /// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
507 /// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
508 /// distributions are defined based on knowledge learned from successful and unsuccessful attempts.
509 /// Then the negative `log10` of the success probability is used to determine the cost of routing a
510 /// specific HTLC amount through a channel.
512 /// Knowledge about channel liquidity balances takes the form of upper and lower bounds on the
513 /// possible liquidity. Certainty of the bounds is decreased over time using a decay function. See
514 /// [`ProbabilisticScoringParameters`] for details.
516 /// Since the scorer aims to learn the current channel liquidity balances, it works best for nodes
517 /// with high payment volume or that actively probe the [`NetworkGraph`]. Nodes with low payment
518 /// volume are more likely to experience failed payment paths, which would need to be retried.
522 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
525 /// [1]: https://arxiv.org/abs/2107.05322
526 pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, time::Eternity>;
528 /// Probabilistic [`Score`] implementation.
530 /// (C-not exported) generally all users should use the [`ProbabilisticScorer`] type alias.
531 pub struct ProbabilisticScorerUsingTime<G: Deref<Target = NetworkGraph>, T: Time> {
532 params: ProbabilisticScoringParameters,
534 // TODO: Remove entries of closed channels.
535 channel_liquidities: HashMap<u64, ChannelLiquidity<T>>,
538 /// Parameters for configuring [`ProbabilisticScorer`].
539 #[derive(Clone, Copy)]
540 pub struct ProbabilisticScoringParameters {
541 /// A multiplier used to determine the amount in msats willing to be paid to avoid routing
542 /// through a channel, as per multiplying by the negative `log10` of the channel's success
543 /// probability for a payment.
545 /// The success probability is determined by the effective channel capacity, the payment amount,
546 /// and knowledge learned from prior successful and unsuccessful payments. The lower bound of
547 /// the success probability is 0.01, effectively limiting the penalty to the range
548 /// `0..=2*liquidity_penalty_multiplier_msat`. The knowledge learned is decayed over time based
549 /// on [`liquidity_offset_half_life`].
551 /// Default value: 10,000 msat
553 /// [`liquidity_offset_half_life`]: Self::liquidity_offset_half_life
554 pub liquidity_penalty_multiplier_msat: u64,
556 /// The time required to elapse before any knowledge learned about channel liquidity balances is
559 /// The bounds are defined in terms of offsets and are initially zero. Increasing the offsets
560 /// gives tighter bounds on the channel liquidity balance. Thus, halving the offsets decreases
561 /// the certainty of the channel liquidity balance.
563 /// Default value: 1 hour
567 /// When built with the `no-std` feature, time will never elapse. Therefore, the channel
568 /// liquidity knowledge will never decay except when the bounds cross.
569 pub liquidity_offset_half_life: Duration,
572 impl_writeable_tlv_based!(ProbabilisticScoringParameters, {
573 (0, liquidity_penalty_multiplier_msat, required),
574 (2, liquidity_offset_half_life, required),
577 /// Accounting for channel liquidity balance uncertainty.
579 /// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
580 /// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
581 /// offset fields gives the opposite direction.
582 struct ChannelLiquidity<T: Time> {
583 /// Lower channel liquidity bound in terms of an offset from zero.
584 min_liquidity_offset_msat: u64,
586 /// Upper channel liquidity bound in terms of an offset from the effective capacity.
587 max_liquidity_offset_msat: u64,
589 /// Time when the liquidity bounds were last modified.
593 /// A snapshot of [`ChannelLiquidity`] in one direction assuming a certain channel capacity and
594 /// decayed with a given half life.
595 struct DirectedChannelLiquidity<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> {
596 min_liquidity_offset_msat: L,
597 max_liquidity_offset_msat: L,
604 impl<G: Deref<Target = NetworkGraph>, T: Time> ProbabilisticScorerUsingTime<G, T> {
605 /// Creates a new scorer using the given scoring parameters for sending payments from a node
606 /// through a network graph.
607 pub fn new(params: ProbabilisticScoringParameters, network_graph: G) -> Self {
611 channel_liquidities: HashMap::new(),
616 fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity<T>) -> Self {
617 assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
622 impl Default for ProbabilisticScoringParameters {
623 fn default() -> Self {
625 liquidity_penalty_multiplier_msat: 10_000,
626 liquidity_offset_half_life: Duration::from_secs(3600),
631 impl<T: Time> ChannelLiquidity<T> {
635 min_liquidity_offset_msat: 0,
636 max_liquidity_offset_msat: 0,
637 last_updated: T::now(),
641 /// Returns a view of the channel liquidity directed from `source` to `target` assuming
644 &self, source: &NodeId, target: &NodeId, capacity_msat: u64, half_life: Duration
645 ) -> DirectedChannelLiquidity<&u64, T, &T> {
646 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
647 (&self.min_liquidity_offset_msat, &self.max_liquidity_offset_msat)
649 (&self.max_liquidity_offset_msat, &self.min_liquidity_offset_msat)
652 DirectedChannelLiquidity {
653 min_liquidity_offset_msat,
654 max_liquidity_offset_msat,
656 last_updated: &self.last_updated,
662 /// Returns a mutable view of the channel liquidity directed from `source` to `target` assuming
665 &mut self, source: &NodeId, target: &NodeId, capacity_msat: u64, half_life: Duration
666 ) -> DirectedChannelLiquidity<&mut u64, T, &mut T> {
667 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
668 (&mut self.min_liquidity_offset_msat, &mut self.max_liquidity_offset_msat)
670 (&mut self.max_liquidity_offset_msat, &mut self.min_liquidity_offset_msat)
673 DirectedChannelLiquidity {
674 min_liquidity_offset_msat,
675 max_liquidity_offset_msat,
677 last_updated: &mut self.last_updated,
684 impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiquidity<L, T, U> {
685 /// Returns the success probability of routing the given HTLC `amount_msat` through the channel
686 /// in this direction.
687 fn success_probability(&self, amount_msat: u64) -> f64 {
688 let max_liquidity_msat = self.max_liquidity_msat();
689 let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
690 if amount_msat > max_liquidity_msat {
692 } else if amount_msat <= min_liquidity_msat {
695 let numerator = max_liquidity_msat + 1 - amount_msat;
696 let denominator = max_liquidity_msat + 1 - min_liquidity_msat;
697 numerator as f64 / denominator as f64
698 }.max(0.01) // Lower bound the success probability to ensure some channel is selected.
701 /// Returns the lower bound of the channel liquidity balance in this direction.
702 fn min_liquidity_msat(&self) -> u64 {
703 self.decayed_offset_msat(*self.min_liquidity_offset_msat)
706 /// Returns the upper bound of the channel liquidity balance in this direction.
707 fn max_liquidity_msat(&self) -> u64 {
709 .checked_sub(self.decayed_offset_msat(*self.max_liquidity_offset_msat))
713 fn decayed_offset_msat(&self, offset_msat: u64) -> u64 {
714 self.now.duration_since(*self.last_updated).as_secs()
715 .checked_div(self.half_life.as_secs())
716 .and_then(|decays| offset_msat.checked_shr(decays as u32))
721 impl<L: DerefMut<Target = u64>, T: Time, U: DerefMut<Target = T>> DirectedChannelLiquidity<L, T, U> {
722 /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
723 fn failed_at_channel(&mut self, amount_msat: u64) {
724 if amount_msat < self.max_liquidity_msat() {
725 self.set_max_liquidity_msat(amount_msat);
729 /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
730 fn failed_downstream(&mut self, amount_msat: u64) {
731 if amount_msat > self.min_liquidity_msat() {
732 self.set_min_liquidity_msat(amount_msat);
736 /// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
737 fn successful(&mut self, amount_msat: u64) {
738 let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
739 self.set_max_liquidity_msat(max_liquidity_msat);
742 /// Adjusts the lower bound of the channel liquidity balance in this direction.
743 fn set_min_liquidity_msat(&mut self, amount_msat: u64) {
744 *self.min_liquidity_offset_msat = amount_msat;
745 *self.max_liquidity_offset_msat = if amount_msat > self.max_liquidity_msat() {
748 self.decayed_offset_msat(*self.max_liquidity_offset_msat)
750 *self.last_updated = self.now;
753 /// Adjusts the upper bound of the channel liquidity balance in this direction.
754 fn set_max_liquidity_msat(&mut self, amount_msat: u64) {
755 *self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
756 *self.min_liquidity_offset_msat = if amount_msat < self.min_liquidity_msat() {
759 self.decayed_offset_msat(*self.min_liquidity_offset_msat)
761 *self.last_updated = self.now;
765 impl<G: Deref<Target = NetworkGraph>, T: Time> Score for ProbabilisticScorerUsingTime<G, T> {
766 fn channel_penalty_msat(
767 &self, short_channel_id: u64, amount_msat: u64, capacity_msat: u64, source: &NodeId,
770 let liquidity_penalty_multiplier_msat = self.params.liquidity_penalty_multiplier_msat;
771 let liquidity_offset_half_life = self.params.liquidity_offset_half_life;
772 let success_probability = self.channel_liquidities
773 .get(&short_channel_id)
774 .unwrap_or(&ChannelLiquidity::new())
775 .as_directed(source, target, capacity_msat, liquidity_offset_half_life)
776 .success_probability(amount_msat);
777 // NOTE: If success_probability is ever changed to return 0.0, log10 is undefined so return
778 // u64::max_value instead.
779 debug_assert!(success_probability > core::f64::EPSILON);
780 (-(success_probability.log10()) * liquidity_penalty_multiplier_msat as f64) as u64
783 fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
784 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
785 let liquidity_offset_half_life = self.params.liquidity_offset_half_life;
786 let network_graph = self.network_graph.read_only();
788 let target = NodeId::from_pubkey(&hop.pubkey);
789 let channel_directed_from_source = network_graph.channels()
790 .get(&hop.short_channel_id)
791 .and_then(|channel| channel.as_directed_to(&target));
793 // Only score announced channels.
794 if let Some((channel, source)) = channel_directed_from_source {
795 let capacity_msat = channel.effective_capacity().as_msat();
796 if hop.short_channel_id == short_channel_id {
797 self.channel_liquidities
798 .entry(hop.short_channel_id)
799 .or_insert_with(ChannelLiquidity::new)
800 .as_directed_mut(source, &target, capacity_msat, liquidity_offset_half_life)
801 .failed_at_channel(amount_msat);
805 self.channel_liquidities
806 .entry(hop.short_channel_id)
807 .or_insert_with(ChannelLiquidity::new)
808 .as_directed_mut(source, &target, capacity_msat, liquidity_offset_half_life)
809 .failed_downstream(amount_msat);
814 fn payment_path_successful(&mut self, path: &[&RouteHop]) {
815 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
816 let liquidity_offset_half_life = self.params.liquidity_offset_half_life;
817 let network_graph = self.network_graph.read_only();
819 let target = NodeId::from_pubkey(&hop.pubkey);
820 let channel_directed_from_source = network_graph.channels()
821 .get(&hop.short_channel_id)
822 .and_then(|channel| channel.as_directed_to(&target));
824 // Only score announced channels.
825 if let Some((channel, source)) = channel_directed_from_source {
826 let capacity_msat = channel.effective_capacity().as_msat();
827 self.channel_liquidities
828 .entry(hop.short_channel_id)
829 .or_insert_with(ChannelLiquidity::new)
830 .as_directed_mut(source, &target, capacity_msat, liquidity_offset_half_life)
831 .successful(amount_msat);
837 impl<G: Deref<Target = NetworkGraph>, T: Time> Writeable for ProbabilisticScorerUsingTime<G, T> {
839 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
840 write_tlv_fields!(w, {
841 (0, self.channel_liquidities, required)
847 impl<G, T> ReadableArgs<(ProbabilisticScoringParameters, G)> for ProbabilisticScorerUsingTime<G, T>
849 G: Deref<Target = NetworkGraph>,
854 r: &mut R, args: (ProbabilisticScoringParameters, G)
855 ) -> Result<Self, DecodeError> {
856 let (params, network_graph) = args;
857 let mut channel_liquidities = HashMap::new();
858 read_tlv_fields!(r, {
859 (0, channel_liquidities, required)
869 impl<T: Time> Writeable for ChannelLiquidity<T> {
871 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
872 let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
873 write_tlv_fields!(w, {
874 (0, self.min_liquidity_offset_msat, required),
875 (2, self.max_liquidity_offset_msat, required),
876 (4, duration_since_epoch, required),
882 impl<T: Time> Readable for ChannelLiquidity<T> {
884 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
885 let mut min_liquidity_offset_msat = 0;
886 let mut max_liquidity_offset_msat = 0;
887 let mut duration_since_epoch = Duration::from_secs(0);
888 read_tlv_fields!(r, {
889 (0, min_liquidity_offset_msat, required),
890 (2, max_liquidity_offset_msat, required),
891 (4, duration_since_epoch, required),
894 min_liquidity_offset_msat,
895 max_liquidity_offset_msat,
896 last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
901 pub(crate) mod time {
903 use core::time::Duration;
904 /// A measurement of time.
905 pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
906 /// Returns an instance corresponding to the current moment.
909 /// Returns the amount of time elapsed since `self` was created.
910 fn elapsed(&self) -> Duration;
912 /// Returns the amount of time passed between `earlier` and `self`.
913 fn duration_since(&self, earlier: Self) -> Duration;
915 /// Returns the amount of time passed since the beginning of [`Time`].
917 /// Used during (de-)serialization.
918 fn duration_since_epoch() -> Duration;
921 /// A state in which time has no meaning.
922 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
925 #[cfg(not(feature = "no-std"))]
926 impl Time for std::time::Instant {
928 std::time::Instant::now()
931 fn duration_since(&self, earlier: Self) -> Duration {
932 self.duration_since(earlier)
935 fn duration_since_epoch() -> Duration {
936 use std::time::SystemTime;
937 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
940 fn elapsed(&self) -> Duration {
941 std::time::Instant::elapsed(self)
945 impl Time for Eternity {
950 fn duration_since(&self, _earlier: Self) -> Duration {
951 Duration::from_secs(0)
954 fn duration_since_epoch() -> Duration {
955 Duration::from_secs(0)
958 fn elapsed(&self) -> Duration {
959 Duration::from_secs(0)
963 impl Sub<Duration> for Eternity {
966 fn sub(self, _other: Duration) -> Self {
972 pub(crate) use self::time::Time;
976 use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime, Time};
977 use super::time::Eternity;
979 use ln::features::{ChannelFeatures, NodeFeatures};
980 use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
981 use routing::scoring::Score;
982 use routing::network_graph::{NetworkGraph, NodeId};
983 use routing::router::RouteHop;
984 use util::ser::{Readable, ReadableArgs, Writeable};
986 use bitcoin::blockdata::constants::genesis_block;
987 use bitcoin::hashes::Hash;
988 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
989 use bitcoin::network::constants::Network;
990 use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
991 use core::cell::Cell;
993 use core::time::Duration;
998 /// Time that can be advanced manually in tests.
999 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
1000 struct SinceEpoch(Duration);
1004 static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
1007 fn advance(duration: Duration) {
1008 Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
1012 impl Time for SinceEpoch {
1014 Self(Self::duration_since_epoch())
1017 fn duration_since(&self, earlier: Self) -> Duration {
1021 fn duration_since_epoch() -> Duration {
1022 Self::ELAPSED.with(|elapsed| elapsed.get())
1025 fn elapsed(&self) -> Duration {
1026 Self::duration_since_epoch() - self.0
1030 impl Sub<Duration> for SinceEpoch {
1033 fn sub(self, other: Duration) -> Self {
1034 Self(self.0 - other)
1039 fn time_passes_when_advanced() {
1040 let now = SinceEpoch::now();
1041 assert_eq!(now.elapsed(), Duration::from_secs(0));
1043 SinceEpoch::advance(Duration::from_secs(1));
1044 SinceEpoch::advance(Duration::from_secs(1));
1046 let elapsed = now.elapsed();
1047 let later = SinceEpoch::now();
1049 assert_eq!(elapsed, Duration::from_secs(2));
1050 assert_eq!(later - elapsed, now);
1054 fn time_never_passes_in_an_eternity() {
1055 let now = Eternity::now();
1056 let elapsed = now.elapsed();
1057 let later = Eternity::now();
1059 assert_eq!(now.elapsed(), Duration::from_secs(0));
1060 assert_eq!(later - elapsed, now);
1065 /// A scorer for testing with time that can be manually advanced.
1066 type Scorer = ScorerUsingTime::<SinceEpoch>;
1068 fn source_privkey() -> SecretKey {
1069 SecretKey::from_slice(&[42; 32]).unwrap()
1072 fn target_privkey() -> SecretKey {
1073 SecretKey::from_slice(&[43; 32]).unwrap()
1076 fn source_pubkey() -> PublicKey {
1077 let secp_ctx = Secp256k1::new();
1078 PublicKey::from_secret_key(&secp_ctx, &source_privkey())
1081 fn target_pubkey() -> PublicKey {
1082 let secp_ctx = Secp256k1::new();
1083 PublicKey::from_secret_key(&secp_ctx, &target_privkey())
1086 fn source_node_id() -> NodeId {
1087 NodeId::from_pubkey(&source_pubkey())
1090 fn target_node_id() -> NodeId {
1091 NodeId::from_pubkey(&target_pubkey())
1095 fn penalizes_without_channel_failures() {
1096 let scorer = Scorer::new(ScoringParameters {
1097 base_penalty_msat: 1_000,
1098 failure_penalty_msat: 512,
1099 failure_penalty_half_life: Duration::from_secs(1),
1100 overuse_penalty_start_1024th: 1024,
1101 overuse_penalty_msat_per_1024th: 0,
1103 let source = source_node_id();
1104 let target = target_node_id();
1105 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1107 SinceEpoch::advance(Duration::from_secs(1));
1108 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1112 fn accumulates_channel_failure_penalties() {
1113 let mut scorer = Scorer::new(ScoringParameters {
1114 base_penalty_msat: 1_000,
1115 failure_penalty_msat: 64,
1116 failure_penalty_half_life: Duration::from_secs(10),
1117 overuse_penalty_start_1024th: 1024,
1118 overuse_penalty_msat_per_1024th: 0,
1120 let source = source_node_id();
1121 let target = target_node_id();
1122 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1124 scorer.payment_path_failed(&[], 42);
1125 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1127 scorer.payment_path_failed(&[], 42);
1128 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1130 scorer.payment_path_failed(&[], 42);
1131 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_192);
1135 fn decays_channel_failure_penalties_over_time() {
1136 let mut scorer = Scorer::new(ScoringParameters {
1137 base_penalty_msat: 1_000,
1138 failure_penalty_msat: 512,
1139 failure_penalty_half_life: Duration::from_secs(10),
1140 overuse_penalty_start_1024th: 1024,
1141 overuse_penalty_msat_per_1024th: 0,
1143 let source = source_node_id();
1144 let target = target_node_id();
1145 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1147 scorer.payment_path_failed(&[], 42);
1148 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1150 SinceEpoch::advance(Duration::from_secs(9));
1151 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1153 SinceEpoch::advance(Duration::from_secs(1));
1154 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1156 SinceEpoch::advance(Duration::from_secs(10 * 8));
1157 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_001);
1159 SinceEpoch::advance(Duration::from_secs(10));
1160 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1162 SinceEpoch::advance(Duration::from_secs(10));
1163 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1167 fn decays_channel_failure_penalties_without_shift_overflow() {
1168 let mut scorer = Scorer::new(ScoringParameters {
1169 base_penalty_msat: 1_000,
1170 failure_penalty_msat: 512,
1171 failure_penalty_half_life: Duration::from_secs(10),
1172 overuse_penalty_start_1024th: 1024,
1173 overuse_penalty_msat_per_1024th: 0,
1175 let source = source_node_id();
1176 let target = target_node_id();
1177 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1179 scorer.payment_path_failed(&[], 42);
1180 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1182 // An unchecked right shift 64 bits or more in ChannelFailure::decayed_penalty_msat would
1183 // cause an overflow.
1184 SinceEpoch::advance(Duration::from_secs(10 * 64));
1185 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1187 SinceEpoch::advance(Duration::from_secs(10));
1188 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1192 fn accumulates_channel_failure_penalties_after_decay() {
1193 let mut scorer = Scorer::new(ScoringParameters {
1194 base_penalty_msat: 1_000,
1195 failure_penalty_msat: 512,
1196 failure_penalty_half_life: Duration::from_secs(10),
1197 overuse_penalty_start_1024th: 1024,
1198 overuse_penalty_msat_per_1024th: 0,
1200 let source = source_node_id();
1201 let target = target_node_id();
1202 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1204 scorer.payment_path_failed(&[], 42);
1205 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1207 SinceEpoch::advance(Duration::from_secs(10));
1208 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1210 scorer.payment_path_failed(&[], 42);
1211 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_768);
1213 SinceEpoch::advance(Duration::from_secs(10));
1214 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_384);
1218 fn reduces_channel_failure_penalties_after_success() {
1219 let mut scorer = Scorer::new(ScoringParameters {
1220 base_penalty_msat: 1_000,
1221 failure_penalty_msat: 512,
1222 failure_penalty_half_life: Duration::from_secs(10),
1223 overuse_penalty_start_1024th: 1024,
1224 overuse_penalty_msat_per_1024th: 0,
1226 let source = source_node_id();
1227 let target = target_node_id();
1228 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1230 scorer.payment_path_failed(&[], 42);
1231 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1233 SinceEpoch::advance(Duration::from_secs(10));
1234 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1236 let hop = RouteHop {
1237 pubkey: PublicKey::from_slice(target.as_slice()).unwrap(),
1238 node_features: NodeFeatures::known(),
1239 short_channel_id: 42,
1240 channel_features: ChannelFeatures::known(),
1242 cltv_expiry_delta: 18,
1244 scorer.payment_path_successful(&[&hop]);
1245 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1247 SinceEpoch::advance(Duration::from_secs(10));
1248 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1252 fn restores_persisted_channel_failure_penalties() {
1253 let mut scorer = Scorer::new(ScoringParameters {
1254 base_penalty_msat: 1_000,
1255 failure_penalty_msat: 512,
1256 failure_penalty_half_life: Duration::from_secs(10),
1257 overuse_penalty_start_1024th: 1024,
1258 overuse_penalty_msat_per_1024th: 0,
1260 let source = source_node_id();
1261 let target = target_node_id();
1263 scorer.payment_path_failed(&[], 42);
1264 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1266 SinceEpoch::advance(Duration::from_secs(10));
1267 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1269 scorer.payment_path_failed(&[], 43);
1270 assert_eq!(scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1272 let mut serialized_scorer = Vec::new();
1273 scorer.write(&mut serialized_scorer).unwrap();
1275 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1276 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1277 assert_eq!(deserialized_scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1281 fn decays_persisted_channel_failure_penalties() {
1282 let mut scorer = Scorer::new(ScoringParameters {
1283 base_penalty_msat: 1_000,
1284 failure_penalty_msat: 512,
1285 failure_penalty_half_life: Duration::from_secs(10),
1286 overuse_penalty_start_1024th: 1024,
1287 overuse_penalty_msat_per_1024th: 0,
1289 let source = source_node_id();
1290 let target = target_node_id();
1292 scorer.payment_path_failed(&[], 42);
1293 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1295 let mut serialized_scorer = Vec::new();
1296 scorer.write(&mut serialized_scorer).unwrap();
1298 SinceEpoch::advance(Duration::from_secs(10));
1300 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1301 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1303 SinceEpoch::advance(Duration::from_secs(10));
1304 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1308 fn charges_per_1024th_penalty() {
1309 let scorer = Scorer::new(ScoringParameters {
1310 base_penalty_msat: 0,
1311 failure_penalty_msat: 0,
1312 failure_penalty_half_life: Duration::from_secs(0),
1313 overuse_penalty_start_1024th: 256,
1314 overuse_penalty_msat_per_1024th: 100,
1316 let source = source_node_id();
1317 let target = target_node_id();
1319 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 1_024_000, &source, &target), 0);
1320 assert_eq!(scorer.channel_penalty_msat(42, 256_999, 1_024_000, &source, &target), 0);
1321 assert_eq!(scorer.channel_penalty_msat(42, 257_000, 1_024_000, &source, &target), 100);
1322 assert_eq!(scorer.channel_penalty_msat(42, 258_000, 1_024_000, &source, &target), 200);
1323 assert_eq!(scorer.channel_penalty_msat(42, 512_000, 1_024_000, &source, &target), 256 * 100);
1326 // `ProbabilisticScorer` tests
1328 /// A probabilistic scorer for testing with time that can be manually advanced.
1329 type ProbabilisticScorer<'a> = ProbabilisticScorerUsingTime::<&'a NetworkGraph, SinceEpoch>;
1331 fn sender_privkey() -> SecretKey {
1332 SecretKey::from_slice(&[41; 32]).unwrap()
1335 fn recipient_privkey() -> SecretKey {
1336 SecretKey::from_slice(&[45; 32]).unwrap()
1339 fn sender_pubkey() -> PublicKey {
1340 let secp_ctx = Secp256k1::new();
1341 PublicKey::from_secret_key(&secp_ctx, &sender_privkey())
1344 fn recipient_pubkey() -> PublicKey {
1345 let secp_ctx = Secp256k1::new();
1346 PublicKey::from_secret_key(&secp_ctx, &recipient_privkey())
1349 fn sender_node_id() -> NodeId {
1350 NodeId::from_pubkey(&sender_pubkey())
1353 fn recipient_node_id() -> NodeId {
1354 NodeId::from_pubkey(&recipient_pubkey())
1357 fn network_graph() -> NetworkGraph {
1358 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1359 let mut network_graph = NetworkGraph::new(genesis_hash);
1360 add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
1361 add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
1367 network_graph: &mut NetworkGraph, short_channel_id: u64, node_1_key: SecretKey,
1368 node_2_key: SecretKey
1370 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1371 let node_1_secret = &SecretKey::from_slice(&[39; 32]).unwrap();
1372 let node_2_secret = &SecretKey::from_slice(&[40; 32]).unwrap();
1373 let secp_ctx = Secp256k1::new();
1374 let unsigned_announcement = UnsignedChannelAnnouncement {
1375 features: ChannelFeatures::known(),
1376 chain_hash: genesis_hash,
1378 node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_key),
1379 node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_key),
1380 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_secret),
1381 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_secret),
1382 excess_data: Vec::new(),
1384 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1385 let signed_announcement = ChannelAnnouncement {
1386 node_signature_1: secp_ctx.sign(&msghash, &node_1_key),
1387 node_signature_2: secp_ctx.sign(&msghash, &node_2_key),
1388 bitcoin_signature_1: secp_ctx.sign(&msghash, &node_1_secret),
1389 bitcoin_signature_2: secp_ctx.sign(&msghash, &node_2_secret),
1390 contents: unsigned_announcement,
1392 let chain_source: Option<&::util::test_utils::TestChainSource> = None;
1393 network_graph.update_channel_from_announcement(
1394 &signed_announcement, &chain_source, &secp_ctx).unwrap();
1395 update_channel(network_graph, short_channel_id, node_1_key, 0);
1396 update_channel(network_graph, short_channel_id, node_2_key, 1);
1400 network_graph: &mut NetworkGraph, short_channel_id: u64, node_key: SecretKey, flags: u8
1402 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1403 let secp_ctx = Secp256k1::new();
1404 let unsigned_update = UnsignedChannelUpdate {
1405 chain_hash: genesis_hash,
1409 cltv_expiry_delta: 18,
1410 htlc_minimum_msat: 0,
1411 htlc_maximum_msat: OptionalField::Present(1_000),
1413 fee_proportional_millionths: 0,
1414 excess_data: Vec::new(),
1416 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
1417 let signed_update = ChannelUpdate {
1418 signature: secp_ctx.sign(&msghash, &node_key),
1419 contents: unsigned_update,
1421 network_graph.update_channel(&signed_update, &secp_ctx).unwrap();
1424 fn payment_path_for_amount(amount_msat: u64) -> Vec<RouteHop> {
1427 pubkey: source_pubkey(),
1428 node_features: NodeFeatures::known(),
1429 short_channel_id: 41,
1430 channel_features: ChannelFeatures::known(),
1432 cltv_expiry_delta: 18,
1435 pubkey: target_pubkey(),
1436 node_features: NodeFeatures::known(),
1437 short_channel_id: 42,
1438 channel_features: ChannelFeatures::known(),
1440 cltv_expiry_delta: 18,
1443 pubkey: recipient_pubkey(),
1444 node_features: NodeFeatures::known(),
1445 short_channel_id: 43,
1446 channel_features: ChannelFeatures::known(),
1447 fee_msat: amount_msat,
1448 cltv_expiry_delta: 18,
1454 fn liquidity_bounds_directed_from_lowest_node_id() {
1455 let last_updated = SinceEpoch::now();
1456 let network_graph = network_graph();
1457 let params = ProbabilisticScoringParameters::default();
1458 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1461 min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100, last_updated
1465 min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100, last_updated
1467 let source = source_node_id();
1468 let target = target_node_id();
1469 let recipient = recipient_node_id();
1470 assert!(source > target);
1471 assert!(target < recipient);
1473 // Update minimum liquidity.
1475 let liquidity_offset_half_life = scorer.params.liquidity_offset_half_life;
1476 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1477 .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1478 assert_eq!(liquidity.min_liquidity_msat(), 100);
1479 assert_eq!(liquidity.max_liquidity_msat(), 300);
1481 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1482 .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1483 assert_eq!(liquidity.min_liquidity_msat(), 700);
1484 assert_eq!(liquidity.max_liquidity_msat(), 900);
1486 scorer.channel_liquidities.get_mut(&42).unwrap()
1487 .as_directed_mut(&source, &target, 1_000, liquidity_offset_half_life)
1488 .set_min_liquidity_msat(200);
1490 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1491 .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1492 assert_eq!(liquidity.min_liquidity_msat(), 200);
1493 assert_eq!(liquidity.max_liquidity_msat(), 300);
1495 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1496 .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1497 assert_eq!(liquidity.min_liquidity_msat(), 700);
1498 assert_eq!(liquidity.max_liquidity_msat(), 800);
1500 // Update maximum liquidity.
1502 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1503 .as_directed(&target, &recipient, 1_000, liquidity_offset_half_life);
1504 assert_eq!(liquidity.min_liquidity_msat(), 700);
1505 assert_eq!(liquidity.max_liquidity_msat(), 900);
1507 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1508 .as_directed(&recipient, &target, 1_000, liquidity_offset_half_life);
1509 assert_eq!(liquidity.min_liquidity_msat(), 100);
1510 assert_eq!(liquidity.max_liquidity_msat(), 300);
1512 scorer.channel_liquidities.get_mut(&43).unwrap()
1513 .as_directed_mut(&target, &recipient, 1_000, liquidity_offset_half_life)
1514 .set_max_liquidity_msat(200);
1516 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1517 .as_directed(&target, &recipient, 1_000, liquidity_offset_half_life);
1518 assert_eq!(liquidity.min_liquidity_msat(), 0);
1519 assert_eq!(liquidity.max_liquidity_msat(), 200);
1521 let liquidity = scorer.channel_liquidities.get(&43).unwrap()
1522 .as_directed(&recipient, &target, 1_000, liquidity_offset_half_life);
1523 assert_eq!(liquidity.min_liquidity_msat(), 800);
1524 assert_eq!(liquidity.max_liquidity_msat(), 1000);
1528 fn resets_liquidity_upper_bound_when_crossed_by_lower_bound() {
1529 let last_updated = SinceEpoch::now();
1530 let network_graph = network_graph();
1531 let params = ProbabilisticScoringParameters::default();
1532 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1535 min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400, last_updated
1537 let source = source_node_id();
1538 let target = target_node_id();
1539 assert!(source > target);
1541 // Check initial bounds.
1542 let liquidity_offset_half_life = scorer.params.liquidity_offset_half_life;
1543 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1544 .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1545 assert_eq!(liquidity.min_liquidity_msat(), 400);
1546 assert_eq!(liquidity.max_liquidity_msat(), 800);
1548 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1549 .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1550 assert_eq!(liquidity.min_liquidity_msat(), 200);
1551 assert_eq!(liquidity.max_liquidity_msat(), 600);
1553 // Reset from source to target.
1554 scorer.channel_liquidities.get_mut(&42).unwrap()
1555 .as_directed_mut(&source, &target, 1_000, liquidity_offset_half_life)
1556 .set_min_liquidity_msat(900);
1558 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1559 .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1560 assert_eq!(liquidity.min_liquidity_msat(), 900);
1561 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1563 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1564 .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1565 assert_eq!(liquidity.min_liquidity_msat(), 0);
1566 assert_eq!(liquidity.max_liquidity_msat(), 100);
1568 // Reset from target to source.
1569 scorer.channel_liquidities.get_mut(&42).unwrap()
1570 .as_directed_mut(&target, &source, 1_000, liquidity_offset_half_life)
1571 .set_min_liquidity_msat(400);
1573 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1574 .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1575 assert_eq!(liquidity.min_liquidity_msat(), 0);
1576 assert_eq!(liquidity.max_liquidity_msat(), 600);
1578 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1579 .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1580 assert_eq!(liquidity.min_liquidity_msat(), 400);
1581 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1585 fn resets_liquidity_lower_bound_when_crossed_by_upper_bound() {
1586 let last_updated = SinceEpoch::now();
1587 let network_graph = network_graph();
1588 let params = ProbabilisticScoringParameters::default();
1589 let mut scorer = ProbabilisticScorer::new(params, &network_graph)
1592 min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400, last_updated
1594 let source = source_node_id();
1595 let target = target_node_id();
1596 assert!(source > target);
1598 // Check initial bounds.
1599 let liquidity_offset_half_life = scorer.params.liquidity_offset_half_life;
1600 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1601 .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1602 assert_eq!(liquidity.min_liquidity_msat(), 400);
1603 assert_eq!(liquidity.max_liquidity_msat(), 800);
1605 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1606 .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1607 assert_eq!(liquidity.min_liquidity_msat(), 200);
1608 assert_eq!(liquidity.max_liquidity_msat(), 600);
1610 // Reset from source to target.
1611 scorer.channel_liquidities.get_mut(&42).unwrap()
1612 .as_directed_mut(&source, &target, 1_000, liquidity_offset_half_life)
1613 .set_max_liquidity_msat(300);
1615 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1616 .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1617 assert_eq!(liquidity.min_liquidity_msat(), 0);
1618 assert_eq!(liquidity.max_liquidity_msat(), 300);
1620 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1621 .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1622 assert_eq!(liquidity.min_liquidity_msat(), 700);
1623 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1625 // Reset from target to source.
1626 scorer.channel_liquidities.get_mut(&42).unwrap()
1627 .as_directed_mut(&target, &source, 1_000, liquidity_offset_half_life)
1628 .set_max_liquidity_msat(600);
1630 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1631 .as_directed(&source, &target, 1_000, liquidity_offset_half_life);
1632 assert_eq!(liquidity.min_liquidity_msat(), 400);
1633 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1635 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1636 .as_directed(&target, &source, 1_000, liquidity_offset_half_life);
1637 assert_eq!(liquidity.min_liquidity_msat(), 0);
1638 assert_eq!(liquidity.max_liquidity_msat(), 600);
1642 fn increased_penalty_nearing_liquidity_upper_bound() {
1643 let network_graph = network_graph();
1644 let params = ProbabilisticScoringParameters {
1645 liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1647 let scorer = ProbabilisticScorer::new(params, &network_graph);
1648 let source = source_node_id();
1649 let target = target_node_id();
1651 assert_eq!(scorer.channel_penalty_msat(42, 100, 100_000, &source, &target), 0);
1652 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 100_000, &source, &target), 4);
1653 assert_eq!(scorer.channel_penalty_msat(42, 10_000, 100_000, &source, &target), 45);
1654 assert_eq!(scorer.channel_penalty_msat(42, 100_000, 100_000, &source, &target), 2_000);
1656 assert_eq!(scorer.channel_penalty_msat(42, 125, 1_000, &source, &target), 57);
1657 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1658 assert_eq!(scorer.channel_penalty_msat(42, 375, 1_000, &source, &target), 203);
1659 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1660 assert_eq!(scorer.channel_penalty_msat(42, 625, 1_000, &source, &target), 425);
1661 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1662 assert_eq!(scorer.channel_penalty_msat(42, 875, 1_000, &source, &target), 900);
1666 fn constant_penalty_outside_liquidity_bounds() {
1667 let last_updated = SinceEpoch::now();
1668 let network_graph = network_graph();
1669 let params = ProbabilisticScoringParameters {
1670 liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1672 let scorer = ProbabilisticScorer::new(params, &network_graph)
1675 min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40, last_updated
1677 let source = source_node_id();
1678 let target = target_node_id();
1680 assert_eq!(scorer.channel_penalty_msat(42, 39, 100, &source, &target), 0);
1681 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 0);
1682 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 2_000);
1683 assert_eq!(scorer.channel_penalty_msat(42, 61, 100, &source, &target), 2_000);
1687 fn does_not_further_penalize_own_channel() {
1688 let network_graph = network_graph();
1689 let params = ProbabilisticScoringParameters {
1690 liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1692 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1693 let sender = sender_node_id();
1694 let source = source_node_id();
1695 let failed_path = payment_path_for_amount(500);
1696 let successful_path = payment_path_for_amount(200);
1698 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1700 scorer.payment_path_failed(&failed_path.iter().collect::<Vec<_>>(), 41);
1701 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1703 scorer.payment_path_successful(&successful_path.iter().collect::<Vec<_>>());
1704 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 300);
1708 fn sets_liquidity_lower_bound_on_downstream_failure() {
1709 let network_graph = network_graph();
1710 let params = ProbabilisticScoringParameters {
1711 liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1713 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1714 let source = source_node_id();
1715 let target = target_node_id();
1716 let path = payment_path_for_amount(500);
1718 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1719 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1720 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1722 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 43);
1724 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 0);
1725 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 0);
1726 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 300);
1730 fn sets_liquidity_upper_bound_on_failure() {
1731 let network_graph = network_graph();
1732 let params = ProbabilisticScoringParameters {
1733 liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1735 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1736 let source = source_node_id();
1737 let target = target_node_id();
1738 let path = payment_path_for_amount(500);
1740 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1741 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1742 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1744 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 42);
1746 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 300);
1747 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2_000);
1748 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 2_000);
1752 fn reduces_liquidity_upper_bound_along_path_on_success() {
1753 let network_graph = network_graph();
1754 let params = ProbabilisticScoringParameters {
1755 liquidity_penalty_multiplier_msat: 1_000, ..Default::default()
1757 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1758 let sender = sender_node_id();
1759 let source = source_node_id();
1760 let target = target_node_id();
1761 let recipient = recipient_node_id();
1762 let path = payment_path_for_amount(500);
1764 assert_eq!(scorer.channel_penalty_msat(41, 250, 1_000, &sender, &source), 124);
1765 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1766 assert_eq!(scorer.channel_penalty_msat(43, 250, 1_000, &target, &recipient), 124);
1768 scorer.payment_path_successful(&path.iter().collect::<Vec<_>>());
1770 assert_eq!(scorer.channel_penalty_msat(41, 250, 1_000, &sender, &source), 124);
1771 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 300);
1772 assert_eq!(scorer.channel_penalty_msat(43, 250, 1_000, &target, &recipient), 300);
1776 fn decays_liquidity_bounds_over_time() {
1777 let network_graph = network_graph();
1778 let params = ProbabilisticScoringParameters {
1779 liquidity_penalty_multiplier_msat: 1_000,
1780 liquidity_offset_half_life: Duration::from_secs(10),
1782 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1783 let source = source_node_id();
1784 let target = target_node_id();
1786 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1787 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1789 scorer.payment_path_failed(&payment_path_for_amount(768).iter().collect::<Vec<_>>(), 42);
1790 scorer.payment_path_failed(&payment_path_for_amount(128).iter().collect::<Vec<_>>(), 43);
1792 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 0);
1793 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 92);
1794 assert_eq!(scorer.channel_penalty_msat(42, 768, 1_024, &source, &target), 1_424);
1795 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), 2_000);
1797 SinceEpoch::advance(Duration::from_secs(9));
1798 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 0);
1799 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 92);
1800 assert_eq!(scorer.channel_penalty_msat(42, 768, 1_024, &source, &target), 1_424);
1801 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), 2_000);
1803 SinceEpoch::advance(Duration::from_secs(1));
1804 assert_eq!(scorer.channel_penalty_msat(42, 64, 1_024, &source, &target), 0);
1805 assert_eq!(scorer.channel_penalty_msat(42, 128, 1_024, &source, &target), 34);
1806 assert_eq!(scorer.channel_penalty_msat(42, 896, 1_024, &source, &target), 1_812);
1807 assert_eq!(scorer.channel_penalty_msat(42, 960, 1_024, &source, &target), 2_000);
1809 // Fully decay liquidity lower bound.
1810 SinceEpoch::advance(Duration::from_secs(10 * 7));
1811 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1812 assert_eq!(scorer.channel_penalty_msat(42, 1, 1_024, &source, &target), 0);
1813 assert_eq!(scorer.channel_penalty_msat(42, 1_023, 1_024, &source, &target), 2_000);
1814 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1816 // Fully decay liquidity upper bound.
1817 SinceEpoch::advance(Duration::from_secs(10));
1818 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1819 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1821 SinceEpoch::advance(Duration::from_secs(10));
1822 assert_eq!(scorer.channel_penalty_msat(42, 0, 1_024, &source, &target), 0);
1823 assert_eq!(scorer.channel_penalty_msat(42, 1_024, 1_024, &source, &target), 2_000);
1827 fn decays_liquidity_bounds_without_shift_overflow() {
1828 let network_graph = network_graph();
1829 let params = ProbabilisticScoringParameters {
1830 liquidity_penalty_multiplier_msat: 1_000,
1831 liquidity_offset_half_life: Duration::from_secs(10),
1833 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1834 let source = source_node_id();
1835 let target = target_node_id();
1836 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 124);
1838 scorer.payment_path_failed(&payment_path_for_amount(512).iter().collect::<Vec<_>>(), 42);
1839 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 281);
1841 // An unchecked right shift 64 bits or more in DirectedChannelLiquidity::decayed_offset_msat
1842 // would cause an overflow.
1843 SinceEpoch::advance(Duration::from_secs(10 * 64));
1844 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 124);
1846 SinceEpoch::advance(Duration::from_secs(10));
1847 assert_eq!(scorer.channel_penalty_msat(42, 256, 1_024, &source, &target), 124);
1851 fn restricts_liquidity_bounds_after_decay() {
1852 let network_graph = network_graph();
1853 let params = ProbabilisticScoringParameters {
1854 liquidity_penalty_multiplier_msat: 1_000,
1855 liquidity_offset_half_life: Duration::from_secs(10),
1857 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1858 let source = source_node_id();
1859 let target = target_node_id();
1861 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 300);
1863 // More knowledge gives higher confidence (256, 768), meaning a lower penalty.
1864 scorer.payment_path_failed(&payment_path_for_amount(768).iter().collect::<Vec<_>>(), 42);
1865 scorer.payment_path_failed(&payment_path_for_amount(256).iter().collect::<Vec<_>>(), 43);
1866 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 281);
1868 // Decaying knowledge gives less confidence (128, 896), meaning a higher penalty.
1869 SinceEpoch::advance(Duration::from_secs(10));
1870 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 293);
1872 // Reducing the upper bound gives more confidence (128, 832) that the payment amount (512)
1873 // is closer to the upper bound, meaning a higher penalty.
1874 scorer.payment_path_successful(&payment_path_for_amount(64).iter().collect::<Vec<_>>());
1875 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 333);
1877 // Increasing the lower bound gives more confidence (256, 832) that the payment amount (512)
1878 // is closer to the lower bound, meaning a lower penalty.
1879 scorer.payment_path_failed(&payment_path_for_amount(256).iter().collect::<Vec<_>>(), 43);
1880 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 247);
1882 // Further decaying affects the lower bound more than the upper bound (128, 928).
1883 SinceEpoch::advance(Duration::from_secs(10));
1884 assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 280);
1888 fn restores_persisted_liquidity_bounds() {
1889 let network_graph = network_graph();
1890 let params = ProbabilisticScoringParameters {
1891 liquidity_penalty_multiplier_msat: 1_000,
1892 liquidity_offset_half_life: Duration::from_secs(10),
1894 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1895 let source = source_node_id();
1896 let target = target_node_id();
1898 scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
1899 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2_000);
1901 SinceEpoch::advance(Duration::from_secs(10));
1902 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 475);
1904 scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
1905 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1907 let mut serialized_scorer = Vec::new();
1908 scorer.write(&mut serialized_scorer).unwrap();
1910 let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
1911 let deserialized_scorer =
1912 <ProbabilisticScorer>::read(&mut serialized_scorer, (params, &network_graph)).unwrap();
1913 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1917 fn decays_persisted_liquidity_bounds() {
1918 let network_graph = network_graph();
1919 let params = ProbabilisticScoringParameters {
1920 liquidity_penalty_multiplier_msat: 1_000,
1921 liquidity_offset_half_life: Duration::from_secs(10),
1923 let mut scorer = ProbabilisticScorer::new(params, &network_graph);
1924 let source = source_node_id();
1925 let target = target_node_id();
1927 scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
1928 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2_000);
1930 let mut serialized_scorer = Vec::new();
1931 scorer.write(&mut serialized_scorer).unwrap();
1933 SinceEpoch::advance(Duration::from_secs(10));
1935 let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
1936 let deserialized_scorer =
1937 <ProbabilisticScorer>::read(&mut serialized_scorer, (params, &network_graph)).unwrap();
1938 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 475);
1940 scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
1941 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1943 SinceEpoch::advance(Duration::from_secs(10));
1944 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 367);