Update state_processing

This commit is contained in:
Michael Sproul
2021-11-25 17:32:18 +11:00
parent 1b4dad0d76
commit 238ac98d7c
18 changed files with 107 additions and 20 deletions

View File

@@ -31,9 +31,14 @@ pub fn initiate_validator_exit<T: EthSpec>(
state
.exit_cache_mut()
.record_validator_exit(exit_queue_epoch)?;
state.get_validator_mut(index)?.exit_epoch = exit_queue_epoch;
state.get_validator_mut(index)?.withdrawable_epoch =
let mut validators = state.validators_mut();
let validator = validators.get_validator_mut(index)?;
validator.exit_epoch = exit_queue_epoch;
validator.withdrawable_epoch =
exit_queue_epoch.safe_add(spec.min_validator_withdrawability_delay)?;
drop(validators);
Ok(())
}

View File

@@ -17,13 +17,16 @@ pub fn slash_validator<T: EthSpec>(
initiate_validator_exit(state, slashed_index, spec)?;
let validator = state.get_validator_mut(slashed_index)?;
let mut validators = state.validators_mut();
let validator = validators.get_validator_mut(slashed_index)?;
validator.slashed = true;
validator.withdrawable_epoch = cmp::max(
validator.withdrawable_epoch,
epoch.safe_add(T::EpochsPerSlashingsVector::to_u64())?,
);
let validator_effective_balance = validator.effective_balance;
drop(validators);
state.set_slashings(
epoch,
state

View File

@@ -76,8 +76,9 @@ pub fn process_activations<T: EthSpec>(
state: &mut BeaconState<T>,
spec: &ChainSpec,
) -> Result<(), Error> {
let (validators, balances) = state.validators_and_balances_mut();
for (index, validator) in validators.iter_mut().enumerate() {
let (mut validators, balances) = state.validators_and_balances_mut();
for index in 0..validators.len() {
let validator = validators.get_validator_mut(index)?;
let balance = balances
.get(index)
.copied()

View File

@@ -57,6 +57,8 @@ pub enum BlockProcessingError {
ArithError(ArithError),
InconsistentBlockFork(InconsistentFork),
InconsistentStateFork(InconsistentFork),
#[cfg(feature = "milhouse")]
MilhouseError(milhouse::Error),
}
impl From<BeaconStateError> for BlockProcessingError {
@@ -89,6 +91,13 @@ impl From<SyncAggregateInvalid> for BlockProcessingError {
}
}
#[cfg(feature = "milhouse")]
impl From<milhouse::Error> for BlockProcessingError {
fn from(e: milhouse::Error) -> Self {
Self::MilhouseError(e)
}
}
impl From<BlockOperationError<HeaderInvalid>> for BlockProcessingError {
fn from(e: BlockOperationError<HeaderInvalid>) -> BlockProcessingError {
match e {

View File

@@ -14,6 +14,9 @@ use types::{
SignedVoluntaryExit, SigningData, Slot, SyncAggregate, SyncAggregatorSelectionData, Unsigned,
};
#[cfg(feature = "milhouse")]
use types::milhouse::prelude::*;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, PartialEq, Clone)]

View File

@@ -21,6 +21,9 @@ use types::{
BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, ParticipationFlags, RelativeEpoch,
};
#[cfg(feature = "milhouse")]
use types::milhouse::prelude::*;
#[derive(Debug, PartialEq)]
pub enum Error {
InvalidFlagIndex(usize),

View File

@@ -6,6 +6,9 @@ use types::eth_spec::EthSpec;
use types::participation_flags::ParticipationFlags;
use types::VariableList;
#[cfg(feature = "milhouse")]
use types::milhouse::prelude::*;
pub fn process_participation_flag_updates<T: EthSpec>(
state: &mut BeaconState<T>,
) -> Result<(), EpochProcessingError> {

View File

@@ -6,6 +6,9 @@ use types::consts::altair::{
};
use types::{BeaconState, ChainSpec, EthSpec};
#[cfg(feature = "milhouse")]
use types::milhouse::prelude::*;
use crate::common::{altair::get_base_reward, decrease_balance, increase_balance};
use crate::per_epoch_processing::{Delta, Error};

View File

@@ -7,6 +7,9 @@ use safe_arith::SafeArith;
use std::array::IntoIter as ArrayIter;
use types::{BeaconState, ChainSpec, EthSpec};
#[cfg(feature = "milhouse")]
use types::milhouse::prelude::*;
/// Combination of several deltas for different components of an attestation reward.
///
/// Exists only for compatibility with EF rewards tests.

View File

@@ -2,6 +2,9 @@ use crate::common::get_attesting_indices;
use safe_arith::SafeArith;
use types::{BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, PendingAttestation};
#[cfg(feature = "milhouse")]
use types::milhouse::prelude::*;
#[cfg(feature = "arbitrary-fuzz")]
use arbitrary::Arbitrary;

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};
use types::{BeaconStateError, EthSpec, GetValidatorMut};
pub fn process_effective_balance_updates<T: EthSpec>(
state: &mut BeaconState<T>,
@@ -13,8 +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 (validators, balances) = state.validators_and_balances_mut();
for (index, validator) in validators.iter_mut().enumerate() {
let (mut validators, balances) = state.validators_and_balances_mut();
for index in 0..validators.len() {
let validator = validators.get_validator_mut(index)?;
let balance = balances
.get(index)
.copied()

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, Validator};
use types::{BeaconState, ChainSpec, EthSpec, GetValidatorMut, Validator};
/// Performs a validator registry update, if required.
///
@@ -30,11 +30,13 @@ pub fn process_registry_updates<T: EthSpec>(
.collect();
for index in indices_to_update {
let validator = state.get_validator_mut(index)?;
let mut validators = state.validators_mut();
let validator = validators.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)?;
}
}
@@ -52,8 +54,9 @@ 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) {
state.get_validator_mut(index)?.activation_epoch = delayed_activation_epoch;
validators.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, BeaconStateError, ChainSpec, EthSpec, Unsigned};
use types::{BeaconState, BeaconStateError, ChainSpec, EthSpec, GetValidatorMut, Unsigned};
/// Process slashings.
pub fn process_slashings<T: EthSpec>(
@@ -16,7 +16,8 @@ pub fn process_slashings<T: EthSpec>(
std::cmp::min(sum_slashings.safe_mul(slashing_multiplier)?, total_balance);
let (validators, balances) = state.validators_and_balances_mut();
for (index, validator) in validators.iter().enumerate() {
for index in 0..validators.len() {
let validator = validators.get_validator(index)?;
if validator.slashed
&& epoch.safe_add(T::EpochsPerSlashingsVector::to_u64().safe_div(2)?)?
== validator.withdrawable_epoch

View File

@@ -6,6 +6,9 @@ use types::{
ParticipationFlags, PendingAttestation, RelativeEpoch, SyncCommittee, VariableList,
};
#[cfg(feature = "milhouse")]
use types::milhouse::prelude::*;
/// Translate the participation information from the epoch prior to the fork into Altair's format.
pub fn translate_participation<E: EthSpec>(
state: &mut BeaconState<E>,
@@ -104,6 +107,7 @@ pub fn upgrade_to_altair<E: EthSpec>(
committee_caches: mem::take(&mut pre.committee_caches),
pubkey_cache: mem::take(&mut pre.pubkey_cache),
exit_cache: mem::take(&mut pre.exit_cache),
#[cfg(not(feature = "milhouse"))]
tree_hash_cache: mem::take(&mut pre.tree_hash_cache),
});