Skip to content

Commit 10f9795

Browse files
committed
move Time trait from scoring mod to util::time and set it visibile within crate
1 parent d166044 commit 10f9795

File tree

3 files changed

+160
-147
lines changed

3 files changed

+160
-147
lines changed

lightning/src/routing/scoring.rs

Lines changed: 7 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use routing::network_graph::{NetworkGraph, NodeId};
5959
use routing::router::RouteHop;
6060
use util::ser::{Readable, ReadableArgs, Writeable, Writer};
6161
use util::logger::Logger;
62+
use util::time::Time;
6263

6364
use prelude::*;
6465
use core::fmt;
@@ -262,7 +263,9 @@ pub type Scorer = ScorerUsingTime::<ConfiguredTime>;
262263
#[cfg(not(feature = "no-std"))]
263264
type ConfiguredTime = std::time::Instant;
264265
#[cfg(feature = "no-std")]
265-
type ConfiguredTime = time::Eternity;
266+
use util::time::Eternity;
267+
#[cfg(feature = "no-std")]
268+
type ConfiguredTime = Eternity;
266269

267270
// Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
268271
// doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
@@ -1327,83 +1330,11 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
13271330
}
13281331
}
13291332

1330-
pub(crate) mod time {
1331-
use core::ops::Sub;
1332-
use core::time::Duration;
1333-
/// A measurement of time.
1334-
pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
1335-
/// Returns an instance corresponding to the current moment.
1336-
fn now() -> Self;
1337-
1338-
/// Returns the amount of time elapsed since `self` was created.
1339-
fn elapsed(&self) -> Duration;
1340-
1341-
/// Returns the amount of time passed between `earlier` and `self`.
1342-
fn duration_since(&self, earlier: Self) -> Duration;
1343-
1344-
/// Returns the amount of time passed since the beginning of [`Time`].
1345-
///
1346-
/// Used during (de-)serialization.
1347-
fn duration_since_epoch() -> Duration;
1348-
}
1349-
1350-
/// A state in which time has no meaning.
1351-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1352-
pub struct Eternity;
1353-
1354-
#[cfg(not(feature = "no-std"))]
1355-
impl Time for std::time::Instant {
1356-
fn now() -> Self {
1357-
std::time::Instant::now()
1358-
}
1359-
1360-
fn duration_since(&self, earlier: Self) -> Duration {
1361-
self.duration_since(earlier)
1362-
}
1363-
1364-
fn duration_since_epoch() -> Duration {
1365-
use std::time::SystemTime;
1366-
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
1367-
}
1368-
1369-
fn elapsed(&self) -> Duration {
1370-
std::time::Instant::elapsed(self)
1371-
}
1372-
}
1373-
1374-
impl Time for Eternity {
1375-
fn now() -> Self {
1376-
Self
1377-
}
1378-
1379-
fn duration_since(&self, _earlier: Self) -> Duration {
1380-
Duration::from_secs(0)
1381-
}
1382-
1383-
fn duration_since_epoch() -> Duration {
1384-
Duration::from_secs(0)
1385-
}
1386-
1387-
fn elapsed(&self) -> Duration {
1388-
Duration::from_secs(0)
1389-
}
1390-
}
1391-
1392-
impl Sub<Duration> for Eternity {
1393-
type Output = Self;
1394-
1395-
fn sub(self, _other: Duration) -> Self {
1396-
self
1397-
}
1398-
}
1399-
}
1400-
1401-
pub(crate) use self::time::Time;
1402-
14031333
#[cfg(test)]
14041334
mod tests {
1405-
use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime, Time};
1406-
use super::time::Eternity;
1335+
use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime};
1336+
use util::time::Time;
1337+
use util::time::tests::SinceEpoch;
14071338

