Make #stateprocessingcompileagain

This commit is contained in:
Paul Hauner 2019-05-19 16:56:39 +10:00
parent 29a3e0c868
commit ea96d24420
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
6 changed files with 89 additions and 35 deletions

View File

@ -159,9 +159,10 @@ pub fn process_crosslinks<T: EthSpec>(
state.previous_crosslinks = state.current_crosslinks.clone(); state.previous_crosslinks = state.current_crosslinks.clone();
for epoch in vec![state.previous_epoch(), state.current_epoch()] { for relative_epoch in vec![RelativeEpoch::Previous, RelativeEpoch::Current] {
for offset in 0..state.get_epoch_committee_count(epoch, spec) { let epoch = relative_epoch.into_epoch(state.current_epoch());
let shard = (state.get_epoch_start_shard(epoch, spec) + offset) % spec.shard_count; for offset in 0..state.get_epoch_committee_count(relative_epoch)? {
let shard = (state.get_epoch_start_shard(relative_epoch)? + offset) % spec.shard_count;
let crosslink_committee = state.get_crosslink_committee(epoch, shard, spec)?; let crosslink_committee = state.get_crosslink_committee(epoch, shard, spec)?;
let winning_root = winning_root(state, shard, epoch, spec)?; let winning_root = winning_root(state, shard, epoch, spec)?;
@ -211,9 +212,7 @@ pub fn process_final_updates<T: EthSpec>(
} }
// Update start shard. // Update start shard.
state.latest_start_shard = (state.latest_start_shard state.latest_start_shard = state.next_epoch_start_shard()?;
+ state.get_shard_delta(current_epoch, spec))
% T::ShardCount::to_u64();
// This is a hack to allow us to update index roots and slashed balances for the next epoch. // This is a hack to allow us to update index roots and slashed balances for the next epoch.
// //

View File

@ -1,5 +1,5 @@
#![cfg(test)] #![cfg(test)]
use crate::per_epoch_processing; use crate::per_epoch_processing::per_epoch_processing;
use env_logger::{Builder, Env}; use env_logger::{Builder, Env};
use types::test_utils::TestingBeaconStateBuilder; use types::test_utils::TestingBeaconStateBuilder;
use types::*; use types::*;

View File

@ -232,7 +232,8 @@ impl ValidatorStatuses {
// The inclusion slot and distance are only required for previous epoch attesters. // The inclusion slot and distance are only required for previous epoch attesters.
let attestation_slot = state.get_attestation_slot(&a.data)?; let attestation_slot = state.get_attestation_slot(&a.data)?;
let inclusion_slot = attestation_slot + a.inclusion_delay; let inclusion_slot = attestation_slot + a.inclusion_delay;
let relative_epoch = RelativeEpoch::from_slot(state.slot, inclusion_slot, spec)?; let relative_epoch =
RelativeEpoch::from_slot(state.slot, inclusion_slot, spec.slots_per_epoch)?;
status.inclusion_info = Some(InclusionInfo { status.inclusion_info = Some(InclusionInfo {
slot: inclusion_slot, slot: inclusion_slot,
distance: a.inclusion_delay, distance: a.inclusion_delay,

View File

@ -42,7 +42,7 @@ pub fn winning_root<T: EthSpec>(
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<Option<WinningRoot>, BeaconStateError> { ) -> Result<Option<WinningRoot>, BeaconStateError> {
let shard_attestations: Vec<&PendingAttestation> = state let shard_attestations: Vec<&PendingAttestation> = state
.get_matching_source_attestations(epoch, spec)? .get_matching_source_attestations(epoch)?
.iter() .iter()
.filter(|a| a.data.shard == shard) .filter(|a| a.data.shard == shard)
.collect(); .collect();

View File

@ -4,7 +4,7 @@ use crate::test_utils::TestRandom;
use crate::*; use crate::*;
use cached_tree_hash::{Error as TreeHashCacheError, TreeHashCache}; use cached_tree_hash::{Error as TreeHashCacheError, TreeHashCache};
use hashing::hash; use hashing::hash;
use int_to_bytes::int_to_bytes32; use int_to_bytes::{int_to_bytes32, int_to_bytes8};
use pubkey_cache::PubkeyCache; use pubkey_cache::PubkeyCache;
use fixed_len_vec::{typenum::Unsigned, FixedLenVec}; use fixed_len_vec::{typenum::Unsigned, FixedLenVec};
@ -24,6 +24,7 @@ mod pubkey_cache;
mod tests; mod tests;
pub const CACHED_EPOCHS: usize = 3; pub const CACHED_EPOCHS: usize = 3;
const MAX_RANDOM_BYTE: u64 = (1 << 8) - 1;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
@ -279,6 +280,27 @@ impl<T: EthSpec> BeaconState<T> {
self.current_epoch() + 1 self.current_epoch() + 1
} }
pub fn get_epoch_committee_count(&self, relative_epoch: RelativeEpoch) -> Result<u64, Error> {
let cache = self.cache(relative_epoch)?;
Ok(cache.epoch_committee_count() as u64)
}
pub fn get_epoch_start_shard(&self, relative_epoch: RelativeEpoch) -> Result<u64, Error> {
let cache = self.cache(relative_epoch)?;
Ok(cache.epoch_start_shard())
}
pub fn next_epoch_start_shard(&self) -> Result<u64, Error> {
let cache = self.cache(RelativeEpoch::Current)?;
Ok(
(cache.epoch_start_shard() + cache.epoch_committee_count() as u64)
& T::shard_count() as u64,
)
}
/// Get the slot of an attestation. /// Get the slot of an attestation.
/// ///
/// Note: Utilizes the cache and will fail if the appropriate cache is not initialized. /// Note: Utilizes the cache and will fail if the appropriate cache is not initialized.
@ -360,33 +382,41 @@ impl<T: EthSpec> BeaconState<T> {
unimplemented!() unimplemented!()
} }
/// Returns the beacon proposer index for the `slot`. /// Returns the beacon proposer index for the `slot` in the given `relative_epoch`.
/// ///
/// If the state does not contain an index for a beacon proposer at the requested `slot`, then `None` is returned. /// Spec v0.6.1
/// // NOTE: be sure to test this bad boy.
/// Spec v0.5.1 pub fn get_beacon_proposer_index(
pub fn get_beacon_proposer_index(&self, _spec: &ChainSpec) -> Result<usize, Error> { &self,
unimplemented!("FIXME(sproul)") slot: Slot,
/* relative_epoch: RelativeEpoch,
let cache = self.cache(relative_epoch, spec)?; spec: &ChainSpec,
) -> Result<usize, Error> {
let cache = self.cache(relative_epoch)?;
let epoch = relative_epoch.into_epoch(self.current_epoch());
let committees = cache let first_committee = cache
.get_crosslink_committees_at_slot(slot, spec) .first_committee_at_slot(slot)
.ok_or_else(|| Error::SlotOutOfBounds)?; .ok_or_else(|| Error::SlotOutOfBounds)?;
let seed = self.generate_seed(epoch, spec)?;
let epoch = slot.epoch(spec.slots_per_epoch); let mut i = 0;
Ok(loop {
committees let candidate_index = first_committee[(epoch.as_usize() + i) % first_committee.len()];
.first() let random_byte = {
.ok_or(Error::UnableToDetermineProducer) let mut preimage = seed.as_bytes().to_vec();
.and_then(|first| { preimage.append(&mut int_to_bytes8((i / 32) as u64));
let index = epoch let hash = hash(&preimage);
.as_usize() hash[i % 32]
.checked_rem(first.committee.len()) };
.ok_or(Error::UnableToDetermineProducer)?; let effective_balance = self.validator_registry[candidate_index].effective_balance;
Ok(first.committee[index]) if (effective_balance * MAX_RANDOM_BYTE)
>= (spec.max_effective_balance * random_byte as u64)
{
break candidate_index;
}
i += 1;
}) })
*/
} }
/// Safely obtains the index for latest block roots, given some `slot`. /// Safely obtains the index for latest block roots, given some `slot`.

View File

@ -128,7 +128,7 @@ impl EpochCache {
let committee_index = let committee_index =
(shard + self.shard_count - self.shuffling_start_shard) % self.shard_count; (shard + self.shard_count - self.shuffling_start_shard) % self.shard_count;
let committee = self.compute_committee(committee_index as usize, self.committee_count)?; let committee = self.compute_committee(committee_index as usize)?;
let slot = self.crosslink_slot_for_shard(shard)?; let slot = self.crosslink_slot_for_shard(shard)?;
Some(CrosslinkCommittee { Some(CrosslinkCommittee {
@ -142,12 +142,36 @@ impl EpochCache {
self.shuffling.len() self.shuffling.len()
} }
fn compute_committee(&self, index: usize, count: usize) -> Option<&[usize]> { pub fn epoch_committee_count(&self) -> usize {
self.committee_count
}
pub fn epoch_start_shard(&self) -> u64 {
self.shuffling_start_shard
}
pub fn first_committee_at_slot(&self, slot: Slot) -> Option<&[usize]> {
let position = self
.initialized_epoch?
.position(slot, self.slots_per_epoch)?;
if position > self.committee_count {
None
} else {
let committees_per_slot = self.committee_count / self.slots_per_epoch as usize;
let index = position * committees_per_slot;
self.compute_committee(index)
}
}
fn compute_committee(&self, index: usize) -> Option<&[usize]> {
if self.initialized_epoch.is_none() { if self.initialized_epoch.is_none() {
return None; return None;
} }
let num_validators = self.shuffling.len(); let num_validators = self.shuffling.len();
let count = self.committee_count;
// Note: `count != 0` is enforced in the constructor. // Note: `count != 0` is enforced in the constructor.
let start = (num_validators * index) / count; let start = (num_validators * index) / count;