diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 701d900c7..d02ab3176 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -523,15 +523,24 @@ impl BeaconChain { result } + /// Retrieves the `BeaconState` used to create the attestation. fn get_attestation_state(&self, attestation: &Attestation) -> Option> { + // Current state is used if the attestation targets a historic block and a slot within an + // equal or adjacent epoch. + let slots_per_epoch = T::EthSpec::slots_per_epoch(); + let min_slot = (self.state.read().slot.epoch(slots_per_epoch) - 1).start_slot(slots_per_epoch); let blocks = BestBlockRootsIterator::owned(self.store.clone(), self.state.read().clone(), self.state.read().slot.clone()); for (root, slot) in blocks { - if root == attestation.data.target_root - && self.slot_epochs_equal_or_adjacent(slot, self.state.read().slot) { + if root == attestation.data.target_root { return Some(self.state.read().clone()); } + + if slot == min_slot { + break; + } }; + // A different state is retrieved from the database. match self.store.get::(&attestation.data.target_root) { Ok(Some(block)) => match self.store.get::>(&block.state_root) { Ok(state) => state, @@ -541,16 +550,6 @@ impl BeaconChain { } } - fn slot_epochs_equal_or_adjacent(&self, slot_a: Slot, slot_b: Slot) -> bool { - let slots_per_epoch = T::EthSpec::slots_per_epoch(); - let epoch_a = slot_a.epoch(slots_per_epoch); - let epoch_b = slot_b.epoch(slots_per_epoch); - - epoch_a == epoch_b - || epoch_a + 1 == epoch_b - || epoch_b + 1 == epoch_a - } - /// Accept some deposit and queue it for inclusion in an appropriate block. pub fn process_deposit( &self,