lighthouse/eth2/utils/slot_clock/src/system_time_slot_clock.rs

124 lines
3.5 KiB
Rust
Raw Normal View History

use super::SlotClock;
2019-08-29 02:46:18 +00:00
use std::time::{Duration, Instant};
use types::Slot;
pub use std::time::SystemTimeError;
/// Determines the present slot based upon the present system time.
#[derive(Clone)]
pub struct SystemTimeSlotClock {
2019-03-26 04:44:28 +00:00
genesis_slot: Slot,
2019-08-29 02:46:18 +00:00
genesis: Instant,
slot_duration: Duration,
}
impl SlotClock for SystemTimeSlotClock {
2019-08-29 02:46:18 +00:00
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.");
}
Self {
genesis_slot,
2019-08-29 02:46:18 +00:00
genesis,
slot_duration,
}
}
2019-08-29 04:26:30 +00:00
fn now(&self) -> Option<Slot> {
2019-08-29 02:46:18 +00:00
let now = Instant::now();
2019-03-26 04:44:28 +00:00
2019-08-29 02:46:18 +00:00
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)
}
}
2019-03-26 23:36:20 +00:00
2019-08-29 02:46:18 +00:00
fn duration_to_next_slot(&self) -> Option<Duration> {
let now = Instant::now();
if now < self.genesis {
Some(self.genesis - now)
2019-08-29 02:46:18 +00:00
} 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();
Fix block processing blowup, upgrade metrics (#500) * Renamed fork_choice::process_attestation_from_block * Processing attestation in fork choice * Retrieving state from store and checking signature * Looser check on beacon state validity. * Cleaned up get_attestation_state * Expanded fork choice api to provide latest validator message. * Checking if the an attestation contains a latest message * Correct process_attestation error handling. * Copy paste error in comment fixed. * Tidy ancestor iterators * Getting attestation slot via helper method * Refactored attestation creation in test utils * Revert "Refactored attestation creation in test utils" This reverts commit 4d277fe4239a7194758b18fb5c00dfe0b8231306. * Integration tests for free attestation processing * Implicit conflicts resolved. * formatting * Do first pass on Grants code * Add another attestation processing test * Tidy attestation processing * Remove old code fragment * Add non-compiling half finished changes * Simplify, fix bugs, add tests for chain iters * Remove attestation processing from op pool * Fix bug with fork choice, tidy * Fix overly restrictive check in fork choice. * Ensure committee cache is build during attn proc * Ignore unknown blocks at fork choice * Various minor fixes * Make fork choice write lock in to read lock * Remove unused method * Tidy comments * Fix attestation prod. target roots change * Fix compile error in store iters * Reject any attestation prior to finalization * Begin metrics refactor * Move beacon_chain to new metrics structure. * Make metrics not panic if already defined * Use global prometheus gather at rest api * Unify common metric fns into a crate * Add heavy metering to block processing * Remove hypen from prometheus metric name * Add more beacon chain metrics * Add beacon chain persistence metric * Prune op pool on finalization * Add extra prom beacon chain metrics * Prefix BeaconChain metrics with "beacon_" * Add more store metrics * Add basic metrics to libp2p * Add metrics to HTTP server * Remove old `http_server` crate * Update metrics names to be more like standard * Fix broken beacon chain metrics, add slot clock metrics * Add lighthouse_metrics gather fn * Remove http args * Fix wrong state given to op pool prune * Make prom metric names more consistent * Add more metrics, tidy existing metrics * Fix store block read metrics * Tidy attestation metrics * Fix minor PR comments * Allow travis failures on beta (see desc) There's a non-backward compatible change in `cargo fmt`. Stable and beta do not agree. * Tidy `lighthouse_metrics` docs * Fix typo
2019-08-19 11:02:34 +00:00
2019-08-29 02:46:18 +00:00
let current_slot = millis_since_genesis / millis_per_slot;
let next_slot = current_slot + 1;
2019-08-29 02:46:18 +00:00
let next_slot =
self.genesis + Duration::from_millis((next_slot * millis_per_slot) as u64);
2019-08-29 02:46:18 +00:00
Some(next_slot.duration_since(now))
}
2019-03-26 23:36:20 +00:00
}
2019-08-29 02:46:18 +00:00
fn slot_duration(&self) -> Duration {
self.slot_duration
}
2019-03-26 23:36:20 +00:00
}
#[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() {
2019-03-26 04:44:28 +00:00
let genesis_slot = Slot::new(0);
2019-08-29 02:46:18 +00:00
let prior_genesis =
|seconds_prior: u64| Instant::now() - Duration::from_secs(seconds_prior);
2019-08-29 02:46:18 +00:00
let clock =
SystemTimeSlotClock::new(genesis_slot, prior_genesis(0), Duration::from_secs(1));
2019-08-29 04:26:30 +00:00
assert_eq!(clock.now(), Some(Slot::new(0)));
2019-08-29 02:46:18 +00:00
let clock =
SystemTimeSlotClock::new(genesis_slot, prior_genesis(5), Duration::from_secs(1));
2019-08-29 04:26:30 +00:00
assert_eq!(clock.now(), Some(Slot::new(5)));
2019-08-29 02:46:18 +00:00
let clock = SystemTimeSlotClock::new(
2019-03-26 04:44:28 +00:00
genesis_slot,
2019-08-29 02:46:18 +00:00
Instant::now() - Duration::from_millis(500),
Duration::from_secs(1),
);
2019-08-29 04:26:30 +00:00
assert_eq!(clock.now(), Some(Slot::new(0)));
2019-08-29 02:46:18 +00:00
assert!(clock.duration_to_next_slot().unwrap() < Duration::from_millis(500));
2019-08-29 02:46:18 +00:00
let clock = SystemTimeSlotClock::new(
2019-03-26 04:44:28 +00:00
genesis_slot,
2019-08-29 02:46:18 +00:00
Instant::now() - Duration::from_millis(1_500),
Duration::from_secs(1),
);
2019-08-29 04:26:30 +00:00
assert_eq!(clock.now(), Some(Slot::new(1)));
2019-08-29 02:46:18 +00:00
assert!(clock.duration_to_next_slot().unwrap() < Duration::from_millis(500));
}
#[test]
2019-08-29 02:46:18 +00:00
#[should_panic]
fn zero_seconds() {
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_secs(0));
}
#[test]
2019-08-29 02:46:18 +00:00
#[should_panic]
fn zero_millis() {
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_millis(0));
}
2019-08-29 02:46:18 +00:00
#[test]
#[should_panic]
fn less_than_one_millis() {
SystemTimeSlotClock::new(Slot::new(0), Instant::now(), Duration::from_nanos(999));
}
}