impl From for Status/SealedHeader (#927)

This commit is contained in:
Georgios Konstantopoulos
2023-01-18 19:23:02 -08:00
committed by GitHub
parent 82b10fae41
commit d0e3741136
6 changed files with 76 additions and 11 deletions

View File

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

View File

@@ -10,7 +10,7 @@ use std::{
time::SystemTime,
};
use crate::constants;
use reth_primitives::constants;
/// Validate header standalone
pub fn validate_header_standalone(

View File

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

View File

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

View File

@@ -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<Block<EthersH256>> for SealedHeader {
fn from(block: Block<EthersH256>) -> 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();

View File

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