Skip to content

Commit

Permalink
Check if block has more than 50% of data columns to accept it as valid
Browse files Browse the repository at this point in the history
  • Loading branch information
povi committed May 10, 2024
1 parent 7692d57 commit c86f3fc
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 23 deletions.
5 changes: 4 additions & 1 deletion eip_7594/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ use types::{
},
traits::{BeaconState, PostDenebBeaconBlockBody},
};
use types::{eip7594::{Cell, DATA_COLUMN_SIDECAR_SUBNET_COUNT}, preset::Preset};
use types::{
eip7594::{Cell, DATA_COLUMN_SIDECAR_SUBNET_COUNT},
preset::Preset,
};

const SAMPLES_PER_SLOT: u64 = 8;
const CUSTODY_REQUIREMENT: u64 = 1;
Expand Down
10 changes: 6 additions & 4 deletions fork_choice_control/src/mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,11 +1648,13 @@ where
.message
.hash_tree_root();

todo!();
// NESUKODINTAS DALYKAS
// self.store_mut().apply_blob_sidecar(blob_sidecar);
self.store_mut()
.apply_data_column_sidecar(data_column_sidecar);

// self.update_store_snapshot();
self.update_store_snapshot();

todo!();
// TODO(feature/eip-7594):

// if let Some(pending_block) = self.delayed_until_blobs.get(&block_root) {
// self.retry_block(wait_group.clone(), pending_block.clone());
Expand Down
86 changes: 70 additions & 16 deletions fork_choice_store/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::misc::DataColumnSidecarAction;
use core::{
cmp::Ordering,
ops::{AddAssign as _, Bound, SubAssign as _},
Expand Down Expand Up @@ -49,7 +48,7 @@ use types::{
containers::{BlobIdentifier, BlobSidecar},
primitives::{BlobIndex, KzgCommitment},
},
eip7594::{ColumnIndex, DataColumnSidecar},
eip7594::{ColumnIndex, DataColumnIdentifier, DataColumnSidecar},
nonstandard::{BlobSidecarWithId, PayloadStatus, Phase, WithStatus},
phase0::{
consts::{ATTESTATION_PROPAGATION_SLOT_RANGE, GENESIS_EPOCH, GENESIS_SLOT},
Expand All @@ -70,9 +69,10 @@ use crate::{
misc::{
AggregateAndProofAction, AggregateAndProofOrigin, ApplyBlockChanges, ApplyTickChanges,
AttestationAction, AttestationOrigin, AttesterSlashingOrigin, BlobSidecarAction,
BlobSidecarOrigin, BlockAction, BranchPoint, ChainLink, Difference, DifferenceAtLocation,
DissolvedDifference, LatestMessage, Location, PartialAttestationAction, PartialBlockAction,
PayloadAction, Score, SegmentId, UnfinalizedBlock, ValidAttestation,
BlobSidecarOrigin, BlockAction, BranchPoint, ChainLink, DataColumnSidecarAction,
Difference, DifferenceAtLocation, DissolvedDifference, LatestMessage, Location,
PartialAttestationAction, PartialBlockAction, PayloadAction, Score, SegmentId,
UnfinalizedBlock, ValidAttestation,
},
segment::{Position, Segment},
state_cache::StateCache,
Expand Down Expand Up @@ -214,8 +214,12 @@ pub struct Store<P: Preset> {
aggregate_and_proof_supersets: Arc<AggregateAndProofSupersets<P>>,
accepted_blob_sidecars:
HashMap<(Slot, ValidatorIndex, BlobIndex), HashMap<H256, KzgCommitment>>,
accepted_data_sidecars: HashSet<(Slot, ValidatorIndex, ColumnIndex)>,
accepted_data_column_sidecars: HashMap<
(Slot, ValidatorIndex, ColumnIndex),
HashMap<H256, ContiguousList<KzgCommitment, P::MaxBlobCommitmentsPerBlock>>,
>,
blob_cache: BlobCache<P>,
data_column_cache: HashMap<DataColumnIdentifier, (Arc<DataColumnSidecar<P>>, Slot)>,
rejected_block_roots: HashSet<H256>,
finished_initial_forward_sync: bool,
}
Expand Down Expand Up @@ -283,8 +287,9 @@ impl<P: Preset> Store<P> {
execution_payload_locations: hashmap! {},
aggregate_and_proof_supersets: Arc::new(AggregateAndProofSupersets::new()),
accepted_blob_sidecars: HashMap::default(),
accepted_data_sidecars: HashSet::default(),
accepted_data_column_sidecars: HashMap::default(),
blob_cache: BlobCache::default(),
data_column_cache: HashMap::default(),
rejected_block_roots: HashSet::default(),
finished_initial_forward_sync,
}
Expand Down Expand Up @@ -1008,8 +1013,19 @@ impl<P: Preset> Store<P> {
NullSlotReport,
)?;

if !self.indices_of_missing_blobs(&block).is_empty() {
return Ok(BlockAction::DelayUntilBlobs(block));
if self
.chain_config
.is_eip7594_fork(accessors::get_current_epoch(&state))
{
let missing_indices = self.indices_of_missing_data_columns(&block);

if missing_indices.len() * 2 >= NUMBER_OF_COLUMNS.try_into()? {
return Ok(BlockAction::DelayUntilBlobs(block));
}
} else {
if !self.indices_of_missing_blobs(&block).is_empty() {
return Ok(BlockAction::DelayUntilBlobs(block));
}
}

let attester_slashing_results = block
Expand Down Expand Up @@ -1892,7 +1908,7 @@ impl<P: Preset> Store<P> {
);

// [IGNORE] The sidecar is the first sidecar for the tuple (block_header.slot, block_header.proposer_index, sidecar.index) with valid header signature, sidecar inclusion proof, and kzg proof.
if self.accepted_data_sidecars.contains(&(
if self.accepted_data_column_sidecars.contains_key(&(
block_header.slot,
block_header.proposer_index,
data_column_sidecar.index,
Expand Down Expand Up @@ -2241,15 +2257,25 @@ impl<P: Preset> Store<P> {
self.blob_cache.insert(blob_sidecar);
}

pub fn apply_data_sidecar(&mut self, data_sidecar: Arc<DataColumnSidecar<P>>) {
pub fn apply_data_column_sidecar(&mut self, data_sidecar: Arc<DataColumnSidecar<P>>) {
let block_header = data_sidecar.signed_block_header.message;
let block_root = block_header.hash_tree_root();

let commitments = self.accepted_data_sidecars.insert((
block_header.slot,
block_header.proposer_index,
data_sidecar.index,
));
let commitments = self
.accepted_data_column_sidecars
.entry((
block_header.slot,
block_header.proposer_index,
data_sidecar.index,
))
.or_default();

commitments.insert(block_root, data_sidecar.kzg_commitments.clone());

let identifier = data_sidecar.as_ref().into();

self.data_column_cache
.insert(identifier, (data_sidecar, block_header.slot));
}

fn insert_block(&mut self, chain_link: ChainLink<P>) -> Result<()> {
Expand Down Expand Up @@ -3137,6 +3163,34 @@ impl<P: Preset> Store<P> {
.collect()
}

pub fn indices_of_missing_data_columns(
&self,
block: &Arc<SignedBeaconBlock<P>>,
) -> Vec<ColumnIndex> {
let block = block.message();

let Some(body) = block.body().post_deneb() else {
return vec![];
};

if body.blob_kzg_commitments().is_empty() {
return vec![];
}

let block_root = block.hash_tree_root();

(0..NUMBER_OF_COLUMNS)
.filter(|index| {
!self
.accepted_data_column_sidecars
.get(&(block.slot(), block.proposer_index(), *index))
.is_some_and(|kzg_commitments| {
kzg_commitments.get(&block_root) == Some(body.blob_kzg_commitments())
})
})
.collect()
}

pub fn register_rejected_block(&mut self, block_root: H256) {
self.rejected_block_roots.insert(block_root);
}
Expand Down
5 changes: 5 additions & 0 deletions types/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,11 @@ impl Config {
.map(|(phase, _)| phase)
}

#[must_use]
pub const fn is_eip7594_fork(&self, epoch: Epoch) -> bool {
epoch >= self.eip7594_fork_epoch
}

fn fork_slots<P: Preset>(&self) -> impl Iterator<Item = (Phase, Toption<Slot>)> + '_ {
enum_iterator::all().map(|phase| (phase, self.fork_slot::<P>(phase)))
}
Expand Down
19 changes: 17 additions & 2 deletions types/src/eip7594.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;

use serde::{Deserialize, Serialize};
use ssz::{ByteVector, ContiguousList, ContiguousVector, Ssz, H256};
use ssz::{ByteVector, ContiguousList, ContiguousVector, Ssz, SszHash as _, H256};
use typenum::{Prod, U4, U64};

use crate::{
Expand All @@ -27,7 +27,7 @@ pub type BlobCommitmentsInclusionProof = ContiguousVector<H256, KzgCommitmentsIn

pub const DATA_COLUMN_SIDECAR_SUBNET_COUNT: u64 = 32;

#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Ssz)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Deserialize, Serialize, Ssz)]
#[serde(deny_unknown_fields)]
pub struct DataColumnIdentifier {
block_root: H256,
Expand Down Expand Up @@ -61,6 +61,21 @@ impl<P: Preset> fmt::Debug for DataColumnSidecar<P> {
}
}

impl<P: Preset> From<&DataColumnSidecar<P>> for DataColumnIdentifier {
fn from(sidecar: &DataColumnSidecar<P>) -> Self {
let DataColumnSidecar {
index,
signed_block_header,
..
} = *sidecar;

let block_header = signed_block_header.message;
let block_root = block_header.hash_tree_root();

Self { block_root, index }
}
}

#[cfg(test)]
mod tests {
use spec_test_utils::Case;
Expand Down

0 comments on commit c86f3fc

Please sign in to comment.