spec: top-level per-epoch processing v0.6.1
This commit is contained in:
parent
894ecdd4ea
commit
79de966d3a
@ -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<u64, WinningRoot>;
|
||||
/// 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<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
@ -47,15 +43,12 @@ pub fn per_epoch_processing<T: EthSpec>(
|
||||
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<T: EthSpec>(
|
||||
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<T: EthSpec>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Maybe resets the eth1 period.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn maybe_reset_eth1_period<T: EthSpec>(state: &mut BeaconState<T>, 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<T: EthSpec>(
|
||||
|
||||
/// Finish up an epoch update.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn finish_epoch_update<T: EthSpec>(
|
||||
/// Spec v0.6.1
|
||||
pub fn process_final_updates<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
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<T: EthSpec>(
|
||||
.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(())
|
||||
|
@ -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<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
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<usize> = 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(())
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
use types::*;
|
||||
|
||||
/// Process the exit queue.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn process_exit_queue<T: EthSpec>(state: &mut BeaconState<T>, 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<usize> = (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<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
validator_index: usize,
|
||||
spec: &ChainSpec,
|
||||
) {
|
||||
state.validator_registry[validator_index].withdrawable_epoch =
|
||||
state.current_epoch(spec) + spec.min_validator_withdrawability_delay;
|
||||
}
|
@ -8,7 +8,6 @@ use types::*;
|
||||
/// Spec v0.6.1
|
||||
pub fn process_registry_updates<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
current_total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
// Process activation eligibility and ejections.
|
||||
|
@ -291,6 +291,16 @@ impl<T: EthSpec> BeaconState<T> {
|
||||
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<T: EthSpec> BeaconState<T> {
|
||||
|
||||
/// 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<usize, Error> {
|
||||
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<T: EthSpec> BeaconState<T> {
|
||||
|
||||
/// 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<Hash256, Error> {
|
||||
let i = self.get_active_index_root_index(epoch, spec)?;
|
||||
Ok(self.latest_active_index_roots[i])
|
||||
@ -563,7 +572,7 @@ impl<T: EthSpec> BeaconState<T> {
|
||||
|
||||
/// 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,
|
||||
|
Loading…
Reference in New Issue
Block a user