add/update types

This commit is contained in:
realbigsean 2022-02-18 18:29:05 -07:00
parent ff145b986f
commit de44b300c0
No known key found for this signature in database
GPG Key ID: B372B64D866BF8CC
23 changed files with 189 additions and 19 deletions

11
Cargo.lock generated
View File

@ -5736,6 +5736,16 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "serde-big-array"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18b20e7752957bbe9661cff4e0bb04d183d0948cdab2ea58cdb9df36a61dfe62"
dependencies = [
"serde",
"serde_derive",
]
[[package]]
name = "serde_array_query"
version = "0.1.0"
@ -7107,6 +7117,7 @@ dependencies = [
"rusqlite",
"safe_arith",
"serde",
"serde-big-array",
"serde_derive",
"serde_json",
"serde_with",

View File

@ -296,7 +296,8 @@ pub fn gossipsub_config(network_load: u8, fork_context: Arc<ForkContext>) -> Gos
match fork_context.current_fork() {
// according to: https://github.com/ethereum/consensus-specs/blob/dev/specs/merge/p2p-interface.md#the-gossip-domain-gossipsub
// the derivation of the message-id remains the same in the merge
ForkName::Altair | ForkName::Merge => {
//TODO(sean): figure this out
ForkName::Altair | ForkName::Merge | ForkName::Dank => {
let topic_len_bytes = topic_bytes.len().to_le_bytes();
let mut vec = Vec::with_capacity(
prefix.len() + topic_len_bytes.len() + topic_bytes.len() + message.data.len(),

View File

@ -17,7 +17,7 @@ use std::sync::Arc;
use tokio_util::codec::{Decoder, Encoder};
use types::{
EthSpec, ForkContext, ForkName, SignedBeaconBlock, SignedBeaconBlockAltair,
SignedBeaconBlockBase, SignedBeaconBlockMerge,
SignedBeaconBlockBase, SignedBeaconBlockDank, SignedBeaconBlockMerge,
};
use unsigned_varint::codec::Uvi;
@ -407,6 +407,7 @@ fn context_bytes<T: EthSpec>(
return match **ref_box_block {
// NOTE: If you are adding another fork type here, be sure to modify the
// `fork_context.to_context_bytes()` function to support it as well!
SignedBeaconBlock::Dank { .. } => fork_context.to_context_bytes(ForkName::Dank),
SignedBeaconBlock::Merge { .. } => {
// Merge context being `None` implies that "merge never happened".
fork_context.to_context_bytes(ForkName::Merge)
@ -586,6 +587,9 @@ fn handle_v2_response<T: EthSpec>(
decoded_buffer,
)?),
)))),
ForkName::Dank => Ok(Some(RPCResponse::BlocksByRange(Box::new(
SignedBeaconBlock::Dank(SignedBeaconBlockDank::from_ssz_bytes(decoded_buffer)?),
)))),
},
Protocol::BlocksByRoot => match fork_name {
ForkName::Altair => Ok(Some(RPCResponse::BlocksByRoot(Arc::new(
@ -601,6 +605,9 @@ fn handle_v2_response<T: EthSpec>(
decoded_buffer,
)?),
)))),
ForkName::Dank => Ok(Some(RPCResponse::BlocksByRoot(Box::new(
SignedBeaconBlock::Dank(SignedBeaconBlockDank::from_ssz_bytes(decoded_buffer)?),
)))),
},
_ => Err(RPCError::ErrorResponse(
RPCResponseErrorCode::InvalidRequest,

View File

@ -11,8 +11,8 @@ use std::sync::Arc;
use types::{
Attestation, AttesterSlashing, EthSpec, ForkContext, ForkName, ProposerSlashing,
SignedAggregateAndProof, SignedBeaconBlock, SignedBeaconBlockAltair, SignedBeaconBlockBase,
SignedBeaconBlockMerge, SignedContributionAndProof, SignedVoluntaryExit, SubnetId,
SyncCommitteeMessage, SyncSubnetId,
SignedBeaconBlockDank, SignedBeaconBlockMerge, SignedContributionAndProof, SignedVoluntaryExit,
SubnetId, SyncCommitteeMessage, SyncSubnetId,
};
#[derive(Debug, Clone, PartialEq)]
@ -167,6 +167,10 @@ impl<T: EthSpec> PubsubMessage<T> {
SignedBeaconBlockMerge::from_ssz_bytes(data)
.map_err(|e| format!("{:?}", e))?,
),
Some(ForkName::Dank) => SignedBeaconBlock::<T>::Dank(
SignedBeaconBlockDank::from_ssz_bytes(data)
.map_err(|e| format!("{:?}", e))?,
),
None => {
return Err(format!(
"Unknown gossipsub fork digest: {:?}",

View File

@ -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) => {

View File

@ -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 {

View File

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

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

View File

@ -9,6 +9,7 @@ name = "benches"
harness = false
[dependencies]
serde-big-array = {version = "0.3.2", features = ["const-generics"]}
bls = { path = "../../crypto/bls" }
compare_fields = { path = "../../common/compare_fields" }
compare_fields_derive = { path = "../../common/compare_fields_derive" }

View File

@ -1,6 +1,6 @@
use crate::beacon_block_body::{
BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge, BeaconBlockBodyRef,
BeaconBlockBodyRefMut,
BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyDank, BeaconBlockBodyMerge,
BeaconBlockBodyRef, BeaconBlockBodyRefMut,
};
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),
variants(Base, Altair, Merge, Dank),
variant_attributes(
derive(
Debug,
@ -64,6 +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(Dank), partial_getter(rename = "body_dank"))]
pub body: BeaconBlockBodyDank<T, Payload>,
}
pub type BlindedBeaconBlock<E> = BeaconBlock<E, BlindedPayload<E>>;
@ -189,6 +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::Dank { .. } => ForkName::Dank,
};
if fork_at_slot == object_fork {

View File

@ -0,0 +1,13 @@
use crate::{BLSFieldElement, Blob, EthSpec, SignedBeaconBlock};
use serde_derive::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
use ssz_types::{FixedVector, VariableList};
use tree_hash::TreeHash;
use tree_hash_derive::TreeHash;
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, TreeHash)]
pub struct BeaconBlockAndBlobs<E: EthSpec> {
pub block: SignedBeaconBlock<E>,
pub blobs: VariableList<Blob<E::ChunksPerBlob>, E::MaxObjectListSize>,
}

View File

@ -13,7 +13,7 @@ use tree_hash_derive::TreeHash;
///
/// This *superstruct* abstracts over the hard-fork.
#[superstruct(
variants(Base, Altair, Merge),
variants(Base, Altair, Merge, Dank),
variant_attributes(
derive(
Debug,
@ -47,14 +47,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))]
#[superstruct(only(Altair, Merge, Dank))]
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))]
#[superstruct(only(Merge, Dank))]
#[serde(flatten)]
pub execution_payload: Payload,
#[superstruct(only(Dank))]
pub blob_kzgs: VariableList<KZGCommitment, T::MaxObjectListSize>,
#[superstruct(only(Base, Altair))]
#[ssz(skip_serializing, skip_deserializing)]
#[tree_hash(skip_hashing)]
@ -69,6 +71,7 @@ impl<'a, T: EthSpec> BeaconBlockBodyRef<'a, T> {
BeaconBlockBodyRef::Base { .. } => ForkName::Base,
BeaconBlockBodyRef::Altair { .. } => ForkName::Altair,
BeaconBlockBodyRef::Merge { .. } => ForkName::Merge,
BeaconBlockBodyRef::Dank { .. } => ForkName::Dank,
}
}
}

