Move beacon state fns to be RelativeEpoch
native
This commit is contained in:
parent
03849de319
commit
29a3e0c868
@ -50,6 +50,7 @@ pub enum Error {
|
|||||||
},
|
},
|
||||||
PreviousEpochCacheUninitialized,
|
PreviousEpochCacheUninitialized,
|
||||||
CurrentEpochCacheUnintialized,
|
CurrentEpochCacheUnintialized,
|
||||||
|
RelativeEpochError(RelativeEpochError),
|
||||||
EpochCacheUnintialized(RelativeEpoch),
|
EpochCacheUnintialized(RelativeEpoch),
|
||||||
EpochCacheError(EpochCacheError),
|
EpochCacheError(EpochCacheError),
|
||||||
TreeHashCacheError(TreeHashCacheError),
|
TreeHashCacheError(TreeHashCacheError),
|
||||||
@ -284,11 +285,11 @@ impl<T: EthSpec> BeaconState<T> {
|
|||||||
///
|
///
|
||||||
/// Spec v0.6.1
|
/// Spec v0.6.1
|
||||||
pub fn get_attestation_slot(&self, attestation_data: &AttestationData) -> Result<Slot, Error> {
|
pub fn get_attestation_slot(&self, attestation_data: &AttestationData) -> Result<Slot, Error> {
|
||||||
|
let target_epoch =
|
||||||
|
RelativeEpoch::from_epoch(self.current_epoch(), attestation_data.target_epoch)?;
|
||||||
|
|
||||||
let cc = self
|
let cc = self
|
||||||
.get_crosslink_committee_for_shard(
|
.get_crosslink_committee_for_shard(attestation_data.shard, target_epoch)?
|
||||||
attestation_data.shard,
|
|
||||||
attestation_data.target_epoch,
|
|
||||||
)?
|
|
||||||
.ok_or_else(|| Error::NoCommitteeForShard)?;
|
.ok_or_else(|| Error::NoCommitteeForShard)?;
|
||||||
Ok(cc.slot)
|
Ok(cc.slot)
|
||||||
}
|
}
|
||||||
@ -298,8 +299,11 @@ impl<T: EthSpec> BeaconState<T> {
|
|||||||
/// Note: the indices are shuffled (i.e., not in ascending order).
|
/// Note: the indices are shuffled (i.e., not in ascending order).
|
||||||
///
|
///
|
||||||
/// Returns an error if that epoch is not cached, or the cache is not initialized.
|
/// Returns an error if that epoch is not cached, or the cache is not initialized.
|
||||||
pub fn get_cached_active_validator_indices(&self, epoch: Epoch) -> Result<&[usize], Error> {
|
pub fn get_cached_active_validator_indices(
|
||||||
let cache = self.cache(epoch)?;
|
&self,
|
||||||
|
relative_epoch: RelativeEpoch,
|
||||||
|
) -> Result<&[usize], Error> {
|
||||||
|
let cache = self.cache(relative_epoch)?;
|
||||||
|
|
||||||
Ok(&cache.active_validator_indices())
|
Ok(&cache.active_validator_indices())
|
||||||
}
|
}
|
||||||
@ -334,9 +338,9 @@ impl<T: EthSpec> BeaconState<T> {
|
|||||||
pub fn get_crosslink_committee_for_shard(
|
pub fn get_crosslink_committee_for_shard(
|
||||||
&self,
|
&self,
|
||||||
shard: u64,
|
shard: u64,
|
||||||
epoch: Epoch,
|
relative_epoch: RelativeEpoch,
|
||||||
) -> Result<Option<CrosslinkCommittee>, Error> {
|
) -> Result<Option<CrosslinkCommittee>, Error> {
|
||||||
let cache = self.cache(epoch)?;
|
let cache = self.cache(relative_epoch)?;
|
||||||
|
|
||||||
Ok(cache.get_crosslink_committee_for_shard(shard))
|
Ok(cache.get_crosslink_committee_for_shard(shard))
|
||||||
}
|
}
|
||||||
@ -662,7 +666,7 @@ impl<T: EthSpec> BeaconState<T> {
|
|||||||
pub fn get_churn_limit(&self, spec: &ChainSpec) -> Result<u64, Error> {
|
pub fn get_churn_limit(&self, spec: &ChainSpec) -> Result<u64, Error> {
|
||||||
Ok(std::cmp::max(
|
Ok(std::cmp::max(
|
||||||
spec.min_per_epoch_churn_limit,
|
spec.min_per_epoch_churn_limit,
|
||||||
self.cache(self.current_epoch())?.active_validator_count() as u64
|
self.cache(RelativeEpoch::Current)?.active_validator_count() as u64
|
||||||
/ spec.churn_limit_quotient,
|
/ spec.churn_limit_quotient,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -679,7 +683,7 @@ impl<T: EthSpec> BeaconState<T> {
|
|||||||
&self,
|
&self,
|
||||||
validator_index: usize,
|
validator_index: usize,
|
||||||
) -> Result<&Option<AttestationDuty>, Error> {
|
) -> Result<&Option<AttestationDuty>, Error> {
|
||||||
let cache = self.cache(self.current_epoch())?;
|
let cache = self.cache(RelativeEpoch::Current)?;
|
||||||
|
|
||||||
Ok(cache
|
Ok(cache
|
||||||
.attestation_duties
|
.attestation_duties
|
||||||
@ -765,13 +769,10 @@ 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, epoch: Epoch) -> Result<&EpochCache, Error> {
|
fn cache(&self, relative_epoch: RelativeEpoch) -> Result<&EpochCache, Error> {
|
||||||
let relative_epoch = RelativeEpoch::from_epoch(self.current_epoch(), epoch)
|
|
||||||
.map_err(|_| Error::EpochOutOfBounds)?;
|
|
||||||
|
|
||||||
let cache = &self.epoch_caches[Self::cache_index(relative_epoch)];
|
let cache = &self.epoch_caches[Self::cache_index(relative_epoch)];
|
||||||
|
|
||||||
if cache.is_initialized_at(epoch) {
|
if cache.is_initialized_at(relative_epoch.into_epoch(self.current_epoch())) {
|
||||||
Ok(cache)
|
Ok(cache)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::EpochCacheUnintialized(relative_epoch))
|
Err(Error::EpochCacheUnintialized(relative_epoch))
|
||||||
@ -845,6 +846,12 @@ impl From<EpochCacheError> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<RelativeEpochError> for Error {
|
||||||
|
fn from(e: RelativeEpochError) -> Error {
|
||||||
|
Error::RelativeEpochError(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<TreeHashCacheError> for Error {
|
impl From<TreeHashCacheError> for Error {
|
||||||
fn from(e: TreeHashCacheError) -> Error {
|
fn from(e: TreeHashCacheError) -> Error {
|
||||||
Error::TreeHashCacheError(e)
|
Error::TreeHashCacheError(e)
|
||||||
|
@ -14,9 +14,10 @@ fn execute_sane_cache_test<T: EthSpec>(
|
|||||||
let active_indices: Vec<usize> = (0..validator_count).collect();
|
let active_indices: Vec<usize> = (0..validator_count).collect();
|
||||||
let seed = state.generate_seed(epoch, spec).unwrap();
|
let seed = state.generate_seed(epoch, spec).unwrap();
|
||||||
let start_shard = 0;
|
let start_shard = 0;
|
||||||
|
let relative_epoch = RelativeEpoch::from_epoch(state.current_epoch(), epoch).unwrap();
|
||||||
|
|
||||||
let mut ordered_indices = state
|
let mut ordered_indices = state
|
||||||
.get_cached_active_validator_indices(epoch)
|
.get_cached_active_validator_indices(relative_epoch)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_vec();
|
.to_vec();
|
||||||
ordered_indices.sort_unstable();
|
ordered_indices.sort_unstable();
|
||||||
@ -34,7 +35,7 @@ fn execute_sane_cache_test<T: EthSpec>(
|
|||||||
let shard = (i + start_shard as usize) % T::shard_count();
|
let shard = (i + start_shard as usize) % T::shard_count();
|
||||||
|
|
||||||
let c = state
|
let c = state
|
||||||
.get_crosslink_committee_for_shard(shard as u64, epoch)
|
.get_crosslink_committee_for_shard(shard as u64, relative_epoch)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user