From 8073296f5dd813cf58cf1c9a6b0e3b8a074cc843 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 31 Jan 2019 16:39:44 +1100 Subject: [PATCH] Ensure per_epoch processing always runs. Previously, it was running _after_ a state transition, not before it with the slot processing. --- .../beacon_chain/src/block_production.rs | 10 +++---- .../beacon_chain/src/canonical_head.rs | 10 +++---- .../beacon_chain/src/state_transition.rs | 14 ++++----- eth2/types/src/beacon_state/mod.rs | 1 + .../types/src/beacon_state/slot_processing.rs | 30 +++++++++++++++++-- 5 files changed, 44 insertions(+), 21 deletions(-) diff --git a/beacon_node/beacon_chain/src/block_production.rs b/beacon_node/beacon_chain/src/block_production.rs index 2a9998ccf..2e8665a77 100644 --- a/beacon_node/beacon_chain/src/block_production.rs +++ b/beacon_node/beacon_chain/src/block_production.rs @@ -4,7 +4,7 @@ use bls::Signature; use log::debug; use slot_clock::TestingSlotClockError; use types::{ - beacon_state::CommitteesError, + beacon_state::SlotProcessingError, readers::{BeaconBlockReader, BeaconStateReader}, BeaconBlock, BeaconBlockBody, BeaconState, Eth1Data, Hash256, }; @@ -14,7 +14,7 @@ pub enum Error { DBError(String), StateTransitionError(TransitionError), PresentSlotIsNone, - CommitteesError(CommitteesError), + SlotProcessingError(SlotProcessingError), } impl BeaconChain @@ -116,8 +116,8 @@ impl From for Error { } } -impl From for Error { - fn from(e: CommitteesError) -> Error { - Error::CommitteesError(e) +impl From for Error { + fn from(e: SlotProcessingError) -> Error { + Error::SlotProcessingError(e) } } diff --git a/beacon_node/beacon_chain/src/canonical_head.rs b/beacon_node/beacon_chain/src/canonical_head.rs index 4bcac5b64..a92cff327 100644 --- a/beacon_node/beacon_chain/src/canonical_head.rs +++ b/beacon_node/beacon_chain/src/canonical_head.rs @@ -1,11 +1,11 @@ use crate::{BeaconChain, CheckPoint, ClientDB, SlotClock}; use std::sync::RwLockReadGuard; -use types::{beacon_state::CommitteesError, BeaconBlock, BeaconState, Hash256}; +use types::{beacon_state::SlotProcessingError, BeaconBlock, BeaconState, Hash256}; #[derive(Debug, PartialEq)] pub enum Error { PastSlot, - CommitteesError(CommitteesError), + SlotProcessingError(SlotProcessingError), } impl BeaconChain @@ -64,8 +64,8 @@ where } } -impl From for Error { - fn from(e: CommitteesError) -> Error { - Error::CommitteesError(e) +impl From for Error { + fn from(e: SlotProcessingError) -> Error { + Error::SlotProcessingError(e) } } diff --git a/beacon_node/beacon_chain/src/state_transition.rs b/beacon_node/beacon_chain/src/state_transition.rs index fc29a63c4..df6712273 100644 --- a/beacon_node/beacon_chain/src/state_transition.rs +++ b/beacon_node/beacon_chain/src/state_transition.rs @@ -5,7 +5,7 @@ use log::debug; use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError}; use ssz::{ssz_encode, TreeHash}; use types::{ - beacon_state::{AttestationValidationError, CommitteesError, EpochProcessingError}, + beacon_state::{AttestationValidationError, CommitteesError, SlotProcessingError}, readers::BeaconBlockReader, BeaconBlock, BeaconState, Exit, Fork, Hash256, PendingAttestation, }; @@ -52,7 +52,7 @@ pub enum Error { BadCustodyResponses, SlotClockError(SystemTimeSlotClockError), CommitteesError(CommitteesError), - EpochProcessingError(EpochProcessingError), + SlotProcessingError(SlotProcessingError), } impl BeaconChain @@ -309,10 +309,6 @@ where Error::BadCustodyResponses ); - if state.slot % self.spec.epoch_length == 0 { - state.per_epoch_processing(&self.spec)?; - } - debug!("State transition complete."); Ok(state) @@ -367,8 +363,8 @@ impl From for Error { } } -impl From for Error { - fn from(e: EpochProcessingError) -> Error { - Error::EpochProcessingError(e) +impl From for Error { + fn from(e: SlotProcessingError) -> Error { + Error::SlotProcessingError(e) } } diff --git a/eth2/types/src/beacon_state/mod.rs b/eth2/types/src/beacon_state/mod.rs index 4ccb3322a..3972eaa82 100644 --- a/eth2/types/src/beacon_state/mod.rs +++ b/eth2/types/src/beacon_state/mod.rs @@ -22,6 +22,7 @@ pub use self::attestation_participants::Error as AttestationParticipantsError; pub use self::attestation_validation::Error as AttestationValidationError; pub use self::committees::Error as CommitteesError; pub use self::epoch_processing::Error as EpochProcessingError; +pub use self::slot_processing::Error as SlotProcessingError; // Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummy class used. type CustodyChallenge = usize; diff --git a/eth2/types/src/beacon_state/slot_processing.rs b/eth2/types/src/beacon_state/slot_processing.rs index 211f0c290..ab5311d21 100644 --- a/eth2/types/src/beacon_state/slot_processing.rs +++ b/eth2/types/src/beacon_state/slot_processing.rs @@ -1,11 +1,21 @@ -use crate::{beacon_state::CommitteesError, BeaconState, ChainSpec, Hash256}; +use crate::{ + beacon_state::{CommitteesError, EpochProcessingError}, + BeaconState, ChainSpec, Hash256, +}; +use log::debug; + +#[derive(Debug, PartialEq)] +pub enum Error { + CommitteesError(CommitteesError), + EpochProcessingError(EpochProcessingError), +} impl BeaconState { pub fn per_slot_processing( &mut self, previous_block_root: Hash256, spec: &ChainSpec, - ) -> Result<(), CommitteesError> { + ) -> Result<(), Error> { self.slot += 1; let block_proposer = self.get_beacon_proposer_index(self.slot, spec)?; @@ -22,6 +32,10 @@ impl BeaconState { let root = merkle_root(&self.latest_block_roots[..]); self.batched_block_roots.push(root); } + + if self.slot % spec.epoch_length == 0 { + self.per_epoch_processing(spec)?; + } Ok(()) } @@ -45,3 +59,15 @@ impl BeaconState { fn merkle_root(_input: &[Hash256]) -> Hash256 { Hash256::zero() } + +impl From for Error { + fn from(e: CommitteesError) -> Error { + Error::CommitteesError(e) + } +} + +impl From for Error { + fn from(e: EpochProcessingError) -> Error { + Error::EpochProcessingError(e) + } +}