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 [`routing::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::scorer::{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 //! If persisting [`Scorer`], it must be restored using the same [`Time`] parameterization. Using a
50 //! different type results in undefined behavior. Specifically, persisting when built with feature
51 //! `no-std` and restoring without it, or vice versa, uses different types and thus is undefined.
53 //! [`find_route`]: crate::routing::router::find_route
57 use ln::msgs::DecodeError;
58 use routing::network_graph::NodeId;
59 use routing::router::RouteHop;
60 use util::ser::{Readable, Writeable, Writer};
64 use core::time::Duration;
67 /// [`routing::Score`] implementation that provides reasonable default behavior.
69 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
70 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
72 /// See [module-level documentation] for usage.
74 /// [module-level documentation]: crate::routing::scorer
75 pub type Scorer = ScorerUsingTime::<DefaultTime>;
77 /// Time used by [`Scorer`].
78 #[cfg(not(feature = "no-std"))]
79 pub type DefaultTime = std::time::Instant;
81 /// Time used by [`Scorer`].
82 #[cfg(feature = "no-std")]
83 pub type DefaultTime = Eternity;
85 /// [`routing::Score`] implementation parameterized by [`Time`].
87 /// See [`Scorer`] for details.
91 /// Mixing [`Time`] types between serialization and deserialization results in undefined behavior.
92 pub struct ScorerUsingTime<T: Time> {
93 params: ScoringParameters,
94 // TODO: Remove entries of closed channels.
95 channel_failures: HashMap<u64, ChannelFailure<T>>,
98 /// Parameters for configuring [`Scorer`].
99 pub struct ScoringParameters {
100 /// A fixed penalty in msats to apply to each channel.
102 /// Default value: 500 msat
103 pub base_penalty_msat: u64,
105 /// A penalty in msats to apply to a channel upon failing to relay a payment.
107 /// This accumulates for each failure but may be reduced over time based on
108 /// [`failure_penalty_half_life`].
110 /// Default value: 1,024,000 msat
112 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
113 pub failure_penalty_msat: u64,
115 /// When the amount being sent over a channel is this many 1024ths of the total channel
116 /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
118 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
120 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
121 pub overuse_penalty_start_1024th: u16,
123 /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
124 /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
126 /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
127 /// to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
129 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
130 pub overuse_penalty_msat_per_1024th: u64,
132 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
137 /// When time is an [`Eternity`], as is default when enabling feature `no-std`, it will never
138 /// elapse. Therefore, this penalty will never decay.
140 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
141 pub failure_penalty_half_life: Duration,
144 impl_writeable_tlv_based!(ScoringParameters, {
145 (0, base_penalty_msat, required),
146 (1, overuse_penalty_start_1024th, (default_value, 128)),
147 (2, failure_penalty_msat, required),
148 (3, overuse_penalty_msat_per_1024th, (default_value, 20)),
149 (4, failure_penalty_half_life, required),
152 /// Accounting for penalties against a channel for failing to relay any payments.
154 /// Penalties decay over time, though accumulate as more failures occur.
155 struct ChannelFailure<T: Time> {
156 /// Accumulated penalty in msats for the channel as of `last_failed`.
157 undecayed_penalty_msat: u64,
159 /// Last time the channel failed. Used to decay `undecayed_penalty_msat`.
163 /// A measurement of time.
164 pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
165 /// Returns an instance corresponding to the current moment.
168 /// Returns the amount of time elapsed since `self` was created.
169 fn elapsed(&self) -> Duration;
171 /// Returns the amount of time passed since the beginning of [`Time`].
173 /// Used during (de-)serialization.
174 fn duration_since_epoch() -> Duration;
177 impl<T: Time> ScorerUsingTime<T> {
178 /// Creates a new scorer using the given scoring parameters.
179 pub fn new(params: ScoringParameters) -> Self {
182 channel_failures: HashMap::new(),
186 /// Creates a new scorer using `penalty_msat` as a fixed channel penalty.
187 #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
188 pub fn with_fixed_penalty(penalty_msat: u64) -> Self {
189 Self::new(ScoringParameters {
190 base_penalty_msat: penalty_msat,
191 failure_penalty_msat: 0,
192 failure_penalty_half_life: Duration::from_secs(0),
193 overuse_penalty_start_1024th: 1024,
194 overuse_penalty_msat_per_1024th: 0,
199 impl<T: Time> ChannelFailure<T> {
200 fn new(failure_penalty_msat: u64) -> Self {
202 undecayed_penalty_msat: failure_penalty_msat,
203 last_failed: T::now(),
207 fn add_penalty(&mut self, failure_penalty_msat: u64, half_life: Duration) {
208 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) + failure_penalty_msat;
209 self.last_failed = T::now();
212 fn decayed_penalty_msat(&self, half_life: Duration) -> u64 {
213 let decays = self.last_failed.elapsed().as_secs().checked_div(half_life.as_secs());
215 Some(decays) => self.undecayed_penalty_msat >> decays,
221 impl<T: Time> Default for ScorerUsingTime<T> {
222 fn default() -> Self {
223 Self::new(ScoringParameters::default())
227 impl Default for ScoringParameters {
228 fn default() -> Self {
230 base_penalty_msat: 500,
231 failure_penalty_msat: 1024 * 1000,
232 failure_penalty_half_life: Duration::from_secs(3600),
233 overuse_penalty_start_1024th: 1024 / 8,
234 overuse_penalty_msat_per_1024th: 20,
239 impl<T: Time> routing::Score for ScorerUsingTime<T> {
240 fn channel_penalty_msat(
241 &self, short_channel_id: u64, send_amt_msat: u64, chan_capacity_opt: Option<u64>, _source: &NodeId, _target: &NodeId
243 let failure_penalty_msat = self.channel_failures
244 .get(&short_channel_id)
245 .map_or(0, |value| value.decayed_penalty_msat(self.params.failure_penalty_half_life));
247 let mut penalty_msat = self.params.base_penalty_msat + failure_penalty_msat;
249 if let Some(chan_capacity_msat) = chan_capacity_opt {
250 let send_1024ths = send_amt_msat.checked_mul(1024).unwrap_or(u64::max_value()) / chan_capacity_msat;
252 if send_1024ths > self.params.overuse_penalty_start_1024th as u64 {
253 penalty_msat = penalty_msat.checked_add(
254 (send_1024ths - self.params.overuse_penalty_start_1024th as u64)
255 .checked_mul(self.params.overuse_penalty_msat_per_1024th).unwrap_or(u64::max_value()))
256 .unwrap_or(u64::max_value());
263 fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
264 let failure_penalty_msat = self.params.failure_penalty_msat;
265 let half_life = self.params.failure_penalty_half_life;
266 self.channel_failures
267 .entry(short_channel_id)
268 .and_modify(|failure| failure.add_penalty(failure_penalty_msat, half_life))
269 .or_insert_with(|| ChannelFailure::new(failure_penalty_msat));
273 #[cfg(not(feature = "no-std"))]
274 impl Time for std::time::Instant {
276 std::time::Instant::now()
279 fn duration_since_epoch() -> Duration {
280 use std::time::SystemTime;
281 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
284 fn elapsed(&self) -> Duration {
285 std::time::Instant::elapsed(self)
289 /// A state in which time has no meaning.
290 #[derive(Debug, PartialEq, Eq)]
293 impl Time for Eternity {
298 fn duration_since_epoch() -> Duration {
299 Duration::from_secs(0)
302 fn elapsed(&self) -> Duration {
303 Duration::from_secs(0)
307 impl Sub<Duration> for Eternity {
310 fn sub(self, _other: Duration) -> Self {
315 impl<T: Time> Writeable for ScorerUsingTime<T> {
317 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
318 self.params.write(w)?;
319 self.channel_failures.write(w)?;
320 write_tlv_fields!(w, {});
325 impl<T: Time> Readable for ScorerUsingTime<T> {
327 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
329 params: Readable::read(r)?,
330 channel_failures: Readable::read(r)?,
332 read_tlv_fields!(r, {});
337 impl<T: Time> Writeable for ChannelFailure<T> {
339 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
340 let duration_since_epoch = T::duration_since_epoch() - self.last_failed.elapsed();
341 write_tlv_fields!(w, {
342 (0, self.undecayed_penalty_msat, required),
343 (2, duration_since_epoch, required),
349 impl<T: Time> Readable for ChannelFailure<T> {
351 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
352 let mut undecayed_penalty_msat = 0;
353 let mut duration_since_epoch = Duration::from_secs(0);
354 read_tlv_fields!(r, {
355 (0, undecayed_penalty_msat, required),
356 (2, duration_since_epoch, required),
359 undecayed_penalty_msat,
360 last_failed: T::now() - (T::duration_since_epoch() - duration_since_epoch),
367 use super::{Eternity, ScoringParameters, ScorerUsingTime, Time};
370 use routing::network_graph::NodeId;
371 use util::ser::{Readable, Writeable};
373 use bitcoin::secp256k1::PublicKey;
374 use core::cell::Cell;
376 use core::time::Duration;
379 /// Time that can be advanced manually in tests.
380 #[derive(Debug, PartialEq, Eq)]
381 struct SinceEpoch(Duration);
385 static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
388 fn advance(duration: Duration) {
389 Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
393 impl Time for SinceEpoch {
395 Self(Self::duration_since_epoch())
398 fn duration_since_epoch() -> Duration {
399 Self::ELAPSED.with(|elapsed| elapsed.get())
402 fn elapsed(&self) -> Duration {
403 Self::duration_since_epoch() - self.0
407 impl Sub<Duration> for SinceEpoch {
410 fn sub(self, other: Duration) -> Self {
416 fn time_passes_when_advanced() {
417 let now = SinceEpoch::now();
418 assert_eq!(now.elapsed(), Duration::from_secs(0));
420 SinceEpoch::advance(Duration::from_secs(1));
421 SinceEpoch::advance(Duration::from_secs(1));
423 let elapsed = now.elapsed();
424 let later = SinceEpoch::now();
426 assert_eq!(elapsed, Duration::from_secs(2));
427 assert_eq!(later - elapsed, now);
431 fn time_never_passes_in_an_eternity() {
432 let now = Eternity::now();
433 let elapsed = now.elapsed();
434 let later = Eternity::now();
436 assert_eq!(now.elapsed(), Duration::from_secs(0));
437 assert_eq!(later - elapsed, now);
440 /// A scorer for testing with time that can be manually advanced.
441 type Scorer = ScorerUsingTime::<SinceEpoch>;
443 fn source_node_id() -> NodeId {
444 NodeId::from_pubkey(&PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap())
447 fn target_node_id() -> NodeId {
448 NodeId::from_pubkey(&PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap())
452 fn penalizes_without_channel_failures() {
453 let scorer = Scorer::new(ScoringParameters {
454 base_penalty_msat: 1_000,
455 failure_penalty_msat: 512,
456 failure_penalty_half_life: Duration::from_secs(1),
457 overuse_penalty_start_1024th: 1024,
458 overuse_penalty_msat_per_1024th: 0,
460 let source = source_node_id();
461 let target = target_node_id();
462 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
464 SinceEpoch::advance(Duration::from_secs(1));
465 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
469 fn accumulates_channel_failure_penalties() {
470 let mut scorer = Scorer::new(ScoringParameters {
471 base_penalty_msat: 1_000,
472 failure_penalty_msat: 64,
473 failure_penalty_half_life: Duration::from_secs(10),
474 overuse_penalty_start_1024th: 1024,
475 overuse_penalty_msat_per_1024th: 0,
477 let source = source_node_id();
478 let target = target_node_id();
479 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
481 scorer.payment_path_failed(&[], 42);
482 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_064);
484 scorer.payment_path_failed(&[], 42);
485 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_128);
487 scorer.payment_path_failed(&[], 42);
488 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_192);
492 fn decays_channel_failure_penalties_over_time() {
493 let mut scorer = Scorer::new(ScoringParameters {
494 base_penalty_msat: 1_000,
495 failure_penalty_msat: 512,
496 failure_penalty_half_life: Duration::from_secs(10),
497 overuse_penalty_start_1024th: 1024,
498 overuse_penalty_msat_per_1024th: 0,
500 let source = source_node_id();
501 let target = target_node_id();
502 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
504 scorer.payment_path_failed(&[], 42);
505 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
507 SinceEpoch::advance(Duration::from_secs(9));
508 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
510 SinceEpoch::advance(Duration::from_secs(1));
511 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_256);
513 SinceEpoch::advance(Duration::from_secs(10 * 8));
514 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_001);
516 SinceEpoch::advance(Duration::from_secs(10));
517 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
519 SinceEpoch::advance(Duration::from_secs(10));
520 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
524 fn accumulates_channel_failure_penalties_after_decay() {
525 let mut scorer = Scorer::new(ScoringParameters {
526 base_penalty_msat: 1_000,
527 failure_penalty_msat: 512,
528 failure_penalty_half_life: Duration::from_secs(10),
529 overuse_penalty_start_1024th: 1024,
530 overuse_penalty_msat_per_1024th: 0,
532 let source = source_node_id();
533 let target = target_node_id();
534 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
536 scorer.payment_path_failed(&[], 42);
537 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
539 SinceEpoch::advance(Duration::from_secs(10));
540 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_256);
542 scorer.payment_path_failed(&[], 42);
543 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_768);
545 SinceEpoch::advance(Duration::from_secs(10));
546 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_384);
550 fn restores_persisted_channel_failure_penalties() {
551 let mut scorer = Scorer::new(ScoringParameters {
552 base_penalty_msat: 1_000,
553 failure_penalty_msat: 512,
554 failure_penalty_half_life: Duration::from_secs(10),
555 overuse_penalty_start_1024th: 1024,
556 overuse_penalty_msat_per_1024th: 0,
558 let source = source_node_id();
559 let target = target_node_id();
561 scorer.payment_path_failed(&[], 42);
562 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
564 SinceEpoch::advance(Duration::from_secs(10));
565 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_256);
567 scorer.payment_path_failed(&[], 43);
568 assert_eq!(scorer.channel_penalty_msat(43, 1, Some(1), &source, &target), 1_512);
570 let mut serialized_scorer = Vec::new();
571 scorer.write(&mut serialized_scorer).unwrap();
573 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
574 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_256);
575 assert_eq!(deserialized_scorer.channel_penalty_msat(43, 1, Some(1), &source, &target), 1_512);
579 fn decays_persisted_channel_failure_penalties() {
580 let mut scorer = Scorer::new(ScoringParameters {
581 base_penalty_msat: 1_000,
582 failure_penalty_msat: 512,
583 failure_penalty_half_life: Duration::from_secs(10),
584 overuse_penalty_start_1024th: 1024,
585 overuse_penalty_msat_per_1024th: 0,
587 let source = source_node_id();
588 let target = target_node_id();
590 scorer.payment_path_failed(&[], 42);
591 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
593 let mut serialized_scorer = Vec::new();
594 scorer.write(&mut serialized_scorer).unwrap();
596 SinceEpoch::advance(Duration::from_secs(10));
598 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
599 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_256);
601 SinceEpoch::advance(Duration::from_secs(10));
602 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_128);
606 fn charges_per_1024th_penalty() {
607 let scorer = Scorer::new(ScoringParameters {
608 base_penalty_msat: 0,
609 failure_penalty_msat: 0,
610 failure_penalty_half_life: Duration::from_secs(0),
611 overuse_penalty_start_1024th: 256,
612 overuse_penalty_msat_per_1024th: 100,
614 let source = source_node_id();
615 let target = target_node_id();
617 assert_eq!(scorer.channel_penalty_msat(42, 1_000, None, &source, &target), 0);
618 assert_eq!(scorer.channel_penalty_msat(42, 1_000, Some(1_024_000), &source, &target), 0);
619 assert_eq!(scorer.channel_penalty_msat(42, 256_999, Some(1_024_000), &source, &target), 0);
620 assert_eq!(scorer.channel_penalty_msat(42, 257_000, Some(1_024_000), &source, &target), 100);
621 assert_eq!(scorer.channel_penalty_msat(42, 258_000, Some(1_024_000), &source, &target), 200);
622 assert_eq!(scorer.channel_penalty_msat(42, 512_000, Some(1_024_000), &source, &target), 256 * 100);