mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-28 10:13:37 +00:00
Start heavy refactor of validator client
- Block production is working
This commit is contained in:
@@ -28,4 +28,7 @@ pub trait SlotClock: Send + Sync + Sized {
|
||||
|
||||
/// Returns the duration until the next slot.
|
||||
fn duration_to_next_slot(&self) -> Option<Duration>;
|
||||
|
||||
/// Returns the duration until the first slot of the next epoch.
|
||||
fn duration_to_next_epoch(&self, slots_per_epoch: u64) -> Option<Duration>;
|
||||
}
|
||||
|
||||
@@ -65,6 +65,35 @@ impl SlotClock for SystemTimeSlotClock {
|
||||
}
|
||||
}
|
||||
|
||||
fn duration_to_next_epoch(&self, slots_per_epoch: u64) -> Option<Duration> {
|
||||
let now = SystemTime::now().duration_since(UNIX_EPOCH).ok()?;
|
||||
let genesis = self.genesis_duration;
|
||||
|
||||
let slot_start = |slot: Slot| -> Duration {
|
||||
let slot = slot.as_u64() as u32;
|
||||
genesis + slot * self.slot_duration
|
||||
};
|
||||
|
||||
let slot = self
|
||||
.now()
|
||||
.map(|slot| slot.epoch(slots_per_epoch))
|
||||
.map(|epoch| (epoch + 1).start_slot(slots_per_epoch))?;
|
||||
|
||||
if now >= genesis {
|
||||
Some(
|
||||
slot_start(self.now()? + 1)
|
||||
.checked_sub(now)
|
||||
.expect("The next epoch cannot start before now"),
|
||||
)
|
||||
} else {
|
||||
Some(
|
||||
genesis
|
||||
.checked_sub(now)
|
||||
.expect("Control flow ensures genesis is greater than or equal to now"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn slot_duration(&self) -> Duration {
|
||||
self.slot_duration
|
||||
}
|
||||
|
||||
@@ -37,6 +37,11 @@ impl SlotClock for TestingSlotClock {
|
||||
Some(Duration::from_secs(1))
|
||||
}
|
||||
|
||||
/// Always returns a duration of `1 * slots_per_epoch` second.
|
||||
fn duration_to_next_epoch(&self, slots_per_epoch: u64) -> Option<Duration> {
|
||||
Some(Duration::from_secs(1 + slots_per_epoch))
|
||||
}
|
||||
|
||||
/// Always returns a slot duration of 0 seconds.
|
||||
fn slot_duration(&self) -> Duration {
|
||||
Duration::from_secs(0)
|
||||
|
||||
Reference in New Issue
Block a user