Merge remote-tracking branch 'origin/unstable' into tree-states

This commit is contained in:
Michael Sproul
2022-03-01 16:03:41 +11:00
209 changed files with 6107 additions and 1362 deletions

View File

@@ -110,9 +110,34 @@ impl<T: EthSpec> SlotData for Attestation<T> {
#[cfg(test)]
mod tests {
use super::*;
use crate::*;
use super::*;
// Check the in-memory size of an `Attestation`, which is useful for reasoning about memory
// and preventing regressions.
//
// This test will only pass with `blst`, if we run these tests with Milagro or another
// BLS library in future we will have to make it generic.
#[test]
fn size_of() {
use std::mem::size_of;
let aggregation_bits =
size_of::<BitList<<MainnetEthSpec as EthSpec>::MaxValidatorsPerCommittee>>();
let attestation_data = size_of::<AttestationData>();
let signature = size_of::<AggregateSignature>();
assert_eq!(aggregation_bits, 56);
assert_eq!(attestation_data, 128);
assert_eq!(signature, 288 + 16);
let attestation_expected = aggregation_bits + attestation_data + signature;
assert_eq!(attestation_expected, 488);
assert_eq!(
size_of::<Attestation<MainnetEthSpec>>(),
attestation_expected
);
}
ssz_and_tree_hash_tests!(Attestation<MainnetEthSpec>);
}

View File

@@ -1,5 +1,5 @@
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::disallowed_method)]
#![allow(clippy::disallowed_methods)]
#![allow(clippy::indexing_slicing)]
use super::Error;

View File

