lighthouse/eth2/state_processing/src/per_epoch_processing/process_ejections.rs
2019-05-10 14:47:09 +10:00

32 lines
952 B
Rust

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.validator_balances[i as usize] < spec.ejection_balance {
Some(i)
} else {
None
}
})
.collect();
for validator_index in exitable {
exit_validator(state, validator_index, spec)?
}
Ok(())
}