Skip to content

Commit

Permalink
Use epoch cache in block packing (sigp#5223)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul authored and dapplion committed Feb 14, 2024
1 parent 94bb079 commit 82d733b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
4 changes: 4 additions & 0 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ use slot_clock::SlotClock;
use ssz::Encode;
use state_processing::{
common::get_attesting_indices_from_state,
epoch_cache::initialize_epoch_cache,
per_block_processing,
per_block_processing::{
errors::AttestationValidationError, get_expected_withdrawals,
Expand Down Expand Up @@ -4967,7 +4968,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let attestation_packing_timer =
metrics::start_timer(&metrics::BLOCK_PRODUCTION_ATTESTATION_TIMES);

// Epoch cache and total balance cache are required for op pool packing.
state.build_total_active_balance_cache_at(state.current_epoch(), &self.spec)?;
initialize_epoch_cache(&mut state, &self.spec)?;

let mut prev_filter_cache = HashMap::new();
let prev_attestation_filter = |att: &AttestationRef<T::EthSpec>| {
self.filter_op_pool_attestation(&mut prev_filter_cache, att, &state)
Expand Down
2 changes: 2 additions & 0 deletions beacon_node/beacon_chain/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ pub enum BlockProductionError {
UnableToProduceAtSlot(Slot),
SlotProcessingError(SlotProcessingError),
BlockProcessingError(BlockProcessingError),
EpochCacheError(EpochCacheError),
ForkChoiceError(ForkChoiceError),
Eth1ChainError(Eth1ChainError),
BeaconStateError(BeaconStateError),
Expand Down Expand Up @@ -299,3 +300,4 @@ easy_from_to!(SlotProcessingError, BlockProductionError);
easy_from_to!(Eth1ChainError, BlockProductionError);
easy_from_to!(StateAdvanceError, BlockProductionError);
easy_from_to!(ForkChoiceError, BlockProductionError);
easy_from_to!(EpochCacheError, BlockProductionError);
14 changes: 3 additions & 11 deletions beacon_node/operation_pool/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::attestation_storage::AttestationRef;
use crate::max_cover::MaxCover;
use crate::reward_cache::RewardCache;
use state_processing::common::{
altair, base, get_attestation_participation_flag_indices, get_attesting_indices,
base, get_attestation_participation_flag_indices, get_attesting_indices,
};
use std::collections::HashMap;
use types::{
Expand Down Expand Up @@ -30,7 +30,7 @@ impl<'a, T: EthSpec> AttMaxCover<'a, T> {
if let BeaconState::Base(ref base_state) = state {
Self::new_for_base(att, state, base_state, total_active_balance, spec)
} else {
Self::new_for_altair_deneb(att, state, reward_cache, total_active_balance, spec)
Self::new_for_altair_deneb(att, state, reward_cache, spec)
}
}

Expand Down Expand Up @@ -72,7 +72,6 @@ impl<'a, T: EthSpec> AttMaxCover<'a, T> {
att: AttestationRef<'a, T>,
state: &BeaconState<T>,
reward_cache: &'a RewardCache,
total_active_balance: u64,
spec: &ChainSpec,
) -> Option<Self> {
let att_data = att.attestation_data();
Expand All @@ -81,8 +80,6 @@ impl<'a, T: EthSpec> AttMaxCover<'a, T> {
let att_participation_flags =
get_attestation_participation_flag_indices(state, &att_data, inclusion_delay, spec)
.ok()?;
let base_reward_per_increment =
altair::BaseRewardPerIncrement::new(total_active_balance, spec).ok()?;

let fresh_validators_rewards = att
.indexed
Expand All @@ -98,12 +95,7 @@ impl<'a, T: EthSpec> AttMaxCover<'a, T> {

let mut proposer_reward_numerator = 0;

// FIXME(sproul): store base_reward in reward cache
// let effective_balance = reward_cache.get_effective_balance(index)?;
let effective_balance = state.get_effective_balance(index as usize).ok()?;
let base_reward =
altair::get_base_reward(effective_balance, base_reward_per_increment, spec)
.ok()?;
let base_reward = state.get_base_reward(index as usize).ok()?;

for (flag_index, weight) in PARTICIPATION_FLAG_WEIGHTS.iter().enumerate() {
if att_participation_flags.contains(&flag_index) {
Expand Down

0 comments on commit 82d733b

Please sign in to comment.