46
46
//!
47
47
//! # Note
48
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.
49
+ //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
50
+ //! different types and thus is undefined.
52
51
//!
53
52
//! [`find_route`]: crate::routing::router::find_route
54
53
@@ -59,9 +58,10 @@ use util::ser::{Readable, Writeable, Writer};
59
58
60
59
use prelude:: * ;
61
60
use core:: cell:: { RefCell , RefMut } ;
62
- use core:: ops:: { DerefMut , Sub } ;
61
+ use core:: ops:: DerefMut ;
63
62
use core:: time:: Duration ;
64
- use io:: { self , Read } ; use sync:: { Mutex , MutexGuard } ;
63
+ use io:: { self , Read } ;
64
+ use sync:: { Mutex , MutexGuard } ;
65
65
66
66
/// We define Score ever-so-slightly differently based on whether we are being built for C bindings
67
67
/// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
@@ -185,23 +185,33 @@ impl<'a, S: Writeable> Writeable for MutexGuard<'a, S> {
185
185
/// See [module-level documentation] for usage.
186
186
///
187
187
/// [module-level documentation]: crate::routing::scoring
188
- pub type Scorer = ScorerUsingTime :: < DefaultTime > ;
189
-
190
- /// Time used by [`Scorer`].
191
188
#[ cfg( not( feature = "no-std" ) ) ]
192
- pub type DefaultTime = std:: time:: Instant ;
193
-
194
- /// Time used by [`Scorer`].
189
+ pub type Scorer = ScorerUsingTime :: < std:: time:: Instant > ;
190
+ /// [`Score`] implementation that provides reasonable default behavior.
191
+ ///
192
+ /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
193
+ /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
194
+ ///
195
+ /// See [module-level documentation] for usage.
196
+ ///
197
+ /// [module-level documentation]: crate::routing::scoring
195
198
#[ cfg( feature = "no-std" ) ]
196
- pub type DefaultTime = Eternity ;
199
+ pub type Scorer = ScorerUsingTime :: < time:: Eternity > ;
200
+
201
+ // Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
202
+ // doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
203
+ // methods at all.
197
204
198
- /// [`Score`] implementation parameterized by [`Time`] .
205
+ /// [`Score`] implementation.
199
206
///
200
207
/// See [`Scorer`] for details.
201
208
///
202
209
/// # Note
203
210
///
204
- /// Mixing [`Time`] types between serialization and deserialization results in undefined behavior.
211
+ /// Mixing the `no-std` feature between serialization and deserialization results in undefined
212
+ /// behavior.
213
+ ///
214
+ /// (C-not exported) generally all users should use the [`Scorer`] type alias.
205
215
pub struct ScorerUsingTime < T : Time > {
206
216
params : ScoringParameters ,
207
217
// TODO: Remove entries of closed channels.
@@ -247,8 +257,8 @@ pub struct ScoringParameters {
247
257
///
248
258
/// # Note
249
259
///
250
- /// When time is an [`Eternity`], as is default when enabling feature `no-std`, it will never
251
- /// elapse. Therefore, this penalty will never decay.
260
+ /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
261
+ /// never decay.
252
262
///
253
263
/// [`failure_penalty_msat`]: Self::failure_penalty_msat
254
264
pub failure_penalty_half_life : Duration ,
@@ -273,20 +283,6 @@ struct ChannelFailure<T: Time> {
273
283
last_failed : T ,
274
284
}
275
285
276
- /// A measurement of time.
277
- pub trait Time : Sub < Duration , Output = Self > where Self : Sized {
278
- /// Returns an instance corresponding to the current moment.
279
- fn now ( ) -> Self ;
280
-
281
- /// Returns the amount of time elapsed since `self` was created.
282
- fn elapsed ( & self ) -> Duration ;
283
-
284
- /// Returns the amount of time passed since the beginning of [`Time`].
285
- ///
286
- /// Used during (de-)serialization.
287
- fn duration_since_epoch ( ) -> Duration ;
288
- }
289
-
290
286
impl < T : Time > ScorerUsingTime < T > {
291
287
/// Creates a new scorer using the given scoring parameters.
292
288
pub fn new ( params : ScoringParameters ) -> Self {
@@ -383,48 +379,6 @@ impl<T: Time> Score for ScorerUsingTime<T> {
383
379
}
384
380
}
385
381
386
- #[ cfg( not( feature = "no-std" ) ) ]
387
- impl Time for std:: time:: Instant {
388
- fn now ( ) -> Self {
389
- std:: time:: Instant :: now ( )
390
- }
391
-
392
- fn duration_since_epoch ( ) -> Duration {
393
- use std:: time:: SystemTime ;
394
- SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( )
395
- }
396
-
397
- fn elapsed ( & self ) -> Duration {
398
- std:: time:: Instant :: elapsed ( self )
399
- }
400
- }
401
-
402
- /// A state in which time has no meaning.
403
- #[ derive( Debug , PartialEq , Eq ) ]
404
- pub struct Eternity ;
405
-
406
- impl Time for Eternity {
407
- fn now ( ) -> Self {
408
- Self
409
- }
410
-
411
- fn duration_since_epoch ( ) -> Duration {
412
- Duration :: from_secs ( 0 )
413
- }
414
-
415
- fn elapsed ( & self ) -> Duration {
416
- Duration :: from_secs ( 0 )
417
- }
418
- }
419
-
420
- impl Sub < Duration > for Eternity {
421
- type Output = Self ;
422
-
423
- fn sub ( self , _other : Duration ) -> Self {
424
- self
425
- }
426
- }
427
-
428
382
impl < T : Time > Writeable for ScorerUsingTime < T > {
429
383
#[ inline]
430
384
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
@@ -475,9 +429,72 @@ impl<T: Time> Readable for ChannelFailure<T> {
475
429
}
476
430
}
477
431
432
+ pub ( crate ) mod time {
433
+ use core:: ops:: Sub ;
434
+ use core:: time:: Duration ;
435
+ /// A measurement of time.
436
+ pub trait Time : Sub < Duration , Output = Self > where Self : Sized {
437
+ /// Returns an instance corresponding to the current moment.
438
+ fn now ( ) -> Self ;
439
+
440
+ /// Returns the amount of time elapsed since `self` was created.
441
+ fn elapsed ( & self ) -> Duration ;
442
+
443
+ /// Returns the amount of time passed since the beginning of [`Time`].
444
+ ///
445
+ /// Used during (de-)serialization.
446
+ fn duration_since_epoch ( ) -> Duration ;
447
+ }
448
+
449
+ /// A state in which time has no meaning.
450
+ #[ derive( Debug , PartialEq , Eq ) ]
451
+ pub struct Eternity ;
452
+
453
+ #[ cfg( not( feature = "no-std" ) ) ]
454
+ impl Time for std:: time:: Instant {
455
+ fn now ( ) -> Self {
456
+ std:: time:: Instant :: now ( )
457
+ }
458
+
459
+ fn duration_since_epoch ( ) -> Duration {
460
+ use std:: time:: SystemTime ;
461
+ SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( )
462
+ }
463
+
464
+ fn elapsed ( & self ) -> Duration {
465
+ std:: time:: Instant :: elapsed ( self )
466
+ }
467
+ }
468
+
469
+ impl Time for Eternity {
470
+ fn now ( ) -> Self {
471
+ Self
472
+ }
473
+
474
+ fn duration_since_epoch ( ) -> Duration {
475
+ Duration :: from_secs ( 0 )
476
+ }
477
+
478
+ fn elapsed ( & self ) -> Duration {
479
+ Duration :: from_secs ( 0 )
480
+ }
481
+ }
482
+
483
+ impl Sub < Duration > for Eternity {
484
+ type Output = Self ;
485
+
486
+ fn sub ( self , _other : Duration ) -> Self {
487
+ self
488
+ }
489
+ }
490
+ }
491
+
492
+ pub ( crate ) use self :: time:: Time ;
493
+
478
494
#[ cfg( test) ]
479
495
mod tests {
480
- use super :: { Eternity , ScoringParameters , ScorerUsingTime , Time } ;
496
+ use super :: { ScoringParameters , ScorerUsingTime , Time } ;
497
+ use super :: time:: Eternity ;
481
498
482
499
use routing:: scoring:: Score ;
483
500
use routing:: network_graph:: NodeId ;
0 commit comments