spec: top-level per-epoch processing v0.6.1

This commit is contained in:
Michael Sproul
2019-05-14 15:00:18 +10:00
parent 894ecdd4ea
commit 79de966d3a
5 changed files with 53 additions and 123 deletions

View File

@@ -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(())
}

View File

@@ -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;
}

View File

@@ -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.