Refactor slot clock.

This commit is contained in:
Paul Hauner 2019-08-29 12:46:18 +10:00
parent 6bb3a65189
commit 7bfe02be1c
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
4 changed files with 98 additions and 156 deletions

View File

@ -5,24 +5,19 @@ mod metrics;
mod system_time_slot_clock; mod system_time_slot_clock;
mod testing_slot_clock; mod testing_slot_clock;
use std::time::Duration; use std::time::{Duration, Instant};
pub use crate::system_time_slot_clock::{Error as SystemTimeSlotClockError, SystemTimeSlotClock}; pub use crate::system_time_slot_clock::SystemTimeSlotClock;
pub use crate::testing_slot_clock::{Error as TestingSlotClockError, TestingSlotClock}; pub use crate::testing_slot_clock::TestingSlotClock;
pub use metrics::scrape_for_metrics; pub use metrics::scrape_for_metrics;
pub use types::Slot; pub use types::Slot;
pub trait SlotClock: Send + Sync + Sized { pub trait SlotClock: Send + Sync + Sized {
type Error; fn new(genesis_slot: Slot, genesis: Instant, slot_duration: Duration) -> Self;
/// Create a new `SlotClock`. fn present_slot(&self) -> Option<Slot>;
///
/// Returns an Error if `slot_duration_seconds == 0`.
fn new(genesis_slot: Slot, genesis_seconds: u64, slot_duration_seconds: u64) -> Self;
fn present_slot(&self) -> Result<Option<Slot>, Self::Error>; fn duration_to_next_slot(&self) -> Option<Duration>;
fn duration_to_next_slot(&self) -> Result<Option<Duration>, Self::Error>; fn slot_duration(&self) -> Duration;
fn slot_duration_millis(&self) -> u64;
} }

View File

@ -18,7 +18,7 @@ lazy_static! {
/// Update the global metrics `DEFAULT_REGISTRY` with info from the slot clock. /// Update the global metrics `DEFAULT_REGISTRY` with info from the slot clock.
pub fn scrape_for_metrics<T: EthSpec, U: SlotClock>(clock: &U) { pub fn scrape_for_metrics<T: EthSpec, U: SlotClock>(clock: &U) {
let present_slot = match clock.present_slot() { let present_slot = match clock.present_slot() {
Ok(Some(slot)) => slot, Some(slot) => slot,
_ => Slot::new(0), _ => Slot::new(0),
}; };
@ -28,5 +28,8 @@ pub fn scrape_for_metrics<T: EthSpec, U: SlotClock>(clock: &U) {
present_slot.epoch(T::slots_per_epoch()).as_u64() as i64, present_slot.epoch(T::slots_per_epoch()).as_u64() as i64,
); );
set_gauge(&SLOTS_PER_EPOCH, T::slots_per_epoch() as i64); set_gauge(&SLOTS_PER_EPOCH, T::slots_per_epoch() as i64);
set_gauge(&MILLISECONDS_PER_SLOT, clock.slot_duration_millis() as i64); set_gauge(
&MILLISECONDS_PER_SLOT,
clock.slot_duration().as_millis() as i64,
);
} }

View File

