Skip to content

Commit a4cb4db

Browse files
committed
Replace Instant wiht MonotonicTime in outbound_payment.rs
1 parent 9628811 commit a4cb4db

File tree

2 files changed

+5
-28
lines changed

2 files changed

+5
-28
lines changed

lightning/src/ln/outbound_payment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl Retry {
239239
},
240240
#[cfg(all(not(feature = "no-std"), not(test)))]
241241
(Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
242-
*max_duration >= std::time::Instant::now().duration_since(*first_attempted_at),
242+
*max_duration >= crate::util::time::MonotonicTime::now().duration_since(*first_attempted_at),
243243
#[cfg(all(not(feature = "no-std"), test))]
244244
(Retry::Timeout(max_duration), PaymentAttempts { first_attempted_at, .. }) =>
245245
*max_duration >= SinceEpoch::now().duration_since(*first_attempted_at),
@@ -274,7 +274,7 @@ pub(crate) struct PaymentAttemptsUsingTime<T: Time> {
274274
}
275275

276276
#[cfg(not(any(feature = "no-std", test)))]
277-
type ConfiguredTime = std::time::Instant;
277+
type ConfiguredTime = crate::util::time::MonotonicTime;
278278
#[cfg(feature = "no-std")]
279279
type ConfiguredTime = crate::util::time::Eternity;
280280
#[cfg(all(not(feature = "no-std"), test))]

lightning/src/util/time.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,9 @@ impl Sub<Duration> for MonotonicTime {
100100
}
101101
}
102102

103-
#[cfg(not(feature = "no-std"))]
104-
impl Time for std::time::Instant {
105-
fn now() -> Self {
106-
std::time::Instant::now()
107-
}
108-
109-
fn duration_since(&self, earlier: Self) -> Duration {
110-
// On rust prior to 1.60 `Instant::duration_since` will panic if time goes backwards.
111-
// However, we support rust versions prior to 1.60 and some users appear to have "monotonic
112-
// clocks" that go backwards in practice (likely relatively ancient kernels/etc). Thus, we
113-
// manually check for time going backwards here and return a duration of zero in that case.
114-
let now = Self::now();
115-
if now > earlier { now - earlier } else { Duration::from_secs(0) }
116-
}
117-
118-
fn duration_since_epoch() -> Duration {
119-
use std::time::SystemTime;
120-
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
121-
}
122-
fn elapsed(&self) -> Duration {
123-
std::time::Instant::elapsed(self)
124-
}
125-
}
126-
127103
#[cfg(test)]
128104
pub mod tests {
129-
use super::{Time, Eternity, MonotonicTime};
105+
use super::{Time, Eternity};
130106

131107
use core::time::Duration;
132108
use core::ops::Sub;
@@ -198,8 +174,9 @@ pub mod tests {
198174
}
199175

200176
#[test]
177+
#[cfg(not(feature = "no-std"))]
201178
fn monotonic_time_go_backward() {
202-
let now = MonotonicTime::now();
179+
let now = super::MonotonicTime::now();
203180
let ten_years = Duration::from_secs(10 * 365 * 24 * 60 * 60);
204181
let _past = now - ten_years;
205182
}

0 commit comments

Comments
 (0)