This commit is contained in:
realbigsean
2022-10-03 10:06:04 -04:00
64 changed files with 1349 additions and 321 deletions

View File

@@ -1,6 +1,6 @@
use crate::beacon_block_body::{
BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyCapella, BeaconBlockBodyMerge,
BeaconBlockBodyRef, BeaconBlockBodyRefMut,
BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge, BeaconBlockBodyRef,
BeaconBlockBodyRefMut, BeaconBlockBodyEip4844
};
use crate::test_utils::TestRandom;
use crate::*;
@@ -17,7 +17,7 @@ use tree_hash_derive::TreeHash;
/// A block of the `BeaconChain`.
#[superstruct(
variants(Base, Altair, Merge, Capella),
variants(Base, Altair, Merge, Eip4844),
variant_attributes(
derive(
Debug,
@@ -64,8 +64,8 @@ pub struct BeaconBlock<T: EthSpec, Payload: ExecPayload<T> = FullPayload<T>> {
pub body: BeaconBlockBodyAltair<T, Payload>,
#[superstruct(only(Merge), partial_getter(rename = "body_merge"))]
pub body: BeaconBlockBodyMerge<T, Payload>,
#[superstruct(only(Capella), partial_getter(rename = "body_capella"))]
pub body: BeaconBlockBodyCapella<T, Payload>,
#[superstruct(only(Eip4844), partial_getter(rename = "body_eip4844"))]
pub body: BeaconBlockBodyEip4844<T, Payload>,
}
pub type BlindedBeaconBlock<E> = BeaconBlock<E, BlindedPayload<E>>;
@@ -191,7 +191,7 @@ impl<'a, T: EthSpec, Payload: ExecPayload<T>> BeaconBlockRef<'a, T, Payload> {
BeaconBlockRef::Base { .. } => ForkName::Base,
BeaconBlockRef::Altair { .. } => ForkName::Altair,
BeaconBlockRef::Merge { .. } => ForkName::Merge,
BeaconBlockRef::Capella { .. } => ForkName::Capella,
BeaconBlockRef::Eip4844 { .. } => ForkName::Eip4844,
};
if fork_at_slot == object_fork {
@@ -545,6 +545,7 @@ macro_rules! impl_from {
impl_from!(BeaconBlockBase, <E, FullPayload<E>>, <E, BlindedPayload<E>>, |body: BeaconBlockBodyBase<_, _>| body.into());
impl_from!(BeaconBlockAltair, <E, FullPayload<E>>, <E, BlindedPayload<E>>, |body: BeaconBlockBodyAltair<_, _>| body.into());
impl_from!(BeaconBlockMerge, <E, FullPayload<E>>, <E, BlindedPayload<E>>, |body: BeaconBlockBodyMerge<_, _>| body.into());
impl_from!(BeaconBlockEip4844, <E, FullPayload<E>>, <E, BlindedPayload<E>>, |body: BeaconBlockBodyEip4844<_, _>| body.into());
// We can clone blocks with payloads to blocks without payloads, without cloning the payload.
macro_rules! impl_clone_as_blinded {
@@ -575,6 +576,7 @@ macro_rules! impl_clone_as_blinded {
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!(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
// execution payload.

View File

@@ -8,12 +8,13 @@ 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.
///
/// This *superstruct* abstracts over the hard-fork.
#[superstruct(
variants(Base, Altair, Merge, Capella),
variants(Base, Altair, Merge, Eip4844),
variant_attributes(
derive(
Debug,
@@ -47,16 +48,16 @@ pub struct BeaconBlockBody<T: EthSpec, Payload: ExecPayload<T> = FullPayload<T>>
pub attestations: VariableList<Attestation<T>, T::MaxAttestations>,
pub deposits: VariableList<Deposit, T::MaxDeposits>,
pub voluntary_exits: VariableList<SignedVoluntaryExit, T::MaxVoluntaryExits>,
#[superstruct(only(Altair, Merge, Capella))]
#[superstruct(only(Altair, Merge, Eip4844))]
pub sync_aggregate: SyncAggregate<T>,
// We flatten the execution payload so that serde can use the name of the inner type,
// either `execution_payload` for full payloads, or `execution_payload_header` for blinded
// payloads.
#[superstruct(only(Merge, Capella))]
#[superstruct(only(Merge, Eip4844))]
#[serde(flatten)]
pub execution_payload: Payload,
#[superstruct(only(Capella))]
pub blob_kzgs: VariableList<KZGCommitment, T::MaxBlobsPerBlock>,
#[superstruct(only(Eip4844))]
pub blob_kzg_commitments: VariableList<KzgCommitment, T::MaxBlobsPerBlock>,
#[superstruct(only(Base, Altair))]
#[ssz(skip_serializing, skip_deserializing)]
#[tree_hash(skip_hashing)]
@@ -71,7 +72,7 @@ impl<'a, T: EthSpec> BeaconBlockBodyRef<'a, T> {
BeaconBlockBodyRef::Base { .. } => ForkName::Base,
BeaconBlockBodyRef::Altair { .. } => ForkName::Altair,
BeaconBlockBodyRef::Merge { .. } => ForkName::Merge,
BeaconBlockBodyRef::Capella { .. } => ForkName::Capella,
BeaconBlockBodyRef::Eip4844 { .. } => ForkName::Eip4844,
}
}
}
@@ -254,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.
impl<E: EthSpec> BeaconBlockBodyBase<E, FullPayload<E>> {
pub fn clone_as_blinded(&self) -> BeaconBlockBodyBase<E, BlindedPayload<E>> {
@@ -301,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>>>
for (
BeaconBlockBody<E, BlindedPayload<E>>,

View File

@@ -172,7 +172,7 @@ impl From<BeaconStateHash> for Hash256 {
/// The state of the `BeaconChain` at some slot.
#[superstruct(
variants(Base, Altair, Merge, Capella),
variants(Base, Altair, Merge, Eip4844),
variant_attributes(
derive(
Derivative,
@@ -250,9 +250,9 @@ where
pub current_epoch_attestations: VariableList<PendingAttestation<T>, T::MaxPendingAttestations>,
// Participation (Altair and later)
#[superstruct(only(Altair, Merge, Capella))]
#[superstruct(only(Altair, Merge, Eip4844))]
pub previous_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>,
#[superstruct(only(Altair, Merge, Capella))]
#[superstruct(only(Altair, Merge, Eip4844))]
pub current_epoch_participation: VariableList<ParticipationFlags, T::ValidatorRegistryLimit>,
// Finality
@@ -267,17 +267,17 @@ where
// Inactivity
#[serde(with = "ssz_types::serde_utils::quoted_u64_var_list")]
#[superstruct(only(Altair, Merge, Capella))]
#[superstruct(only(Altair, Merge, Eip4844))]
pub inactivity_scores: VariableList<u64, T::ValidatorRegistryLimit>,
// Light-client sync committees
#[superstruct(only(Altair, Merge, Capella))]
#[superstruct(only(Altair, Merge, Eip4844))]
pub current_sync_committee: Arc<SyncCommittee<T>>,
#[superstruct(only(Altair, Merge, Capella))]
#[superstruct(only(Altair, Merge, Eip4844))]
pub next_sync_committee: Arc<SyncCommittee<T>>,
// Execution
#[superstruct(only(Merge, Capella))]
#[superstruct(only(Merge, Eip4844))]
pub latest_execution_payload_header: ExecutionPayloadHeader<T>,
// Caching (not in the spec)
@@ -389,7 +389,7 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base { .. } => ForkName::Base,
BeaconState::Altair { .. } => ForkName::Altair,
BeaconState::Merge { .. } => ForkName::Merge,
BeaconState::Capella { .. } => ForkName::Capella,
BeaconState::Eip4844 { .. } => ForkName::Eip4844,
};
if fork_at_slot == object_fork {
@@ -1103,7 +1103,7 @@ impl<T: EthSpec> BeaconState<T> {
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::Capella(state) => (&mut state.validators, &mut state.balances),
BeaconState::Eip4844(state) => (&mut state.validators, &mut state.balances),
}
}
@@ -1300,14 +1300,14 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base(_) => Err(BeaconStateError::IncorrectStateVariant),
BeaconState::Altair(state) => Ok(&mut state.current_epoch_participation),
BeaconState::Merge(state) => Ok(&mut state.current_epoch_participation),
BeaconState::Capella(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::Capella(state) => Ok(&mut state.previous_epoch_participation),
BeaconState::Eip4844(state) => Ok(&mut state.previous_epoch_participation),
}
} else {
Err(BeaconStateError::EpochOutOfBounds)
@@ -1612,7 +1612,7 @@ impl<T: EthSpec> BeaconState<T> {
BeaconState::Base(inner) => BeaconState::Base(inner.clone()),
BeaconState::Altair(inner) => BeaconState::Altair(inner.clone()),
BeaconState::Merge(inner) => BeaconState::Merge(inner.clone()),
BeaconState::Capella(inner) => BeaconState::Capella(inner.clone()),
BeaconState::Eip4844(inner) => BeaconState::Eip4844(inner.clone()),
};
if config.committee_caches {
*res.committee_caches_mut() = self.committee_caches().clone();

View File

@@ -0,0 +1,74 @@
use ssz_types::VariableList;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use ssz::{Decode, DecodeError, Encode};
use tree_hash::{PackedEncoding, TreeHash};
use crate::test_utils::RngCore;
use crate::bls_field_element::BlsFieldElement;
use crate::{EthSpec, Uint256};
use crate::test_utils::TestRandom;
#[derive(Default, Debug, PartialEq, Hash, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Blob<T: EthSpec>(pub VariableList<BlsFieldElement, T::FieldElementsPerBlob>);
impl <T: EthSpec> TestRandom for Blob<T> {
fn random_for_test(rng: &mut impl RngCore) -> Self {
let mut res = Blob(VariableList::empty());
for i in 0..4096 {
let slice = ethereum_types::U256([rng.next_u64(), rng.next_u64(), rng.next_u64(), rng.next_u64()]);
let elem =BlsFieldElement(slice);
res.0.push(elem);
}
res
}
}
impl<T: EthSpec> Encode for Blob<T> {
fn is_ssz_fixed_len() -> bool {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob> as Encode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob> as Encode>::ssz_fixed_len()
}
fn ssz_bytes_len(&self) -> usize {
self.0.ssz_bytes_len()
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
self.0.ssz_append(buf)
}
}
impl<T: EthSpec> Decode for Blob<T> {
fn is_ssz_fixed_len() -> bool {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob> as Decode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob> as Decode>::ssz_fixed_len()
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob>>::from_ssz_bytes(bytes).map(Self)
}
}
impl<T: EthSpec> TreeHash for Blob<T> {
fn tree_hash_type() -> tree_hash::TreeHashType {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob>>::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> PackedEncoding{
self.0.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob>>::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> tree_hash::Hash256 {
self.0.tree_hash_root()
}
}

View File

@@ -5,6 +5,7 @@ use ssz_derive::{Decode, Encode};
use ssz_types::VariableList;
use tree_hash::TreeHash;
use tree_hash_derive::TreeHash;
use crate::kzg_proof::KzgProof;
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, PartialEq, Default)]
@@ -12,6 +13,7 @@ pub struct BlobsSidecar<E: EthSpec> {
pub beacon_block_root: Hash256,
pub beacon_block_slot: Slot,
pub blobs: VariableList<Blob<E::FieldElementsPerBlob>, E::MaxBlobsPerBlock>,
pub kzg_aggregate_proof: KzgProof,
}
impl<E: EthSpec> BlobsSidecar<E> {

View File

@@ -0,0 +1,59 @@
use crate::{EthSpec, Uint256};
use serde::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode};
use tree_hash::{PackedEncoding, TreeHash};
#[derive(Default, Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)]
#[serde(transparent)]
pub struct BlsFieldElement(pub Uint256);
impl Encode for BlsFieldElement {
fn is_ssz_fixed_len() -> bool {
<Uint256 as Encode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<Uint256 as Encode>::ssz_fixed_len()
}
fn ssz_bytes_len(&self) -> usize {
self.0.ssz_bytes_len()
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
self.0.ssz_append(buf)
}
}
impl Decode for BlsFieldElement {
fn is_ssz_fixed_len() -> bool {
<Uint256 as Decode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<Uint256 as Decode>::ssz_fixed_len()
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
<Uint256 as Decode>::from_ssz_bytes(bytes).map(Self)
}
}
impl TreeHash for BlsFieldElement {
fn tree_hash_type() -> tree_hash::TreeHashType {
<Uint256>::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> PackedEncoding{
self.0.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
<Uint256>::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> tree_hash::Hash256 {
self.0.tree_hash_root()
}
}

View File

@@ -153,10 +153,10 @@ pub struct ChainSpec {
pub safe_slots_to_import_optimistically: u64,
/*
* Capella hard fork params
* Eip4844 hard fork params
*/
pub capella_fork_version: [u8; 4],
pub capella_fork_epoch: Option<Epoch>,
pub eip4844_fork_version: [u8; 4],
pub eip4844_fork_epoch: Option<Epoch>,
/*
* Networking
@@ -238,16 +238,17 @@ impl ChainSpec {
/// Returns the name of the fork which is active at `epoch`.
pub fn fork_name_at_epoch(&self, epoch: Epoch) -> ForkName {
match self.capella_fork_epoch {
Some(fork_epoch) if epoch >= fork_epoch => ForkName::Capella,
match self.eip4844_fork_epoch {
Some(fork_epoch) if epoch >= fork_epoch => ForkName::Eip4844,
_ => match self.bellatrix_fork_epoch {
Some(fork_epoch) if epoch >= fork_epoch => ForkName::Merge,
_ => match self.altair_fork_epoch {
Some(fork_epoch) if epoch >= fork_epoch => ForkName::Altair,
_ => ForkName::Base,
},
},
}
}
}
/// Returns the fork version for a named fork.
@@ -256,7 +257,7 @@ impl ChainSpec {
ForkName::Base => self.genesis_fork_version,
ForkName::Altair => self.altair_fork_version,
ForkName::Merge => self.bellatrix_fork_version,
ForkName::Capella => self.capella_fork_version,
ForkName::Eip4844 => self.eip4844_fork_version,
}
}
@@ -266,7 +267,7 @@ impl ChainSpec {
ForkName::Base => Some(Epoch::new(0)),
ForkName::Altair => self.altair_fork_epoch,
ForkName::Merge => self.bellatrix_fork_epoch,
ForkName::Capella => self.capella_fork_epoch,
ForkName::Eip4844 => self.eip4844_fork_epoch,
}
}
@@ -276,7 +277,7 @@ impl ChainSpec {
BeaconState::Base(_) => self.inactivity_penalty_quotient,
BeaconState::Altair(_) => self.inactivity_penalty_quotient_altair,
BeaconState::Merge(_) => self.inactivity_penalty_quotient_bellatrix,
BeaconState::Capella(_) => self.inactivity_penalty_quotient_bellatrix,
BeaconState::Eip4844(_) => self.inactivity_penalty_quotient_bellatrix,
}
}
@@ -289,7 +290,7 @@ impl ChainSpec {
BeaconState::Base(_) => self.proportional_slashing_multiplier,
BeaconState::Altair(_) => self.proportional_slashing_multiplier_altair,
BeaconState::Merge(_) => self.proportional_slashing_multiplier_bellatrix,
BeaconState::Capella(_) => self.proportional_slashing_multiplier_bellatrix,
BeaconState::Eip4844(_) => self.proportional_slashing_multiplier_bellatrix,
}
}
@@ -302,7 +303,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::Capella(_) => self.min_slashing_penalty_quotient_bellatrix,
BeaconState::Eip4844(_) => self.min_slashing_penalty_quotient_bellatrix,
}
}
@@ -565,7 +566,7 @@ impl ChainSpec {
domain_sync_committee: 7,
domain_sync_committee_selection_proof: 8,
domain_contribution_and_proof: 9,
altair_fork_version: [0x01, 0x00, 0x00, 0x00],
altair_fork_version: [0x01, 0x00, 0x0f, 0xfd],
altair_fork_epoch: Some(Epoch::new(74240)),
/*
@@ -576,7 +577,7 @@ impl ChainSpec {
min_slashing_penalty_quotient_bellatrix: u64::checked_pow(2, 5)
.expect("pow does not overflow"),
proportional_slashing_multiplier_bellatrix: 3,
bellatrix_fork_version: [0x02, 0x00, 0x00, 0x00],
bellatrix_fork_version: [0x02, 0x00, 0x0f, 0xfd],
bellatrix_fork_epoch: Some(Epoch::new(144896)),
terminal_total_difficulty: Uint256::from_dec_str("58750000000000000000000")
.expect("terminal_total_difficulty is a valid integer"),
@@ -585,11 +586,11 @@ impl ChainSpec {
safe_slots_to_import_optimistically: 128u64,
/*
* Capella hardfork params
* Eip4844 hard fork params
*/
//FIXME(sean)
capella_fork_version: [0x03, 0x00, 0x00, 0x00],
capella_fork_epoch: None,
eip4844_fork_version: [0x04, 0x00, 0x00, 0xfd],
eip4844_fork_epoch: Some(Epoch::new(u64::MAX)),
/*
* Network specific
*/
@@ -645,10 +646,10 @@ impl ChainSpec {
// `Uint256::MAX` which is `2*256- 1`.
.checked_add(Uint256::one())
.expect("addition does not overflow"),
// Capella
// Eip4844
//FIXME(sean)
capella_fork_version: [0x03, 0x00, 0x00, 0x01],
capella_fork_epoch: None,
eip4844_fork_version: [0x03, 0x00, 0x00, 0x01],
eip4844_fork_epoch: None,
// Other
network_id: 2, // lighthouse testnet network id
deposit_chain_id: 5,
@@ -804,9 +805,8 @@ impl ChainSpec {
terminal_block_hash_activation_epoch: Epoch::new(u64::MAX),
safe_slots_to_import_optimistically: 128u64,
//FIXME(sean)
capella_fork_version: [0x03, 0x00, 0x00, 0x64],
capella_fork_epoch: None,
eip4844_fork_version: [0x04, 0x00, 0x00, 0x64],
eip4844_fork_epoch: Some(Epoch::new(u64::MAX)),
/*
* Network specific
@@ -883,15 +883,12 @@ pub struct Config {
#[serde(deserialize_with = "deserialize_fork_epoch")]
pub bellatrix_fork_epoch: Option<MaybeQuoted<Epoch>>,
// FIXME(sean): remove this default
#[serde(default = "default_capella_fork_version")]
#[serde(default = "default_eip4844_fork_version")]
#[serde(with = "eth2_serde_utils::bytes_4_hex")]
capella_fork_version: [u8; 4],
// FIXME(sean): remove this default
#[serde(default = "default_capella_fork_epoch")]
eip4844_fork_version: [u8; 4],
#[serde(serialize_with = "serialize_fork_epoch")]
#[serde(deserialize_with = "deserialize_fork_epoch")]
pub capella_fork_epoch: Option<MaybeQuoted<Epoch>>,
pub eip4844_fork_epoch: Option<MaybeQuoted<Epoch>>,
#[serde(with = "eth2_serde_utils::quoted_u64")]
seconds_per_slot: u64,
@@ -930,7 +927,7 @@ fn default_bellatrix_fork_version() -> [u8; 4] {
[0xff, 0xff, 0xff, 0xff]
}
fn default_capella_fork_version() -> [u8; 4] {
fn default_eip4844_fork_version() -> [u8; 4] {
// This value shouldn't be used.
[0xff, 0xff, 0xff, 0xff]
}
@@ -1031,9 +1028,9 @@ impl Config {
bellatrix_fork_epoch: spec
.bellatrix_fork_epoch
.map(|epoch| MaybeQuoted { value: epoch }),
capella_fork_version: spec.capella_fork_version,
capella_fork_epoch: spec
.capella_fork_epoch
eip4844_fork_version: spec.eip4844_fork_version,
eip4844_fork_epoch: spec
.eip4844_fork_epoch
.map(|epoch| MaybeQuoted { value: epoch }),
seconds_per_slot: spec.seconds_per_slot,
@@ -1080,8 +1077,8 @@ impl Config {
altair_fork_epoch,
bellatrix_fork_epoch,
bellatrix_fork_version,
capella_fork_epoch,
capella_fork_version,
eip4844_fork_epoch,
eip4844_fork_version,
seconds_per_slot,
seconds_per_eth1_block,
min_validator_withdrawability_delay,
@@ -1112,8 +1109,8 @@ impl Config {
altair_fork_epoch: altair_fork_epoch.map(|q| q.value),
bellatrix_fork_epoch: bellatrix_fork_epoch.map(|q| q.value),
bellatrix_fork_version,
capella_fork_epoch: capella_fork_epoch.map(|q| q.value),
capella_fork_version,
eip4844_fork_epoch: eip4844_fork_epoch.map(|q| q.value),
eip4844_fork_version,
seconds_per_slot,
seconds_per_eth1_block,
min_validator_withdrawability_delay,

View File

@@ -96,7 +96,7 @@ pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq +
type MinGasLimit: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MaxExtraDataBytes: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/*
* New in Capella
* New in Eip4844
*/
type MaxBlobsPerBlock: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type FieldElementsPerBlob: Unsigned + Clone + Sync + Send + Debug + PartialEq;

View File

@@ -1,4 +1,4 @@
use crate::{test_utils::TestRandom, *};
use crate::{test_utils::TestRandom, test_utils::RngCore, *, kzg_commitment::KzgCommitment, kzg_proof::KzgProof, blob::Blob};
use derivative::Derivative;
use serde_derive::{Deserialize, Serialize};
use ssz::Encode;

View File

@@ -47,10 +47,13 @@ impl ForkContext {
));
}
if spec.capella_fork_epoch.is_some() {
if spec.eip4844_fork_epoch.is_some() {
fork_to_digest.push((
ForkName::Capella,
ChainSpec::compute_fork_digest(spec.capella_fork_version, genesis_validators_root),
ForkName::Eip4844,
ChainSpec::compute_fork_digest(
spec.eip4844_fork_version,
genesis_validators_root,
),
));
}

View File

@@ -11,12 +11,12 @@ pub enum ForkName {
Base,
Altair,
Merge,
Capella,
Eip4844
}
impl 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`
@@ -27,21 +27,25 @@ impl ForkName {
ForkName::Base => {
spec.altair_fork_epoch = None;
spec.bellatrix_fork_epoch = None;
spec.eip4844_fork_epoch = None;
spec
}
ForkName::Altair => {
spec.altair_fork_epoch = Some(Epoch::new(0));
spec.bellatrix_fork_epoch = None;
spec.eip4844_fork_epoch = None;
spec
}
ForkName::Merge => {
spec.altair_fork_epoch = Some(Epoch::new(0));
spec.bellatrix_fork_epoch = Some(Epoch::new(0));
spec.eip4844_fork_epoch = None;
spec
}
ForkName::Capella => {
ForkName::Eip4844 => {
spec.altair_fork_epoch = Some(Epoch::new(0));
spec.bellatrix_fork_epoch = Some(Epoch::new(0));
spec.capella_fork_epoch = Some(Epoch::new(0));
spec.eip4844_fork_epoch = Some(Epoch::new(0));
spec
}
}
@@ -55,7 +59,7 @@ impl ForkName {
ForkName::Base => None,
ForkName::Altair => Some(ForkName::Base),
ForkName::Merge => Some(ForkName::Altair),
ForkName::Capella => Some(ForkName::Merge),
ForkName::Eip4844 => Some(ForkName::Merge),
}
}
@@ -66,8 +70,8 @@ impl ForkName {
match self {
ForkName::Base => Some(ForkName::Altair),
ForkName::Altair => Some(ForkName::Merge),
ForkName::Merge => Some(ForkName::Capella),
ForkName::Capella => None,
ForkName::Merge => Some(ForkName::Eip4844),
ForkName::Eip4844 => None,
}
}
}
@@ -109,10 +113,9 @@ macro_rules! map_fork_name_with {
let (value, extra_data) = $body;
($t::Merge(value), extra_data)
}
//TODO: don't have a beacon state variant for the new fork yet
ForkName::Capella => {
ForkName::Eip4844 => {
let (value, extra_data) = $body;
($t::Merge(value), extra_data)
($t::Eip4844(value), extra_data)
}
}
};
@@ -126,6 +129,7 @@ impl FromStr for ForkName {
"phase0" | "base" => ForkName::Base,
"altair" => ForkName::Altair,
"bellatrix" | "merge" => ForkName::Merge,
"eip4844" => ForkName::Eip4844,
_ => return Err(format!("unknown fork name: {}", fork_name)),
})
}
@@ -137,7 +141,7 @@ impl Display for ForkName {
ForkName::Base => "phase0".fmt(f),
ForkName::Altair => "altair".fmt(f),
ForkName::Merge => "bellatrix".fmt(f),
ForkName::Capella => "capella".fmt(f),
ForkName::Eip4844 => "eip4844".fmt(f),
}
}
}

View File

@@ -3,18 +3,18 @@ use crate::*;
use derivative::Derivative;
use serde_derive::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode};
use tree_hash::TreeHash;
use tree_hash::{PackedEncoding, TreeHash};
//TODO: is there a way around this newtype
#[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
#[derivative(PartialEq, Eq, Hash)]
pub struct KZGCommitment(#[serde(with = "BigArray")] [u8; 48]);
impl TreeHash for KZGCommitment {
pub struct KzgCommitment(#[serde(with = "BigArray")] [u8; 48]);
impl TreeHash for KzgCommitment {
fn tree_hash_type() -> tree_hash::TreeHashType {
<[u8; 48] as TreeHash>::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding{
self.0.tree_hash_packed_encoding()
}
@@ -27,23 +27,23 @@ impl TreeHash for KZGCommitment {
}
}
impl TestRandom for KZGCommitment {
impl TestRandom for KzgCommitment {
fn random_for_test(rng: &mut impl rand::RngCore) -> Self {
KZGCommitment(<[u8; 48] as TestRandom>::random_for_test(rng))
KzgCommitment(<[u8; 48] as TestRandom>::random_for_test(rng))
}
}
impl Decode for KZGCommitment {
impl Decode for KzgCommitment {
fn is_ssz_fixed_len() -> bool {
<[u8; 48] as Decode>::is_ssz_fixed_len()
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
<[u8; 48] as Decode>::from_ssz_bytes(bytes).map(KZGCommitment)
<[u8; 48] as Decode>::from_ssz_bytes(bytes).map(KzgCommitment)
}
}
impl Encode for KZGCommitment {
impl Encode for KzgCommitment {
fn is_ssz_fixed_len() -> bool {
<[u8; 48] as Encode>::is_ssz_fixed_len()
}

View File

@@ -0,0 +1,127 @@
use std::fmt;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use ssz::{Decode, DecodeError, Encode};
use tree_hash::{PackedEncoding, TreeHash};
use crate::test_utils::{RngCore, TestRandom};
const KZG_PROOF_BYTES_LEN: usize = 48;
#[derive(Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)]
#[serde(transparent)]
pub struct KzgProof(#[serde(with = "serde_kzg_proof")] pub [u8; KZG_PROOF_BYTES_LEN]);
impl fmt::Display for KzgProof {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", eth2_serde_utils::hex::encode(&self.0))
}
}
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)
}
}
impl Into<[u8; KZG_PROOF_BYTES_LEN]> for KzgProof {
fn into(self) -> [u8; KZG_PROOF_BYTES_LEN] {
self.0
}
}
pub mod serde_kzg_proof {
use serde::de::Error;
use super::*;
pub fn serialize<S>(bytes: &[u8; KZG_PROOF_BYTES_LEN], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&eth2_serde_utils::hex::encode(bytes))
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; KZG_PROOF_BYTES_LEN], D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
let bytes = eth2_serde_utils::hex::decode(&s).map_err(D::Error::custom)?;
if bytes.len() != KZG_PROOF_BYTES_LEN {
return Err(D::Error::custom(format!(
"incorrect byte length {}, expected {}",
bytes.len(),
KZG_PROOF_BYTES_LEN
)));
}
let mut array = [0; KZG_PROOF_BYTES_LEN];
array[..].copy_from_slice(&bytes);
Ok(array)
}
}
impl Encode for KzgProof {
fn is_ssz_fixed_len() -> bool {
<[u8; KZG_PROOF_BYTES_LEN] as Encode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<[u8; KZG_PROOF_BYTES_LEN] as Encode>::ssz_fixed_len()
}
fn ssz_bytes_len(&self) -> usize {
self.0.ssz_bytes_len()
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
self.0.ssz_append(buf)
}
}
impl Decode for KzgProof {
fn is_ssz_fixed_len() -> bool {
<[u8; KZG_PROOF_BYTES_LEN] as Decode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<[u8; KZG_PROOF_BYTES_LEN] as Decode>::ssz_fixed_len()
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
<[u8; KZG_PROOF_BYTES_LEN]>::from_ssz_bytes(bytes).map(Self)
}
}
impl TreeHash for KzgProof {
fn tree_hash_type() -> tree_hash::TreeHashType {
<[u8; KZG_PROOF_BYTES_LEN]>::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
self.0.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
<[u8; KZG_PROOF_BYTES_LEN]>::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> tree_hash::Hash256 {
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

@@ -86,14 +86,16 @@ pub mod sync_subnet_id;
mod tree_hash_impls;
pub mod validator_registration_data;
mod blobs_sidecar;
mod kzg_commitment;
mod signed_blobs_sidecar;
pub mod slot_data;
#[cfg(feature = "sqlite")]
pub mod sqlite;
pub use kzg_commitment::KZGCommitment;
pub mod kzg_commitment;
pub mod kzg_proof;
pub mod bls_field_element;
pub mod blob;
pub mod signed_blobs_sidecar;
pub mod blobs_sidecar;
use ethereum_types::{H160, H256};
@@ -103,12 +105,12 @@ pub use crate::attestation_data::AttestationData;
pub use crate::attestation_duty::AttestationDuty;
pub use crate::attester_slashing::AttesterSlashing;
pub use crate::beacon_block::{
BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockCapella, BeaconBlockMerge, BeaconBlockRef,
BeaconBlockRefMut, BlindedBeaconBlock,
BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockMerge, BeaconBlockRef,
BeaconBlockRefMut, BeaconBlockEip4844
};
pub use crate::beacon_block_body::{
BeaconBlockBody, BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyCapella,
BeaconBlockBodyMerge, BeaconBlockBodyRef, BeaconBlockBodyRefMut,
BeaconBlockBody, BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge,
BeaconBlockBodyRef, BeaconBlockBodyRefMut, BeaconBlockBodyEip4844
};
pub use crate::beacon_block_header::BeaconBlockHeader;
pub use crate::beacon_committee::{BeaconCommittee, OwnedBeaconCommittee};
@@ -150,7 +152,7 @@ pub use crate::shuffling_id::AttestationShufflingId;
pub use crate::signed_aggregate_and_proof::SignedAggregateAndProof;
pub use crate::signed_beacon_block::{
SignedBeaconBlock, SignedBeaconBlockAltair, SignedBeaconBlockBase, SignedBeaconBlockHash,
SignedBeaconBlockMerge, SignedBlindedBeaconBlock,SignedBeaconBlockCapella
SignedBeaconBlockMerge, SignedBlindedBeaconBlock, SignedBeaconBlockEip4844,
};
pub use crate::signed_beacon_block_header::SignedBeaconBlockHeader;
pub use crate::signed_contribution_and_proof::SignedContributionAndProof;

View File

@@ -8,6 +8,7 @@ use std::fmt::Debug;
use std::hash::Hash;
use test_random_derive::TestRandom;
use tree_hash::{PackedEncoding, TreeHash};
use core::hash::Hasher;
#[derive(Debug)]
pub enum BlockType {

View File

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