View File

@ -150,6 +150,10 @@ pub struct ChainSpec {
pub terminal_block_hash_activation_epoch: Epoch,
pub safe_slots_to_import_optimistically: u64,
/*
* Danksharding hard fork params
*/
/*
* Networking
*/
@ -245,6 +249,8 @@ impl ChainSpec {
ForkName::Base => self.genesis_fork_version,
ForkName::Altair => self.altair_fork_version,
ForkName::Merge => self.bellatrix_fork_version,
//TODO: update this
ForkName::Dank => self.bellatrix_fork_version,
}
}
@ -254,6 +260,8 @@ impl ChainSpec {
ForkName::Base => Some(Epoch::new(0)),
ForkName::Altair => self.altair_fork_epoch,
ForkName::Merge => self.bellatrix_fork_epoch,
//TODO: update this
ForkName::Dank => self.bellatrix_fork_epoch,
}
}

View File

@ -95,6 +95,11 @@ pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq +
type GasLimitDenominator: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MinGasLimit: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type MaxExtraDataBytes: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/*
* New in Danksharding
*/
type MaxObjectListSize: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type ChunksPerBlob: Unsigned + Clone + Sync + Send + Debug + PartialEq;
/*
* Derived values (set these CAREFULLY)
*/
@ -262,6 +267,8 @@ impl EthSpec for MainnetEthSpec {
type GasLimitDenominator = U1024;
type MinGasLimit = U5000;
type MaxExtraDataBytes = U32;
type MaxObjectListSize = U16777216; // 2**24
type ChunksPerBlob = U4096;
type SyncSubcommitteeSize = U128; // 512 committee size / 4 sync committee subnet count
type MaxPendingAttestations = U4096; // 128 max attestations * 32 slots per epoch
type SlotsPerEth1VotingPeriod = U2048; // 64 epochs * 32 slots per epoch
@ -309,7 +316,9 @@ impl EthSpec for MinimalEthSpec {
BytesPerLogsBloom,
GasLimitDenominator,
MinGasLimit,
MaxExtraDataBytes
MaxExtraDataBytes,
MaxObjectListSize,
ChunksPerBlob
});
fn default_spec() -> ChainSpec {
@ -354,6 +363,8 @@ impl EthSpec for GnosisEthSpec {
type SyncSubcommitteeSize = U128; // 512 committee size / 4 sync committee subnet count
type MaxPendingAttestations = U2048; // 128 max attestations * 16 slots per epoch
type SlotsPerEth1VotingPeriod = U1024; // 64 epochs * 16 slots per epoch
type MaxObjectListSize = U16777216; // 2**24
type ChunksPerBlob = U4096;
fn default_spec() -> ChainSpec {
ChainSpec::gnosis()

View File

@ -1,4 +1,4 @@
use crate::{ChainSpec, Epoch};
use crate::{ChainSpec, Epoch, Fork};
use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fmt::{self, Display, Formatter};
@ -11,6 +11,7 @@ pub enum ForkName {
Base,
Altair,
Merge,
Dank,
}
impl ForkName {
@ -38,6 +39,12 @@ impl ForkName {
spec.bellatrix_fork_epoch = Some(Epoch::new(0));
spec
}
//TODO(sean): update
ForkName::Dank => {
spec.altair_fork_epoch = Some(Epoch::new(0));
spec.bellatrix_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::Dank => 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::Dank),
ForkName::Dank => None,
}
}
}
@ -101,6 +110,11 @@ 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::Dank => {
let (value, extra_data) = $body;
($t::Merge(value), extra_data)
}
}
};
}
@ -124,6 +138,7 @@ impl Display for ForkName {
ForkName::Base => "phase0".fmt(f),
ForkName::Altair => "altair".fmt(f),
ForkName::Merge => "bellatrix".fmt(f),
ForkName::Dank => "dank".fmt(f),
}
}
}

