From 39d4f0a1f388bb4105f4c4c4a560975ce1247420 Mon Sep 17 00:00:00 2001 From: Pawan Dhananjay Date: Wed, 8 Feb 2023 13:00:18 +0530 Subject: [PATCH] Add BlobSidecar type --- consensus/types/src/blob_sidecar.rs | 51 +++++++++++++++++++++++++++++ consensus/types/src/lib.rs | 4 ++- crypto/kzg/src/kzg_commitment.rs | 6 ++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 consensus/types/src/blob_sidecar.rs diff --git a/consensus/types/src/blob_sidecar.rs b/consensus/types/src/blob_sidecar.rs new file mode 100644 index 000000000..3a2012839 --- /dev/null +++ b/consensus/types/src/blob_sidecar.rs @@ -0,0 +1,51 @@ +use crate::test_utils::TestRandom; +use crate::{Blob, EthSpec, Hash256, SignedRoot, Slot}; +use derivative::Derivative; +use kzg::{KzgCommitment, KzgProof}; +use serde_derive::{Deserialize, Serialize}; +use ssz::Encode; +use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; +use tree_hash_derive::TreeHash; + +#[derive( + Debug, + Clone, + Serialize, + Deserialize, + Encode, + Decode, + TreeHash, + Default, + TestRandom, + Derivative, + arbitrary::Arbitrary, +)] +#[serde(bound = "T: EthSpec")] +#[arbitrary(bound = "T: EthSpec")] +#[derivative(PartialEq, Hash(bound = "T: EthSpec"))] +pub struct BlobSidecar { + pub block_root: Hash256, + // TODO: fix the type, should fit in u8 as well + pub index: u64, + pub slot: Slot, + pub block_parent_root: Hash256, + pub proposer_index: u64, + pub blob: Blob, + pub kzg_commitment: KzgCommitment, + pub kzg_proof: KzgProof, +} + +impl SignedRoot for BlobSidecar {} + +impl BlobSidecar { + pub fn empty() -> Self { + Self::default() + } + + #[allow(clippy::integer_arithmetic)] + pub fn max_size() -> usize { + // Fixed part + Self::empty().as_ssz_bytes().len() + } +} diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index 380f5e527..4ec740eff 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -99,6 +99,7 @@ pub mod slot_data; #[cfg(feature = "sqlite")] pub mod sqlite; +pub mod blob_sidecar; pub mod blobs_sidecar; pub mod signed_block_and_blobs; pub mod transaction; @@ -121,7 +122,8 @@ pub use crate::beacon_block_body::{ pub use crate::beacon_block_header::BeaconBlockHeader; pub use crate::beacon_committee::{BeaconCommittee, OwnedBeaconCommittee}; pub use crate::beacon_state::{BeaconTreeHashCache, Error as BeaconStateError, *}; -pub use crate::blobs_sidecar::{Blobs, BlobsSidecar, KzgCommitments}; +pub use crate::blob_sidecar::BlobSidecar; +pub use crate::blobs_sidecar::BlobsSidecar; pub use crate::bls_to_execution_change::BlsToExecutionChange; pub use crate::chain_spec::{ChainSpec, Config, Domain}; pub use crate::checkpoint::Checkpoint; diff --git a/crypto/kzg/src/kzg_commitment.rs b/crypto/kzg/src/kzg_commitment.rs index 16fe1d830..7a43d7009 100644 --- a/crypto/kzg/src/kzg_commitment.rs +++ b/crypto/kzg/src/kzg_commitment.rs @@ -20,6 +20,12 @@ impl Display for KzgCommitment { } } +impl Default for KzgCommitment { + fn default() -> Self { + KzgCommitment([0; KZG_COMMITMENT_BYTES_LEN]) + } +} + impl TreeHash for KzgCommitment { fn tree_hash_type() -> tree_hash::TreeHashType { <[u8; KZG_COMMITMENT_BYTES_LEN] as TreeHash>::tree_hash_type()