Add unit tests for Scorer
[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         pub base_penalty_msat: u64,
102
103         /// A penalty in msats to apply to a channel upon failing to relay a payment.
104         ///
105         /// This accumulates for each failure but may be reduced over time based on
106         /// [`failure_penalty_half_life`].
107         ///
108         /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
109         pub failure_penalty_msat: u64,
110
111         /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
112         /// cut in half.
113         ///
114         /// # Note
115         ///
116         /// When time is an [`Eternity`], as is default when enabling feature `no-std`, it will never
117         /// elapse. Therefore, this penalty will never decay.
118         ///
119         /// [`failure_penalty_msat`]: Self::failure_penalty_msat
120         pub failure_penalty_half_life: Duration,
121 }
122
123 impl_writeable_tlv_based!(ScoringParameters, {
124         (0, base_penalty_msat, required),
125         (2, failure_penalty_msat, required),
126         (4, failure_penalty_half_life, required),
127 });
128
129 /// Accounting for penalties against a channel for failing to relay any payments.
130 ///
131 /// Penalties decay over time, though accumulate as more failures occur.
132 struct ChannelFailure<T: Time> {
133         /// Accumulated penalty in msats for the channel as of `last_failed`.
134         undecayed_penalty_msat: u64,
135
136         /// Last time the channel failed. Used to decay `undecayed_penalty_msat`.
137         last_failed: T,
138 }
139
140 /// A measurement of time.
141 pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
142         /// Returns an instance corresponding to the current moment.
143         fn now() -> Self;
144
145         /// Returns the amount of time elapsed since `self` was created.
146         fn elapsed(&self) -> Duration;
147
148         /// Returns the amount of time passed since the beginning of [`Time`].
149         ///
150         /// Used during (de-)serialization.
151         fn duration_since_epoch() -> Duration;
152 }
153
154 impl<T: Time> ScorerUsingTime<T> {
155         /// Creates a new scorer using the given scoring parameters.
156         pub fn new(params: ScoringParameters) -> Self {
157                 Self {
158                         params,
159                         channel_failures: HashMap::new(),
160                 }
161         }
162
163         /// Creates a new scorer using `penalty_msat` as a fixed channel penalty.
164         #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
165         pub fn with_fixed_penalty(penalty_msat: u64) -> Self {
166                 Self::new(ScoringParameters {
167                         base_penalty_msat: penalty_msat,
168                         failure_penalty_msat: 0,
169                         failure_penalty_half_life: Duration::from_secs(0),
170                 })
171         }
172 }
173
174 impl<T: Time> ChannelFailure<T> {
175         fn new(failure_penalty_msat: u64) -> Self {
176                 Self {
177                         undecayed_penalty_msat: failure_penalty_msat,
178                         last_failed: T::now(),
179                 }
180         }
181
182         fn add_penalty(&mut self, failure_penalty_msat: u64, half_life: Duration) {
183                 self.undecayed_penalty_msat = self.decayed_penalty_msat(half_life) + failure_penalty_msat;
184                 self.last_failed = T::now();
185         }
186
187         fn decayed_penalty_msat(&self, half_life: Duration) -> u64 {
188                 let decays = self.last_failed.elapsed().as_secs().checked_div(half_life.as_secs());
189                 match decays {
190                         Some(decays) => self.undecayed_penalty_msat >> decays,
191                         None => 0,
192                 }
193         }
194 }
195
196 impl<T: Time> Default for ScorerUsingTime<T> {
197         fn default() -> Self {
198                 Self::new(ScoringParameters::default())
199         }
200 }
201
202 impl Default for ScoringParameters {
203         fn default() -> Self {
204                 Self {
205                         base_penalty_msat: 500,
206                         failure_penalty_msat: 1024 * 1000,
207                         failure_penalty_half_life: Duration::from_secs(3600),
208                 }
209         }
210 }
211
212 impl<T: Time> routing::Score for ScorerUsingTime<T> {
213         fn channel_penalty_msat(
214                 &self, short_channel_id: u64, _source: &NodeId, _target: &NodeId
215         ) -> u64 {
216                 let failure_penalty_msat = self.channel_failures
217                         .get(&short_channel_id)
218                         .map_or(0, |value| value.decayed_penalty_msat(self.params.failure_penalty_half_life));
219
220                 self.params.base_penalty_msat + failure_penalty_msat
221         }
222
223         fn payment_path_failed(&mut self, _path: &[&RouteHop], short_channel_id: u64) {
224                 let failure_penalty_msat = self.params.failure_penalty_msat;
225                 let half_life = self.params.failure_penalty_half_life;
226                 self.channel_failures
227                         .entry(short_channel_id)
228                         .and_modify(|failure| failure.add_penalty(failure_penalty_msat, half_life))
229                         .or_insert_with(|| ChannelFailure::new(failure_penalty_msat));
230         }
231 }
232
233 #[cfg(not(feature = "no-std"))]
234 impl Time for std::time::Instant {
235         fn now() -> Self {
236                 std::time::Instant::now()
237         }
238
239         fn duration_since_epoch() -> Duration {
240                 use std::time::SystemTime;
241                 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
242         }
243
244         fn elapsed(&self) -> Duration {
245                 std::time::Instant::elapsed(self)
246         }
247 }
248
249 /// A state in which time has no meaning.
250 #[derive(Debug, PartialEq, Eq)]
251 pub struct Eternity;
252
253 impl Time for Eternity {
254         fn now() -> Self {
255                 Self
256         }
257
258         fn duration_since_epoch() -> Duration {
259                 Duration::from_secs(0)
260         }
261
262         fn elapsed(&self) -> Duration {
263                 Duration::from_secs(0)
264         }
265 }
266
267 impl Sub<Duration> for Eternity {
268         type Output = Self;
269
270         fn sub(self, _other: Duration) -> Self {
271                 self
272         }
273 }
274
275 impl<T: Time> Writeable for ScorerUsingTime<T> {
276         #[inline]
277         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
278                 self.params.write(w)?;
279                 self.channel_failures.write(w)?;
280                 write_tlv_fields!(w, {});
281                 Ok(())
282         }
283 }
284
285 impl<T: Time> Readable for ScorerUsingTime<T> {
286         #[inline]
287         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
288                 let res = Ok(Self {
289                         params: Readable::read(r)?,
290                         channel_failures: Readable::read(r)?,
291                 });
292                 read_tlv_fields!(r, {});
293                 res
294         }
295 }
296
297 impl<T: Time> Writeable for ChannelFailure<T> {
298         #[inline]
299         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
300                 let duration_since_epoch = T::duration_since_epoch() - self.last_failed.elapsed();
301                 write_tlv_fields!(w, {
302                         (0, self.undecayed_penalty_msat, required),
303                         (2, duration_since_epoch, required),
304                 });
305                 Ok(())
306         }
307 }
308
309 impl<T: Time> Readable for ChannelFailure<T> {
310         #[inline]
311         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
312                 let mut undecayed_penalty_msat = 0;
313                 let mut duration_since_epoch = Duration::from_secs(0);
314                 read_tlv_fields!(r, {
315                         (0, undecayed_penalty_msat, required),
316                         (2, duration_since_epoch, required),
317                 });
318                 Ok(Self {
319                         undecayed_penalty_msat,
320                         last_failed: T::now() - (T::duration_since_epoch() - duration_since_epoch),
321                 })
322         }
323 }
324
325 #[cfg(test)]
326 mod tests {
327         use super::{Eternity, ScoringParameters, ScorerUsingTime, Time};
328
329         use routing::Score;
330         use routing::network_graph::NodeId;
331         use util::ser::{Readable, Writeable};
332
333         use bitcoin::secp256k1::PublicKey;
334         use core::cell::Cell;
335         use core::ops::Sub;
336         use core::time::Duration;
337         use io;
338
339         /// Time that can be advanced manually in tests.
340         #[derive(Debug, PartialEq, Eq)]
341         struct SinceEpoch(Duration);
342
343         impl SinceEpoch {
344                 thread_local! {
345                         static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
346                 }
347
348                 fn advance(duration: Duration) {
349                         Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
350                 }
351         }
352
353         impl Time for SinceEpoch {
354                 fn now() -> Self {
355                         Self(Self::duration_since_epoch())
356                 }
357
358                 fn duration_since_epoch() -> Duration {
359                         Self::ELAPSED.with(|elapsed| elapsed.get())
360                 }
361
362                 fn elapsed(&self) -> Duration {
363                         Self::duration_since_epoch() - self.0
364                 }
365         }
366
367         impl Sub<Duration> for SinceEpoch {
368                 type Output = Self;
369
370                 fn sub(self, other: Duration) -> Self {
371                         Self(self.0 - other)
372                 }
373         }
374
375         #[test]
376         fn time_passes_when_advanced() {
377                 let now = SinceEpoch::now();
378                 assert_eq!(now.elapsed(), Duration::from_secs(0));
379
380                 SinceEpoch::advance(Duration::from_secs(1));
381                 SinceEpoch::advance(Duration::from_secs(1));
382
383                 let elapsed = now.elapsed();
384                 let later = SinceEpoch::now();
385
386                 assert_eq!(elapsed, Duration::from_secs(2));
387                 assert_eq!(later - elapsed, now);
388         }
389
390         #[test]
391         fn time_never_passes_in_an_eternity() {
392                 let now = Eternity::now();
393                 let elapsed = now.elapsed();
394                 let later = Eternity::now();
395
396                 assert_eq!(now.elapsed(), Duration::from_secs(0));
397                 assert_eq!(later - elapsed, now);
398         }
399
400         /// A scorer for testing with time that can be manually advanced.
401         type Scorer = ScorerUsingTime::<SinceEpoch>;
402
403         fn source_node_id() -> NodeId {
404                 NodeId::from_pubkey(&PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap())
405         }
406
407         fn target_node_id() -> NodeId {
408                 NodeId::from_pubkey(&PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap())
409         }
410
411         #[test]
412         fn penalizes_without_channel_failures() {
413                 let scorer = Scorer::new(ScoringParameters {
414                         base_penalty_msat: 1_000,
415                         failure_penalty_msat: 512,
416                         failure_penalty_half_life: Duration::from_secs(1),
417                 });
418                 let source = source_node_id();
419                 let target = target_node_id();
420                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_000);
421
422                 SinceEpoch::advance(Duration::from_secs(1));
423                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_000);
424         }
425
426         #[test]
427         fn accumulates_channel_failure_penalties() {
428                 let mut scorer = Scorer::new(ScoringParameters {
429                         base_penalty_msat: 1_000,
430                         failure_penalty_msat: 64,
431                         failure_penalty_half_life: Duration::from_secs(10),
432                 });
433                 let source = source_node_id();
434                 let target = target_node_id();
435                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_000);
436
437                 scorer.payment_path_failed(&[], 42);
438                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_064);
439
440                 scorer.payment_path_failed(&[], 42);
441                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_128);
442
443                 scorer.payment_path_failed(&[], 42);
444                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_192);
445         }
446
447         #[test]
448         fn decays_channel_failure_penalties_over_time() {
449                 let mut scorer = Scorer::new(ScoringParameters {
450                         base_penalty_msat: 1_000,
451                         failure_penalty_msat: 512,
452                         failure_penalty_half_life: Duration::from_secs(10),
453                 });
454                 let source = source_node_id();
455                 let target = target_node_id();
456                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_000);
457
458                 scorer.payment_path_failed(&[], 42);
459                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_512);
460
461                 SinceEpoch::advance(Duration::from_secs(9));
462                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_512);
463
464                 SinceEpoch::advance(Duration::from_secs(1));
465                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_256);
466
467                 SinceEpoch::advance(Duration::from_secs(10 * 8));
468                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_001);
469
470                 SinceEpoch::advance(Duration::from_secs(10));
471                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_000);
472
473                 SinceEpoch::advance(Duration::from_secs(10));
474                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_000);
475         }
476
477         #[test]
478         fn accumulates_channel_failure_penalties_after_decay() {
479                 let mut scorer = Scorer::new(ScoringParameters {
480                         base_penalty_msat: 1_000,
481                         failure_penalty_msat: 512,
482                         failure_penalty_half_life: Duration::from_secs(10),
483                 });
484                 let source = source_node_id();
485                 let target = target_node_id();
486                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_000);
487
488                 scorer.payment_path_failed(&[], 42);
489                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_512);
490
491                 SinceEpoch::advance(Duration::from_secs(10));
492                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_256);
493
494                 scorer.payment_path_failed(&[], 42);
495                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_768);
496
497                 SinceEpoch::advance(Duration::from_secs(10));
498                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_384);
499         }
500
501         #[test]
502         fn restores_persisted_channel_failure_penalties() {
503                 let mut scorer = Scorer::new(ScoringParameters {
504                         base_penalty_msat: 1_000,
505                         failure_penalty_msat: 512,
506                         failure_penalty_half_life: Duration::from_secs(10),
507                 });
508                 let source = source_node_id();
509                 let target = target_node_id();
510
511                 scorer.payment_path_failed(&[], 42);
512                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_512);
513
514                 SinceEpoch::advance(Duration::from_secs(10));
515                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_256);
516
517                 scorer.payment_path_failed(&[], 43);
518                 assert_eq!(scorer.channel_penalty_msat(43, &source, &target), 1_512);
519
520                 let mut serialized_scorer = Vec::new();
521                 scorer.write(&mut serialized_scorer).unwrap();
522
523                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
524                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, &source, &target), 1_256);
525                 assert_eq!(deserialized_scorer.channel_penalty_msat(43, &source, &target), 1_512);
526         }
527
528         #[test]
529         fn decays_persisted_channel_failure_penalties() {
530                 let mut scorer = Scorer::new(ScoringParameters {
531                         base_penalty_msat: 1_000,
532                         failure_penalty_msat: 512,
533                         failure_penalty_half_life: Duration::from_secs(10),
534                 });
535                 let source = source_node_id();
536                 let target = target_node_id();
537
538                 scorer.payment_path_failed(&[], 42);
539                 assert_eq!(scorer.channel_penalty_msat(42, &source, &target), 1_512);
540
541                 let mut serialized_scorer = Vec::new();
542                 scorer.write(&mut serialized_scorer).unwrap();
543
544                 SinceEpoch::advance(Duration::from_secs(10));
545
546                 let deserialized_scorer = <Scorer>::read(&mut io::Cursor::new(&serialized_scorer)).unwrap();
547                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, &source, &target), 1_256);
548
549                 SinceEpoch::advance(Duration::from_secs(10));
550                 assert_eq!(deserialized_scorer.channel_penalty_msat(42, &source, &target), 1_128);
551         }
552 }