From a5de6a1915b3d9b8afd24d22f0c1acb2987abf40 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 22 Feb 2019 18:14:16 +1300 Subject: [PATCH] Add caching to BeaconState. Removes CachingBeaconState --- .../src/attestation_aggregator.rs | 111 +++---- beacon_node/beacon_chain/src/beacon_chain.rs | 35 ++- .../beacon_chain/src/cached_beacon_state.rs | 150 --------- beacon_node/beacon_chain/src/checkpoint.rs | 2 +- beacon_node/beacon_chain/src/lib.rs | 5 +- .../test_harness/src/beacon_chain_harness.rs | 13 +- .../beacon_chain/test_harness/tests/chain.rs | 3 + .../state_processing/src/block_processable.rs | 29 +- .../state_processing/src/epoch_processable.rs | 65 ++-- eth2/state_processing/src/slot_processable.rs | 2 +- eth2/types/src/beacon_state.rs | 286 +++++++++++------- eth2/types/src/beacon_state/epoch_cache.rs | 77 +++++ eth2/types/src/beacon_state/tests.rs | 51 +++- eth2/types/src/lib.rs | 4 +- 14 files changed, 459 insertions(+), 374 deletions(-) delete mode 100644 beacon_node/beacon_chain/src/cached_beacon_state.rs create mode 100644 eth2/types/src/beacon_state/epoch_cache.rs diff --git a/beacon_node/beacon_chain/src/attestation_aggregator.rs b/beacon_node/beacon_chain/src/attestation_aggregator.rs index fa2ec87ab..70348dc94 100644 --- a/beacon_node/beacon_chain/src/attestation_aggregator.rs +++ b/beacon_node/beacon_chain/src/attestation_aggregator.rs @@ -1,9 +1,9 @@ -use crate::cached_beacon_state::CachedBeaconState; +use log::trace; use state_processing::validate_attestation_without_signature; use std::collections::{HashMap, HashSet}; use types::{ - beacon_state::BeaconStateError, AggregateSignature, Attestation, AttestationData, BeaconState, - Bitfield, ChainSpec, FreeAttestation, Signature, + AggregateSignature, Attestation, AttestationData, BeaconState, BeaconStateError, Bitfield, + ChainSpec, FreeAttestation, Signature, }; const PHASE_0_CUSTODY_BIT: bool = false; @@ -42,21 +42,28 @@ pub enum Message { BadSignature, /// The given `slot` does not match the validators committee assignment. BadSlot, - /// The given `shard` does not match the validators committee assignment. + /// The given `shard` does not match the validators committee assignment, or is not included in + /// a committee for the given slot. BadShard, + /// Attestation is from the epoch prior to this, ignoring. + TooOld, } -macro_rules! some_or_invalid { - ($expression: expr, $error: expr) => { - match $expression { - Some(x) => x, - None => { - return Ok(Outcome { - valid: false, - message: $error, - }); - } - } +macro_rules! valid_outcome { + ($error: expr) => { + return Ok(Outcome { + valid: true, + message: $error, + }); + }; +} + +macro_rules! invalid_outcome { + ($error: expr) => { + return Ok(Outcome { + valid: false, + message: $error, + }); }; } @@ -77,49 +84,56 @@ impl AttestationAggregator { /// - The signature is verified against that of the validator at `validator_index`. pub fn process_free_attestation( &mut self, - cached_state: &CachedBeaconState, + cached_state: &BeaconState, free_attestation: &FreeAttestation, spec: &ChainSpec, ) -> Result { - let (slot, shard, committee_index) = some_or_invalid!( - cached_state.attestation_slot_and_shard_for_validator( - free_attestation.validator_index as usize, - spec, - )?, - Message::BadValidatorIndex + let attestation_duties = match cached_state.attestation_slot_and_shard_for_validator( + free_attestation.validator_index as usize, + spec, + ) { + Err(BeaconStateError::EpochCacheUninitialized(e)) => { + panic!("Attempted to access unbuilt cache {:?}.", e) + } + Err(BeaconStateError::EpochOutOfBounds) => invalid_outcome!(Message::TooOld), + Err(BeaconStateError::ShardOutOfBounds) => invalid_outcome!(Message::BadShard), + Err(e) => return Err(e), + Ok(None) => invalid_outcome!(Message::BadValidatorIndex), + Ok(Some(attestation_duties)) => attestation_duties, + }; + + let (slot, shard, committee_index) = attestation_duties; + + trace!( + "slot: {}, shard: {}, committee_index: {}, val_index: {}", + slot, + shard, + committee_index, + free_attestation.validator_index ); if free_attestation.data.slot != slot { - return Ok(Outcome { - valid: false, - message: Message::BadSlot, - }); + invalid_outcome!(Message::BadSlot); } if free_attestation.data.shard != shard { - return Ok(Outcome { - valid: false, - message: Message::BadShard, - }); + invalid_outcome!(Message::BadShard); } let signable_message = free_attestation.data.signable_message(PHASE_0_CUSTODY_BIT); - let validator_record = some_or_invalid!( - cached_state - .state - .validator_registry - .get(free_attestation.validator_index as usize), - Message::BadValidatorIndex - ); + let validator_record = match cached_state + .validator_registry + .get(free_attestation.validator_index as usize) + { + None => invalid_outcome!(Message::BadValidatorIndex), + Some(validator_record) => validator_record, + }; if !free_attestation .signature .verify(&signable_message, &validator_record.pubkey) { - return Ok(Outcome { - valid: false, - message: Message::BadSignature, - }); + invalid_outcome!(Message::BadSignature); } if let Some(existing_attestation) = self.store.get(&signable_message) { @@ -129,15 +143,9 @@ impl AttestationAggregator { committee_index as usize, ) { self.store.insert(signable_message, updated_attestation); - Ok(Outcome { - valid: true, - message: Message::Aggregated, - }) + valid_outcome!(Message::Aggregated); } else { - Ok(Outcome { - valid: true, - message: Message::AggregationNotRequired, - }) + valid_outcome!(Message::AggregationNotRequired); } } else { let mut aggregate_signature = AggregateSignature::new(); @@ -151,10 +159,7 @@ impl AttestationAggregator { aggregate_signature, }; self.store.insert(signable_message, new_attestation); - Ok(Outcome { - valid: true, - message: Message::NewAttestationCreated, - }) + valid_outcome!(Message::NewAttestationCreated); } } diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index b2d041654..9ee55e5a3 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1,5 +1,4 @@ use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome}; -use crate::cached_beacon_state::CachedBeaconState; use crate::checkpoint::CheckPoint; use db::{ stores::{BeaconBlockStore, BeaconStateStore}, @@ -15,10 +14,10 @@ use state_processing::{ }; use std::sync::Arc; use types::{ - beacon_state::BeaconStateError, readers::{BeaconBlockReader, BeaconStateReader}, - AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, ChainSpec, Crosslink, Deposit, - Epoch, Eth1Data, FreeAttestation, Hash256, PublicKey, Signature, Slot, + AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, BeaconStateError, ChainSpec, + Crosslink, Deposit, Epoch, Eth1Data, FreeAttestation, Hash256, PublicKey, RelativeEpoch, + Signature, Slot, }; #[derive(Debug, PartialEq)] @@ -70,7 +69,6 @@ pub struct BeaconChain { canonical_head: RwLock, finalized_head: RwLock, pub state: RwLock, - pub cached_state: RwLock, pub spec: ChainSpec, pub fork_choice: RwLock, } @@ -96,7 +94,7 @@ where return Err(Error::InsufficientValidators); } - let genesis_state = BeaconState::genesis( + let mut genesis_state = BeaconState::genesis( genesis_time, initial_validator_deposits, latest_eth1_data, @@ -109,32 +107,32 @@ where let block_root = genesis_block.canonical_root(); block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?; - let cached_state = RwLock::new(CachedBeaconState::from_beacon_state( - genesis_state.clone(), - spec.clone(), - )?); - let finalized_head = RwLock::new(CheckPoint::new( genesis_block.clone(), block_root, + // TODO: this is a memory waste; remove full clone. genesis_state.clone(), state_root, )); let canonical_head = RwLock::new(CheckPoint::new( genesis_block.clone(), block_root, + // TODO: this is a memory waste; remove full clone. genesis_state.clone(), state_root, )); let attestation_aggregator = RwLock::new(AttestationAggregator::new()); + genesis_state.build_epoch_cache(RelativeEpoch::Previous, &spec)?; + genesis_state.build_epoch_cache(RelativeEpoch::Current, &spec)?; + genesis_state.build_epoch_cache(RelativeEpoch::Next, &spec)?; + Ok(Self { block_store, state_store, slot_clock, attestation_aggregator, - state: RwLock::new(genesis_state.clone()), - cached_state, + state: RwLock::new(genesis_state), finalized_head, canonical_head, spec, @@ -150,6 +148,10 @@ where new_beacon_state: BeaconState, new_beacon_state_root: Hash256, ) { + debug!( + "Updating canonical head with block at slot: {}", + new_beacon_block.slot + ); let mut head = self.canonical_head.write(); head.update( new_beacon_block, @@ -288,7 +290,7 @@ where validator_index ); if let Some((slot, shard, _committee)) = self - .cached_state + .state .read() .attestation_slot_and_shard_for_validator(validator_index, &self.spec)? { @@ -346,7 +348,7 @@ where let aggregation_outcome = self .attestation_aggregator .write() - .process_free_attestation(&self.cached_state.read(), &free_attestation, &self.spec)?; + .process_free_attestation(&self.state.read(), &free_attestation, &self.spec)?; // return if the attestation is invalid if !aggregation_outcome.valid { @@ -501,9 +503,6 @@ where ); // Update the local state variable. *self.state.write() = state.clone(); - // Update the cached state variable. - *self.cached_state.write() = - CachedBeaconState::from_beacon_state(state.clone(), self.spec.clone())?; } Ok(BlockProcessingOutcome::ValidBlock(ValidBlock::Processed)) diff --git a/beacon_node/beacon_chain/src/cached_beacon_state.rs b/beacon_node/beacon_chain/src/cached_beacon_state.rs deleted file mode 100644 index e14e9fe99..000000000 --- a/beacon_node/beacon_chain/src/cached_beacon_state.rs +++ /dev/null @@ -1,150 +0,0 @@ -use log::{debug, trace}; -use std::collections::HashMap; -use types::{beacon_state::BeaconStateError, BeaconState, ChainSpec, Epoch, Slot}; - -pub const CACHE_PREVIOUS: bool = false; -pub const CACHE_CURRENT: bool = true; -pub const CACHE_NEXT: bool = false; - -pub type CrosslinkCommittees = Vec<(Vec, u64)>; -pub type Shard = u64; -pub type CommitteeIndex = u64; -pub type AttestationDuty = (Slot, Shard, CommitteeIndex); -pub type AttestationDutyMap = HashMap; - -// TODO: CachedBeaconState is presently duplicating `BeaconState` and `ChainSpec`. This is a -// massive memory waste, switch them to references. - -pub struct CachedBeaconState { - pub state: BeaconState, - committees: Vec>, - attestation_duties: Vec, - next_epoch: Epoch, - current_epoch: Epoch, - previous_epoch: Epoch, - spec: ChainSpec, -} - -impl CachedBeaconState { - pub fn from_beacon_state( - state: BeaconState, - spec: ChainSpec, - ) -> Result { - let current_epoch = state.current_epoch(&spec); - let previous_epoch = if current_epoch == spec.genesis_epoch { - current_epoch - } else { - current_epoch.saturating_sub(1_u64) - }; - let next_epoch = state.next_epoch(&spec); - - let mut committees: Vec> = Vec::with_capacity(3); - let mut attestation_duties: Vec = Vec::with_capacity(3); - - if CACHE_PREVIOUS { - debug!("from_beacon_state: building previous epoch cache."); - let cache = build_epoch_cache(&state, previous_epoch, &spec)?; - committees.push(cache.committees); - attestation_duties.push(cache.attestation_duty_map); - } else { - committees.push(vec![]); - attestation_duties.push(HashMap::new()); - } - if CACHE_CURRENT { - debug!("from_beacon_state: building current epoch cache."); - let cache = build_epoch_cache(&state, current_epoch, &spec)?; - committees.push(cache.committees); - attestation_duties.push(cache.attestation_duty_map); - } else { - committees.push(vec![]); - attestation_duties.push(HashMap::new()); - } - if CACHE_NEXT { - debug!("from_beacon_state: building next epoch cache."); - let cache = build_epoch_cache(&state, next_epoch, &spec)?; - committees.push(cache.committees); - attestation_duties.push(cache.attestation_duty_map); - } else { - committees.push(vec![]); - attestation_duties.push(HashMap::new()); - } - - Ok(Self { - state, - committees, - attestation_duties, - next_epoch, - current_epoch, - previous_epoch, - spec, - }) - } - - fn slot_to_cache_index(&self, slot: Slot) -> Option { - trace!("slot_to_cache_index: cache lookup"); - match slot.epoch(self.spec.epoch_length) { - epoch if (epoch == self.previous_epoch) & CACHE_PREVIOUS => Some(0), - epoch if (epoch == self.current_epoch) & CACHE_CURRENT => Some(1), - epoch if (epoch == self.next_epoch) & CACHE_NEXT => Some(2), - _ => None, - } - } - - /// Returns the `slot`, `shard` and `committee_index` for which a validator must produce an - /// attestation. - /// - /// Cached method. - /// - /// Spec v0.2.0 - pub fn attestation_slot_and_shard_for_validator( - &self, - validator_index: usize, - _spec: &ChainSpec, - ) -> Result, BeaconStateError> { - // Get the result for this epoch. - let cache_index = self - .slot_to_cache_index(self.state.slot) - .expect("Current epoch should always have a cache index."); - - let duties = self.attestation_duties[cache_index] - .get(&(validator_index as u64)) - .and_then(|tuple| Some(*tuple)); - - Ok(duties) - } -} - -struct EpochCacheResult { - committees: Vec, - attestation_duty_map: AttestationDutyMap, -} - -fn build_epoch_cache( - state: &BeaconState, - epoch: Epoch, - spec: &ChainSpec, -) -> Result { - let mut epoch_committees: Vec = - Vec::with_capacity(spec.epoch_length as usize); - let mut attestation_duty_map: AttestationDutyMap = HashMap::new(); - - for slot in epoch.slot_iter(spec.epoch_length) { - let slot_committees = state.get_crosslink_committees_at_slot(slot, false, spec)?; - - for (committee, shard) in slot_committees { - for (committee_index, validator_index) in committee.iter().enumerate() { - attestation_duty_map.insert( - *validator_index as u64, - (slot, shard, committee_index as u64), - ); - } - } - - epoch_committees.push(state.get_crosslink_committees_at_slot(slot, false, spec)?) - } - - Ok(EpochCacheResult { - committees: epoch_committees, - attestation_duty_map, - }) -} diff --git a/beacon_node/beacon_chain/src/checkpoint.rs b/beacon_node/beacon_chain/src/checkpoint.rs index bef97d2ed..828e462de 100644 --- a/beacon_node/beacon_chain/src/checkpoint.rs +++ b/beacon_node/beacon_chain/src/checkpoint.rs @@ -3,7 +3,7 @@ use types::{BeaconBlock, BeaconState, Hash256}; /// Represents some block and it's associated state. Generally, this will be used for tracking the /// head, justified head and finalized head. -#[derive(PartialEq, Clone, Serialize)] +#[derive(Clone, Serialize)] pub struct CheckPoint { pub beacon_block: BeaconBlock, pub beacon_block_root: Hash256, diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index bc9085fbe..d7d1d9664 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -1,8 +1,9 @@ mod attestation_aggregator; mod beacon_chain; -mod cached_beacon_state; mod checkpoint; -pub use self::beacon_chain::{BeaconChain, Error}; +pub use self::beacon_chain::{ + BeaconChain, BlockProcessingOutcome, Error, InvalidBlock, ValidBlock, +}; pub use self::checkpoint::CheckPoint; pub use fork_choice::{ForkChoice, ForkChoiceAlgorithms, ForkChoiceError}; diff --git a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs index 9d61952f0..a15e82aa2 100644 --- a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs +++ b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs @@ -1,5 +1,5 @@ use super::ValidatorHarness; -use beacon_chain::BeaconChain; +use beacon_chain::{BeaconChain, BlockProcessingOutcome}; pub use beacon_chain::{CheckPoint, Error as BeaconChainError}; use bls::create_proof_of_possession; use db::{ @@ -157,7 +157,7 @@ impl BeaconChainHarness { .beacon_chain .state .read() - .get_crosslink_committees_at_slot(present_slot, false, &self.spec) + .get_crosslink_committees_at_slot(present_slot, &self.spec) .unwrap() .iter() .fold(vec![], |mut acc, (committee, _slot)| { @@ -223,7 +223,10 @@ impl BeaconChainHarness { debug!("Producing block..."); let block = self.produce_block(); debug!("Submitting block for processing..."); - self.beacon_chain.process_block(block).unwrap(); + match self.beacon_chain.process_block(block) { + Ok(BlockProcessingOutcome::ValidBlock(_)) => {} + other => panic!("block processing failed with {:?}", other), + }; debug!("...block processed by BeaconChain."); debug!("Producing free attestations..."); @@ -242,6 +245,10 @@ impl BeaconChainHarness { debug!("Free attestations processed."); } + pub fn run_fork_choice(&mut self) { + self.beacon_chain.fork_choice().unwrap() + } + /// Dump all blocks and states from the canonical beacon chain. pub fn chain_dump(&self) -> Result, BeaconChainError> { self.beacon_chain.chain_dump() diff --git a/beacon_node/beacon_chain/test_harness/tests/chain.rs b/beacon_node/beacon_chain/test_harness/tests/chain.rs index 1a08ffcf1..1b29a412f 100644 --- a/beacon_node/beacon_chain/test_harness/tests/chain.rs +++ b/beacon_node/beacon_chain/test_harness/tests/chain.rs @@ -35,6 +35,9 @@ fn it_can_produce_past_first_epoch_boundary() { harness.advance_chain_with_block(); debug!("Produced block {}/{}.", i + 1, blocks); } + + harness.run_fork_choice(); + let dump = harness.chain_dump().expect("Chain dump failed."); assert_eq!(dump.len() as u64, blocks + 1); // + 1 for genesis block. diff --git a/eth2/state_processing/src/block_processable.rs b/eth2/state_processing/src/block_processable.rs index 539711c69..0e6b57cf0 100644 --- a/eth2/state_processing/src/block_processable.rs +++ b/eth2/state_processing/src/block_processable.rs @@ -4,9 +4,8 @@ use int_to_bytes::int_to_bytes32; use log::{debug, trace}; use ssz::{ssz_encode, TreeHash}; use types::{ - beacon_state::{AttestationParticipantsError, BeaconStateError}, - AggregatePublicKey, Attestation, BeaconBlock, BeaconState, ChainSpec, Crosslink, Epoch, Exit, - Fork, Hash256, PendingAttestation, PublicKey, Signature, + AggregatePublicKey, Attestation, BeaconBlock, BeaconState, BeaconStateError, ChainSpec, + Crosslink, Epoch, Exit, Fork, Hash256, PendingAttestation, PublicKey, RelativeEpoch, Signature, }; // TODO: define elsehwere. @@ -27,7 +26,6 @@ pub enum Error { MissingBeaconBlock(Hash256), InvalidBeaconBlock(Hash256), MissingParentBlock(Hash256), - NoBlockProducer, StateSlotMismatch, BadBlockSignature, BadRandaoSignature, @@ -56,7 +54,7 @@ pub enum AttestationValidationError { BadSignature, ShardBlockRootNotZero, NoBlockRoot, - AttestationParticipantsError(AttestationParticipantsError), + BeaconStateError(BeaconStateError), } macro_rules! ensure { @@ -98,12 +96,15 @@ fn per_block_processing_signature_optional( ) -> Result<(), Error> { ensure!(block.slot == state.slot, Error::StateSlotMismatch); + // Building the previous epoch could be delayed until an attestation from a previous epoch is + // included. This is left for future optimisation. + state.build_epoch_cache(RelativeEpoch::Previous, spec)?; + state.build_epoch_cache(RelativeEpoch::Current, spec)?; + /* * Proposer Signature */ - let block_proposer_index = state - .get_beacon_proposer_index(block.slot, spec) - .map_err(|_| Error::NoBlockProducer)?; + let block_proposer_index = state.get_beacon_proposer_index(block.slot, spec)?; let block_proposer = &state.validator_registry[block_proposer_index]; if verify_block_signature { @@ -361,6 +362,12 @@ fn validate_attestation_signature_optional( &attestation.aggregation_bitfield, spec, )?; + trace!( + "slot: {}, shard: {}, participants: {:?}", + attestation.data.slot, + attestation.data.shard, + participants + ); let mut group_public_key = AggregatePublicKey::new(); for participant in participants { group_public_key.add( @@ -417,8 +424,8 @@ impl From for Error { } } -impl From for AttestationValidationError { - fn from(e: AttestationParticipantsError) -> AttestationValidationError { - AttestationValidationError::AttestationParticipantsError(e) +impl From for AttestationValidationError { + fn from(e: BeaconStateError) -> AttestationValidationError { + AttestationValidationError::BeaconStateError(e) } } diff --git a/eth2/state_processing/src/epoch_processable.rs b/eth2/state_processing/src/epoch_processable.rs index 11b2b224d..409d40a2c 100644 --- a/eth2/state_processing/src/epoch_processable.rs +++ b/eth2/state_processing/src/epoch_processable.rs @@ -5,9 +5,8 @@ use ssz::TreeHash; use std::collections::{HashMap, HashSet}; use std::iter::FromIterator; use types::{ - beacon_state::{AttestationParticipantsError, BeaconStateError, InclusionError}, - validator_registry::get_active_validator_indices, - BeaconState, ChainSpec, Crosslink, Epoch, Hash256, PendingAttestation, + validator_registry::get_active_validator_indices, BeaconState, BeaconStateError, ChainSpec, + Crosslink, Epoch, Hash256, InclusionError, PendingAttestation, RelativeEpoch, }; macro_rules! safe_add_assign { @@ -28,7 +27,6 @@ pub enum Error { BaseRewardQuotientIsZero, NoRandaoSeed, BeaconStateError(BeaconStateError), - AttestationParticipantsError(AttestationParticipantsError), InclusionError(InclusionError), WinningRootError(WinningRootError), } @@ -36,7 +34,7 @@ pub enum Error { #[derive(Debug, PartialEq)] pub enum WinningRootError { NoWinningRoot, - AttestationParticipantsError(AttestationParticipantsError), + BeaconStateError(BeaconStateError), } #[derive(Clone)] @@ -66,6 +64,11 @@ impl EpochProcessable for BeaconState { self.current_epoch(spec) ); + // Ensure all of the caches are built. + self.build_epoch_cache(RelativeEpoch::Previous, spec)?; + self.build_epoch_cache(RelativeEpoch::Current, spec)?; + self.build_epoch_cache(RelativeEpoch::Next, spec)?; + /* * Validators attesting during the current epoch. */ @@ -322,8 +325,11 @@ impl EpochProcessable for BeaconState { slot, slot.epoch(spec.epoch_length) ); + + // Clone is used to remove the borrow. It becomes an issue later when trying to mutate + // `self.balances`. let crosslink_committees_at_slot = - self.get_crosslink_committees_at_slot(slot, false, spec)?; + self.get_crosslink_committees_at_slot(slot, spec)?.clone(); for (crosslink_committee, shard) in crosslink_committees_at_slot { let shard = shard as u64; @@ -499,8 +505,10 @@ impl EpochProcessable for BeaconState { * Crosslinks */ for slot in self.previous_epoch(spec).slot_iter(spec.epoch_length) { + // Clone is used to remove the borrow. It becomes an issue later when trying to mutate + // `self.balances`. let crosslink_committees_at_slot = - self.get_crosslink_committees_at_slot(slot, false, spec)?; + self.get_crosslink_committees_at_slot(slot, spec)?.clone(); for (_crosslink_committee, shard) in crosslink_committees_at_slot { let shard = shard as u64; @@ -609,6 +617,12 @@ impl EpochProcessable for BeaconState { .cloned() .collect(); + /* + * Manage the beacon state caches + */ + self.advance_caches(); + self.build_epoch_cache(RelativeEpoch::Next, spec)?; + debug!("Epoch transition complete."); Ok(()) @@ -645,19 +659,18 @@ fn winning_root( } // TODO: `cargo fmt` makes this rather ugly; tidy up. - let attesting_validator_indices = attestations.iter().try_fold::<_, _, Result< - _, - AttestationParticipantsError, - >>(vec![], |mut acc, a| { - if (a.data.shard == shard) && (a.data.shard_block_root == *shard_block_root) { - acc.append(&mut state.get_attestation_participants( - &a.data, - &a.aggregation_bitfield, - spec, - )?); - } - Ok(acc) - })?; + let attesting_validator_indices = attestations + .iter() + .try_fold::<_, _, Result<_, BeaconStateError>>(vec![], |mut acc, a| { + if (a.data.shard == shard) && (a.data.shard_block_root == *shard_block_root) { + acc.append(&mut state.get_attestation_participants( + &a.data, + &a.aggregation_bitfield, + spec, + )?); + } + Ok(acc) + })?; let total_balance: u64 = attesting_validator_indices .iter() @@ -708,15 +721,9 @@ impl From for Error { } } -impl From for Error { - fn from(e: AttestationParticipantsError) -> Error { - Error::AttestationParticipantsError(e) - } -} - -impl From for WinningRootError { - fn from(e: AttestationParticipantsError) -> WinningRootError { - WinningRootError::AttestationParticipantsError(e) +impl From for WinningRootError { + fn from(e: BeaconStateError) -> WinningRootError { + WinningRootError::BeaconStateError(e) } } diff --git a/eth2/state_processing/src/slot_processable.rs b/eth2/state_processing/src/slot_processable.rs index 9e3b611fd..0bbc79ab0 100644 --- a/eth2/state_processing/src/slot_processable.rs +++ b/eth2/state_processing/src/slot_processable.rs @@ -1,5 +1,5 @@ use crate::{EpochProcessable, EpochProcessingError}; -use types::{beacon_state::BeaconStateError, BeaconState, ChainSpec, Hash256}; +use types::{BeaconState, BeaconStateError, ChainSpec, Hash256}; #[derive(Debug, PartialEq)] pub enum Error { diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 2216e9516..47805da80 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -1,3 +1,4 @@ +use self::epoch_cache::EpochCache; use crate::test_utils::TestRandom; use crate::{ validator::StatusFlags, validator_registry::get_active_validator_indices, AttestationData, @@ -10,13 +11,35 @@ use log::trace; use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; +use std::collections::HashMap; use swap_or_not_shuffle::get_permutated_index; +mod epoch_cache; mod tests; +pub type Committee = Vec; +pub type CrosslinkCommittees = Vec<(Committee, u64)>; +pub type Shard = u64; +pub type CommitteeIndex = u64; +pub type AttestationDuty = (Slot, Shard, CommitteeIndex); +pub type AttestationDutyMap = HashMap; +pub type ShardCommitteeIndexMap = HashMap; + +pub const CACHED_EPOCHS: usize = 3; + +#[derive(Debug, PartialEq, Clone, Copy)] +pub enum RelativeEpoch { + Previous, + Current, + Next, +} + #[derive(Debug, PartialEq)] -pub enum BeaconStateError { +pub enum Error { EpochOutOfBounds, + /// The supplied shard is unknown. It may be larger than the maximum shard count, or not in a + /// committee for the given slot. + ShardOutOfBounds, UnableToShuffle, InsufficientRandaoMixes, InsufficientValidators, @@ -24,20 +47,14 @@ pub enum BeaconStateError { InsufficientIndexRoots, InsufficientAttestations, InsufficientCommittees, + EpochCacheUninitialized(RelativeEpoch), } #[derive(Debug, PartialEq)] pub enum InclusionError { /// The validator did not participate in an attestation in this period. NoAttestationsForValidator, - AttestationParticipantsError(AttestationParticipantsError), -} - -#[derive(Debug, PartialEq)] -pub enum AttestationParticipantsError { - /// There is no committee for the given shard in the given epoch. - NoCommitteeForShard, - BeaconStateError(BeaconStateError), + Error(Error), } macro_rules! safe_add_assign { @@ -89,6 +106,10 @@ pub struct BeaconState { // Ethereum 1.0 chain data pub latest_eth1_data: Eth1Data, pub eth1_data_votes: Vec, + + // Caching + pub cache_index_offset: usize, + pub caches: Vec, } impl BeaconState { @@ -98,7 +119,7 @@ impl BeaconState { initial_validator_deposits: Vec, latest_eth1_data: Eth1Data, spec: &ChainSpec, - ) -> Result { + ) -> Result { let initial_crosslink = Crosslink { epoch: spec.genesis_epoch, shard_block_root: spec.zero_hash, @@ -157,6 +178,12 @@ impl BeaconState { */ latest_eth1_data, eth1_data_votes: vec![], + + /* + * Caching (not in spec) + */ + cache_index_offset: 0, + caches: vec![EpochCache::empty(); CACHED_EPOCHS], }; for deposit in initial_validator_deposits { @@ -187,6 +214,81 @@ impl BeaconState { Ok(genesis_state) } + /// Build an epoch cache, unless it is has already been built. + pub fn build_epoch_cache( + &mut self, + relative_epoch: RelativeEpoch, + spec: &ChainSpec, + ) -> Result<(), Error> { + let cache_index = self.cache_index(relative_epoch); + + if self.caches[cache_index].initialized == false { + self.force_build_epoch_cache(relative_epoch, spec) + } else { + Ok(()) + } + } + + /// Always builds an epoch cache, even if it is alread initialized. + pub fn force_build_epoch_cache( + &mut self, + relative_epoch: RelativeEpoch, + spec: &ChainSpec, + ) -> Result<(), Error> { + let epoch = self.absolute_epoch(relative_epoch, spec); + let cache_index = self.cache_index(relative_epoch); + + self.caches[cache_index] = EpochCache::initialized(&self, epoch, spec)?; + + Ok(()) + } + + fn absolute_epoch(&self, relative_epoch: RelativeEpoch, spec: &ChainSpec) -> Epoch { + match relative_epoch { + RelativeEpoch::Previous => self.previous_epoch(spec), + RelativeEpoch::Current => self.current_epoch(spec), + RelativeEpoch::Next => self.next_epoch(spec), + } + } + + fn relative_epoch(&self, epoch: Epoch, spec: &ChainSpec) -> Result { + match epoch { + e if e == self.current_epoch(spec) => Ok(RelativeEpoch::Current), + e if e == self.previous_epoch(spec) => Ok(RelativeEpoch::Previous), + e if e == self.next_epoch(spec) => Ok(RelativeEpoch::Next), + _ => Err(Error::EpochOutOfBounds), + } + } + + pub fn advance_caches(&mut self) { + let previous_cache_index = self.cache_index(RelativeEpoch::Previous); + + self.caches[previous_cache_index] = EpochCache::empty(); + + self.cache_index_offset += 1; + self.cache_index_offset %= CACHED_EPOCHS; + } + + fn cache_index(&self, relative_epoch: RelativeEpoch) -> usize { + let base_index = match relative_epoch { + RelativeEpoch::Current => 1, + RelativeEpoch::Previous => 0, + RelativeEpoch::Next => 2, + }; + + (base_index + self.cache_index_offset) % CACHED_EPOCHS + } + + fn cache<'a>(&'a self, relative_epoch: RelativeEpoch) -> Result<&'a EpochCache, Error> { + let cache = &self.caches[self.cache_index(relative_epoch)]; + + if cache.initialized == false { + Err(Error::EpochCacheUninitialized(relative_epoch)) + } else { + Ok(cache) + } + } + /// Return the tree hash root for this `BeaconState`. /// /// Spec v0.2.0 @@ -258,7 +360,7 @@ impl BeaconState { /// committee is itself a list of validator indices. /// /// Spec v0.1 - pub fn get_shuffling( + pub(crate) fn get_shuffling( &self, seed: Hash256, epoch: Epoch, @@ -271,11 +373,6 @@ impl BeaconState { return None; } - trace!( - "get_shuffling: active_validator_indices.len() == {}", - active_validator_indices.len() - ); - let committees_per_epoch = self.get_epoch_committee_count(active_validator_indices.len(), spec); @@ -339,17 +436,9 @@ impl BeaconState { + 1; let latest_index_root = current_epoch + spec.entry_exit_delay; - trace!( - "get_active_index_root: epoch: {}, earliest: {}, latest: {}", - epoch, - earliest_index_root, - latest_index_root - ); - if (epoch >= earliest_index_root) & (epoch <= latest_index_root) { Some(self.latest_index_roots[epoch.as_usize() % spec.latest_index_roots_length]) } else { - trace!("get_active_index_root: epoch out of range."); None } } @@ -357,20 +446,16 @@ impl BeaconState { /// Generate a seed for the given ``epoch``. /// /// Spec v0.2.0 - pub fn generate_seed( - &self, - epoch: Epoch, - spec: &ChainSpec, - ) -> Result { + pub fn generate_seed(&self, epoch: Epoch, spec: &ChainSpec) -> Result { let mut input = self .get_randao_mix(epoch, spec) - .ok_or_else(|| BeaconStateError::InsufficientRandaoMixes)? + .ok_or_else(|| Error::InsufficientRandaoMixes)? .to_vec(); input.append( &mut self .get_active_index_root(epoch, spec) - .ok_or_else(|| BeaconStateError::InsufficientIndexRoots)? + .ok_or_else(|| Error::InsufficientIndexRoots)? .to_vec(), ); @@ -380,18 +465,34 @@ impl BeaconState { Ok(Hash256::from(&hash(&input[..])[..])) } + pub fn get_crosslink_committees_at_slot( + &self, + slot: Slot, + spec: &ChainSpec, + ) -> Result<&CrosslinkCommittees, Error> { + let epoch = slot.epoch(spec.epoch_length); + let relative_epoch = self.relative_epoch(epoch, spec)?; + let cache = self.cache(relative_epoch)?; + + let slot_offset = slot - epoch.start_slot(spec.epoch_length); + + Ok(&cache.committees[slot_offset.as_usize()]) + } + /// Return the list of ``(committee, shard)`` tuples for the ``slot``. /// /// Note: There are two possible shufflings for crosslink committees for a /// `slot` in the next epoch: with and without a `registry_change` /// + /// Note: this is equivalent to the `get_crosslink_committees_at_slot` function in the spec. + /// /// Spec v0.2.0 - pub fn get_crosslink_committees_at_slot( + pub(crate) fn calculate_crosslink_committees_at_slot( &self, slot: Slot, registry_change: bool, spec: &ChainSpec, - ) -> Result, u64)>, BeaconStateError> { + ) -> Result, u64)>, Error> { let epoch = slot.epoch(spec.epoch_length); let current_epoch = self.current_epoch(spec); let previous_epoch = self.previous_epoch(spec); @@ -441,24 +542,17 @@ impl BeaconState { shuffling_start_shard, ) } else { - return Err(BeaconStateError::EpochOutOfBounds); + return Err(Error::EpochOutOfBounds); }; let shuffling = self .get_shuffling(seed, shuffling_epoch, spec) - .ok_or_else(|| BeaconStateError::UnableToShuffle)?; + .ok_or_else(|| Error::UnableToShuffle)?; let offset = slot.as_u64() % spec.epoch_length; let committees_per_slot = committees_per_epoch / spec.epoch_length; let slot_start_shard = (shuffling_start_shard + committees_per_slot * offset) % spec.shard_count; - trace!( - "get_crosslink_committees_at_slot: committees_per_slot: {}, slot_start_shard: {}, seed: {}", - committees_per_slot, - slot_start_shard, - seed - ); - let mut crosslinks_at_slot = vec![]; for i in 0..committees_per_slot { let tuple = ( @@ -473,22 +567,20 @@ impl BeaconState { /// Returns the `slot`, `shard` and `committee_index` for which a validator must produce an /// attestation. /// + /// Only reads the current epoch. + /// /// Spec v0.2.0 pub fn attestation_slot_and_shard_for_validator( &self, validator_index: usize, - spec: &ChainSpec, - ) -> Result, BeaconStateError> { - let mut result = None; - for slot in self.current_epoch(spec).slot_iter(spec.epoch_length) { - for (committee, shard) in self.get_crosslink_committees_at_slot(slot, false, spec)? { - if let Some(committee_index) = committee.iter().position(|&i| i == validator_index) - { - result = Some((slot, shard, committee_index as u64)); - } - } - } - Ok(result) + _spec: &ChainSpec, + ) -> Result, Error> { + let cache = self.cache(RelativeEpoch::Current)?; + + Ok(cache + .attestation_duty_map + .get(&(validator_index as u64)) + .and_then(|tuple| Some(*tuple))) } /// An entry or exit triggered in the ``epoch`` given by the input takes effect at @@ -504,12 +596,8 @@ impl BeaconState { /// If the state does not contain an index for a beacon proposer at the requested `slot`, then `None` is returned. /// /// Spec v0.2.0 - pub fn get_beacon_proposer_index( - &self, - slot: Slot, - spec: &ChainSpec, - ) -> Result { - let committees = self.get_crosslink_committees_at_slot(slot, false, spec)?; + pub fn get_beacon_proposer_index(&self, slot: Slot, spec: &ChainSpec) -> Result { + let committees = self.get_crosslink_committees_at_slot(slot, spec)?; trace!( "get_beacon_proposer_index: slot: {}, committees_count: {}", slot, @@ -517,11 +605,12 @@ impl BeaconState { ); committees .first() - .ok_or(BeaconStateError::InsufficientValidators) + .ok_or(Error::InsufficientValidators) .and_then(|(first_committee, _)| { - let index = (slot.as_usize()) + let index = slot + .as_usize() .checked_rem(first_committee.len()) - .ok_or(BeaconStateError::InsufficientValidators)?; + .ok_or(Error::InsufficientValidators)?; Ok(first_committee[index]) }) } @@ -730,7 +819,7 @@ impl BeaconState { &mut self, validator_index: usize, spec: &ChainSpec, - ) -> Result<(), BeaconStateError> { + ) -> Result<(), Error> { self.exit_validator(validator_index, spec); let current_epoch = self.current_epoch(spec); @@ -899,54 +988,47 @@ impl BeaconState { &self, attestations: &[&PendingAttestation], spec: &ChainSpec, - ) -> Result, AttestationParticipantsError> { - let mut all_participants = attestations.iter().try_fold::<_, _, Result< - Vec, - AttestationParticipantsError, - >>(vec![], |mut acc, a| { - acc.append(&mut self.get_attestation_participants( - &a.data, - &a.aggregation_bitfield, - spec, - )?); - Ok(acc) - })?; + ) -> Result, Error> { + let mut all_participants = attestations + .iter() + .try_fold::<_, _, Result, Error>>(vec![], |mut acc, a| { + acc.append(&mut self.get_attestation_participants( + &a.data, + &a.aggregation_bitfield, + spec, + )?); + Ok(acc) + })?; all_participants.sort_unstable(); all_participants.dedup(); Ok(all_participants) } - /// Return the participant indices at for the ``attestation_data`` and ``bitfield``. - /// - /// In effect, this converts the "committee indices" on the bitfield into "validator indices" - /// for self.validator_registy. - /// - /// Spec v0.2.0 pub fn get_attestation_participants( &self, attestation_data: &AttestationData, bitfield: &Bitfield, spec: &ChainSpec, - ) -> Result, AttestationParticipantsError> { - let crosslink_committees = - self.get_crosslink_committees_at_slot(attestation_data.slot, false, spec)?; + ) -> Result, Error> { + let epoch = attestation_data.slot.epoch(spec.epoch_length); + let relative_epoch = self.relative_epoch(epoch, spec)?; + let cache = self.cache(relative_epoch)?; - let committee_index: usize = crosslink_committees - .iter() - .position(|(_committee, shard)| *shard == attestation_data.shard) - .ok_or_else(|| AttestationParticipantsError::NoCommitteeForShard)?; - let (crosslink_committee, _shard) = &crosslink_committees[committee_index]; + let (committee_slot_index, committee_index) = cache + .shard_committee_index_map + .get(&attestation_data.shard) + .ok_or_else(|| Error::ShardOutOfBounds)?; + let (committee, shard) = &cache.committees[*committee_slot_index][*committee_index]; - /* - * TODO: verify bitfield length is valid. - */ + assert_eq!(*shard, attestation_data.shard, "Bad epoch cache build."); let mut participants = vec![]; - for (i, validator_index) in crosslink_committee.iter().enumerate() { + for (i, validator_index) in committee.iter().enumerate() { if bitfield.get(i).unwrap() { participants.push(*validator_index); } } + Ok(participants) } } @@ -955,15 +1037,9 @@ fn hash_tree_root(input: Vec) -> Hash256 { Hash256::from(&input.hash_tree_root()[..]) } -impl From for AttestationParticipantsError { - fn from(e: BeaconStateError) -> AttestationParticipantsError { - AttestationParticipantsError::BeaconStateError(e) - } -} - -impl From for InclusionError { - fn from(e: AttestationParticipantsError) -> InclusionError { - InclusionError::AttestationParticipantsError(e) +impl From for InclusionError { + fn from(e: Error) -> InclusionError { + InclusionError::Error(e) } } @@ -1052,6 +1128,8 @@ impl Decodable for BeaconState { batched_block_roots, latest_eth1_data, eth1_data_votes, + cache_index_offset: 0, + caches: vec![EpochCache::empty(); CACHED_EPOCHS], }, i, )) @@ -1122,6 +1200,8 @@ impl TestRandom for BeaconState { batched_block_roots: <_>::random_for_test(rng), latest_eth1_data: <_>::random_for_test(rng), eth1_data_votes: <_>::random_for_test(rng), + cache_index_offset: 0, + caches: vec![EpochCache::empty(); CACHED_EPOCHS], } } } diff --git a/eth2/types/src/beacon_state/epoch_cache.rs b/eth2/types/src/beacon_state/epoch_cache.rs new file mode 100644 index 000000000..0653aaa3d --- /dev/null +++ b/eth2/types/src/beacon_state/epoch_cache.rs @@ -0,0 +1,77 @@ +use super::{AttestationDutyMap, BeaconState, CrosslinkCommittees, Error, ShardCommitteeIndexMap}; +use crate::{ChainSpec, Epoch}; +use log::trace; +use serde_derive::Serialize; +use std::collections::HashMap; + +#[derive(Debug, PartialEq, Clone, Serialize)] +pub struct EpochCache { + /// True if this cache has been initialized. + pub initialized: bool, + /// The crosslink committees for an epoch. + pub committees: Vec, + /// Maps validator index to a slot, shard and committee index for attestation. + pub attestation_duty_map: AttestationDutyMap, + /// Maps a shard to an index of `self.committees`. + pub shard_committee_index_map: ShardCommitteeIndexMap, +} + +impl EpochCache { + pub fn empty() -> EpochCache { + EpochCache { + initialized: false, + committees: vec![], + attestation_duty_map: AttestationDutyMap::new(), + shard_committee_index_map: ShardCommitteeIndexMap::new(), + } + } + + pub fn initialized( + state: &BeaconState, + epoch: Epoch, + spec: &ChainSpec, + ) -> Result { + let mut epoch_committees: Vec = + Vec::with_capacity(spec.epoch_length as usize); + let mut attestation_duty_map: AttestationDutyMap = HashMap::new(); + let mut shard_committee_index_map: ShardCommitteeIndexMap = HashMap::new(); + + for (epoch_committeess_index, slot) in epoch.slot_iter(spec.epoch_length).enumerate() { + let slot_committees = + state.calculate_crosslink_committees_at_slot(slot, false, spec)?; + + for (slot_committees_index, (committee, shard)) in slot_committees.iter().enumerate() { + // Empty committees are not permitted. + if committee.is_empty() { + return Err(Error::InsufficientValidators); + } + + trace!( + "shard: {}, epoch_i: {}, slot_i: {}", + shard, + epoch_committeess_index, + slot_committees_index + ); + + shard_committee_index_map + .insert(*shard, (epoch_committeess_index, slot_committees_index)); + + for (committee_index, validator_index) in committee.iter().enumerate() { + attestation_duty_map.insert( + *validator_index as u64, + (slot, *shard, committee_index as u64), + ); + } + } + + epoch_committees.push(slot_committees) + } + + Ok(EpochCache { + initialized: true, + committees: epoch_committees, + attestation_duty_map, + shard_committee_index_map, + }) + } +} diff --git a/eth2/types/src/beacon_state/tests.rs b/eth2/types/src/beacon_state/tests.rs index 2b7c5b539..d503d8c6a 100644 --- a/eth2/types/src/beacon_state/tests.rs +++ b/eth2/types/src/beacon_state/tests.rs @@ -3,8 +3,8 @@ use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::{ - beacon_state::BeaconStateError, BeaconState, ChainSpec, Deposit, DepositData, DepositInput, - Eth1Data, Hash256, Keypair, + BeaconState, BeaconStateError, ChainSpec, Deposit, DepositData, DepositInput, Eth1Data, + Hash256, Keypair, }; use bls::create_proof_of_possession; use ssz::ssz_encode; @@ -73,6 +73,53 @@ pub fn can_produce_genesis_block() { builder.build().unwrap(); } +/// Tests that `get_attestation_participants` is consistent with the result of +/// get_crosslink_committees_at_slot` with a full bitfield. +#[test] +pub fn get_attestation_participants_consistency() { + let mut rng = XorShiftRng::from_seed([42; 16]); + + let mut builder = BeaconStateTestBuilder::with_random_validators(8); + builder.spec = ChainSpec::few_validators(); + + let mut state = builder.build().unwrap(); + let spec = builder.spec.clone(); + + state + .build_epoch_cache(RelativeEpoch::Previous, &spec) + .unwrap(); + state + .build_epoch_cache(RelativeEpoch::Current, &spec) + .unwrap(); + state.build_epoch_cache(RelativeEpoch::Next, &spec).unwrap(); + + for slot in state + .slot + .epoch(spec.epoch_length) + .slot_iter(spec.epoch_length) + { + let committees = state.get_crosslink_committees_at_slot(slot, &spec).unwrap(); + + for (committee, shard) in committees { + let mut attestation_data = AttestationData::random_for_test(&mut rng); + attestation_data.slot = slot; + attestation_data.shard = *shard; + + let mut bitfield = Bitfield::new(); + for (i, _) in committee.iter().enumerate() { + bitfield.set(i, true); + } + + assert_eq!( + state + .get_attestation_participants(&attestation_data, &bitfield, &spec) + .unwrap(), + *committee + ); + } + } +} + #[test] pub fn test_ssz_round_trip() { let mut rng = XorShiftRng::from_seed([42; 16]); diff --git a/eth2/types/src/lib.rs b/eth2/types/src/lib.rs index f2c128440..4f196b9e9 100644 --- a/eth2/types/src/lib.rs +++ b/eth2/types/src/lib.rs @@ -42,7 +42,9 @@ pub use crate::attestation_data_and_custody_bit::AttestationDataAndCustodyBit; pub use crate::attester_slashing::AttesterSlashing; pub use crate::beacon_block::BeaconBlock; pub use crate::beacon_block_body::BeaconBlockBody; -pub use crate::beacon_state::BeaconState; +pub use crate::beacon_state::{ + BeaconState, Error as BeaconStateError, InclusionError, RelativeEpoch, +}; pub use crate::casper_slashing::CasperSlashing; pub use crate::chain_spec::ChainSpec; pub use crate::crosslink::Crosslink;