Skip to content

Commit

Permalink
Keep indexed attestations, thanks Sean
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Apr 19, 2024
1 parent 3a16649 commit 62ebdac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct DietAvailabilityPendingExecutedBlock<E: EthSpec> {
parent_block: SignedBeaconBlock<E, BlindedPayload<E>>,
parent_eth1_finalization_data: Eth1FinalizationData,
confirmed_state_roots: Vec<Hash256>,
consensus_context: OnDiskConsensusContext,
consensus_context: OnDiskConsensusContext<E>,
payload_verification_outcome: PayloadVerificationOutcome,
}

Expand Down Expand Up @@ -96,7 +96,7 @@ impl<T: BeaconChainTypes> StateLRUCache<T> {
parent_eth1_finalization_data: executed_block.import_data.parent_eth1_finalization_data,
confirmed_state_roots: executed_block.import_data.confirmed_state_roots,
consensus_context: OnDiskConsensusContext::from_consensus_context(
&executed_block.import_data.consensus_context,
executed_block.import_data.consensus_context,
),
payload_verification_outcome: executed_block.payload_verification_outcome,
}
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<E: EthSpec> From<AvailabilityPendingExecutedBlock<E>>
parent_eth1_finalization_data: value.import_data.parent_eth1_finalization_data,
confirmed_state_roots: value.import_data.confirmed_state_roots,
consensus_context: OnDiskConsensusContext::from_consensus_context(
&value.import_data.consensus_context,
value.import_data.consensus_context,
),
payload_verification_outcome: value.payload_verification_outcome,
}
Expand Down
26 changes: 18 additions & 8 deletions beacon_node/store/src/consensus_context.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,56 @@
use ssz_derive::{Decode, Encode};
use state_processing::ConsensusContext;
use types::{EthSpec, Hash256, Slot};
use std::collections::HashMap;
use types::{AttestationData, BitList, EthSpec, Hash256, IndexedAttestation, Slot};

/// The consensus context is stored on disk as part of the data availability overflow cache.
///
/// We use this separate struct to keep the on-disk format stable in the presence of changes to the
/// in-memory `ConsensusContext`. You MUST NOT change the fields of this struct without
/// superstructing it and implementing a schema migration.
#[derive(Debug, PartialEq, Clone, Encode, Decode)]
pub struct OnDiskConsensusContext {
pub struct OnDiskConsensusContext<E: EthSpec> {
/// Slot to act as an identifier/safeguard
slot: Slot,
/// Proposer index of the block at `slot`.
proposer_index: Option<u64>,
/// Block root of the block at `slot`.
current_block_root: Option<Hash256>,
/// We keep the indexed attestations in the *in-memory* version of this struct so that we don't
/// need to regenerate them if roundtripping via this type *without* going to disk.
///
/// They are not part of the on-disk format.
#[ssz(skip_serializing, skip_deserializing)]
indexed_attestations:
HashMap<(AttestationData, BitList<E::MaxValidatorsPerCommittee>), IndexedAttestation<E>>,
}

impl OnDiskConsensusContext {
pub fn from_consensus_context<E: EthSpec>(ctxt: &ConsensusContext<E>) -> Self {
impl<E: EthSpec> OnDiskConsensusContext<E> {
pub fn from_consensus_context(ctxt: ConsensusContext<E>) -> Self {
// Match exhaustively on fields here so we are forced to *consider* updating the on-disk
// format when the `ConsensusContext` fields change.
let &ConsensusContext {
let ConsensusContext {
slot,
previous_epoch: _,
current_epoch: _,
proposer_index,
current_block_root,
indexed_attestations: _,
indexed_attestations,
} = ctxt;
OnDiskConsensusContext {
slot,
proposer_index,
current_block_root,
indexed_attestations,
}
}

pub fn into_consensus_context<E: EthSpec>(self) -> ConsensusContext<E> {
pub fn into_consensus_context(self) -> ConsensusContext<E> {
let OnDiskConsensusContext {
slot,
proposer_index,
current_block_root,
indexed_attestations,
} = self;

let mut ctxt = ConsensusContext::new(slot);
Expand All @@ -51,6 +61,6 @@ impl OnDiskConsensusContext {
if let Some(block_root) = current_block_root {
ctxt = ctxt.set_current_block_root(block_root);
}
ctxt
ctxt.set_indexed_attestations(indexed_attestations)
}
}
14 changes: 14 additions & 0 deletions consensus/state_processing/src/consensus_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<E: EthSpec> ConsensusContext<E> {
}
}

#[must_use]
pub fn set_proposer_index(mut self, proposer_index: u64) -> Self {
self.proposer_index = Some(proposer_index);
self
Expand Down Expand Up @@ -106,6 +107,7 @@ impl<E: EthSpec> ConsensusContext<E> {
Ok(proposer_index)
}

#[must_use]
pub fn set_current_block_root(mut self, block_root: Hash256) -> Self {
self.current_block_root = Some(block_root);
self
Expand Down Expand Up @@ -171,4 +173,16 @@ impl<E: EthSpec> ConsensusContext<E> {
pub fn num_cached_indexed_attestations(&self) -> usize {
self.indexed_attestations.len()
}

#[must_use]
pub fn set_indexed_attestations(
mut self,
attestations: HashMap<
(AttestationData, BitList<E::MaxValidatorsPerCommittee>),
IndexedAttestation<E>,
>,
) -> Self {
self.indexed_attestations = attestations;
self
}
}

0 comments on commit 62ebdac

Please sign in to comment.