mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-22 22:34:45 +00:00
Strip out old code
All of these files have been moved to either: - https://github.com/sigp/lighthouse-beacon - https://github.com/sigp/lighthouse-validator - https://github.com/sigp/lighthouse-common For rationale, see: https://github.com/sigp/lighthouse/issues/197
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
mod system_time_slot_clock;
|
||||
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};
|
||||
pub use types::Slot;
|
||||
|
||||
pub trait SlotClock: Send + Sync {
|
||||
type Error;
|
||||
|
||||
fn present_slot(&self) -> Result<Option<Slot>, Self::Error>;
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
use super::SlotClock;
|
||||
use std::time::{Duration, SystemTime};
|
||||
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_seconds: u64,
|
||||
slot_duration_seconds: u64,
|
||||
}
|
||||
|
||||
impl SystemTimeSlotClock {
|
||||
/// Create a new `SystemTimeSlotClock`.
|
||||
///
|
||||
/// Returns an Error if `slot_duration_seconds == 0`.
|
||||
pub fn new(
|
||||
genesis_seconds: u64,
|
||||
slot_duration_seconds: u64,
|
||||
) -> Result<SystemTimeSlotClock, Error> {
|
||||
if slot_duration_seconds == 0 {
|
||||
Err(Error::SlotDurationIsZero)
|
||||
} else {
|
||||
Ok(Self {
|
||||
genesis_seconds,
|
||||
slot_duration_seconds,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SlotClock for SystemTimeSlotClock {
|
||||
type Error = Error;
|
||||
|
||||
fn present_slot(&self) -> Result<Option<Slot>, Error> {
|
||||
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)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)?,
|
||||
))
|
||||
}
|
||||
|
||||
#[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() {
|
||||
let slot_time = 100;
|
||||
|
||||
let now = SystemTime::now();
|
||||
let since_epoch = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
|
||||
|
||||
let genesis = since_epoch.as_secs() - slot_time * 89;
|
||||
|
||||
let clock = SystemTimeSlotClock {
|
||||
genesis_seconds: genesis,
|
||||
slot_duration_seconds: slot_time,
|
||||
};
|
||||
assert_eq!(clock.present_slot().unwrap(), Some(Slot::new(89)));
|
||||
|
||||
let clock = SystemTimeSlotClock {
|
||||
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_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]
|
||||
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))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slot_from_duration_slot_time_zero() {
|
||||
let slot_time = 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
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
use super::SlotClock;
|
||||
use std::sync::RwLock;
|
||||
use types::Slot;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {}
|
||||
|
||||
/// Determines the present slot based upon the present system time.
|
||||
pub struct TestingSlotClock {
|
||||
slot: RwLock<u64>,
|
||||
}
|
||||
|
||||
impl TestingSlotClock {
|
||||
/// Create a new `TestingSlotClock`.
|
||||
///
|
||||
/// Returns an Error if `slot_duration_seconds == 0`.
|
||||
pub fn new(slot: u64) -> TestingSlotClock {
|
||||
TestingSlotClock {
|
||||
slot: RwLock::new(slot),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_slot(&self, slot: u64) {
|
||||
*self.slot.write().expect("TestingSlotClock poisoned.") = slot;
|
||||
}
|
||||
}
|
||||
|
||||
impl SlotClock for TestingSlotClock {
|
||||
type Error = Error;
|
||||
|
||||
fn present_slot(&self) -> Result<Option<Slot>, Error> {
|
||||
let slot = *self.slot.read().expect("TestingSlotClock poisoned.");
|
||||
Ok(Some(Slot::new(slot)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_slot_now() {
|
||||
let clock = TestingSlotClock::new(10);
|
||||
assert_eq!(clock.present_slot(), Ok(Some(Slot::new(10))));
|
||||
clock.set_slot(123);
|
||||
assert_eq!(clock.present_slot(), Ok(Some(Slot::new(123))));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user