14081339
use ln::features::{ChannelFeatures, NodeFeatures};
14091340
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
@@ -1418,80 +1349,9 @@ mod tests {
14181349
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
14191350
use bitcoin::network::constants::Network;
14201351
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
1421-
use core::cell::Cell;
1422-
use core::ops::Sub;
14231352
use core::time::Duration;
14241353
use io;
14251354

1426-
// `Time` tests
1427-
1428-
/// Time that can be advanced manually in tests.
1429-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1430-
struct SinceEpoch(Duration);
1431-
1432-
impl SinceEpoch {
1433-
thread_local! {
1434-
static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
1435-
}
1436-
1437-
fn advance(duration: Duration) {
1438-
Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
1439-
}
1440-
}
1441-
1442-
impl Time for SinceEpoch {
1443-
fn now() -> Self {
1444-
Self(Self::duration_since_epoch())
1445-
}
1446-
1447-
fn duration_since(&self, earlier: Self) -> Duration {
1448-
self.0 - earlier.0
1449-
}
1450-
1451-
fn duration_since_epoch() -> Duration {
1452-
Self::ELAPSED.with(|elapsed| elapsed.get())
1453-
}
1454-
1455-
fn elapsed(&self) -> Duration {
1456-
Self::duration_since_epoch() - self.0
1457-
}
1458-
}
1459-
1460-
impl Sub<Duration> for SinceEpoch {
1461-
type Output = Self;
1462-
1463-
fn sub(self, other: Duration) -> Self {
1464-
Self(self.0 - other)
1465-
}
1466-
}
1467-
1468-
#[test]
1469-
fn time_passes_when_advanced() {
1470-
let now = SinceEpoch::now();
1471-
assert_eq!(now.elapsed(), Duration::from_secs(0));
1472-
1473-
SinceEpoch::advance(Duration::from_secs(1));
1474-
SinceEpoch::advance(Duration::from_secs(1));
1475-
1476-
let elapsed = now.elapsed();
1477-
let later = SinceEpoch::now();
1478-
1479-
assert_eq!(elapsed, Duration::from_secs(2));
1480-
assert_eq!(later - elapsed, now);
1481-
}
1482-
1483-
#[test]
1484-
fn time_never_passes_in_an_eternity() {
1485-
let now = Eternity::now();
1486-
let elapsed = now.elapsed();
1487-
let later = Eternity::now();
1488-
1489-
assert_eq!(now.elapsed(), Duration::from_secs(0));
1490-
assert_eq!(later - elapsed, now);
1491-
}
1492-
1493-
// `Scorer` tests
1494-
14951355
/// A scorer for testing with time that can be manually advanced.
14961356
type Scorer = ScorerUsingTime::<SinceEpoch>;
14971357

lightning/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub(crate) mod poly1305;
3636
pub(crate) mod chacha20poly1305rfc;
3737
pub(crate) mod transaction_utils;
3838
pub(crate) mod scid_utils;
39+
pub(crate) mod time;
3940

4041
/// Logging macro utilities.
4142
#[macro_use]

lightning/src/util/time.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
2+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
4+
// You may not use this file except in accordance with one or both of these
5+
// licenses.
6+
7+
//! [`Time`] trait and different implementations. Currently, it's mainly used in tests so we can
8+
//! manually advance time.
9+
//! Other crates may symlink this file to use it while [`Time`] trait is sealed here.
10+
11+
use core::ops::Sub;
12+
use core::time::Duration;
13+
14+
/// A measurement of time.
15+
pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
16+
/// Returns an instance corresponding to the current moment.
17+
fn now() -> Self;
18+
19+
/// Returns the amount of time elapsed since `self` was created.
20+
fn elapsed(&self) -> Duration;
21+
22+
/// Returns the amount of time passed between `earlier` and `self`.
23+
fn duration_since(&self, earlier: Self) -> Duration;
24+
25+
/// Returns the amount of time passed since the beginning of [`Time`].
26+
///
27+
/// Used during (de-)serialization.
28+
fn duration_since_epoch() -> Duration;
29+
}
30+
31+
/// A state in which time has no meaning.
32+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33+
pub struct Eternity;
34+
35+
impl Time for Eternity {
36+
fn now() -> Self {
37+
Self
38+
}
39+
40+
fn duration_since(&self, _earlier: Self) -> Duration {
41+
Duration::from_secs(0)
42+
}
43+
44+
fn duration_since_epoch() -> Duration {
45+
Duration::from_secs(0)
46+
}
47+
48+
fn elapsed(&self) -> Duration {
49+
Duration::from_secs(0)
50+
}
51+
}
52+
53+
impl Sub<Duration> for Eternity {
54+
type Output = Self;
55+
56+
fn sub(self, _other: Duration) -> Self {
57+
self
58+
}
59+
}
60+
61+
#[cfg(not(feature = "no-std"))]
62+
impl Time for std::time::Instant {
63+
fn now() -> Self {
64+
std::time::Instant::now()
65+
}
66+
67+
fn duration_since(&self, earlier: Self) -> Duration {
68+
self.duration_since(earlier)
69+
}
70+
71+
fn duration_since_epoch() -> Duration {
72+
use std::time::SystemTime;
73+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
74+
}
75+
fn elapsed(&self) -> Duration {
76+
std::time::Instant::elapsed(self)
77+
}
78+
}
79+
80+
#[cfg(test)]
81+
pub mod tests {
82+
use super::{Time, Eternity};
83+
84+
use core::time::Duration;
85+
use core::ops::Sub;
86+
use core::cell::Cell;
87+
88+
/// Time that can be advanced manually in tests.
89+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
90+
pub struct SinceEpoch(Duration);
91+
92+
impl SinceEpoch {
93+
thread_local! {
94+
static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
95+
}
96+
97+
pub fn advance(duration: Duration) {
98+
Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
99+
}
100+
}
101+
102+
impl Time for SinceEpoch {
103+
fn now() -> Self {
104+
Self(Self::duration_since_epoch())
105+
}
106+
107+
fn duration_since(&self, earlier: Self) -> Duration {
108+
self.0 - earlier.0
109+
}
110+
111+
fn duration_since_epoch() -> Duration {
112+
Self::ELAPSED.with(|elapsed| elapsed.get())
113+
}
114+
115+
fn elapsed(&self) -> Duration {
116+
Self::duration_since_epoch() - self.0
117+
}
118+
}
119+
120+
impl Sub<Duration> for SinceEpoch {
121+
type Output = Self;
122+
123+
fn sub(self, other: Duration) -> Self {
124+
Self(self.0 - other)
125+
}
126+
}
127+
128+
#[test]
129+
fn time_passes_when_advanced() {
130+
let now = SinceEpoch::now();
131+
assert_eq!(now.elapsed(), Duration::from_secs(0));
132+
133+
SinceEpoch::advance(Duration::from_secs(1));
134+
SinceEpoch::advance(Duration::from_secs(1));
135+
136+
let elapsed = now.elapsed();
137+
let later = SinceEpoch::now();
138+
139+
assert_eq!(elapsed, Duration::from_secs(2));
140+
assert_eq!(later - elapsed, now);
141+
}
142+
143+
#[test]
144+
fn time_never_passes_in_an_eternity() {
145+
let now = Eternity::now();
146+
let elapsed = now.elapsed();
147+
let later = Eternity::now();
148+
149+
assert_eq!(now.elapsed(), Duration::from_secs(0));
150+
assert_eq!(later - elapsed, now);
151+
}
152+
}

0 commit comments

Comments
 (0)