44
44
//! # }
45
45
//! ```
46
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
+ //!
47
53
//! [`find_route`]: crate::routing::router::find_route
48
54
49
55
use routing;
@@ -79,6 +85,10 @@ pub type DefaultTime = Eternity;
79
85
/// [`routing::Score`] implementation parameterized by [`Time`].
80
86
///
81
87
/// See [`Scorer`] for details.
88
+ ///
89
+ /// # Note
90
+ ///
91
+ /// Mixing [`Time`] types between serialization and deserialization results in undefined behavior.
82
92
pub struct ScorerUsingTime < T : Time > {
83
93
params : ScoringParameters ,
84
94
// TODO: Remove entries of closed channels.
@@ -131,6 +141,11 @@ pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
131
141
132
142
/// Returns the amount of time elapsed since created.
133
143
fn elapsed ( & self ) -> Duration ;
144
+
145
+ /// Returns the amount of time passed since the beginning of [`Time`].
146
+ ///
147
+ /// Used during (de-)serialization.
148
+ fn duration_since_epoch ( ) -> Duration ;
134
149
}
135
150
136
151
impl < T : Time > ScorerUsingTime < T > {
@@ -218,6 +233,11 @@ impl Time for std::time::Instant {
218
233
std:: time:: Instant :: now ( )
219
234
}
220
235
236
+ fn duration_since_epoch ( ) -> Duration {
237
+ use std:: time:: SystemTime ;
238
+ SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( )
239
+ }
240
+
221
241
fn elapsed ( & self ) -> Duration {
222
242
std:: time:: Instant :: elapsed ( self )
223
243
}
@@ -231,6 +251,10 @@ impl Time for Eternity {
231
251
Self
232
252
}
233
253
254
+ fn duration_since_epoch ( ) -> Duration {
255
+ Duration :: from_secs ( 0 )
256
+ }
257
+
234
258
fn elapsed ( & self ) -> Duration {
235
259
Duration :: from_secs ( 0 )
236
260
}
@@ -266,7 +290,7 @@ impl<T: Time> Writeable for ChannelFailure<T> {
266
290
#[ inline]
267
291
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
268
292
self . undecayed_penalty_msat . write ( w) ?;
269
- ( duration_since_epoch ( ) - self . last_failed . elapsed ( ) ) . write ( w)
293
+ ( T :: duration_since_epoch ( ) - self . last_failed . elapsed ( ) ) . write ( w)
270
294
}
271
295
}
272
296
@@ -275,17 +299,7 @@ impl<T: Time> Readable for ChannelFailure<T> {
275
299
fn read < R : Read > ( r : & mut R ) -> Result < Self , DecodeError > {
276
300
Ok ( Self {
277
301
undecayed_penalty_msat : Readable :: read ( r) ?,
278
- last_failed : T :: now ( ) - ( duration_since_epoch ( ) - Readable :: read ( r) ?) ,
302
+ last_failed : T :: now ( ) - ( T :: duration_since_epoch ( ) - Readable :: read ( r) ?) ,
279
303
} )
280
304
}
281
305
}
282
-
283
- fn duration_since_epoch ( ) -> Duration {
284
- #[ cfg( not( feature = "no-std" ) ) ]
285
- {
286
- use std:: time:: SystemTime ;
287
- SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( )
288
- }
289
- #[ cfg( feature = "no-std" ) ]
290
- return Duration :: from_secs ( 0 ) ;
291
- }
0 commit comments