diff --git a/eth2/state_processing/src/lib.rs b/eth2/state_processing/src/lib.rs index 44c0e28a0..2b30844cb 100644 --- a/eth2/state_processing/src/lib.rs +++ b/eth2/state_processing/src/lib.rs @@ -3,12 +3,11 @@ mod macros; pub mod per_block_processing; pub mod per_epoch_processing; -// mod slot_processable; +pub mod per_slot_processing; pub use per_block_processing::{ errors::{BlockInvalid, BlockProcessingError}, per_block_processing, per_block_processing_without_verifying_block_signature, }; pub use per_epoch_processing::{errors::EpochProcessingError, per_epoch_processing}; -// pub use epoch_processable::{EpochProcessable, Error as EpochProcessingError}; -// pub use slot_processable::{Error as SlotProcessingError, SlotProcessable}; +pub use per_slot_processing::{per_slot_processing, Error as SlotProcessingError}; diff --git a/eth2/state_processing/src/per_slot_processing.rs b/eth2/state_processing/src/per_slot_processing.rs new file mode 100644 index 000000000..0bb405c98 --- /dev/null +++ b/eth2/state_processing/src/per_slot_processing.rs @@ -0,0 +1,58 @@ +use crate::*; +use types::{BeaconState, BeaconStateError, ChainSpec, Hash256}; + +#[derive(Debug, PartialEq)] +pub enum Error { + BeaconStateError(BeaconStateError), + EpochProcessingError(EpochProcessingError), +} + +/// Advances a state forward by one slot, performing per-epoch processing if required. +/// +/// Spec v0.4.0 +pub fn per_slot_processing( + state: &mut BeaconState, + previous_block_root: Hash256, + spec: &ChainSpec, +) -> Result<(), Error> { + if (state.slot + 1) % spec.slots_per_epoch == 0 { + per_epoch_processing(state, spec)?; + state.advance_caches(); + } + + state.slot += 1; + + update_block_roots(state, previous_block_root, spec); + + Ok(()) +} + +/// Updates the state's block roots as per-slot processing is performed. +/// +/// Spec v0.4.0 +pub fn update_block_roots(state: &mut BeaconState, previous_block_root: Hash256, spec: &ChainSpec) { + state.latest_block_roots[(state.slot.as_usize() - 1) % spec.latest_block_roots_length] = + previous_block_root; + + if state.slot.as_usize() % spec.latest_block_roots_length == 0 { + let root = merkle_root(&state.latest_block_roots[..]); + state.batched_block_roots.push(root); + } +} + +fn merkle_root(_input: &[Hash256]) -> Hash256 { + // TODO: implement correctly. + Hash256::zero() +} + +impl From for Error { + fn from(e: BeaconStateError) -> Error { + Error::BeaconStateError(e) + } +} + +impl From for Error { + fn from(e: EpochProcessingError) -> Error { + Error::EpochProcessingError(e) + } +} diff --git a/eth2/state_processing/src/slot_processable.rs b/eth2/state_processing/src/slot_processable.rs deleted file mode 100644 index 8d6506c36..000000000 --- a/eth2/state_processing/src/slot_processable.rs +++ /dev/null @@ -1,71 +0,0 @@ -use crate::{EpochProcessable, EpochProcessingError}; -use types::{BeaconState, BeaconStateError, ChainSpec, Hash256}; - -#[derive(Debug, PartialEq)] -pub enum Error { - BeaconStateError(BeaconStateError), - EpochProcessingError(EpochProcessingError), -} - -pub trait SlotProcessable { - fn per_slot_processing( - &mut self, - previous_block_root: Hash256, - spec: &ChainSpec, - ) -> Result<(), Error>; -} - -impl SlotProcessable for BeaconState -where - BeaconState: EpochProcessable, -{ - fn per_slot_processing( - &mut self, - previous_block_root: Hash256, - spec: &ChainSpec, - ) -> Result<(), Error> { - if (self.slot + 1) % spec.slots_per_epoch == 0 { - self.per_epoch_processing(spec)?; - self.advance_caches(); - } - - self.slot += 1; - - self.latest_randao_mixes[self.slot.as_usize() % spec.latest_randao_mixes_length] = - self.latest_randao_mixes[(self.slot.as_usize() - 1) % spec.latest_randao_mixes_length]; - - // Block roots. - self.latest_block_roots[(self.slot.as_usize() - 1) % spec.latest_block_roots_length] = - previous_block_root; - - if self.slot.as_usize() % spec.latest_block_roots_length == 0 { - let root = merkle_root(&self.latest_block_roots[..]); - self.batched_block_roots.push(root); - } - Ok(()) - } -} - -fn merkle_root(_input: &[Hash256]) -> Hash256 { - Hash256::zero() -} - -impl From for Error { - fn from(e: BeaconStateError) -> Error { - Error::BeaconStateError(e) - } -} - -impl From for Error { - fn from(e: EpochProcessingError) -> Error { - Error::EpochProcessingError(e) - } -} - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -}