From 942ef4b002838842344344d440a701c9a61af378 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 1 Feb 2019 16:30:42 +1100 Subject: [PATCH] Move `advance_slot` to its own file. --- .../beacon_chain/src/canonical_head.rs | 12 --------- beacon_node/beacon_chain/src/lib.rs | 2 +- beacon_node/beacon_chain/src/state.rs | 25 +++++++++++++++++++ 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 beacon_node/beacon_chain/src/state.rs diff --git a/beacon_node/beacon_chain/src/canonical_head.rs b/beacon_node/beacon_chain/src/canonical_head.rs index 14f1adf81..35262961e 100644 --- a/beacon_node/beacon_chain/src/canonical_head.rs +++ b/beacon_node/beacon_chain/src/canonical_head.rs @@ -4,7 +4,6 @@ use types::{beacon_state::SlotProcessingError, BeaconBlock, BeaconState, Hash256 #[derive(Debug, PartialEq)] pub enum Error { - PastSlot, SlotProcessingError(SlotProcessingError), } @@ -32,17 +31,6 @@ where pub fn head(&self) -> RwLockReadGuard { self.canonical_head.read() } - - pub fn advance_state(&self, slot: u64) -> Result<(), SlotProcessingError> { - let state_slot = self.state.read().slot; - let head_block_root = self.head().beacon_block_root; - for _ in state_slot..slot { - self.state - .write() - .per_slot_processing(head_block_root.clone(), &self.spec)?; - } - Ok(()) - } } impl From for Error { diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 5c719dddd..314f18dc9 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -10,7 +10,7 @@ pub mod dump; mod finalized_head; mod info; mod lmd_ghost; -// mod state_transition; +mod state; use self::attestation_targets::AttestationTargets; use self::block_graph::BlockGraph; diff --git a/beacon_node/beacon_chain/src/state.rs b/beacon_node/beacon_chain/src/state.rs new file mode 100644 index 000000000..33e50ce35 --- /dev/null +++ b/beacon_node/beacon_chain/src/state.rs @@ -0,0 +1,25 @@ +use crate::{BeaconChain, ClientDB, SlotClock}; +use types::beacon_state::SlotProcessingError; + +impl BeaconChain +where + T: ClientDB, + U: SlotClock, +{ + /// Advance the `self.state` `BeaconState` to the supplied slot. + /// + /// This will perform per_slot and per_epoch processing as required. + /// + /// The `previous_block_root` will be set to the root of the current head block (as determined + /// by the fork-choice rule). + pub fn advance_state(&self, slot: u64) -> Result<(), SlotProcessingError> { + let state_slot = self.state.read().slot; + let head_block_root = self.head().beacon_block_root; + for _ in state_slot..slot { + self.state + .write() + .per_slot_processing(head_block_root.clone(), &self.spec)?; + } + Ok(()) + } +}