Merge pull request #2967 from tnull/2024-03-refactor-drop-handle-message
[rust-lightning] / lightning / src / util / time.rs
index d3768aa7ca6441d2943c5ac080e57113ab9d957d..a6e6f4d1fda6b6e58a102a0baeb97cbed102159f 100644 (file)
@@ -58,22 +58,48 @@ impl Sub<Duration> for Eternity {
        }
 }
 
-#[cfg(not(feature = "no-std"))]
-impl Time for std::time::Instant {
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[cfg(feature = "std")]
+pub struct MonotonicTime(std::time::Instant);
+
+/// The amount of time to shift `Instant` forward to prevent overflow when subtracting a `Duration`
+/// from `Instant::now` on some operating systems (e.g., iOS representing `Instance` as `u64`).
+#[cfg(feature = "std")]
+const SHIFT: Duration = Duration::from_secs(10 * 365 * 24 * 60 * 60); // 10 years.
+
+#[cfg(feature = "std")]
+impl Time for MonotonicTime {
        fn now() -> Self {
-               std::time::Instant::now()
+               let instant = std::time::Instant::now().checked_add(SHIFT).expect("Overflow on MonotonicTime instantiation");
+               Self(instant)
        }
 
        fn duration_since(&self, earlier: Self) -> Duration {
-               self.duration_since(earlier)
+               // On rust prior to 1.60 `Instant::duration_since` will panic if time goes backwards.
+               // However, we support rust versions prior to 1.60 and some users appear to have "monotonic
+               // clocks" that go backwards in practice (likely relatively ancient kernels/etc). Thus, we
+               // manually check for time going backwards here and return a duration of zero in that case.
+               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 {
-               std::time::Instant::elapsed(self)
+               Self::now().0 - self.0
+       }
+}
+
+#[cfg(feature = "std")]
+impl Sub<Duration> for MonotonicTime {
+       type Output = Self;
+
+       fn sub(self, other: Duration) -> Self {
+               let instant = self.0.checked_sub(other).expect("MonotonicTime is not supposed to go backward futher than 10 years");
+               Self(instant)
        }
 }
 
@@ -149,4 +175,15 @@ pub mod tests {
                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);
+       }
 }