@ -1,99 +1,68 @@
use super::SlotClock; use super::SlotClock;
use std::time::{Duration, SystemTime}; use std::time::{Duration, Instant};
use types::Slot; use types::Slot;
pub use std::time::SystemTimeError; pub use std::time::SystemTimeError;
#[derive(Debug, PartialEq)]
pub enum Error {
SlotDurationIsZero,
SystemTimeError(String),
}
/// Determines the present slot based upon the present system time. /// Determines the present slot based upon the present system time.
#[derive(Clone)] #[derive(Clone)]
pub struct SystemTimeSlotClock { pub struct SystemTimeSlotClock {
genesis_slot: Slot, genesis_slot: Slot,
genesis_seconds: u64, genesis: Instant,
slot_duration_seconds: u64, slot_duration: Duration,
} }
impl SlotClock for SystemTimeSlotClock { impl SlotClock for SystemTimeSlotClock {
type Error = Error; 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.");
}
/// Create a new `SystemTimeSlotClock`.
///
/// Returns an Error if `slot_duration_seconds == 0`.
fn new(genesis_slot: Slot, genesis_seconds: u64, slot_duration_seconds: u64) -> Self {
Self { Self {
genesis_slot, genesis_slot,
genesis_seconds, genesis,
slot_duration_seconds, slot_duration,
} }
} }
fn present_slot(&self) -> Result<Option<Slot>, Error> { fn present_slot(&self) -> Option<Slot> {
if self.slot_duration_seconds == 0 { let now = Instant::now();
return Err(Error::SlotDurationIsZero);
}
let syslot_time = SystemTime::now(); if now < self.genesis {
let duration_since_epoch = syslot_time.duration_since(SystemTime::UNIX_EPOCH)?; None
let duration_since_genesis = } else {
duration_since_epoch.checked_sub(Duration::from_secs(self.genesis_seconds)); let slot = Slot::from(
(now.duration_since(self.genesis).as_millis() / self.slot_duration.as_millis())
match duration_since_genesis { as u64,
None => Ok(None), );
Some(d) => Ok(slot_from_duration(self.slot_duration_seconds, d) Some(slot + self.genesis_slot)
.and_then(|s| Some(s + self.genesis_slot))),
} }
} }
fn duration_to_next_slot(&self) -> Result<Option<Duration>, Error> { fn duration_to_next_slot(&self) -> Option<Duration> {
duration_to_next_slot(self.genesis_seconds, self.slot_duration_seconds) 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();
let current_slot = millis_since_genesis / millis_per_slot;
let next_slot = current_slot + 1;
let next_slot =
self.genesis + Duration::from_millis((next_slot * millis_per_slot) as u64);
Some(next_slot.duration_since(now))
}
} }
fn slot_duration_millis(&self) -> u64 { fn slot_duration(&self) -> Duration {
self.slot_duration_seconds * 1000 self.slot_duration
} }
} }
impl From<SystemTimeError> for Error {
fn from(e: SystemTimeError) -> Error {
Error::SystemTimeError(format!("{:?}", e))
}
}
fn slot_from_duration(slot_duration_seconds: u64, duration: Duration) -> Option<Slot> {
Some(Slot::new(
duration.as_secs().checked_div(slot_duration_seconds)?,
))
}
// calculate the duration to the next slot
fn duration_to_next_slot(
genesis_time: u64,
seconds_per_slot: u64,
) -> Result<Option<Duration>, Error> {
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let genesis_time = Duration::from_secs(genesis_time);
if now < genesis_time {
return Ok(None);
}
let since_genesis = now - genesis_time;
let elapsed_slots = since_genesis.as_secs() / seconds_per_slot;
let next_slot_start_seconds = (elapsed_slots + 1)
.checked_mul(seconds_per_slot)
.expect("Next slot time should not overflow u64");
let time_to_next_slot = Duration::from_secs(next_slot_start_seconds) - since_genesis;
Ok(Some(time_to_next_slot))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -104,71 +73,51 @@ mod tests {
*/ */
#[test] #[test]
fn test_slot_now() { fn test_slot_now() {
let slot_time = 100;
let genesis_slot = Slot::new(0); let genesis_slot = Slot::new(0);
let now = SystemTime::now(); let prior_genesis =
let since_epoch = now.duration_since(SystemTime::UNIX_EPOCH).unwrap(); |seconds_prior: u64| Instant::now() - Duration::from_secs(seconds_prior);
let genesis = since_epoch.as_secs() - slot_time * 89; let clock =
SystemTimeSlotClock::new(genesis_slot, prior_genesis(0), Duration::from_secs(1));
assert_eq!(clock.present_slot(), Some(Slot::new(0)));
let clock = SystemTimeSlotClock { let clock =
SystemTimeSlotClock::new(genesis_slot, prior_genesis(5), Duration::from_secs(1));
assert_eq!(clock.present_slot(), Some(Slot::new(5)));
let clock = SystemTimeSlotClock::new(
genesis_slot, genesis_slot,
genesis_seconds: genesis, Instant::now() - Duration::from_millis(500),
slot_duration_seconds: slot_time, Duration::from_secs(1),
}; );
assert_eq!(clock.present_slot().unwrap(), Some(Slot::new(89))); assert_eq!(clock.present_slot(), Some(Slot::new(0)));
assert!(clock.duration_to_next_slot().unwrap() < Duration::from_millis(500));
let clock = SystemTimeSlotClock { let clock = SystemTimeSlotClock::new(
genesis_slot, genesis_slot,
genesis_seconds: since_epoch.as_secs(), Instant::now() - Duration::from_millis(1_500),
slot_duration_seconds: slot_time, Duration::from_secs(1),
}; );
assert_eq!(clock.present_slot().unwrap(), Some(Slot::new(0))); assert_eq!(clock.present_slot(), Some(Slot::new(1)));
assert!(clock.duration_to_next_slot().unwrap() < Duration::from_millis(500));
let clock = SystemTimeSlotClock {
genesis_slot,
genesis_seconds: since_epoch.as_secs() - slot_time * 42 - 5,
slot_duration_seconds: slot_time,
};
assert_eq!(clock.present_slot().unwrap(), Some(Slot::new(42)));
} }
#[test] #[test]
fn test_slot_from_duration() { #[should_panic]
let slot_time = 100; fn zero_seconds() {
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_secs(0));
assert_eq!(
slot_from_duration(slot_time, Duration::from_secs(0)),
Some(Slot::new(0))
);
assert_eq!(
slot_from_duration(slot_time, Duration::from_secs(10)),
Some(Slot::new(0))
);
assert_eq!(
slot_from_duration(slot_time, Duration::from_secs(100)),
Some(Slot::new(1))
);
assert_eq!(
slot_from_duration(slot_time, Duration::from_secs(101)),
Some(Slot::new(1))
);
assert_eq!(
slot_from_duration(slot_time, Duration::from_secs(1000)),
Some(Slot::new(10))
);
} }
#[test] #[test]
fn test_slot_from_duration_slot_time_zero() { #[should_panic]
let slot_time = 0; fn zero_millis() {
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_millis(0));
}
assert_eq!(slot_from_duration(slot_time, Duration::from_secs(0)), None); #[test]
assert_eq!(slot_from_duration(slot_time, Duration::from_secs(10)), None); #[should_panic]
assert_eq!( fn less_than_one_millis() {
slot_from_duration(slot_time, Duration::from_secs(1000)), SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_nanos(999));
None
);
} }
} }