View File

@ -0,0 +1,63 @@
use crate::test_utils::TestRandom;
use crate::*;
use derivative::Derivative;
use serde_derive::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode};
use ssz_derive::{Decode, Encode};
use ssz_types::VariableList;
use superstruct::superstruct;
use test_random_derive::TestRandom;
use tree_hash::TreeHash;
use tree_hash_derive::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 {
fn tree_hash_type() -> tree_hash::TreeHashType {
<[u8; 48] as TreeHash>::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
self.0.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
<[u8; 48] as TreeHash>::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> tree_hash::Hash256 {
self.0.tree_hash_root()
}
}
impl TestRandom for KZGCommitment {
fn random_for_test(rng: &mut impl rand::RngCore) -> Self {
KZGCommitment(<[u8; 48] as TestRandom>::random_for_test(rng))
}
}
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)
}
}
impl Encode for KZGCommitment {
fn is_ssz_fixed_len() -> bool {
<[u8; 48] as Encode>::is_ssz_fixed_len()
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
self.0.ssz_append(buf)
}
fn ssz_bytes_len(&self) -> usize {
self.0.ssz_bytes_len()
}
}

View File

@ -86,11 +86,15 @@ pub mod sync_subnet_id;
mod tree_hash_impls;
pub mod validator_registration_data;
mod beacon_block_and_blobs;
mod kzg_commitment;
pub mod slot_data;
#[cfg(feature = "sqlite")]
pub mod sqlite;
pub use kzg_commitment::KZGCommitment;
use ethereum_types::{H160, H256};
use serde::Serialize;
pub use crate::aggregate_and_proof::AggregateAndProof;
pub use crate::attestation::{Attestation, Error as AttestationError};
@ -98,12 +102,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, BeaconBlockMerge, BeaconBlockRef,
BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockDank, BeaconBlockMerge, BeaconBlockRef,
BeaconBlockRefMut, BlindedBeaconBlock,
};
pub use crate::beacon_block_body::{
BeaconBlockBody, BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyMerge,
BeaconBlockBodyRef, BeaconBlockBodyRefMut,
BeaconBlockBody, BeaconBlockBodyAltair, BeaconBlockBodyBase, BeaconBlockBodyDank,
BeaconBlockBodyMerge, BeaconBlockBodyRef, BeaconBlockBodyRefMut,
};
pub use crate::beacon_block_header::BeaconBlockHeader;
pub use crate::beacon_committee::{BeaconCommittee, OwnedBeaconCommittee};
@ -144,7 +148,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,
SignedBeaconBlockMerge, SignedBlindedBeaconBlock,SignedBeaconBlockDank
};
pub use crate::signed_beacon_block_header::SignedBeaconBlockHeader;
pub use crate::signed_contribution_and_proof::SignedContributionAndProof;
@ -165,12 +169,15 @@ pub use crate::validator::Validator;
pub use crate::validator_registration_data::*;
pub use crate::validator_subscription::ValidatorSubscription;
pub use crate::voluntary_exit::VoluntaryExit;
use serde_big_array::BigArray;
pub type CommitteeIndex = u64;
pub type Hash256 = H256;
pub type Uint256 = ethereum_types::U256;
pub type Address = H160;
pub type ForkVersion = [u8; 4];
pub type BLSFieldElement = Uint256;
pub type Blob<T> = FixedVector<BLSFieldElement, T>;
pub use bls::{
AggregatePublicKey, AggregateSignature, Keypair, PublicKey, PublicKeyBytes, SecretKey,

View File

@ -38,7 +38,7 @@ impl From<SignedBeaconBlockHash> for Hash256 {
/// A `BeaconBlock` and a signature from its proposer.
#[superstruct(
variants(Base, Altair, Merge),
variants(Base, Altair, Merge, Dank),
variant_attributes(
derive(
Debug,
@ -72,6 +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(Dank), partial_getter(rename = "message_dank"))]
pub message: BeaconBlockDank<E, Payload>,
pub signature: Signature,
}
@ -129,6 +131,9 @@ impl<E: EthSpec, Payload: ExecPayload<E>> SignedBeaconBlock<E, Payload> {
BeaconBlock::Merge(message) => {
SignedBeaconBlock::Merge(SignedBeaconBlockMerge { message, signature })
}
BeaconBlock::Dank(message) => {
SignedBeaconBlock::Dank(SignedBeaconBlockDank { message, signature })
}
}
}

