mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-01 19:53:32 +00:00
In-memory tree states (#5533)
* Consensus changes
* EF tests
* lcli
* common and watch
* account manager
* cargo
* fork choice
* promise cache
* beacon chain
* interop genesis
* http api
* lighthouse
* op pool
* beacon chain misc
* parallel state cache
* store
* fix issues in store
* IT COMPILES
* Remove some unnecessary module qualification
* Revert Arced pubkey optimization (#5536)
* Merge remote-tracking branch 'origin/unstable' into tree-states-memory
* Fix caching, rebasing and some tests
* Remove unused deps
* Merge remote-tracking branch 'origin/unstable' into tree-states-memory
* Small cleanups
* Revert shuffling cache/promise cache changes
* Fix state advance bugs
* Fix shuffling tests
* Remove some resolved FIXMEs
* Remove StateProcessingStrategy
* Optimise withdrawals calculation
* Don't reorg if state cache is missed
* Remove inconsistent state func
* Fix beta compiler
* Rebase early, rebase often
* Fix state caching behaviour
* Update to milhouse release
* Fix on-disk consensus context format
* Merge remote-tracking branch 'origin/unstable' into tree-states-memory
* Squashed commit of the following:
commit 3a16649023
Author: Michael Sproul <michael@sigmaprime.io>
Date: Thu Apr 18 14:26:09 2024 +1000
Fix on-disk consensus context format
* Keep indexed attestations, thanks Sean
* Merge branch 'on-disk-consensus-context' into tree-states-memory
* Merge branch 'unstable' into tree-states-memory
* Address half of Sean's review
* More simplifications from Sean's review
* Cache state after get_advanced_hot_state
This commit is contained in:
@@ -9,12 +9,14 @@ use types::{BeaconState, ChainSpec, EpochCacheError, EthSpec, Hash256, RelativeE
|
||||
pub trait AllCaches {
|
||||
/// Build all caches.
|
||||
///
|
||||
/// Note that this excludes the tree-hash cache. That needs to be managed separately.
|
||||
/// Note that this excludes milhouse's intrinsic tree-hash cache. That needs to be managed
|
||||
/// separately.
|
||||
fn build_all_caches(&mut self, spec: &ChainSpec) -> Result<(), EpochCacheError>;
|
||||
|
||||
/// Return true if all caches are built.
|
||||
///
|
||||
/// Note that this excludes the tree-hash cache. That needs to be managed separately.
|
||||
/// Note that this excludes milhouse's intrinsic tree-hash cache. That needs to be managed
|
||||
/// separately.
|
||||
fn all_caches_built(&self) -> bool;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,19 +6,23 @@ use crate::{
|
||||
use itertools::Itertools;
|
||||
use std::iter::Peekable;
|
||||
use std::marker::PhantomData;
|
||||
use types::{BeaconState, BlindedPayload, ChainSpec, EthSpec, Hash256, SignedBeaconBlock, Slot};
|
||||
use types::{
|
||||
BeaconState, BeaconStateError, BlindedPayload, ChainSpec, EthSpec, Hash256, SignedBeaconBlock,
|
||||
Slot,
|
||||
};
|
||||
|
||||
type PreBlockHook<'a, E, Error> = Box<
|
||||
pub type PreBlockHook<'a, E, Error> = Box<
|
||||
dyn FnMut(&mut BeaconState<E>, &SignedBeaconBlock<E, BlindedPayload<E>>) -> Result<(), Error>
|
||||
+ 'a,
|
||||
>;
|
||||
type PostBlockHook<'a, E, Error> = PreBlockHook<'a, E, Error>;
|
||||
type PreSlotHook<'a, E, Error> = Box<dyn FnMut(&mut BeaconState<E>) -> Result<(), Error> + 'a>;
|
||||
type PostSlotHook<'a, E, Error> = Box<
|
||||
pub type PostBlockHook<'a, E, Error> = PreBlockHook<'a, E, Error>;
|
||||
pub type PreSlotHook<'a, E, Error> =
|
||||
Box<dyn FnMut(Hash256, &mut BeaconState<E>) -> Result<(), Error> + 'a>;
|
||||
pub type PostSlotHook<'a, E, Error> = Box<
|
||||
dyn FnMut(&mut BeaconState<E>, Option<EpochProcessingSummary<E>>, bool) -> Result<(), Error>
|
||||
+ 'a,
|
||||
>;
|
||||
type StateRootIterDefault<Error> = std::iter::Empty<Result<(Hash256, Slot), Error>>;
|
||||
pub type StateRootIterDefault<Error> = std::iter::Empty<Result<(Hash256, Slot), Error>>;
|
||||
|
||||
/// Efficiently apply blocks to a state while configuring various parameters.
|
||||
///
|
||||
@@ -31,7 +35,6 @@ pub struct BlockReplayer<
|
||||
> {
|
||||
state: BeaconState<Spec>,
|
||||
spec: &'a ChainSpec,
|
||||
state_processing_strategy: StateProcessingStrategy,
|
||||
block_sig_strategy: BlockSignatureStrategy,
|
||||
verify_block_root: Option<VerifyBlockRoot>,
|
||||
pre_block_hook: Option<PreBlockHook<'a, Spec, Error>>,
|
||||
@@ -45,9 +48,9 @@ pub struct BlockReplayer<
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BlockReplayError {
|
||||
NoBlocks,
|
||||
SlotProcessing(SlotProcessingError),
|
||||
BlockProcessing(BlockProcessingError),
|
||||
BeaconState(BeaconStateError),
|
||||
}
|
||||
|
||||
impl From<SlotProcessingError> for BlockReplayError {
|
||||
@@ -62,14 +65,10 @@ impl From<BlockProcessingError> for BlockReplayError {
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines how state roots should be computed and whether to perform all state transitions during block replay.
|
||||
#[derive(PartialEq, Clone, Copy)]
|
||||
pub enum StateProcessingStrategy {
|
||||
/// Perform all transitions faithfully to the specification.
|
||||
Accurate,
|
||||
/// Don't compute state roots and process withdrawals, eventually computing an invalid beacon
|
||||
/// state that can only be used for obtaining shuffling.
|
||||
Inconsistent,
|
||||
impl From<BeaconStateError> for BlockReplayError {
|
||||
fn from(e: BeaconStateError) -> Self {
|
||||
Self::BeaconState(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, E, Error, StateRootIter> BlockReplayer<'a, E, Error, StateRootIter>
|
||||
@@ -89,7 +88,6 @@ where
|
||||
Self {
|
||||
state,
|
||||
spec,
|
||||
state_processing_strategy: StateProcessingStrategy::Accurate,
|
||||
block_sig_strategy: BlockSignatureStrategy::VerifyBulk,
|
||||
verify_block_root: Some(VerifyBlockRoot::True),
|
||||
pre_block_hook: None,
|
||||
@@ -102,18 +100,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the replayer's state processing strategy different from the default.
|
||||
pub fn state_processing_strategy(
|
||||
mut self,
|
||||
state_processing_strategy: StateProcessingStrategy,
|
||||
) -> Self {
|
||||
if state_processing_strategy == StateProcessingStrategy::Inconsistent {
|
||||
self.verify_block_root = None;
|
||||
}
|
||||
self.state_processing_strategy = state_processing_strategy;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the replayer's block signature verification strategy.
|
||||
pub fn block_signature_strategy(mut self, block_sig_strategy: BlockSignatureStrategy) -> Self {
|
||||
self.block_sig_strategy = block_sig_strategy;
|
||||
@@ -175,21 +161,24 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Compute the state root for `slot` as efficiently as possible.
|
||||
/// Compute the state root for `self.state` as efficiently as possible.
|
||||
///
|
||||
/// This function MUST only be called when `self.state` is a post-state, i.e. it MUST not be
|
||||
/// called between advancing a state with `per_slot_processing` and applying the block for that
|
||||
/// slot.
|
||||
///
|
||||
/// The `blocks` should be the full list of blocks being applied and `i` should be the index of
|
||||
/// the next block that will be applied, or `blocks.len()` if all blocks have already been
|
||||
/// applied.
|
||||
///
|
||||
/// If the state root is not available from the state root iterator or the blocks then it will
|
||||
/// be computed from `self.state` and a state root iterator miss will be recorded.
|
||||
fn get_state_root(
|
||||
&mut self,
|
||||
slot: Slot,
|
||||
blocks: &[SignedBeaconBlock<E, BlindedPayload<E>>],
|
||||
i: usize,
|
||||
) -> Result<Option<Hash256>, Error> {
|
||||
// If we don't care about state roots then return immediately.
|
||||
if self.state_processing_strategy == StateProcessingStrategy::Inconsistent {
|
||||
return Ok(Some(Hash256::zero()));
|
||||
}
|
||||
) -> Result<Hash256, Error> {
|
||||
let slot = self.state.slot();
|
||||
|
||||
// If a state root iterator is configured, use it to find the root.
|
||||
if let Some(ref mut state_root_iter) = self.state_root_iter {
|
||||
@@ -199,7 +188,7 @@ where
|
||||
.transpose()?;
|
||||
|
||||
if let Some((root, _)) = opt_root {
|
||||
return Ok(Some(root));
|
||||
return Ok(root);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,13 +196,17 @@ where
|
||||
if let Some(prev_i) = i.checked_sub(1) {
|
||||
if let Some(prev_block) = blocks.get(prev_i) {
|
||||
if prev_block.slot() == slot {
|
||||
return Ok(Some(prev_block.state_root()));
|
||||
return Ok(prev_block.state_root());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.state_root_miss = true;
|
||||
Ok(None)
|
||||
let state_root = self
|
||||
.state
|
||||
.update_tree_hash_cache()
|
||||
.map_err(BlockReplayError::from)?;
|
||||
Ok(state_root)
|
||||
}
|
||||
|
||||
/// Apply `blocks` atop `self.state`, taking care of slot processing.
|
||||
@@ -232,12 +225,13 @@ where
|
||||
}
|
||||
|
||||
while self.state.slot() < block.slot() {
|
||||
let state_root = self.get_state_root(&blocks, i)?;
|
||||
|
||||
if let Some(ref mut pre_slot_hook) = self.pre_slot_hook {
|
||||
pre_slot_hook(&mut self.state)?;
|
||||
pre_slot_hook(state_root, &mut self.state)?;
|
||||
}
|
||||
|
||||
let state_root = self.get_state_root(self.state.slot(), &blocks, i)?;
|
||||
let summary = per_slot_processing(&mut self.state, state_root, self.spec)
|
||||
let summary = per_slot_processing(&mut self.state, Some(state_root), self.spec)
|
||||
.map_err(BlockReplayError::from)?;
|
||||
|
||||
if let Some(ref mut post_slot_hook) = self.post_slot_hook {
|
||||
@@ -250,15 +244,11 @@ where
|
||||
pre_block_hook(&mut self.state, block)?;
|
||||
}
|
||||
|
||||
let verify_block_root = self.verify_block_root.unwrap_or_else(|| {
|
||||
// If no explicit policy is set, verify only the first 1 or 2 block roots if using
|
||||
// accurate state roots. Inaccurate state roots require block root verification to
|
||||
// be off.
|
||||
if i <= 1 && self.state_processing_strategy == StateProcessingStrategy::Accurate {
|
||||
VerifyBlockRoot::True
|
||||
} else {
|
||||
VerifyBlockRoot::False
|
||||
}
|
||||
// If no explicit policy is set, verify only the first 1 or 2 block roots.
|
||||
let verify_block_root = self.verify_block_root.unwrap_or(if i <= 1 {
|
||||
VerifyBlockRoot::True
|
||||
} else {
|
||||
VerifyBlockRoot::False
|
||||
});
|
||||
// Proposer index was already checked when this block was originally processed, we
|
||||
// can omit recomputing it during replay.
|
||||
@@ -268,7 +258,6 @@ where
|
||||
&mut self.state,
|
||||
block,
|
||||
self.block_sig_strategy,
|
||||
self.state_processing_strategy,
|
||||
verify_block_root,
|
||||
&mut ctxt,
|
||||
self.spec,
|
||||
@@ -282,12 +271,13 @@ where
|
||||
|
||||
if let Some(target_slot) = target_slot {
|
||||
while self.state.slot() < target_slot {
|
||||
let state_root = self.get_state_root(&blocks, blocks.len())?;
|
||||
|
||||
if let Some(ref mut pre_slot_hook) = self.pre_slot_hook {
|
||||
pre_slot_hook(&mut self.state)?;
|
||||
pre_slot_hook(state_root, &mut self.state)?;
|
||||
}
|
||||
|
||||
let state_root = self.get_state_root(self.state.slot(), &blocks, blocks.len())?;
|
||||
let summary = per_slot_processing(&mut self.state, state_root, self.spec)
|
||||
let summary = per_slot_processing(&mut self.state, Some(state_root), self.spec)
|
||||
.map_err(BlockReplayError::from)?;
|
||||
|
||||
if let Some(ref mut post_slot_hook) = self.post_slot_hook {
|
||||
|
||||
@@ -30,13 +30,14 @@ pub fn initiate_validator_exit<E: EthSpec>(
|
||||
exit_queue_epoch.safe_add_assign(1)?;
|
||||
}
|
||||
|
||||
let validator = state.get_validator_mut(index)?;
|
||||
let validator = state.get_validator_cow(index)?;
|
||||
|
||||
// Return if the validator already initiated exit
|
||||
if validator.exit_epoch != spec.far_future_epoch {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let validator = validator.into_mut()?;
|
||||
validator.exit_epoch = exit_queue_epoch;
|
||||
validator.withdrawable_epoch =
|
||||
exit_queue_epoch.safe_add(spec.min_validator_withdrawability_delay)?;
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn initialize_beacon_state_from_eth1<E: EthSpec>(
|
||||
let mut state = BeaconState::new(genesis_time, eth1_data, spec);
|
||||
|
||||
// Seed RANDAO with Eth1 entropy
|
||||
state.fill_randao_mixes_with(eth1_block_hash);
|
||||
state.fill_randao_mixes_with(eth1_block_hash)?;
|
||||
|
||||
let mut deposit_tree = DepositDataTree::create(&[], 0, DEPOSIT_TREE_DEPTH);
|
||||
|
||||
@@ -152,7 +152,9 @@ pub fn process_activations<E: EthSpec>(
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let (validators, balances, _) = state.validators_and_balances_and_progressive_balances_mut();
|
||||
for (index, validator) in validators.iter_mut().enumerate() {
|
||||
let mut validators_iter = validators.iter_cow();
|
||||
while let Some((index, validator)) = validators_iter.next_cow() {
|
||||
let validator = validator.into_mut()?;
|
||||
let balance = balances
|
||||
.get(index)
|
||||
.copied()
|
||||
|
||||
@@ -30,7 +30,7 @@ pub mod upgrade;
|
||||
pub mod verify_operation;
|
||||
|
||||
pub use all_caches::AllCaches;
|
||||
pub use block_replayer::{BlockReplayError, BlockReplayer, StateProcessingStrategy};
|
||||
pub use block_replayer::{BlockReplayError, BlockReplayer};
|
||||
pub use consensus_context::{ConsensusContext, ContextError};
|
||||
pub use genesis::{
|
||||
eth2_genesis_time, initialize_beacon_state_from_eth1, is_valid_genesis_state,
|
||||
|
||||
@@ -40,7 +40,6 @@ mod verify_exit;
|
||||
mod verify_proposer_slashing;
|
||||
|
||||
use crate::common::decrease_balance;
|
||||
use crate::StateProcessingStrategy;
|
||||
|
||||
use crate::common::update_progressive_balances_cache::{
|
||||
initialize_progressive_balances_cache, update_progressive_balances_metrics,
|
||||
@@ -102,7 +101,6 @@ pub fn per_block_processing<E: EthSpec, Payload: AbstractExecPayload<E>>(
|
||||
state: &mut BeaconState<E>,
|
||||
signed_block: &SignedBeaconBlock<E, Payload>,
|
||||
block_signature_strategy: BlockSignatureStrategy,
|
||||
state_processing_strategy: StateProcessingStrategy,
|
||||
verify_block_root: VerifyBlockRoot,
|
||||
ctxt: &mut ConsensusContext<E>,
|
||||
spec: &ChainSpec,
|
||||
@@ -172,9 +170,7 @@ pub fn per_block_processing<E: EthSpec, Payload: AbstractExecPayload<E>>(
|
||||
// previous block.
|
||||
if is_execution_enabled(state, block.body()) {
|
||||
let body = block.body();
|
||||
if state_processing_strategy == StateProcessingStrategy::Accurate {
|
||||
process_withdrawals::<E, Payload>(state, body.execution_payload()?, spec)?;
|
||||
}
|
||||
process_withdrawals::<E, Payload>(state, body.execution_payload()?, spec)?;
|
||||
process_execution_payload::<E, Payload>(state, body, spec)?;
|
||||
}
|
||||
|
||||
|
||||
@@ -82,6 +82,7 @@ pub enum BlockProcessingError {
|
||||
},
|
||||
ExecutionInvalid,
|
||||
ConsensusContext(ContextError),
|
||||
MilhouseError(milhouse::Error),
|
||||
EpochCacheError(EpochCacheError),
|
||||
WithdrawalsRootMismatch {
|
||||
expected: Hash256,
|
||||
@@ -138,6 +139,12 @@ impl From<EpochCacheError> for BlockProcessingError {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@@ -128,7 +128,7 @@ pub mod altair_deneb {
|
||||
let previous_epoch = ctxt.previous_epoch;
|
||||
let current_epoch = ctxt.current_epoch;
|
||||
|
||||
let attesting_indices = &verify_attestation_for_block_inclusion(
|
||||
let attesting_indices = verify_attestation_for_block_inclusion(
|
||||
state,
|
||||
attestation,
|
||||
ctxt,
|
||||
@@ -136,7 +136,8 @@ pub mod altair_deneb {
|
||||
spec,
|
||||
)
|
||||
.map_err(|e| e.into_with_index(att_index))?
|
||||
.attesting_indices;
|
||||
.attesting_indices
|
||||
.clone();
|
||||
|
||||
// Matching roots, participation flag indices
|
||||
let data = &attestation.data;
|
||||
@@ -146,7 +147,7 @@ pub mod altair_deneb {
|
||||
|
||||
// Update epoch participation flags.
|
||||
let mut proposer_reward_numerator = 0;
|
||||
for index in attesting_indices {
|
||||
for index in &attesting_indices {
|
||||
let index = *index as usize;
|
||||
|
||||
let validator_effective_balance = state.epoch_cache().get_effective_balance(index)?;
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::per_block_processing::errors::{
|
||||
DepositInvalid, HeaderInvalid, IndexedAttestationInvalid, IntoWithIndex,
|
||||
ProposerSlashingInvalid,
|
||||
};
|
||||
use crate::{per_block_processing, BlockReplayError, BlockReplayer, StateProcessingStrategy};
|
||||
use crate::{per_block_processing, BlockReplayError, BlockReplayer};
|
||||
use crate::{
|
||||
per_block_processing::{process_operations, verify_exit::verify_exit},
|
||||
BlockSignatureStrategy, ConsensusContext, VerifyBlockRoot, VerifySignatures,
|
||||
@@ -72,7 +72,6 @@ async fn valid_block_ok() {
|
||||
&mut state,
|
||||
&block,
|
||||
BlockSignatureStrategy::VerifyIndividual,
|
||||
StateProcessingStrategy::Accurate,
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
&spec,
|
||||
@@ -98,7 +97,6 @@ async fn invalid_block_header_state_slot() {
|
||||
&mut state,
|
||||
&SignedBeaconBlock::from_block(block, signature),
|
||||
BlockSignatureStrategy::VerifyIndividual,
|
||||
StateProcessingStrategy::Accurate,
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
&spec,
|
||||
@@ -131,7 +129,6 @@ async fn invalid_parent_block_root() {
|
||||
&mut state,
|
||||
&SignedBeaconBlock::from_block(block, signature),
|
||||
BlockSignatureStrategy::VerifyIndividual,
|
||||
StateProcessingStrategy::Accurate,
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
&spec,
|
||||
@@ -165,7 +162,6 @@ async fn invalid_block_signature() {
|
||||
&mut state,
|
||||
&SignedBeaconBlock::from_block(block, Signature::empty()),
|
||||
BlockSignatureStrategy::VerifyIndividual,
|
||||
StateProcessingStrategy::Accurate,
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
&spec,
|
||||
@@ -199,7 +195,6 @@ async fn invalid_randao_reveal_signature() {
|
||||
&mut state,
|
||||
&signed_block,
|
||||
BlockSignatureStrategy::VerifyIndividual,
|
||||
StateProcessingStrategy::Accurate,
|
||||
VerifyBlockRoot::True,
|
||||
&mut ctxt,
|
||||
&spec,
|
||||
|
||||
@@ -2,17 +2,14 @@ use crate::EpochProcessingError;
|
||||
use types::beacon_state::BeaconState;
|
||||
use types::eth_spec::EthSpec;
|
||||
use types::participation_flags::ParticipationFlags;
|
||||
use types::VariableList;
|
||||
use types::List;
|
||||
|
||||
pub fn process_participation_flag_updates<E: EthSpec>(
|
||||
state: &mut BeaconState<E>,
|
||||
) -> Result<(), EpochProcessingError> {
|
||||
*state.previous_epoch_participation_mut()? =
|
||||
std::mem::take(state.current_epoch_participation_mut()?);
|
||||
*state.current_epoch_participation_mut()? = VariableList::new(vec![
|
||||
ParticipationFlags::default(
|
||||
);
|
||||
state.validators().len()
|
||||
])?;
|
||||
*state.current_epoch_participation_mut()? =
|
||||
List::repeat(ParticipationFlags::default(), state.validators().len())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@ pub fn process_historical_summaries_update<E: EthSpec>(
|
||||
.safe_rem((E::slots_per_historical_root() as u64).safe_div(E::slots_per_epoch())?)?
|
||||
== 0
|
||||
{
|
||||
// We need to flush any pending mutations before hashing.
|
||||
state.block_roots_mut().apply_updates()?;
|
||||
state.state_roots_mut().apply_updates()?;
|
||||
let summary = HistoricalSummary::new(state);
|
||||
return state
|
||||
.historical_summaries_mut()?
|
||||
|
||||
@@ -21,7 +21,9 @@ pub fn process_effective_balance_updates<E: EthSpec>(
|
||||
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_and_progressive_balances_mut();
|
||||
for (index, validator) in validators.iter_mut().enumerate() {
|
||||
let mut validators_iter = validators.iter_cow();
|
||||
|
||||
while let Some((index, validator)) = validators_iter.next_cow() {
|
||||
let balance = balances
|
||||
.get(index)
|
||||
.copied()
|
||||
@@ -44,7 +46,7 @@ pub fn process_effective_balance_updates<E: EthSpec>(
|
||||
}
|
||||
|
||||
if new_effective_balance != validator.effective_balance {
|
||||
validator.effective_balance = new_effective_balance;
|
||||
validator.into_mut()?.effective_balance = new_effective_balance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ use crate::metrics;
|
||||
use std::sync::Arc;
|
||||
use types::{
|
||||
consts::altair::{TIMELY_HEAD_FLAG_INDEX, TIMELY_SOURCE_FLAG_INDEX, TIMELY_TARGET_FLAG_INDEX},
|
||||
BeaconStateError, Epoch, EthSpec, ParticipationFlags, ProgressiveBalancesCache, SyncCommittee,
|
||||
Validator, VariableList,
|
||||
BeaconStateError, Epoch, EthSpec, List, ParticipationFlags, ProgressiveBalancesCache,
|
||||
SyncCommittee, Validator,
|
||||
};
|
||||
|
||||
/// Provides a summary of validator participation during the epoch.
|
||||
@@ -25,20 +25,20 @@ pub enum EpochProcessingSummary<E: EthSpec> {
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct ParticipationEpochSummary<E: EthSpec> {
|
||||
/// Copy of the validator registry prior to mutation.
|
||||
validators: VariableList<Validator, E::ValidatorRegistryLimit>,
|
||||
validators: List<Validator, E::ValidatorRegistryLimit>,
|
||||
/// Copy of the participation flags for the previous epoch.
|
||||
previous_epoch_participation: VariableList<ParticipationFlags, E::ValidatorRegistryLimit>,
|
||||
previous_epoch_participation: List<ParticipationFlags, E::ValidatorRegistryLimit>,
|
||||
/// Copy of the participation flags for the current epoch.
|
||||
current_epoch_participation: VariableList<ParticipationFlags, E::ValidatorRegistryLimit>,
|
||||
current_epoch_participation: List<ParticipationFlags, E::ValidatorRegistryLimit>,
|
||||
previous_epoch: Epoch,
|
||||
current_epoch: Epoch,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> ParticipationEpochSummary<E> {
|
||||
pub fn new(
|
||||
validators: VariableList<Validator, E::ValidatorRegistryLimit>,
|
||||
previous_epoch_participation: VariableList<ParticipationFlags, E::ValidatorRegistryLimit>,
|
||||
current_epoch_participation: VariableList<ParticipationFlags, E::ValidatorRegistryLimit>,
|
||||
validators: List<Validator, E::ValidatorRegistryLimit>,
|
||||
previous_epoch_participation: List<ParticipationFlags, E::ValidatorRegistryLimit>,
|
||||
current_epoch_participation: List<ParticipationFlags, E::ValidatorRegistryLimit>,
|
||||
previous_epoch: Epoch,
|
||||
current_epoch: Epoch,
|
||||
) -> Self {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use types::{BeaconStateError, EpochCacheError, InconsistentFork};
|
||||
use types::{milhouse, BeaconStateError, EpochCacheError, InconsistentFork};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum EpochProcessingError {
|
||||
@@ -23,6 +23,7 @@ pub enum EpochProcessingError {
|
||||
InconsistentStateFork(InconsistentFork),
|
||||
InvalidJustificationBit(ssz_types::Error),
|
||||
InvalidFlagIndex(usize),
|
||||
MilhouseError(milhouse::Error),
|
||||
EpochCache(EpochCacheError),
|
||||
}
|
||||
|
||||
@@ -50,6 +51,12 @@ impl From<safe_arith::ArithError> for EpochProcessingError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<milhouse::Error> for EpochProcessingError {
|
||||
fn from(e: milhouse::Error) -> Self {
|
||||
Self::MilhouseError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EpochCacheError> for EpochProcessingError {
|
||||
fn from(e: EpochCacheError) -> Self {
|
||||
EpochProcessingError::EpochCache(e)
|
||||
|
||||
@@ -14,7 +14,7 @@ pub fn process_historical_roots_update<E: EthSpec>(
|
||||
.safe_rem(E::SlotsPerHistoricalRoot::to_u64().safe_div(E::slots_per_epoch())?)?
|
||||
== 0
|
||||
{
|
||||
let historical_batch = state.historical_batch();
|
||||
let historical_batch = state.historical_batch()?;
|
||||
state
|
||||
.historical_roots_mut()
|
||||
.push(historical_batch.tree_hash_root())?;
|
||||
|
||||
@@ -2,7 +2,7 @@ use super::errors::EpochProcessingError;
|
||||
use safe_arith::SafeArith;
|
||||
use types::beacon_state::BeaconState;
|
||||
use types::eth_spec::EthSpec;
|
||||
use types::{Unsigned, VariableList};
|
||||
use types::{List, Unsigned};
|
||||
|
||||
pub fn process_eth1_data_reset<E: EthSpec>(
|
||||
state: &mut BeaconState<E>,
|
||||
@@ -13,7 +13,7 @@ pub fn process_eth1_data_reset<E: EthSpec>(
|
||||
.safe_rem(E::SlotsPerEth1VotingPeriod::to_u64())?
|
||||
== 0
|
||||
{
|
||||
*state.eth1_data_votes_mut() = VariableList::empty();
|
||||
*state.eth1_data_votes_mut() = List::empty();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ use types::{
|
||||
NUM_FLAG_INDICES, PARTICIPATION_FLAG_WEIGHTS, TIMELY_HEAD_FLAG_INDEX,
|
||||
TIMELY_TARGET_FLAG_INDEX, WEIGHT_DENOMINATOR,
|
||||
},
|
||||
milhouse::Cow,
|
||||
ActivationQueue, BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, ExitCache, ForkName,
|
||||
ParticipationFlags, ProgressiveBalancesCache, RelativeEpoch, Unsigned, Validator,
|
||||
};
|
||||
@@ -173,9 +174,9 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
let effective_balances_ctxt = &EffectiveBalancesContext::new(spec)?;
|
||||
|
||||
// Iterate over the validators and related fields in one pass.
|
||||
let mut validators_iter = validators.iter_mut();
|
||||
let mut balances_iter = balances.iter_mut();
|
||||
let mut inactivity_scores_iter = inactivity_scores.iter_mut();
|
||||
let mut validators_iter = validators.iter_cow();
|
||||
let mut balances_iter = balances.iter_cow();
|
||||
let mut inactivity_scores_iter = inactivity_scores.iter_cow();
|
||||
|
||||
// Values computed for the next epoch transition.
|
||||
let mut next_epoch_total_active_balance = 0;
|
||||
@@ -186,14 +187,14 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
previous_epoch_participation.iter(),
|
||||
current_epoch_participation.iter(),
|
||||
) {
|
||||
let validator = validators_iter
|
||||
.next()
|
||||
let (_, mut validator) = validators_iter
|
||||
.next_cow()
|
||||
.ok_or(BeaconStateError::UnknownValidator(index))?;
|
||||
let balance = balances_iter
|
||||
.next()
|
||||
let (_, mut balance) = balances_iter
|
||||
.next_cow()
|
||||
.ok_or(BeaconStateError::UnknownValidator(index))?;
|
||||
let inactivity_score = inactivity_scores_iter
|
||||
.next()
|
||||
let (_, mut inactivity_score) = inactivity_scores_iter
|
||||
.next_cow()
|
||||
.ok_or(BeaconStateError::UnknownValidator(index))?;
|
||||
|
||||
let is_active_current_epoch = validator.is_active_at(current_epoch);
|
||||
@@ -223,7 +224,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
// `process_inactivity_updates`
|
||||
if conf.inactivity_updates {
|
||||
process_single_inactivity_update(
|
||||
inactivity_score,
|
||||
&mut inactivity_score,
|
||||
validator_info,
|
||||
state_ctxt,
|
||||
spec,
|
||||
@@ -233,8 +234,8 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
// `process_rewards_and_penalties`
|
||||
if conf.rewards_and_penalties {
|
||||
process_single_reward_and_penalty(
|
||||
balance,
|
||||
inactivity_score,
|
||||
&mut balance,
|
||||
&inactivity_score,
|
||||
validator_info,
|
||||
rewards_ctxt,
|
||||
state_ctxt,
|
||||
@@ -246,7 +247,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
// `process_registry_updates`
|
||||
if conf.registry_updates {
|
||||
process_single_registry_update(
|
||||
validator,
|
||||
&mut validator,
|
||||
validator_info,
|
||||
exit_cache,
|
||||
activation_queue,
|
||||
@@ -258,14 +259,14 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
|
||||
// `process_slashings`
|
||||
if conf.slashings {
|
||||
process_single_slashing(balance, validator, slashings_ctxt, state_ctxt, spec)?;
|
||||
process_single_slashing(&mut balance, &validator, slashings_ctxt, state_ctxt, spec)?;
|
||||
}
|
||||
|
||||
// `process_effective_balance_updates`
|
||||
if conf.effective_balance_updates {
|
||||
process_single_effective_balance_update(
|
||||
*balance,
|
||||
validator,
|
||||
&mut validator,
|
||||
validator_info,
|
||||
&mut next_epoch_total_active_balance,
|
||||
&mut next_epoch_cache,
|
||||
@@ -290,7 +291,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
}
|
||||
|
||||
fn process_single_inactivity_update(
|
||||
inactivity_score: &mut u64,
|
||||
inactivity_score: &mut Cow<u64>,
|
||||
validator_info: &ValidatorInfo,
|
||||
state_ctxt: &StateContext,
|
||||
spec: &ChainSpec,
|
||||
@@ -303,25 +304,27 @@ fn process_single_inactivity_update(
|
||||
if validator_info.is_unslashed_participating_index(TIMELY_TARGET_FLAG_INDEX)? {
|
||||
// Avoid mutating when the inactivity score is 0 and can't go any lower -- the common
|
||||
// case.
|
||||
if *inactivity_score == 0 {
|
||||
if **inactivity_score == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
inactivity_score.safe_sub_assign(1)?;
|
||||
inactivity_score.make_mut()?.safe_sub_assign(1)?;
|
||||
} else {
|
||||
inactivity_score.safe_add_assign(spec.inactivity_score_bias)?;
|
||||
inactivity_score
|
||||
.make_mut()?
|
||||
.safe_add_assign(spec.inactivity_score_bias)?;
|
||||
}
|
||||
|
||||
// Decrease the score of all validators for forgiveness when not during a leak
|
||||
if !state_ctxt.is_in_inactivity_leak {
|
||||
let deduction = min(spec.inactivity_score_recovery_rate, *inactivity_score);
|
||||
inactivity_score.safe_sub_assign(deduction)?;
|
||||
let deduction = min(spec.inactivity_score_recovery_rate, **inactivity_score);
|
||||
inactivity_score.make_mut()?.safe_sub_assign(deduction)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_single_reward_and_penalty(
|
||||
balance: &mut u64,
|
||||
balance: &mut Cow<u64>,
|
||||
inactivity_score: &u64,
|
||||
validator_info: &ValidatorInfo,
|
||||
rewards_ctxt: &RewardsAndPenaltiesContext,
|
||||
@@ -351,6 +354,7 @@ fn process_single_reward_and_penalty(
|
||||
)?;
|
||||
|
||||
if delta.rewards != 0 || delta.penalties != 0 {
|
||||
let balance = balance.make_mut()?;
|
||||
balance.safe_add_assign(delta.rewards)?;
|
||||
*balance = balance.saturating_sub(delta.penalties);
|
||||
}
|
||||
@@ -452,7 +456,7 @@ impl RewardsAndPenaltiesContext {
|
||||
}
|
||||
|
||||
fn process_single_registry_update(
|
||||
validator: &mut Validator,
|
||||
validator: &mut Cow<Validator>,
|
||||
validator_info: &ValidatorInfo,
|
||||
exit_cache: &mut ExitCache,
|
||||
activation_queue: &BTreeSet<usize>,
|
||||
@@ -463,7 +467,7 @@ fn process_single_registry_update(
|
||||
let current_epoch = state_ctxt.current_epoch;
|
||||
|
||||
if validator.is_eligible_for_activation_queue(spec) {
|
||||
validator.activation_eligibility_epoch = current_epoch.safe_add(1)?;
|
||||
validator.make_mut()?.activation_eligibility_epoch = current_epoch.safe_add(1)?;
|
||||
}
|
||||
|
||||
if validator.is_active_at(current_epoch) && validator.effective_balance <= spec.ejection_balance
|
||||
@@ -472,7 +476,8 @@ fn process_single_registry_update(
|
||||
}
|
||||
|
||||
if activation_queue.contains(&validator_info.index) {
|
||||
validator.activation_epoch = spec.compute_activation_exit_epoch(current_epoch)?;
|
||||
validator.make_mut()?.activation_epoch =
|
||||
spec.compute_activation_exit_epoch(current_epoch)?;
|
||||
}
|
||||
|
||||
// Caching: add to speculative activation queue for next epoch.
|
||||
@@ -487,7 +492,7 @@ fn process_single_registry_update(
|
||||
}
|
||||
|
||||
fn initiate_validator_exit(
|
||||
validator: &mut Validator,
|
||||
validator: &mut Cow<Validator>,
|
||||
exit_cache: &mut ExitCache,
|
||||
state_ctxt: &StateContext,
|
||||
spec: &ChainSpec,
|
||||
@@ -508,6 +513,7 @@ fn initiate_validator_exit(
|
||||
exit_queue_epoch.safe_add_assign(1)?;
|
||||
}
|
||||
|
||||
let validator = validator.make_mut()?;
|
||||
validator.exit_epoch = exit_queue_epoch;
|
||||
validator.withdrawable_epoch =
|
||||
exit_queue_epoch.safe_add(spec.min_validator_withdrawability_delay)?;
|
||||
@@ -540,7 +546,7 @@ impl SlashingsContext {
|
||||
}
|
||||
|
||||
fn process_single_slashing(
|
||||
balance: &mut u64,
|
||||
balance: &mut Cow<u64>,
|
||||
validator: &Validator,
|
||||
slashings_ctxt: &SlashingsContext,
|
||||
state_ctxt: &StateContext,
|
||||
@@ -557,7 +563,7 @@ fn process_single_slashing(
|
||||
.safe_div(state_ctxt.total_active_balance)?
|
||||
.safe_mul(increment)?;
|
||||
|
||||
*balance = balance.saturating_sub(penalty);
|
||||
*balance.make_mut()? = balance.saturating_sub(penalty);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -581,7 +587,7 @@ impl EffectiveBalancesContext {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn process_single_effective_balance_update(
|
||||
balance: u64,
|
||||
validator: &mut Validator,
|
||||
validator: &mut Cow<Validator>,
|
||||
validator_info: &ValidatorInfo,
|
||||
next_epoch_total_active_balance: &mut u64,
|
||||
next_epoch_cache: &mut PreEpochCache,
|
||||
@@ -611,7 +617,7 @@ fn process_single_effective_balance_update(
|
||||
}
|
||||
|
||||
if new_effective_balance != old_effective_balance {
|
||||
validator.effective_balance = new_effective_balance;
|
||||
validator.make_mut()?.effective_balance = new_effective_balance;
|
||||
|
||||
// Update progressive balances cache for the *current* epoch, which will soon become the
|
||||
// previous epoch once the epoch transition completes.
|
||||
|
||||
@@ -4,13 +4,13 @@ use std::mem;
|
||||
use std::sync::Arc;
|
||||
use types::{
|
||||
BeaconState, BeaconStateAltair, BeaconStateError as Error, ChainSpec, EpochCache, EthSpec,
|
||||
Fork, ParticipationFlags, PendingAttestation, RelativeEpoch, SyncCommittee, VariableList,
|
||||
Fork, List, ParticipationFlags, PendingAttestation, RelativeEpoch, SyncCommittee,
|
||||
};
|
||||
|
||||
/// 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>,
|
||||
pending_attestations: &VariableList<PendingAttestation<E>, E::MaxPendingAttestations>,
|
||||
pending_attestations: &List<PendingAttestation<E>, E::MaxPendingAttestations>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
// Previous epoch committee cache is required for `get_attesting_indices`.
|
||||
@@ -51,8 +51,8 @@ pub fn upgrade_to_altair<E: EthSpec>(
|
||||
let pre = pre_state.as_base_mut()?;
|
||||
|
||||
let default_epoch_participation =
|
||||
VariableList::new(vec![ParticipationFlags::default(); pre.validators.len()])?;
|
||||
let inactivity_scores = VariableList::new(vec![0; pre.validators.len()])?;
|
||||
List::new(vec![ParticipationFlags::default(); pre.validators.len()])?;
|
||||
let inactivity_scores = List::new(vec![0; pre.validators.len()])?;
|
||||
|
||||
let temp_sync_committee = Arc::new(SyncCommittee::temporary());
|
||||
|
||||
@@ -108,7 +108,6 @@ pub fn upgrade_to_altair<E: EthSpec>(
|
||||
exit_cache: mem::take(&mut pre.exit_cache),
|
||||
slashings_cache: mem::take(&mut pre.slashings_cache),
|
||||
epoch_cache: EpochCache::default(),
|
||||
tree_hash_cache: mem::take(&mut pre.tree_hash_cache),
|
||||
});
|
||||
|
||||
// Fill in previous epoch participation from the pre state's pending attestations.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::mem;
|
||||
use types::{
|
||||
BeaconState, BeaconStateCapella, BeaconStateError as Error, ChainSpec, EpochCache, EthSpec,
|
||||
Fork, VariableList,
|
||||
Fork, List,
|
||||
};
|
||||
|
||||
/// Transform a `Merge` state into an `Capella` state.
|
||||
@@ -61,7 +61,7 @@ pub fn upgrade_to_capella<E: EthSpec>(
|
||||
// Capella
|
||||
next_withdrawal_index: 0,
|
||||
next_withdrawal_validator_index: 0,
|
||||
historical_summaries: VariableList::default(),
|
||||
historical_summaries: List::default(),
|
||||
// Caches
|
||||
total_active_balance: pre.total_active_balance,
|
||||
progressive_balances_cache: mem::take(&mut pre.progressive_balances_cache),
|
||||
@@ -70,7 +70,6 @@ pub fn upgrade_to_capella<E: EthSpec>(
|
||||
exit_cache: mem::take(&mut pre.exit_cache),
|
||||
slashings_cache: mem::take(&mut pre.slashings_cache),
|
||||
epoch_cache: EpochCache::default(),
|
||||
tree_hash_cache: mem::take(&mut pre.tree_hash_cache),
|
||||
});
|
||||
|
||||
*pre_state = post;
|
||||
|
||||
@@ -71,7 +71,6 @@ pub fn upgrade_to_deneb<E: EthSpec>(
|
||||
exit_cache: mem::take(&mut pre.exit_cache),
|
||||
slashings_cache: mem::take(&mut pre.slashings_cache),
|
||||
epoch_cache: EpochCache::default(),
|
||||
tree_hash_cache: mem::take(&mut pre.tree_hash_cache),
|
||||
});
|
||||
|
||||
*pre_state = post;
|
||||
|
||||
@@ -70,7 +70,6 @@ pub fn upgrade_to_electra<E: EthSpec>(
|
||||
exit_cache: mem::take(&mut pre.exit_cache),
|
||||
slashings_cache: mem::take(&mut pre.slashings_cache),
|
||||
epoch_cache: EpochCache::default(),
|
||||
tree_hash_cache: mem::take(&mut pre.tree_hash_cache),
|
||||
});
|
||||
|
||||
*pre_state = post;
|
||||
|
||||
@@ -66,7 +66,6 @@ pub fn upgrade_to_bellatrix<E: EthSpec>(
|
||||
exit_cache: mem::take(&mut pre.exit_cache),
|
||||
slashings_cache: mem::take(&mut pre.slashings_cache),
|
||||
epoch_cache: EpochCache::default(),
|
||||
tree_hash_cache: mem::take(&mut pre.tree_hash_cache),
|
||||
});
|
||||
|
||||
*pre_state = post;
|
||||
|
||||
Reference in New Issue
Block a user