mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
Merged age-validator-client into luke's changes on validator_client, and fixed all the merge conflicts.
This commit is contained in:
@@ -3,10 +3,13 @@ mod testing_slot_clock;
|
||||
|
||||
pub use crate::system_time_slot_clock::{Error as SystemTimeSlotClockError, SystemTimeSlotClock};
|
||||
pub use crate::testing_slot_clock::{Error as TestingSlotClockError, TestingSlotClock};
|
||||
use std::time::Duration;
|
||||
pub use types::Slot;
|
||||
|
||||
pub trait SlotClock: Send + Sync {
|
||||
type Error;
|
||||
|
||||
fn present_slot(&self) -> Result<Option<Slot>, Self::Error>;
|
||||
|
||||
fn duration_to_next_slot(&self) -> Result<Option<Duration>, Self::Error>;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ pub enum Error {
|
||||
/// 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,
|
||||
}
|
||||
@@ -22,6 +23,7 @@ impl SystemTimeSlotClock {
|
||||
///
|
||||
/// Returns an Error if `slot_duration_seconds == 0`.
|
||||
pub fn new(
|
||||
genesis_slot: Slot,
|
||||
genesis_seconds: u64,
|
||||
slot_duration_seconds: u64,
|
||||
) -> Result<SystemTimeSlotClock, Error> {
|
||||
@@ -29,6 +31,7 @@ impl SystemTimeSlotClock {
|
||||
Err(Error::SlotDurationIsZero)
|
||||
} else {
|
||||
Ok(Self {
|
||||
genesis_slot,
|
||||
genesis_seconds,
|
||||
slot_duration_seconds,
|
||||
})
|
||||
@@ -44,11 +47,17 @@ impl SlotClock for SystemTimeSlotClock {
|
||||
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)),
|
||||
Some(d) => Ok(slot_from_duration(self.slot_duration_seconds, d)
|
||||
.and_then(|s| Some(s + self.genesis_slot))),
|
||||
}
|
||||
}
|
||||
|
||||
fn duration_to_next_slot(&self) -> Result<Option<Duration>, Error> {
|
||||
duration_to_next_slot(self.genesis_seconds, self.slot_duration_seconds)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SystemTimeError> for Error {
|
||||
@@ -62,6 +71,30 @@ fn slot_from_duration(slot_duration_seconds: u64, duration: Duration) -> Option<
|
||||
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 {
|
||||
@@ -74,6 +107,7 @@ 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();
|
||||
@@ -81,18 +115,21 @@ mod tests {
|
||||
let genesis = since_epoch.as_secs() - slot_time * 89;
|
||||
|
||||
let clock = SystemTimeSlotClock {
|
||||
genesis_slot,
|
||||
genesis_seconds: genesis,
|
||||
slot_duration_seconds: slot_time,
|
||||
};
|
||||
assert_eq!(clock.present_slot().unwrap(), Some(Slot::new(89)));
|
||||
|
||||
let clock = SystemTimeSlotClock {
|
||||
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,
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use super::SlotClock;
|
||||
use std::sync::RwLock;
|
||||
use std::time::Duration;
|
||||
use types::Slot;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -32,6 +33,11 @@ impl SlotClock for TestingSlotClock {
|
||||
let slot = *self.slot.read().expect("TestingSlotClock poisoned.");
|
||||
Ok(Some(Slot::new(slot)))
|
||||
}
|
||||
|
||||
/// Always returns a duration of 1 second.
|
||||
fn duration_to_next_slot(&self) -> Result<Option<Duration>, Error> {
|
||||
Ok(Some(Duration::from_secs(1)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user