View File

@ -1,12 +1,11 @@
use super::SlotClock; use super::SlotClock;
use std::sync::RwLock; use std::sync::RwLock;
use std::time::Duration; use std::time::{Duration, Instant};
use types::Slot; use types::Slot;
#[derive(Debug, PartialEq)] /// A slot clock where the slot is manually set instead of being determined by the system time.
pub enum Error {} ///
/// Useful for testing scenarios.
/// Determines the present slot based upon the present system time.
pub struct TestingSlotClock { pub struct TestingSlotClock {
slot: RwLock<Slot>, slot: RwLock<Slot>,
} }
@ -17,32 +16,30 @@ impl TestingSlotClock {
} }
pub fn advance_slot(&self) { pub fn advance_slot(&self) {
self.set_slot(self.present_slot().unwrap().unwrap().as_u64() + 1) self.set_slot(self.present_slot().unwrap().as_u64() + 1)
} }
} }
impl SlotClock for TestingSlotClock { impl SlotClock for TestingSlotClock {
type Error = Error; fn new(genesis_slot: Slot, _genesis: Instant, _slot_duration: Duration) -> Self {
/// Create a new `TestingSlotClock` at `genesis_slot`.
fn new(genesis_slot: Slot, _genesis_seconds: u64, _slot_duration_seconds: u64) -> Self {
TestingSlotClock { TestingSlotClock {
slot: RwLock::new(genesis_slot), slot: RwLock::new(genesis_slot),
} }
} }
fn present_slot(&self) -> Result<Option<Slot>, Error> { fn present_slot(&self) -> Option<Slot> {
let slot = *self.slot.read().expect("TestingSlotClock poisoned."); let slot = *self.slot.read().expect("TestingSlotClock poisoned.");
Ok(Some(slot)) Some(slot)
} }
/// Always returns a duration of 1 second. /// Always returns a duration of 1 second.
fn duration_to_next_slot(&self) -> Result<Option<Duration>, Error> { fn duration_to_next_slot(&self) -> Option<Duration> {
Ok(Some(Duration::from_secs(1))) Some(Duration::from_secs(1))
} }
fn slot_duration_millis(&self) -> u64 { /// Always returns a slot duration of 0 seconds.
0 fn slot_duration(&self) -> Duration {
Duration::from_secs(0)
} }
} }
@ -52,11 +49,9 @@ mod tests {
#[test] #[test]
fn test_slot_now() { fn test_slot_now() {
let null = 0; let clock = TestingSlotClock::new(Slot::new(10), Instant::now(), Duration::from_secs(0));
assert_eq!(clock.present_slot(), Some(Slot::new(10)));
let clock = TestingSlotClock::new(Slot::new(10), null, null);
assert_eq!(clock.present_slot(), Ok(Some(Slot::new(10))));
clock.set_slot(123); clock.set_slot(123);
assert_eq!(clock.present_slot(), Ok(Some(Slot::new(123)))); assert_eq!(clock.present_slot(), Some(Slot::new(123)));
} }
} }