View File

@ -78,5 +78,6 @@ pub fn previous_fork(fork_name: ForkName) -> ForkName {
ForkName::Base => ForkName::Base,
ForkName::Altair => ForkName::Base,
ForkName::Merge => ForkName::Altair, // TODO: Check this when tests are released..
ForkName::Dank => ForkName::Merge, // TODO: Check this when tests are released..
}
}

View File

@ -278,6 +278,7 @@ impl<E: EthSpec, T: EpochTransition<E>> Case for EpochProcessing<E, T> {
}
// No phase0 tests for Altair and later.
ForkName::Altair | ForkName::Merge => T::name() != "participation_record_updates",
ForkName::Dank => false, // TODO: revisit when tests are out
}
}

View File

@ -61,6 +61,7 @@ impl<E: EthSpec> Case for ForkTest<E> {
ForkName::Base => panic!("phase0 not supported"),
ForkName::Altair => upgrade_to_altair(&mut result_state, spec).map(|_| result_state),
ForkName::Merge => upgrade_to_bellatrix(&mut result_state, spec).map(|_| result_state),
ForkName::Dank => panic!("danksharding not supported"),
};
compare_beacon_state_results_without_caches(&mut result, &mut expected)

View File

@ -42,6 +42,11 @@ impl<E: EthSpec> LoadCase for TransitionTest<E> {
spec.altair_fork_epoch = Some(Epoch::new(0));
spec.bellatrix_fork_epoch = Some(metadata.fork_epoch);
}
//TODO(sean): fix
ForkName::Dank => {
spec.altair_fork_epoch = Some(Epoch::new(0));
spec.bellatrix_fork_epoch = Some(metadata.fork_epoch);
}
}
// Load blocks

View File

@ -90,6 +90,11 @@ impl<'a, T: EthSpec, Payload: ExecPayload<T>> Web3SignerObject<'a, T, Payload> {
block: None,
block_header: Some(block.block_header()),
}),
BeaconBlock::Dank(_) => Ok(Web3SignerObject::BeaconBlock {
version: ForkName::Dank,
block: None,
block_header: Some(block.block_header()),
}),
}
}