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 {
475 /// A penalty applied after multiplying by the negative log of channel's success probability for
478 /// The success probability is determined by the effective channel capacity, the payment amount,
479 /// and knowledge learned from prior successful and unsuccessful payments.
481 /// Default value: 1,000 msat
482 pub liquidity_penalty_multiplier_msat: u64,
485 impl_writeable_tlv_based!(ProbabilisticScoringParameters, {
486 (0, liquidity_penalty_multiplier_msat, required),
489 /// Accounting for channel liquidity balance uncertainty.
491 /// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
492 /// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
493 /// offset fields gives the opposite direction.
494 struct ChannelLiquidity {
495 min_liquidity_offset_msat: u64,
496 max_liquidity_offset_msat: u64,
499 /// A view of [`ChannelLiquidity`] in one direction assuming a certain channel capacity.
500 struct DirectedChannelLiquidity<L: Deref<Target = u64>> {
501 min_liquidity_offset_msat: L,
502 max_liquidity_offset_msat: L,
506 /// The likelihood of an event occurring.
510 Ratio { numerator: u64, denominator: u64 },
513 impl<G: Deref<Target = NetworkGraph>> ProbabilisticScorer<G> {
514 /// Creates a new scorer using the given scoring parameters for sending payments from a node
515 /// through a network graph.
517 params: ProbabilisticScoringParameters, node_pubkey: PublicKey, network_graph: G
521 node_id: NodeId::from_pubkey(&node_pubkey),
523 channel_liquidities: HashMap::new(),
528 fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity) -> Self {
529 assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
534 impl Default for ProbabilisticScoringParameters {
535 fn default() -> Self {
537 liquidity_penalty_multiplier_msat: 1000,
542 impl ChannelLiquidity {
546 min_liquidity_offset_msat: 0,
547 max_liquidity_offset_msat: 0,
551 /// Returns a view of the channel liquidity directed from `source` to `target` assuming
554 &self, source: &NodeId, target: &NodeId, capacity_msat: u64
555 ) -> DirectedChannelLiquidity<&u64> {
556 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
557 (&self.min_liquidity_offset_msat, &self.max_liquidity_offset_msat)
559 (&self.max_liquidity_offset_msat, &self.min_liquidity_offset_msat)
562 DirectedChannelLiquidity {
563 min_liquidity_offset_msat,
564 max_liquidity_offset_msat,
569 /// Returns a mutable view of the channel liquidity directed from `source` to `target` assuming
572 &mut self, source: &NodeId, target: &NodeId, capacity_msat: u64
573 ) -> DirectedChannelLiquidity<&mut u64> {
574 let (min_liquidity_offset_msat, max_liquidity_offset_msat) = if source < target {
575 (&mut self.min_liquidity_offset_msat, &mut self.max_liquidity_offset_msat)
577 (&mut self.max_liquidity_offset_msat, &mut self.min_liquidity_offset_msat)
580 DirectedChannelLiquidity {
581 min_liquidity_offset_msat,
582 max_liquidity_offset_msat,
588 impl<L: Deref<Target = u64>> DirectedChannelLiquidity<L> {
589 /// Returns the success probability of routing the given HTLC `amount_msat` through the channel
590 /// in this direction.
591 fn success_probability(&self, amount_msat: u64) -> Probability {
592 let max_liquidity_msat = self.max_liquidity_msat();
593 let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
594 if amount_msat > max_liquidity_msat {
596 } else if amount_msat < min_liquidity_msat {
599 let numerator = max_liquidity_msat + 1 - amount_msat;
600 let denominator = max_liquidity_msat + 1 - min_liquidity_msat;
601 if numerator == denominator {
604 Probability::Ratio { numerator, denominator }
609 /// Returns the lower bound of the channel liquidity balance in this direction.
610 fn min_liquidity_msat(&self) -> u64 {
611 *self.min_liquidity_offset_msat
614 /// Returns the upper bound of the channel liquidity balance in this direction.
615 fn max_liquidity_msat(&self) -> u64 {
616 self.capacity_msat.checked_sub(*self.max_liquidity_offset_msat).unwrap_or(0)
620 impl<L: DerefMut<Target = u64>> DirectedChannelLiquidity<L> {
621 /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
622 fn failed_at_channel(&mut self, amount_msat: u64) {
623 if amount_msat < self.max_liquidity_msat() {
624 self.set_max_liquidity_msat(amount_msat);
628 /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
629 fn failed_downstream(&mut self, amount_msat: u64) {
630 if amount_msat > self.min_liquidity_msat() {
631 self.set_min_liquidity_msat(amount_msat);
635 /// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
636 fn successful(&mut self, amount_msat: u64) {
637 let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
638 self.set_max_liquidity_msat(max_liquidity_msat);
641 /// Adjusts the lower bound of the channel liquidity balance in this direction.
642 fn set_min_liquidity_msat(&mut self, amount_msat: u64) {
643 *self.min_liquidity_offset_msat = amount_msat;
645 if amount_msat > self.max_liquidity_msat() {
646 *self.max_liquidity_offset_msat = 0;
650 /// Adjusts the upper bound of the channel liquidity balance in this direction.
651 fn set_max_liquidity_msat(&mut self, amount_msat: u64) {
652 *self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
654 if amount_msat < self.min_liquidity_msat() {
655 *self.min_liquidity_offset_msat = 0;
660 impl<G: Deref<Target = NetworkGraph>> Score for ProbabilisticScorer<G> {
661 fn channel_penalty_msat(
662 &self, short_channel_id: u64, amount_msat: u64, capacity_msat: u64, source: &NodeId,
665 if *source == self.node_id || *target == self.node_id {
669 let liquidity_penalty_multiplier_msat = self.params.liquidity_penalty_multiplier_msat;
670 let success_probability = self.channel_liquidities
671 .get(&short_channel_id)
672 .unwrap_or(&ChannelLiquidity::new())
673 .as_directed(source, target, capacity_msat)
674 .success_probability(amount_msat);
675 match success_probability {
676 Probability::Zero => u64::max_value(),
677 Probability::One => 0,
678 Probability::Ratio { numerator, denominator } => {
679 let success_probability = numerator as f64 / denominator as f64;
680 (-(success_probability.log10()) * liquidity_penalty_multiplier_msat as f64) as u64
685 fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
686 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
687 let network_graph = self.network_graph.read_only();
688 let hop_sources = core::iter::once(self.node_id)
689 .chain(path.iter().map(|hop| NodeId::from_pubkey(&hop.pubkey)));
690 for (source, hop) in hop_sources.zip(path.iter()) {
691 let target = NodeId::from_pubkey(&hop.pubkey);
692 if source == self.node_id || target == self.node_id {
696 let capacity_msat = network_graph.channels()
697 .get(&hop.short_channel_id)
698 .and_then(|channel| channel.as_directed_to(&target).map(|d| d.effective_capacity()))
699 .unwrap_or(EffectiveCapacity::Unknown)
702 if hop.short_channel_id == short_channel_id {
703 self.channel_liquidities
704 .entry(hop.short_channel_id)
705 .or_insert_with(|| ChannelLiquidity::new())
706 .as_directed_mut(&source, &target, capacity_msat)
707 .failed_at_channel(amount_msat);
711 self.channel_liquidities
712 .entry(hop.short_channel_id)
713 .or_insert_with(|| ChannelLiquidity::new())
714 .as_directed_mut(&source, &target, capacity_msat)
715 .failed_downstream(amount_msat);
719 fn payment_path_successful(&mut self, path: &[&RouteHop]) {
720 let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
721 let network_graph = self.network_graph.read_only();
722 let hop_sources = core::iter::once(self.node_id)
723 .chain(path.iter().map(|hop| NodeId::from_pubkey(&hop.pubkey)));
724 for (source, hop) in hop_sources.zip(path.iter()) {
725 let target = NodeId::from_pubkey(&hop.pubkey);
726 if source == self.node_id || target == self.node_id {
730 let capacity_msat = network_graph.channels()
731 .get(&hop.short_channel_id)
732 .and_then(|channel| channel.as_directed_to(&target).map(|d| d.effective_capacity()))
733 .unwrap_or(EffectiveCapacity::Unknown)
736 self.channel_liquidities
737 .entry(hop.short_channel_id)
738 .or_insert_with(|| ChannelLiquidity::new())
739 .as_directed_mut(&source, &target, capacity_msat)
740 .successful(amount_msat);
745 impl<G: Deref<Target = NetworkGraph>> Writeable for ProbabilisticScorer<G> {
747 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
748 self.params.write(w)?;
749 self.node_id.write(w)?;
750 self.channel_liquidities.write(w)?;
751 write_tlv_fields!(w, {});
756 impl<G: Deref<Target = NetworkGraph>> ReadableArgs<G> for ProbabilisticScorer<G> {
758 fn read<R: Read>(r: &mut R, args: G) -> Result<Self, DecodeError> {
760 params: Readable::read(r)?,
761 node_id: Readable::read(r)?,
763 channel_liquidities: Readable::read(r)?,
765 read_tlv_fields!(r, {});
770 impl Writeable for ChannelLiquidity {
772 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
773 write_tlv_fields!(w, {
774 (0, self.min_liquidity_offset_msat, required),
775 (2, self.max_liquidity_offset_msat, required),
781 impl Readable for ChannelLiquidity {
783 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
784 let mut min_liquidity_offset_msat = 0;
785 let mut max_liquidity_offset_msat = 0;
786 read_tlv_fields!(r, {
787 (0, min_liquidity_offset_msat, required),
788 (2, max_liquidity_offset_msat, required),
791 min_liquidity_offset_msat,
792 max_liquidity_offset_msat
797 pub(crate) mod time {
799 use core::time::Duration;
800 /// A measurement of time.
801 pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
802 /// Returns an instance corresponding to the current moment.
805 /// Returns the amount of time elapsed since `self` was created.
806 fn elapsed(&self) -> Duration;
808 /// Returns the amount of time passed since the beginning of [`Time`].
810 /// Used during (de-)serialization.
811 fn duration_since_epoch() -> Duration;
814 /// A state in which time has no meaning.
815 #[derive(Debug, PartialEq, Eq)]
818 #[cfg(not(feature = "no-std"))]
819 impl Time for std::time::Instant {
821 std::time::Instant::now()
824 fn duration_since_epoch() -> Duration {
825 use std::time::SystemTime;
826 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
829 fn elapsed(&self) -> Duration {
830 std::time::Instant::elapsed(self)
834 impl Time for Eternity {
839 fn duration_since_epoch() -> Duration {
840 Duration::from_secs(0)
843 fn elapsed(&self) -> Duration {
844 Duration::from_secs(0)
848 impl Sub<Duration> for Eternity {
851 fn sub(self, _other: Duration) -> Self {
857 pub(crate) use self::time::Time;
861 use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorer, ScoringParameters, ScorerUsingTime, Time};
862 use super::time::Eternity;
864 use ln::features::{ChannelFeatures, NodeFeatures};
865 use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
866 use routing::scoring::Score;
867 use routing::network_graph::{NetworkGraph, NodeId};
868 use routing::router::RouteHop;
869 use util::ser::{Readable, Writeable};
871 use bitcoin::blockdata::constants::genesis_block;
872 use bitcoin::hashes::Hash;
873 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
874 use bitcoin::network::constants::Network;
875 use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
876 use core::cell::Cell;
878 use core::time::Duration;
883 /// Time that can be advanced manually in tests.
884 #[derive(Debug, PartialEq, Eq)]
885 struct SinceEpoch(Duration);
889 static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
892 fn advance(duration: Duration) {
893 Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
897 impl Time for SinceEpoch {
899 Self(Self::duration_since_epoch())
902 fn duration_since_epoch() -> Duration {
903 Self::ELAPSED.with(|elapsed| elapsed.get())
906 fn elapsed(&self) -> Duration {
907 Self::duration_since_epoch() - self.0
911 impl Sub<Duration> for SinceEpoch {
914 fn sub(self, other: Duration) -> Self {
920 fn time_passes_when_advanced() {
921 let now = SinceEpoch::now();
922 assert_eq!(now.elapsed(), Duration::from_secs(0));
924 SinceEpoch::advance(Duration::from_secs(1));
925 SinceEpoch::advance(Duration::from_secs(1));
927 let elapsed = now.elapsed();
928 let later = SinceEpoch::now();
930 assert_eq!(elapsed, Duration::from_secs(2));
931 assert_eq!(later - elapsed, now);
935 fn time_never_passes_in_an_eternity() {
936 let now = Eternity::now();
937 let elapsed = now.elapsed();
938 let later = Eternity::now();
940 assert_eq!(now.elapsed(), Duration::from_secs(0));
941 assert_eq!(later - elapsed, now);
946 /// A scorer for testing with time that can be manually advanced.
947 type Scorer = ScorerUsingTime::<SinceEpoch>;
949 fn source_privkey() -> SecretKey {
950 SecretKey::from_slice(&[42; 32]).unwrap()
953 fn target_privkey() -> SecretKey {
954 SecretKey::from_slice(&[43; 32]).unwrap()
957 fn source_pubkey() -> PublicKey {
958 let secp_ctx = Secp256k1::new();
959 PublicKey::from_secret_key(&secp_ctx, &source_privkey())
962 fn target_pubkey() -> PublicKey {
963 let secp_ctx = Secp256k1::new();
964 PublicKey::from_secret_key(&secp_ctx, &target_privkey())
967 fn source_node_id() -> NodeId {
968 NodeId::from_pubkey(&source_pubkey())
971 fn target_node_id() -> NodeId {
972 NodeId::from_pubkey(&target_pubkey())
976 fn penalizes_without_channel_failures() {
977 let scorer = Scorer::new(ScoringParameters {
978 base_penalty_msat: 1_000,
979 failure_penalty_msat: 512,
980 failure_penalty_half_life: Duration::from_secs(1),
981 overuse_penalty_start_1024th: 1024,
982 overuse_penalty_msat_per_1024th: 0,
984 let source = source_node_id();
985 let target = target_node_id();
986 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
988 SinceEpoch::advance(Duration::from_secs(1));
989 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
993 fn accumulates_channel_failure_penalties() {
994 let mut scorer = Scorer::new(ScoringParameters {
995 base_penalty_msat: 1_000,
996 failure_penalty_msat: 64,
997 failure_penalty_half_life: Duration::from_secs(10),
998 overuse_penalty_start_1024th: 1024,
999 overuse_penalty_msat_per_1024th: 0,
1001 let source = source_node_id();
1002 let target = target_node_id();
1003 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1005 scorer.payment_path_failed(&[], 42);
1006 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1008 scorer.payment_path_failed(&[], 42);
1009 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1011 scorer.payment_path_failed(&[], 42);
1012 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_192);
1016 fn decays_channel_failure_penalties_over_time() {
1017 let mut scorer = Scorer::new(ScoringParameters {
1018 base_penalty_msat: 1_000,
1019 failure_penalty_msat: 512,
1020 failure_penalty_half_life: Duration::from_secs(10),
1021 overuse_penalty_start_1024th: 1024,
1022 overuse_penalty_msat_per_1024th: 0,
1024 let source = source_node_id();
1025 let target = target_node_id();
1026 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1028 scorer.payment_path_failed(&[], 42);
1029 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1031 SinceEpoch::advance(Duration::from_secs(9));
1032 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1034 SinceEpoch::advance(Duration::from_secs(1));
1035 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1037 SinceEpoch::advance(Duration::from_secs(10 * 8));
1038 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_001);
1040 SinceEpoch::advance(Duration::from_secs(10));
1041 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1043 SinceEpoch::advance(Duration::from_secs(10));
1044 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1048 fn decays_channel_failure_penalties_without_shift_overflow() {
1049 let mut scorer = Scorer::new(ScoringParameters {
1050 base_penalty_msat: 1_000,
1051 failure_penalty_msat: 512,
1052 failure_penalty_half_life: Duration::from_secs(10),
1053 overuse_penalty_start_1024th: 1024,
1054 overuse_penalty_msat_per_1024th: 0,
1056 let source = source_node_id();
1057 let target = target_node_id();
1058 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1060 scorer.payment_path_failed(&[], 42);
1061 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1063 // An unchecked right shift 64 bits or more in ChannelFailure::decayed_penalty_msat would
1064 // cause an overflow.
1065 SinceEpoch::advance(Duration::from_secs(10 * 64));
1066 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1068 SinceEpoch::advance(Duration::from_secs(10));
1069 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1073 fn accumulates_channel_failure_penalties_after_decay() {
1074 let mut scorer = Scorer::new(ScoringParameters {
1075 base_penalty_msat: 1_000,
1076 failure_penalty_msat: 512,
1077 failure_penalty_half_life: Duration::from_secs(10),
1078 overuse_penalty_start_1024th: 1024,
1079 overuse_penalty_msat_per_1024th: 0,
1081 let source = source_node_id();
1082 let target = target_node_id();
1083 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1085 scorer.payment_path_failed(&[], 42);
1086 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1088 SinceEpoch::advance(Duration::from_secs(10));
1089 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1091 scorer.payment_path_failed(&[], 42);
1092 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_768);
1094 SinceEpoch::advance(Duration::from_secs(10));
1095 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_384);
1099 fn reduces_channel_failure_penalties_after_success() {
1100 let mut scorer = Scorer::new(ScoringParameters {
1101 base_penalty_msat: 1_000,
1102 failure_penalty_msat: 512,
1103 failure_penalty_half_life: Duration::from_secs(10),
1104 overuse_penalty_start_1024th: 1024,
1105 overuse_penalty_msat_per_1024th: 0,
1107 let source = source_node_id();
1108 let target = target_node_id();
1109 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_000);
1111 scorer.payment_path_failed(&[], 42);
1112 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1114 SinceEpoch::advance(Duration::from_secs(10));
1115 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1117 let hop = RouteHop {
1118 pubkey: PublicKey::from_slice(target.as_slice()).unwrap(),
1119 node_features: NodeFeatures::known(),
1120 short_channel_id: 42,
1121 channel_features: ChannelFeatures::known(),
1123 cltv_expiry_delta: 18,
1125 scorer.payment_path_successful(&[&hop]);
1126 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1128 SinceEpoch::advance(Duration::from_secs(10));
1129 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_064);
1133 fn restores_persisted_channel_failure_penalties() {
1134 let mut scorer = Scorer::new(ScoringParameters {
1135 base_penalty_msat: 1_000,
1136 failure_penalty_msat: 512,
1137 failure_penalty_half_life: Duration::from_secs(10),
1138 overuse_penalty_start_1024th: 1024,
1139 overuse_penalty_msat_per_1024th: 0,
1141 let source = source_node_id();
1142 let target = target_node_id();
1144 scorer.payment_path_failed(&[], 42);
1145 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1147 SinceEpoch::advance(Duration::from_secs(10));
1148 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1150 scorer.payment_path_failed(&[], 43);
1151 assert_eq!(scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1153 let mut serialized_scorer = Vec::new();
1154 scorer.write(&mut serialized_scorer).unwrap();
1156 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1157 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1158 assert_eq!(deserialized_scorer.channel_penalty_msat(43, 1, 1, &source, &target), 1_512);
1162 fn decays_persisted_channel_failure_penalties() {
1163 let mut scorer = Scorer::new(ScoringParameters {
1164 base_penalty_msat: 1_000,
1165 failure_penalty_msat: 512,
1166 failure_penalty_half_life: Duration::from_secs(10),
1167 overuse_penalty_start_1024th: 1024,
1168 overuse_penalty_msat_per_1024th: 0,
1170 let source = source_node_id();
1171 let target = target_node_id();
1173 scorer.payment_path_failed(&[], 42);
1174 assert_eq!(scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_512);
1176 let mut serialized_scorer = Vec::new();
1177 scorer.write(&mut serialized_scorer).unwrap();
1179 SinceEpoch::advance(Duration::from_secs(10));
1181 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
1182 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_256);
1184 SinceEpoch::advance(Duration::from_secs(10));
1185 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, 1, &source, &target), 1_128);
1189 fn charges_per_1024th_penalty() {
1190 let scorer = Scorer::new(ScoringParameters {
1191 base_penalty_msat: 0,
1192 failure_penalty_msat: 0,
1193 failure_penalty_half_life: Duration::from_secs(0),
1194 overuse_penalty_start_1024th: 256,
1195 overuse_penalty_msat_per_1024th: 100,
1197 let source = source_node_id();
1198 let target = target_node_id();
1200 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 1_024_000, &source, &target), 0);
1201 assert_eq!(scorer.channel_penalty_msat(42, 256_999, 1_024_000, &source, &target), 0);
1202 assert_eq!(scorer.channel_penalty_msat(42, 257_000, 1_024_000, &source, &target), 100);
1203 assert_eq!(scorer.channel_penalty_msat(42, 258_000, 1_024_000, &source, &target), 200);
1204 assert_eq!(scorer.channel_penalty_msat(42, 512_000, 1_024_000, &source, &target), 256 * 100);
1207 // `ProbabilisticScorer` tests
1209 fn sender_privkey() -> SecretKey {
1210 SecretKey::from_slice(&[41; 32]).unwrap()
1213 fn recipient_privkey() -> SecretKey {
1214 SecretKey::from_slice(&[45; 32]).unwrap()
1217 fn sender_pubkey() -> PublicKey {
1218 let secp_ctx = Secp256k1::new();
1219 PublicKey::from_secret_key(&secp_ctx, &sender_privkey())
1222 fn recipient_pubkey() -> PublicKey {
1223 let secp_ctx = Secp256k1::new();
1224 PublicKey::from_secret_key(&secp_ctx, &recipient_privkey())
1227 fn sender_node_id() -> NodeId {
1228 NodeId::from_pubkey(&sender_pubkey())
1231 fn recipient_node_id() -> NodeId {
1232 NodeId::from_pubkey(&recipient_pubkey())
1235 fn network_graph() -> NetworkGraph {
1236 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1237 let mut network_graph = NetworkGraph::new(genesis_hash);
1238 add_channel(&mut network_graph, 41, sender_privkey(), source_privkey());
1239 add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
1240 add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
1246 network_graph: &mut NetworkGraph, short_channel_id: u64, node_1_key: SecretKey,
1247 node_2_key: SecretKey
1249 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1250 let node_1_secret = &SecretKey::from_slice(&[39; 32]).unwrap();
1251 let node_2_secret = &SecretKey::from_slice(&[40; 32]).unwrap();
1252 let secp_ctx = Secp256k1::new();
1253 let unsigned_announcement = UnsignedChannelAnnouncement {
1254 features: ChannelFeatures::known(),
1255 chain_hash: genesis_hash,
1257 node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_key),
1258 node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_key),
1259 bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_secret),
1260 bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_secret),
1261 excess_data: Vec::new(),
1263 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
1264 let signed_announcement = ChannelAnnouncement {
1265 node_signature_1: secp_ctx.sign(&msghash, &node_1_key),
1266 node_signature_2: secp_ctx.sign(&msghash, &node_2_key),
1267 bitcoin_signature_1: secp_ctx.sign(&msghash, &node_1_secret),
1268 bitcoin_signature_2: secp_ctx.sign(&msghash, &node_2_secret),
1269 contents: unsigned_announcement,
1271 let chain_source: Option<&::util::test_utils::TestChainSource> = None;
1272 network_graph.update_channel_from_announcement(
1273 &signed_announcement, &chain_source, &secp_ctx).unwrap();
1274 update_channel(network_graph, short_channel_id, node_1_key, 0);
1275 update_channel(network_graph, short_channel_id, node_2_key, 1);
1279 network_graph: &mut NetworkGraph, short_channel_id: u64, node_key: SecretKey, flags: u8
1281 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1282 let secp_ctx = Secp256k1::new();
1283 let unsigned_update = UnsignedChannelUpdate {
1284 chain_hash: genesis_hash,
1288 cltv_expiry_delta: 18,
1289 htlc_minimum_msat: 0,
1290 htlc_maximum_msat: OptionalField::Present(1_000),
1292 fee_proportional_millionths: 0,
1293 excess_data: Vec::new(),
1295 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
1296 let signed_update = ChannelUpdate {
1297 signature: secp_ctx.sign(&msghash, &node_key),
1298 contents: unsigned_update,
1300 network_graph.update_channel(&signed_update, &secp_ctx).unwrap();
1303 fn payment_path(amount_msat: u64) -> Vec<RouteHop> {
1306 pubkey: source_pubkey(),
1307 node_features: NodeFeatures::known(),
1308 short_channel_id: 41,
1309 channel_features: ChannelFeatures::known(),
1311 cltv_expiry_delta: 18,
1314 pubkey: target_pubkey(),
1315 node_features: NodeFeatures::known(),
1316 short_channel_id: 42,
1317 channel_features: ChannelFeatures::known(),
1319 cltv_expiry_delta: 18,
1322 pubkey: recipient_pubkey(),
1323 node_features: NodeFeatures::known(),
1324 short_channel_id: 43,
1325 channel_features: ChannelFeatures::known(),
1326 fee_msat: amount_msat,
1327 cltv_expiry_delta: 18,
1333 fn liquidity_bounds_directed_from_lowest_node_id() {
1334 let network_graph = network_graph();
1335 let params = ProbabilisticScoringParameters::default();
1336 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1339 min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1343 min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 100
1345 let source = source_node_id();
1346 let target = target_node_id();
1347 let recipient = recipient_node_id();
1349 let liquidity = scorer.channel_liquidities.get_mut(&42).unwrap();
1350 assert!(source > target);
1351 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 100);
1352 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1353 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1354 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 900);
1356 liquidity.as_directed_mut(&source, &target, 1_000).set_min_liquidity_msat(200);
1357 assert_eq!(liquidity.as_directed(&source, &target, 1_000).min_liquidity_msat(), 200);
1358 assert_eq!(liquidity.as_directed(&source, &target, 1_000).max_liquidity_msat(), 300);
1359 assert_eq!(liquidity.as_directed(&target, &source, 1_000).min_liquidity_msat(), 700);
1360 assert_eq!(liquidity.as_directed(&target, &source, 1_000).max_liquidity_msat(), 800);
1362 let liquidity = scorer.channel_liquidities.get_mut(&43).unwrap();
1363 assert!(target < recipient);
1364 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 700);
1365 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 900);
1366 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 100);
1367 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 300);
1369 liquidity.as_directed_mut(&target, &recipient, 1_000).set_max_liquidity_msat(200);
1370 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).min_liquidity_msat(), 0);
1371 assert_eq!(liquidity.as_directed(&target, &recipient, 1_000).max_liquidity_msat(), 200);
1372 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).min_liquidity_msat(), 800);
1373 assert_eq!(liquidity.as_directed(&recipient, &target, 1_000).max_liquidity_msat(), 1000);
1377 fn increased_penalty_nearing_liquidity_upper_bound() {
1378 let network_graph = network_graph();
1379 let params = ProbabilisticScoringParameters::default();
1380 let scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph);
1381 let source = source_node_id();
1382 let target = target_node_id();
1384 assert_eq!(scorer.channel_penalty_msat(42, 100, 100_000, &source, &target), 0);
1385 assert_eq!(scorer.channel_penalty_msat(42, 1_000, 100_000, &source, &target), 4);
1386 assert_eq!(scorer.channel_penalty_msat(42, 10_000, 100_000, &source, &target), 45);
1387 assert_eq!(scorer.channel_penalty_msat(42, 100_000, 100_000, &source, &target), 5_000);
1389 assert_eq!(scorer.channel_penalty_msat(42, 125, 1_000, &source, &target), 57);
1390 assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124);
1391 assert_eq!(scorer.channel_penalty_msat(42, 375, 1_000, &source, &target), 203);
1392 assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1393 assert_eq!(scorer.channel_penalty_msat(42, 625, 1_000, &source, &target), 425);
1394 assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600);
1395 assert_eq!(scorer.channel_penalty_msat(42, 875, 1_000, &source, &target), 900);
1399 fn constant_penalty_outside_liquidity_bounds() {
1400 let network_graph = network_graph();
1401 let params = ProbabilisticScoringParameters::default();
1402 let scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1404 ChannelLiquidity { min_liquidity_offset_msat: 40, max_liquidity_offset_msat: 40 });
1405 let source = source_node_id();
1406 let target = target_node_id();
1408 assert_eq!(scorer.channel_penalty_msat(42, 39, 100, &source, &target), 0);
1409 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), 0);
1410 assert_ne!(scorer.channel_penalty_msat(42, 50, 100, &source, &target), u64::max_value());
1411 assert_eq!(scorer.channel_penalty_msat(42, 61, 100, &source, &target), u64::max_value());
1415 fn reduces_liquidity_upper_bound_on_success() {
1416 let network_graph = network_graph();
1417 let params = ProbabilisticScoringParameters::default();
1418 let mut scorer = ProbabilisticScorer::new(params, sender_pubkey(), &network_graph)
1420 ChannelLiquidity { min_liquidity_offset_msat: 700, max_liquidity_offset_msat: 0 })
1422 ChannelLiquidity { min_liquidity_offset_msat: 0, max_liquidity_offset_msat: 400 });
1423 let sender = sender_node_id();
1424 let source = source_node_id();
1425 let target = target_node_id();
1426 let recipient = recipient_node_id();
1427 let path = payment_path(200);
1429 assert_eq!(scorer.channel_penalty_msat(41, 200, 1_000, &sender, &source), 0);
1430 assert_eq!(scorer.channel_penalty_msat(42, 200, 1_000, &source, &target), 474);
1431 assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 175);
1433 scorer.payment_path_successful(&path.iter().collect::<Vec<_>>());
1435 assert_eq!(scorer.channel_penalty_msat(41, 200, 1_000, &sender, &source), 0);
1436 assert_eq!(scorer.channel_penalty_msat(42, 200, 1_000, &source, &target), u64::max_value());
1437 assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 299);
1440 // TODO: Add more test coverage