mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-29 02:33:48 +00:00
Rename EpochCache to CommitteeCache
This commit is contained in:
@@ -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 crate::test_utils::TestRandom;
|
||||
use crate::*;
|
||||
@@ -18,7 +20,7 @@ use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
pub use beacon_state_types::*;
|
||||
|
||||
mod beacon_state_types;
|
||||
mod epoch_cache;
|
||||
mod committee_cache;
|
||||
mod exit_cache;
|
||||
mod pubkey_cache;
|
||||
mod tests;
|
||||
@@ -50,11 +52,11 @@ pub enum Error {
|
||||
cache_len: usize,
|
||||
registry_len: usize,
|
||||
},
|
||||
PreviousEpochCacheUninitialized,
|
||||
CurrentEpochCacheUninitialized,
|
||||
PreviousCommitteeCacheUninitialized,
|
||||
CurrentCommitteeCacheUninitialized,
|
||||
RelativeEpochError(RelativeEpochError),
|
||||
EpochCacheUninitialized(RelativeEpoch),
|
||||
EpochCacheError(EpochCacheError),
|
||||
CommitteeCacheUninitialized(RelativeEpoch),
|
||||
CommitteeCacheError(CommitteeCacheError),
|
||||
TreeHashCacheError(TreeHashCacheError),
|
||||
}
|
||||
|
||||
@@ -122,7 +124,7 @@ where
|
||||
#[ssz(skip_deserializing)]
|
||||
#[tree_hash(skip_hashing)]
|
||||
#[test_random(default)]
|
||||
pub epoch_caches: [EpochCache; CACHED_EPOCHS],
|
||||
pub committee_caches: [CommitteeCache; CACHED_EPOCHS],
|
||||
#[serde(default)]
|
||||
#[ssz(skip_serializing)]
|
||||
#[ssz(skip_deserializing)]
|
||||
@@ -213,10 +215,10 @@ impl<T: EthSpec> BeaconState<T> {
|
||||
/*
|
||||
* Caching (not in spec)
|
||||
*/
|
||||
epoch_caches: [
|
||||
EpochCache::default(),
|
||||
EpochCache::default(),
|
||||
EpochCache::default(),
|
||||
committee_caches: [
|
||||
CommitteeCache::default(),
|
||||
CommitteeCache::default(),
|
||||
CommitteeCache::default(),
|
||||
],
|
||||
pubkey_cache: PubkeyCache::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.
|
||||
pub fn build_all_caches(&mut self, spec: &ChainSpec) -> Result<(), Error> {
|
||||
self.build_epoch_cache(RelativeEpoch::Previous, spec)?;
|
||||
self.build_epoch_cache(RelativeEpoch::Current, spec)?;
|
||||
self.build_epoch_cache(RelativeEpoch::Next, spec)?;
|
||||
self.build_committee_cache(RelativeEpoch::Previous, spec)?;
|
||||
self.build_committee_cache(RelativeEpoch::Current, spec)?;
|
||||
self.build_committee_cache(RelativeEpoch::Next, spec)?;
|
||||
self.update_pubkey_cache()?;
|
||||
self.update_tree_hash_cache()?;
|
||||
self.exit_cache
|
||||
@@ -744,30 +746,30 @@ impl<T: EthSpec> BeaconState<T> {
|
||||
}
|
||||
|
||||
/// Build an epoch cache, unless it is has already been built.
|
||||
pub fn build_epoch_cache(
|
||||
pub fn build_committee_cache(
|
||||
&mut self,
|
||||
relative_epoch: RelativeEpoch,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
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(())
|
||||
} 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.
|
||||
pub fn force_build_epoch_cache(
|
||||
pub fn force_build_committee_cache(
|
||||
&mut self,
|
||||
relative_epoch: RelativeEpoch,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let epoch = relative_epoch.into_epoch(self.current_epoch());
|
||||
|
||||
self.epoch_caches[Self::cache_index(relative_epoch)] =
|
||||
EpochCache::initialized(&self, epoch, spec)?;
|
||||
self.committee_caches[Self::cache_index(relative_epoch)] =
|
||||
CommitteeCache::initialized(&self, epoch, spec)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -779,9 +781,9 @@ impl<T: EthSpec> BeaconState<T> {
|
||||
pub fn advance_caches(&mut self) {
|
||||
let next = Self::cache_index(RelativeEpoch::Previous);
|
||||
|
||||
let caches = &mut self.epoch_caches[..];
|
||||
let caches = &mut self.committee_caches[..];
|
||||
caches.rotate_left(1);
|
||||
caches[next] = EpochCache::default();
|
||||
caches[next] = CommitteeCache::default();
|
||||
}
|
||||
|
||||
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
|
||||
/// initialized.
|
||||
fn cache(&self, relative_epoch: RelativeEpoch) -> Result<&EpochCache, Error> {
|
||||
let cache = &self.epoch_caches[Self::cache_index(relative_epoch)];
|
||||
fn cache(&self, relative_epoch: RelativeEpoch) -> Result<&CommitteeCache, Error> {
|
||||
let cache = &self.committee_caches[Self::cache_index(relative_epoch)];
|
||||
|
||||
if cache.is_initialized_at(relative_epoch.into_epoch(self.current_epoch())) {
|
||||
Ok(cache)
|
||||
} else {
|
||||
Err(Error::EpochCacheUninitialized(relative_epoch))
|
||||
Err(Error::CommitteeCacheUninitialized(relative_epoch))
|
||||
}
|
||||
}
|
||||
|
||||
/// Drops the cache, leaving it in an uninitialized state.
|
||||
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.
|
||||
///
|
||||
@@ -870,9 +872,9 @@ impl<T: EthSpec> BeaconState<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EpochCacheError> for Error {
|
||||
fn from(e: EpochCacheError) -> Error {
|
||||
Error::EpochCacheError(e)
|
||||
impl From<CommitteeCacheError> for Error {
|
||||
fn from(e: CommitteeCacheError) -> Error {
|
||||
Error::CommitteeCacheError(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ pub enum Error {
|
||||
mod tests;
|
||||
|
||||
#[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.
|
||||
initialized_epoch: Option<Epoch>,
|
||||
shuffling_start_shard: u64,
|
||||
@@ -26,13 +26,13 @@ pub struct EpochCache {
|
||||
pub attestation_duties: Vec<Option<AttestationDuty>>,
|
||||
}
|
||||
|
||||
impl EpochCache {
|
||||
impl CommitteeCache {
|
||||
/// Return a new, fully initialized cache.
|
||||
pub fn initialized<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
epoch: Epoch,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<EpochCache, BeaconStateError> {
|
||||
) -> Result<CommitteeCache, BeaconStateError> {
|
||||
let relative_epoch = RelativeEpoch::from_epoch(state.current_epoch(), epoch)
|
||||
.map_err(|_| BeaconStateError::EpochOutOfBounds)?;
|
||||
|
||||
@@ -74,7 +74,7 @@ impl EpochCache {
|
||||
)
|
||||
.ok_or_else(|| Error::UnableToShuffle)?;
|
||||
|
||||
let mut cache = EpochCache {
|
||||
let mut cache = CommitteeCache {
|
||||
initialized_epoch: Some(epoch),
|
||||
shuffling_start_shard,
|
||||
shuffling,
|
||||
@@ -23,7 +23,7 @@ fn fails_without_validators() {
|
||||
let spec = &FewValidatorsEthSpec::spec();
|
||||
|
||||
assert_eq!(
|
||||
EpochCache::initialized(&state, state.current_epoch(), &spec),
|
||||
CommitteeCache::initialized(&state, state.current_epoch(), &spec),
|
||||
Err(BeaconStateError::InsufficientValidators)
|
||||
);
|
||||
}
|
||||
@@ -33,16 +33,16 @@ fn initializes_with_the_right_epoch() {
|
||||
let state = new_state::<FewValidatorsEthSpec>(16, Slot::new(0));
|
||||
let spec = &FewValidatorsEthSpec::spec();
|
||||
|
||||
let cache = EpochCache::default();
|
||||
let cache = CommitteeCache::default();
|
||||
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()));
|
||||
|
||||
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()));
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
@@ -78,13 +78,13 @@ fn shuffles_for_the_right_epoch() {
|
||||
.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));
|
||||
|
||||
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));
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -100,13 +100,13 @@ fn can_start_on_any_shard() {
|
||||
for i in 0..FewValidatorsEthSpec::shard_count() as u64 {
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -195,16 +195,16 @@ fn starts_on_the_correct_shard() {
|
||||
for i in 0..ExcessShardsEthSpec::shard_count() {
|
||||
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);
|
||||
|
||||
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 as usize,
|
||||
(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!(
|
||||
cache.shuffling_start_shard as usize,
|
||||
(i + current_shards) % shard_count
|
||||
@@ -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.
|
||||
assert_eq!(
|
||||
state.get_attestation_duties(0, relative_epoch),
|
||||
Err(BeaconStateError::EpochCacheUninitialized(relative_epoch))
|
||||
Err(BeaconStateError::CommitteeCacheUninitialized(
|
||||
relative_epoch
|
||||
))
|
||||
);
|
||||
|
||||
// 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.
|
||||
let _ = state
|
||||
@@ -106,7 +108,9 @@ fn test_cache_initialization<'a, T: EthSpec>(
|
||||
// Assert a call to a cache-using function fail.
|
||||
assert_eq!(
|
||||
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
|
||||
.build_epoch_cache(RelativeEpoch::Previous, spec)
|
||||
.build_committee_cache(RelativeEpoch::Previous, spec)
|
||||
.unwrap();
|
||||
state
|
||||
.build_epoch_cache(RelativeEpoch::Current, spec)
|
||||
.build_committee_cache(RelativeEpoch::Current, spec)
|
||||
.unwrap();
|
||||
state
|
||||
.build_committee_cache(RelativeEpoch::Next, spec)
|
||||
.unwrap();
|
||||
state.build_epoch_cache(RelativeEpoch::Next, spec).unwrap();
|
||||
|
||||
let cache_epoch = cache_epoch.into_epoch(state_epoch);
|
||||
|
||||
|
||||
@@ -205,10 +205,10 @@ impl<T: EthSpec> TestingBeaconStateBuilder<T> {
|
||||
let state = &mut self.state;
|
||||
|
||||
state
|
||||
.build_epoch_cache(RelativeEpoch::Previous, spec)
|
||||
.build_committee_cache(RelativeEpoch::Previous, spec)
|
||||
.unwrap();
|
||||
state
|
||||
.build_epoch_cache(RelativeEpoch::Current, spec)
|
||||
.build_committee_cache(RelativeEpoch::Current, spec)
|
||||
.unwrap();
|
||||
|
||||
let current_epoch = state.current_epoch();
|
||||
|
||||
Reference in New Issue
Block a user