Skip to content

Commit 4187576

Browse files
committed
move time logic in scoring to its util::time module
1 parent d8c653f commit 4187576

File tree

3 files changed

+81
-73
lines changed

3 files changed

+81
-73
lines changed

lightning/src/routing/scoring.rs

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,83 +1062,12 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
10621062
}
10631063
}
10641064

1065-
pub(crate) mod time {
1066-
use core::ops::Sub;
1067-
use core::time::Duration;
1068-
/// A measurement of time.
1069-
pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
1070-
/// Returns an instance corresponding to the current moment.
1071-
fn now() -> Self;
1072-
1073-
/// Returns the amount of time elapsed since `self` was created.
1074-
fn elapsed(&self) -> Duration;
1075-
1076-
/// Returns the amount of time passed between `earlier` and `self`.
1077-
fn duration_since(&self, earlier: Self) -> Duration;
1078-
1079-
/// Returns the amount of time passed since the beginning of [`Time`].
1080-
///
1081-
/// Used during (de-)serialization.
1082-
fn duration_since_epoch() -> Duration;
1083-
}
1084-
1085-
/// A state in which time has no meaning.
1086-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1087-
pub struct Eternity;
1088-
1089-
#[cfg(not(feature = "no-std"))]
1090-
impl Time for std::time::Instant {
1091-
fn now() -> Self {
1092-
std::time::Instant::now()
1093-
}
1094-
1095-
fn duration_since(&self, earlier: Self) -> Duration {
1096-
self.duration_since(earlier)
1097-
}
1098-
1099-
fn duration_since_epoch() -> Duration {
1100-
use std::time::SystemTime;
1101-
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
1102-
}
1103-
1104-
fn elapsed(&self) -> Duration {
1105-
std::time::Instant::elapsed(self)
1106-
}
1107-
}
1108-
1109-
impl Time for Eternity {
1110-
fn now() -> Self {
1111-
Self
1112-
}
1113-
1114-
fn duration_since(&self, _earlier: Self) -> Duration {
1115-
Duration::from_secs(0)
1116-
}
1117-
1118-
fn duration_since_epoch() -> Duration {
1119-
Duration::from_secs(0)
1120-
}
1121-
1122-
fn elapsed(&self) -> Duration {
1123-
Duration::from_secs(0)
1124-
}
1125-
}
1126-
1127-
impl Sub<Duration> for Eternity {
1128-
type Output = Self;
1129-
1130-
fn sub(self, _other: Duration) -> Self {
1131-
self
1132-
}
1133-
}
1134-
}
1135-
1136-
pub(crate) use self::time::Time;
1065+
use util::time::Time;
11371066

11381067
#[cfg(test)]
11391068
mod tests {
11401069
use super::{ChannelLiquidity, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime, ScoringParameters, ScorerUsingTime, Time};
1141-
use super::time::Eternity;
1070+
use util::time::Eternity;
11421071

11431072
use ln::features::{ChannelFeatures, NodeFeatures};
11441073
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, OptionalField, UnsignedChannelAnnouncement, UnsignedChannelUpdate};

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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+

0 commit comments

Comments
 (0)