spec v0.6.1: update per-epoch processing

This commit is contained in:
Michael Sproul 2019-05-21 11:47:52 +10:00
parent d3d2900a6a
commit 56424d94c6
No known key found for this signature in database
GPG Key ID: 77B1309D2E54E914
4 changed files with 13 additions and 35 deletions

View File

@ -1,22 +0,0 @@
use types::{BeaconStateError as Error, *};
/// Exit the validator of the given `index`.
///
/// Spec v0.5.1
pub fn exit_validator<T: EthSpec>(
state: &mut BeaconState<T>,
validator_index: usize,
spec: &ChainSpec,
) -> Result<(), Error> {
if validator_index >= state.validator_registry.len() {
return Err(Error::UnknownValidator);
}
let delayed_epoch = state.get_delayed_activation_exit_epoch(state.current_epoch(), spec);
if state.validator_registry[validator_index].exit_epoch > delayed_epoch {
state.validator_registry[validator_index].exit_epoch = delayed_epoch;
}
Ok(())
}

View File

@ -161,7 +161,8 @@ pub fn process_crosslinks<T: EthSpec>(
for relative_epoch in vec![RelativeEpoch::Previous, RelativeEpoch::Current] {
let epoch = relative_epoch.into_epoch(state.current_epoch());
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 shard =
(state.get_epoch_start_shard(relative_epoch)? + offset) % T::ShardCount::to_u64();
let crosslink_committee =
state.get_crosslink_committee_for_shard(shard, relative_epoch)?;

View File

@ -248,7 +248,7 @@ impl ValidatorStatuses {
status.is_previous_epoch_target_attester = true;
}
if has_common_beacon_block_root(a, state, spec)? {
if has_common_beacon_block_root(a, state)? {
status.is_previous_epoch_head_attester = true;
}
}
@ -298,8 +298,7 @@ impl ValidatorStatuses {
) -> Result<(), BeaconStateError> {
// Loop through each slot in the previous epoch.
for slot in state.previous_epoch().slot_iter(spec.slots_per_epoch) {
let crosslink_committees_at_slot =
state.get_crosslink_committees_at_slot(slot, spec)?;
let crosslink_committees_at_slot = state.get_crosslink_committees_at_slot(slot)?;
// Loop through each committee in the slot.
for c in crosslink_committees_at_slot {
@ -352,7 +351,6 @@ fn target_matches_epoch_start_block<T: EthSpec>(
fn has_common_beacon_block_root<T: EthSpec>(
a: &PendingAttestation,
state: &BeaconState<T>,
spec: &ChainSpec,
) -> Result<bool, BeaconStateError> {
let attestation_slot = state.get_attestation_slot(&a.data)?;
let state_block_root = *state.get_block_root(attestation_slot)?;

View File

@ -47,15 +47,16 @@ pub fn winning_root<T: EthSpec>(
.filter(|a| a.data.shard == shard)
.collect();
let shard_crosslinks = shard_attestations.iter().map(|att| {
(
let mut shard_crosslinks = Vec::with_capacity(shard_attestations.len());
for att in shard_attestations {
shard_crosslinks.push((
att,
state.get_crosslink_from_attestation_data(&att.data, spec),
)
});
state.get_crosslink_from_attestation_data(&att.data, spec)?,
));
}
let current_shard_crosslink_root = state.current_crosslinks[shard as usize].tree_hash_root();
let candidate_crosslinks = shard_crosslinks.filter(|(_, c)| {
let current_shard_crosslink_root = state.get_current_crosslink(shard)?.tree_hash_root();
let candidate_crosslinks = shard_crosslinks.into_iter().filter(|(_, c)| {
c.previous_crosslink_root.as_bytes() == &current_shard_crosslink_root[..]
|| c.tree_hash_root() == current_shard_crosslink_root
});
@ -63,7 +64,7 @@ pub fn winning_root<T: EthSpec>(
// Build a map from candidate crosslink to attestations that support that crosslink.
let mut candidate_crosslink_map: HashMap<Crosslink, Vec<&PendingAttestation>> = HashMap::new();
for (&attestation, crosslink) in candidate_crosslinks {
for (attestation, crosslink) in candidate_crosslinks {
let supporting_attestations = candidate_crosslink_map
.entry(crosslink)
.or_insert_with(Vec::new);