refactor: phase out ether genesis in primitives (#2311)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Aditya Pandey
2023-06-16 20:10:02 +05:30
committed by GitHub
parent b90d0b4106
commit 3ace10d835
7 changed files with 1040 additions and 72 deletions

View File

@ -3,17 +3,13 @@ use crate::{
forkid::ForkFilterKey,
header::Head,
proofs::genesis_state_root,
BlockNumber, Chain, ForkFilter, ForkHash, ForkId, Genesis, GenesisAccount, Hardfork, Header,
SealedHeader, H160, H256, U256,
BlockNumber, Chain, ForkFilter, ForkHash, ForkId, Genesis, Hardfork, Header, SealedHeader,
H256, U256,
};
use ethers_core::utils::Genesis as EthersGenesis;
use hex_literal::hex;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashMap},
sync::Arc,
};
use std::{collections::BTreeMap, sync::Arc};
/// The Ethereum mainnet spec
pub static MAINNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
@ -316,25 +312,8 @@ impl ChainSpec {
}
}
impl From<EthersGenesis> for ChainSpec {
fn from(genesis: EthersGenesis) -> Self {
let alloc = genesis
.alloc
.iter()
.map(|(addr, account)| (addr.0.into(), account.clone().into()))
.collect::<HashMap<H160, GenesisAccount>>();
let genesis_block = 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,
};
impl From<Genesis> for ChainSpec {
fn from(genesis: Genesis) -> Self {
// Block-based hardforks
let hardfork_opts = vec![
(Hardfork::Homestead, genesis.config.homestead_block),
@ -361,7 +340,7 @@ impl From<EthersGenesis> for ChainSpec {
hardforks.insert(
Hardfork::Paris,
ForkCondition::TTD {
total_difficulty: ttd.into(),
total_difficulty: ttd,
fork_block: genesis.config.merge_netsplit_block,
},
);
@ -379,7 +358,7 @@ impl From<EthersGenesis> for ChainSpec {
Self {
chain: genesis.config.chain_id.into(),
genesis: genesis_block,
genesis,
genesis_hash: None,
fork_timestamps: ForkTimestamps::from_hardforks(&hardforks),
hardforks,
@ -417,21 +396,26 @@ impl ForkTimestamps {
#[serde(untagged)]
pub enum AllGenesisFormats {
/// The geth genesis format
Geth(EthersGenesis),
Geth(Genesis),
/// The reth genesis format
Reth(ChainSpec),
}
impl From<EthersGenesis> for AllGenesisFormats {
fn from(genesis: EthersGenesis) -> Self {
impl From<Genesis> for AllGenesisFormats {
fn from(genesis: Genesis) -> Self {
Self::Geth(genesis)
}
}
impl From<ChainSpec> for AllGenesisFormats {
fn from(genesis: ChainSpec) -> Self {
Self::Reth(genesis)
}
}
impl From<Arc<ChainSpec>> for AllGenesisFormats {
fn from(mut genesis: Arc<ChainSpec>) -> Self {
let cloned_genesis = Arc::make_mut(&mut genesis).clone();
Self::Reth(cloned_genesis)
fn from(genesis: Arc<ChainSpec>) -> Self {
Arc::try_unwrap(genesis).unwrap_or_else(|arc| (*arc).clone()).into()
}
}
@ -1195,7 +1179,7 @@ mod tests {
}
"#;
let genesis: ethers_core::utils::Genesis = serde_json::from_str(geth_genesis).unwrap();
let genesis: Genesis = serde_json::from_str(geth_genesis).unwrap();
let chainspec = ChainSpec::from(genesis);
// assert a bunch of hardforks that should be set
@ -1334,6 +1318,8 @@ mod tests {
}
}
"#;
let _genesis = serde_json::from_str::<Genesis>(hive_json).unwrap();
let genesis = serde_json::from_str::<AllGenesisFormats>(hive_json).unwrap();
let chainspec: ChainSpec = genesis.into();
assert_eq!(chainspec.genesis_hash, None);