Skip to content

Commit

Permalink
impl From for Status/SealedHeader (paradigmxyz#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Jan 19, 2023
1 parent 82b10fa commit d0e3741
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 11 deletions.
7 changes: 0 additions & 7 deletions crates/consensus/src/constants.rs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion crates/consensus/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
time::SystemTime,
};

use crate::constants;
use reth_primitives::constants;

/// Validate header standalone
pub fn validate_header_standalone(
Expand Down
40 changes: 39 additions & 1 deletion crates/net/eth-wire/src/types/status.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions crates/primitives/src/constants.rs
Original file line number Diff line number Diff line change
@@ -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"));
Expand Down
25 changes: 24 additions & 1 deletion crates/primitives/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d0e3741

Please sign in to comment.