Skip to content

Commit 3da75a8

Browse files
committed
move Time trait from scoring mod to util::time and set it visibile within crate
1 parent b4c645a commit 3da75a8

File tree

3 files changed

+178
-120
lines changed

3 files changed

+178
-120
lines changed

lightning/src/routing/scoring.rs

Lines changed: 7 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use ln::msgs::DecodeError;
5858
use routing::network_graph::{NetworkGraph, NodeId};
5959
use routing::router::RouteHop;
6060
use util::ser::{Readable, ReadableArgs, Writeable, Writer};
61+
use util::time::Time;
6162

6263
use prelude::*;
6364
use core::cell::{RefCell, RefMut};
@@ -252,7 +253,9 @@ pub type Scorer = ScorerUsingTime::<ConfiguredTime>;
252253
#[cfg(not(feature = "no-std"))]
253254
type ConfiguredTime = std::time::Instant;
254255
#[cfg(feature = "no-std")]
255-
type ConfiguredTime = time::Eternity;
256+
use util::time::Eternity;
257+
#[cfg(feature = "no-std")]
258+
type ConfiguredTime = Eternity;
256259

257260
// Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
258261
// doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
@@ -1267,83 +1270,11 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
12671270
}
12681271
}
12691272

1270-
pub(crate) mod time {
1271-
use core::ops::Sub;
1272-
use core::time::Duration;
1273-
/// A measurement of time.
1274-
pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
1275-
/// Returns an instance corresponding to the current moment.
1276-
fn now() -> Self;
1277-
1278-
/// Returns the amount of time elapsed since `self` was created.
1279-
fn elapsed(&self) -> Duration;
1280-
1281-
/// Returns the amount of time passed between `earlier` and `self`.
1282-
fn duration_since(&self, earlier: Self) -> Duration;
1283-
1284-
/// Returns the amount of time passed since the beginning of [`Time`].
1285-
///
1286-
/// Used during (de-)serialization.
1287-
fn duration_since_epoch() -> Duration;
1288-
}
1289-
1290-
/// A state in which time has no meaning.
1291-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1292-
pub struct Eternity;
1293-
1294-
#[cfg(not(feature = "no-std"))]
1295-
impl Time for std::time::Instant {
1296-
fn now() -> Self {
1297-
std::time::Instant::now()
1298-
}
1299-
1300-
fn duration_since(&self, earlier: Self) -> Duration {
1301-
self.duration_since(earlier)
1302-
}
1303-
1304-
fn duration_since_epoch() -> Duration {
1305-
use std::time::SystemTime;
1306-
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
1307-
}
1308-
1309-
fn elapsed(&self) -> Duration {
1310-
std::time::Instant::elapsed(self)
1311-
}
1312-
}
1313-
1314-
impl Time for Eternity {
1315-
fn now() -> Self {
1316-
Self
1317-
}
1318-
1319-
fn duration_since(&self, _earlier: Self) -> Duration {
1320-
Duration::from_secs(0)
1321-
}
1322-
1323-
fn duration_since_epoch() -> Duration {
1324-
Duration::from_secs(0)
1325-
}
1326-
1327-
fn elapsed(&self) -> Duration {
1328-
Duration::from_secs(0)
1329-
}
1330-
}
1331-
1332-
impl Sub<Duration> for Eternity {
1333-
type Output = Self;
1334-
1335-
fn sub(self, _other: Duration) -> Self {
1336-
self
1337-
}
1338-
}
1339-
}
1340-
1341-
pub(crate) use self::time::Time;
1342-
13431273
#[cfg(test)]
13441274
mod tests {
1345-
use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime, Time};
1346-
use super::time::Eternity;
1275+
use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime};
1276+
use util::time::{Eternity, Time};
1277+
use util::time::tests::SinceEpoch;
13471278

