Skip to content

Commit

Permalink
f - Add duration_since_epoch to Time and note undefined behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Nov 1, 2021
1 parent aba246d commit 73ffbcf
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions lightning/src/routing/scorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
//! # }
//! ```
//!
//! # Note
//!
//! If persisting [`Scorer`], it must be restored using the same [`Time`] parameterization. Using a
//! different type results in undefined behavior. Specifically, persisting when built with feature
//! `no-std` and restoring without it, or vice versa, uses different types and thus is undefined.
//!
//! [`find_route`]: crate::routing::router::find_route
use routing;
Expand Down Expand Up @@ -79,6 +85,10 @@ pub type DefaultTime = Eternity;
/// [`routing::Score`] implementation parameterized by [`Time`].
///
/// See [`Scorer`] for details.
///
/// # Note
///
/// Mixing [`Time`] types between serialization and deserialization results in undefined behavior.
pub struct ScorerUsingTime<T: Time> {
params: ScoringParameters,
// TODO: Remove entries of closed channels.
Expand Down Expand Up @@ -131,6 +141,11 @@ pub trait Time: Sub<Duration, Output = Self> where Self: Sized {

/// Returns the amount of time elapsed since created.
fn elapsed(&self) -> Duration;

/// Returns the amount of time passed since the beginning of [`Time`].
///
/// Used during (de-)serialization.
fn duration_since_epoch() -> Duration;
}

impl<T: Time> ScorerUsingTime<T> {
Expand Down Expand Up @@ -218,6 +233,11 @@ impl Time for std::time::Instant {
std::time::Instant::now()
}

fn duration_since_epoch() -> Duration {
use std::time::SystemTime;
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
}

fn elapsed(&self) -> Duration {
std::time::Instant::elapsed(self)
}
Expand All @@ -231,6 +251,10 @@ impl Time for Eternity {
Self
}

fn duration_since_epoch() -> Duration {
Duration::from_secs(0)
}

fn elapsed(&self) -> Duration {
Duration::from_secs(0)
}
Expand Down Expand Up @@ -266,7 +290,7 @@ impl<T: Time> Writeable for ChannelFailure<T> {
#[inline]
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
self.undecayed_penalty_msat.write(w)?;
(duration_since_epoch() - self.last_failed.elapsed()).write(w)
(T::duration_since_epoch() - self.last_failed.elapsed()).write(w)
}
}

Expand All @@ -275,17 +299,7 @@ impl<T: Time> Readable for ChannelFailure<T> {
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
Ok(Self {
undecayed_penalty_msat: Readable::read(r)?,
last_failed: T::now() - (duration_since_epoch() - Readable::read(r)?),
last_failed: T::now() - (T::duration_since_epoch() - Readable::read(r)?),
})
}
}

fn duration_since_epoch() -> Duration {
#[cfg(not(feature = "no-std"))]
{
use std::time::SystemTime;
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
}
#[cfg(feature = "no-std")]
return Duration::from_secs(0);
}

0 comments on commit 73ffbcf

Please sign in to comment.