From 56424d94c6fb31b0100708ea1a6ba2ff87252cbe Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 21 May 2019 11:47:52 +1000 Subject: [PATCH] spec v0.6.1: update per-epoch processing --- .../src/common/exit_validator.rs | 22 ------------------- .../src/per_epoch_processing.rs | 3 ++- .../validator_statuses.rs | 6 ++--- .../src/per_epoch_processing/winning_root.rs | 17 +++++++------- 4 files changed, 13 insertions(+), 35 deletions(-) delete mode 100644 eth2/state_processing/src/common/exit_validator.rs diff --git a/eth2/state_processing/src/common/exit_validator.rs b/eth2/state_processing/src/common/exit_validator.rs deleted file mode 100644 index 7a817b126..000000000 --- a/eth2/state_processing/src/common/exit_validator.rs +++ /dev/null @@ -1,22 +0,0 @@ -use types::{BeaconStateError as Error, *}; - -/// Exit the validator of the given `index`. -/// -/// Spec v0.5.1 -pub fn exit_validator( - state: &mut BeaconState, - 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(()) -} diff --git a/eth2/state_processing/src/per_epoch_processing.rs b/eth2/state_processing/src/per_epoch_processing.rs index 5253cd95c..5e37877e4 100644 --- a/eth2/state_processing/src/per_epoch_processing.rs +++ b/eth2/state_processing/src/per_epoch_processing.rs @@ -161,7 +161,8 @@ pub fn process_crosslinks( 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)?; diff --git a/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs b/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs index a4062b988..a2ce292eb 100644 --- a/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs +++ b/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs @@ -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( fn has_common_beacon_block_root( a: &PendingAttestation, state: &BeaconState, - spec: &ChainSpec, ) -> Result { let attestation_slot = state.get_attestation_slot(&a.data)?; let state_block_root = *state.get_block_root(attestation_slot)?; diff --git a/eth2/state_processing/src/per_epoch_processing/winning_root.rs b/eth2/state_processing/src/per_epoch_processing/winning_root.rs index 550e1eaf4..62917a134 100644 --- a/eth2/state_processing/src/per_epoch_processing/winning_root.rs +++ b/eth2/state_processing/src/per_epoch_processing/winning_root.rs @@ -47,15 +47,16 @@ pub fn winning_root( .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() == ¤t_shard_crosslink_root[..] || c.tree_hash_root() == current_shard_crosslink_root }); @@ -63,7 +64,7 @@ pub fn winning_root( // Build a map from candidate crosslink to attestations that support that crosslink. let mut candidate_crosslink_map: HashMap> = 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);