From: Matt Corallo Date: Thu, 21 Mar 2024 15:44:45 +0000 (+0000) Subject: Drop now-unused methods on `Time` X-Git-Tag: v0.0.123-beta~16^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=d9b9854e87b81e43bcd33ae7bc059cf40af5b5e1;p=rust-lightning Drop now-unused methods on `Time` We no longer use `Time` during scoring, which makes several of its methods now useless. We remove those here. --- diff --git a/lightning/src/util/time.rs b/lightning/src/util/time.rs index a6e6f4d1f..bedeab1d4 100644 --- a/lightning/src/util/time.rs +++ b/lightning/src/util/time.rs @@ -16,16 +16,8 @@ pub trait Time: Copy + Sub where Self: Sized { /// Returns an instance corresponding to the current moment. fn now() -> Self; - /// Returns the amount of time elapsed since `self` was created. - fn elapsed(&self) -> Duration; - /// Returns the amount of time passed between `earlier` and `self`. fn duration_since(&self, earlier: Self) -> Duration; - - /// Returns the amount of time passed since the beginning of [`Time`]. - /// - /// Used during (de-)serialization. - fn duration_since_epoch() -> Duration; } /// A state in which time has no meaning. @@ -40,14 +32,6 @@ impl Time for Eternity { fn duration_since(&self, _earlier: Self) -> Duration { Duration::from_secs(0) } - - fn duration_since_epoch() -> Duration { - Duration::from_secs(0) - } - - fn elapsed(&self) -> Duration { - Duration::from_secs(0) - } } impl Sub for Eternity { @@ -82,15 +66,6 @@ impl Time for MonotonicTime { let now = Self::now(); if now.0 > earlier.0 { now.0 - earlier.0 } else { Duration::from_secs(0) } } - - fn duration_since_epoch() -> Duration { - use std::time::SystemTime; - SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap() - } - - fn elapsed(&self) -> Duration { - Self::now().0 - self.0 - } } #[cfg(feature = "std")] @@ -127,20 +102,12 @@ pub mod tests { impl Time for SinceEpoch { fn now() -> Self { - Self(Self::duration_since_epoch()) + Self(Self::ELAPSED.with(|elapsed| elapsed.get())) } fn duration_since(&self, earlier: Self) -> Duration { self.0 - earlier.0 } - - fn duration_since_epoch() -> Duration { - Self::ELAPSED.with(|elapsed| elapsed.get()) - } - - fn elapsed(&self) -> Duration { - Self::duration_since_epoch() - self.0 - } } impl Sub for SinceEpoch { @@ -154,36 +121,20 @@ pub mod tests { #[test] fn time_passes_when_advanced() { let now = SinceEpoch::now(); - assert_eq!(now.elapsed(), Duration::from_secs(0)); SinceEpoch::advance(Duration::from_secs(1)); SinceEpoch::advance(Duration::from_secs(1)); - let elapsed = now.elapsed(); let later = SinceEpoch::now(); - assert_eq!(elapsed, Duration::from_secs(2)); - assert_eq!(later - elapsed, now); + assert_eq!(now.0 + Duration::from_secs(2), later.0); } #[test] fn time_never_passes_in_an_eternity() { let now = Eternity::now(); - let elapsed = now.elapsed(); let later = Eternity::now(); - assert_eq!(now.elapsed(), Duration::from_secs(0)); - assert_eq!(later - elapsed, now); - } - - #[test] - #[cfg(feature = "std")] - fn monotonic_time_subtracts() { - let now = super::MonotonicTime::now(); - assert!(now.elapsed() < Duration::from_secs(10)); - - let ten_years = Duration::from_secs(10 * 365 * 24 * 60 * 60); - let past = now - ten_years; - assert!(past.elapsed() >= ten_years); + assert_eq!(later, now); } }