Files
lighthouse/consensus/state_processing/src/all_caches.rs
Michael Sproul c754234b2c Fix bugs in proposer calculation post-Fulu (#8101)
As identified by a researcher during the Fusaka security competition, we were computing the proposer index incorrectly in some places by computing without lookahead.


  - [x] Add "low level" checks to computation functions in `consensus/types` to ensure they error cleanly
- [x] Re-work the determination of proposer shuffling decision roots, which are now fork aware.
- [x] Re-work and simplify the beacon proposer cache to be fork-aware.
- [x] Optimise `with_proposer_cache` to use `OnceCell`.
- [x] All tests passing.
- [x] Resolve all remaining `FIXME(sproul)`s.
- [x] Unit tests for `ProtoBlock::proposer_shuffling_root_for_child_block`.
- [x] End-to-end regression test.
- [x] Test on pre-Fulu network.
- [x] Test on post-Fulu network.


Co-Authored-By: Michael Sproul <michael@sigmaprime.io>
2025-09-26 14:44:50 +00:00

56 lines
2.2 KiB
Rust

use crate::common::update_progressive_balances_cache::initialize_progressive_balances_cache;
use crate::epoch_cache::initialize_epoch_cache;
use tracing::instrument;
use types::{BeaconState, ChainSpec, EpochCacheError, EthSpec, Hash256, RelativeEpoch};
/// Mixin trait for the beacon state that provides operations on *all* caches.
///
/// The reason this trait exists here away from `BeaconState` itself is that some caches are
/// computed by functions in `state_processing`.
pub trait AllCaches {
/// Build all caches.
///
/// Note that this excludes milhouse's intrinsic tree-hash cache. That needs to be managed
/// separately.
fn build_all_caches(&mut self, spec: &ChainSpec) -> Result<(), EpochCacheError>;
/// Return true if all caches are built.
///
/// Note that this excludes milhouse's intrinsic tree-hash cache. That needs to be managed
/// separately.
fn all_caches_built(&self) -> bool;
}
impl<E: EthSpec> AllCaches for BeaconState<E> {
#[instrument(skip_all)]
fn build_all_caches(&mut self, spec: &ChainSpec) -> Result<(), EpochCacheError> {
self.build_caches(spec)?;
initialize_epoch_cache(self, spec)?;
initialize_progressive_balances_cache(self, spec)?;
Ok(())
}
fn all_caches_built(&self) -> bool {
let current_epoch = self.current_epoch();
let Ok(epoch_cache_decision_block_root) = self.epoch_cache_decision_root(Hash256::ZERO)
else {
return false;
};
self.get_total_active_balance_at_epoch(current_epoch)
.is_ok()
&& self.committee_cache_is_initialized(RelativeEpoch::Previous)
&& self.committee_cache_is_initialized(RelativeEpoch::Current)
&& self.committee_cache_is_initialized(RelativeEpoch::Next)
&& self
.progressive_balances_cache()
.is_initialized_at(current_epoch)
&& self.pubkey_cache().len() == self.validators().len()
&& self.exit_cache().check_initialized().is_ok()
&& self.slashings_cache_is_initialized()
&& self
.epoch_cache()
.check_validity(current_epoch, epoch_cache_decision_block_root)
.is_ok()
}
}