Skip to content

Commit

Permalink
impl forkhash calculation to ChainSpecification
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Jan 17, 2023
1 parent 55a504d commit 368ba7d
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 196 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/cli/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ eyre = "0.6.8"
shellexpand = "2.1"
walkdir = "2.3"

# ethers type conversions
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }

# Tracing
tracing = "0.1"
74 changes: 69 additions & 5 deletions crates/cli/utils/src/chainspec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ethers_core::utils::Genesis as EthersGenesis;
use reth_primitives::{
utils::serde_helpers::deserialize_stringified_u64, Address, Bytes, GenesisAccount, Header,
H256, U256,
proofs::genesis_state_root, utils::serde_helpers::deserialize_stringified_u64, Address, Bytes,
ForkHash, GenesisAccount, Header, H160, H256, INITIAL_BASE_FEE, U256,
};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf};
Expand All @@ -16,6 +17,23 @@ pub struct ChainSpecification {
pub genesis: Genesis,
}

impl ChainSpecification {
/// Obtains a [`ForkHash`] based on the genesis hash and configured forks activated by block
/// number.
pub fn fork_hash(&self) -> ForkHash {
let mut genesis_header = Header::from(self.genesis.clone());

// set initial base fee depending on eip-1559
if self.consensus.london_block == 0 {
genesis_header.base_fee_per_gas = Some(INITIAL_BASE_FEE);
}

let fork_blocks = self.consensus.fork_blocks();
let sealed_genesis = genesis_header.seal();
fork_blocks.iter().fold(ForkHash::from(sealed_genesis.hash()), |acc, block| acc + *block)
}
}

/// The genesis block specification.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -37,8 +55,6 @@ pub struct Genesis {
pub mix_hash: H256,
/// The genesis header coinbase address.
pub coinbase: Address,
/// The genesis state root.
pub state_root: H256,
/// The initial state of accounts in the genesis block.
pub alloc: HashMap<Address, GenesisAccount>,
}
Expand All @@ -50,7 +66,7 @@ impl From<Genesis> for Header {
difficulty: genesis.difficulty,
nonce: genesis.nonce,
extra_data: genesis.extra_data,
state_root: genesis.state_root,
state_root: genesis_state_root(genesis.alloc),
timestamp: genesis.timestamp,
mix_hash: genesis.mix_hash,
beneficiary: genesis.coinbase,
Expand All @@ -59,6 +75,54 @@ impl From<Genesis> for Header {
}
}

impl From<EthersGenesis> for ChainSpecification {
fn from(genesis: EthersGenesis) -> Self {
let alloc = genesis
.alloc
.iter()
.map(|(addr, account)| (addr.0.into(), account.clone().into()))
.collect::<HashMap<H160, GenesisAccount>>();

Self {
consensus: reth_consensus::Config {
chain_id: genesis.config.chain_id,
homestead_block: genesis.config.homestead_block.unwrap_or_default(),
dao_fork_block: genesis.config.dao_fork_block.unwrap_or_default(),
dao_fork_support: genesis.config.dao_fork_support,
eip_150_block: genesis.config.eip150_block.unwrap_or_default(),
eip_155_block: genesis.config.eip155_block.unwrap_or_default(),
eip_158_block: genesis.config.eip158_block.unwrap_or_default(),
byzantium_block: genesis.config.byzantium_block.unwrap_or_default(),
petersburg_block: genesis.config.petersburg_block.unwrap_or_default(),
constantinople_block: genesis.config.constantinople_block.unwrap_or_default(),
istanbul_block: genesis.config.istanbul_block.unwrap_or_default(),
muir_glacier_block: genesis.config.muir_glacier_block.unwrap_or_default(),
berlin_block: genesis.config.berlin_block.unwrap_or_default(),
london_block: genesis.config.london_block.unwrap_or_default(),
arrow_glacier_block: genesis.config.arrow_glacier_block.unwrap_or_default(),
gray_glacier_block: genesis.config.gray_glacier_block.unwrap_or_default(),
merge_netsplit_block: genesis.config.merge_netsplit_block,
merge_terminal_total_difficulty: genesis
.config
.terminal_total_difficulty
.unwrap_or_default()
.as_u128(),
..Default::default() // TODO: when paris_block is removed, remove this
},
genesis: Genesis {
nonce: genesis.nonce.as_u64(),
timestamp: genesis.timestamp.as_u64(),
gas_limit: genesis.gas_limit.as_u64(),
difficulty: genesis.difficulty.into(),
mix_hash: genesis.mix_hash.0.into(),
coinbase: genesis.coinbase.0.into(),
extra_data: genesis.extra_data.0.into(),
alloc,
},
}
}
}

/// Clap value parser for [ChainSpecification]s that takes either a built-in chainspec or the path
/// to a custom one.
pub fn chain_spec_value_parser(s: &str) -> Result<ChainSpecification, eyre::Error> {
Expand Down
7 changes: 3 additions & 4 deletions crates/net/network/src/test_utils/testnet.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! A network implementation for testing purposes.

use futures::{FutureExt, StreamExt};
use pin_project::pin_project;
use reth_eth_wire::DisconnectReason;
use crate::{
error::NetworkError, eth_requests::EthRequestHandler, NetworkConfig, NetworkEvent,
NetworkHandle, NetworkManager,
};
use futures::{FutureExt, StreamExt};
use pin_project::pin_project;
use reth_eth_wire::DisconnectReason;
use reth_primitives::PeerId;
use reth_provider::{test_utils::NoopProvider, BlockProvider, HeaderProvider};
use secp256k1::SecretKey;
Expand Down Expand Up @@ -355,4 +355,3 @@ impl NetworkEventStream {
}
}
}

10 changes: 6 additions & 4 deletions crates/net/network/tests/it/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ use reth_interfaces::{
sync::{SyncState, SyncStateUpdater},
};
use reth_net_common::ban_list::BanList;
use reth_network::test_utils::{
enr_to_peer_id, unused_tcp_udp, NetworkEventStream, PeerConfig, Testnet, GETH_TIMEOUT,
use reth_network::{
test_utils::{
enr_to_peer_id, unused_tcp_udp, NetworkEventStream, PeerConfig, Testnet, GETH_TIMEOUT,
},
NetworkConfig, NetworkEvent, NetworkManager, PeersConfig,
};
use reth_network::{NetworkConfig, NetworkEvent, NetworkManager, PeersConfig};
use reth_network_api::NetworkInfo;
use reth_network_api::{NetworkInfo, PeersInfo};
use reth_primitives::{HeadersDirection, NodeRecord, PeerId};
use reth_provider::test_utils::NoopProvider;
use reth_transaction_pool::test_utils::testing_pool;
Expand Down

0 comments on commit 368ba7d

Please sign in to comment.