This commit is contained in:
Michael Sproul
2022-02-08 09:48:48 +11:00
parent f6230a5143
commit 0c742aedff
17 changed files with 152 additions and 244 deletions

View File

@@ -19,6 +19,7 @@ use types::{
TIMELY_TARGET_FLAG_INDEX,
},
BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, ParticipationFlags, RelativeEpoch,
Validator,
};
#[derive(Debug, PartialEq)]
@@ -120,12 +121,10 @@ impl SingleEpochParticipationCache {
fn process_active_validator<T: EthSpec>(
&mut self,
val_index: usize,
validator: &Validator,
state: &BeaconState<T>,
relative_epoch: RelativeEpoch,
) -> Result<(), BeaconStateError> {
let val_balance = state.get_effective_balance(val_index)?;
let validator = state.get_validator(val_index)?;
// Sanity check to ensure the validator is active.
let epoch = relative_epoch.into_epoch(state.current_epoch());
if !validator.is_active_at(epoch) {
@@ -141,7 +140,8 @@ impl SingleEpochParticipationCache {
.ok_or(BeaconStateError::ParticipationOutOfBounds(val_index))?;
// All active validators increase the total active balance.
self.total_active_balance.safe_add_assign(val_balance)?;
self.total_active_balance
.safe_add_assign(validator.effective_balance)?;
// Only unslashed validators may proceed.
if validator.slashed {
@@ -156,7 +156,7 @@ impl SingleEpochParticipationCache {
// are set for `val_index`.
for (flag, balance) in self.total_flag_balances.iter_mut().enumerate() {
if epoch_participation.has_flag(flag)? {
balance.safe_add_assign(val_balance)?;
balance.safe_add_assign(validator.effective_balance)?;
}
}
@@ -223,6 +223,7 @@ impl ParticipationCache {
if val.is_active_at(current_epoch) {
current_epoch_participation.process_active_validator(
val_index,
val,
state,
RelativeEpoch::Current,
)?;
@@ -231,6 +232,7 @@ impl ParticipationCache {
if val.is_active_at(previous_epoch) {
previous_epoch_participation.process_active_validator(
val_index,
val,
state,
RelativeEpoch::Previous,
)?;

View File

@@ -69,6 +69,7 @@ pub fn get_flag_index_deltas<T: EthSpec>(
let active_increments = total_active_balance.safe_div(spec.effective_balance_increment)?;
for &index in participation_cache.eligible_validator_indices() {
// FIXME(sproul): compute base reward in participation cache
let base_reward = get_base_reward(state, index, total_active_balance, spec)?;
let mut delta = Delta::default();

View File

@@ -2,7 +2,7 @@ use super::errors::EpochProcessingError;
use safe_arith::SafeArith;
use types::beacon_state::BeaconState;
use types::chain_spec::ChainSpec;
use types::{BeaconStateError, EthSpec, GetValidatorMut};
use types::{BeaconStateError, EthSpec};
pub fn process_effective_balance_updates<T: EthSpec>(
state: &mut BeaconState<T>,
@@ -13,9 +13,9 @@ pub fn process_effective_balance_updates<T: EthSpec>(
.safe_div(spec.hysteresis_quotient)?;
let downward_threshold = hysteresis_increment.safe_mul(spec.hysteresis_downward_multiplier)?;
let upward_threshold = hysteresis_increment.safe_mul(spec.hysteresis_upward_multiplier)?;
let (mut validators, balances) = state.validators_and_balances_mut();
for index in 0..validators.len() {
let validator = validators.get_validator_mut(index)?;
let (validators, balances) = state.validators_and_balances_mut();
let mut validators_iter = validators.iter_cow();
while let Some((index, validator)) = validators_iter.next_cow() {
let balance = balances
.get(index)
.copied()
@@ -24,7 +24,7 @@ pub fn process_effective_balance_updates<T: EthSpec>(
if balance.safe_add(downward_threshold)? < validator.effective_balance
|| validator.effective_balance.safe_add(upward_threshold)? < balance
{
validator.effective_balance = std::cmp::min(
validator.to_mut().effective_balance = std::cmp::min(
balance.safe_sub(balance.safe_rem(spec.effective_balance_increment)?)?,
spec.max_effective_balance,
);

View File

@@ -1,7 +1,7 @@
use crate::{common::initiate_validator_exit, per_epoch_processing::Error};
use itertools::Itertools;
use safe_arith::SafeArith;
use types::{BeaconState, ChainSpec, EthSpec, GetValidatorMut, Validator};
use types::{BeaconState, ChainSpec, EthSpec, Validator};
/// Performs a validator registry update, if required.
///
@@ -30,13 +30,11 @@ pub fn process_registry_updates<T: EthSpec>(
.collect();
for index in indices_to_update {
let mut validators = state.validators_mut();
let validator = validators.get_validator_mut(index)?;
let validator = state.get_validator_mut(index)?;
if validator.is_eligible_for_activation_queue(spec) {
validator.activation_eligibility_epoch = current_epoch.safe_add(1)?;
}
if is_ejectable(validator) {
drop(validators);
initiate_validator_exit(state, index, spec)?;
}
}
@@ -54,9 +52,8 @@ pub fn process_registry_updates<T: EthSpec>(
// Dequeue validators for activation up to churn limit
let churn_limit = state.get_churn_limit(spec)? as usize;
let delayed_activation_epoch = state.compute_activation_exit_epoch(current_epoch, spec)?;
let mut validators = state.validators_mut();
for index in activation_queue.into_iter().take(churn_limit) {
validators.get_validator_mut(index)?.activation_epoch = delayed_activation_epoch;
state.get_validator_mut(index)?.activation_epoch = delayed_activation_epoch;
}
Ok(())

View File

@@ -1,6 +1,6 @@
use crate::per_epoch_processing::Error;
use safe_arith::{SafeArith, SafeArithIter};
use types::{BeaconState, ChainSpec, EthSpec, GetBalanceMut, GetValidatorMut, Unsigned};
use types::{BeaconState, BeaconStateError, ChainSpec, EthSpec, Unsigned};
/// Process slashings.
pub fn process_slashings<T: EthSpec>(
@@ -16,9 +16,9 @@ pub fn process_slashings<T: EthSpec>(
total_balance,
);
let (validators, mut balances) = state.validators_and_balances_mut();
for index in 0..validators.len() {
let validator = validators.get_validator(index)?;
let (validators, balances) = state.validators_and_balances_mut();
let mut validators_iter = validators.iter_cow();
while let Some((index, validator)) = validators_iter.next_cow() {
if validator.slashed
&& epoch.safe_add(T::EpochsPerSlashingsVector::to_u64().safe_div(2)?)?
== validator.withdrawable_epoch
@@ -33,7 +33,9 @@ pub fn process_slashings<T: EthSpec>(
.safe_mul(increment)?;
// Equivalent to `decrease_balance(state, index, penalty)`, but avoids borrowing `state`.
let balance = balances.get_balance_mut(index)?;
let balance = balances
.get_mut(index)
.ok_or(BeaconStateError::BalancesOutOfBounds(index))?;
*balance = balance.saturating_sub(penalty);
}
}