From d0e3741136103dafd2701d176faf2a337808e491 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Wed, 18 Jan 2023 19:23:02 -0800 Subject: [PATCH] impl From for Status/SealedHeader (#927) --- crates/consensus/src/constants.rs | 7 ----- crates/consensus/src/verification.rs | 2 +- crates/net/eth-wire/src/types/status.rs | 40 ++++++++++++++++++++++++- crates/primitives/src/constants.rs | 11 +++++++ crates/primitives/src/header.rs | 25 +++++++++++++++- crates/primitives/src/lib.rs | 2 +- 6 files changed, 76 insertions(+), 11 deletions(-) diff --git a/crates/consensus/src/constants.rs b/crates/consensus/src/constants.rs index be83c30336e3..e16bbae6c09c 100644 --- a/crates/consensus/src/constants.rs +++ b/crates/consensus/src/constants.rs @@ -1,8 +1 @@ //! Reth block execution/validation configuration and constants - -/// Initial base fee as defined in: https://eips.ethereum.org/EIPS/eip-1559 -pub const EIP1559_INITIAL_BASE_FEE: u64 = 1_000_000_000; -/// Base fee max change denominator as defined in: https://eips.ethereum.org/EIPS/eip-1559 -pub const EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR: u64 = 8; -/// Elasticity multiplier as defined in: https://eips.ethereum.org/EIPS/eip-1559 -pub const EIP1559_ELASTICITY_MULTIPLIER: u64 = 2; diff --git a/crates/consensus/src/verification.rs b/crates/consensus/src/verification.rs index 26aa85140363..753b7e6c3c15 100644 --- a/crates/consensus/src/verification.rs +++ b/crates/consensus/src/verification.rs @@ -10,7 +10,7 @@ use std::{ time::SystemTime, }; -use crate::constants; +use reth_primitives::constants; /// Validate header standalone pub fn validate_header_standalone( diff --git a/crates/net/eth-wire/src/types/status.rs b/crates/net/eth-wire/src/types/status.rs index a589d0e14619..ddf0f79cf4c7 100644 --- a/crates/net/eth-wire/src/types/status.rs +++ b/crates/net/eth-wire/src/types/status.rs @@ -1,7 +1,11 @@ use crate::{EthVersion, StatusBuilder}; +use ethers_core::utils::Genesis; use reth_codecs::derive_arbitrary; -use reth_primitives::{Chain, ForkId, Hardfork, H256, MAINNET, U256}; +use reth_primitives::{ + constants::EIP1559_INITIAL_BASE_FEE, Chain, ChainSpec, ForkId, Hardfork, Header, H256, MAINNET, + U256, +}; use reth_rlp::{RlpDecodable, RlpEncodable}; use serde::{Deserialize, Serialize}; use std::fmt::{Debug, Display}; @@ -39,6 +43,40 @@ pub struct Status { pub forkid: ForkId, } +impl From for Status { + fn from(genesis: Genesis) -> Status { + let chain = genesis.config.chain_id; + let total_difficulty = genesis.difficulty.into(); + let mut chainspec = ChainSpec::from(genesis); + let mut header = Header::from(chainspec.genesis().clone()); + + let hardforks = chainspec.hardforks(); + + // set initial base fee depending on eip-1559 + if Some(&0u64) == hardforks.get(&Hardfork::London) { + header.base_fee_per_gas = Some(EIP1559_INITIAL_BASE_FEE); + } + + // calculate the hash + let sealed_header = header.seal(); + + // set the new genesis hash after modifying the base fee + chainspec.genesis_hash = sealed_header.hash(); + + // we need to calculate the fork id AFTER re-setting the genesis hash + let forkid = chainspec.fork_id(0); + + Status { + version: EthVersion::Eth67 as u8, + chain: Chain::Id(chain), + total_difficulty, + blockhash: sealed_header.hash(), + genesis: sealed_header.hash(), + forkid, + } + } +} + impl Status { /// Helper for returning a builder for the status message. pub fn builder() -> StatusBuilder { diff --git a/crates/primitives/src/constants.rs b/crates/primitives/src/constants.rs index e830caba8b5b..49425e45a16a 100644 --- a/crates/primitives/src/constants.rs +++ b/crates/primitives/src/constants.rs @@ -1,6 +1,17 @@ +//! Ethereum protocol-related constants + use crate::H256; use hex_literal::hex; +/// Initial base fee as defined in: https://eips.ethereum.org/EIPS/eip-1559 +pub const EIP1559_INITIAL_BASE_FEE: u64 = 1_000_000_000; + +/// Base fee max change denominator as defined in: https://eips.ethereum.org/EIPS/eip-1559 +pub const EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR: u64 = 8; + +/// Elasticity multiplier as defined in: https://eips.ethereum.org/EIPS/eip-1559 +pub const EIP1559_ELASTICITY_MULTIPLIER: u64 = 2; + /// The Ethereum mainnet genesis hash. pub const MAINNET_GENESIS: H256 = H256(hex!("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")); diff --git a/crates/primitives/src/header.rs b/crates/primitives/src/header.rs index 99a433b18bae..e791f0aa7d3e 100644 --- a/crates/primitives/src/header.rs +++ b/crates/primitives/src/header.rs @@ -4,7 +4,7 @@ use crate::{ BlockHash, BlockNumber, Bloom, Bytes, H160, H256, U256, }; use bytes::{BufMut, BytesMut}; -use ethers_core::types::H64; +use ethers_core::types::{Block, H256 as EthersH256, H64}; use reth_codecs::{derive_arbitrary, main_codec, Compact}; use reth_rlp::{length_of_length, Decodable, Encodable}; use serde::{Deserialize, Serialize}; @@ -217,6 +217,29 @@ pub struct SealedHeader { hash: BlockHash, } +impl From> for SealedHeader { + fn from(block: Block) -> Self { + let header = Header { + number: block.number.unwrap().as_u64(), + gas_limit: block.gas_limit.as_u64(), + difficulty: block.difficulty.into(), + nonce: block.nonce.unwrap().to_low_u64_be(), + extra_data: block.extra_data.0.into(), + state_root: block.state_root.0.into(), + timestamp: block.timestamp.as_u64(), + mix_hash: block.mix_hash.unwrap().0.into(), + beneficiary: block.author.unwrap().0.into(), + base_fee_per_gas: block.base_fee_per_gas.map(|fee| fee.as_u64()), + ..Default::default() + }; + let hash = match block.hash { + Some(hash) => hash.0.into(), + None => header.hash_slow(), + }; + SealedHeader::new(header, hash) + } +} + impl Default for SealedHeader { fn default() -> Self { let header = Header::default(); diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 691101264a4f..c95003cae807 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -15,7 +15,7 @@ mod block; pub mod bloom; mod chain; mod chain_spec; -mod constants; +pub mod constants; mod error; mod forkid; mod genesis;