Skip to content

Commit 9628811

Browse files
committed
Use MonotonicTime as Instant shifted by 10 years forward
Such implementation allows `MonotonicTime` to go backward up to 10 years on all platforms. On some platforms (e.g. iOS) `Instant` is represented as `u64` of nanoseconds since the boot of the system. Obviously such implementation does not allow to go backward before the time of the boot.
1 parent fb140b5 commit 9628811

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

lightning/src/routing/scoring.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl ReadableArgs<u64> for FixedPenaltyScorer {
325325
}
326326

327327
#[cfg(not(feature = "no-std"))]
328-
type ConfiguredTime = std::time::Instant;
328+
type ConfiguredTime = crate::util::time::MonotonicTime;
329329
#[cfg(feature = "no-std")]
330330
use crate::util::time::Eternity;
331331
#[cfg(feature = "no-std")]

lightning/src/util/time.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,48 @@ impl Sub<Duration> for Eternity {
5858
}
5959
}
6060

61+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
62+
#[cfg(not(feature = "no-std"))]
63+
pub struct MonotonicTime(std::time::Instant);
64+
#[cfg(not(feature = "no-std"))]
65+
static SHIFT: Duration = Duration::from_secs(10 * 365 * 24 * 60 * 60); // 10 years.
66+
67+
#[cfg(not(feature = "no-std"))]
68+
impl Time for MonotonicTime {
69+
fn now() -> Self {
70+
let instant = std::time::Instant::now().checked_add(SHIFT).expect("Overflow on MonotonicTime instantiation");
71+
Self(instant)
72+
}
73+
74+
fn duration_since(&self, earlier: Self) -> Duration {
75+
// On rust prior to 1.60 `Instant::duration_since` will panic if time goes backwards.
76+
// However, we support rust versions prior to 1.60 and some users appear to have "monotonic
77+
// clocks" that go backwards in practice (likely relatively ancient kernels/etc). Thus, we
78+
// manually check for time going backwards here and return a duration of zero in that case.
79+
let now = Self::now();
80+
if now.0 > earlier.0 { now.0 - earlier.0 } else { Duration::from_secs(0) }
81+
}
82+
83+
fn duration_since_epoch() -> Duration {
84+
use std::time::SystemTime;
85+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
86+
}
87+
88+
fn elapsed(&self) -> Duration {
89+
self.0.elapsed()
90+
}
91+
}
92+
93+
#[cfg(not(feature = "no-std"))]
94+
impl Sub<Duration> for MonotonicTime {
95+
type Output = Self;
96+
97+
fn sub(self, other: Duration) -> Self {
98+
let instant = self.0.checked_sub(other).expect("MonotonicTime is not supposed to go backward futher than 10 years");
99+
Self(instant)
100+
}
101+
}
102+
61103
#[cfg(not(feature = "no-std"))]
62104
impl Time for std::time::Instant {
63105
fn now() -> Self {
@@ -84,7 +126,7 @@ impl Time for std::time::Instant {
84126

85127
#[cfg(test)]
86128
pub mod tests {
87-
use super::{Time, Eternity};
129+
use super::{Time, Eternity, MonotonicTime};
88130

89131
use core::time::Duration;
90132
use core::ops::Sub;
@@ -154,4 +196,11 @@ pub mod tests {
154196
assert_eq!(now.elapsed(), Duration::from_secs(0));
155197
assert_eq!(later - elapsed, now);
156198
}
199+
200+
#[test]
201+
fn monotonic_time_go_backward() {
202+
let now = MonotonicTime::now();
203+
let ten_years = Duration::from_secs(10 * 365 * 24 * 60 * 60);
204+
let _past = now - ten_years;
205+
}
157206
}

0 commit comments

Comments
 (0)