382eae25b2264747429bfa2614208383f403d220
[rust-lightning] / lightning / src / routing / scorer.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
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
8 // licenses.
9
10 //! Utilities for scoring payment channels.
11 //!
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.
14 //!
15 //! # Example
16 //!
17 //! ```
18 //! # extern crate secp256k1;
19 //! #
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;
25 //! #
26 //! # struct FakeLogger {};
27 //! # impl Logger for FakeLogger {
28 //! #     fn log(&self, record: &Record) { unimplemented!() }
29 //! # }
30 //! # fn find_scored_route(payer: PublicKey, params: RouteParameters, network_graph: NetworkGraph) {
31 //! # let logger = FakeLogger {};
32 //! #
33 //! // Use the default channel penalties.
34 //! let scorer = Scorer::default();
35 //!
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()
41 //! });
42 //!
43 //! let route = find_route(&payer, &params, &network_graph, None, &logger, &scorer);
44 //! # }
45 //! ```
46 //!
47 //! # Note
48 //!
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.
52 //!
53 //! [`find_route`]: crate::routing::router::find_route
54
55 use routing;
56
57 use ln::msgs::DecodeError;
58 use routing::network_graph::NodeId;
59 use routing::router::RouteHop;
60 use util::ser::{Readable, Writeable, Writer};
61
62 use prelude::*;
63 use core::ops::Sub;
64 use core::time::Duration;
65 use io::{self, Read};
66
67 /// [`routing::Score`] implementation that provides reasonable default behavior.
68 ///
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.
71 ///
72 /// See [module-level documentation] for usage.
73 ///
74 /// [module-level documentation]: crate::routing::scorer
75 pub type Scorer = ScorerUsingTime::<DefaultTime>;
76
77 /// Time used by [`Scorer`].
78 #[cfg(not(feature = "no-std"))]
79 pub type DefaultTime = std::time::Instant;
80
81 /// Time used by [`Scorer`].
82 #[cfg(feature = "no-std")]
83 pub type DefaultTime = Eternity;
84
85 /// [`routing::Score`] implementation parameterized by [`Time`].
86 ///
87 /// See [`Scorer`] for details.
88 ///
89 /// # Note
90 ///
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>>,
96 }
97
98 /// Parameters for configuring [`Scorer`].
99 pub struct ScoringParameters {
100         /// A fixed penalty in msats to apply to each channel.
101         ///
102         /// Default value: 500 msat
103         pub base_penalty_msat: u64,
104
105         /// A penalty in msats to apply to a channel upon failing to relay a payment.
106         ///
107         /// This accumulates for each failure but may be reduced over time based on
108         /// [`failure_penalty_half_life`].
109         ///
110         /// Default value: 1,024,000 msat
111         ///
112         /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
113         pub failure_penalty_msat: u64,
114
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`].
117         ///
118         /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
119         ///
120         /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
121         pub overuse_penalty_start_1024th: u16,
122
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.
125         ///
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)
128         ///
129         /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
130         pub overuse_penalty_msat_per_1024th: u64,
131
132         /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
133         /// cut in half.
134         ///
135         /// # Note
136         ///
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.
139         ///
140         /// [`failure_penalty_msat`]: Self::failure_penalty_msat
141         pub failure_penalty_half_life: Duration,
142 }
143
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),
150 });
151
152 /// Accounting for penalties against a channel for failing to relay any payments.
153 ///
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,
158
159         /// Last time the channel failed. Used to decay `undecayed_penalty_msat`.
160         last_failed: T,
161 }
162
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.
166         fn now() -> Self;
167
168         /// Returns the amount of time elapsed since `self` was created.
169         fn elapsed(&self) -> Duration;
170
171         /// Returns the amount of time passed since the beginning of [`Time`].
172         ///
173         /// Used during (de-)serialization.
174         fn duration_since_epoch() -> Duration;
175 }
176
177 impl<T: Time> ScorerUsingTime<T> {
178         /// Creates a new scorer using the given scoring parameters.
179         pub fn new(params: ScoringParameters) -> Self {
180                 Self {
181                         params,
182                         channel_failures: HashMap::new(),
183                 }
184         }
185
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,
195                 })
196         }
197 }
198
199 impl<T: Time> ChannelFailure<T> {
200         fn new(failure_penalty_msat: u64) -> Self {
201                 Self {
202                         undecayed_penalty_msat: failure_penalty_msat,
203                         last_failed: T::now(),
204                 }
205         }
206
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();
210         }
211
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());
214                 match decays {
215                         Some(decays) => self.undecayed_penalty_msat >> decays,
216                         None => 0,
217                 }
218         }
219 }
220
221 impl<T: Time> Default for ScorerUsingTime<T> {
222         fn default() -> Self {
223                 Self::new(ScoringParameters::default())
224         }
225 }
226
227 impl Default for ScoringParameters {
228         fn default() -> Self {
229                 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,
235                 }
236         }
237 }
238
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
242         ) -> u64 {
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));
246
247                 let mut penalty_msat = self.params.base_penalty_msat + failure_penalty_msat;
248
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;
251
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());
257                         }
258                 }
259
260                 penalty_msat
261         }
262
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));
270         }
271 }
272
273 #[cfg(not(feature = "no-std"))]
274 impl Time for std::time::Instant {
275         fn now() -> Self {
276                 std::time::Instant::now()
277         }
278
279         fn duration_since_epoch() -> Duration {
280                 use std::time::SystemTime;
281                 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
282         }
283
284         fn elapsed(&self) -> Duration {
285                 std::time::Instant::elapsed(self)
286         }
287 }
288
289 /// A state in which time has no meaning.
290 #[derive(Debug, PartialEq, Eq)]
291 pub struct Eternity;
292
293 impl Time for Eternity {
294         fn now() -> Self {
295                 Self
296         }
297
298         fn duration_since_epoch() -> Duration {
299                 Duration::from_secs(0)
300         }
301
302         fn elapsed(&self) -> Duration {
303                 Duration::from_secs(0)
304         }
305 }
306
307 impl Sub<Duration> for Eternity {
308         type Output = Self;
309
310         fn sub(self, _other: Duration) -> Self {
311                 self
312         }
313 }
314
315 impl<T: Time> Writeable for ScorerUsingTime<T> {
316         #[inline]
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, {});
321                 Ok(())
322         }
323 }
324
325 impl<T: Time> Readable for ScorerUsingTime<T> {
326         #[inline]
327         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
328                 let res = Ok(Self {
329                         params: Readable::read(r)?,
330                         channel_failures: Readable::read(r)?,
331                 });
332                 read_tlv_fields!(r, {});
333                 res
334         }
335 }
336
337 impl<T: Time> Writeable for ChannelFailure<T> {
338         #[inline]
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),
344                 });
345                 Ok(())
346         }
347 }
348
349 impl<T: Time> Readable for ChannelFailure<T> {
350         #[inline]
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),
357                 });
358                 Ok(Self {
359                         undecayed_penalty_msat,
360                         last_failed: T::now() - (T::duration_since_epoch() - duration_since_epoch),
361                 })
362         }
363 }
364
365 #[cfg(test)]
366 mod tests {
367         use super::{Eternity, ScoringParameters, ScorerUsingTime, Time};
368
369         use routing::Score;
370         use routing::network_graph::NodeId;
371         use util::ser::{Readable, Writeable};
372
373         use bitcoin::secp256k1::PublicKey;
374         use core::cell::Cell;
375         use core::ops::Sub;
376         use core::time::Duration;
377         use io;
378
379         /// Time that can be advanced manually in tests.
380         #[derive(Debug, PartialEq, Eq)]
381         struct SinceEpoch(Duration);
382
383         impl SinceEpoch {
384                 thread_local! {
385                         static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
386                 }
387
388                 fn advance(duration: Duration) {
389                         Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
390                 }
391         }
392
393         impl Time for SinceEpoch {
394                 fn now() -> Self {
395                         Self(Self::duration_since_epoch())
396                 }
397
398                 fn duration_since_epoch() -> Duration {
399                         Self::ELAPSED.with(|elapsed| elapsed.get())
400                 }
401
402                 fn elapsed(&self) -> Duration {
403                         Self::duration_since_epoch() - self.0
404                 }
405         }
406
407         impl Sub<Duration> for SinceEpoch {
408                 type Output = Self;
409
410                 fn sub(self, other: Duration) -> Self {
411                         Self(self.0 - other)
412                 }
413         }
414
415         #[test]
416         fn time_passes_when_advanced() {
417                 let now = SinceEpoch::now();
418                 assert_eq!(now.elapsed(), Duration::from_secs(0));
419
420                 SinceEpoch::advance(Duration::from_secs(1));
421                 SinceEpoch::advance(Duration::from_secs(1));
422
423                 let elapsed = now.elapsed();
424                 let later = SinceEpoch::now();
425
426                 assert_eq!(elapsed, Duration::from_secs(2));
427                 assert_eq!(later - elapsed, now);
428         }
429
430         #[test]
431         fn time_never_passes_in_an_eternity() {
432                 let now = Eternity::now();
433                 let elapsed = now.elapsed();
434                 let later = Eternity::now();
435
436                 assert_eq!(now.elapsed(), Duration::from_secs(0));
437                 assert_eq!(later - elapsed, now);
438         }
439
440         /// A scorer for testing with time that can be manually advanced.
441         type Scorer = ScorerUsingTime::<SinceEpoch>;
442
443         fn source_node_id() -> NodeId {
444                 NodeId::from_pubkey(&PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap())
445         }
446
447         fn target_node_id() -> NodeId {
448                 NodeId::from_pubkey(&PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap())
449         }
450
451         #[test]
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,
459                 });
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);
463
464                 SinceEpoch::advance(Duration::from_secs(1));
465                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
466         }
467
468         #[test]
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,
476                 });
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);
480
481                 scorer.payment_path_failed(&[], 42);
482                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_064);
483
484                 scorer.payment_path_failed(&[], 42);
485                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_128);
486
487                 scorer.payment_path_failed(&[], 42);
488                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_192);
489         }
490
491         #[test]
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,
499                 });
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);
503
504                 scorer.payment_path_failed(&[], 42);
505                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
506
507                 SinceEpoch::advance(Duration::from_secs(9));
508                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
509
510                 SinceEpoch::advance(Duration::from_secs(1));
511                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_256);
512
513                 SinceEpoch::advance(Duration::from_secs(10 * 8));
514                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_001);
515
516                 SinceEpoch::advance(Duration::from_secs(10));
517                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
518
519                 SinceEpoch::advance(Duration::from_secs(10));
520                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_000);
521         }
522
523         #[test]
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,
531                 });
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);
535
536                 scorer.payment_path_failed(&[], 42);
537                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
538
539                 SinceEpoch::advance(Duration::from_secs(10));
540                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_256);
541
542                 scorer.payment_path_failed(&[], 42);
543                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_768);
544
545                 SinceEpoch::advance(Duration::from_secs(10));
546                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_384);
547         }
548
549         #[test]
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,
557                 });
558                 let source = source_node_id();
559                 let target = target_node_id();
560
561                 scorer.payment_path_failed(&[], 42);
562                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
563
564                 SinceEpoch::advance(Duration::from_secs(10));
565                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_256);
566
567                 scorer.payment_path_failed(&[], 43);
568                 assert_eq!(scorer.channel_penalty_msat(43, 1, Some(1), &source, &target), 1_512);
569
570                 let mut serialized_scorer = Vec::new();
571                 scorer.write(&mut serialized_scorer).unwrap();
572
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);
576         }
577
578         #[test]
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,
586                 });
587                 let source = source_node_id();
588                 let target = target_node_id();
589
590                 scorer.payment_path_failed(&[], 42);
591                 assert_eq!(scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_512);
592
593                 let mut serialized_scorer = Vec::new();
594                 scorer.write(&mut serialized_scorer).unwrap();
595
596                 SinceEpoch::advance(Duration::from_secs(10));
597
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);
600
601                 SinceEpoch::advance(Duration::from_secs(10));
602                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, 1, Some(1), &source, &target), 1_128);
603         }
604
605         #[test]
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,
613                 });
614                 let source = source_node_id();
615                 let target = target_node_id();
616
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);
623         }
624 }