13481279
use ln::features::{ChannelFeatures, NodeFeatures};
13491280
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
@@ -1357,53 +1288,9 @@ mod tests {
13571288
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
13581289
use bitcoin::network::constants::Network;
13591290
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
1360-
use core::cell::Cell;
1361-
use core::ops::Sub;
13621291
use core::time::Duration;
13631292
use io;
13641293

1365-
// `Time` tests
1366-
1367-
/// Time that can be advanced manually in tests.
1368-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1369-
struct SinceEpoch(Duration);
1370-
1371-
impl SinceEpoch {
1372-
thread_local! {
1373-
static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
1374-
}
1375-
1376-
fn advance(duration: Duration) {
1377-
Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
1378-
}
1379-
}
1380-
1381-
impl Time for SinceEpoch {
1382-
fn now() -> Self {
1383-
Self(Self::duration_since_epoch())
1384-
}
1385-
1386-
fn duration_since(&self, earlier: Self) -> Duration {
1387-
self.0 - earlier.0
1388-
}
1389-
1390-
fn duration_since_epoch() -> Duration {
1391-
Self::ELAPSED.with(|elapsed| elapsed.get())
1392-
}
1393-
1394-
fn elapsed(&self) -> Duration {
1395-
Self::duration_since_epoch() - self.0
1396-
}
1397-
}
1398-
1399-
impl Sub<Duration> for SinceEpoch {
1400-
type Output = Self;
1401-
1402-
fn sub(self, other: Duration) -> Self {
1403-
Self(self.0 - other)
1404-
}
1405-
}
1406-
14071294
#[test]
14081295
fn time_passes_when_advanced() {
14091296
let now = SinceEpoch::now();

lightning/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub(crate) mod poly1305;
3333
pub(crate) mod chacha20poly1305rfc;
3434
pub(crate) mod transaction_utils;
3535
pub(crate) mod scid_utils;
36+
pub(crate) mod time;
3637

3738
/// Logging macro utilities.
3839
#[macro_use]

lightning/src/util/time.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
//! A very simple serialization framework which is used to serialize/deserialize messages as well
8+
//! as ChannelsManagers and ChannelMonitors.
9+
10+
use core::ops::Sub;
11+
use core::time::Duration;
12+
/// A measurement of time.
13+
pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
14+
/// Returns an instance corresponding to the current moment.
15+
fn now() -> Self;
16+
17+
/// Returns the amount of time elapsed since `self` was created.
18+
fn elapsed(&self) -> Duration;
19+
20+
/// Returns the amount of time passed between `earlier` and `self`.
21+
fn duration_since(&self, earlier: Self) -> Duration;
22+
23+
/// Returns the amount of time passed since the beginning of [`Time`].
24+
///
25+
/// Used during (de-)serialization.
26+
fn duration_since_epoch() -> Duration;
27+
}
28+
29+
/// A state in which time has no meaning.
30+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31+
pub struct Eternity;
32+
33+
impl Time for Eternity {
34+
fn now() -> Self {
35+
Self
36+
}
37+
38+
fn duration_since(&self, _earlier: Self) -> Duration {
39+
Duration::from_secs(0)
40+
}
41+
42+
fn duration_since_epoch() -> Duration {
43+
Duration::from_secs(0)
44+
}
45+
46+
fn elapsed(&self) -> Duration {
47+
Duration::from_secs(0)
48+
}
49+
}
50+
51+
impl Sub<Duration> for Eternity {
52+
type Output = Self;
53+
54+
fn sub(self, _other: Duration) -> Self {
55+
self
56+
}
57+
}
58+
59+
#[cfg(not(feature = "no-std"))]
60+
impl Time for std::time::Instant {
61+
fn now() -> Self {
62+
std::time::Instant::now()
63+
}
64+
65+
fn duration_since(&self, earlier: Self) -> Duration {
66+
self.duration_since(earlier)
67+
}
68+
69+
fn duration_since_epoch() -> Duration {
70+
use std::time::SystemTime;
71+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
72+
}
73+
fn elapsed(&self) -> Duration {
74+
std::time::Instant::elapsed(self)
75+
}
76+
}
77+
78+
#[cfg(not(feature = "no-std"))]
79+
impl Time for std::time::SystemTime {
80+
fn now() -> Self {
81+
std::time::SystemTime::now()
82+
}
83+
84+
fn duration_since(&self, earlier: Self) -> Duration {
85+
self.duration_since(earlier).unwrap()
86+
}
87+
88+
fn duration_since_epoch() -> Duration {
89+
use std::time::SystemTime;
90+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
91+
}
92+
93+
fn elapsed(&self) -> Duration {
94+
std::time::SystemTime::elapsed(self).unwrap()
95+
}
96+
}
97+
98+
#[cfg(test)]
99+
pub mod tests {
100+
use super::{Time, Eternity};
101+
102+
use core::time::Duration;
103+
use core::ops::Sub;
104+
use core::cell::Cell;
105+
106+
/// Time that can be advanced manually in tests.
107+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
108+
pub struct SinceEpoch(Duration);
109+
110+
impl SinceEpoch {
111+
thread_local! {
112+
static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
113+
}
114+
115+
pub fn advance(duration: Duration) {
116+
Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
117+
}
118+
}
119+
120+
impl Time for SinceEpoch {
121+
fn now() -> Self {
122+
Self(Self::duration_since_epoch())
123+
}
124+
125+
fn duration_since(&self, earlier: Self) -> Duration {
126+
self.0 - earlier.0
127+
}
128+
129+
fn duration_since_epoch() -> Duration {
130+
Self::ELAPSED.with(|elapsed| elapsed.get())
131+
}
132+
133+
fn elapsed(&self) -> Duration {
134+
Self::duration_since_epoch() - self.0
135+
}
136+
}
137+
138+
impl Sub<Duration> for SinceEpoch {
139+
type Output = Self;
140+
141+
fn sub(self, other: Duration) -> Self {
142+
Self(self.0 - other)
143+
}
144+
}
145+
146+
#[test]
147+
fn time_passes_when_advanced() {
148+
let now = SinceEpoch::now();
149+
assert_eq!(now.elapsed(), Duration::from_secs(0));
150+
151+
SinceEpoch::advance(Duration::from_secs(1));
152+
SinceEpoch::advance(Duration::from_secs(1));
153+
154+
let elapsed = now.elapsed();
155+
let later = SinceEpoch::now();
156+
157+
assert_eq!(elapsed, Duration::from_secs(2));
158+
assert_eq!(later - elapsed, now);
159+
}
160+
161+
#[test]
162+
fn time_never_passes_in_an_eternity() {
163+
let now = Eternity::now();
164+
let elapsed = now.elapsed();
165+
let later = Eternity::now();
166+
167+
assert_eq!(now.elapsed(), Duration::from_secs(0));
168+
assert_eq!(later - elapsed, now);
169+
}
170+
}

0 commit comments

Comments
 (0)