2019-02-14 01:09:18 +00:00
|
|
|
use super::SlotClock;
|
2019-09-21 01:21:47 +00:00
|
|
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
2019-02-14 01:09:18 +00:00
|
|
|
use types::Slot;
|
|
|
|
|
|
|
|
pub use std::time::SystemTimeError;
|
|
|
|
|
|
|
|
/// Determines the present slot based upon the present system time.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SystemTimeSlotClock {
|
2019-03-26 04:44:28 +00:00
|
|
|
genesis_slot: Slot,
|
2019-09-21 01:21:47 +00:00
|
|
|
genesis_duration: Duration,
|
2019-08-29 02:46:18 +00:00
|
|
|
slot_duration: Duration,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 05:12:51 +00:00
|
|
|
impl SlotClock for SystemTimeSlotClock {
|
2019-09-21 01:21:47 +00:00
|
|
|
fn new(genesis_slot: Slot, genesis_duration: Duration, slot_duration: Duration) -> Self {
|
2019-08-29 02:46:18 +00:00
|
|
|
if slot_duration.as_millis() == 0 {
|
|
|
|
panic!("SystemTimeSlotClock cannot have a < 1ms slot duration.");
|
|
|
|
}
|
2019-05-27 05:12:51 +00:00
|
|
|
|
|
|
|
Self {
|
|
|
|
genesis_slot,
|
2019-09-21 01:21:47 +00:00
|
|
|
genesis_duration,
|
2019-08-29 02:46:18 +00:00
|
|
|
slot_duration,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 04:26:30 +00:00
|
|
|
fn now(&self) -> Option<Slot> {
|
2019-09-21 01:21:47 +00:00
|
|
|
let now = SystemTime::now().duration_since(UNIX_EPOCH).ok()?;
|
|
|
|
let genesis = self.genesis_duration;
|
|
|
|
|
2019-10-29 01:51:32 +00:00
|
|
|
if now >= genesis {
|
2019-09-21 01:21:47 +00:00
|
|
|
let since_genesis = now
|
|
|
|
.checked_sub(genesis)
|
2019-10-29 01:51:32 +00:00
|
|
|
.expect("Control flow ensures now is greater than or equal to genesis");
|
2019-09-21 01:21:47 +00:00
|
|
|
let slot =
|
|
|
|
Slot::from((since_genesis.as_millis() / self.slot_duration.as_millis()) as u64);
|
2019-08-29 02:46:18 +00:00
|
|
|
Some(slot + self.genesis_slot)
|
2019-09-21 01:21:47 +00:00
|
|
|
} else {
|
|
|
|
None
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 23:36:20 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
fn duration_to_next_slot(&self) -> Option<Duration> {
|
2019-09-21 01:21:47 +00:00
|
|
|
let now = SystemTime::now().duration_since(UNIX_EPOCH).ok()?;
|
|
|
|
let genesis = self.genesis_duration;
|
|
|
|
|
|
|
|
let slot_start = |slot: Slot| -> Duration {
|
|
|
|
let slot = slot.as_u64() as u32;
|
|
|
|
genesis + slot * self.slot_duration
|
|
|
|
};
|
|
|
|
|
2019-10-29 01:51:32 +00:00
|
|
|
if now >= genesis {
|
2019-09-21 01:21:47 +00:00
|
|
|
Some(
|
|
|
|
slot_start(self.now()? + 1)
|
|
|
|
.checked_sub(now)
|
|
|
|
.expect("The next slot cannot start before now"),
|
|
|
|
)
|
2019-08-29 02:46:18 +00:00
|
|
|
} else {
|
2019-09-21 01:21:47 +00:00
|
|
|
Some(
|
|
|
|
genesis
|
|
|
|
.checked_sub(now)
|
|
|
|
.expect("Control flow ensures genesis is greater than or equal to now"),
|
|
|
|
)
|
2019-08-29 02:46:18 +00:00
|
|
|
}
|
2019-03-26 23:36:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
fn slot_duration(&self) -> Duration {
|
|
|
|
self.slot_duration
|
|
|
|
}
|
2019-03-26 23:36:20 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: these tests are using actual system times and could fail if they are executed on a
|
|
|
|
* very slow machine.
|
|
|
|
*/
|
|
|
|
#[test]
|
|
|
|
fn test_slot_now() {
|
2019-03-26 04:44:28 +00:00
|
|
|
let genesis_slot = Slot::new(0);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-09-21 01:21:47 +00:00
|
|
|
let prior_genesis = |milliseconds_prior: u64| {
|
|
|
|
SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("should get system time")
|
|
|
|
- Duration::from_millis(milliseconds_prior)
|
|
|
|
};
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
let clock =
|
|
|
|
SystemTimeSlotClock::new(genesis_slot, prior_genesis(0), Duration::from_secs(1));
|
2019-08-29 04:26:30 +00:00
|
|
|
assert_eq!(clock.now(), Some(Slot::new(0)));
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
let clock =
|
2019-09-21 01:21:47 +00:00
|
|
|
SystemTimeSlotClock::new(genesis_slot, prior_genesis(5_000), Duration::from_secs(1));
|
2019-08-29 04:26:30 +00:00
|
|
|
assert_eq!(clock.now(), Some(Slot::new(5)));
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-09-21 01:21:47 +00:00
|
|
|
let clock =
|
|
|
|
SystemTimeSlotClock::new(genesis_slot, prior_genesis(500), Duration::from_secs(1));
|
2019-08-29 04:26:30 +00:00
|
|
|
assert_eq!(clock.now(), Some(Slot::new(0)));
|
2019-10-29 01:51:32 +00:00
|
|
|
assert!(clock.duration_to_next_slot().unwrap() <= Duration::from_millis(500));
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-09-21 01:21:47 +00:00
|
|
|
let clock =
|
|
|
|
SystemTimeSlotClock::new(genesis_slot, prior_genesis(1_500), Duration::from_secs(1));
|
2019-08-29 04:26:30 +00:00
|
|
|
assert_eq!(clock.now(), Some(Slot::new(1)));
|
2019-10-29 01:51:32 +00:00
|
|
|
assert!(clock.duration_to_next_slot().unwrap() <= Duration::from_millis(500));
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-08-29 02:46:18 +00:00
|
|
|
#[should_panic]
|
|
|
|
fn zero_seconds() {
|
2019-09-21 01:21:47 +00:00
|
|
|
SystemTimeSlotClock::new(Slot::new(0), Duration::from_secs(0), Duration::from_secs(0));
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-08-29 02:46:18 +00:00
|
|
|
#[should_panic]
|
|
|
|
fn zero_millis() {
|
2019-09-21 01:21:47 +00:00
|
|
|
SystemTimeSlotClock::new(
|
|
|
|
Slot::new(0),
|
|
|
|
Duration::from_secs(0),
|
|
|
|
Duration::from_millis(0),
|
|
|
|
);
|
2019-08-29 02:46:18 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn less_than_one_millis() {
|
2019-09-21 01:21:47 +00:00
|
|
|
SystemTimeSlotClock::new(
|
|
|
|
Slot::new(0),
|
|
|
|
Duration::from_secs(0),
|
|
|
|
Duration::from_nanos(999),
|
|
|
|
);
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|