Skip to content

Commit 73ffbcf

Browse files
committed
f - Add duration_since_epoch to Time and note undefined behavior
1 parent aba246d commit 73ffbcf

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

lightning/src/routing/scorer.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
//! # }
4545
//! ```
4646
//!
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+
//!
4753
//! [`find_route`]: crate::routing::router::find_route
4854
4955
use routing;
@@ -79,6 +85,10 @@ pub type DefaultTime = Eternity;
7985
/// [`routing::Score`] implementation parameterized by [`Time`].
8086
///
8187
/// See [`Scorer`] for details.
88+
///
89+
/// # Note
90+
///
91+
/// Mixing [`Time`] types between serialization and deserialization results in undefined behavior.
8292
pub struct ScorerUsingTime<T: Time> {
8393
params: ScoringParameters,
8494
// TODO: Remove entries of closed channels.
@@ -131,6 +141,11 @@ pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
131141

132142
/// Returns the amount of time elapsed since created.
133143
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;
134149
}
135150

136151
impl<T: Time> ScorerUsingTime<T> {
@@ -218,6 +233,11 @@ impl Time for std::time::Instant {
218233
std::time::Instant::now()
219234
}
220235

236+
fn duration_since_epoch() -> Duration {
237+
use std::time::SystemTime;
238+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
239+
}
240+
221241
fn elapsed(&self) -> Duration {
222242
std::time::Instant::elapsed(self)
223243
}
@@ -231,6 +251,10 @@ impl Time for Eternity {
231251
Self
232252
}
233253

254+
fn duration_since_epoch() -> Duration {
255+
Duration::from_secs(0)
256+
}
257+
234258
fn elapsed(&self) -> Duration {
235259
Duration::from_secs(0)
236260
}
@@ -266,7 +290,7 @@ impl<T: Time> Writeable for ChannelFailure<T> {
266290
#[inline]
267291
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
268292
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)
270294
}
271295
}
272296

@@ -275,17 +299,7 @@ impl<T: Time> Readable for ChannelFailure<T> {
275299
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
276300
Ok(Self {
277301
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)?),
279303
})
280304
}
281305
}
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

Comments
 (0)