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 //! [`Scorer`] may be given to [`find_route`] to score payment channels during path finding when a
13 //! 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::{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, params: RouteParameters, network_graph: NetworkGraph) {
31 //! # let logger = FakeLogger {};
33 //! // Use the default channel penalties.
34 //! let scorer = Scorer::default();
36 //! // Or use custom channel penalties.
37 //! let scorer = Scorer::new(ScoringParameters {
38 //! base_penalty_msat: 1000,
39 //! failure_penalty_msat: 2 * 1024 * 1000,
40 //! ..ScoringParameters::default()
43 //! let route = find_route(&payer, ¶ms, &network_graph, None, &logger, &scorer);
49 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
50 //! different types and thus is undefined.
52 //! [`find_route`]: crate::routing::router::find_route
54 use bitcoin::secp256k1::key::PublicKey;
56 use ln::msgs::DecodeError;
57 use routing::network_graph::{EffectiveCapacity, NetworkGraph, NodeId};
58 use routing::router::RouteHop;
59 use util::ser::{Readable, ReadableArgs, Writeable, Writer};
62 use core::cell::{RefCell, RefMut};
63 use core::ops::{Deref, DerefMut};
64 use core::time::Duration;
66 use sync::{Mutex, MutexGuard};
68 /// We define Score ever-so-slightly differently based on whether we are being built for C bindings
69 /// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
70 /// no problem - you move a `Score` that implements `Writeable` into a `Mutex`, lock it, and now
71 /// you have the original, concrete, `Score` type, which presumably implements `Writeable`.
73 /// For C users, once you've moved the `Score` into a `LockableScore` all you have after locking it
74 /// is an opaque trait object with an opaque pointer with no type info. Users could take the unsafe
75 /// approach of blindly casting that opaque pointer to a concrete type and calling `Writeable` from
76 /// there, but other languages downstream of the C bindings (e.g. Java) can't even do that.
77 /// Instead, we really want `Score` and `LockableScore` to implement `Writeable` directly, which we
78 /// do here by defining `Score` differently for `cfg(c_bindings)`.
79 macro_rules! define_score { ($($supertrait: path)*) => {
80 /// An interface used to score payment channels for path finding.
82 /// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
83 pub trait Score $(: $supertrait)* {
84 /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
85 /// given channel in the direction from `source` to `target`.
87 /// The channel's capacity (less any other MPP parts that are also being considered for use in
88 /// the same payment) is given by `capacity_msat`. It may be determined from various sources
89 /// such as a chain data, network gossip, or invoice hints, the latter indicating sufficient
90 /// capacity (i.e., near [`u64::max_value`]). Thus, implementations should be overflow-safe.
91 fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &NodeId, target: &NodeId) -> u64;
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 provides reasonable default behavior.
194 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
195 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
197 /// See [module-level documentation] for usage.
199 /// [module-level documentation]: crate::routing::scoring
200 #[cfg(not(feature = "no-std"))]
201 pub type Scorer = ScorerUsingTime::<std::time::Instant>;
202 /// [`Score`] implementation that provides reasonable default behavior.
204 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
205 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
207 /// See [module-level documentation] for usage and [`ScoringParameters`] for customization.
209 /// [module-level documentation]: crate::routing::scoring
210 #[cfg(feature = "no-std")]
211 pub type Scorer = ScorerUsingTime::<time::Eternity>;
213 // Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
214 // doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
217 /// [`Score`] implementation.
219 /// See [`Scorer`] for details.
223 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
226 /// (C-not exported) generally all users should use the [`Scorer`] type alias.
227 pub struct ScorerUsingTime<T: Time> {
228 params: ScoringParameters,
229 // TODO: Remove entries of closed channels.
230 channel_failures: HashMap<u64, ChannelFailure<T>>,
233 /// Parameters for configuring [`Scorer`].
234 pub struct ScoringParameters {
235 /// A fixed penalty in msats to apply to each channel.
237 /// Default value: 500 msat
238 pub base_penalty_msat: u64,
240 /// A penalty in msats to apply to a channel upon failing to relay a payment.
242 /// This accumulates for each failure but may be reduced over time based on
243 /// [`failure_penalty_half_life`] or when successfully routing through a channel.
245 /// Default value: 1,024,000 msat
247 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
248 pub failure_penalty_msat: u64,
250 /// When the amount being sent over a channel is this many 1024ths of the total channel
251 /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
253 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
255 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
256 pub overuse_penalty_start_1024th: u16,
258 /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
259 /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
261 /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
262 /// to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
264 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
265 pub overuse_penalty_msat_per_1024th: u64,
267 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
270 /// Successfully routing through a channel will immediately cut the penalty in half as well.
274 /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
277 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
278 pub failure_penalty_half_life: Duration,
281 impl_writeable_tlv_based!(ScoringParameters, {
282 (0, base_penalty_msat, required),
283 (1, overuse_penalty_start_1024th, (default_value, 128)),
284 (2, failure_penalty_msat, required),
285 (3, overuse_penalty_msat_per_1024th, (default_value, 20)),
286 (4, failure_penalty_half_life, required),
289 /// Accounting for penalties against a channel for failing to relay any payments.
291 /// Penalties decay over time, though accumulate as more failures occur.
292 struct ChannelFailure<T: Time> {
293 /// Accumulated penalty in msats for the channel as of `last_updated`.
294 undecayed_penalty_msat: u64,
296 /// Last time the channel either failed to route or successfully routed a payment. Used to decay
297 /// `undecayed_penalty_msat`.
301 impl<T: Time> ScorerUsingTime<T> {
302 /// Creates a new scorer using the given scoring parameters.
303 pub fn new(params: ScoringParameters) -> Self {
306 channel_failures: HashMap::new(),
310 /// Creates a new scorer using `penalty_msat` as a fixed channel penalty.
311 #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
312 pub fn with_fixed_penalty(penalty_msat: u64) -> Self {
313 Self::new(ScoringParameters {
314 base_penalty_msat: penalty_msat,
315 failure_penalty_msat: 0,
316 failure_penalty_half_life: Duration::from_secs(0),
317 overuse_penalty_start_1024th: 1024,
318 overuse_penalty_msat_per_1024th: 0,
323 impl<T: Time> ChannelFailure<T> {
324 fn new(failure_penalty_msat: u64) -> Self {
326 undecayed_penalty_msat: failure_penalty_msat,
327 last_updated: T::now(),
331 fn add_penalty(&mut self, failure_penalty_msat: u64, half_life: Duration) {
332 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) + failure_penalty_msat;
333 self.last_updated = T::now();
336 fn reduce_penalty(&mut self, half_life: Duration) {
337 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) >> 1;
338 self.last_updated = T::now();
341 fn decayed_penalty_msat(&self, half_life: Duration) -> u64 {
342 self.last_updated.elapsed().as_secs()
343 .checked_div(half_life.as_secs())
344 .and_then(|decays| self.undecayed_penalty_msat.checked_shr(decays as u32))
349 impl<T: Time> Default for ScorerUsingTime<T> {
350 fn default() -> Self {
351 Self::new(ScoringParameters::default())
355 impl Default for ScoringParameters {
356 fn default() -> Self {
358 base_penalty_msat: 500,
359 failure_penalty_msat: 1024 * 1000,
360 failure_penalty_half_life: Duration::from_secs(3600),
361 overuse_penalty_start_1024th: 1024 / 8,
362 overuse_penalty_msat_per_1024th: 20,
367 impl<T: Time> Score for ScorerUsingTime<T> {
368 fn channel_penalty_msat(
369 &self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, _source: &NodeId, _target: &NodeId
371 let failure_penalty_msat = self.channel_failures
372 .get(&short_channel_id)
373 .map_or(0, |value| value.decayed_penalty_msat(self.params.failure_penalty_half_life));
375 let mut penalty_msat = self.params.base_penalty_msat + failure_penalty_msat;
376 let send_1024ths = send_amt_msat.checked_mul(1024).unwrap_or(u64::max_value()) / capacity_msat;
377 if send_1024ths > self.params.overuse_penalty_start_1024th as u64 {
378 penalty_msat = penalty_msat.checked_add(
379 (send_1024ths - self.params.overuse_penalty_start_1024th as u64)
380 .checked_mul(self.params.overuse_penalty_msat_per_1024th).unwrap_or(u64::max_value()))
381 .unwrap_or(u64::max_value());
387 fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
388 let failure_penalty_msat = self.params.failure_penalty_msat;
389 let half_life = self.params.failure_penalty_half_life;
390 self.channel_failures
391 .entry(short_channel_id)
392 .and_modify(|failure| failure.add_penalty(failure_penalty_msat, half_life))
393 .or_insert_with(|| ChannelFailure::new(failure_penalty_msat));
396 fn payment_path_successful(&mut self, path: &[&RouteHop]) {
397 let half_life = self.params.failure_penalty_half_life;
398 for hop in path.iter() {
399 self.channel_failures
400 .entry(hop.short_channel_id)
401 .and_modify(|failure| failure.reduce_penalty(half_life));
406 impl<T: Time> Writeable for ScorerUsingTime<T> {
408 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
409 self.params.write(w)?;
410 self.channel_failures.write(w)?;
411 write_tlv_fields!(w, {});
416 impl<T: Time> Readable for ScorerUsingTime<T> {
418 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
420 params: Readable::read(r)?,
421 channel_failures: Readable::read(r)?,
423 read_tlv_fields!(r, {});
428 impl<T: Time> Writeable for ChannelFailure<T> {
430 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
431 let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
432 write_tlv_fields!(w, {
433 (0, self.undecayed_penalty_msat, required),
434 (2, duration_since_epoch, required),
440 impl<T: Time> Readable for ChannelFailure<T> {
442 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
443 let mut undecayed_penalty_msat = 0;
444 let mut duration_since_epoch = Duration::from_secs(0);
445 read_tlv_fields!(r, {
446 (0, undecayed_penalty_msat, required),
447 (2, duration_since_epoch, required),
450 undecayed_penalty_msat,
451 last_updated: T::now() - (T::duration_since_epoch() - duration_since_epoch),
456 /// [`Score`] implementation using channel success probability distributions.
458 /// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
459 /// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
460 /// distributions are defined based on knowledge learned from successful and unsuccessful attempts.
461 /// Then the negative log of the success probability is used to determine the cost of routing a
462 /// specific HTLC amount through a channel.
464 /// [1]: https://arxiv.org/abs/2107.05322
465 pub struct ProbabilisticScorer<G: Deref<Target = NetworkGraph>> {
466 _params: ProbabilisticScoringParameters,
469 // TODO: Remove entries of closed channels.
470 channel_liquidities: HashMap<u64, ChannelLiquidity>,
473 /// Parameters for configuring [`ProbabilisticScorer`].
474 pub struct ProbabilisticScoringParameters;
476 impl_writeable_tlv_based!(ProbabilisticScoringParameters, {
479 /// Accounting for channel liquidity balance uncertainty.
481 /// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
482 /// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
483 /// offset fields gives the opposite direction.
484 struct ChannelLiquidity {
485 min_liquidity_offset_msat: u64,
486 max_liquidity_offset_msat: u64,
489 /// A view of [`ChannelLiquidity`] in one direction assuming a certain channel capacity.
490 struct DirectedChannelLiquidity<L: Deref<Target = u64>> {
491 min_liquidity_offset_msat: L,
492 max_liquidity_offset_msat: L,
496 /// The likelihood of an event occurring.
500 Ratio { numerator: u64, denominator: u64 },
503 impl<G: Deref<Target = NetworkGraph>> ProbabilisticScorer<G> {
504 /// Creates a new scorer using the given scoring parameters for sending payments from a node
505 /// through a network graph.
507 _params: ProbabilisticScoringParameters, node_pubkey: PublicKey, network_graph: G
511 node_id: NodeId::from_pubkey(&node_pubkey),
513 channel_liquidities: HashMap::new(),
518 fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity) -> Self {
519 assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
524 impl Default for ProbabilisticScoringParameters {
525 fn default() -> Self {
530 impl ChannelLiquidity {
534 min_liquidity_offset_msat: 0,
535 max_liquidity_offset_msat: 0,
539 /// Returns a view of the channel liquidity directed from `source` to `target` assuming
542 &self, source: &NodeId, target: &NodeId, capacity_msat: u64
543 ) -> DirectedChannelLiquidity<&u64> {
544 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
545 (&self.min_liquidity_offset_msat, &self.max_liquidity_offset_msat)
547 (&self.max_liquidity_offset_msat, &self.min_liquidity_offset_msat)
550 DirectedChannelLiquidity {
551 min_liquidity_offset_msat,
552 max_liquidity_offset_msat,
557 /// Returns a mutable view of the channel liquidity directed from `source` to `target` assuming
560 &mut self, source: &NodeId, target: &NodeId, capacity_msat: u64
561 ) -> DirectedChannelLiquidity<&mut u64> {
562 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
563 (&mut self.min_liquidity_offset_msat, &mut self.max_liquidity_offset_msat)
565 (&mut self.max_liquidity_offset_msat, &mut self.min_liquidity_offset_msat)
568 DirectedChannelLiquidity {
569 min_liquidity_offset_msat,
570 max_liquidity_offset_msat,
576 impl<L: Deref<Target = u64>> DirectedChannelLiquidity<L> {
577 /// Returns the success probability of routing the given HTLC `amount_msat` through the channel
578 /// in this direction.
579 fn success_probability(&self, amount_msat: u64) -> Probability {
580 let max_liquidity_msat = self.max_liquidity_msat();
581 let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
582 if amount_msat > max_liquidity_msat {
584 } else if amount_msat < min_liquidity_msat {
587 let numerator = max_liquidity_msat + 1 - amount_msat;
588 let denominator = max_liquidity_msat + 1 - min_liquidity_msat;
589 if numerator == denominator {
592 Probability::Ratio { numerator, denominator }
597 /// Returns the lower bound of the channel liquidity balance in this direction.
598 fn min_liquidity_msat(&self) -> u64 {
599 *self.min_liquidity_offset_msat
602 /// Returns the upper bound of the channel liquidity balance in this direction.
603 fn max_liquidity_msat(&self) -> u64 {
604 self.capacity_msat.checked_sub(*self.max_liquidity_offset_msat).unwrap_or(0)
608 impl<L: DerefMut<Target = u64>> DirectedChannelLiquidity<L> {
609 /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
610 fn failed_at_channel(&mut self, amount_msat: u64) {
611 if amount_msat < self.max_liquidity_msat() {
612 self.set_max_liquidity_msat(amount_msat);
616 /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
617 fn failed_downstream(&mut self, amount_msat: u64) {
618 if amount_msat > self.min_liquidity_msat() {
619 self.set_min_liquidity_msat(amount_msat);
623 /// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
624 fn successful(&mut self, amount_msat: u64) {
625 let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
626 self.set_max_liquidity_msat(max_liquidity_msat);
629 /// Adjusts the lower bound of the channel liquidity balance in this direction.
630 fn set_min_liquidity_msat(&mut self, amount_msat: u64) {
631 *self.min_liquidity_offset_msat = amount_msat;
633 if amount_msat > self.max_liquidity_msat() {
634 *self.max_liquidity_offset_msat = 0;
638 /// Adjusts the upper bound of the channel liquidity balance in this direction.
639 fn set_max_liquidity_msat(&mut self, amount_msat: u64) {
640 *self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
642 if amount_msat < self.min_liquidity_msat() {
643 *self.min_liquidity_offset_msat = 0;
648 impl<G: Deref<Target = NetworkGraph>> Score for ProbabilisticScorer<G> {
649 fn channel_penalty_msat(
650 &self, short_channel_id: u64, amount_msat: u64, capacity_msat: u64, source: &NodeId,
653 if *source == self.node_id || *target == self.node_id {
657 let success_probability = self.channel_liquidities
658 .get(&short_channel_id)
659 .unwrap_or(&ChannelLiquidity::new())
660 .as_directed(source, target, capacity_msat)
661 .success_probability(amount_msat);
662 match success_probability {
663 Probability::Zero => u64::max_value(),
664 Probability::One => 0,
665 Probability::Ratio { numerator, denominator } => {
666 let success_probability = numerator as f64 / denominator as f64;
667 (-(success_probability.log10()) * amount_msat as f64) as u64
672 fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
673 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
674 let network_graph = self.network_graph.read_only();
675 let hop_sources = core::iter::once(self.node_id)
676 .chain(path.iter().map(|hop| NodeId::from_pubkey(&hop.pubkey)));
677 for (source, hop) in hop_sources.zip(path.iter()) {
678 let target = NodeId::from_pubkey(&hop.pubkey);
679 if source == self.node_id || target == self.node_id {
683 let capacity_msat = network_graph.channels()
684 .get(&hop.short_channel_id)
685 .and_then(|channel| channel.as_directed_to(&target).map(|d| d.effective_capacity()))
686 .unwrap_or(EffectiveCapacity::Unknown)
689 if hop.short_channel_id == short_channel_id {
690 self.channel_liquidities
691 .entry(hop.short_channel_id)
692 .or_insert_with(|| ChannelLiquidity::new())
693 .as_directed_mut(&source, &target, capacity_msat)
694 .failed_at_channel(amount_msat);
698 self.channel_liquidities
699 .entry(hop.short_channel_id)
700 .or_insert_with(|| ChannelLiquidity::new())
701 .as_directed_mut(&source, &target, capacity_msat)
702 .failed_downstream(amount_msat);
706 fn payment_path_successful(&mut self, path: &[&RouteHop]) {
707 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
708 let network_graph = self.network_graph.read_only();
709 let hop_sources = core::iter::once(self.node_id)
710 .chain(path.iter().map(|hop| NodeId::from_pubkey(&hop.pubkey)));
711 for (source, hop) in hop_sources.zip(path.iter()) {
712 let target = NodeId::from_pubkey(&hop.pubkey);
713 if source == self.node_id || target == self.node_id {
717 let capacity_msat = network_graph.channels()
718 .get(&hop.short_channel_id)
719 .and_then(|channel| channel.as_directed_to(&target).map(|d| d.effective_capacity()))
720 .unwrap_or(EffectiveCapacity::Unknown)
723 self.channel_liquidities
724 .entry(hop.short_channel_id)
725 .or_insert_with(|| ChannelLiquidity::new())
726 .as_directed_mut(&source, &target, capacity_msat)
727 .successful(amount_msat);
732 impl<G: Deref<Target = NetworkGraph>> Writeable for ProbabilisticScorer<G> {
734 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
735 self._params.write(w)?;
736 self.node_id.write(w)?;
737 self.channel_liquidities.write(w)?;
738 write_tlv_fields!(w, {});
743 impl<G: Deref<Target = NetworkGraph>> ReadableArgs<G> for ProbabilisticScorer<G> {
745 fn read<R: Read>(r: &mut R, args: G) -> Result<Self, DecodeError> {
747 _params: Readable::read(r)?,
748 node_id: Readable::read(r)?,
750 channel_liquidities: Readable::read(r)?,
752 read_tlv_fields!(r, {});
757 impl Writeable for ChannelLiquidity {
759 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
760 write_tlv_fields!(w, {
761 (0, self.min_liquidity_offset_msat, required),
762 (2, self.max_liquidity_offset_msat, required),
768 impl Readable for ChannelLiquidity {
770 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
771 let mut min_liquidity_offset_msat = 0;
772 let mut max_liquidity_offset_msat = 0;
773 read_tlv_fields!(r, {
774 (0, min_liquidity_offset_msat, required),
775 (2, max_liquidity_offset_msat, required),
778 min_liquidity_offset_msat,
779 max_liquidity_offset_msat
784 pub(crate) mod time {
786 use core::time::Duration;
787 /// A measurement of time.
788 pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
789 /// Returns an instance corresponding to the current moment.
792 /// Returns the amount of time elapsed since `self` was created.
793 fn elapsed(&self) -> Duration;
795 /// Returns the amount of time passed since the beginning of [`Time`].
797 /// Used during (de-)serialization.
798 fn duration_since_epoch() -> Duration;
801 /// A state in which time has no meaning.
802 #[derive(Debug, PartialEq, Eq)]
805 #[cfg(not(feature = "no-std"))]
806 impl Time for std::time::Instant {
808 std::time::Instant::now()
811 fn duration_since_epoch() -> Duration {
812 use std::time::SystemTime;
813 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
816 fn elapsed(&self) -> Duration {
817 std::time::Instant::elapsed(self)
821 impl Time for Eternity {
826 fn duration_since_epoch() -> Duration {
827 Duration::from_secs(0)
830 fn elapsed(&self) -> Duration {
831 Duration::from_secs(0)
835 impl Sub<Duration> for Eternity {
838 fn sub(self, _other: Duration) -> Self {
844 pub(crate) use self::time::Time;
848 use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorer, ScoringParameters, ScorerUsingTime, Time};
849 use super::time::Eternity;
851 use ln::features::{ChannelFeatures, NodeFeatures};
852 use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
853 use routing::scoring::Score;
854 use routing::network_graph::{NetworkGraph, NodeId};
855 use routing::router::RouteHop;
856 use util::ser::{Readable, Writeable};
858 use bitcoin::blockdata::constants::genesis_block;
859 use bitcoin::hashes::Hash;
860 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
861 use bitcoin::network::constants::Network;
862 use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
863 use core::cell::Cell;
865 use core::time::Duration;
870 /// Time that can be advanced manually in tests.
871 #[derive(Debug, PartialEq, Eq)]
872 struct SinceEpoch(Duration);
876 static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
879 fn advance(duration: Duration) {
880 Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
884 impl Time for SinceEpoch {
886 Self(Self::duration_since_epoch())
889 fn duration_since_epoch() -> Duration {
890 Self::ELAPSED.with(|elapsed| elapsed.get())
893 fn elapsed(&self) -> Duration {
894 Self::duration_since_epoch() - self.0
898 impl Sub<Duration> for SinceEpoch {
901 fn sub(self, other: Duration) -> Self {
907 fn time_passes_when_advanced() {
908 let now = SinceEpoch::now();
909 assert_eq!(now.elapsed(), Duration::from_secs(0));
911 SinceEpoch::advance(Duration::from_secs(1));
912 SinceEpoch::advance(Duration::from_secs(1));
914 let elapsed = now.elapsed();
915 let later = SinceEpoch::now();
917 assert_eq!(elapsed, Duration::from_secs(2));
918 assert_eq!(later - elapsed, now);
922 fn time_never_passes_in_an_eternity() {
923 let now = Eternity::now();
924 let elapsed = now.elapsed();
925 let later = Eternity::now();
927 assert_eq!(now.elapsed(), Duration::from_secs(0));
928 assert_eq!(later - elapsed, now);
933 /// A scorer for testing with time that can be manually advanced.
934 type Scorer = ScorerUsingTime::<SinceEpoch>;
936 fn source_privkey() -> SecretKey {
937 SecretKey::from_slice(&[42; 32]).unwrap()
940 fn target_privkey() -> SecretKey {
941 SecretKey::from_slice(&[43; 32]).unwrap()
944 fn source_pubkey() -> PublicKey {
945 let secp_ctx = Secp256k1::new();
946 PublicKey::from_secret_key(&secp_ctx, &source_privkey())
949 fn target_pubkey() -> PublicKey {
950 let secp_ctx = Secp256k1::new();
951 PublicKey::from_secret_key(&secp_ctx, &target_privkey())
954 fn source_node_id() -> NodeId {
955 NodeId::from_pubkey(&source_pubkey())
958 fn target_node_id() -> NodeId {
959 NodeId::from_pubkey(&target_pubkey())
963 fn penalizes_without_channel_failures() {
964 let scorer = Scorer::new(ScoringParameters {
965 base_penalty_msat: 1_000,
966 failure_penalty_msat: 512,
967 failure_penalty_half_life: Duration::from_secs(1),
968 overuse_penalty_start_1024th: 1024,
969 overuse_penalty_msat_per_1024th: 0,
971 let source = source_node_id();
972 let target = target_node_id();
973 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
975 SinceEpoch::advance(Duration::from_secs(1));
976 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
980 fn accumulates_channel_failure_penalties() {
981 let mut scorer = Scorer::new(ScoringParameters {
982 base_penalty_msat: 1_000,
983 failure_penalty_msat: 64,
984 failure_penalty_half_life: Duration::from_secs(10),
985 overuse_penalty_start_1024th: 1024,
986 overuse_penalty_msat_per_1024th: 0,
988 let source = source_node_id();
989 let target = target_node_id();
990 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
992 scorer.payment_path_failed(&[], 42);
993 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
995 scorer.payment_path_failed(&[], 42);
996 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
998 scorer.payment_path_failed(&[], 42);
999 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_192);
1003 fn decays_channel_failure_penalties_over_time() {
1004 let mut scorer = Scorer::new(ScoringParameters {
1005 base_penalty_msat: 1_000,
1006 failure_penalty_msat: 512,
1007 failure_penalty_half_life: Duration::from_secs(10),
1008 overuse_penalty_start_1024th: 1024,
1009 overuse_penalty_msat_per_1024th: 0,
1011 let source = source_node_id();
1012 let target = target_node_id();
1013 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1015 scorer.payment_path_failed(&[], 42);
1016 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1018 SinceEpoch::advance(Duration::from_secs(9));
1019 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1021 SinceEpoch::advance(Duration::from_secs(1));
1022 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1024 SinceEpoch::advance(Duration::from_secs(10 * 8));
1025 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_001);
1027 SinceEpoch::advance(Duration::from_secs(10));
1028 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1030 SinceEpoch::advance(Duration::from_secs(10));
1031 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1035 fn decays_channel_failure_penalties_without_shift_overflow() {
1036 let mut scorer = Scorer::new(ScoringParameters {
1037 base_penalty_msat: 1_000,
1038 failure_penalty_msat: 512,
1039 failure_penalty_half_life: Duration::from_secs(10),
1040 overuse_penalty_start_1024th: 1024,
1041 overuse_penalty_msat_per_1024th: 0,
1043 let source = source_node_id();
1044 let target = target_node_id();
1045 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1047 scorer.payment_path_failed(&[], 42);
1048 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1050 // An unchecked right shift 64 bits or more in ChannelFailure::decayed_penalty_msat would
1051 // cause an overflow.
1052 SinceEpoch::advance(Duration::from_secs(10 * 64));
1053 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1055 SinceEpoch::advance(Duration::from_secs(10));
1056 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1060 fn accumulates_channel_failure_penalties_after_decay() {
1061 let mut scorer = Scorer::new(ScoringParameters {
1062 base_penalty_msat: 1_000,
1063 failure_penalty_msat: 512,
1064 failure_penalty_half_life: Duration::from_secs(10),
1065 overuse_penalty_start_1024th: 1024,
1066 overuse_penalty_msat_per_1024th: 0,
1068 let source = source_node_id();
1069 let target = target_node_id();
1070 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1072 scorer.payment_path_failed(&[], 42);
1073 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1075 SinceEpoch::advance(Duration::from_secs(10));
1076 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1078 scorer.payment_path_failed(&[], 42);
1079 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_768);
1081 SinceEpoch::advance(Duration::from_secs(10));
1082 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_384);
1086 fn reduces_channel_failure_penalties_after_success() {
1087 let mut scorer = Scorer::new(ScoringParameters {
1088 base_penalty_msat: 1_000,
1089 failure_penalty_msat: 512,
1090 failure_penalty_half_life: Duration::from_secs(10),
1091 overuse_penalty_start_1024th: 1024,
1092 overuse_penalty_msat_per_1024th: 0,
1094 let source = source_node_id();
1095 let target = target_node_id();
1096 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1098 scorer.payment_path_failed(&[], 42);
1099 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1101 SinceEpoch::advance(Duration::from_secs(10));
1102 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1104 let hop = RouteHop {
1105 pubkey: PublicKey::from_slice(target.as_slice()).unwrap(),
1106 node_features: NodeFeatures::known(),
1107 short_channel_id: 42,
1108 channel_features: ChannelFeatures::known(),
1110 cltv_expiry_delta: 18,
1112 scorer.payment_path_successful(&[&hop]);
1113 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1115 SinceEpoch::advance(Duration::from_secs(10));
1116 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1120 fn restores_persisted_channel_failure_penalties() {
1121 let mut scorer = Scorer::new(ScoringParameters {
1122 base_penalty_msat: 1_000,
1123 failure_penalty_msat: 512,
1124 failure_penalty_half_life: Duration::from_secs(10),
1125 overuse_penalty_start_1024th: 1024,
1126 overuse_penalty_msat_per_1024th: 0,
1128 let source = source_node_id();
1129 let target = target_node_id();
1131 scorer.payment_path_failed(&[], 42);
1132 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1134 SinceEpoch::advance(Duration::from_secs(10));
1135 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1137 scorer.payment_path_failed(&[], 43);
1138 assert_eq!(scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1140 let mut serialized_scorer = Vec::new();
1141 scorer.write(&mut serialized_scorer).unwrap();
1143 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1144 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1145 assert_eq!(deserialized_scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1149 fn decays_persisted_channel_failure_penalties() {
1150 let mut scorer = Scorer::new(ScoringParameters {
1151 base_penalty_msat: 1_000,
1152 failure_penalty_msat: 512,
1153 failure_penalty_half_life: Duration::from_secs(10),
1154 overuse_penalty_start_1024th: 1024,
1155 overuse_penalty_msat_per_1024th: 0,
1157 let source = source_node_id();
1158 let target = target_node_id();
1160 scorer.payment_path_failed(&[], 42);
1161 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1163 let mut serialized_scorer = Vec::new();
1164 scorer.write(&mut serialized_scorer).unwrap();
1166 SinceEpoch::advance(Duration::from_secs(10));
1168 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1169 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1171 SinceEpoch::advance(Duration::from_secs(10));
1172 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1176 fn charges_per_1024th_penalty() {
1177 let scorer = Scorer::new(ScoringParameters {
1178 base_penalty_msat: 0,
1179 failure_penalty_msat: 0,
1180 failure_penalty_half_life: Duration::from_secs(0),
1181 overuse_penalty_start_1024th: 256,
1182 overuse_penalty_msat_per_1024th: 100,
1184 let source = source_node_id();
1185 let target = target_node_id();
1187 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 1_024_000, &source, &target), 0);
1188 assert_eq!(scorer.channel_penalty_msat(42, 256_999, 1_024_000, &source, &target), 0);
1189 assert_eq!(scorer.channel_penalty_msat(42, 257_000, 1_024_000, &source, &target), 100);
1190 assert_eq!(scorer.channel_penalty_msat(42, 258_000, 1_024_000, &source, &target), 200);
1191 assert_eq!(scorer.channel_penalty_msat(42, 512_000, 1_024_000, &source, &target), 256 * 100);
1194 // `ProbabilisticScorer` tests
1196 fn sender_privkey() -> SecretKey {
1197 SecretKey::from_slice(&[41; 32]).unwrap()
1200 fn recipient_privkey() -> SecretKey {
1201 SecretKey::from_slice(&[45; 32]).unwrap()
1204 fn sender_pubkey() -> PublicKey {
1205 let secp_ctx = Secp256k1::new();
1206 PublicKey::from_secret_key(&secp_ctx, &sender_privkey())
1209 fn recipient_pubkey() -> PublicKey {
1210 let secp_ctx = Secp256k1::new();
1211 PublicKey::from_secret_key(&secp_ctx, &recipient_privkey())
1214 fn sender_node_id() -> NodeId {
1215 NodeId::from_pubkey(&sender_pubkey())
1218 fn recipient_node_id() -> NodeId {
1219 NodeId::from_pubkey(&recipient_pubkey())
1222 fn network_graph() -> NetworkGraph {
1223 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1224 let mut network_graph = NetworkGraph::new(genesis_hash);
1225 add_channel(&mut network_graph, 41, sender_privkey(), source_privkey());
1226 add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
1227 add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
1233 network_graph: &mut NetworkGraph, short_channel_id: u64, node_1_key: SecretKey,
1234 node_2_key: SecretKey
1236 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1237 let node_1_secret = &SecretKey::from_slice(&[39; 32]).unwrap();
1238 let node_2_secret = &SecretKey::from_slice(&[40; 32]).unwrap();
1239 let secp_ctx = Secp256k1::new();
1240 let unsigned_announcement = UnsignedChannelAnnouncement {
1241 features: ChannelFeatures::known(),
1242 chain_hash: genesis_hash,
1244 node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_key),
1245 node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_key),
1246 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_secret),
1247 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_secret),
1248 excess_data: Vec::new(),
1250 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1251 let signed_announcement = ChannelAnnouncement {
1252 node_signature_1: secp_ctx.sign(&msghash, &node_1_key),
1253 node_signature_2: secp_ctx.sign(&msghash, &node_2_key),
1254 bitcoin_signature_1: secp_ctx.sign(&msghash, &node_1_secret),
1255 bitcoin_signature_2: secp_ctx.sign(&msghash, &node_2_secret),
1256 contents: unsigned_announcement,
1258 let chain_source: Option<&::util::test_utils::TestChainSource> = None;
1259 network_graph.update_channel_from_announcement(
1260 &signed_announcement, &chain_source, &secp_ctx).unwrap();
1261 update_channel(network_graph, short_channel_id, node_1_key, 0);
1262 update_channel(network_graph, short_channel_id, node_2_key, 1);
1266 network_graph: &mut NetworkGraph, short_channel_id: u64, node_key: SecretKey, flags: u8
1268 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1269 let secp_ctx = Secp256k1::new();
1270 let unsigned_update = UnsignedChannelUpdate {
1271 chain_hash: genesis_hash,
1275 cltv_expiry_delta: 18,
1276 htlc_minimum_msat: 0,
1277 htlc_maximum_msat: OptionalField::Present(1_000),
1279 fee_proportional_millionths: 0,
1280 excess_data: Vec::new(),
1282 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
1283 let signed_update = ChannelUpdate {
1284 signature: secp_ctx.sign(&msghash, &node_key),
1285 contents: unsigned_update,
1287 network_graph.update_channel(&signed_update, &secp_ctx).unwrap();
1290 fn payment_path(amount_msat: u64) -> Vec<RouteHop> {
1293 pubkey: source_pubkey(),
1294 node_features: NodeFeatures::known(),
1295 short_channel_id: 41,
1296 channel_features: ChannelFeatures::known(),
1298 cltv_expiry_delta: 18,
1301 pubkey: target_pubkey(),
1302 node_features: NodeFeatures::known(),
1303 short_channel_id: 42,
1304 channel_features: ChannelFeatures::known(),
1306 cltv_expiry_delta: 18,
1309 pubkey: recipient_pubkey(),
1310 node_features: NodeFeatures::known(),
1311 short_channel_id: 43,
1312 channel_features: ChannelFeatures::known(),
1313 fee_msat: amount_msat,
1314 cltv_expiry_delta: 18,
1320 fn liquidity_bounds_directed_from_lowest_node_id() {
1321 let network_graph = network_graph();
1322 let params = ProbabilisticScoringParameters::default();
1323 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1326 min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1330 min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1332 let source = source_node_id();
1333 let target = target_node_id();
1334 let recipient = recipient_node_id();
1336 let liquidity = scorer.channel_liquidities.get_mut(&42).unwrap();
1337 assert!(source > target);
1338 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 100);
1339 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1340 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1341 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 900);
1343 liquidity.as_directed_mut(&source, &target, 1_000).set_min_liquidity_msat(200);
1344 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 200);
1345 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1346 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1347 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 800);
1349 let liquidity = scorer.channel_liquidities.get_mut(&43).unwrap();
1350 assert!(target < recipient);
1351 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 700);
1352 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 900);
1353 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 100);
1354 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 300);
1356 liquidity.as_directed_mut(&target, &recipient, 1_000).set_max_liquidity_msat(200);
1357 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 0);
1358 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 200);
1359 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 800);
1360 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 1000);
1364 fn increased_penalty_nearing_liquidity_upper_bound() {
1365 let network_graph = network_graph();
1366 let params = ProbabilisticScoringParameters::default();
1367 let scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph);
1368 let source = source_node_id();
1369 let target = target_node_id();
1371 assert_eq!(scorer.channel_penalty_msat(42, 100, 100_000, &source, &target), 0);
1372 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 100_000, &source, &target), 4);
1373 assert_eq!(scorer.channel_penalty_msat(42, 10_000, 100_000, &source, &target), 457);
1374 assert_eq!(scorer.channel_penalty_msat(42, 100_000, 100_000, &source, &target), 500_000);
1376 assert_eq!(scorer.channel_penalty_msat(42, 125, 1_000, &source, &target), 7);
1377 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 31);
1378 assert_eq!(scorer.channel_penalty_msat(42, 375, 1_000, &source, &target), 76);
1379 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 150);
1380 assert_eq!(scorer.channel_penalty_msat(42, 625, 1_000, &source, &target), 265);
1381 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 450);
1382 assert_eq!(scorer.channel_penalty_msat(42, 875, 1_000, &source, &target), 787);
1386 fn constant_penalty_outside_liquidity_bounds() {
1387 let network_graph = network_graph();
1388 let params = ProbabilisticScoringParameters::default();
1389 let scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1391 ChannelLiquidity { min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40 });
1392 let source = source_node_id();
1393 let target = target_node_id();
1395 assert_eq!(scorer.channel_penalty_msat(42, 39, 100, &source, &target), 0);
1396 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 0);
1397 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), u64::max_value());
1398 assert_eq!(scorer.channel_penalty_msat(42, 61, 100, &source, &target), u64::max_value());
1402 fn reduces_liquidity_upper_bound_on_success() {
1403 let network_graph = network_graph();
1404 let params = ProbabilisticScoringParameters::default();
1405 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1407 ChannelLiquidity { min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 0 })
1409 ChannelLiquidity { min_liquidity_offset_msat: 0, max_liquidity_offset_msat: 400 });
1410 let sender = sender_node_id();
1411 let source = source_node_id();
1412 let target = target_node_id();
1413 let recipient = recipient_node_id();
1414 let path = payment_path(200);
1416 assert_eq!(scorer.channel_penalty_msat(41, 200, 1_000, &sender, &source), 0);
1417 assert_eq!(scorer.channel_penalty_msat(42, 200, 1_000, &source, &target), 94);
1418 assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 35);
1420 scorer.payment_path_successful(&path.iter().collect::<Vec<_>>());
1422 assert_eq!(scorer.channel_penalty_msat(41, 200, 1_000, &sender, &source), 0);
1423 assert_eq!(scorer.channel_penalty_msat(42, 200, 1_000, &source, &target), u64::max_value());
1424 assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 59);
1427 // TODO: Add more test coverage