@@ -28,6 +28,11 @@ pub enum Domain {
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(PartialEq, Debug, Clone)]
pub struct ChainSpec {
/*
* Config name
*/
pub config_name: Option<String>,
/*
* Constants
*/
@@ -139,7 +144,7 @@ pub struct ChainSpec {
/// The Merge fork epoch is optional, with `None` representing "Merge never happens".
pub bellatrix_fork_epoch: Option<Epoch>,
pub terminal_total_difficulty: Uint256,
pub terminal_block_hash: Hash256,
pub terminal_block_hash: ExecutionBlockHash,
pub terminal_block_hash_activation_epoch: Epoch,
/*
@@ -412,6 +417,10 @@ impl ChainSpec {
/// Returns a `ChainSpec` compatible with the Ethereum Foundation specification.
pub fn mainnet() -> Self {
Self {
/*
* Config name
*/
config_name: Some("mainnet".to_string()),
/*
* Constants
*/
@@ -547,7 +556,7 @@ impl ChainSpec {
// `Uint256::MAX` which is `2*256- 1`.
.checked_add(Uint256::one())
.expect("addition does not overflow"),
terminal_block_hash: Hash256::zero(),
terminal_block_hash: ExecutionBlockHash::zero(),
terminal_block_hash_activation_epoch: Epoch::new(u64::MAX),
/*
@@ -570,6 +579,7 @@ impl ChainSpec {
let boot_nodes = vec![];
Self {
config_name: None,
max_committees_per_slot: 4,
target_committee_size: 4,
churn_limit_quotient: 32,
@@ -607,6 +617,7 @@ impl ChainSpec {
/// Returns a `ChainSpec` compatible with the Gnosis Beacon Chain specification.
pub fn gnosis() -> Self {
Self {
config_name: Some("gnosis".to_string()),
/*
* Constants
*/
@@ -742,7 +753,7 @@ impl ChainSpec {
// `Uint256::MAX` which is `2*256- 1`.
.checked_add(Uint256::one())
.expect("addition does not overflow"),
terminal_block_hash: Hash256::zero(),
terminal_block_hash: ExecutionBlockHash::zero(),
terminal_block_hash_activation_epoch: Epoch::new(u64::MAX),
/*
@@ -770,6 +781,10 @@ impl Default for ChainSpec {
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "UPPERCASE")]
pub struct Config {
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub config_name: Option<String>,
#[serde(default)]
pub preset_base: String,
@@ -779,7 +794,7 @@ pub struct Config {
pub terminal_total_difficulty: Uint256,
// TODO(merge): remove this default
#[serde(default = "default_terminal_block_hash")]
pub terminal_block_hash: Hash256,
pub terminal_block_hash: ExecutionBlockHash,
// TODO(merge): remove this default
#[serde(default = "default_terminal_block_hash_activation_epoch")]
pub terminal_block_hash_activation_epoch: Epoch,
@@ -862,8 +877,8 @@ const fn default_terminal_total_difficulty() -> Uint256 {
])
}
fn default_terminal_block_hash() -> Hash256 {
Hash256::zero()
fn default_terminal_block_hash() -> ExecutionBlockHash {
ExecutionBlockHash::zero()
}
fn default_terminal_block_hash_activation_epoch() -> Epoch {
@@ -921,6 +936,7 @@ impl Config {
pub fn from_chain_spec<T: EthSpec>(spec: &ChainSpec) -> Self {
Self {
config_name: spec.config_name.clone(),
preset_base: T::spec_name().to_string(),
terminal_total_difficulty: spec.terminal_total_difficulty,
@@ -971,6 +987,7 @@ impl Config {
pub fn apply_to_chain_spec<T: EthSpec>(&self, chain_spec: &ChainSpec) -> Option<ChainSpec> {
// Pattern match here to avoid missing any fields.
let &Config {
ref config_name,
ref preset_base,
terminal_total_difficulty,
terminal_block_hash,
@@ -1004,6 +1021,7 @@ impl Config {
}
Some(ChainSpec {
config_name: config_name.clone(),
min_genesis_active_validator_count,
min_genesis_time,
genesis_fork_version,

View File

@@ -46,7 +46,6 @@ impl ConfigAndPreset {
let u32_hex = |v: u32| hex_string(&v.to_le_bytes());
let u8_hex = |v: u8| hex_string(&v.to_le_bytes());
let fields = vec![
("config_name", self.config.preset_base.clone()),
(
"bls_withdrawal_prefix",
u8_hex(spec.bls_withdrawal_prefix_byte),

View File

@@ -0,0 +1,101 @@
use crate::test_utils::TestRandom;
use crate::Hash256;
use rand::RngCore;
use serde_derive::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode};
use std::fmt;
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, Hash)]
#[serde(transparent)]
pub struct ExecutionBlockHash(Hash256);
impl ExecutionBlockHash {
pub fn zero() -> Self {
Self(Hash256::zero())
}
pub fn repeat_byte(b: u8) -> Self {
Self(Hash256::repeat_byte(b))
}
pub fn from_root(root: Hash256) -> Self {
Self(root)
}
pub fn into_root(self) -> Hash256 {
self.0
}
}
impl Encode for ExecutionBlockHash {
fn is_ssz_fixed_len() -> bool {
<Hash256 as Encode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<Hash256 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 ExecutionBlockHash {
fn is_ssz_fixed_len() -> bool {
<Hash256 as Decode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<Hash256 as Decode>::ssz_fixed_len()
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
Hash256::from_ssz_bytes(bytes).map(Self)
}
}
impl tree_hash::TreeHash for ExecutionBlockHash {
fn tree_hash_type() -> tree_hash::TreeHashType {
Hash256::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
self.0.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
Hash256::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> tree_hash::Hash256 {
self.0.tree_hash_root()
}
}
impl TestRandom for ExecutionBlockHash {
fn random_for_test(rng: &mut impl RngCore) -> Self {
Self(Hash256::random_for_test(rng))
}
}
impl std::str::FromStr for ExecutionBlockHash {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Hash256::from_str(s)
.map(Self)
.map_err(|e| format!("{:?}", e))
}
}
impl fmt::Display for ExecutionBlockHash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

View File

@@ -18,10 +18,10 @@ pub type Transaction<T> = VariableList<u8, T>;
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
#[serde(bound = "T: EthSpec")]
pub struct ExecutionPayload<T: EthSpec> {
pub parent_hash: Hash256,
pub parent_hash: ExecutionBlockHash,
pub fee_recipient: Address,
pub state_root: Hash256,
pub receipt_root: Hash256,
pub receipts_root: Hash256,
#[serde(with = "ssz_types::serde_utils::hex_fixed_vec")]
pub logs_bloom: FixedVector<u8, T::BytesPerLogsBloom>,
pub random: Hash256,
@@ -37,7 +37,7 @@ pub struct ExecutionPayload<T: EthSpec> {
pub extra_data: VariableList<u8, T::MaxExtraDataBytes>,
#[serde(with = "eth2_serde_utils::quoted_u256")]
pub base_fee_per_gas: Uint256,
pub block_hash: Hash256,
pub block_hash: ExecutionBlockHash,
#[serde(with = "ssz_types::serde_utils::list_of_hex_var_list")]
pub transactions:
VariableList<Transaction<T::MaxBytesPerTransaction>, T::MaxTransactionsPerPayload>,

View File

@@ -12,10 +12,10 @@ use ssz_types::{FixedVector, VariableList};
Default, Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
)]
pub struct ExecutionPayloadHeader<T: EthSpec> {
pub parent_hash: Hash256,
pub parent_hash: ExecutionBlockHash,
pub fee_recipient: Address,
pub state_root: Hash256,
pub receipt_root: Hash256,
pub receipts_root: Hash256,
#[serde(with = "ssz_types::serde_utils::hex_fixed_vec")]
pub logs_bloom: FixedVector<u8, T::BytesPerLogsBloom>,
pub random: Hash256,
@@ -31,7 +31,7 @@ pub struct ExecutionPayloadHeader<T: EthSpec> {
pub extra_data: VariableList<u8, T::MaxExtraDataBytes>,
#[serde(with = "eth2_serde_utils::quoted_u256")]
pub base_fee_per_gas: Uint256,
pub block_hash: Hash256,
pub block_hash: ExecutionBlockHash,
pub transactions_root: Hash256,
}

View File

@@ -7,7 +7,7 @@
not(test),
deny(
clippy::integer_arithmetic,
clippy::disallowed_method,
clippy::disallowed_methods,
clippy::indexing_slicing
)
)]
@@ -37,6 +37,7 @@ pub mod deposit_message;
pub mod enr_fork_id;
pub mod eth1_data;
pub mod eth_spec;
pub mod execution_block_hash;
pub mod execution_payload;
pub mod execution_payload_header;
pub mod fork;
@@ -113,6 +114,7 @@ pub use crate::deposit_message::DepositMessage;
pub use crate::enr_fork_id::EnrForkId;
pub use crate::eth1_data::Eth1Data;
pub use crate::eth_spec::EthSpecId;
pub use crate::execution_block_hash::ExecutionBlockHash;
pub use crate::execution_payload::{ExecutionPayload, Transaction};
pub use crate::execution_payload_header::ExecutionPayloadHeader;
pub use crate::fork::Fork;