Skip to content

Commit 835fa70

Browse files
Fix EpochCache handling in ef-tests (sigp#4454)
1 parent 2df714e commit 835fa70

File tree

7 files changed

+32
-6
lines changed

7 files changed

+32
-6
lines changed

consensus/state_processing/src/epoch_cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn initialize_epoch_cache<E: EthSpec>(
2323
}
2424

2525
// Compute base rewards.
26+
state.build_total_active_balance_cache_at(epoch, spec)?;
2627
let total_active_balance = state.get_total_active_balance_at_epoch(epoch)?;
2728
let sqrt_total_active_balance = SqrtTotalActiveBalance::new(total_active_balance);
2829
let base_reward_per_increment = BaseRewardPerIncrement::new(total_active_balance, spec)?;

consensus/state_processing/src/per_epoch_processing/altair.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub fn process_epoch<T: EthSpec>(
2828
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
2929
state.build_committee_cache(RelativeEpoch::Current, spec)?;
3030
state.build_committee_cache(RelativeEpoch::Next, spec)?;
31+
state.build_total_active_balance_cache_at(state.current_epoch(), spec)?;
32+
initialize_epoch_cache(state, state.current_epoch(), spec)?;
3133

3234
// Pre-compute participating indices and total balances.
3335
let mut participation_cache = ParticipationCache::new(state, spec)?;

consensus/state_processing/src/per_epoch_processing/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub fn process_epoch<T: EthSpec>(
2424
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
2525
state.build_committee_cache(RelativeEpoch::Current, spec)?;
2626
state.build_committee_cache(RelativeEpoch::Next, spec)?;
27+
state.build_total_active_balance_cache_at(state.current_epoch(), spec)?;
28+
initialize_epoch_cache(state, state.current_epoch(), spec)?;
2729

2830
// Load the struct we use to assign validators into sets based on their participation.
2931
//

consensus/state_processing/src/per_epoch_processing/capella.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub fn process_epoch<T: EthSpec>(
2424
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
2525
state.build_committee_cache(RelativeEpoch::Current, spec)?;
2626
state.build_committee_cache(RelativeEpoch::Next, spec)?;
27+
state.build_total_active_balance_cache_at(state.current_epoch(), spec)?;
28+
initialize_epoch_cache(state, state.current_epoch(), spec)?;
2729

2830
// Pre-compute participating indices and total balances.
2931
let mut participation_cache = ParticipationCache::new(state, spec)?;

consensus/types/src/beacon_state.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,10 +1468,24 @@ impl<T: EthSpec> BeaconState<T> {
14681468
}
14691469

14701470
/// Build the total active balance cache.
1471-
fn build_total_active_balance_cache(&mut self, spec: &ChainSpec) -> Result<(), Error> {
1472-
let current_epoch = self.current_epoch();
1473-
let total_active_balance = self.compute_total_active_balance(current_epoch, spec)?;
1474-
*self.total_active_balance_mut() = Some((current_epoch, total_active_balance));
1471+
pub fn build_total_active_balance_cache_at(
1472+
&mut self,
1473+
epoch: Epoch,
1474+
spec: &ChainSpec,
1475+
) -> Result<(), Error> {
1476+
if self.get_total_active_balance_at_epoch(epoch).is_err() {
1477+
self.force_build_total_active_balance_cache_at(epoch, spec)?;
1478+
}
1479+
Ok(())
1480+
}
1481+
1482+
pub fn force_build_total_active_balance_cache_at(
1483+
&mut self,
1484+
epoch: Epoch,
1485+
spec: &ChainSpec,
1486+
) -> Result<(), Error> {
1487+
let total_active_balance = self.compute_total_active_balance(epoch, spec)?;
1488+
*self.total_active_balance_mut() = Some((epoch, total_active_balance));
14751489
Ok(())
14761490
}
14771491

@@ -1552,6 +1566,7 @@ impl<T: EthSpec> BeaconState<T> {
15521566
self.drop_committee_cache(RelativeEpoch::Next)?;
15531567
self.drop_pubkey_cache();
15541568
*self.exit_cache_mut() = ExitCache::default();
1569+
*self.epoch_cache_mut() = EpochCache::default();
15551570
Ok(())
15561571
}
15571572

@@ -1580,7 +1595,7 @@ impl<T: EthSpec> BeaconState<T> {
15801595
}
15811596

15821597
if self.total_active_balance().is_none() && relative_epoch == RelativeEpoch::Current {
1583-
self.build_total_active_balance_cache(spec)?;
1598+
self.build_total_active_balance_cache_at(self.current_epoch(), spec)?;
15841599
}
15851600
Ok(())
15861601
}
@@ -1874,7 +1889,7 @@ impl<T: EthSpec> BeaconState<T> {
18741889
// Ensure total active balance cache remains built whenever current committee
18751890
// cache is built.
18761891
if epoch == self.current_epoch() {
1877-
self.build_total_active_balance_cache(spec)?;
1892+
self.build_total_active_balance_cache_at(self.current_epoch(), spec)?;
18781893
}
18791894
}
18801895
}

testing/ef_tests/src/cases/epoch_processing.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::decode::{ssz_decode_state, yaml_decode_file};
55
use crate::type_name;
66
use crate::type_name::TypeName;
77
use serde_derive::Deserialize;
8+
use state_processing::epoch_cache::initialize_epoch_cache;
89
use state_processing::per_epoch_processing::capella::process_historical_summaries_update;
910
use state_processing::per_epoch_processing::{
1011
altair, base,
@@ -135,6 +136,7 @@ impl<E: EthSpec> EpochTransition<E> for RewardsAndPenalties {
135136

136137
impl<E: EthSpec> EpochTransition<E> for RegistryUpdates {
137138
fn run(state: &mut BeaconState<E>, spec: &ChainSpec) -> Result<(), EpochProcessingError> {
139+
initialize_epoch_cache(state, state.current_epoch(), spec)?;
138140
process_registry_updates(state, spec)
139141
}
140142
}

testing/ef_tests/src/cases/operations.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::case_result::{check_state_diff, compare_beacon_state_results_without_
44
use crate::decode::{ssz_decode_file, ssz_decode_file_with, ssz_decode_state, yaml_decode_file};
55
use crate::testing_spec;
66
use serde_derive::Deserialize;
7+
use state_processing::epoch_cache::initialize_epoch_cache;
78
use state_processing::{
89
per_block_processing::{
910
errors::BlockProcessingError,
@@ -86,6 +87,7 @@ impl<E: EthSpec> Operation<E> for Attestation<E> {
8687
spec: &ChainSpec,
8788
_: &Operations<E, Self>,
8889
) -> Result<(), BlockProcessingError> {
90+
initialize_epoch_cache(state, state.current_epoch(), spec)?;
8991
let mut ctxt = ConsensusContext::new(state.slot());
9092
match state {
9193
BeaconState::Base(_) => base::process_attestations(

0 commit comments

Comments
 (0)