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, payer, &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, payer, &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 bitcoin::secp256k1::key::PublicKey;
57 use ln::msgs::DecodeError;
58 use routing::network_graph::{EffectiveCapacity, NetworkGraph, NodeId};
59 use routing::router::RouteHop;
60 use util::ser::{Readable, ReadableArgs, Writeable, Writer};
63 use core::cell::{RefCell, RefMut};
64 use core::ops::{Deref, DerefMut};
65 use core::time::Duration;
67 use sync::{Mutex, MutexGuard};
69 /// We define Score ever-so-slightly differently based on whether we are being built for C bindings
70 /// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
71 /// no problem - you move a `Score` that implements `Writeable` into a `Mutex`, lock it, and now
72 /// you have the original, concrete, `Score` type, which presumably implements `Writeable`.
74 /// For C users, once you've moved the `Score` into a `LockableScore` all you have after locking it
75 /// is an opaque trait object with an opaque pointer with no type info. Users could take the unsafe
76 /// approach of blindly casting that opaque pointer to a concrete type and calling `Writeable` from
77 /// there, but other languages downstream of the C bindings (e.g. Java) can't even do that.
78 /// Instead, we really want `Score` and `LockableScore` to implement `Writeable` directly, which we
79 /// do here by defining `Score` differently for `cfg(c_bindings)`.
80 macro_rules! define_score { ($($supertrait: path)*) => {
81 /// An interface used to score payment channels for path finding.
83 /// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
84 pub trait Score $(: $supertrait)* {
85 /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
86 /// given channel in the direction from `source` to `target`.
88 /// The channel's capacity (less any other MPP parts that are also being considered for use in
89 /// the same payment) is given by `capacity_msat`. It may be determined from various sources
90 /// such as a chain data, network gossip, or invoice hints, the latter indicating sufficient
91 /// capacity (i.e., near [`u64::max_value`]). Thus, implementations should be overflow-safe.
92 fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &NodeId, target: &NodeId) -> u64;
94 /// Handles updating channel penalties after failing to route through a channel.
95 fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64);
97 /// Handles updating channel penalties after successfully routing along a path.
98 fn payment_path_successful(&mut self, path: &[&RouteHop]);
101 impl<S: Score, T: DerefMut<Target=S> $(+ $supertrait)*> Score for T {
102 fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &NodeId, target: &NodeId) -> u64 {
103 self.deref().channel_penalty_msat(short_channel_id, send_amt_msat, capacity_msat, source, target)
106 fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
107 self.deref_mut().payment_path_failed(path, short_channel_id)
110 fn payment_path_successful(&mut self, path: &[&RouteHop]) {
111 self.deref_mut().payment_path_successful(path)
117 define_score!(Writeable);
118 #[cfg(not(c_bindings))]
121 /// A scorer that is accessed under a lock.
123 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
124 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
125 /// implementations. Internal locking would be detrimental to route finding performance and could
126 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
128 /// [`find_route`]: crate::routing::router::find_route
129 pub trait LockableScore<'a> {
130 /// The locked [`Score`] type.
131 type Locked: 'a + Score;
133 /// Returns the locked scorer.
134 fn lock(&'a self) -> Self::Locked;
138 impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
139 type Locked = MutexGuard<'a, T>;
141 fn lock(&'a self) -> MutexGuard<'a, T> {
142 Mutex::lock(self).unwrap()
146 impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
147 type Locked = RefMut<'a, T>;
149 fn lock(&'a self) -> RefMut<'a, T> {
155 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
156 pub struct MultiThreadedLockableScore<S: Score> {
161 impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore<T> {
162 type Locked = MutexGuard<'a, T>;
164 fn lock(&'a self) -> MutexGuard<'a, T> {
165 Mutex::lock(&self.score).unwrap()
170 impl<T: Score> MultiThreadedLockableScore<T> {
171 /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
172 pub fn new(score: T) -> Self {
173 MultiThreadedLockableScore { score: Mutex::new(score) }
179 impl<'a, T: Writeable> Writeable for RefMut<'a, T> {
180 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
181 T::write(&**self, writer)
187 impl<'a, S: Writeable> Writeable for MutexGuard<'a, S> {
188 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
189 S::write(&**self, writer)
193 /// [`Score`] implementation that provides reasonable default behavior.
195 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
196 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
198 /// See [module-level documentation] for usage.
200 /// [module-level documentation]: crate::routing::scoring
201 #[cfg(not(feature = "no-std"))]
202 pub type Scorer = ScorerUsingTime::<std::time::Instant>;
203 /// [`Score`] implementation that provides reasonable default behavior.
205 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
206 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
208 /// See [module-level documentation] for usage and [`ScoringParameters`] for customization.
210 /// [module-level documentation]: crate::routing::scoring
211 #[cfg(feature = "no-std")]
212 pub type Scorer = ScorerUsingTime::<time::Eternity>;
214 // Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
215 // doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
218 /// [`Score`] implementation.
220 /// See [`Scorer`] for details.
224 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
227 /// (C-not exported) generally all users should use the [`Scorer`] type alias.
228 pub struct ScorerUsingTime<T: Time> {
229 params: ScoringParameters,
230 // TODO: Remove entries of closed channels.
231 channel_failures: HashMap<u64, ChannelFailure<T>>,
234 /// Parameters for configuring [`Scorer`].
235 pub struct ScoringParameters {
236 /// A fixed penalty in msats to apply to each channel.
238 /// Default value: 500 msat
239 pub base_penalty_msat: u64,
241 /// A penalty in msats to apply to a channel upon failing to relay a payment.
243 /// This accumulates for each failure but may be reduced over time based on
244 /// [`failure_penalty_half_life`] or when successfully routing through a channel.
246 /// Default value: 1,024,000 msat
248 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
249 pub failure_penalty_msat: u64,
251 /// When the amount being sent over a channel is this many 1024ths of the total channel
252 /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
254 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
256 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
257 pub overuse_penalty_start_1024th: u16,
259 /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
260 /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
262 /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
263 /// to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
265 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
266 pub overuse_penalty_msat_per_1024th: u64,
268 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
271 /// Successfully routing through a channel will immediately cut the penalty in half as well.
275 /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
278 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
279 pub failure_penalty_half_life: Duration,
282 impl_writeable_tlv_based!(ScoringParameters, {
283 (0, base_penalty_msat, required),
284 (1, overuse_penalty_start_1024th, (default_value, 128)),
285 (2, failure_penalty_msat, required),
286 (3, overuse_penalty_msat_per_1024th, (default_value, 20)),
287 (4, failure_penalty_half_life, required),
290 /// Accounting for penalties against a channel for failing to relay any payments.
292 /// Penalties decay over time, though accumulate as more failures occur.
293 struct ChannelFailure<T: Time> {
294 /// Accumulated penalty in msats for the channel as of `last_updated`.
295 undecayed_penalty_msat: u64,
297 /// Last time the channel either failed to route or successfully routed a payment. Used to decay
298 /// `undecayed_penalty_msat`.
302 impl<T: Time> ScorerUsingTime<T> {
303 /// Creates a new scorer using the given scoring parameters.
304 pub fn new(params: ScoringParameters) -> Self {
307 channel_failures: HashMap::new(),
311 /// Creates a new scorer using `penalty_msat` as a fixed channel penalty.
312 #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
313 pub fn with_fixed_penalty(penalty_msat: u64) -> Self {
314 Self::new(ScoringParameters {
315 base_penalty_msat: penalty_msat,
316 failure_penalty_msat: 0,
317 failure_penalty_half_life: Duration::from_secs(0),
318 overuse_penalty_start_1024th: 1024,
319 overuse_penalty_msat_per_1024th: 0,
324 impl<T: Time> ChannelFailure<T> {
325 fn new(failure_penalty_msat: u64) -> Self {
327 undecayed_penalty_msat: failure_penalty_msat,
328 last_updated: T::now(),
332 fn add_penalty(&mut self, failure_penalty_msat: u64, half_life: Duration) {
333 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) + failure_penalty_msat;
334 self.last_updated = T::now();
337 fn reduce_penalty(&mut self, half_life: Duration) {
338 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) >> 1;
339 self.last_updated = T::now();
342 fn decayed_penalty_msat(&self, half_life: Duration) -> u64 {
343 self.last_updated.elapsed().as_secs()
344 .checked_div(half_life.as_secs())
345 .and_then(|decays| self.undecayed_penalty_msat.checked_shr(decays as u32))
350 impl<T: Time> Default for ScorerUsingTime<T> {
351 fn default() -> Self {
352 Self::new(ScoringParameters::default())
356 impl Default for ScoringParameters {
357 fn default() -> Self {
359 base_penalty_msat: 500,
360 failure_penalty_msat: 1024 * 1000,
361 failure_penalty_half_life: Duration::from_secs(3600),
362 overuse_penalty_start_1024th: 1024 / 8,
363 overuse_penalty_msat_per_1024th: 20,
368 impl<T: Time> Score for ScorerUsingTime<T> {
369 fn channel_penalty_msat(
370 &self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, _source: &NodeId, _target: &NodeId
372 let failure_penalty_msat = self.channel_failures
373 .get(&short_channel_id)
374 .map_or(0, |value| value.decayed_penalty_msat(self.params.failure_penalty_half_life));
376 let mut penalty_msat = self.params.base_penalty_msat + failure_penalty_msat;
377 let send_1024ths = send_amt_msat.checked_mul(1024).unwrap_or(u64::max_value()) / capacity_msat;
378 if send_1024ths > self.params.overuse_penalty_start_1024th as u64 {
379 penalty_msat = penalty_msat.checked_add(
380 (send_1024ths - self.params.overuse_penalty_start_1024th as u64)
381 .checked_mul(self.params.overuse_penalty_msat_per_1024th).unwrap_or(u64::max_value()))
382 .unwrap_or(u64::max_value());
388 fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
389 let failure_penalty_msat = self.params.failure_penalty_msat;
390 let half_life = self.params.failure_penalty_half_life;
391 self.channel_failures
392 .entry(short_channel_id)
393 .and_modify(|failure| failure.add_penalty(failure_penalty_msat, half_life))
394 .or_insert_with(|| ChannelFailure::new(failure_penalty_msat));
397 fn payment_path_successful(&mut self, path: &[&RouteHop]) {
398 let half_life = self.params.failure_penalty_half_life;
399 for hop in path.iter() {
400 self.channel_failures
401 .entry(hop.short_channel_id)
402 .and_modify(|failure| failure.reduce_penalty(half_life));
407 impl<T: Time> Writeable for ScorerUsingTime<T> {
409 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
410 self.params.write(w)?;
411 self.channel_failures.write(w)?;
412 write_tlv_fields!(w, {});
417 impl<T: Time> Readable for ScorerUsingTime<T> {
419 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
421 params: Readable::read(r)?,
422 channel_failures: Readable::read(r)?,
424 read_tlv_fields!(r, {});
429 impl<T: Time> Writeable for ChannelFailure<T> {
431 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
432 let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
433 write_tlv_fields!(w, {
434 (0, self.undecayed_penalty_msat, required),
435 (2, duration_since_epoch, required),
441 impl<T: Time> Readable for ChannelFailure<T> {
443 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
444 let mut undecayed_penalty_msat = 0;
445 let mut duration_since_epoch = Duration::from_secs(0);
446 read_tlv_fields!(r, {
447 (0, undecayed_penalty_msat, required),
448 (2, duration_since_epoch, required),
451 undecayed_penalty_msat,
452 last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
457 /// [`Score`] implementation using channel success probability distributions.
459 /// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
460 /// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
461 /// distributions are defined based on knowledge learned from successful and unsuccessful attempts.
462 /// Then the negative log of the success probability is used to determine the cost of routing a
463 /// specific HTLC amount through a channel.
465 /// [1]: https://arxiv.org/abs/2107.05322
466 pub struct ProbabilisticScorer<G: Deref<Target = NetworkGraph>> {
467 params: ProbabilisticScoringParameters,
470 // TODO: Remove entries of closed channels.
471 channel_liquidities: HashMap<u64, ChannelLiquidity>,
474 /// Parameters for configuring [`ProbabilisticScorer`].
475 pub struct ProbabilisticScoringParameters {
476 /// A penalty applied after multiplying by the negative log of channel's success probability for
479 /// The success probability is determined by the effective channel capacity, the payment amount,
480 /// and knowledge learned from prior successful and unsuccessful payments.
482 /// Default value: 1,000 msat
483 pub liquidity_penalty_multiplier_msat: u64,
486 impl_writeable_tlv_based!(ProbabilisticScoringParameters, {
487 (0, liquidity_penalty_multiplier_msat, required),
490 /// Accounting for channel liquidity balance uncertainty.
492 /// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
493 /// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
494 /// offset fields gives the opposite direction.
495 struct ChannelLiquidity {
496 min_liquidity_offset_msat: u64,
497 max_liquidity_offset_msat: u64,
500 /// A view of [`ChannelLiquidity`] in one direction assuming a certain channel capacity.
501 struct DirectedChannelLiquidity<L: Deref<Target = u64>> {
502 min_liquidity_offset_msat: L,
503 max_liquidity_offset_msat: L,
507 /// The likelihood of an event occurring.
511 Ratio { numerator: u64, denominator: u64 },
514 impl<G: Deref<Target = NetworkGraph>> ProbabilisticScorer<G> {
515 /// Creates a new scorer using the given scoring parameters for sending payments from a node
516 /// through a network graph.
518 params: ProbabilisticScoringParameters, node_pubkey: PublicKey, network_graph: G
522 node_id: NodeId::from_pubkey(&node_pubkey),
524 channel_liquidities: HashMap::new(),
529 fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity) -> Self {
530 assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
535 impl Default for ProbabilisticScoringParameters {
536 fn default() -> Self {
538 liquidity_penalty_multiplier_msat: 1000,
543 impl ChannelLiquidity {
547 min_liquidity_offset_msat: 0,
548 max_liquidity_offset_msat: 0,
552 /// Returns a view of the channel liquidity directed from `source` to `target` assuming
555 &self, source: &NodeId, target: &NodeId, capacity_msat: u64
556 ) -> DirectedChannelLiquidity<&u64> {
557 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
558 (&self.min_liquidity_offset_msat, &self.max_liquidity_offset_msat)
560 (&self.max_liquidity_offset_msat, &self.min_liquidity_offset_msat)
563 DirectedChannelLiquidity {
564 min_liquidity_offset_msat,
565 max_liquidity_offset_msat,
570 /// Returns a mutable view of the channel liquidity directed from `source` to `target` assuming
573 &mut self, source: &NodeId, target: &NodeId, capacity_msat: u64
574 ) -> DirectedChannelLiquidity<&mut u64> {
575 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
576 (&mut self.min_liquidity_offset_msat, &mut self.max_liquidity_offset_msat)
578 (&mut self.max_liquidity_offset_msat, &mut self.min_liquidity_offset_msat)
581 DirectedChannelLiquidity {
582 min_liquidity_offset_msat,
583 max_liquidity_offset_msat,
589 impl<L: Deref<Target = u64>> DirectedChannelLiquidity<L> {
590 /// Returns the success probability of routing the given HTLC `amount_msat` through the channel
591 /// in this direction.
592 fn success_probability(&self, amount_msat: u64) -> Probability {
593 let max_liquidity_msat = self.max_liquidity_msat();
594 let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
595 if amount_msat > max_liquidity_msat {
597 } else if amount_msat < min_liquidity_msat {
600 let numerator = max_liquidity_msat + 1 - amount_msat;
601 let denominator = max_liquidity_msat + 1 - min_liquidity_msat;
602 if numerator == denominator {
605 Probability::Ratio { numerator, denominator }
610 /// Returns the lower bound of the channel liquidity balance in this direction.
611 fn min_liquidity_msat(&self) -> u64 {
612 *self.min_liquidity_offset_msat
615 /// Returns the upper bound of the channel liquidity balance in this direction.
616 fn max_liquidity_msat(&self) -> u64 {
617 self.capacity_msat.checked_sub(*self.max_liquidity_offset_msat).unwrap_or(0)
621 impl<L: DerefMut<Target = u64>> DirectedChannelLiquidity<L> {
622 /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
623 fn failed_at_channel(&mut self, amount_msat: u64) {
624 if amount_msat < self.max_liquidity_msat() {
625 self.set_max_liquidity_msat(amount_msat);
629 /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
630 fn failed_downstream(&mut self, amount_msat: u64) {
631 if amount_msat > self.min_liquidity_msat() {
632 self.set_min_liquidity_msat(amount_msat);
636 /// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
637 fn successful(&mut self, amount_msat: u64) {
638 let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
639 self.set_max_liquidity_msat(max_liquidity_msat);
642 /// Adjusts the lower bound of the channel liquidity balance in this direction.
643 fn set_min_liquidity_msat(&mut self, amount_msat: u64) {
644 *self.min_liquidity_offset_msat = amount_msat;
646 if amount_msat > self.max_liquidity_msat() {
647 *self.max_liquidity_offset_msat = 0;
651 /// Adjusts the upper bound of the channel liquidity balance in this direction.
652 fn set_max_liquidity_msat(&mut self, amount_msat: u64) {
653 *self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
655 if amount_msat < self.min_liquidity_msat() {
656 *self.min_liquidity_offset_msat = 0;
661 impl<G: Deref<Target = NetworkGraph>> Score for ProbabilisticScorer<G> {
662 fn channel_penalty_msat(
663 &self, short_channel_id: u64, amount_msat: u64, capacity_msat: u64, source: &NodeId,
666 if *source == self.node_id || *target == self.node_id {
670 let liquidity_penalty_multiplier_msat = self.params.liquidity_penalty_multiplier_msat;
671 let success_probability = self.channel_liquidities
672 .get(&short_channel_id)
673 .unwrap_or(&ChannelLiquidity::new())
674 .as_directed(source, target, capacity_msat)
675 .success_probability(amount_msat);
676 match success_probability {
677 Probability::Zero => u64::max_value(),
678 Probability::One => 0,
679 Probability::Ratio { numerator, denominator } => {
680 let success_probability = numerator as f64 / denominator as f64;
681 (-(success_probability.log10()) * liquidity_penalty_multiplier_msat as f64) as u64
686 fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
687 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
688 let network_graph = self.network_graph.read_only();
689 let hop_sources = core::iter::once(self.node_id)
690 .chain(path.iter().map(|hop| NodeId::from_pubkey(&hop.pubkey)));
691 for (source, hop) in hop_sources.zip(path.iter()) {
692 let target = NodeId::from_pubkey(&hop.pubkey);
693 if source == self.node_id || target == self.node_id {
697 let capacity_msat = network_graph.channels()
698 .get(&hop.short_channel_id)
699 .and_then(|channel| channel.as_directed_to(&target).map(|d| d.effective_capacity()))
700 .unwrap_or(EffectiveCapacity::Unknown)
703 if hop.short_channel_id == short_channel_id {
704 self.channel_liquidities
705 .entry(hop.short_channel_id)
706 .or_insert_with(|| ChannelLiquidity::new())
707 .as_directed_mut(&source, &target, capacity_msat)
708 .failed_at_channel(amount_msat);
712 self.channel_liquidities
713 .entry(hop.short_channel_id)
714 .or_insert_with(|| ChannelLiquidity::new())
715 .as_directed_mut(&source, &target, capacity_msat)
716 .failed_downstream(amount_msat);
720 fn payment_path_successful(&mut self, path: &[&RouteHop]) {
721 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
722 let network_graph = self.network_graph.read_only();
723 let hop_sources = core::iter::once(self.node_id)
724 .chain(path.iter().map(|hop| NodeId::from_pubkey(&hop.pubkey)));
725 for (source, hop) in hop_sources.zip(path.iter()) {
726 let target = NodeId::from_pubkey(&hop.pubkey);
727 if source == self.node_id || target == self.node_id {
731 let capacity_msat = network_graph.channels()
732 .get(&hop.short_channel_id)
733 .and_then(|channel| channel.as_directed_to(&target).map(|d| d.effective_capacity()))
734 .unwrap_or(EffectiveCapacity::Unknown)
737 self.channel_liquidities
738 .entry(hop.short_channel_id)
739 .or_insert_with(|| ChannelLiquidity::new())
740 .as_directed_mut(&source, &target, capacity_msat)
741 .successful(amount_msat);
746 impl<G: Deref<Target = NetworkGraph>> Writeable for ProbabilisticScorer<G> {
748 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
749 self.params.write(w)?;
750 self.node_id.write(w)?;
751 self.channel_liquidities.write(w)?;
752 write_tlv_fields!(w, {});
757 impl<G: Deref<Target = NetworkGraph>> ReadableArgs<G> for ProbabilisticScorer<G> {
759 fn read<R: Read>(r: &mut R, args: G) -> Result<Self, DecodeError> {
761 params: Readable::read(r)?,
762 node_id: Readable::read(r)?,
764 channel_liquidities: Readable::read(r)?,
766 read_tlv_fields!(r, {});
771 impl Writeable for ChannelLiquidity {
773 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
774 write_tlv_fields!(w, {
775 (0, self.min_liquidity_offset_msat, required),
776 (2, self.max_liquidity_offset_msat, required),
782 impl Readable for ChannelLiquidity {
784 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
785 let mut min_liquidity_offset_msat = 0;
786 let mut max_liquidity_offset_msat = 0;
787 read_tlv_fields!(r, {
788 (0, min_liquidity_offset_msat, required),
789 (2, max_liquidity_offset_msat, required),
792 min_liquidity_offset_msat,
793 max_liquidity_offset_msat
798 pub(crate) mod time {
800 use core::time::Duration;
801 /// A measurement of time.
802 pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
803 /// Returns an instance corresponding to the current moment.
806 /// Returns the amount of time elapsed since `self` was created.
807 fn elapsed(&self) -> Duration;
809 /// Returns the amount of time passed since the beginning of [`Time`].
811 /// Used during (de-)serialization.
812 fn duration_since_epoch() -> Duration;
815 /// A state in which time has no meaning.
816 #[derive(Debug, PartialEq, Eq)]
819 #[cfg(not(feature = "no-std"))]
820 impl Time for std::time::Instant {
822 std::time::Instant::now()
825 fn duration_since_epoch() -> Duration {
826 use std::time::SystemTime;
827 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
830 fn elapsed(&self) -> Duration {
831 std::time::Instant::elapsed(self)
835 impl Time for Eternity {
840 fn duration_since_epoch() -> Duration {
841 Duration::from_secs(0)
844 fn elapsed(&self) -> Duration {
845 Duration::from_secs(0)
849 impl Sub<Duration> for Eternity {
852 fn sub(self, _other: Duration) -> Self {
858 pub(crate) use self::time::Time;
862 use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorer, ScoringParameters, ScorerUsingTime, Time};
863 use super::time::Eternity;
865 use ln::features::{ChannelFeatures, NodeFeatures};
866 use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
867 use routing::scoring::Score;
868 use routing::network_graph::{NetworkGraph, NodeId};
869 use routing::router::RouteHop;
870 use util::ser::{Readable, Writeable};
872 use bitcoin::blockdata::constants::genesis_block;
873 use bitcoin::hashes::Hash;
874 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
875 use bitcoin::network::constants::Network;
876 use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
877 use core::cell::Cell;
879 use core::time::Duration;
884 /// Time that can be advanced manually in tests.
885 #[derive(Debug, PartialEq, Eq)]
886 struct SinceEpoch(Duration);
890 static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
893 fn advance(duration: Duration) {
894 Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
898 impl Time for SinceEpoch {
900 Self(Self::duration_since_epoch())
903 fn duration_since_epoch() -> Duration {
904 Self::ELAPSED.with(|elapsed| elapsed.get())
907 fn elapsed(&self) -> Duration {
908 Self::duration_since_epoch() - self.0
912 impl Sub<Duration> for SinceEpoch {
915 fn sub(self, other: Duration) -> Self {
921 fn time_passes_when_advanced() {
922 let now = SinceEpoch::now();
923 assert_eq!(now.elapsed(), Duration::from_secs(0));
925 SinceEpoch::advance(Duration::from_secs(1));
926 SinceEpoch::advance(Duration::from_secs(1));
928 let elapsed = now.elapsed();
929 let later = SinceEpoch::now();
931 assert_eq!(elapsed, Duration::from_secs(2));
932 assert_eq!(later - elapsed, now);
936 fn time_never_passes_in_an_eternity() {
937 let now = Eternity::now();
938 let elapsed = now.elapsed();
939 let later = Eternity::now();
941 assert_eq!(now.elapsed(), Duration::from_secs(0));
942 assert_eq!(later - elapsed, now);
947 /// A scorer for testing with time that can be manually advanced.
948 type Scorer = ScorerUsingTime::<SinceEpoch>;
950 fn source_privkey() -> SecretKey {
951 SecretKey::from_slice(&[42; 32]).unwrap()
954 fn target_privkey() -> SecretKey {
955 SecretKey::from_slice(&[43; 32]).unwrap()
958 fn source_pubkey() -> PublicKey {
959 let secp_ctx = Secp256k1::new();
960 PublicKey::from_secret_key(&secp_ctx, &source_privkey())
963 fn target_pubkey() -> PublicKey {
964 let secp_ctx = Secp256k1::new();
965 PublicKey::from_secret_key(&secp_ctx, &target_privkey())
968 fn source_node_id() -> NodeId {
969 NodeId::from_pubkey(&source_pubkey())
972 fn target_node_id() -> NodeId {
973 NodeId::from_pubkey(&target_pubkey())
977 fn penalizes_without_channel_failures() {
978 let scorer = Scorer::new(ScoringParameters {
979 base_penalty_msat: 1_000,
980 failure_penalty_msat: 512,
981 failure_penalty_half_life: Duration::from_secs(1),
982 overuse_penalty_start_1024th: 1024,
983 overuse_penalty_msat_per_1024th: 0,
985 let source = source_node_id();
986 let target = target_node_id();
987 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
989 SinceEpoch::advance(Duration::from_secs(1));
990 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
994 fn accumulates_channel_failure_penalties() {
995 let mut scorer = Scorer::new(ScoringParameters {
996 base_penalty_msat: 1_000,
997 failure_penalty_msat: 64,
998 failure_penalty_half_life: Duration::from_secs(10),
999 overuse_penalty_start_1024th: 1024,
1000 overuse_penalty_msat_per_1024th: 0,
1002 let source = source_node_id();
1003 let target = target_node_id();
1004 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1006 scorer.payment_path_failed(&[], 42);
1007 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1009 scorer.payment_path_failed(&[], 42);
1010 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1012 scorer.payment_path_failed(&[], 42);
1013 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_192);
1017 fn decays_channel_failure_penalties_over_time() {
1018 let mut scorer = Scorer::new(ScoringParameters {
1019 base_penalty_msat: 1_000,
1020 failure_penalty_msat: 512,
1021 failure_penalty_half_life: Duration::from_secs(10),
1022 overuse_penalty_start_1024th: 1024,
1023 overuse_penalty_msat_per_1024th: 0,
1025 let source = source_node_id();
1026 let target = target_node_id();
1027 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1029 scorer.payment_path_failed(&[], 42);
1030 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1032 SinceEpoch::advance(Duration::from_secs(9));
1033 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1035 SinceEpoch::advance(Duration::from_secs(1));
1036 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1038 SinceEpoch::advance(Duration::from_secs(10 * 8));
1039 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_001);
1041 SinceEpoch::advance(Duration::from_secs(10));
1042 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1044 SinceEpoch::advance(Duration::from_secs(10));
1045 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1049 fn decays_channel_failure_penalties_without_shift_overflow() {
1050 let mut scorer = Scorer::new(ScoringParameters {
1051 base_penalty_msat: 1_000,
1052 failure_penalty_msat: 512,
1053 failure_penalty_half_life: Duration::from_secs(10),
1054 overuse_penalty_start_1024th: 1024,
1055 overuse_penalty_msat_per_1024th: 0,
1057 let source = source_node_id();
1058 let target = target_node_id();
1059 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1061 scorer.payment_path_failed(&[], 42);
1062 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1064 // An unchecked right shift 64 bits or more in ChannelFailure::decayed_penalty_msat would
1065 // cause an overflow.
1066 SinceEpoch::advance(Duration::from_secs(10 * 64));
1067 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1069 SinceEpoch::advance(Duration::from_secs(10));
1070 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1074 fn accumulates_channel_failure_penalties_after_decay() {
1075 let mut scorer = Scorer::new(ScoringParameters {
1076 base_penalty_msat: 1_000,
1077 failure_penalty_msat: 512,
1078 failure_penalty_half_life: Duration::from_secs(10),
1079 overuse_penalty_start_1024th: 1024,
1080 overuse_penalty_msat_per_1024th: 0,
1082 let source = source_node_id();
1083 let target = target_node_id();
1084 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1086 scorer.payment_path_failed(&[], 42);
1087 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1089 SinceEpoch::advance(Duration::from_secs(10));
1090 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1092 scorer.payment_path_failed(&[], 42);
1093 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_768);
1095 SinceEpoch::advance(Duration::from_secs(10));
1096 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_384);
1100 fn reduces_channel_failure_penalties_after_success() {
1101 let mut scorer = Scorer::new(ScoringParameters {
1102 base_penalty_msat: 1_000,
1103 failure_penalty_msat: 512,
1104 failure_penalty_half_life: Duration::from_secs(10),
1105 overuse_penalty_start_1024th: 1024,
1106 overuse_penalty_msat_per_1024th: 0,
1108 let source = source_node_id();
1109 let target = target_node_id();
1110 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1112 scorer.payment_path_failed(&[], 42);
1113 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1115 SinceEpoch::advance(Duration::from_secs(10));
1116 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1118 let hop = RouteHop {
1119 pubkey: PublicKey::from_slice(target.as_slice()).unwrap(),
1120 node_features: NodeFeatures::known(),
1121 short_channel_id: 42,
1122 channel_features: ChannelFeatures::known(),
1124 cltv_expiry_delta: 18,
1126 scorer.payment_path_successful(&[&hop]);
1127 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1129 SinceEpoch::advance(Duration::from_secs(10));
1130 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1134 fn restores_persisted_channel_failure_penalties() {
1135 let mut scorer = Scorer::new(ScoringParameters {
1136 base_penalty_msat: 1_000,
1137 failure_penalty_msat: 512,
1138 failure_penalty_half_life: Duration::from_secs(10),
1139 overuse_penalty_start_1024th: 1024,
1140 overuse_penalty_msat_per_1024th: 0,
1142 let source = source_node_id();
1143 let target = target_node_id();
1145 scorer.payment_path_failed(&[], 42);
1146 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1148 SinceEpoch::advance(Duration::from_secs(10));
1149 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1151 scorer.payment_path_failed(&[], 43);
1152 assert_eq!(scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1154 let mut serialized_scorer = Vec::new();
1155 scorer.write(&mut serialized_scorer).unwrap();
1157 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1158 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1159 assert_eq!(deserialized_scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1163 fn decays_persisted_channel_failure_penalties() {
1164 let mut scorer = Scorer::new(ScoringParameters {
1165 base_penalty_msat: 1_000,
1166 failure_penalty_msat: 512,
1167 failure_penalty_half_life: Duration::from_secs(10),
1168 overuse_penalty_start_1024th: 1024,
1169 overuse_penalty_msat_per_1024th: 0,
1171 let source = source_node_id();
1172 let target = target_node_id();
1174 scorer.payment_path_failed(&[], 42);
1175 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1177 let mut serialized_scorer = Vec::new();
1178 scorer.write(&mut serialized_scorer).unwrap();
1180 SinceEpoch::advance(Duration::from_secs(10));
1182 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1183 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1185 SinceEpoch::advance(Duration::from_secs(10));
1186 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1190 fn charges_per_1024th_penalty() {
1191 let scorer = Scorer::new(ScoringParameters {
1192 base_penalty_msat: 0,
1193 failure_penalty_msat: 0,
1194 failure_penalty_half_life: Duration::from_secs(0),
1195 overuse_penalty_start_1024th: 256,
1196 overuse_penalty_msat_per_1024th: 100,
1198 let source = source_node_id();
1199 let target = target_node_id();
1201 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 1_024_000, &source, &target), 0);
1202 assert_eq!(scorer.channel_penalty_msat(42, 256_999, 1_024_000, &source, &target), 0);
1203 assert_eq!(scorer.channel_penalty_msat(42, 257_000, 1_024_000, &source, &target), 100);
1204 assert_eq!(scorer.channel_penalty_msat(42, 258_000, 1_024_000, &source, &target), 200);
1205 assert_eq!(scorer.channel_penalty_msat(42, 512_000, 1_024_000, &source, &target), 256 * 100);
1208 // `ProbabilisticScorer` tests
1210 fn sender_privkey() -> SecretKey {
1211 SecretKey::from_slice(&[41; 32]).unwrap()
1214 fn recipient_privkey() -> SecretKey {
1215 SecretKey::from_slice(&[45; 32]).unwrap()
1218 fn sender_pubkey() -> PublicKey {
1219 let secp_ctx = Secp256k1::new();
1220 PublicKey::from_secret_key(&secp_ctx, &sender_privkey())
1223 fn recipient_pubkey() -> PublicKey {
1224 let secp_ctx = Secp256k1::new();
1225 PublicKey::from_secret_key(&secp_ctx, &recipient_privkey())
1228 fn sender_node_id() -> NodeId {
1229 NodeId::from_pubkey(&sender_pubkey())
1232 fn recipient_node_id() -> NodeId {
1233 NodeId::from_pubkey(&recipient_pubkey())
1236 fn network_graph() -> NetworkGraph {
1237 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1238 let mut network_graph = NetworkGraph::new(genesis_hash);
1239 add_channel(&mut network_graph, 41, sender_privkey(), source_privkey());
1240 add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
1241 add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
1247 network_graph: &mut NetworkGraph, short_channel_id: u64, node_1_key: SecretKey,
1248 node_2_key: SecretKey
1250 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1251 let node_1_secret = &SecretKey::from_slice(&[39; 32]).unwrap();
1252 let node_2_secret = &SecretKey::from_slice(&[40; 32]).unwrap();
1253 let secp_ctx = Secp256k1::new();
1254 let unsigned_announcement = UnsignedChannelAnnouncement {
1255 features: ChannelFeatures::known(),
1256 chain_hash: genesis_hash,
1258 node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_key),
1259 node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_key),
1260 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_secret),
1261 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_secret),
1262 excess_data: Vec::new(),
1264 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1265 let signed_announcement = ChannelAnnouncement {
1266 node_signature_1: secp_ctx.sign(&msghash, &node_1_key),
1267 node_signature_2: secp_ctx.sign(&msghash, &node_2_key),
1268 bitcoin_signature_1: secp_ctx.sign(&msghash, &node_1_secret),
1269 bitcoin_signature_2: secp_ctx.sign(&msghash, &node_2_secret),
1270 contents: unsigned_announcement,
1272 let chain_source: Option<&::util::test_utils::TestChainSource> = None;
1273 network_graph.update_channel_from_announcement(
1274 &signed_announcement, &chain_source, &secp_ctx).unwrap();
1275 update_channel(network_graph, short_channel_id, node_1_key, 0);
1276 update_channel(network_graph, short_channel_id, node_2_key, 1);
1280 network_graph: &mut NetworkGraph, short_channel_id: u64, node_key: SecretKey, flags: u8
1282 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1283 let secp_ctx = Secp256k1::new();
1284 let unsigned_update = UnsignedChannelUpdate {
1285 chain_hash: genesis_hash,
1289 cltv_expiry_delta: 18,
1290 htlc_minimum_msat: 0,
1291 htlc_maximum_msat: OptionalField::Present(1_000),
1293 fee_proportional_millionths: 0,
1294 excess_data: Vec::new(),
1296 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
1297 let signed_update = ChannelUpdate {
1298 signature: secp_ctx.sign(&msghash, &node_key),
1299 contents: unsigned_update,
1301 network_graph.update_channel(&signed_update, &secp_ctx).unwrap();
1304 fn payment_path_for_amount(amount_msat: u64) -> Vec<RouteHop> {
1307 pubkey: source_pubkey(),
1308 node_features: NodeFeatures::known(),
1309 short_channel_id: 41,
1310 channel_features: ChannelFeatures::known(),
1312 cltv_expiry_delta: 18,
1315 pubkey: target_pubkey(),
1316 node_features: NodeFeatures::known(),
1317 short_channel_id: 42,
1318 channel_features: ChannelFeatures::known(),
1320 cltv_expiry_delta: 18,
1323 pubkey: recipient_pubkey(),
1324 node_features: NodeFeatures::known(),
1325 short_channel_id: 43,
1326 channel_features: ChannelFeatures::known(),
1327 fee_msat: amount_msat,
1328 cltv_expiry_delta: 18,
1334 fn liquidity_bounds_directed_from_lowest_node_id() {
1335 let network_graph = network_graph();
1336 let params = ProbabilisticScoringParameters::default();
1337 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1340 min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1344 min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1346 let source = source_node_id();
1347 let target = target_node_id();
1348 let recipient = recipient_node_id();
1350 let liquidity = scorer.channel_liquidities.get_mut(&42).unwrap();
1351 assert!(source > target);
1352 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 100);
1353 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1354 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1355 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 900);
1357 liquidity.as_directed_mut(&source, &target, 1_000).set_min_liquidity_msat(200);
1358 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 200);
1359 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1360 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1361 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 800);
1363 let liquidity = scorer.channel_liquidities.get_mut(&43).unwrap();
1364 assert!(target < recipient);
1365 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 700);
1366 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 900);
1367 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 100);
1368 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 300);
1370 liquidity.as_directed_mut(&target, &recipient, 1_000).set_max_liquidity_msat(200);
1371 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 0);
1372 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 200);
1373 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 800);
1374 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 1000);
1378 fn resets_liquidity_upper_bound_when_crossed_by_lower_bound() {
1379 let network_graph = network_graph();
1380 let params = ProbabilisticScoringParameters::default();
1381 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1384 min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400
1386 let source = source_node_id();
1387 let target = target_node_id();
1388 assert!(source > target);
1390 // Check initial bounds.
1391 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1392 .as_directed(&source, &target, 1_000);
1393 assert_eq!(liquidity.min_liquidity_msat(), 400);
1394 assert_eq!(liquidity.max_liquidity_msat(), 800);
1396 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1397 .as_directed(&target, &source, 1_000);
1398 assert_eq!(liquidity.min_liquidity_msat(), 200);
1399 assert_eq!(liquidity.max_liquidity_msat(), 600);
1401 // Reset from source to target.
1402 scorer.channel_liquidities.get_mut(&42).unwrap()
1403 .as_directed_mut(&source, &target, 1_000)
1404 .set_min_liquidity_msat(900);
1406 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1407 .as_directed(&source, &target, 1_000);
1408 assert_eq!(liquidity.min_liquidity_msat(), 900);
1409 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1411 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1412 .as_directed(&target, &source, 1_000);
1413 assert_eq!(liquidity.min_liquidity_msat(), 0);
1414 assert_eq!(liquidity.max_liquidity_msat(), 100);
1416 // Reset from target to source.
1417 scorer.channel_liquidities.get_mut(&42).unwrap()
1418 .as_directed_mut(&target, &source, 1_000)
1419 .set_min_liquidity_msat(400);
1421 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1422 .as_directed(&source, &target, 1_000);
1423 assert_eq!(liquidity.min_liquidity_msat(), 0);
1424 assert_eq!(liquidity.max_liquidity_msat(), 600);
1426 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1427 .as_directed(&target, &source, 1_000);
1428 assert_eq!(liquidity.min_liquidity_msat(), 400);
1429 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1433 fn resets_liquidity_lower_bound_when_crossed_by_upper_bound() {
1434 let network_graph = network_graph();
1435 let params = ProbabilisticScoringParameters::default();
1436 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1439 min_liquidity_offset_msat: 200, max_liquidity_offset_msat: 400
1441 let source = source_node_id();
1442 let target = target_node_id();
1443 assert!(source > target);
1445 // Check initial bounds.
1446 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1447 .as_directed(&source, &target, 1_000);
1448 assert_eq!(liquidity.min_liquidity_msat(), 400);
1449 assert_eq!(liquidity.max_liquidity_msat(), 800);
1451 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1452 .as_directed(&target, &source, 1_000);
1453 assert_eq!(liquidity.min_liquidity_msat(), 200);
1454 assert_eq!(liquidity.max_liquidity_msat(), 600);
1456 // Reset from source to target.
1457 scorer.channel_liquidities.get_mut(&42).unwrap()
1458 .as_directed_mut(&source, &target, 1_000)
1459 .set_max_liquidity_msat(300);
1461 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1462 .as_directed(&source, &target, 1_000);
1463 assert_eq!(liquidity.min_liquidity_msat(), 0);
1464 assert_eq!(liquidity.max_liquidity_msat(), 300);
1466 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1467 .as_directed(&target, &source, 1_000);
1468 assert_eq!(liquidity.min_liquidity_msat(), 700);
1469 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1471 // Reset from target to source.
1472 scorer.channel_liquidities.get_mut(&42).unwrap()
1473 .as_directed_mut(&target, &source, 1_000)
1474 .set_max_liquidity_msat(600);
1476 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1477 .as_directed(&source, &target, 1_000);
1478 assert_eq!(liquidity.min_liquidity_msat(), 400);
1479 assert_eq!(liquidity.max_liquidity_msat(), 1_000);
1481 let liquidity = scorer.channel_liquidities.get(&42).unwrap()
1482 .as_directed(&target, &source, 1_000);
1483 assert_eq!(liquidity.min_liquidity_msat(), 0);
1484 assert_eq!(liquidity.max_liquidity_msat(), 600);
1488 fn increased_penalty_nearing_liquidity_upper_bound() {
1489 let network_graph = network_graph();
1490 let params = ProbabilisticScoringParameters::default();
1491 let scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph);
1492 let source = source_node_id();
1493 let target = target_node_id();
1495 assert_eq!(scorer.channel_penalty_msat(42, 100, 100_000, &source, &target), 0);
1496 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 100_000, &source, &target), 4);
1497 assert_eq!(scorer.channel_penalty_msat(42, 10_000, 100_000, &source, &target), 45);
1498 assert_eq!(scorer.channel_penalty_msat(42, 100_000, 100_000, &source, &target), 5_000);
1500 assert_eq!(scorer.channel_penalty_msat(42, 125, 1_000, &source, &target), 57);
1501 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1502 assert_eq!(scorer.channel_penalty_msat(42, 375, 1_000, &source, &target), 203);
1503 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1504 assert_eq!(scorer.channel_penalty_msat(42, 625, 1_000, &source, &target), 425);
1505 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1506 assert_eq!(scorer.channel_penalty_msat(42, 875, 1_000, &source, &target), 900);
1510 fn constant_penalty_outside_liquidity_bounds() {
1511 let network_graph = network_graph();
1512 let params = ProbabilisticScoringParameters::default();
1513 let scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1515 ChannelLiquidity { min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40 });
1516 let source = source_node_id();
1517 let target = target_node_id();
1519 assert_eq!(scorer.channel_penalty_msat(42, 39, 100, &source, &target), 0);
1520 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 0);
1521 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), u64::max_value());
1522 assert_eq!(scorer.channel_penalty_msat(42, 61, 100, &source, &target), u64::max_value());
1526 fn does_not_penalize_own_channel() {
1527 let network_graph = network_graph();
1528 let params = ProbabilisticScoringParameters::default();
1529 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph);
1530 let sender = sender_node_id();
1531 let source = source_node_id();
1532 let failed_path = payment_path_for_amount(500);
1533 let successful_path = payment_path_for_amount(200);
1535 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 0);
1537 scorer.payment_path_failed(&failed_path.iter().collect::<Vec<_>>(), 41);
1538 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 0);
1540 scorer.payment_path_successful(&successful_path.iter().collect::<Vec<_>>());
1541 assert_eq!(scorer.channel_penalty_msat(41, 500, 1_000, &sender, &source), 0);
1545 fn sets_liquidity_lower_bound_on_downstream_failure() {
1546 let network_graph = network_graph();
1547 let params = ProbabilisticScoringParameters::default();
1548 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph);
1549 let source = source_node_id();
1550 let target = target_node_id();
1551 let path = payment_path_for_amount(500);
1553 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1554 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1555 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1557 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 43);
1559 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 0);
1560 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 0);
1561 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 300);
1565 fn sets_liquidity_upper_bound_on_failure() {
1566 let network_graph = network_graph();
1567 let params = ProbabilisticScoringParameters::default();
1568 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph);
1569 let source = source_node_id();
1570 let target = target_node_id();
1571 let path = payment_path_for_amount(500);
1573 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1574 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1575 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1577 scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 42);
1579 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 300);
1580 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2699);
1581 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), u64::max_value());
1585 fn reduces_liquidity_upper_bound_along_path_on_success() {
1586 let network_graph = network_graph();
1587 let params = ProbabilisticScoringParameters::default();
1588 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph);
1589 let sender = sender_node_id();
1590 let source = source_node_id();
1591 let target = target_node_id();
1592 let recipient = recipient_node_id();
1593 let path = payment_path_for_amount(500);
1595 assert_eq!(scorer.channel_penalty_msat(41, 250, 1_000, &sender, &source), 0);
1596 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1597 assert_eq!(scorer.channel_penalty_msat(43, 250, 1_000, &target, &recipient), 124);
1599 scorer.payment_path_successful(&path.iter().collect::<Vec<_>>());
1601 assert_eq!(scorer.channel_penalty_msat(41, 250, 1_000, &sender, &source), 0);
1602 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 300);
1603 assert_eq!(scorer.channel_penalty_msat(43, 250, 1_000, &target, &recipient), 300);