2019-02-14 01:09:18 +00:00
|
|
|
use super::SlotClock;
|
2019-08-29 02:46:18 +00:00
|
|
|
use std::time::{Duration, Instant};
|
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-08-29 02:46:18 +00:00
|
|
|
genesis: Instant,
|
|
|
|
slot_duration: Duration,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 05:12:51 +00:00
|
|
|
impl SlotClock for SystemTimeSlotClock {
|
2019-08-29 02:46:18 +00:00
|
|
|
fn new(genesis_slot: Slot, genesis: Instant, slot_duration: Duration) -> Self {
|
|
|
|
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-08-29 02:46:18 +00:00
|
|
|
genesis,
|
|
|
|
slot_duration,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
fn present_slot(&self) -> Option<Slot> {
|
|
|
|
let now = Instant::now();
|
2019-03-26 04:44:28 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
if now < self.genesis {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let slot = Slot::from(
|
|
|
|
(now.duration_since(self.genesis).as_millis() / self.slot_duration.as_millis())
|
|
|
|
as u64,
|
|
|
|
);
|
|
|
|
Some(slot + self.genesis_slot)
|
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> {
|
|
|
|
let now = Instant::now();
|
|
|
|
if now < self.genesis {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let duration_since_genesis = now - self.genesis;
|
|
|
|
let millis_since_genesis = duration_since_genesis.as_millis();
|
|
|
|
let millis_per_slot = self.slot_duration.as_millis();
|
2019-08-19 11:02:34 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
let current_slot = millis_since_genesis / millis_per_slot;
|
|
|
|
let next_slot = current_slot + 1;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
let next_slot =
|
|
|
|
self.genesis + Duration::from_millis((next_slot * millis_per_slot) as u64);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
Some(next_slot.duration_since(now))
|
|
|
|
}
|
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-08-29 02:46:18 +00:00
|
|
|
let prior_genesis =
|
|
|
|
|seconds_prior: u64| Instant::now() - Duration::from_secs(seconds_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));
|
|
|
|
assert_eq!(clock.present_slot(), Some(Slot::new(0)));
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
let clock =
|
|
|
|
SystemTimeSlotClock::new(genesis_slot, prior_genesis(5), Duration::from_secs(1));
|
|
|
|
assert_eq!(clock.present_slot(), Some(Slot::new(5)));
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
let clock = SystemTimeSlotClock::new(
|
2019-03-26 04:44:28 +00:00
|
|
|
genesis_slot,
|
2019-08-29 02:46:18 +00:00
|
|
|
Instant::now() - Duration::from_millis(500),
|
|
|
|
Duration::from_secs(1),
|
|
|
|
);
|
|
|
|
assert_eq!(clock.present_slot(), Some(Slot::new(0)));
|
|
|
|
assert!(clock.duration_to_next_slot().unwrap() < Duration::from_millis(500));
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
let clock = SystemTimeSlotClock::new(
|
2019-03-26 04:44:28 +00:00
|
|
|
genesis_slot,
|
2019-08-29 02:46:18 +00:00
|
|
|
Instant::now() - Duration::from_millis(1_500),
|
|
|
|
Duration::from_secs(1),
|
|
|
|
);
|
|
|
|
assert_eq!(clock.present_slot(), Some(Slot::new(1)));
|
|
|
|
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() {
|
|
|
|
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), 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() {
|
|
|
|
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_millis(0));
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-08-29 02:46:18 +00:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn less_than_one_millis() {
|
|
|
|
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_nanos(999));
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|