2019-03-18 10:34:42 +00:00
|
|
|
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.0
|
|
|
|
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<usize> = state
|
2019-03-18 22:09:57 +00:00
|
|
|
.get_cached_active_validator_indices(RelativeEpoch::Current, spec)?
|
2019-03-18 10:34:42 +00:00
|
|
|
.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(())
|
|
|
|
}
|