diff --git a/beacon_node/store/src/state_cache.rs b/beacon_node/store/src/state_cache.rs index 30d0e2b2d5..352760e808 100644 --- a/beacon_node/store/src/state_cache.rs +++ b/beacon_node/store/src/state_cache.rs @@ -186,8 +186,13 @@ impl StateCache { state: &mut BeaconState, spec: &ChainSpec, ) -> Result<(), Error> { + // Do not attempt to rebase states prior to the finalized state. This method might be called + // with states on the hdiff grid prior to finalization, as part of the reconstruction of + // some later unfinalized state. if let Some(finalized_state) = &self.finalized_state { - state.rebase_on(&finalized_state.state, spec)?; + if state.slot() >= finalized_state.state.slot() { + state.rebase_on(&finalized_state.state, spec)?; + } } Ok(()) } diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index c7dfabbfee..8bfbce87d3 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -2535,11 +2535,17 @@ impl BeaconState { pub fn rebase_caches_on(&mut self, base: &Self, spec: &ChainSpec) -> Result<(), Error> { // Use pubkey cache from `base` if it contains superior information (likely if our cache is - // uninitialized). + // uninitialized). Be careful not to use a cache which has *more* validators than expected, + // as other code expects `self.pubkey_cache().len() <= self.validators.len()`. let num_validators = self.validators().len(); let pubkey_cache = self.pubkey_cache_mut(); let base_pubkey_cache = base.pubkey_cache(); - if pubkey_cache.len() < base_pubkey_cache.len() && pubkey_cache.len() < num_validators { + + let current_cache_is_incomplete = pubkey_cache.len() < num_validators; + let base_cache_is_compatible = base_pubkey_cache.len() <= num_validators; + let base_cache_is_superior = base_pubkey_cache.len() > pubkey_cache.len(); + + if current_cache_is_incomplete && base_cache_is_compatible && base_cache_is_superior { *pubkey_cache = base_pubkey_cache.clone(); }