Skip to content

Commit 4507d6f

Browse files
andrei-21jkczyz
authored andcommitted
Replace Instant wiht MonotonicTime in outbound_payment.rs
1 parent 36a199f commit 4507d6f

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
@@ -103,33 +103,9 @@ impl Sub<Duration> for MonotonicTime {
103103
}
104104
}
105105

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

134110
use core::time::Duration;
135111
use core::ops::Sub;
@@ -201,8 +177,9 @@ pub mod tests {
201177
}
202178

203179
#[test]
180+
#[cfg(not(feature = "no-std"))]
204181
fn monotonic_time_go_backward() {
205-
let now = MonotonicTime::now();
182+
let now = super::MonotonicTime::now();
206183
let ten_years = Duration::from_secs(10 * 365 * 24 * 60 * 60);
207184
let _past = now - ten_years;
208185
}

0 commit comments

Comments
 (0)