Skip to content

Commit f1550bb

Browse files
committed
move time logic in scoring to its util::time module
1 parent 83595db commit f1550bb

File tree

3 files changed

+158
-145
lines changed

3 files changed

+158
-145
lines changed

lightning/src/routing/scoring.rs

Lines changed: 5 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ pub type Scorer = ScorerUsingTime::<ConfiguredTime>;
252252
#[cfg(not(feature = "no-std"))]
253253
type ConfiguredTime = std::time::Instant;
254254
#[cfg(feature = "no-std")]
255-
type ConfiguredTime = time::Eternity;
255+
use util::time::Eternity;
256+
#[cfg(feature = "no-std")]
257+
type ConfiguredTime = Eternity;
256258

257259
// Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
258260
// doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
@@ -1267,83 +1269,12 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
12671269
}
12681270
}
12691271

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;
1272+
use util::time::Time;
13421273

13431274
#[cfg(test)]
13441275
mod tests {
13451276
use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime, Time};
1346-
use super::time::Eternity;
1277+
use util::time::tests::SinceEpoch;
13471278

13481279
use ln::features::{ChannelFeatures, NodeFeatures};
13491280
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
@@ -1357,80 +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-
1407-
#[test]
1408-
fn time_passes_when_advanced() {
1409-
let now = SinceEpoch::now();
1410-
assert_eq!(now.elapsed(), Duration::from_secs(0));
1411-
1412-
SinceEpoch::advance(Duration::from_secs(1));
1413-
SinceEpoch::advance(Duration::from_secs(1));
1414-
1415-
let elapsed = now.elapsed();
1416-
let later = SinceEpoch::now();
1417-
1418-
assert_eq!(elapsed, Duration::from_secs(2));
1419-
assert_eq!(later - elapsed, now);
1420-
}
1421-
1422-
#[test]
1423-
fn time_never_passes_in_an_eternity() {
1424-
let now = Eternity::now();
1425-
let elapsed = now.elapsed();
1426-
let later = Eternity::now();
1427-
1428-
assert_eq!(now.elapsed(), Duration::from_secs(0));
1429-
assert_eq!(later - elapsed, now);
1430-
}
1431-
1432-
// `Scorer` tests
1433-
14341294
/// A scorer for testing with time that can be manually advanced.
14351295
type Scorer = ScorerUsingTime::<SinceEpoch>;
14361296

lightning/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod errors;
2020
pub mod ser;
2121
pub mod message_signing;
2222
pub mod invoice;
23+
pub mod time;
2324

2425
pub(crate) mod atomic_counter;
2526
pub(crate) mod byte_utils;

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+
//! 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+
#[cfg(not(feature = "no-std"))]
34+
impl Time for std::time::Instant {
35+
fn now() -> Self {
36+
std::time::Instant::now()
37+
}
38+
39+
fn duration_since(&self, earlier: Self) -> Duration {
40+
self.duration_since(earlier)
41+
}
42+
43+
fn duration_since_epoch() -> Duration {
44+
use std::time::SystemTime;
45+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
46+
}
47+
48+
fn elapsed(&self) -> Duration {
49+
std::time::Instant::elapsed(self)
50+
}
51+
}
52+
53+
impl Time for Eternity {
54+
fn now() -> Self {
55+
Self
56+
}
57+
58+
fn duration_since(&self, _earlier: Self) -> Duration {
59+
Duration::from_secs(0)
60+
}
61+
62+
fn duration_since_epoch() -> Duration {
63+
Duration::from_secs(0)
64+
}
65+
66+
fn elapsed(&self) -> Duration {
67+
Duration::from_secs(0)
68+
}
69+
}
70+
71+
impl Sub<Duration> for Eternity {
72+
type Output = Self;
73+
74+
fn sub(self, _other: Duration) -> Self {
75+
self
76+
}
77+
}
78+
79+
#[cfg(test)]
80+
pub mod tests {
81+
use util::time::{Eternity, Time};
82+
83+
use core::cell::Cell;
84+
use core::ops::Sub;
85+
use core::time::Duration;
86+
87+
/// Time that can be advanced manually in tests.
88+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89+
pub struct SinceEpoch(Duration);
90+
91+
impl SinceEpoch {
92+
thread_local! {
93+
static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
94+
}
95+
96+
pub fn advance(duration: Duration) {
97+
Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
98+
}
99+
}
100+
101+
impl Time for SinceEpoch {
102+
fn now() -> Self {
103+
Self(Self::duration_since_epoch())
104+
}
105+
106+
fn duration_since(&self, earlier: Self) -> Duration {
107+
self.0 - earlier.0
108+
}
109+
110+
fn duration_since_epoch() -> Duration {
111+
Self::ELAPSED.with(|elapsed| elapsed.get())
112+
}
113+
114+
fn elapsed(&self) -> Duration {
115+
Self::duration_since_epoch() - self.0
116+
}
117+
}
118+
119+
impl Sub<Duration> for SinceEpoch {
120+
type Output = Self;
121+
122+
fn sub(self, other: Duration) -> Self {
123+
Self(self.0 - other)
124+
}
125+
}
126+
127+
#[test]
128+
fn time_passes_when_advanced() {
129+
let now = SinceEpoch::now();
130+
assert_eq!(now.elapsed(), Duration::from_secs(0));
131+
132+
SinceEpoch::advance(Duration::from_secs(1));
133+
SinceEpoch::advance(Duration::from_secs(1));
134+
135+
let elapsed = now.elapsed();
136+
let later = SinceEpoch::now();
137+
138+
assert_eq!(elapsed, Duration::from_secs(2));
139+
assert_eq!(later - elapsed, now);
140+
}
141+
142+
#[test]
143+
fn time_never_passes_in_an_eternity() {
144+
let now = Eternity::now();
145+
let elapsed = now.elapsed();
146+
let later = Eternity::now();
147+
148+
assert_eq!(now.elapsed(), Duration::from_secs(0));
149+
assert_eq!(later - elapsed, now);
150+
}
151+
}
152+

0 commit comments

Comments
 (0)