Skip to content

Commit a299cbe

Browse files
committed
sim-lib: add clock trait to mock time with default impl
Add a trait to mock time so that when we get to using simulation time, we don't need to refactor. At present, this is just a wrapper over system time.
1 parent 3dc2eb7 commit a299cbe

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

simln-lib/src/clock.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use async_trait::async_trait;
2+
use std::time::{Duration, SystemTime};
3+
use tokio::time;
4+
5+
#[async_trait]
6+
pub trait Clock: Send + Sync {
7+
fn now(&self) -> SystemTime;
8+
async fn sleep(&self, wait: Duration);
9+
}
10+
11+
/// Provides a wall clock implementation of the Clock trait.
12+
#[derive(Clone)]
13+
pub struct SystemClock {}
14+
15+
#[async_trait]
16+
impl Clock for SystemClock {
17+
fn now(&self) -> SystemTime {
18+
SystemTime::now()
19+
}
20+
21+
async fn sleep(&self, wait: Duration) {
22+
time::sleep(wait).await;
23+
}
24+
}

simln-lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use self::defined_activity::DefinedPaymentActivity;
2929
use self::random_activity::{NetworkGraphView, RandomPaymentActivity};
3030

3131
pub mod cln;
32+
pub mod clock;
3233
mod defined_activity;
3334
pub mod eclair;
3435
pub mod lnd;

0 commit comments

Comments
 (0)