Rename EpochCache to CommitteeCache

This commit is contained in:
Paul Hauner 2019-05-20 14:36:54 +10:00
parent cb74187cfc
commit 6660311b2b
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
10 changed files with 78 additions and 70 deletions

View File

@ -332,8 +332,8 @@ where
// If required, transition the new state to the present slot. // If required, transition the new state to the present slot.
for _ in state.slot.as_u64()..present_slot.as_u64() { for _ in state.slot.as_u64()..present_slot.as_u64() {
// Ensure the next epoch state caches are built in case of an epoch transition. // Ensure the next epoch state caches are built in case of an epoch transition.
state.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, &self.spec)?; state.build_committee_cache(RelativeEpoch::NextWithoutRegistryChange, &self.spec)?;
state.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, &self.spec)?; state.build_committee_cache(RelativeEpoch::NextWithRegistryChange, &self.spec)?;
per_slot_processing(&mut *state, &self.spec)?; per_slot_processing(&mut *state, &self.spec)?;
} }
@ -439,7 +439,7 @@ where
pub fn block_proposer(&self, slot: Slot) -> Result<usize, BeaconStateError> { pub fn block_proposer(&self, slot: Slot) -> Result<usize, BeaconStateError> {
self.state self.state
.write() .write()
.build_epoch_cache(RelativeEpoch::Current, &self.spec)?; .build_committee_cache(RelativeEpoch::Current, &self.spec)?;
let index = self.state.read().get_beacon_proposer_index( let index = self.state.read().get_beacon_proposer_index(
slot, slot,
@ -669,7 +669,7 @@ where
let mut state = self.state.read().clone(); let mut state = self.state.read().clone();
state.build_epoch_cache(RelativeEpoch::Current, &self.spec)?; state.build_committee_cache(RelativeEpoch::Current, &self.spec)?;
trace!("Finding attestations for new block..."); trace!("Finding attestations for new block...");

View File

@ -207,12 +207,12 @@ pub fn bench_block_processing(
let spec = initial_spec.clone(); let spec = initial_spec.clone();
c.bench( c.bench(
&format!("{}/block_processing", desc), &format!("{}/block_processing", desc),
Benchmark::new("build_previous_state_epoch_cache", move |b| { Benchmark::new("build_previous_state_committee_cache", move |b| {
b.iter_batched( b.iter_batched(
|| state.clone(), || state.clone(),
|mut state| { |mut state| {
state state
.build_epoch_cache(RelativeEpoch::Previous, &spec) .build_committee_cache(RelativeEpoch::Previous, &spec)
.unwrap(); .unwrap();
state state
}, },
@ -227,12 +227,12 @@ pub fn bench_block_processing(
let spec = initial_spec.clone(); let spec = initial_spec.clone();
c.bench( c.bench(
&format!("{}/block_processing", desc), &format!("{}/block_processing", desc),
Benchmark::new("build_current_state_epoch_cache", move |b| { Benchmark::new("build_current_state_committee_cache", move |b| {
b.iter_batched( b.iter_batched(
|| state.clone(), || state.clone(),
|mut state| { |mut state| {
state state
.build_epoch_cache(RelativeEpoch::Current, &spec) .build_committee_cache(RelativeEpoch::Current, &spec)
.unwrap(); .unwrap();
state state
}, },

View File

@ -30,7 +30,7 @@ pub fn get_genesis_state<T: EthSpec>(
} }
// Ensure the current epoch cache is built. // Ensure the current epoch cache is built.
state.build_epoch_cache(RelativeEpoch::Current, spec)?; state.build_committee_cache(RelativeEpoch::Current, spec)?;
// Set all the active index roots to be the genesis active index root. // Set all the active index roots to be the genesis active index root.
let active_validator_indices = state let active_validator_indices = state

View File

@ -79,8 +79,8 @@ fn per_block_processing_signature_optional<T: EthSpec>(
process_block_header(state, block, spec)?; process_block_header(state, block, spec)?;
// Ensure the current and previous epoch cache is built. // Ensure the current and previous epoch cache is built.
state.build_epoch_cache(RelativeEpoch::Previous, spec)?; state.build_committee_cache(RelativeEpoch::Previous, spec)?;
state.build_epoch_cache(RelativeEpoch::Current, spec)?; state.build_committee_cache(RelativeEpoch::Current, spec)?;
if should_verify_block_signature { if should_verify_block_signature {
verify_block_signature(&state, &block, &spec)?; verify_block_signature(&state, &block, &spec)?;
@ -315,7 +315,7 @@ pub fn process_attestations<T: EthSpec>(
); );
// Ensure the previous epoch cache exists. // Ensure the previous epoch cache exists.
state.build_epoch_cache(RelativeEpoch::Previous, spec)?; state.build_committee_cache(RelativeEpoch::Previous, spec)?;
// Verify attestations in parallel. // Verify attestations in parallel.
attestations attestations

View File

@ -34,8 +34,8 @@ pub fn per_epoch_processing<T: EthSpec>(
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(), Error> { ) -> Result<(), Error> {
// Ensure the previous and next epoch caches are built. // Ensure the previous and next epoch caches are built.
state.build_epoch_cache(RelativeEpoch::Previous, spec)?; state.build_committee_cache(RelativeEpoch::Previous, spec)?;
state.build_epoch_cache(RelativeEpoch::Current, spec)?; state.build_committee_cache(RelativeEpoch::Current, spec)?;
// Load the struct we use to assign validators into sets based on their participation. // Load the struct we use to assign validators into sets based on their participation.
// //

View File

@ -1,4 +1,6 @@
use self::epoch_cache::{get_active_validator_indices, EpochCache, Error as EpochCacheError}; use self::committee_cache::{
get_active_validator_indices, CommitteeCache, Error as CommitteeCacheError,
};
use self::exit_cache::ExitCache; use self::exit_cache::ExitCache;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use crate::*; use crate::*;
@ -18,7 +20,7 @@ use tree_hash_derive::{CachedTreeHash, TreeHash};
pub use beacon_state_types::*; pub use beacon_state_types::*;
mod beacon_state_types; mod beacon_state_types;
mod epoch_cache; mod committee_cache;
mod exit_cache; mod exit_cache;
mod pubkey_cache; mod pubkey_cache;
mod tests; mod tests;
@ -50,11 +52,11 @@ pub enum Error {
cache_len: usize, cache_len: usize,
registry_len: usize, registry_len: usize,
}, },
PreviousEpochCacheUninitialized, PreviousCommitteeCacheUninitialized,
CurrentEpochCacheUninitialized, CurrentCommitteeCacheUninitialized,
RelativeEpochError(RelativeEpochError), RelativeEpochError(RelativeEpochError),
EpochCacheUninitialized(RelativeEpoch), CommitteeCacheUninitialized(RelativeEpoch),
EpochCacheError(EpochCacheError), CommitteeCacheError(CommitteeCacheError),
TreeHashCacheError(TreeHashCacheError), TreeHashCacheError(TreeHashCacheError),
} }
@ -122,7 +124,7 @@ where
#[ssz(skip_deserializing)] #[ssz(skip_deserializing)]
#[tree_hash(skip_hashing)] #[tree_hash(skip_hashing)]
#[test_random(default)] #[test_random(default)]
pub epoch_caches: [EpochCache; CACHED_EPOCHS], pub committee_caches: [CommitteeCache; CACHED_EPOCHS],
#[serde(default)] #[serde(default)]
#[ssz(skip_serializing)] #[ssz(skip_serializing)]
#[ssz(skip_deserializing)] #[ssz(skip_deserializing)]
@ -213,10 +215,10 @@ impl<T: EthSpec> BeaconState<T> {
/* /*
* Caching (not in spec) * Caching (not in spec)
*/ */
epoch_caches: [ committee_caches: [
EpochCache::default(), CommitteeCache::default(),
EpochCache::default(), CommitteeCache::default(),
EpochCache::default(), CommitteeCache::default(),
], ],
pubkey_cache: PubkeyCache::default(), pubkey_cache: PubkeyCache::default(),
tree_hash_cache: TreeHashCache::default(), tree_hash_cache: TreeHashCache::default(),
@ -732,9 +734,9 @@ impl<T: EthSpec> BeaconState<T> {
/// Build all the caches, if they need to be built. /// Build all the caches, if they need to be built.
pub fn build_all_caches(&mut self, spec: &ChainSpec) -> Result<(), Error> { pub fn build_all_caches(&mut self, spec: &ChainSpec) -> Result<(), Error> {
self.build_epoch_cache(RelativeEpoch::Previous, spec)?; self.build_committee_cache(RelativeEpoch::Previous, spec)?;
self.build_epoch_cache(RelativeEpoch::Current, spec)?; self.build_committee_cache(RelativeEpoch::Current, spec)?;
self.build_epoch_cache(RelativeEpoch::Next, spec)?; self.build_committee_cache(RelativeEpoch::Next, spec)?;
self.update_pubkey_cache()?; self.update_pubkey_cache()?;
self.update_tree_hash_cache()?; self.update_tree_hash_cache()?;
self.exit_cache self.exit_cache
@ -744,30 +746,30 @@ impl<T: EthSpec> BeaconState<T> {
} }
/// Build an epoch cache, unless it is has already been built. /// Build an epoch cache, unless it is has already been built.
pub fn build_epoch_cache( pub fn build_committee_cache(
&mut self, &mut self,
relative_epoch: RelativeEpoch, relative_epoch: RelativeEpoch,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(), Error> { ) -> Result<(), Error> {
let i = Self::cache_index(relative_epoch); let i = Self::cache_index(relative_epoch);
if self.epoch_caches[i].is_initialized_at(self.previous_epoch()) { if self.committee_caches[i].is_initialized_at(self.previous_epoch()) {
Ok(()) Ok(())
} else { } else {
self.force_build_epoch_cache(relative_epoch, spec) self.force_build_committee_cache(relative_epoch, spec)
} }
} }
/// Always builds the previous epoch cache, even if it is already initialized. /// Always builds the previous epoch cache, even if it is already initialized.
pub fn force_build_epoch_cache( pub fn force_build_committee_cache(
&mut self, &mut self,
relative_epoch: RelativeEpoch, relative_epoch: RelativeEpoch,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(), Error> { ) -> Result<(), Error> {
let epoch = relative_epoch.into_epoch(self.current_epoch()); let epoch = relative_epoch.into_epoch(self.current_epoch());
self.epoch_caches[Self::cache_index(relative_epoch)] = self.committee_caches[Self::cache_index(relative_epoch)] =
EpochCache::initialized(&self, epoch, spec)?; CommitteeCache::initialized(&self, epoch, spec)?;
Ok(()) Ok(())
} }
@ -779,9 +781,9 @@ impl<T: EthSpec> BeaconState<T> {
pub fn advance_caches(&mut self) { pub fn advance_caches(&mut self) {
let next = Self::cache_index(RelativeEpoch::Previous); let next = Self::cache_index(RelativeEpoch::Previous);
let caches = &mut self.epoch_caches[..]; let caches = &mut self.committee_caches[..];
caches.rotate_left(1); caches.rotate_left(1);
caches[next] = EpochCache::default(); caches[next] = CommitteeCache::default();
} }
fn cache_index(relative_epoch: RelativeEpoch) -> usize { fn cache_index(relative_epoch: RelativeEpoch) -> usize {
@ -794,22 +796,22 @@ impl<T: EthSpec> BeaconState<T> {
/// Returns the cache for some `RelativeEpoch`. Returns an error if the cache has not been /// Returns the cache for some `RelativeEpoch`. Returns an error if the cache has not been
/// initialized. /// initialized.
fn cache(&self, relative_epoch: RelativeEpoch) -> Result<&EpochCache, Error> { fn cache(&self, relative_epoch: RelativeEpoch) -> Result<&CommitteeCache, Error> {
let cache = &self.epoch_caches[Self::cache_index(relative_epoch)]; let cache = &self.committee_caches[Self::cache_index(relative_epoch)];
if cache.is_initialized_at(relative_epoch.into_epoch(self.current_epoch())) { if cache.is_initialized_at(relative_epoch.into_epoch(self.current_epoch())) {
Ok(cache) Ok(cache)
} else { } else {
Err(Error::EpochCacheUninitialized(relative_epoch)) Err(Error::CommitteeCacheUninitialized(relative_epoch))
} }
} }
/// Drops the cache, leaving it in an uninitialized state. /// Drops the cache, leaving it in an uninitialized state.
fn drop_cache(&mut self, relative_epoch: RelativeEpoch) { fn drop_cache(&mut self, relative_epoch: RelativeEpoch) {
self.epoch_caches[Self::cache_index(relative_epoch)] = EpochCache::default(); self.committee_caches[Self::cache_index(relative_epoch)] = CommitteeCache::default();
} }
// FIXME(sproul): drop_previous/current_epoch_cache // FIXME(sproul): drop_previous/current_committee_cache
/// Updates the pubkey cache, if required. /// Updates the pubkey cache, if required.
/// ///
@ -870,9 +872,9 @@ impl<T: EthSpec> BeaconState<T> {
} }
} }
impl From<EpochCacheError> for Error { impl From<CommitteeCacheError> for Error {
fn from(e: EpochCacheError) -> Error { fn from(e: CommitteeCacheError) -> Error {
Error::EpochCacheError(e) Error::CommitteeCacheError(e)
} }
} }

View File

@ -14,7 +14,7 @@ pub enum Error {
mod tests; mod tests;
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)] #[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)]
pub struct EpochCache { pub struct CommitteeCache {
/// `Some(epoch)` if the cache is initialized, where `epoch` is the cache it holds. /// `Some(epoch)` if the cache is initialized, where `epoch` is the cache it holds.
initialized_epoch: Option<Epoch>, initialized_epoch: Option<Epoch>,
shuffling_start_shard: u64, shuffling_start_shard: u64,
@ -26,13 +26,13 @@ pub struct EpochCache {
pub attestation_duties: Vec<Option<AttestationDuty>>, pub attestation_duties: Vec<Option<AttestationDuty>>,
} }
impl EpochCache { impl CommitteeCache {
/// Return a new, fully initialized cache. /// Return a new, fully initialized cache.
pub fn initialized<T: EthSpec>( pub fn initialized<T: EthSpec>(
state: &BeaconState<T>, state: &BeaconState<T>,
epoch: Epoch, epoch: Epoch,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<EpochCache, BeaconStateError> { ) -> Result<CommitteeCache, BeaconStateError> {
let relative_epoch = RelativeEpoch::from_epoch(state.current_epoch(), epoch) let relative_epoch = RelativeEpoch::from_epoch(state.current_epoch(), epoch)
.map_err(|_| BeaconStateError::EpochOutOfBounds)?; .map_err(|_| BeaconStateError::EpochOutOfBounds)?;
@ -74,7 +74,7 @@ impl EpochCache {
) )
.ok_or_else(|| Error::UnableToShuffle)?; .ok_or_else(|| Error::UnableToShuffle)?;
let mut cache = EpochCache { let mut cache = CommitteeCache {
initialized_epoch: Some(epoch), initialized_epoch: Some(epoch),
shuffling_start_shard, shuffling_start_shard,
shuffling, shuffling,

View File

@ -23,7 +23,7 @@ fn fails_without_validators() {
let spec = &FewValidatorsEthSpec::spec(); let spec = &FewValidatorsEthSpec::spec();
assert_eq!( assert_eq!(
EpochCache::initialized(&state, state.current_epoch(), &spec), CommitteeCache::initialized(&state, state.current_epoch(), &spec),
Err(BeaconStateError::InsufficientValidators) Err(BeaconStateError::InsufficientValidators)
); );
} }
@ -33,16 +33,16 @@ fn initializes_with_the_right_epoch() {
let state = new_state::<FewValidatorsEthSpec>(16, Slot::new(0)); let state = new_state::<FewValidatorsEthSpec>(16, Slot::new(0));
let spec = &FewValidatorsEthSpec::spec(); let spec = &FewValidatorsEthSpec::spec();
let cache = EpochCache::default(); let cache = CommitteeCache::default();
assert_eq!(cache.initialized_epoch, None); assert_eq!(cache.initialized_epoch, None);
let cache = EpochCache::initialized(&state, state.current_epoch(), &spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.current_epoch(), &spec).unwrap();
assert_eq!(cache.initialized_epoch, Some(state.current_epoch())); assert_eq!(cache.initialized_epoch, Some(state.current_epoch()));
let cache = EpochCache::initialized(&state, state.previous_epoch(), &spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.previous_epoch(), &spec).unwrap();
assert_eq!(cache.initialized_epoch, Some(state.previous_epoch())); assert_eq!(cache.initialized_epoch, Some(state.previous_epoch()));
let cache = EpochCache::initialized(&state, state.next_epoch(), &spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.next_epoch(), &spec).unwrap();
assert_eq!(cache.initialized_epoch, Some(state.next_epoch())); assert_eq!(cache.initialized_epoch, Some(state.next_epoch()));
} }
@ -78,13 +78,13 @@ fn shuffles_for_the_right_epoch() {
.unwrap() .unwrap()
}; };
let cache = EpochCache::initialized(&state, state.current_epoch(), spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.current_epoch(), spec).unwrap();
assert_eq!(cache.shuffling, shuffling_with_seed(current_seed)); assert_eq!(cache.shuffling, shuffling_with_seed(current_seed));
let cache = EpochCache::initialized(&state, state.previous_epoch(), spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.previous_epoch(), spec).unwrap();
assert_eq!(cache.shuffling, shuffling_with_seed(previous_seed)); assert_eq!(cache.shuffling, shuffling_with_seed(previous_seed));
let cache = EpochCache::initialized(&state, state.next_epoch(), spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.next_epoch(), spec).unwrap();
assert_eq!(cache.shuffling, shuffling_with_seed(next_seed)); assert_eq!(cache.shuffling, shuffling_with_seed(next_seed));
} }
@ -100,13 +100,13 @@ fn can_start_on_any_shard() {
for i in 0..FewValidatorsEthSpec::shard_count() as u64 { for i in 0..FewValidatorsEthSpec::shard_count() as u64 {
state.latest_start_shard = i; state.latest_start_shard = i;
let cache = EpochCache::initialized(&state, state.current_epoch(), spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.current_epoch(), spec).unwrap();
assert_eq!(cache.shuffling_start_shard, i); assert_eq!(cache.shuffling_start_shard, i);
let cache = EpochCache::initialized(&state, state.previous_epoch(), spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.previous_epoch(), spec).unwrap();
assert_eq!(cache.shuffling_start_shard, i); assert_eq!(cache.shuffling_start_shard, i);
let cache = EpochCache::initialized(&state, state.next_epoch(), spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.next_epoch(), spec).unwrap();
assert_eq!(cache.shuffling_start_shard, i); assert_eq!(cache.shuffling_start_shard, i);
} }
} }
@ -195,16 +195,16 @@ fn starts_on_the_correct_shard() {
for i in 0..ExcessShardsEthSpec::shard_count() { for i in 0..ExcessShardsEthSpec::shard_count() {
state.latest_start_shard = i as u64; state.latest_start_shard = i as u64;
let cache = EpochCache::initialized(&state, state.current_epoch(), spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.current_epoch(), spec).unwrap();
assert_eq!(cache.shuffling_start_shard as usize, i); assert_eq!(cache.shuffling_start_shard as usize, i);
let cache = EpochCache::initialized(&state, state.previous_epoch(), spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.previous_epoch(), spec).unwrap();
assert_eq!( assert_eq!(
cache.shuffling_start_shard as usize, cache.shuffling_start_shard as usize,
(i + shard_count - previous_shards) % shard_count (i + shard_count - previous_shards) % shard_count
); );
let cache = EpochCache::initialized(&state, state.next_epoch(), spec).unwrap(); let cache = CommitteeCache::initialized(&state, state.next_epoch(), spec).unwrap();
assert_eq!( assert_eq!(
cache.shuffling_start_shard as usize, cache.shuffling_start_shard as usize,
(i + current_shards) % shard_count (i + current_shards) % shard_count

View File

@ -89,11 +89,13 @@ fn test_cache_initialization<'a, T: EthSpec>(
// Assuming the cache isn't already built, assert that a call to a cache-using function fails. // Assuming the cache isn't already built, assert that a call to a cache-using function fails.
assert_eq!( assert_eq!(
state.get_attestation_duties(0, relative_epoch), state.get_attestation_duties(0, relative_epoch),
Err(BeaconStateError::EpochCacheUninitialized(relative_epoch)) Err(BeaconStateError::CommitteeCacheUninitialized(
relative_epoch
))
); );
// Build the cache. // Build the cache.
state.build_epoch_cache(relative_epoch, spec).unwrap(); state.build_committee_cache(relative_epoch, spec).unwrap();
// Assert a call to a cache-using function passes. // Assert a call to a cache-using function passes.
let _ = state let _ = state
@ -106,7 +108,9 @@ fn test_cache_initialization<'a, T: EthSpec>(
// Assert a call to a cache-using function fail. // Assert a call to a cache-using function fail.
assert_eq!( assert_eq!(
state.get_beacon_proposer_index(slot, relative_epoch, spec), state.get_beacon_proposer_index(slot, relative_epoch, spec),
Err(BeaconStateError::EpochCacheUninitialized(relative_epoch)) Err(BeaconStateError::CommitteeCacheUninitialized(
relative_epoch
))
); );
} }
@ -256,12 +260,14 @@ mod committees {
state.latest_randao_mixes = FixedLenVec::from(distinct_hashes); state.latest_randao_mixes = FixedLenVec::from(distinct_hashes);
state state
.build_epoch_cache(RelativeEpoch::Previous, spec) .build_committee_cache(RelativeEpoch::Previous, spec)
.unwrap(); .unwrap();
state state
.build_epoch_cache(RelativeEpoch::Current, spec) .build_committee_cache(RelativeEpoch::Current, spec)
.unwrap();
state
.build_committee_cache(RelativeEpoch::Next, spec)
.unwrap(); .unwrap();
state.build_epoch_cache(RelativeEpoch::Next, spec).unwrap();
let cache_epoch = cache_epoch.into_epoch(state_epoch); let cache_epoch = cache_epoch.into_epoch(state_epoch);

View File

@ -205,10 +205,10 @@ impl<T: EthSpec> TestingBeaconStateBuilder<T> {
let state = &mut self.state; let state = &mut self.state;
state state
.build_epoch_cache(RelativeEpoch::Previous, spec) .build_committee_cache(RelativeEpoch::Previous, spec)
.unwrap(); .unwrap();
state state
.build_epoch_cache(RelativeEpoch::Current, spec) .build_committee_cache(RelativeEpoch::Current, spec)
.unwrap(); .unwrap();
let current_epoch = state.current_epoch(); let current_epoch = state.current_epoch();