From 95203c51d41b89cdccc0228d2ba7050b892f727d Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Sat, 17 Sep 2022 11:26:18 +0200 Subject: [PATCH] fix some bugx, adjust stucts --- beacon_node/store/src/partial_beacon_state.rs | 2 +- consensus/fork_choice/src/fork_choice.rs | 2 +- consensus/ssz/src/decode/impls.rs | 1 + consensus/ssz/src/encode/impls.rs | 1 + .../src/common/slash_validator.rs | 2 +- .../process_operations.rs | 2 +- .../src/per_epoch_processing.rs | 2 +- consensus/tree_hash/src/impls.rs | 1 + consensus/types/src/beacon_block.rs | 4 +- consensus/types/src/beacon_block_body.rs | 80 ++++++++++++++++++- consensus/types/src/beacon_state.rs | 11 ++- consensus/types/src/blob.rs | 9 +++ consensus/types/src/bls_field_element.rs | 7 ++ consensus/types/src/chain_spec.rs | 20 +++++ consensus/types/src/eth_spec.rs | 6 +- consensus/types/src/fork_name.rs | 18 ++++- consensus/types/src/kzg_commitment.rs | 19 ++++- consensus/types/src/kzg_proof.rs | 19 ++++- consensus/types/src/lib.rs | 6 +- consensus/types/src/signed_beacon_block.rs | 64 ++++++++++++++- 20 files changed, 256 insertions(+), 20 deletions(-) create mode 100644 consensus/types/src/blob.rs create mode 100644 consensus/types/src/bls_field_element.rs diff --git a/beacon_node/store/src/partial_beacon_state.rs b/beacon_node/store/src/partial_beacon_state.rs index 010796afd..2d3d9cc7b 100644 --- a/beacon_node/store/src/partial_beacon_state.rs +++ b/beacon_node/store/src/partial_beacon_state.rs @@ -14,7 +14,7 @@ use types::*; /// /// Utilises lazy-loading from separate storage for its vector fields. #[superstruct( - variants(Base, Altair, Merge), + variants(Base, Altair, Merge, Eip4844), variant_attributes(derive(Debug, PartialEq, Clone, Encode, Decode)) )] #[derive(Debug, PartialEq, Clone, Encode)] diff --git a/consensus/fork_choice/src/fork_choice.rs b/consensus/fork_choice/src/fork_choice.rs index f55a283ed..e2780e41a 100644 --- a/consensus/fork_choice/src/fork_choice.rs +++ b/consensus/fork_choice/src/fork_choice.rs @@ -773,7 +773,7 @@ where (parent_justified, parent_finalized) } else { let justification_and_finalization_state = match block { - BeaconBlockRef::Merge(_) | BeaconBlockRef::Altair(_) => { + BeaconBlockRef::Eip4844(_) | BeaconBlockRef::Merge(_) | BeaconBlockRef::Altair(_) => { let participation_cache = per_epoch_processing::altair::ParticipationCache::new(state, spec) .map_err(Error::ParticipationCacheBuild)?; diff --git a/consensus/ssz/src/decode/impls.rs b/consensus/ssz/src/decode/impls.rs index d91ddabe0..99f318585 100644 --- a/consensus/ssz/src/decode/impls.rs +++ b/consensus/ssz/src/decode/impls.rs @@ -374,6 +374,7 @@ macro_rules! impl_decodable_for_u8_array { impl_decodable_for_u8_array!(4); impl_decodable_for_u8_array!(32); +impl_decodable_for_u8_array!(48); macro_rules! impl_for_vec { ($type: ty, $max_len: expr) => { diff --git a/consensus/ssz/src/encode/impls.rs b/consensus/ssz/src/encode/impls.rs index cfd95ba40..1faf9123f 100644 --- a/consensus/ssz/src/encode/impls.rs +++ b/consensus/ssz/src/encode/impls.rs @@ -483,6 +483,7 @@ macro_rules! impl_encodable_for_u8_array { impl_encodable_for_u8_array!(4); impl_encodable_for_u8_array!(32); +impl_encodable_for_u8_array!(48); #[cfg(test)] mod tests { diff --git a/consensus/state_processing/src/common/slash_validator.rs b/consensus/state_processing/src/common/slash_validator.rs index e9d94a106..966a45860 100644 --- a/consensus/state_processing/src/common/slash_validator.rs +++ b/consensus/state_processing/src/common/slash_validator.rs @@ -45,7 +45,7 @@ pub fn slash_validator( validator_effective_balance.safe_div(spec.whistleblower_reward_quotient)?; let proposer_reward = match state { BeaconState::Base(_) => whistleblower_reward.safe_div(spec.proposer_reward_quotient)?, - BeaconState::Altair(_) | BeaconState::Merge(_) => whistleblower_reward + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Eip4844(_) => whistleblower_reward .safe_mul(PROPOSER_WEIGHT)? .safe_div(WEIGHT_DENOMINATOR)?, }; diff --git a/consensus/state_processing/src/per_block_processing/process_operations.rs b/consensus/state_processing/src/per_block_processing/process_operations.rs index 31a4ac1fb..6f2f7831d 100644 --- a/consensus/state_processing/src/per_block_processing/process_operations.rs +++ b/consensus/state_processing/src/per_block_processing/process_operations.rs @@ -230,7 +230,7 @@ pub fn process_attestations<'a, T: EthSpec, Payload: ExecPayload>( BeaconBlockBodyRef::Base(_) => { base::process_attestations(state, block_body.attestations(), verify_signatures, spec)?; } - BeaconBlockBodyRef::Altair(_) | BeaconBlockBodyRef::Merge(_) => { + BeaconBlockBodyRef::Altair(_) | BeaconBlockBodyRef::Merge(_) | BeaconBlockBodyRef::Eip4844(_) => { altair::process_attestations( state, block_body.attestations(), diff --git a/consensus/state_processing/src/per_epoch_processing.rs b/consensus/state_processing/src/per_epoch_processing.rs index cb90c67b5..fa7101716 100644 --- a/consensus/state_processing/src/per_epoch_processing.rs +++ b/consensus/state_processing/src/per_epoch_processing.rs @@ -37,7 +37,7 @@ pub fn process_epoch( match state { BeaconState::Base(_) => base::process_epoch(state, spec), - BeaconState::Altair(_) | BeaconState::Merge(_) => altair::process_epoch(state, spec), + BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Eip4844(_) => altair::process_epoch(state, spec), } } diff --git a/consensus/tree_hash/src/impls.rs b/consensus/tree_hash/src/impls.rs index 00fed489c..263a93334 100644 --- a/consensus/tree_hash/src/impls.rs +++ b/consensus/tree_hash/src/impls.rs @@ -81,6 +81,7 @@ macro_rules! impl_for_lt_32byte_u8_array { impl_for_lt_32byte_u8_array!(4); impl_for_lt_32byte_u8_array!(32); +impl_for_lt_32byte_u8_array!(48); impl TreeHash for U128 { fn tree_hash_type() -> TreeHashType { diff --git a/consensus/types/src/beacon_block.rs b/consensus/types/src/beacon_block.rs index cbfbd250f..6984c2561 100644 --- a/consensus/types/src/beacon_block.rs +++ b/consensus/types/src/beacon_block.rs @@ -1,6 +1,6 @@ use crate::beacon_block_body::{ BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge, BeaconBlockBodyRef, - BeaconBlockBodyRefMut, BeaconBlockBobyEip4844 + BeaconBlockBodyRefMut, BeaconBlockBodyEip4844 }; use crate::test_utils::TestRandom; use crate::*; @@ -189,6 +189,7 @@ impl<'a, T: EthSpec, Payload: ExecPayload> BeaconBlockRef<'a, T, Payload> { BeaconBlockRef::Base { .. } => ForkName::Base, BeaconBlockRef::Altair { .. } => ForkName::Altair, BeaconBlockRef::Merge { .. } => ForkName::Merge, + BeaconBlockRef::Eip4844 { .. } => ForkName::Eip4844, }; if fork_at_slot == object_fork { @@ -573,6 +574,7 @@ macro_rules! impl_clone_as_blinded { impl_clone_as_blinded!(BeaconBlockBase, >, >); impl_clone_as_blinded!(BeaconBlockAltair, >, >); impl_clone_as_blinded!(BeaconBlockMerge, >, >); +impl_clone_as_blinded!(BeaconBlockEip4844, >, >); // A reference to a full beacon block can be cloned into a blinded beacon block, without cloning the // execution payload. diff --git a/consensus/types/src/beacon_block_body.rs b/consensus/types/src/beacon_block_body.rs index a4fdbf689..eaa99718c 100644 --- a/consensus/types/src/beacon_block_body.rs +++ b/consensus/types/src/beacon_block_body.rs @@ -8,6 +8,7 @@ use std::marker::PhantomData; use superstruct::superstruct; use test_random_derive::TestRandom; use tree_hash_derive::TreeHash; +use crate::kzg_commitment::KzgCommitment; /// The body of a `BeaconChain` block, containing operations. /// @@ -56,7 +57,7 @@ pub struct BeaconBlockBody = FullPayload> #[serde(flatten)] pub execution_payload: Payload, #[superstruct(only(Eip4844))] - pub blob_kzg_commitments: VariableList, + pub blob_kzg_commitments: VariableList, #[superstruct(only(Base, Altair))] #[ssz(skip_serializing, skip_deserializing)] #[tree_hash(skip_hashing)] @@ -71,6 +72,7 @@ impl<'a, T: EthSpec> BeaconBlockBodyRef<'a, T> { BeaconBlockBodyRef::Base { .. } => ForkName::Base, BeaconBlockBodyRef::Altair { .. } => ForkName::Altair, BeaconBlockBodyRef::Merge { .. } => ForkName::Merge, + BeaconBlockBodyRef::Eip4844 { .. } => ForkName::Eip4844, } } } @@ -253,6 +255,48 @@ impl From>> } } +impl From>> +for ( + BeaconBlockBodyEip4844>, + Option>, +) +{ + fn from(body: BeaconBlockBodyEip4844>) -> Self { + let BeaconBlockBodyEip4844 { + randao_reveal, + eth1_data, + graffiti, + proposer_slashings, + attester_slashings, + attestations, + deposits, + voluntary_exits, + sync_aggregate, + execution_payload: FullPayload { execution_payload }, + blob_kzg_commitments, + } = body; + + ( + BeaconBlockBodyEip4844 { + randao_reveal, + eth1_data, + graffiti, + proposer_slashings, + attester_slashings, + attestations, + deposits, + voluntary_exits, + sync_aggregate, + execution_payload: BlindedPayload { + execution_payload_header: From::from(&execution_payload), + }, + blob_kzg_commitments, + }, + None, + ) + } +} + // We can clone a full block into a blinded block, without cloning the payload. impl BeaconBlockBodyBase> { pub fn clone_as_blinded(&self) -> BeaconBlockBodyBase> { @@ -300,6 +344,40 @@ impl BeaconBlockBodyMerge> { } } +impl BeaconBlockBodyEip4844> { + pub fn clone_as_blinded(&self) -> BeaconBlockBodyEip4844> { + let BeaconBlockBodyEip4844 { + randao_reveal, + eth1_data, + graffiti, + proposer_slashings, + attester_slashings, + attestations, + deposits, + voluntary_exits, + sync_aggregate, + execution_payload: FullPayload { execution_payload }, + blob_kzg_commitments, + } = self; + + BeaconBlockBodyEip4844 { + randao_reveal: randao_reveal.clone(), + eth1_data: eth1_data.clone(), + graffiti: *graffiti, + proposer_slashings: proposer_slashings.clone(), + attester_slashings: attester_slashings.clone(), + attestations: attestations.clone(), + deposits: deposits.clone(), + voluntary_exits: voluntary_exits.clone(), + sync_aggregate: sync_aggregate.clone(), + execution_payload: BlindedPayload { + execution_payload_header: From::from(execution_payload), + }, + blob_kzg_commitments: blob_kzg_commitments.clone(), + } + } +} + impl From>> for ( BeaconBlockBody>, diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index fca200312..0d9920ec9 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -172,7 +172,7 @@ impl From for Hash256 { /// The state of the `BeaconChain` at some slot. #[superstruct( - variants(Base, Altair, Merge), + variants(Base, Altair, Merge, Eip4844), variant_attributes( derive( Derivative, @@ -250,9 +250,9 @@ where pub current_epoch_attestations: VariableList, T::MaxPendingAttestations>, // Participation (Altair and later) - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Eip4844))] pub previous_epoch_participation: VariableList, - #[superstruct(only(Altair, Merge))] + #[superstruct(only(Altair, Merge, Eip4844))] pub current_epoch_participation: VariableList, // Finality @@ -389,6 +389,7 @@ impl BeaconState { BeaconState::Base { .. } => ForkName::Base, BeaconState::Altair { .. } => ForkName::Altair, BeaconState::Merge { .. } => ForkName::Merge, + BeaconState::Eip4844 { .. } => ForkName::Eip4844, }; if fork_at_slot == object_fork { @@ -1102,6 +1103,7 @@ impl BeaconState { BeaconState::Base(state) => (&mut state.validators, &mut state.balances), BeaconState::Altair(state) => (&mut state.validators, &mut state.balances), BeaconState::Merge(state) => (&mut state.validators, &mut state.balances), + BeaconState::Eip4844(state) => (&mut state.validators, &mut state.balances), } } @@ -1298,12 +1300,14 @@ impl BeaconState { BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant), BeaconState::Altair(state) => Ok(&mut state.current_epoch_participation), BeaconState::Merge(state) => Ok(&mut state.current_epoch_participation), + BeaconState::Eip4844(state) => Ok(&mut state.current_epoch_participation), } } else if epoch == self.previous_epoch() { match self { BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant), BeaconState::Altair(state) => Ok(&mut state.previous_epoch_participation), BeaconState::Merge(state) => Ok(&mut state.previous_epoch_participation), + BeaconState::Eip4844(state) => Ok(&mut state.previous_epoch_participation), } } else { Err(BeaconStateError::EpochOutOfBounds) @@ -1588,6 +1592,7 @@ impl BeaconState { BeaconState::Base(inner) => BeaconState::Base(inner.clone()), BeaconState::Altair(inner) => BeaconState::Altair(inner.clone()), BeaconState::Merge(inner) => BeaconState::Merge(inner.clone()), + BeaconState::Eip4844(inner) => BeaconState::Eip4844(inner.clone()), }; if config.committee_caches { *res.committee_caches_mut() = self.committee_caches().clone(); diff --git a/consensus/types/src/blob.rs b/consensus/types/src/blob.rs new file mode 100644 index 000000000..8ea3468ff --- /dev/null +++ b/consensus/types/src/blob.rs @@ -0,0 +1,9 @@ +use ssz_types::VariableList; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use ssz::{Decode, DecodeError, Encode}; +use crate::bls_field_element::BlsFieldElement; +use crate::EthSpec; + +#[derive(Default, Debug, PartialEq, Hash, Clone, Serialize, Deserialize)] +#[serde(transparent)] +pub struct Blob(pub VariableList); \ No newline at end of file diff --git a/consensus/types/src/bls_field_element.rs b/consensus/types/src/bls_field_element.rs new file mode 100644 index 000000000..6693b5765 --- /dev/null +++ b/consensus/types/src/bls_field_element.rs @@ -0,0 +1,7 @@ +use crate::Uint256; +use serde::{Deserialize, Serialize}; +use ssz::{Decode, DecodeError, Encode}; + +#[derive(Default, Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)] +#[serde(transparent)] +pub struct BlsFieldElement(pub Uint256); \ No newline at end of file diff --git a/consensus/types/src/chain_spec.rs b/consensus/types/src/chain_spec.rs index b2ba24ac3..b894bef12 100644 --- a/consensus/types/src/chain_spec.rs +++ b/consensus/types/src/chain_spec.rs @@ -150,6 +150,12 @@ pub struct ChainSpec { pub terminal_block_hash_activation_epoch: Epoch, pub safe_slots_to_import_optimistically: u64, + /* + * Eip4844 hard fork params + */ + pub eip4844_fork_epoch: Option, + pub eip4844_fork_version: [u8; 4], + /* * Networking */ @@ -245,6 +251,7 @@ impl ChainSpec { ForkName::Base => self.genesis_fork_version, ForkName::Altair => self.altair_fork_version, ForkName::Merge => self.bellatrix_fork_version, + ForkName::Eip4844 => self.eip4844_fork_version, } } @@ -254,6 +261,7 @@ impl ChainSpec { ForkName::Base => Some(Epoch::new(0)), ForkName::Altair => self.altair_fork_epoch, ForkName::Merge => self.bellatrix_fork_epoch, + ForkName::Eip4844 => self.eip4844_fork_epoch, } } @@ -263,6 +271,7 @@ impl ChainSpec { BeaconState::Base(_) => self.inactivity_penalty_quotient, BeaconState::Altair(_) => self.inactivity_penalty_quotient_altair, BeaconState::Merge(_) => self.inactivity_penalty_quotient_bellatrix, + BeaconState::Eip4844(_) => self.inactivity_penalty_quotient_bellatrix, } } @@ -275,6 +284,7 @@ impl ChainSpec { BeaconState::Base(_) => self.proportional_slashing_multiplier, BeaconState::Altair(_) => self.proportional_slashing_multiplier_altair, BeaconState::Merge(_) => self.proportional_slashing_multiplier_bellatrix, + BeaconState::Eip4844(_) => self.proportional_slashing_multiplier_bellatrix, } } @@ -287,6 +297,7 @@ impl ChainSpec { BeaconState::Base(_) => self.min_slashing_penalty_quotient, BeaconState::Altair(_) => self.min_slashing_penalty_quotient_altair, BeaconState::Merge(_) => self.min_slashing_penalty_quotient_bellatrix, + BeaconState::Eip4844(_) => self.min_slashing_penalty_quotient_bellatrix, } } @@ -568,6 +579,12 @@ impl ChainSpec { terminal_block_hash_activation_epoch: Epoch::new(u64::MAX), safe_slots_to_import_optimistically: 128u64, + /* + * Eip4844 hard fork params + */ + eip4844_fork_epoch: None, + eip4844_fork_version: [0x03, 0x00, 0x00, 0x00], + /* * Network specific */ @@ -778,6 +795,9 @@ impl ChainSpec { terminal_block_hash_activation_epoch: Epoch::new(u64::MAX), safe_slots_to_import_optimistically: 128u64, + eip4844_fork_epoch: None, + eip4844_fork_version: [0x03, 0x00, 0x00, 0x64], + /* * Network specific */ diff --git a/consensus/types/src/eth_spec.rs b/consensus/types/src/eth_spec.rs index aafb3d236..9d8a765c2 100644 --- a/consensus/types/src/eth_spec.rs +++ b/consensus/types/src/eth_spec.rs @@ -99,6 +99,7 @@ pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq + * New in Eip4844 */ type MaxBlobsPerBlock: Unsigned + Clone + Sync + Send + Debug + PartialEq; + type FieldElementsPerBlob: Unsigned + Clone + Sync + Send + Debug + PartialEq; /* * Derived values (set these CAREFULLY) */ @@ -275,6 +276,7 @@ impl EthSpec for MainnetEthSpec { type MaxPendingAttestations = U4096; // 128 max attestations * 32 slots per epoch type SlotsPerEth1VotingPeriod = U2048; // 64 epochs * 32 slots per epoch type MaxBlobsPerBlock = U16; + type FieldElementsPerBlob = U4096; fn default_spec() -> ChainSpec { ChainSpec::mainnet() @@ -320,7 +322,8 @@ impl EthSpec for MinimalEthSpec { GasLimitDenominator, MinGasLimit, MaxExtraDataBytes, - MaxBlobsPerBlock + MaxBlobsPerBlock, + FieldElementsPerBlob }); fn default_spec() -> ChainSpec { @@ -366,6 +369,7 @@ impl EthSpec for GnosisEthSpec { type MaxPendingAttestations = U2048; // 128 max attestations * 16 slots per epoch type SlotsPerEth1VotingPeriod = U1024; // 64 epochs * 16 slots per epoch type MaxBlobsPerBlock = U16; + type FieldElementsPerBlob = U4096; fn default_spec() -> ChainSpec { ChainSpec::gnosis() diff --git a/consensus/types/src/fork_name.rs b/consensus/types/src/fork_name.rs index e97b08309..7afe0d753 100644 --- a/consensus/types/src/fork_name.rs +++ b/consensus/types/src/fork_name.rs @@ -11,11 +11,12 @@ pub enum ForkName { Base, Altair, Merge, + Eip4844 } impl ForkName { pub fn list_all() -> Vec { - vec![ForkName::Base, ForkName::Altair, ForkName::Merge] + vec![ForkName::Base, ForkName::Altair, ForkName::Merge, ForkName::Eip4844] } /// Set the activation slots in the given `ChainSpec` so that the fork named by `self` @@ -38,6 +39,12 @@ impl ForkName { spec.bellatrix_fork_epoch = Some(Epoch::new(0)); spec } + ForkName::Eip4844 => { + spec.altair_fork_epoch = Some(Epoch::new(0)); + spec.bellatrix_fork_epoch = Some(Epoch::new(0)); + spec.eip4844_fork_epoch = Some(Epoch::new(0)); + spec + } } } @@ -49,6 +56,7 @@ impl ForkName { ForkName::Base => None, ForkName::Altair => Some(ForkName::Base), ForkName::Merge => Some(ForkName::Altair), + ForkName::Eip4844 => Some(ForkName::Merge), } } @@ -59,7 +67,8 @@ impl ForkName { match self { ForkName::Base => Some(ForkName::Altair), ForkName::Altair => Some(ForkName::Merge), - ForkName::Merge => None, + ForkName::Merge => Some(ForkName::Eip4844), + ForkName::Eip4844 => None, } } } @@ -101,6 +110,10 @@ macro_rules! map_fork_name_with { let (value, extra_data) = $body; ($t::Merge(value), extra_data) } + ForkName::Eip4844 => { + let (value, extra_data) = $body; + ($t::Eip4844(value), extra_data) + } } }; } @@ -124,6 +137,7 @@ impl Display for ForkName { ForkName::Base => "phase0".fmt(f), ForkName::Altair => "altair".fmt(f), ForkName::Merge => "bellatrix".fmt(f), + ForkName::Eip4844 => "eip4844".fmt(f), } } } diff --git a/consensus/types/src/kzg_commitment.rs b/consensus/types/src/kzg_commitment.rs index 688f96e05..cfddc95b3 100644 --- a/consensus/types/src/kzg_commitment.rs +++ b/consensus/types/src/kzg_commitment.rs @@ -1,14 +1,21 @@ use std::fmt; -use serde::{Deserialize, Deserializer, Serializer}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use ssz::{Decode, DecodeError, Encode}; use tree_hash::TreeHash; +use crate::test_utils::{RngCore, TestRandom}; const KZG_COMMITMENT_BYTES_LEN: usize = 48; -#[derive(Default, Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)] #[serde(transparent)] pub struct KzgCommitment(#[serde(with = "serde_kzg_commitment")] pub [u8; KZG_COMMITMENT_BYTES_LEN]); +impl Default for KzgCommitment { + fn default() -> Self { + KzgCommitment([0; 48]) + } +} + impl fmt::Display for KzgCommitment { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", eth2_serde_utils::hex::encode(&self.0)) @@ -110,3 +117,11 @@ impl TreeHash for KzgCommitment { self.0.tree_hash_root() } } + +impl TestRandom for KzgCommitment { + fn random_for_test(rng: &mut impl RngCore) -> Self { + let mut bytes = [0; KZG_COMMITMENT_BYTES_LEN]; + rng.fill_bytes(&mut bytes); + Self(bytes) + } +} diff --git a/consensus/types/src/kzg_proof.rs b/consensus/types/src/kzg_proof.rs index 50ee1266d..5bec8e2f8 100644 --- a/consensus/types/src/kzg_proof.rs +++ b/consensus/types/src/kzg_proof.rs @@ -1,11 +1,12 @@ use std::fmt; -use serde::{Deserialize, Deserializer, Serializer}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use ssz::{Decode, DecodeError, Encode}; use tree_hash::TreeHash; +use crate::test_utils::{RngCore, TestRandom}; const KZG_PROOF_BYTES_LEN: usize = 48; -#[derive(Default, Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)] #[serde(transparent)] pub struct KzgProof(#[serde(with = "serde_kzg_proof")] pub [u8; KZG_PROOF_BYTES_LEN]); @@ -15,6 +16,12 @@ impl fmt::Display for KzgProof { } } +impl Default for KzgProof { + fn default() -> Self { + KzgProof([0; 48]) + } +} + impl From<[u8; KZG_PROOF_BYTES_LEN]> for KzgProof { fn from(bytes: [u8; KZG_PROOF_BYTES_LEN]) -> Self { Self(bytes) @@ -110,3 +117,11 @@ impl TreeHash for KzgProof { self.0.tree_hash_root() } } + +impl TestRandom for KzgProof { + fn random_for_test(rng: &mut impl RngCore) -> Self { + let mut bytes = [0; KZG_PROOF_BYTES_LEN]; + rng.fill_bytes(&mut bytes); + Self(bytes) + } +} diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index c5a0e6ba0..8085b1460 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -94,6 +94,8 @@ pub mod sqlite; pub mod kzg_commitment; pub mod kzg_proof; +pub mod bls_field_element; +pub mod blob; use ethereum_types::{H160, H256}; @@ -104,11 +106,11 @@ pub use crate::attestation_duty::AttestationDuty; pub use crate::attester_slashing::AttesterSlashing; pub use crate::beacon_block::{ BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockMerge, BeaconBlockRef, - BeaconBlockRefMut, + BeaconBlockRefMut, BeaconBlockEip4844 }; pub use crate::beacon_block_body::{ BeaconBlockBody, BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge, - BeaconBlockBodyRef, BeaconBlockBodyRefMut, + BeaconBlockBodyRef, BeaconBlockBodyRefMut, BeaconBlockBodyEip4844 }; pub use crate::beacon_block_header::BeaconBlockHeader; pub use crate::beacon_committee::{BeaconCommittee, OwnedBeaconCommittee}; diff --git a/consensus/types/src/signed_beacon_block.rs b/consensus/types/src/signed_beacon_block.rs index 5c40c4685..4ab74ac21 100644 --- a/consensus/types/src/signed_beacon_block.rs +++ b/consensus/types/src/signed_beacon_block.rs @@ -38,7 +38,7 @@ impl From for Hash256 { /// A `BeaconBlock` and a signature from its proposer. #[superstruct( - variants(Base, Altair, Merge), + variants(Base, Altair, Merge, Eip4844), variant_attributes( derive( Debug, @@ -72,6 +72,8 @@ pub struct SignedBeaconBlock = FullPayload, #[superstruct(only(Merge), partial_getter(rename = "message_merge"))] pub message: BeaconBlockMerge, + #[superstruct(only(Eip4844), partial_getter(rename = "message_eip4844"))] + pub message: BeaconBlockEip4844, pub signature: Signature, } @@ -129,6 +131,9 @@ impl> SignedBeaconBlock { BeaconBlock::Merge(message) => { SignedBeaconBlock::Merge(SignedBeaconBlockMerge { message, signature }) } + BeaconBlock::Eip4844(message) => { + SignedBeaconBlock::Eip4844(SignedBeaconBlockEip4844 { message, signature }) + } } } @@ -307,6 +312,60 @@ impl SignedBeaconBlockMerge> { } } +impl SignedBeaconBlockEip4844> { + pub fn into_full_block( + self, + execution_payload: ExecutionPayload, + ) -> SignedBeaconBlockEip4844> { + let SignedBeaconBlockEip4844 { + message: + BeaconBlockEip4844 { + slot, + proposer_index, + parent_root, + state_root, + body: + BeaconBlockBodyEip4844 { + randao_reveal, + eth1_data, + graffiti, + proposer_slashings, + attester_slashings, + attestations, + deposits, + voluntary_exits, + sync_aggregate, + execution_payload: BlindedPayload { .. }, + blob_kzg_commitments, + }, + }, + signature, + } = self; + SignedBeaconBlockEip4844 { + message: BeaconBlockEip4844 { + slot, + proposer_index, + parent_root, + state_root, + body: BeaconBlockBodyEip4844 { + randao_reveal, + eth1_data, + graffiti, + proposer_slashings, + attester_slashings, + attestations, + deposits, + voluntary_exits, + sync_aggregate, + execution_payload: FullPayload { execution_payload }, + blob_kzg_commitments, + }, + }, + signature, + } + } +} + impl SignedBeaconBlock> { pub fn try_into_full_block( self, @@ -318,6 +377,9 @@ impl SignedBeaconBlock> { SignedBeaconBlock::Merge(block) => { SignedBeaconBlock::Merge(block.into_full_block(execution_payload?)) } + SignedBeaconBlock::Eip4844(block) => { + SignedBeaconBlock::Eip4844(block.into_full_block(execution_payload?)) + } }; Some(full_block) }