fix some bugx, adjust stucts

This commit is contained in:
Daniel Knopik 2022-09-17 11:26:18 +02:00
parent 8e4e499b51
commit 95203c51d4
20 changed files with 256 additions and 20 deletions

View File

@ -14,7 +14,7 @@ use types::*;
/// ///
/// Utilises lazy-loading from separate storage for its vector fields. /// Utilises lazy-loading from separate storage for its vector fields.
#[superstruct( #[superstruct(
variants(Base, Altair, Merge), variants(Base, Altair, Merge, Eip4844),
variant_attributes(derive(Debug, PartialEq, Clone, Encode, Decode)) variant_attributes(derive(Debug, PartialEq, Clone, Encode, Decode))
)] )]
#[derive(Debug, PartialEq, Clone, Encode)] #[derive(Debug, PartialEq, Clone, Encode)]

View File

@ -773,7 +773,7 @@ where
(parent_justified, parent_finalized) (parent_justified, parent_finalized)
} else { } else {
let justification_and_finalization_state = match block { let justification_and_finalization_state = match block {
BeaconBlockRef::Merge(_) | BeaconBlockRef::Altair(_) => { BeaconBlockRef::Eip4844(_) | BeaconBlockRef::Merge(_) | BeaconBlockRef::Altair(_) => {
let participation_cache = let participation_cache =
per_epoch_processing::altair::ParticipationCache::new(state, spec) per_epoch_processing::altair::ParticipationCache::new(state, spec)
.map_err(Error::ParticipationCacheBuild)?; .map_err(Error::ParticipationCacheBuild)?;

View File

@ -374,6 +374,7 @@ macro_rules! impl_decodable_for_u8_array {
impl_decodable_for_u8_array!(4); impl_decodable_for_u8_array!(4);
impl_decodable_for_u8_array!(32); impl_decodable_for_u8_array!(32);
impl_decodable_for_u8_array!(48);
macro_rules! impl_for_vec { macro_rules! impl_for_vec {
($type: ty, $max_len: expr) => { ($type: ty, $max_len: expr) => {

View File

@ -483,6 +483,7 @@ macro_rules! impl_encodable_for_u8_array {
impl_encodable_for_u8_array!(4); impl_encodable_for_u8_array!(4);
impl_encodable_for_u8_array!(32); impl_encodable_for_u8_array!(32);
impl_encodable_for_u8_array!(48);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -45,7 +45,7 @@ pub fn slash_validator<T: EthSpec>(
validator_effective_balance.safe_div(spec.whistleblower_reward_quotient)?; validator_effective_balance.safe_div(spec.whistleblower_reward_quotient)?;
let proposer_reward = match state { let proposer_reward = match state {
BeaconState::Base(_) => whistleblower_reward.safe_div(spec.proposer_reward_quotient)?, 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_mul(PROPOSER_WEIGHT)?
.safe_div(WEIGHT_DENOMINATOR)?, .safe_div(WEIGHT_DENOMINATOR)?,
}; };

View File

@ -230,7 +230,7 @@ pub fn process_attestations<'a, T: EthSpec, Payload: ExecPayload<T>>(
BeaconBlockBodyRef::Base(_) => { BeaconBlockBodyRef::Base(_) => {
base::process_attestations(state, block_body.attestations(), verify_signatures, spec)?; base::process_attestations(state, block_body.attestations(), verify_signatures, spec)?;
} }
BeaconBlockBodyRef::Altair(_) | BeaconBlockBodyRef::Merge(_) => { BeaconBlockBodyRef::Altair(_) | BeaconBlockBodyRef::Merge(_) | BeaconBlockBodyRef::Eip4844(_) => {
altair::process_attestations( altair::process_attestations(
state, state,
block_body.attestations(), block_body.attestations(),

View File

@ -37,7 +37,7 @@ pub fn process_epoch<T: EthSpec>(
match state { match state {
BeaconState::Base(_) => base::process_epoch(state, spec), 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),
} }
} }

View File

@ -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!(4);
impl_for_lt_32byte_u8_array!(32); impl_for_lt_32byte_u8_array!(32);
impl_for_lt_32byte_u8_array!(48);
impl TreeHash for U128 { impl TreeHash for U128 {
fn tree_hash_type() -> TreeHashType { fn tree_hash_type() -> TreeHashType {

View File

@ -1,6 +1,6 @@
use crate::beacon_block_body::{ use crate::beacon_block_body::{
BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge, BeaconBlockBodyRef, BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge, BeaconBlockBodyRef,
BeaconBlockBodyRefMut, BeaconBlockBobyEip4844 BeaconBlockBodyRefMut, BeaconBlockBodyEip4844
}; };
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use crate::*; use crate::*;
@ -189,6 +189,7 @@ impl<'a, T: EthSpec, Payload: ExecPayload<T>> BeaconBlockRef<'a, T, Payload> {
BeaconBlockRef::Base { .. } => ForkName::Base, BeaconBlockRef::Base { .. } => ForkName::Base,
BeaconBlockRef::Altair { .. } => ForkName::Altair, BeaconBlockRef::Altair { .. } => ForkName::Altair,
BeaconBlockRef::Merge { .. } => ForkName::Merge, BeaconBlockRef::Merge { .. } => ForkName::Merge,
BeaconBlockRef::Eip4844 { .. } => ForkName::Eip4844,
}; };
if fork_at_slot == object_fork { if fork_at_slot == object_fork {
@ -573,6 +574,7 @@ macro_rules! impl_clone_as_blinded {
impl_clone_as_blinded!(BeaconBlockBase, <E, FullPayload<E>>, <E, BlindedPayload<E>>); impl_clone_as_blinded!(BeaconBlockBase, <E, FullPayload<E>>, <E, BlindedPayload<E>>);
impl_clone_as_blinded!(BeaconBlockAltair, <E, FullPayload<E>>, <E, BlindedPayload<E>>); impl_clone_as_blinded!(BeaconBlockAltair, <E, FullPayload<E>>, <E, BlindedPayload<E>>);
impl_clone_as_blinded!(BeaconBlockMerge, <E, FullPayload<E>>, <E, BlindedPayload<E>>); impl_clone_as_blinded!(BeaconBlockMerge, <E, FullPayload<E>>, <E, BlindedPayload<E>>);
impl_clone_as_blinded!(BeaconBlockEip4844, <E, FullPayload<E>>, <E, BlindedPayload<E>>);
// A reference to a full beacon block can be cloned into a blinded beacon block, without cloning the // A reference to a full beacon block can be cloned into a blinded beacon block, without cloning the
// execution payload. // execution payload.

View File

@ -8,6 +8,7 @@ use std::marker::PhantomData;
use superstruct::superstruct; use superstruct::superstruct;
use test_random_derive::TestRandom; use test_random_derive::TestRandom;
use tree_hash_derive::TreeHash; use tree_hash_derive::TreeHash;
use crate::kzg_commitment::KzgCommitment;
/// The body of a `BeaconChain` block, containing operations. /// The body of a `BeaconChain` block, containing operations.
/// ///
@ -56,7 +57,7 @@ pub struct BeaconBlockBody<T: EthSpec, Payload: ExecPayload<T> = FullPayload<T>>
#[serde(flatten)] #[serde(flatten)]
pub execution_payload: Payload, pub execution_payload: Payload,
#[superstruct(only(Eip4844))] #[superstruct(only(Eip4844))]
pub blob_kzg_commitments: VariableList<TODO, T::MaxBlobsPerBlock>, pub blob_kzg_commitments: VariableList<KzgCommitment, T::MaxBlobsPerBlock>,
#[superstruct(only(Base, Altair))] #[superstruct(only(Base, Altair))]
#[ssz(skip_serializing, skip_deserializing)] #[ssz(skip_serializing, skip_deserializing)]
#[tree_hash(skip_hashing)] #[tree_hash(skip_hashing)]
@ -71,6 +72,7 @@ impl<'a, T: EthSpec> BeaconBlockBodyRef<'a, T> {
BeaconBlockBodyRef::Base { .. } => ForkName::Base, BeaconBlockBodyRef::Base { .. } => ForkName::Base,
BeaconBlockBodyRef::Altair { .. } => ForkName::Altair, BeaconBlockBodyRef::Altair { .. } => ForkName::Altair,
BeaconBlockBodyRef::Merge { .. } => ForkName::Merge, BeaconBlockBodyRef::Merge { .. } => ForkName::Merge,
BeaconBlockBodyRef::Eip4844 { .. } => ForkName::Eip4844,
} }
} }
} }
@ -253,6 +255,48 @@ impl<E: EthSpec> From<BeaconBlockBodyMerge<E, FullPayload<E>>>
} }
} }
impl<E: EthSpec> From<BeaconBlockBodyEip4844<E, FullPayload<E>>>
for (
BeaconBlockBodyEip4844<E, BlindedPayload<E>>,
Option<ExecutionPayload<E>>,
)
{
fn from(body: BeaconBlockBodyEip4844<E, FullPayload<E>>) -> 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. // We can clone a full block into a blinded block, without cloning the payload.
impl<E: EthSpec> BeaconBlockBodyBase<E, FullPayload<E>> { impl<E: EthSpec> BeaconBlockBodyBase<E, FullPayload<E>> {
pub fn clone_as_blinded(&self) -> BeaconBlockBodyBase<E, BlindedPayload<E>> { pub fn clone_as_blinded(&self) -> BeaconBlockBodyBase<E, BlindedPayload<E>> {
@ -300,6 +344,40 @@ impl<E: EthSpec> BeaconBlockBodyMerge<E, FullPayload<E>> {
} }
} }
impl<E: EthSpec> BeaconBlockBodyEip4844<E, FullPayload<E>> {
pub fn clone_as_blinded(&self) -> BeaconBlockBodyEip4844<E, BlindedPayload<E>> {
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<E: EthSpec> From<BeaconBlockBody<E, FullPayload<E>>> impl<E: EthSpec> From<BeaconBlockBody<E, FullPayload<E>>>
for ( for (
BeaconBlockBody<E, BlindedPayload<E>>, BeaconBlockBody<E, BlindedPayload<E>>,

View File

@ -172,7 +172,7 @@ impl From<BeaconStateHash> for Hash256 {
/// The state of the `BeaconChain` at some slot. /// The state of the `BeaconChain` at some slot.
#[superstruct( #[superstruct(
variants(Base, Altair, Merge), variants(Base, Altair, Merge, Eip4844),
variant_attributes( variant_attributes(
derive( derive(
Derivative, Derivative,
@ -250,9 +250,9 @@ where
pub current_epoch_attestations: VariableList<PendingAttestation<T>, T::MaxPendingAttestations>, pub current_epoch_attestations: VariableList<PendingAttestation<T>, T::MaxPendingAttestations>,
// Participation (Altair and later) // Participation (Altair and later)
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Eip4844))]
pub previous_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>, pub previous_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>,
#[superstruct(only(Altair, Merge))] #[superstruct(only(Altair, Merge, Eip4844))]
pub current_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>, pub current_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>,
// Finality // Finality
@ -389,6 +389,7 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base { .. } => ForkName::Base, BeaconState::Base { .. } => ForkName::Base,
BeaconState::Altair { .. } => ForkName::Altair, BeaconState::Altair { .. } => ForkName::Altair,
BeaconState::Merge { .. } => ForkName::Merge, BeaconState::Merge { .. } => ForkName::Merge,
BeaconState::Eip4844 { .. } => ForkName::Eip4844,
}; };
if fork_at_slot == object_fork { if fork_at_slot == object_fork {
@ -1102,6 +1103,7 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base(state) => (&mut state.validators, &mut state.balances), BeaconState::Base(state) => (&mut state.validators, &mut state.balances),
BeaconState::Altair(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::Merge(state) => (&mut state.validators, &mut state.balances),
BeaconState::Eip4844(state) => (&mut state.validators, &mut state.balances),
} }
} }
@ -1298,12 +1300,14 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant), BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant),
BeaconState::Altair(state) => Ok(&mut state.current_epoch_participation), BeaconState::Altair(state) => Ok(&mut state.current_epoch_participation),
BeaconState::Merge(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() { } else if epoch == self.previous_epoch() {
match self { match self {
BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant), BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant),
BeaconState::Altair(state) => Ok(&mut state.previous_epoch_participation), BeaconState::Altair(state) => Ok(&mut state.previous_epoch_participation),
BeaconState::Merge(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 { } else {
Err(BeaconStateError::EpochOutOfBounds) Err(BeaconStateError::EpochOutOfBounds)
@ -1588,6 +1592,7 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base(inner) => BeaconState::Base(inner.clone()), BeaconState::Base(inner) => BeaconState::Base(inner.clone()),
BeaconState::Altair(inner) => BeaconState::Altair(inner.clone()), BeaconState::Altair(inner) => BeaconState::Altair(inner.clone()),
BeaconState::Merge(inner) => BeaconState::Merge(inner.clone()), BeaconState::Merge(inner) => BeaconState::Merge(inner.clone()),
BeaconState::Eip4844(inner) => BeaconState::Eip4844(inner.clone()),
}; };
if config.committee_caches { if config.committee_caches {
*res.committee_caches_mut() = self.committee_caches().clone(); *res.committee_caches_mut() = self.committee_caches().clone();

View File

@ -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<T: EthSpec>(pub VariableList<BlsFieldElement, T::FieldElementsPerBlob>);

View File

@ -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);

View File

@ -150,6 +150,12 @@ pub struct ChainSpec {
pub terminal_block_hash_activation_epoch: Epoch, pub terminal_block_hash_activation_epoch: Epoch,
pub safe_slots_to_import_optimistically: u64, pub safe_slots_to_import_optimistically: u64,
/*
* Eip4844 hard fork params
*/
pub eip4844_fork_epoch: Option<Epoch>,
pub eip4844_fork_version: [u8; 4],
/* /*
* Networking * Networking
*/ */
@ -245,6 +251,7 @@ impl ChainSpec {
ForkName::Base => self.genesis_fork_version, ForkName::Base => self.genesis_fork_version,
ForkName::Altair => self.altair_fork_version, ForkName::Altair => self.altair_fork_version,
ForkName::Merge => self.bellatrix_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::Base => Some(Epoch::new(0)),
ForkName::Altair => self.altair_fork_epoch, ForkName::Altair => self.altair_fork_epoch,
ForkName::Merge => self.bellatrix_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::Base(_) => self.inactivity_penalty_quotient,
BeaconState::Altair(_) => self.inactivity_penalty_quotient_altair, BeaconState::Altair(_) => self.inactivity_penalty_quotient_altair,
BeaconState::Merge(_) => self.inactivity_penalty_quotient_bellatrix, 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::Base(_) => self.proportional_slashing_multiplier,
BeaconState::Altair(_) => self.proportional_slashing_multiplier_altair, BeaconState::Altair(_) => self.proportional_slashing_multiplier_altair,
BeaconState::Merge(_) => self.proportional_slashing_multiplier_bellatrix, 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::Base(_) => self.min_slashing_penalty_quotient,
BeaconState::Altair(_) => self.min_slashing_penalty_quotient_altair, BeaconState::Altair(_) => self.min_slashing_penalty_quotient_altair,
BeaconState::Merge(_) => self.min_slashing_penalty_quotient_bellatrix, 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), terminal_block_hash_activation_epoch: Epoch::new(u64::MAX),
safe_slots_to_import_optimistically: 128u64, safe_slots_to_import_optimistically: 128u64,
/*
* Eip4844 hard fork params
*/
eip4844_fork_epoch: None,
eip4844_fork_version: [0x03, 0x00, 0x00, 0x00],
/* /*
* Network specific * Network specific
*/ */
@ -778,6 +795,9 @@ impl ChainSpec {
terminal_block_hash_activation_epoch: Epoch::new(u64::MAX), terminal_block_hash_activation_epoch: Epoch::new(u64::MAX),
safe_slots_to_import_optimistically: 128u64, safe_slots_to_import_optimistically: 128u64,
eip4844_fork_epoch: None,
eip4844_fork_version: [0x03, 0x00, 0x00, 0x64],
/* /*
* Network specific * Network specific
*/ */

View File

@ -99,6 +99,7 @@ pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq +
* New in Eip4844 * New in Eip4844
*/ */
type MaxBlobsPerBlock: Unsigned + Clone + Sync + Send + Debug + PartialEq; type MaxBlobsPerBlock: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type FieldElementsPerBlob: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/* /*
* Derived values (set these CAREFULLY) * Derived values (set these CAREFULLY)
*/ */
@ -275,6 +276,7 @@ impl EthSpec for MainnetEthSpec {
type MaxPendingAttestations = U4096; // 128 max attestations * 32 slots per epoch type MaxPendingAttestations = U4096; // 128 max attestations * 32 slots per epoch
type SlotsPerEth1VotingPeriod = U2048; // 64 epochs * 32 slots per epoch type SlotsPerEth1VotingPeriod = U2048; // 64 epochs * 32 slots per epoch
type MaxBlobsPerBlock = U16; type MaxBlobsPerBlock = U16;
type FieldElementsPerBlob = U4096;
fn default_spec() -> ChainSpec { fn default_spec() -> ChainSpec {
ChainSpec::mainnet() ChainSpec::mainnet()
@ -320,7 +322,8 @@ impl EthSpec for MinimalEthSpec {
GasLimitDenominator, GasLimitDenominator,
MinGasLimit, MinGasLimit,
MaxExtraDataBytes, MaxExtraDataBytes,
MaxBlobsPerBlock MaxBlobsPerBlock,
FieldElementsPerBlob
}); });
fn default_spec() -> ChainSpec { fn default_spec() -> ChainSpec {
@ -366,6 +369,7 @@ impl EthSpec for GnosisEthSpec {
type MaxPendingAttestations = U2048; // 128 max attestations * 16 slots per epoch type MaxPendingAttestations = U2048; // 128 max attestations * 16 slots per epoch
type SlotsPerEth1VotingPeriod = U1024; // 64 epochs * 16 slots per epoch type SlotsPerEth1VotingPeriod = U1024; // 64 epochs * 16 slots per epoch
type MaxBlobsPerBlock = U16; type MaxBlobsPerBlock = U16;
type FieldElementsPerBlob = U4096;
fn default_spec() -> ChainSpec { fn default_spec() -> ChainSpec {
ChainSpec::gnosis() ChainSpec::gnosis()

View File

@ -11,11 +11,12 @@ pub enum ForkName {
Base, Base,
Altair, Altair,
Merge, Merge,
Eip4844
} }
impl ForkName { impl ForkName {
pub fn list_all() -> Vec<ForkName> { pub fn list_all() -> Vec<ForkName> {
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` /// 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.bellatrix_fork_epoch = Some(Epoch::new(0));
spec 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::Base => None,
ForkName::Altair => Some(ForkName::Base), ForkName::Altair => Some(ForkName::Base),
ForkName::Merge => Some(ForkName::Altair), ForkName::Merge => Some(ForkName::Altair),
ForkName::Eip4844 => Some(ForkName::Merge),
} }
} }
@ -59,7 +67,8 @@ impl ForkName {
match self { match self {
ForkName::Base => Some(ForkName::Altair), ForkName::Base => Some(ForkName::Altair),
ForkName::Altair => Some(ForkName::Merge), 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; let (value, extra_data) = $body;
($t::Merge(value), extra_data) ($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::Base => "phase0".fmt(f),
ForkName::Altair => "altair".fmt(f), ForkName::Altair => "altair".fmt(f),
ForkName::Merge => "bellatrix".fmt(f), ForkName::Merge => "bellatrix".fmt(f),
ForkName::Eip4844 => "eip4844".fmt(f),
} }
} }
} }

View File

@ -1,14 +1,21 @@
use std::fmt; use std::fmt;
use serde::{Deserialize, Deserializer, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use ssz::{Decode, DecodeError, Encode}; use ssz::{Decode, DecodeError, Encode};
use tree_hash::TreeHash; use tree_hash::TreeHash;
use crate::test_utils::{RngCore, TestRandom};
const KZG_COMMITMENT_BYTES_LEN: usize = 48; 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)] #[serde(transparent)]
pub struct KzgCommitment(#[serde(with = "serde_kzg_commitment")] pub [u8; KZG_COMMITMENT_BYTES_LEN]); 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 { impl fmt::Display for KzgCommitment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", eth2_serde_utils::hex::encode(&self.0)) write!(f, "{}", eth2_serde_utils::hex::encode(&self.0))
@ -110,3 +117,11 @@ impl TreeHash for KzgCommitment {
self.0.tree_hash_root() 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)
}
}

View File

@ -1,11 +1,12 @@
use std::fmt; use std::fmt;
use serde::{Deserialize, Deserializer, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use ssz::{Decode, DecodeError, Encode}; use ssz::{Decode, DecodeError, Encode};
use tree_hash::TreeHash; use tree_hash::TreeHash;
use crate::test_utils::{RngCore, TestRandom};
const KZG_PROOF_BYTES_LEN: usize = 48; 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)] #[serde(transparent)]
pub struct KzgProof(#[serde(with = "serde_kzg_proof")] pub [u8; KZG_PROOF_BYTES_LEN]); 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 { impl From<[u8; KZG_PROOF_BYTES_LEN]> for KzgProof {
fn from(bytes: [u8; KZG_PROOF_BYTES_LEN]) -> Self { fn from(bytes: [u8; KZG_PROOF_BYTES_LEN]) -> Self {
Self(bytes) Self(bytes)
@ -110,3 +117,11 @@ impl TreeHash for KzgProof {
self.0.tree_hash_root() 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)
}
}

View File

@ -94,6 +94,8 @@ pub mod sqlite;
pub mod kzg_commitment; pub mod kzg_commitment;
pub mod kzg_proof; pub mod kzg_proof;
pub mod bls_field_element;
pub mod blob;
use ethereum_types::{H160, H256}; use ethereum_types::{H160, H256};
@ -104,11 +106,11 @@ pub use crate::attestation_duty::AttestationDuty;
pub use crate::attester_slashing::AttesterSlashing; pub use crate::attester_slashing::AttesterSlashing;
pub use crate::beacon_block::{ pub use crate::beacon_block::{
BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockMerge, BeaconBlockRef, BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockMerge, BeaconBlockRef,
BeaconBlockRefMut, BeaconBlockRefMut, BeaconBlockEip4844
}; };
pub use crate::beacon_block_body::{ pub use crate::beacon_block_body::{
BeaconBlockBody, BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge, BeaconBlockBody, BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge,
BeaconBlockBodyRef, BeaconBlockBodyRefMut, BeaconBlockBodyRef, BeaconBlockBodyRefMut, BeaconBlockBodyEip4844
}; };
pub use crate::beacon_block_header::BeaconBlockHeader; pub use crate::beacon_block_header::BeaconBlockHeader;
pub use crate::beacon_committee::{BeaconCommittee, OwnedBeaconCommittee}; pub use crate::beacon_committee::{BeaconCommittee, OwnedBeaconCommittee};

View File

@ -38,7 +38,7 @@ impl From<SignedBeaconBlockHash> for Hash256 {
/// A `BeaconBlock` and a signature from its proposer. /// A `BeaconBlock` and a signature from its proposer.
#[superstruct( #[superstruct(
variants(Base, Altair, Merge), variants(Base, Altair, Merge, Eip4844),
variant_attributes( variant_attributes(
derive( derive(
Debug, Debug,
@ -72,6 +72,8 @@ pub struct SignedBeaconBlock<E: EthSpec, Payload: ExecPayload<E> = FullPayload<E
pub message: BeaconBlockAltair<E, Payload>, pub message: BeaconBlockAltair<E, Payload>,
#[superstruct(only(Merge), partial_getter(rename = "message_merge"))] #[superstruct(only(Merge), partial_getter(rename = "message_merge"))]
pub message: BeaconBlockMerge<E, Payload>, pub message: BeaconBlockMerge<E, Payload>,
#[superstruct(only(Eip4844), partial_getter(rename = "message_eip4844"))]
pub message: BeaconBlockEip4844<E, Payload>,
pub signature: Signature, pub signature: Signature,
} }
@ -129,6 +131,9 @@ impl<E: EthSpec, Payload: ExecPayload<E>> SignedBeaconBlock<E, Payload> {
BeaconBlock::Merge(message) => { BeaconBlock::Merge(message) => {
SignedBeaconBlock::Merge(SignedBeaconBlockMerge { message, signature }) SignedBeaconBlock::Merge(SignedBeaconBlockMerge { message, signature })
} }
BeaconBlock::Eip4844(message) => {
SignedBeaconBlock::Eip4844(SignedBeaconBlockEip4844 { message, signature })
}
} }
} }
@ -307,6 +312,60 @@ impl<E: EthSpec> SignedBeaconBlockMerge<E, BlindedPayload<E>> {
} }
} }
impl<E: EthSpec> SignedBeaconBlockEip4844<E, BlindedPayload<E>> {
pub fn into_full_block(
self,
execution_payload: ExecutionPayload<E>,
) -> SignedBeaconBlockEip4844<E, FullPayload<E>> {
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<E: EthSpec> SignedBeaconBlock<E, BlindedPayload<E>> { impl<E: EthSpec> SignedBeaconBlock<E, BlindedPayload<E>> {
pub fn try_into_full_block( pub fn try_into_full_block(
self, self,
@ -318,6 +377,9 @@ impl<E: EthSpec> SignedBeaconBlock<E, BlindedPayload<E>> {
SignedBeaconBlock::Merge(block) => { SignedBeaconBlock::Merge(block) => {
SignedBeaconBlock::Merge(block.into_full_block(execution_payload?)) SignedBeaconBlock::Merge(block.into_full_block(execution_payload?))
} }
SignedBeaconBlock::Eip4844(block) => {
SignedBeaconBlock::Eip4844(block.into_full_block(execution_payload?))
}
}; };
Some(full_block) Some(full_block)
} }