Skip to content

Commit

Permalink
Fix Electra Fork Choice Tests (#5764)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethDreamer authored May 12, 2024
1 parent e1dcfb6 commit 3b1fb0a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 44 deletions.
5 changes: 2 additions & 3 deletions beacon_node/store/src/consensus_context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ssz_derive::{Decode, Encode};
use state_processing::ConsensusContext;
use std::collections::HashMap;
use types::{AttestationData, BitList, EthSpec, Hash256, IndexedAttestation, Slot};
use types::{EthSpec, Hash256, IndexedAttestation, Slot};

/// The consensus context is stored on disk as part of the data availability overflow cache.
///
Expand All @@ -21,8 +21,7 @@ pub struct OnDiskConsensusContext<E: EthSpec> {
///
/// They are not part of the on-disk format.
#[ssz(skip_serializing, skip_deserializing)]
indexed_attestations:
HashMap<(AttestationData, BitList<E::MaxValidatorsPerSlot>), IndexedAttestation<E>>,
indexed_attestations: HashMap<Hash256, IndexedAttestation<E>>,
}

impl<E: EthSpec> OnDiskConsensusContext<E> {
Expand Down
61 changes: 20 additions & 41 deletions consensus/state_processing/src/consensus_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use crate::EpochCacheError;
use std::collections::{hash_map::Entry, HashMap};
use tree_hash::TreeHash;
use types::{
AbstractExecPayload, AttestationData, AttestationRef, BeaconState, BeaconStateError, BitList,
ChainSpec, Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationRef,
SignedBeaconBlock, Slot,
AbstractExecPayload, AttestationRef, BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec,
Hash256, IndexedAttestation, IndexedAttestationRef, SignedBeaconBlock, Slot,
};

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -22,8 +21,7 @@ pub struct ConsensusContext<E: EthSpec> {
/// Block root of the block at `slot`.
pub current_block_root: Option<Hash256>,
/// Cache of indexed attestations constructed during block processing.
pub indexed_attestations:
HashMap<(AttestationData, BitList<E::MaxValidatorsPerSlot>), IndexedAttestation<E>>,
pub indexed_attestations: HashMap<Hash256, IndexedAttestation<E>>,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -154,41 +152,25 @@ impl<E: EthSpec> ConsensusContext<E> {
state: &BeaconState<E>,
attestation: AttestationRef<'a, E>,
) -> Result<IndexedAttestationRef<E>, BlockOperationError<AttestationInvalid>> {
let key = attestation.tree_hash_root();
match attestation {
AttestationRef::Base(attn) => {
let extended_aggregation_bits = attn
.extend_aggregation_bits()
.map_err(BeaconStateError::from)?;

let key = (attn.data.clone(), extended_aggregation_bits);

match self.indexed_attestations.entry(key) {
Entry::Occupied(occupied) => Ok(occupied.into_mut()),
Entry::Vacant(vacant) => {
let committee =
state.get_beacon_committee(attn.data.slot, attn.data.index)?;
let indexed_attestation = attesting_indices_base::get_indexed_attestation(
committee.committee,
attn,
)?;
Ok(vacant.insert(indexed_attestation))
}
AttestationRef::Base(attn) => match self.indexed_attestations.entry(key) {
Entry::Occupied(occupied) => Ok(occupied.into_mut()),
Entry::Vacant(vacant) => {
let committee = state.get_beacon_committee(attn.data.slot, attn.data.index)?;
let indexed_attestation =
attesting_indices_base::get_indexed_attestation(committee.committee, attn)?;
Ok(vacant.insert(indexed_attestation))
}
}
AttestationRef::Electra(attn) => {
let key = (attn.data.clone(), attn.aggregation_bits.clone());

match self.indexed_attestations.entry(key) {
Entry::Occupied(occupied) => Ok(occupied.into_mut()),
Entry::Vacant(vacant) => {
let indexed_attestation =
attesting_indices_electra::get_indexed_attestation_from_state(
state, attn,
)?;
Ok(vacant.insert(indexed_attestation))
}
},
AttestationRef::Electra(attn) => match self.indexed_attestations.entry(key) {
Entry::Occupied(occupied) => Ok(occupied.into_mut()),
Entry::Vacant(vacant) => {
let indexed_attestation =
attesting_indices_electra::get_indexed_attestation_from_state(state, attn)?;
Ok(vacant.insert(indexed_attestation))
}
}
},
}
.map(|indexed_attestation| (*indexed_attestation).to_ref())
}
Expand All @@ -200,10 +182,7 @@ impl<E: EthSpec> ConsensusContext<E> {
#[must_use]
pub fn set_indexed_attestations(
mut self,
attestations: HashMap<
(AttestationData, BitList<E::MaxValidatorsPerSlot>),
IndexedAttestation<E>,
>,
attestations: HashMap<Hash256, IndexedAttestation<E>>,
) -> Self {
self.indexed_attestations = attestations;
self
Expand Down

0 comments on commit 3b1fb0a

Please sign in to comment.