Implement activation queue cache

This commit is contained in:
Michael Sproul
2023-07-03 12:03:14 +10:00
parent f631b515f6
commit b414c323f8
6 changed files with 95 additions and 17 deletions

View File

@@ -2,7 +2,7 @@ use crate::common::altair::BaseRewardPerIncrement;
use crate::common::base::SqrtTotalActiveBalance;
use crate::common::{altair, base};
use types::epoch_cache::{EpochCache, EpochCacheError, EpochCacheKey};
use types::{BeaconState, ChainSpec, Epoch, EthSpec, Hash256};
use types::{ActivationQueue, BeaconState, ChainSpec, Epoch, EthSpec, Hash256};
pub fn initialize_epoch_cache<E: EthSpec>(
state: &mut BeaconState<E>,
@@ -23,13 +23,17 @@ pub fn initialize_epoch_cache<E: EthSpec>(
}
// Compute base rewards.
state.build_total_active_balance_cache_at(epoch, spec)?;
let total_active_balance = state.get_total_active_balance_at_epoch(epoch)?;
let sqrt_total_active_balance = SqrtTotalActiveBalance::new(total_active_balance);
let base_reward_per_increment = BaseRewardPerIncrement::new(total_active_balance, spec)?;
let mut base_rewards = Vec::with_capacity(state.validators().len());
for validator in state.validators().iter() {
// Compute activation queue.
let mut activation_queue = ActivationQueue::default();
for (index, validator) in state.validators().iter().enumerate() {
let effective_balance = validator.effective_balance();
let base_reward = if spec
@@ -41,6 +45,9 @@ pub fn initialize_epoch_cache<E: EthSpec>(
altair::get_base_reward(effective_balance, base_reward_per_increment, spec)?
};
base_rewards.push(base_reward);
// Add to speculative activation queue.
activation_queue.add_if_could_be_eligible_for_activation(index, validator, epoch, spec);
}
*state.epoch_cache_mut() = EpochCache::new(
@@ -49,6 +56,7 @@ pub fn initialize_epoch_cache<E: EthSpec>(
decision_block_root,
},
base_rewards,
activation_queue,
);
Ok(())

View File

@@ -1,5 +1,4 @@
use crate::{common::initiate_validator_exit, per_epoch_processing::Error};
use itertools::Itertools;
use safe_arith::SafeArith;
use types::{BeaconState, ChainSpec, EthSpec, Validator};
@@ -40,19 +39,16 @@ pub fn process_registry_updates<T: EthSpec>(
}
// Queue validators eligible for activation and not dequeued for activation prior to finalized epoch
let activation_queue = state
.validators()
.iter()
.enumerate()
.filter(|(_, validator)| validator.is_eligible_for_activation(state, spec))
.sorted_by_key(|(index, validator)| (validator.activation_eligibility_epoch(), *index))
.map(|(index, _)| index)
.collect_vec();
// Dequeue validators for activation up to churn limit
let churn_limit = state.get_churn_limit(spec)? as usize;
let epoch_cache = state.epoch_cache().clone();
let activation_queue = epoch_cache
.activation_queue()?
.get_validators_eligible_for_activation(state.finalized_checkpoint().epoch, churn_limit);
let delayed_activation_epoch = state.compute_activation_exit_epoch(current_epoch, spec)?;
for index in activation_queue.into_iter().take(churn_limit) {
for index in activation_queue {
state.get_validator_mut(index)?.mutable.activation_epoch = delayed_activation_epoch;
}