Prevent writing to state cache when migrating the database (#7067)

* add an update_cache flag to get_state to have more granular control over when we write to the cache

* State cache tweaks

- add state-cache-headroom flag to control pruning
- prune old epoch boundary states ahead of mid-epoch states
- never prune head block's state
- avoid caching ancestor states unless they are on an epoch boundary

---------

Co-authored-by: Michael Sproul <michael@sigmaprime.io>
This commit is contained in:
Eitan Seri-Levi
2025-03-03 17:15:12 -07:00
committed by GitHub
parent 8f62b1934a
commit defdc595fc
28 changed files with 178 additions and 77 deletions

View File

@@ -821,7 +821,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.get_blinded_block(&block_root)?
.ok_or(Error::MissingBeaconBlock(block_root))?;
let state = self
.get_state(&block.state_root(), Some(block.slot()))?
.get_state(&block.state_root(), Some(block.slot()), true)?
.ok_or_else(|| Error::MissingBeaconState(block.state_root()))?;
let iter = BlockRootsIterator::owned(&self.store, state);
Ok(std::iter::once(Ok((block_root, block.slot())))
@@ -1348,8 +1348,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
&self,
state_root: &Hash256,
slot: Option<Slot>,
update_cache: bool,
) -> Result<Option<BeaconState<T::EthSpec>>, Error> {
Ok(self.store.get_state(state_root, slot)?)
Ok(self.store.get_state(state_root, slot, update_cache)?)
}
/// Return the sync committee at `slot + 1` from the canonical chain.
@@ -1524,8 +1525,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
})?
.ok_or(Error::NoStateForSlot(slot))?;
// Since this slot is less than the current head, don't cache the state
Ok(self
.get_state(&state_root, Some(slot))?
.get_state(&state_root, Some(slot), false)?
.ok_or(Error::NoStateForSlot(slot))?)
}
}
@@ -6943,7 +6945,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let mut beacon_state = self
.store
.get_state(&beacon_state_root, Some(beacon_block.slot()))?
.get_state(&beacon_state_root, Some(beacon_block.slot()), true)?
.ok_or_else(|| {
Error::DBInconsistent(format!("Missing state {:?}", beacon_state_root))
})?;
@@ -7096,7 +7098,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
if signed_beacon_block.slot() % T::EthSpec::slots_per_epoch() == 0 {
let block = self.get_blinded_block(&block_hash).unwrap().unwrap();
let state = self
.get_state(&block.state_root(), Some(block.slot()))
.get_state(&block.state_root(), Some(block.slot()), true)
.unwrap()
.unwrap();
finalized_blocks.insert(state.finalized_checkpoint().root);