Migrate codebase across to new SlotClock API

This commit is contained in:
Paul Hauner
2019-08-29 13:25:55 +10:00
parent 7bfe02be1c
commit bcd53a8b10
8 changed files with 78 additions and 62 deletions

View File

@@ -1,16 +1,9 @@
use slot_clock;
use error_chain::error_chain;
error_chain! {
links { }
errors {
SlotClockError(e: slot_clock::SystemTimeSlotClockError) {
description("Error reading system time"),
display("SlotClockError: '{:?}'", e)
}
SystemTimeError(t: String ) {
description("Error reading system time"),
display("SystemTimeError: '{}'", t)

View File

@@ -13,7 +13,6 @@ use crate::block_producer::{BeaconBlockGrpcClient, BlockProducer};
use crate::config::Config as ValidatorConfig;
use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap};
use crate::error as error_chain;
use crate::error::ErrorKind;
use crate::signer::Signer;
use bls::Keypair;
use eth2_config::Eth2Config;
@@ -159,17 +158,19 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static, E: EthSpec> Service<B,
};
// build the validator slot clock
let slot_clock = SystemTimeSlotClock::new(
let slot_clock = SystemTimeSlotClock::from_eth2_genesis(
genesis_slot,
genesis_time,
eth2_config.spec.seconds_per_slot,
);
Duration::from_secs(eth2_config.spec.seconds_per_slot),
)
.ok_or_else::<error_chain::Error, _>(|| {
"Unable to start slot clock. Genesis may not have occurred yet. Exiting.".into()
})?;
let current_slot = slot_clock
.present_slot()
.map_err(ErrorKind::SlotClockError)?
.ok_or_else::<error_chain::Error, _>(|| {
"Genesis is not in the past. Exiting.".into()
"Genesis has not yet occurred. Exiting.".into()
})?;
/* Generate the duties manager */
@@ -244,7 +245,6 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static, E: EthSpec> Service<B,
let duration_to_next_slot = service
.slot_clock
.duration_to_next_slot()
.map_err(|e| format!("System clock error: {:?}", e))?
.ok_or_else::<error_chain::Error, _>(|| {
"Genesis is not in the past. Exiting.".into()
})?;
@@ -291,15 +291,12 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static, E: EthSpec> Service<B,
/// Updates the known current slot and epoch.
fn update_current_slot(&mut self) -> error_chain::Result<()> {
let current_slot = match self.slot_clock.present_slot() {
Err(e) => {
error!(self.log, "SystemTimeError {:?}", e);
return Err("Could not read system time".into());
}
Ok(slot) => slot.ok_or_else::<error_chain::Error, _>(|| {
let current_slot = self
.slot_clock
.present_slot()
.ok_or_else::<error_chain::Error, _>(|| {
"Genesis is not in the past. Exiting.".into()
})?,
};
})?;
let current_epoch = current_slot.epoch(self.slots_per_epoch);