Skip to content

Commit b6d634c

Browse files
committed
Add EpochCache
1 parent c5658f0 commit b6d634c

File tree

7 files changed

+49
-25
lines changed

7 files changed

+49
-25
lines changed

consensus/state_processing/src/per_epoch_processing/altair.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ use crate::per_epoch_processing::{
55
resets::{process_eth1_data_reset, process_randao_mixes_reset, process_slashings_reset},
66
validator_statuses::ValidatorStatuses,
77
};
8+
pub use epoch_cache::EpochCache;
89
pub use inactivity_updates::process_inactivity_updates;
910
pub use justification_and_finalization::process_justification_and_finalization;
10-
pub use participation_cache::ParticipationCache;
1111
pub use participation_flag_updates::process_participation_flag_updates;
1212
pub use rewards_and_penalties::process_rewards_and_penalties;
1313
pub use sync_committee_updates::process_sync_committee_updates;
1414
use types::{BeaconState, ChainSpec, EthSpec, RelativeEpoch};
1515

16+
pub mod epoch_cache;
1617
pub mod inactivity_updates;
1718
pub mod justification_and_finalization;
18-
pub mod participation_cache;
1919
pub mod participation_flag_updates;
2020
pub mod rewards_and_penalties;
2121
pub mod sync_committee_updates;
@@ -29,15 +29,15 @@ pub fn process_epoch<T: EthSpec>(
2929
state.build_committee_cache(RelativeEpoch::Current, spec)?;
3030
state.build_committee_cache(RelativeEpoch::Next, spec)?;
3131

32-
let participation_cache = ParticipationCache::altair(state, spec)?;
32+
let cache = EpochCache::new(state, spec)?;
3333

3434
// Justification and finalization.
35-
process_justification_and_finalization(state, &participation_cache, spec)?;
35+
process_justification_and_finalization(state, &cache, spec)?;
3636

37-
process_inactivity_updates(state, &participation_cache, spec)?;
37+
process_inactivity_updates(state, &cache, spec)?;
3838

3939
// Rewards and Penalties.
40-
process_rewards_and_penalties(state, &participation_cache, spec)?;
40+
process_rewards_and_penalties(state, &cache, spec)?;
4141

4242
// Registry Updates.
4343
process_registry_updates(state, spec)?;
@@ -81,6 +81,6 @@ pub fn process_epoch<T: EthSpec>(
8181

8282
Ok(EpochProcessingSummary::Altair {
8383
total_balances: validator_statuses.total_balances,
84-
participation_cache,
84+
participation_cache: cache.participation,
8585
})
8686
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub use participation_cache::ParticipationCache;
2+
use types::{BeaconState, BeaconStateError, ChainSpec, EthSpec};
3+
4+
mod participation_cache;
5+
6+
pub struct EpochCache {
7+
pub participation: ParticipationCache,
8+
}
9+
10+
impl EpochCache {
11+
pub fn new<T: EthSpec>(
12+
state: &BeaconState<T>,
13+
spec: &ChainSpec,
14+
) -> Result<Self, BeaconStateError> {
15+
Ok(Self {
16+
participation: ParticipationCache::new(state, spec)?,
17+
})
18+
}
19+
}

consensus/state_processing/src/per_epoch_processing/altair/participation_cache.rs renamed to consensus/state_processing/src/per_epoch_processing/altair/epoch_cache/participation_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct ParticipationCache {
1717
}
1818

1919
impl ParticipationCache {
20-
pub fn altair<T: EthSpec>(
20+
pub fn new<T: EthSpec>(
2121
state: &BeaconState<T>,
2222
spec: &ChainSpec,
2323
) -> Result<Self, BeaconStateError> {

consensus/state_processing/src/per_epoch_processing/altair/inactivity_updates.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::ParticipationCache;
1+
use super::EpochCache;
22
use crate::EpochProcessingError;
33
use core::result::Result;
44
use core::result::Result::Ok;
@@ -11,15 +11,16 @@ use types::eth_spec::EthSpec;
1111

1212
pub fn process_inactivity_updates<T: EthSpec>(
1313
state: &mut BeaconState<T>,
14-
participation_cache: &ParticipationCache,
14+
cache: &EpochCache,
1515
spec: &ChainSpec,
1616
) -> Result<(), EpochProcessingError> {
1717
// Score updates based on previous epoch participation, skip genesis epoch
1818
if state.current_epoch() == T::genesis_epoch() {
1919
return Ok(());
2020
}
2121

22-
let unslashed_indices = participation_cache
22+
let unslashed_indices = cache
23+
.participation
2324
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, state.previous_epoch())?;
2425

2526
for index in state.get_eligible_validator_indices()? {

consensus/state_processing/src/per_epoch_processing/altair/justification_and_finalization.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::ParticipationCache;
1+
use super::EpochCache;
22
use crate::per_epoch_processing::weigh_justification_and_finalization;
33
use crate::per_epoch_processing::Error;
44
use safe_arith::SafeArith;
@@ -8,7 +8,7 @@ use types::{BeaconState, ChainSpec, EthSpec};
88
/// Update the justified and finalized checkpoints for matching target attestations.
99
pub fn process_justification_and_finalization<T: EthSpec>(
1010
state: &mut BeaconState<T>,
11-
participation_cache: &ParticipationCache,
11+
cache: &EpochCache,
1212
spec: &ChainSpec,
1313
) -> Result<(), Error> {
1414
if state.current_epoch() <= T::genesis_epoch().safe_add(1)? {
@@ -17,9 +17,11 @@ pub fn process_justification_and_finalization<T: EthSpec>(
1717

1818
let previous_epoch = state.previous_epoch();
1919
let current_epoch = state.current_epoch();
20-
let previous_indices = participation_cache
20+
let previous_indices = cache
21+
.participation
2122
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, previous_epoch)?;
22-
let current_indices = participation_cache
23+
let current_indices = cache
24+
.participation
2325
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, current_epoch)?;
2426
let total_active_balance = state.get_total_balance(
2527
state

consensus/state_processing/src/per_epoch_processing/altair/rewards_and_penalties.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::ParticipationCache;
1+
use super::EpochCache;
22
use safe_arith::SafeArith;
33
use types::consts::altair::{
44
PARTICIPATION_FLAG_WEIGHTS, TIMELY_HEAD_FLAG_INDEX, TIMELY_TARGET_FLAG_INDEX,
@@ -14,7 +14,7 @@ use crate::per_epoch_processing::{Delta, Error};
1414
/// Spec v1.1.0
1515
pub fn process_rewards_and_penalties<T: EthSpec>(
1616
state: &mut BeaconState<T>,
17-
participation_cache: &ParticipationCache,
17+
cache: &EpochCache,
1818
spec: &ChainSpec,
1919
) -> Result<(), Error> {
2020
if state.current_epoch() == T::genesis_epoch() {
@@ -31,12 +31,12 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
3131
state,
3232
flag_index,
3333
total_active_balance,
34-
participation_cache,
34+
cache,
3535
spec,
3636
)?;
3737
}
3838

39-
get_inactivity_penalty_deltas(&mut deltas, state, participation_cache, spec)?;
39+
get_inactivity_penalty_deltas(&mut deltas, state, cache, spec)?;
4040

4141
// Apply the deltas, erroring on overflow above but not on overflow below (saturating at 0
4242
// instead).
@@ -56,12 +56,13 @@ pub fn get_flag_index_deltas<T: EthSpec>(
5656
state: &BeaconState<T>,
5757
flag_index: usize,
5858
total_active_balance: u64,
59-
participation_cache: &ParticipationCache,
59+
cache: &EpochCache,
6060
spec: &ChainSpec,
6161
) -> Result<(), Error> {
6262
let previous_epoch = state.previous_epoch();
63-
let unslashed_participating_indices =
64-
participation_cache.get_unslashed_participating_indices(flag_index, previous_epoch)?;
63+
let unslashed_participating_indices = cache
64+
.participation
65+
.get_unslashed_participating_indices(flag_index, previous_epoch)?;
6566
let weight = get_flag_weight(flag_index)?;
6667
let unslashed_participating_balance =
6768
state.get_total_balance(&unslashed_participating_indices, spec)?;
@@ -104,11 +105,12 @@ pub fn get_flag_weight(flag_index: usize) -> Result<u64, Error> {
104105
pub fn get_inactivity_penalty_deltas<T: EthSpec>(
105106
deltas: &mut Vec<Delta>,
106107
state: &BeaconState<T>,
107-
participation_cache: &ParticipationCache,
108+
cache: &EpochCache,
108109
spec: &ChainSpec,
109110
) -> Result<(), Error> {
110111
let previous_epoch = state.previous_epoch();
111-
let matching_target_indices = participation_cache
112+
let matching_target_indices = cache
113+
.participation
112114
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, previous_epoch)?;
113115
for index in state.get_eligible_validator_indices()? {
114116
let mut delta = Delta::default();

consensus/state_processing/src/per_epoch_processing/epoch_processing_summary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{
2-
altair::ParticipationCache,
2+
altair::epoch_cache::ParticipationCache,
33
base::{TotalBalances, ValidatorStatus},
44
validator_statuses::InclusionInfo,
55
};

0 commit comments

Comments
 (0)