mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-26 01:03:40 +00:00
Refactor slot clock.
This commit is contained in:
@@ -1,99 +1,68 @@
|
||||
use super::SlotClock;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::time::{Duration, Instant};
|
||||
use types::Slot;
|
||||
|
||||
pub use std::time::SystemTimeError;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
SlotDurationIsZero,
|
||||
SystemTimeError(String),
|
||||
}
|
||||
|
||||
/// Determines the present slot based upon the present system time.
|
||||
#[derive(Clone)]
|
||||
pub struct SystemTimeSlotClock {
|
||||
genesis_slot: Slot,
|
||||
genesis_seconds: u64,
|
||||
slot_duration_seconds: u64,
|
||||
genesis: Instant,
|
||||
slot_duration: Duration,
|
||||
}
|
||||
|
||||
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 {
|
||||
genesis_slot,
|
||||
genesis_seconds,
|
||||
slot_duration_seconds,
|
||||
genesis,
|
||||
slot_duration,
|
||||
}
|
||||
}
|
||||
|
||||
fn present_slot(&self) -> Result<Option<Slot>, Error> {
|
||||
if self.slot_duration_seconds == 0 {
|
||||
return Err(Error::SlotDurationIsZero);
|
||||
}
|
||||
fn present_slot(&self) -> Option<Slot> {
|
||||
let now = Instant::now();
|
||||
|
||||
let syslot_time = SystemTime::now();
|
||||
let duration_since_epoch = syslot_time.duration_since(SystemTime::UNIX_EPOCH)?;
|
||||
let duration_since_genesis =
|
||||
duration_since_epoch.checked_sub(Duration::from_secs(self.genesis_seconds));
|
||||
|
||||
match duration_since_genesis {
|
||||
None => Ok(None),
|
||||
Some(d) => Ok(slot_from_duration(self.slot_duration_seconds, d)
|
||||
.and_then(|s| Some(s + self.genesis_slot))),
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
fn duration_to_next_slot(&self) -> Result<Option<Duration>, Error> {
|
||||
duration_to_next_slot(self.genesis_seconds, self.slot_duration_seconds)
|
||||
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();
|
||||
|
||||
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 {
|
||||
self.slot_duration_seconds * 1000
|
||||
fn slot_duration(&self) -> Duration {
|
||||
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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -104,71 +73,51 @@ mod tests {
|
||||
*/
|
||||
#[test]
|
||||
fn test_slot_now() {
|
||||
let slot_time = 100;
|
||||
let genesis_slot = Slot::new(0);
|
||||
|
||||
let now = SystemTime::now();
|
||||
let since_epoch = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
|
||||
let prior_genesis =
|
||||
|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_seconds: genesis,
|
||||
slot_duration_seconds: slot_time,
|
||||
};
|
||||
assert_eq!(clock.present_slot().unwrap(), Some(Slot::new(89)));
|
||||
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));
|
||||
|
||||
let clock = SystemTimeSlotClock {
|
||||
let clock = SystemTimeSlotClock::new(
|
||||
genesis_slot,
|
||||
genesis_seconds: since_epoch.as_secs(),
|
||||
slot_duration_seconds: slot_time,
|
||||
};
|
||||
assert_eq!(clock.present_slot().unwrap(), Some(Slot::new(0)));
|
||||
|
||||
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)));
|
||||
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));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slot_from_duration() {
|
||||
let slot_time = 100;
|
||||
|
||||
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))
|
||||
);
|
||||
#[should_panic]
|
||||
fn zero_seconds() {
|
||||
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_secs(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slot_from_duration_slot_time_zero() {
|
||||
let slot_time = 0;
|
||||
#[should_panic]
|
||||
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);
|
||||
assert_eq!(slot_from_duration(slot_time, Duration::from_secs(10)), None);
|
||||
assert_eq!(
|
||||
slot_from_duration(slot_time, Duration::from_secs(1000)),
|
||||
None
|
||||
);
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn less_than_one_millis() {
|
||||
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_nanos(999));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user