Skip to content

Commit 50a03dd

Browse files
committed
Squashed commit of the following:
commit 8e2f7c5 Author: Michael Sproul <[email protected]> Date: Thu Feb 8 09:20:28 2024 +1100 Delete unused epoch processing code (sigp#5170) * Delete unused epoch processing code * Compare total deltas * Remove unnecessary apply_pending * cargo fmt * Remove newline commit ae6620e Author: Michael Sproul <[email protected]> Date: Fri Feb 2 17:47:14 2024 +1100 Delete `lighthouse db diff` (sigp#5171) * Delete `lighthouse db diff` * Fix help text commit 25bcd2a Author: Michael Sproul <[email protected]> Date: Wed Jan 31 10:13:18 2024 +1100 Tree states v4.6.222-exp (sigp#5147) commit 68a9a2e Merge: 8e68926 0f345c7 Author: Michael Sproul <[email protected]> Date: Tue Jan 30 17:13:57 2024 +1100 Merge remote-tracking branch 'origin/unstable' into tree-states commit 8e68926 Author: Michael Sproul <[email protected]> Date: Tue Jan 30 17:08:43 2024 +1100 `fsync` during backfill to prevent DB corruption (sigp#5144) commit 7862c71 Author: Michael Sproul <[email protected]> Date: Tue Jan 30 15:56:48 2024 +1100 Fix tree-states sub-epoch diffs (sigp#5097) commit 11461d8 Author: Michael Sproul <[email protected]> Date: Tue Jan 30 09:59:25 2024 +1100 Fix new CLI tests for tree-states (sigp#5132) commit 262e5f2 Merge: 6262be7 1be5253 Author: Michael Sproul <[email protected]> Date: Thu Jan 25 15:10:19 2024 +1100 Merge remote-tracking branch 'origin/unstable' into tree-states commit 6262be7 Author: Michael Sproul <[email protected]> Date: Sun Jan 14 09:41:42 2024 +1100 Don't error on inactive indices in att. rewards commit 9cd9243 Author: Michael Sproul <[email protected]> Date: Fri Jan 12 10:50:34 2024 +1100 Tree states release v4.6.111-exp commit 664a778 Author: Michael Sproul <[email protected]> Date: Thu Jan 11 17:13:43 2024 +1100 Add cache for parallel HTTP requests (sigp#4879) commit 8db17da Merge: c8dc082 2e8e160 Author: Michael Sproul <[email protected]> Date: Thu Jan 11 13:15:06 2024 +1100 Merge remote-tracking branch 'origin/unstable' into tree-states commit c8dc082 Merge: 4741bf1 a3a3703 Author: Michael Sproul <[email protected]> Date: Thu Dec 14 09:59:43 2023 +1100 Merge remote-tracking branch 'origin/unstable' into tree-states commit 4741bf1 Author: Michael Sproul <[email protected]> Date: Tue Dec 5 09:23:24 2023 +1100 Remove stray println commit cefe9fd Author: Michael Sproul <[email protected]> Date: Mon Dec 4 17:15:25 2023 +1100 Restore crash safety for database pruning (sigp#4975) * Add some DB sanity checks * Restore crash safety for database pruning commit 66d30bc Merge: e880d9d 44aaf13 Author: Michael Sproul <[email protected]> Date: Fri Dec 1 12:02:21 2023 +1100 Merge remote-tracking branch 'origin/unstable' into tree-states commit e880d9d Author: Michael Sproul <[email protected]> Date: Fri Dec 1 11:06:27 2023 +1100 Fix cache initialization in block rewards API (sigp#4960) commit 9cdc4b9 Merge: d36ebba 051c3e8 Author: Michael Sproul <[email protected]> Date: Sun Nov 12 22:19:07 2023 +0300 Merge remote-tracking branch 'origin/unstable' into tree-states commit d36ebba Author: Michael Sproul <[email protected]> Date: Thu Oct 26 18:09:28 2023 +1100 Handle out-of-order forks in epoch cache (sigp#4881) commit 1aca484 Author: Michael Sproul <[email protected]> Date: Fri Oct 20 12:31:41 2023 +1100 Tree states release v4.5.444-exp - Update xdelta3 to remove dodgy build steps - Fix asset paths for draft release commit 6b4f154 Author: Michael Sproul <[email protected]> Date: Thu Oct 19 15:57:11 2023 +1100 Tree states release v4.5.333-exp commit 0cb8fdf Author: Michael Sproul <[email protected]> Date: Thu Oct 19 14:59:29 2023 +1100 Various small tree-states fixes (sigp#4861) * Fix block backfill with genesis skip slots * Fix freezer upper limit * Fix: write post state in lcli skip-slots (sigp#4843) * Added CARGO_USE_GIT_CLI to the Dockerfile (sigp#4828) * chore: replace deprecated hub with gh for releases (sigp#4839) * Put schema version back to 24 (ignore Deneb) * Minimise diff --------- Co-authored-by: Joe Clapis <[email protected]> Co-authored-by: Dustin Brickwood <[email protected]> commit 72d8c38 Merge: bb6675e 98cac2b Author: Michael Sproul <[email protected]> Date: Thu Oct 19 12:07:35 2023 +1100 Merge remote-tracking branch 'origin/unstable' into tree-states commit bb6675e Author: Michael Sproul <[email protected]> Date: Fri Oct 13 16:45:56 2023 +1100 Clean up progressive balance slashings further (sigp#4834) * Clean up progressive balance slashings further * Fix Rayon deadlock in test utils * Fix cargo-fmt commit b121e69 Author: Michael Sproul <[email protected]> Date: Fri Oct 13 16:45:35 2023 +1100 Fix cache logic for epoch boundary skips (sigp#4833) commit b77de69 Author: Michael Sproul <[email protected]> Date: Wed Oct 11 14:37:46 2023 +1100 Re-enable ARM builds commit dfa3b43 Author: Michael Sproul <[email protected]> Date: Wed Oct 11 14:03:11 2023 +1100 Fix Clippy for 1.73 commit 6ae4c22 Author: Michael Sproul <[email protected]> Date: Wed Oct 11 11:57:39 2023 +1100 Fix merge snafu in tests commit e63d02e Merge: d4f87ef 4ad7e15 Author: Michael Sproul <[email protected]> Date: Wed Oct 11 11:52:39 2023 +1100 Merge remote-tracking branch 'origin/deneb-free-blobs' into tree-states-deneb commit d4f87ef Author: Michael Sproul <[email protected]> Date: Wed Oct 11 11:18:22 2023 +1100 Fix three consensus bugs! commit ca1abfe Author: Michael Sproul <[email protected]> Date: Wed Oct 11 10:43:49 2023 +1100 Support iterables in compare_fields commit e2a60a6 Merge: 9446fc8 203ac65 Author: Michael Sproul <[email protected]> Date: Fri Oct 6 11:11:36 2023 +1100 Merge remote-tracking branch 'origin/deneb-free-blobs' into tree-states-deneb commit 9446fc8 Author: Michael Sproul <[email protected]> Date: Tue Oct 3 16:07:25 2023 +1100 Fix semantic Deneb <> tree-states conflicts commit 109c4a5 Merge: f1f76f2 57edc0f Author: Michael Sproul <[email protected]> Date: Fri Sep 29 16:34:29 2023 +1000 Merge remote-tracking branch 'origin/deneb-free-blobs' into tree-states commit f1f76f2 Author: Michael Sproul <[email protected]> Date: Tue Sep 26 12:23:28 2023 +1000 Tree states release v4.5.222-exp (sigp#4782) commit cae73a4 Merge: 364074d 441fc16 Author: Michael Sproul <[email protected]> Date: Tue Sep 26 11:21:44 2023 +1000 Merge tag 'v4.5.0' into tree-states v4.5.0 commit 364074d Author: Michael Sproul <[email protected]> Date: Fri Sep 22 15:52:23 2023 +1000 Tree states release v4.5.111-exp (sigp#4769) commit d24875f Merge: cd23c89 69c39ad Author: Michael Sproul <[email protected]> Date: Fri Sep 22 15:11:42 2023 +1000 Merge remote-tracking branch 'origin/unstable' into tree-states commit cd23c89 Author: Michael Sproul <[email protected]> Date: Fri Sep 22 14:49:15 2023 +1000 Improve state cache eviction and reduce mem usage (sigp#4762) * Improve state cache eviction and reduce mem usage * Fix epochs_per_state_diff tests commit 1b4bc88 Author: Michael Sproul <[email protected]> Date: Thu Sep 14 10:07:26 2023 +1000 Release v4.4.111-exp (sigp#4729) commit 5cb2ed3 Author: Michael Sproul <[email protected]> Date: Wed Sep 13 14:43:02 2023 +1000 Restore custom image for Cross commit f7c6b7d Author: Michael Sproul <[email protected]> Date: Wed Sep 13 14:00:28 2023 +1000 Bump schema version to v24 commit 68f80cc Author: Michael Sproul <[email protected]> Date: Wed Sep 13 13:56:53 2023 +1000 Change default epochs-per-state-diff to 16 This should make replaying diffs during non-finality a bit quicker. commit 838e104 Author: Michael Sproul <[email protected]> Date: Wed Sep 13 13:54:03 2023 +1000 Attempt to fix flaky test commit d961d2c Author: Michael Sproul <[email protected]> Date: Wed Sep 13 12:51:20 2023 +1000 Disable ARM docker builds commit b8e04ce Merge: 1e4ee7a 35f47f4 Author: Michael Sproul <[email protected]> Date: Wed Sep 13 11:25:18 2023 +1000 Merge remote-tracking branch 'origin/unstable' into tree-states commit 1e4ee7a Author: Jimmy Chen <[email protected]> Date: Mon Sep 11 10:19:40 2023 +1000 Tree states to support per-slot state diffs (sigp#4652) * Support per slot state diffs * Store HierarchyConfig on disk. Support storing hdiffs at per slot level. * Revert HierachyConfig change for testing. * Add validity check for the hierarchy config when opening the DB. * Update HDiff tests. * Fix `get_cold_state` panic when the diff for the slot isn't stored. * Use slots instead of epochs for storing snapshots in freezer DB. * Add snapshot buffer to `diff_buffer_cache` instead of loading it from db every time. * Add `hierarchy-exponents` cli flag to beacon node. * Add test for `StorageStrategy::ReplayFrom` and ignore a flaky test. * Drop hierarchy_config in tests for more frequent snapshot and fix an issue where hdiff wasn't stored unless it's a epoch boundary slot. commit e373e9a Author: Michael Sproul <[email protected]> Date: Wed Aug 9 19:42:14 2023 +1000 Fix genesis state storage for genesis sync (sigp#4589) commit bba1526 Author: Michael Sproul <[email protected]> Date: Tue Aug 8 13:57:05 2023 +1000 Fix deadlock in finalization migration (sigp#4576) commit 18e64e6 Author: Michael Sproul <[email protected]> Date: Tue Aug 8 11:25:26 2023 +1000 Optimise mutations in single-pass epoch processing (sigp#4573) * Optimise mutations in single-pass epoch processing * Use safer Cow::make_mut * Update to upstream milhouse commit 8423e9f Merge: 5d2063d fc7f1ba Author: Michael Sproul <[email protected]> Date: Wed Jul 19 11:23:52 2023 +1000 Merge remote-tracking branch 'origin/unstable' into tree-states commit 5d2063d Author: Michael Sproul <[email protected]> Date: Tue Jul 18 16:59:55 2023 +1000 Single-pass epoch processing (sigp#4483) commit 079cd67 Merge: 0291998 835fa70 Author: Michael Sproul <[email protected]> Date: Mon Jul 3 15:03:54 2023 +1000 Merge remote-tracking branch 'origin/tree-states' into tree-states commit 0291998 Merge: b414c32 46be05f Author: Michael Sproul <[email protected]> Date: Mon Jul 3 15:01:21 2023 +1000 Merge remote-tracking branch 'origin/unstable' into tree-states commit b414c32 Author: Michael Sproul <[email protected]> Date: Mon Jul 3 12:03:14 2023 +1000 Implement activation queue cache commit 835fa70 Author: Michael Sproul <[email protected]> Date: Sat Jul 1 09:53:06 2023 +1000 Fix EpochCache handling in ef-tests (sigp#4454) commit f631b51 Author: Michael Sproul <[email protected]> Date: Fri Jun 30 22:57:36 2023 +1000 Fix EpochCache handling in ef-tests commit 2df714e Author: Jimmy Chen <[email protected]> Date: Fri Jun 30 11:25:51 2023 +1000 Tree states optimization using `EpochCache` (sigp#4429) * Relocate epoch cache to BeaconState * Optimize per block processing by pulling previous epoch & current epoch calculation up. * Revert `get_cow` change (no performance improvement) * Initialize `EpochCache` in epoch processing and load it from state when getting base rewards. * Initialize `EpochCache` at start of block processing if required. * Initialize `EpochCache` in `transition_blocks` if `exclude_cache_builds` is enabled * Fix epoch cache initialization logic * Remove FIXME comment. * Cache previous & current epochs in `consensus_context.rs`. * Move `get_base_rewards` from `ConsensusContext` to `BeaconState`. * Update Milhouse version commit 160bbde Author: Michael Sproul <[email protected]> Date: Fri Jun 30 10:29:34 2023 +1000 Fix db-migration-period default (sigp#4441) * Fix db-migration-period default * Fix version regex commit 6954de6 Author: Michael Sproul <[email protected]> Date: Tue Jun 27 17:34:41 2023 +1000 Tree states alpha release v4.2.990-exp commit 8dc374e Author: Michael Sproul <[email protected]> Date: Tue Jun 27 17:33:25 2023 +1000 Temporarily disable ARM builds commit af5fb20 Author: Michael Sproul <[email protected]> Date: Tue Jun 27 15:10:52 2023 +1000 Tree states alpha release v4.2.99-exp commit 56c7a52 Author: Michael Sproul <[email protected]> Date: Tue Jun 27 16:52:24 2023 +1000 Install Clang 5 in Cross builder image commit 7c2eb96 Author: Michael Sproul <[email protected]> Date: Tue Jun 27 15:06:43 2023 +1000 Set epochs per migration to 1 Workaround for sigp#4236 commit 88e30b6 Author: Michael Sproul <[email protected]> Date: Tue Jun 27 15:04:39 2023 +1000 Fix failing tests (sigp#4423) * Get tests passing * Get benchmarks compiling * Fix EF withdrawals test * Remove unused deps * Fix tree_hash panic in tests * Fix slasher compilation * Fix ssz_generic test * Get more tests passing * Fix EF tests for real * Fix local testnet scripts commit ca412ab Author: Michael Sproul <[email protected]> Date: Wed Jun 21 11:05:09 2023 +1000 Use rebasing to minimise BeaconState mem usage (sigp#4416) * Use "rebasing" to minimise BeaconState mem usage * Update metastruct * Use upstream milhouse, update cargo lock * Rebase caches for extra memory savings commit 6eb1513 Author: Michael Sproul <[email protected]> Date: Tue Jun 20 19:10:05 2023 +1000 Configurable diff buffer cache size (sigp#4420) commit d56cec8 Author: Paul Hauner <[email protected]> Date: Tue Jun 20 11:47:52 2023 +1000 Address clippy lints in `tree-states` (sigp#4414) * Address some clippy lints * Box errors to fix error size lint * Add Default impl for Validator * Address more clippy lints * Re-implement `check_state_diff` * Fix misc test compile errors commit 23db089 Author: Michael Sproul <[email protected]> Date: Mon Jun 19 10:14:47 2023 +1000 Implement tree states & hierarchical state DB
1 parent 20f53e7 commit 50a03dd

File tree

21 files changed

+126
-111
lines changed

21 files changed

+126
-111
lines changed

beacon_node/beacon_chain/src/attestation_rewards.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{BeaconChain, BeaconChainError, BeaconChainTypes};
22
use eth2::lighthouse::attestation_rewards::{IdealAttestationRewards, TotalAttestationRewards};
33
use eth2::lighthouse::StandardAttestationRewards;
4-
use participation_cache::ParticipationCache;
54
use safe_arith::SafeArith;
65
use serde_utils::quoted_u64::Quoted;
76
use slog::debug;
@@ -10,7 +9,7 @@ use state_processing::per_epoch_processing::altair::{
109
};
1110
use state_processing::{
1211
common::altair::BaseRewardPerIncrement,
13-
per_epoch_processing::altair::{participation_cache, rewards_and_penalties::get_flag_weight},
12+
per_epoch_processing::altair::rewards_and_penalties::get_flag_weight,
1413
};
1514
use std::collections::HashMap;
1615
use store::consts::altair::{
@@ -134,8 +133,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
134133
let spec = &self.spec;
135134

136135
// Calculate ideal_rewards
137-
let participation_cache = ParticipationCache::new(&state, spec)
138-
.map_err(|_| BeaconChainError::AttestationRewardsError)?;
139136
process_justification_and_finalization(&state)?.apply_changes_to_state(&mut state);
140137
process_inactivity_updates_slow(&mut state, spec)?;
141138

@@ -147,14 +144,14 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
147144
let weight = get_flag_weight(flag_index)
148145
.map_err(|_| BeaconChainError::AttestationRewardsError)?;
149146

150-
let unslashed_participating_balance = participation_cache
151-
.previous_epoch_flag_attesting_balance(flag_index)
152-
.map_err(|_| BeaconChainError::AttestationRewardsError)?;
147+
let unslashed_participating_balance = state
148+
.progressive_balances_cache()
149+
.previous_epoch_flag_attesting_balance(flag_index)?;
153150

154151
let unslashed_participating_increments =
155152
unslashed_participating_balance.safe_div(spec.effective_balance_increment)?;
156153

157-
let total_active_balance = participation_cache.current_epoch_total_active_balance();
154+
let total_active_balance = state.get_total_active_balance()?;
158155

159156
let active_increments =
160157
total_active_balance.safe_div(spec.effective_balance_increment)?;
@@ -190,15 +187,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
190187
let mut total_rewards: Vec<TotalAttestationRewards> = Vec::new();
191188

192189
let validators = if validators.is_empty() {
193-
participation_cache.eligible_validator_indices().to_vec()
190+
Self::all_eligible_validator_indices(&state, previous_epoch)?
194191
} else {
195192
Self::validators_ids_to_indices(&mut state, validators)?
196193
};
197194

198195
for &validator_index in &validators {
199196
// Return 0s for unknown/inactive validator indices. This is a bit different from stable
200197
// where we error for unknown pubkeys.
201-
let Ok(validator) = participation_cache.get_validator(validator_index) else {
198+
let Ok(validator) = state.get_validator(validator_index) else {
202199
debug!(
203200
self.log,
204201
"No rewards for inactive/unknown validator";
@@ -215,22 +212,25 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
215212
});
216213
continue;
217214
};
218-
let eligible = validator.is_eligible;
215+
let previous_epoch_participation_flags = state
216+
.previous_epoch_participation()?
217+
.get(validator_index)
218+
.ok_or(BeaconChainError::AttestationRewardsError)?;
219+
let eligible = state.is_eligible_validator(previous_epoch, validator)?;
219220
let mut head_reward = 0i64;
220221
let mut target_reward = 0i64;
221222
let mut source_reward = 0i64;
222223
let mut inactivity_penalty = 0i64;
223224

224225
if eligible {
225-
let effective_balance = validator.effective_balance;
226+
let effective_balance = validator.effective_balance();
226227

227228
for flag_index in 0..PARTICIPATION_FLAG_WEIGHTS.len() {
228229
let (ideal_reward, penalty) = ideal_rewards_hashmap
229230
.get(&(flag_index, effective_balance))
230231
.ok_or(BeaconChainError::AttestationRewardsError)?;
231-
let voted_correctly = validator
232-
.is_unslashed_participating_index(flag_index)
233-
.map_err(|_| BeaconChainError::AttestationRewardsError)?;
232+
let voted_correctly = !validator.slashed()
233+
&& previous_epoch_participation_flags.has_flag(flag_index)?;
234234
if voted_correctly {
235235
if flag_index == TIMELY_HEAD_FLAG_INDEX {
236236
head_reward += *ideal_reward as i64;
@@ -246,9 +246,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
246246

247247
let penalty_numerator = effective_balance
248248
.safe_mul(state.get_inactivity_score(validator_index)?)?;
249-
let penalty_denominator = spec
250-
.inactivity_score_bias
251-
.safe_mul(spec.inactivity_penalty_quotient_for_state(&state))?;
249+
let penalty_denominator = spec.inactivity_score_bias.safe_mul(
250+
spec.inactivity_penalty_quotient_for_fork(state.fork_name_unchecked()),
251+
)?;
252252
inactivity_penalty =
253253
-(penalty_numerator.safe_div(penalty_denominator)? as i64);
254254
} else if flag_index == TIMELY_SOURCE_FLAG_INDEX {
@@ -314,6 +314,24 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
314314
Ok(max_steps)
315315
}
316316

317+
fn all_eligible_validator_indices(
318+
state: &BeaconState<T::EthSpec>,
319+
previous_epoch: Epoch,
320+
) -> Result<Vec<usize>, BeaconChainError> {
321+
state
322+
.validators()
323+
.iter()
324+
.enumerate()
325+
.filter_map(|(i, validator)| {
326+
state
327+
.is_eligible_validator(previous_epoch, validator)
328+
.map(|eligible| eligible.then_some(i))
329+
.map_err(BeaconChainError::BeaconStateError)
330+
.transpose()
331+
})
332+
.collect()
333+
}
334+
317335
fn validators_ids_to_indices(
318336
state: &mut BeaconState<T::EthSpec>,
319337
validators: Vec<ValidatorId>,

beacon_node/http_api/src/attestation_performance.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use eth2::lighthouse::{
33
AttestationPerformance, AttestationPerformanceQuery, AttestationPerformanceStatistics,
44
};
55
use state_processing::{
6-
per_epoch_processing::altair::participation_cache::Error as ParticipationCacheError,
76
per_epoch_processing::EpochProcessingSummary, BlockReplayError, BlockReplayer,
87
};
98
use std::sync::Arc;
@@ -18,7 +17,6 @@ const BLOCK_ROOT_CHUNK_SIZE: usize = 100;
1817
enum AttestationPerformanceError {
1918
BlockReplay(#[allow(dead_code)] BlockReplayError),
2019
BeaconState(#[allow(dead_code)] BeaconStateError),
21-
ParticipationCache(#[allow(dead_code)] ParticipationCacheError),
2220
UnableToFindValidator(#[allow(dead_code)] usize),
2321
}
2422

@@ -34,12 +32,6 @@ impl From<BeaconStateError> for AttestationPerformanceError {
3432
}
3533
}
3634

37-
impl From<ParticipationCacheError> for AttestationPerformanceError {
38-
fn from(e: ParticipationCacheError) -> Self {
39-
Self::ParticipationCache(e)
40-
}
41-
}
42-
4335
pub fn get_attestation_performance<T: BeaconChainTypes>(
4436
target: String,
4537
query: AttestationPerformanceQuery,

beacon_node/http_api/src/validator_inclusion.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub fn global_validator_inclusion_data<T: BeaconChainTypes>(
4747

4848
Ok(GlobalValidatorInclusionData {
4949
current_epoch_active_gwei: summary.current_epoch_total_active_balance(),
50-
previous_epoch_active_gwei: summary.previous_epoch_total_active_balance(),
5150
current_epoch_target_attesting_gwei: summary
5251
.current_epoch_target_attesting_balance()
5352
.map_err(convert_cache_error)?,

common/eth2/src/lighthouse.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ pub struct Peer<T: EthSpec> {
5454
pub struct GlobalValidatorInclusionData {
5555
/// The total effective balance of all active validators during the _current_ epoch.
5656
pub current_epoch_active_gwei: u64,
57-
/// The total effective balance of all active validators during the _previous_ epoch.
58-
pub previous_epoch_active_gwei: u64,
5957
/// The total effective balance of all validators who attested during the _current_ epoch and
6058
/// agreed with the state about the beacon block at the first slot of the _current_ epoch.
6159
pub current_epoch_target_attesting_gwei: u64,

consensus/fork_choice/src/fork_choice.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,11 @@ where
747747
| BeaconBlockRef::Capella(_)
748748
| BeaconBlockRef::Merge(_)
749749
| BeaconBlockRef::Altair(_) => {
750-
// FIXME(sproul): initialize progressive balances
750+
// NOTE: Processing justification & finalization requires the progressive
751+
// balances cache, but we cannot initialize it here as we only have an
752+
// immutable reference. The state *should* have come straight from block
753+
// processing, which initialises the cache, but if we add other `on_block`
754+
// calls in future it could be worth passing a mutable reference.
751755
per_epoch_processing::altair::process_justification_and_finalization(state)?
752756
}
753757
BeaconBlockRef::Base(_) => {

consensus/state_processing/src/all_caches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<E: EthSpec> AllCaches for BeaconState<E> {
2424
fn build_all_caches(&mut self, spec: &ChainSpec) -> Result<(), EpochCacheError> {
2525
self.build_caches(spec)?;
2626
initialize_epoch_cache(self, spec)?;
27-
initialize_progressive_balances_cache(self, None, spec)?;
27+
initialize_progressive_balances_cache(self, spec)?;
2828
Ok(())
2929
}
3030

consensus/state_processing/src/common/update_progressive_balances_cache.rs

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@ use crate::metrics::{
33
PARTICIPATION_CURR_EPOCH_TARGET_ATTESTING_GWEI_PROGRESSIVE_TOTAL,
44
PARTICIPATION_PREV_EPOCH_TARGET_ATTESTING_GWEI_PROGRESSIVE_TOTAL,
55
};
6-
use crate::per_epoch_processing::altair::ParticipationCache;
76
use crate::{BlockProcessingError, EpochProcessingError};
87
use lighthouse_metrics::set_gauge;
9-
use std::borrow::Cow;
108
use types::{
119
is_progressive_balances_enabled, BeaconState, BeaconStateError, ChainSpec, Epoch,
12-
EpochTotalBalances, EthSpec, ProgressiveBalancesCache,
10+
EpochTotalBalances, EthSpec, ParticipationFlags, ProgressiveBalancesCache, Validator,
1311
};
1412

15-
/// Initializes the `ProgressiveBalancesCache` cache using balance values from the
16-
/// `ParticipationCache`. If the optional `&ParticipationCache` is not supplied, it will be computed
17-
/// from the `BeaconState`.
13+
/// Initializes the `ProgressiveBalancesCache` if it is unbuilt.
1814
pub fn initialize_progressive_balances_cache<E: EthSpec>(
1915
state: &mut BeaconState<E>,
20-
maybe_participation_cache: Option<&ParticipationCache>,
2116
spec: &ChainSpec,
2217
) -> Result<(), BeaconStateError> {
2318
if !is_progressive_balances_enabled(state)
@@ -26,29 +21,37 @@ pub fn initialize_progressive_balances_cache<E: EthSpec>(
2621
return Ok(());
2722
}
2823

29-
// FIXME(sproul): simplify the participation cache
30-
let participation_cache = match maybe_participation_cache {
31-
Some(cache) => Cow::Borrowed(cache),
32-
None => {
33-
state.build_total_active_balance_cache_at(state.current_epoch(), spec)?;
34-
Cow::Owned(
35-
ParticipationCache::new(state, spec)
36-
.map_err(|e| BeaconStateError::ParticipationCacheError(format!("{e:?}")))?,
37-
)
24+
// Calculate the total flag balances for previous & current epoch in a single iteration.
25+
// This calculates `get_total_balance(unslashed_participating_indices(..))` for each flag in
26+
// the current and previous epoch.
27+
let current_epoch = state.current_epoch();
28+
let previous_epoch = state.previous_epoch();
29+
let mut previous_epoch_cache = EpochTotalBalances::new(spec);
30+
let mut current_epoch_cache = EpochTotalBalances::new(spec);
31+
for ((validator, current_epoch_flags), previous_epoch_flags) in state
32+
.validators()
33+
.iter()
34+
.zip(state.current_epoch_participation()?)
35+
.zip(state.previous_epoch_participation()?)
36+
{
37+
// Exclude slashed validators. We are calculating *unslashed* participating totals.
38+
if validator.slashed() {
39+
continue;
3840
}
39-
};
4041

41-
let current_epoch = state.current_epoch();
42-
let previous_epoch_cache = EpochTotalBalances {
43-
total_flag_balances: participation_cache
44-
.previous_epoch_participation
45-
.total_flag_balances,
46-
};
47-
let current_epoch_cache = EpochTotalBalances {
48-
total_flag_balances: participation_cache
49-
.current_epoch_participation
50-
.total_flag_balances,
51-
};
42+
// Update current epoch flag balances.
43+
if validator.is_active_at(current_epoch) {
44+
update_flag_total_balances(&mut current_epoch_cache, *current_epoch_flags, validator)?;
45+
}
46+
// Update previous epoch flag balances.
47+
if validator.is_active_at(previous_epoch) {
48+
update_flag_total_balances(
49+
&mut previous_epoch_cache,
50+
*previous_epoch_flags,
51+
validator,
52+
)?;
53+
}
54+
}
5255

5356
state.progressive_balances_cache_mut().initialize(
5457
current_epoch,
@@ -61,6 +64,26 @@ pub fn initialize_progressive_balances_cache<E: EthSpec>(
6164
Ok(())
6265
}
6366

67+
/// During the initialization of the progressive balances for a single epoch, add
68+
/// `validator.effective_balance` to the flag total, for each flag present in `participation_flags`.
69+
///
70+
/// Pre-conditions:
71+
///
72+
/// - `validator` must not be slashed
73+
/// - the `participation_flags` must be for `validator` in the same epoch as the `total_balances`
74+
fn update_flag_total_balances(
75+
total_balances: &mut EpochTotalBalances,
76+
participation_flags: ParticipationFlags,
77+
validator: &Validator,
78+
) -> Result<(), BeaconStateError> {
79+
for (flag, balance) in total_balances.total_flag_balances.iter_mut().enumerate() {
80+
if participation_flags.has_flag(flag)? {
81+
balance.safe_add_assign(validator.effective_balance())?;
82+
}
83+
}
84+
Ok(())
85+
}
86+
6487
/// Updates the `ProgressiveBalancesCache` when a new target attestation has been processed.
6588
pub fn update_progressive_balances_on_attestation<T: EthSpec>(
6689
state: &mut BeaconState<T>,

consensus/state_processing/src/per_block_processing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub fn per_block_processing<T: EthSpec, Payload: AbstractExecPayload<T>>(
121121

122122
// Build epoch cache if it hasn't already been built, or if it is no longer valid
123123
initialize_epoch_cache(state, spec)?;
124-
initialize_progressive_balances_cache(state, None, spec)?;
124+
initialize_progressive_balances_cache(state, spec)?;
125125
state.build_slashings_cache()?;
126126

127127
let verify_signatures = match block_signature_strategy {

consensus/state_processing/src/per_block_processing/altair/sync_committee.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::{signature_sets::sync_aggregate_signature_set, VerifySignatures};
44
use safe_arith::SafeArith;
55
use std::borrow::Cow;
66
use types::consts::altair::{PROPOSER_WEIGHT, SYNC_REWARD_WEIGHT, WEIGHT_DENOMINATOR};
7-
use types::{BeaconState, ChainSpec, EthSpec, PublicKeyBytes, SyncAggregate, Unsigned};
7+
use types::{
8+
BeaconState, BeaconStateError, ChainSpec, EthSpec, PublicKeyBytes, SyncAggregate, Unsigned,
9+
};
810

911
pub fn process_sync_aggregate<T: EthSpec>(
1012
state: &mut BeaconState<T>,
@@ -47,20 +49,34 @@ pub fn process_sync_aggregate<T: EthSpec>(
4749
// Apply participant and proposer rewards
4850
let committee_indices = state.get_sync_committee_indices(&current_sync_committee)?;
4951

50-
let mut total_proposer_reward = 0;
52+
let proposer_index = proposer_index as usize;
53+
let mut proposer_balance = *state
54+
.balances()
55+
.get(proposer_index)
56+
.ok_or(BeaconStateError::BalancesOutOfBounds(proposer_index))?;
57+
5158
for (participant_index, participation_bit) in committee_indices
5259
.into_iter()
5360
.zip(aggregate.sync_committee_bits.iter())
5461
{
5562
// FIXME(sproul): double-check this for Capella, proposer shouldn't have 0 effective balance
5663
if participation_bit {
57-
increase_balance(state, participant_index, participant_reward)?;
58-
total_proposer_reward.safe_add_assign(proposer_reward)?;
64+
// Accumulate proposer rewards in a temp var in case the proposer has very low balance, is
65+
// part of the sync committee, does not participate and its penalties saturate.
66+
if participant_index == proposer_index {
67+
proposer_balance.safe_add_assign(participant_reward)?;
68+
} else {
69+
increase_balance(state, participant_index, participant_reward)?;
70+
}
71+
proposer_balance.safe_add_assign(proposer_reward)?;
72+
} else if participant_index == proposer_index {
73+
proposer_balance = proposer_balance.saturating_sub(participant_reward);
5974
} else {
6075
decrease_balance(state, participant_index, participant_reward)?;
6176
}
6277
}
63-
increase_balance(state, proposer_index as usize, total_proposer_reward)?;
78+
79+
*state.get_balance_mut(proposer_index)? = proposer_balance;
6480

6581
Ok(())
6682
}

consensus/state_processing/src/per_block_processing/errors.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use super::signature_sets::Error as SignatureSetError;
2-
use crate::per_epoch_processing::altair::participation_cache;
32
use crate::{ContextError, EpochCacheError};
43
use merkle_proof::MerkleTreeError;
5-
use participation_cache::Error as ParticipationCacheError;
64
use safe_arith::ArithError;
75
use ssz::DecodeError;
86
use types::*;
@@ -91,7 +89,6 @@ pub enum BlockProcessingError {
9189
found: Hash256,
9290
},
9391
WithdrawalCredentialsInvalid,
94-
ParticipationCacheError(ParticipationCacheError),
9592
}
9693

9794
impl From<BeaconStateError> for BlockProcessingError {
@@ -161,12 +158,6 @@ impl From<BlockOperationError<HeaderInvalid>> for BlockProcessingError {
161158
}
162159
}
163160

164-
impl From<ParticipationCacheError> for BlockProcessingError {
165-
fn from(e: ParticipationCacheError) -> Self {
166-
BlockProcessingError::ParticipationCacheError(e)
167-
}
168-
}
169-
170161
/// A conversion that consumes `self` and adds an `index` variable to resulting struct.
171162
///
172163
/// Used here to allow converting an error into an upstream error that points to the object that

0 commit comments

Comments
 (0)