From a8a3f1c31881b65e47a7f6c02e5c6135c2e95514 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 18:03:23 +1100 Subject: [PATCH] Removes duty manager service in favour of tokio timer --- validator_client/src/duties/mod.rs | 26 ++++++----------- validator_client/src/duties/service.rs | 40 -------------------------- validator_client/src/service.rs | 20 +++++++------ 3 files changed, 19 insertions(+), 67 deletions(-) delete mode 100644 validator_client/src/duties/service.rs diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index c2b95b1c5..f6460afd2 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -1,21 +1,19 @@ mod epoch_duties; mod grpc; -mod service; #[cfg(test)] mod test_node; mod traits; pub use self::epoch_duties::EpochDutiesMap; use self::epoch_duties::{EpochDuties, EpochDutiesMapError}; -pub use self::service::DutiesManagerService; use self::traits::{BeaconNode, BeaconNodeError}; use bls::PublicKey; use slot_clock::SlotClock; use std::sync::Arc; -use types::{ChainSpec, Epoch}; +use types::{ChainSpec, Epoch, Slot}; #[derive(Debug, PartialEq, Clone, Copy)] -pub enum PollOutcome { +pub enum UpdateOutcome { /// The `EpochDuties` were not updated during this poll. NoChange(Epoch), /// The `EpochDuties` for the `epoch` were previously unknown, but obtained in the poll. @@ -50,19 +48,11 @@ pub struct DutiesManager { } impl DutiesManager { - /// Poll the Beacon Node for `EpochDuties`. + /// Check the Beacon Node for `EpochDuties`. /// /// The present `epoch` will be learned from the supplied `SlotClock`. In production this will /// be a wall-clock (e.g., system time, remote server time, etc.). - //TODO: Remove the poll and trust the tokio system-clock timer. Leave for now to ensure the - //timer is accurate. - pub fn poll(&self) -> Result { - let slot = self - .slot_clock - .present_slot() - .map_err(|_| Error::SlotClockError)? - .ok_or(Error::SlotUnknowable)?; - + pub fn update(&self, slot: Slot) -> Result { let epoch = slot.epoch(self.spec.slots_per_epoch); if let Some(duties) = self @@ -72,17 +62,17 @@ impl DutiesManager { // If these duties were known, check to see if they're updates or identical. let result = if let Some(known_duties) = self.duties_map.get(epoch)? { if known_duties == duties { - Ok(PollOutcome::NoChange(epoch)) + Ok(UpdateOutcome::NoChange(epoch)) } else { - Ok(PollOutcome::DutiesChanged(epoch, duties)) + Ok(UpdateOutcome::DutiesChanged(epoch, duties)) } } else { - Ok(PollOutcome::NewDuties(epoch, duties)) + Ok(UpdateOutcome::NewDuties(epoch, duties)) }; self.duties_map.insert(epoch, duties)?; result } else { - Ok(PollOutcome::UnknownValidatorOrEpoch(epoch)) + Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) } } } diff --git a/validator_client/src/duties/service.rs b/validator_client/src/duties/service.rs deleted file mode 100644 index bdb6faefa..000000000 --- a/validator_client/src/duties/service.rs +++ /dev/null @@ -1,40 +0,0 @@ -use super::traits::BeaconNode; -use super::{DutiesManager, PollOutcome}; -use slog::{debug, error, info, Logger}; -use slot_clock::SlotClock; -use std::time::Duration; - -pub struct DutiesManagerService { - pub manager: DutiesManager, - pub poll_interval_millis: u64, - pub log: Logger, -} - -impl DutiesManagerService { - /// Run a loop which polls the manager each `poll_interval_millis` milliseconds. - /// - /// Logs the results of the polls. - pub fn run(&mut self) { - loop { - match self.manager.poll() { - Err(error) => { - error!(self.log, "Epoch duties poll error"; "error" => format!("{:?}", error)) - } - Ok(PollOutcome::NoChange(epoch)) => { - debug!(self.log, "No change in duties"; "epoch" => epoch) - } - Ok(PollOutcome::DutiesChanged(epoch, duties)) => { - info!(self.log, "Duties changed (potential re-org)"; "epoch" => epoch, "duties" => format!("{:?}", duties)) - } - Ok(PollOutcome::NewDuties(epoch, duties)) => { - info!(self.log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties)) - } - Ok(PollOutcome::UnknownValidatorOrEpoch(epoch)) => { - error!(self.log, "Epoch or validator unknown"; "epoch" => epoch) - } - }; - - std::thread::sleep(Duration::from_millis(self.poll_interval_millis)); - } - } -} diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 720388a61..8a7e90d10 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -2,8 +2,8 @@ use crate::attester_service::{AttestationGrpcClient, AttesterService}; use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use crate::config::Config as ValidatorConfig; -use crate::duties::PollOutcome; -use crate::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; +use crate::duties::UpdateOutcome; +use crate::duties::{DutiesManager, EpochDutiesMap}; use crate::error as error_chain; use crate::error::ErrorKind; use attester::test_utils::EpochMap; @@ -230,9 +230,10 @@ impl Service { beacon_node: service.validator_client.clone(), }; + // run the core thread runtime .block_on(interval.for_each(move |_| { - // update duties + // get the current slot let current_slot = match service.slot_clock.present_slot() { Err(e) => { error!(service.log, "SystemTimeError {:?}", e); @@ -246,23 +247,24 @@ impl Service { "The Timer should poll a new slot" ); - debug!(service.log, "Processing slot: {}", current_slot.as_u64()); + info!(service.log, "Processing slot: {}", current_slot.as_u64()); // check for new duties - match manager.poll() { + // TODO: Convert to its own thread + match manager.update(current_slot) { Err(error) => { error!(service.log, "Epoch duties poll error"; "error" => format!("{:?}", error)) } - Ok(PollOutcome::NoChange(epoch)) => { + Ok(UpdateOutcome::NoChange(epoch)) => { debug!(service.log, "No change in duties"; "epoch" => epoch) } - Ok(PollOutcome::DutiesChanged(epoch, duties)) => { + Ok(UpdateOutcome::DutiesChanged(epoch, duties)) => { info!(service.log, "Duties changed (potential re-org)"; "epoch" => epoch, "duties" => format!("{:?}", duties)) } - Ok(PollOutcome::NewDuties(epoch, duties)) => { + Ok(UpdateOutcome::NewDuties(epoch, duties)) => { info!(service.log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties)) } - Ok(PollOutcome::UnknownValidatorOrEpoch(epoch)) => { + Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) => { error!(service.log, "Epoch or validator unknown"; "epoch" => epoch) } };