Skip to content

Commit 614d33b

Browse files
committed
Swap back to ParticipationCache
1 parent 6531be5 commit 614d33b

File tree

8 files changed

+296
-95
lines changed

8 files changed

+296
-95
lines changed

beacon_node/beacon_chain/src/block_verification.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,21 +1438,22 @@ fn expose_participation_metrics(summaries: &[EpochProcessingSummary]) {
14381438
}
14391439

14401440
for summary in summaries {
1441-
metrics::maybe_set_float_gauge(
1442-
&metrics::PARTICIPATION_PREV_EPOCH_TARGET_ATTESTER,
1443-
participation_ratio(
1444-
summary.previous_epoch_target_attesting_balance(),
1445-
summary.previous_epoch_total_active_balance(),
1446-
),
1447-
);
1441+
if let Ok(target_balance) = summary.previous_epoch_target_attesting_balance() {
1442+
metrics::maybe_set_float_gauge(
1443+
&metrics::PARTICIPATION_PREV_EPOCH_TARGET_ATTESTER,
1444+
participation_ratio(
1445+
target_balance,
1446+
summary.previous_epoch_total_active_balance(),
1447+
),
1448+
);
1449+
}
14481450

1449-
metrics::maybe_set_float_gauge(
1450-
&metrics::PARTICIPATION_PREV_EPOCH_HEAD_ATTESTER,
1451-
participation_ratio(
1452-
summary.previous_epoch_head_attesting_balance(),
1453-
summary.previous_epoch_total_active_balance(),
1454-
),
1455-
);
1451+
if let Ok(head_balance) = summary.previous_epoch_head_attesting_balance() {
1452+
metrics::maybe_set_float_gauge(
1453+
&metrics::PARTICIPATION_PREV_EPOCH_HEAD_ATTESTER,
1454+
participation_ratio(head_balance, summary.previous_epoch_total_active_balance()),
1455+
);
1456+
}
14561457
}
14571458
}
14581459

consensus/state_processing/src/per_epoch_processing/altair.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ use crate::per_epoch_processing::{
33
effective_balance_updates::process_effective_balance_updates,
44
historical_roots_update::process_historical_roots_update,
55
resets::{process_eth1_data_reset, process_randao_mixes_reset, process_slashings_reset},
6-
validator_statuses::ValidatorStatuses,
76
};
8-
pub use epoch_cache::EpochCache;
97
pub use inactivity_updates::process_inactivity_updates;
108
pub use justification_and_finalization::process_justification_and_finalization;
9+
pub use participation_cache::ParticipationCache;
1110
pub use participation_flag_updates::process_participation_flag_updates;
1211
pub use rewards_and_penalties::process_rewards_and_penalties;
1312
pub use sync_committee_updates::process_sync_committee_updates;
1413
use types::{BeaconState, ChainSpec, EthSpec, RelativeEpoch};
1514

16-
pub mod epoch_cache;
1715
pub mod inactivity_updates;
1816
pub mod justification_and_finalization;
17+
pub mod participation_cache;
1918
pub mod participation_flag_updates;
2019
pub mod rewards_and_penalties;
2120
pub mod sync_committee_updates;
@@ -29,23 +28,25 @@ pub fn process_epoch<T: EthSpec>(
2928
state.build_committee_cache(RelativeEpoch::Current, spec)?;
3029
state.build_committee_cache(RelativeEpoch::Next, spec)?;
3130

32-
let cache = EpochCache::new(state, spec)?;
31+
// Pre-compute participating indices and total balances.
32+
let participation_cache = ParticipationCache::new(state, spec)?;
3333

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

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

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

4242
// Registry Updates.
4343
process_registry_updates(state, spec)?;
4444

4545
// Slashings.
4646
process_slashings(
4747
state,
48-
cache.total_active_balance,
48+
// FIXME(paul): could this be invalidated by rewards/penalties?
49+
participation_cache.current_epoch_total_active_balance(),
4950
spec.proportional_slashing_multiplier_altair,
5051
spec,
5152
)?;
@@ -73,14 +74,7 @@ pub fn process_epoch<T: EthSpec>(
7374
// Rotate the epoch caches to suit the epoch transition.
7475
state.advance_caches()?;
7576

76-
// FIXME(altair): this is an incorrect dummy value, we should think harder
77-
// about how we want to unify validator statuses between phase0 & altair.
78-
// We should benchmark the new state transition and work out whether Altair could
79-
// be accelerated by some similar cache.
80-
let validator_statuses = ValidatorStatuses::new(state, spec)?;
81-
8277
Ok(EpochProcessingSummary::Altair {
83-
total_balances: validator_statuses.total_balances,
84-
participation_cache: cache.participation,
78+
participation_cache,
8579
})
8680
}

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

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

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

1212
pub fn process_inactivity_updates<T: EthSpec>(
1313
state: &mut BeaconState<T>,
14-
cache: &EpochCache,
14+
participation_cache: &ParticipationCache,
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 = cache
23-
.participation
22+
let unslashed_indices = participation_cache
2423
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, state.previous_epoch())?;
2524

2625
for index in state.get_eligible_validator_indices()? {

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
use super::EpochCache;
1+
use super::ParticipationCache;
22
use crate::per_epoch_processing::weigh_justification_and_finalization;
33
use crate::per_epoch_processing::Error;
44
use safe_arith::SafeArith;
55
use types::consts::altair::TIMELY_TARGET_FLAG_INDEX;
6-
use types::{BeaconState, ChainSpec, EthSpec};
6+
use types::{BeaconState, EthSpec};
77

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-
cache: &EpochCache,
12-
spec: &ChainSpec,
11+
participation_cache: &ParticipationCache,
1312
) -> Result<(), Error> {
1413
if state.current_epoch() <= T::genesis_epoch().safe_add(1)? {
1514
return Ok(());
1615
}
1716

1817
let previous_epoch = state.previous_epoch();
1918
let current_epoch = state.current_epoch();
20-
let previous_indices = cache
21-
.participation
19+
let previous_indices = participation_cache
2220
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, previous_epoch)?;
23-
let current_indices = cache
24-
.participation
21+
let current_indices = participation_cache
2522
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, current_epoch)?;
26-
let total_active_balance = state.get_total_balance(
27-
state
28-
.get_active_validator_indices(current_epoch, spec)?
29-
.as_slice(),
30-
spec,
31-
)?;
23+
let total_active_balance = participation_cache.current_epoch_total_active_balance();
3224
let previous_target_balance = previous_indices.total_balance()?;
3325
let current_target_balance = current_indices.total_balance()?;
3426
weigh_justification_and_finalization(

0 commit comments

Comments
 (0)