From 79de966d3aa4b634ee0342d7e58cdfc693404e29 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 14 May 2019 15:00:18 +1000 Subject: [PATCH] spec: top-level per-epoch processing v0.6.1 --- .../src/per_epoch_processing.rs | 76 +++++++++---------- .../per_epoch_processing/process_ejections.rs | 32 -------- .../process_exit_queue.rs | 44 ----------- .../per_epoch_processing/registry_updates.rs | 1 - eth2/types/src/beacon_state.rs | 23 ++++-- 5 files changed, 53 insertions(+), 123 deletions(-) delete mode 100644 eth2/state_processing/src/per_epoch_processing/process_ejections.rs delete mode 100644 eth2/state_processing/src/per_epoch_processing/process_exit_queue.rs diff --git a/eth2/state_processing/src/per_epoch_processing.rs b/eth2/state_processing/src/per_epoch_processing.rs index 86d4a5eee..251dc3ec1 100644 --- a/eth2/state_processing/src/per_epoch_processing.rs +++ b/eth2/state_processing/src/per_epoch_processing.rs @@ -1,7 +1,5 @@ use apply_rewards::process_rewards_and_penalties; use errors::EpochProcessingError as Error; -use process_ejections::process_ejections; -use process_exit_queue::process_exit_queue; use process_slashings::process_slashings; use registry_updates::process_registry_updates; use std::collections::HashMap; @@ -14,8 +12,6 @@ pub mod apply_rewards; pub mod errors; pub mod get_attesting_indices; pub mod inclusion_distance; -pub mod process_ejections; -pub mod process_exit_queue; pub mod process_slashings; pub mod registry_updates; pub mod tests; @@ -32,7 +28,7 @@ pub type WinningRootHashSet = HashMap; /// Mutates the given `BeaconState`, returning early if an error is encountered. If an error is /// returned, a state might be "half-processed" and therefore in an invalid state. /// -/// Spec v0.5.1 +/// Spec v0.6.1 pub fn per_epoch_processing( state: &mut BeaconState, spec: &ChainSpec, @@ -47,15 +43,12 @@ pub fn per_epoch_processing( let mut validator_statuses = ValidatorStatuses::new(state, spec)?; validator_statuses.process_attestations(&state, spec)?; - // Justification. + // Justification and finalization. process_justification_and_finalization(state, &validator_statuses.total_balances, spec)?; // Crosslinks. let winning_root_for_shards = process_crosslinks(state, spec)?; - // Eth1 data. - maybe_reset_eth1_period(state, spec); - // Rewards and Penalities. process_rewards_and_penalties( state, @@ -64,18 +57,14 @@ pub fn per_epoch_processing( spec, )?; - // Ejections. - process_ejections(state, spec)?; + // Registry Updates. + process_registry_updates(state, spec)?; - // Validator Registry. - process_registry_updates(state, validator_statuses.total_balances.current_epoch, spec)?; - - // Slashings and exit queue. + // Slashings. process_slashings(state, validator_statuses.total_balances.current_epoch, spec)?; - process_exit_queue(state, spec); // Final updates. - finish_epoch_update(state, spec)?; + process_final_updates(state, spec)?; // Rotate the epoch caches to suit the epoch transition. state.advance_caches(); @@ -83,25 +72,6 @@ pub fn per_epoch_processing( Ok(()) } -/// Maybe resets the eth1 period. -/// -/// Spec v0.5.1 -pub fn maybe_reset_eth1_period(state: &mut BeaconState, spec: &ChainSpec) { - /* FIXME(sproul) - let next_epoch = state.next_epoch(spec); - let voting_period = spec.epochs_per_eth1_voting_period; - - if next_epoch % voting_period == 0 { - for eth1_data_vote in &state.eth1_data_votes { - if eth1_data_vote.vote_count * 2 > voting_period * spec.slots_per_epoch { - state.latest_eth1_data = eth1_data_vote.eth1_data.clone(); - } - } - state.eth1_data_votes = vec![]; - } - */ -} - /// Update the following fields on the `BeaconState`: /// /// - `justification_bitfield`. @@ -213,14 +183,38 @@ pub fn process_crosslinks( /// Finish up an epoch update. /// -/// Spec v0.5.1 -pub fn finish_epoch_update( +/// Spec v0.6.1 +pub fn process_final_updates( state: &mut BeaconState, spec: &ChainSpec, ) -> Result<(), Error> { let current_epoch = state.current_epoch(spec); let next_epoch = state.next_epoch(spec); + // Reset eth1 data votes. + if (state.slot + 1) % spec.slots_per_eth1_voting_period == 0 { + state.eth1_data_votes = vec![]; + } + + // Update effective balances with hysteresis (lag). + for (index, validator) in state.validator_registry.iter_mut().enumerate() { + let balance = state.balances[index]; + let half_increment = spec.effective_balance_increment / 2; + if balance < validator.effective_balance + || validator.effective_balance + 3 * half_increment < balance + { + validator.effective_balance = std::cmp::min( + balance - balance % spec.effective_balance_increment, + spec.max_effective_balance, + ); + } + } + + // Update start shard. + state.latest_start_shard = (state.latest_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. // // The indentation here is to make it obvious where the weird stuff happens. @@ -255,7 +249,11 @@ pub fn finish_epoch_update( .push(Hash256::from_slice(&historical_batch.tree_hash_root()[..])); } - state.previous_epoch_attestations = state.current_epoch_attestations.clone(); + // Rotate current/previous epoch attestations + std::mem::swap( + &mut state.previous_epoch_attestations, + &mut state.current_epoch_attestations, + ); state.current_epoch_attestations = vec![]; Ok(()) diff --git a/eth2/state_processing/src/per_epoch_processing/process_ejections.rs b/eth2/state_processing/src/per_epoch_processing/process_ejections.rs deleted file mode 100644 index af329283f..000000000 --- a/eth2/state_processing/src/per_epoch_processing/process_ejections.rs +++ /dev/null @@ -1,32 +0,0 @@ -// use crate::common::exit_validator; -use types::{BeaconStateError as Error, *}; - -/// Iterate through the validator registry and eject active validators with balance below -/// ``EJECTION_BALANCE``. -/// -/// Spec v0.5.1 -pub fn process_ejections( - state: &mut BeaconState, - spec: &ChainSpec, -) -> Result<(), Error> { - // There is an awkward double (triple?) loop here because we can't loop across the borrowed - // active validator indices and mutate state in the one loop. - let exitable: Vec = state - .get_cached_active_validator_indices(RelativeEpoch::Current, spec)? - .iter() - .filter_map(|&i| { - if state.balances[i as usize] < spec.ejection_balance { - Some(i) - } else { - None - } - }) - .collect(); - - for validator_index in exitable { - // FIXME(sproul) - // exit_validator(state, validator_index, spec)? - } - - Ok(()) -} diff --git a/eth2/state_processing/src/per_epoch_processing/process_exit_queue.rs b/eth2/state_processing/src/per_epoch_processing/process_exit_queue.rs deleted file mode 100644 index b2f793ff4..000000000 --- a/eth2/state_processing/src/per_epoch_processing/process_exit_queue.rs +++ /dev/null @@ -1,44 +0,0 @@ -use types::*; - -/// Process the exit queue. -/// -/// Spec v0.5.1 -pub fn process_exit_queue(state: &mut BeaconState, spec: &ChainSpec) { - let current_epoch = state.current_epoch(spec); - - let eligible = |index: usize| { - let validator = &state.validator_registry[index]; - - if validator.withdrawable_epoch != spec.far_future_epoch { - false - } else { - current_epoch >= validator.exit_epoch + spec.min_validator_withdrawability_delay - } - }; - - let mut eligable_indices: Vec = (0..state.validator_registry.len()) - .filter(|i| eligible(*i)) - .collect(); - eligable_indices.sort_by_key(|i| state.validator_registry[*i].exit_epoch); - - for (dequeues, index) in eligable_indices.iter().enumerate() { - /* FIXME(sproul) - if dequeues as u64 >= spec.max_exit_dequeues_per_epoch { - break; - } - */ - prepare_validator_for_withdrawal(state, *index, spec); - } -} - -/// Initiate an exit for the validator of the given `index`. -/// -/// Spec v0.5.1 -fn prepare_validator_for_withdrawal( - state: &mut BeaconState, - validator_index: usize, - spec: &ChainSpec, -) { - state.validator_registry[validator_index].withdrawable_epoch = - state.current_epoch(spec) + spec.min_validator_withdrawability_delay; -} diff --git a/eth2/state_processing/src/per_epoch_processing/registry_updates.rs b/eth2/state_processing/src/per_epoch_processing/registry_updates.rs index ad7899e39..6f867b3af 100644 --- a/eth2/state_processing/src/per_epoch_processing/registry_updates.rs +++ b/eth2/state_processing/src/per_epoch_processing/registry_updates.rs @@ -8,7 +8,6 @@ use types::*; /// Spec v0.6.1 pub fn process_registry_updates( state: &mut BeaconState, - current_total_balance: u64, spec: &ChainSpec, ) -> Result<(), Error> { // Process activation eligibility and ejections. diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 5b38e3f6a..590e826ea 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -291,6 +291,16 @@ impl BeaconState { spec.get_epoch_committee_count(active_validator_indices.len()) } + /// Return the number of shards to increment `state.latest_start_shard` during `epoch`. + /// + /// Spec v0.6.1 + pub fn get_shard_delta(&self, epoch: Epoch, spec: &ChainSpec) -> u64 { + std::cmp::min( + self.get_epoch_committee_count(epoch, spec), + T::ShardCount::to_u64() - T::ShardCount::to_u64() / spec.slots_per_epoch, + ) + } + pub fn get_epoch_start_shard(&self, epoch: Epoch, spec: &ChainSpec) -> u64 { drop((epoch, spec)); unimplemented!("FIXME(sproul) get_epoch_start_shard") @@ -538,14 +548,13 @@ impl BeaconState { /// Safely obtains the index for `latest_active_index_roots`, given some `epoch`. /// - /// Spec v0.5.1 + /// Spec v0.6.1 fn get_active_index_root_index(&self, epoch: Epoch, spec: &ChainSpec) -> Result { let current_epoch = self.current_epoch(spec); - if (current_epoch - self.latest_active_index_roots.len() as u64 - + spec.activation_exit_delay - < epoch) - & (epoch <= current_epoch + spec.activation_exit_delay) + if current_epoch - self.latest_active_index_roots.len() as u64 + spec.activation_exit_delay + < epoch + && epoch <= current_epoch + spec.activation_exit_delay { Ok(epoch.as_usize() % self.latest_active_index_roots.len()) } else { @@ -555,7 +564,7 @@ impl BeaconState { /// Return the `active_index_root` at a recent `epoch`. /// - /// Spec v0.5.1 + /// Spec v0.6.1 pub fn get_active_index_root(&self, epoch: Epoch, spec: &ChainSpec) -> Result { let i = self.get_active_index_root_index(epoch, spec)?; Ok(self.latest_active_index_roots[i]) @@ -563,7 +572,7 @@ impl BeaconState { /// Set the `active_index_root` at a recent `epoch`. /// - /// Spec v0.5.1 + /// Spec v0.6.1 pub fn set_active_index_root( &mut self, epoch: Epoch,