Skip to content

Commit 3ace10d

Browse files
i-m-adityamattsse
andauthored
refactor: phase out ether genesis in primitives (paradigmxyz#2311)
Co-authored-by: Matthias Seitz <[email protected]>
1 parent b90d0b4 commit 3ace10d

File tree

7 files changed

+1040
-72
lines changed

7 files changed

+1040
-72
lines changed

crates/net/eth-wire/src/types/status.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::{EthVersion, StatusBuilder};
22

3-
use ethers_core::utils::Genesis;
43
use reth_codecs::derive_arbitrary;
5-
use reth_primitives::{hex, Chain, ChainSpec, ForkId, Hardfork, Head, H256, MAINNET, U256};
4+
use reth_primitives::{
5+
hex, Chain, ChainSpec, ForkId, Genesis, Hardfork, Head, H256, MAINNET, U256,
6+
};
67
use reth_rlp::{RlpDecodable, RlpEncodable};
78
use std::fmt::{Debug, Display};
89

@@ -46,7 +47,7 @@ pub struct Status {
4647
impl From<Genesis> for Status {
4748
fn from(genesis: Genesis) -> Status {
4849
let chain = genesis.config.chain_id;
49-
let total_difficulty = genesis.difficulty.into();
50+
let total_difficulty = genesis.difficulty;
5051
let chainspec = ChainSpec::from(genesis);
5152

5253
Status {

crates/primitives/src/chain/spec.rs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ use crate::{
33
forkid::ForkFilterKey,
44
header::Head,
55
proofs::genesis_state_root,
6-
BlockNumber, Chain, ForkFilter, ForkHash, ForkId, Genesis, GenesisAccount, Hardfork, Header,
7-
SealedHeader, H160, H256, U256,
6+
BlockNumber, Chain, ForkFilter, ForkHash, ForkId, Genesis, Hardfork, Header, SealedHeader,
7+
H256, U256,
88
};
9-
use ethers_core::utils::Genesis as EthersGenesis;
109
use hex_literal::hex;
1110
use once_cell::sync::Lazy;
1211
use serde::{Deserialize, Serialize};
13-
use std::{
14-
collections::{BTreeMap, HashMap},
15-
sync::Arc,
16-
};
12+
use std::{collections::BTreeMap, sync::Arc};
1713

1814
/// The Ethereum mainnet spec
1915
pub static MAINNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
@@ -316,25 +312,8 @@ impl ChainSpec {
316312
}
317313
}
318314

319-
impl From<EthersGenesis> for ChainSpec {
320-
fn from(genesis: EthersGenesis) -> Self {
321-
let alloc = genesis
322-
.alloc
323-
.iter()
324-
.map(|(addr, account)| (addr.0.into(), account.clone().into()))
325-
.collect::<HashMap<H160, GenesisAccount>>();
326-
327-
let genesis_block = Genesis {
328-
nonce: genesis.nonce.as_u64(),
329-
timestamp: genesis.timestamp.as_u64(),
330-
gas_limit: genesis.gas_limit.as_u64(),
331-
difficulty: genesis.difficulty.into(),
332-
mix_hash: genesis.mix_hash.0.into(),
333-
coinbase: genesis.coinbase.0.into(),
334-
extra_data: genesis.extra_data.0.into(),
335-
alloc,
336-
};
337-
315+
impl From<Genesis> for ChainSpec {
316+
fn from(genesis: Genesis) -> Self {
338317
// Block-based hardforks
339318
let hardfork_opts = vec![
340319
(Hardfork::Homestead, genesis.config.homestead_block),
@@ -361,7 +340,7 @@ impl From<EthersGenesis> for ChainSpec {
361340
hardforks.insert(
362341
Hardfork::Paris,
363342
ForkCondition::TTD {
364-
total_difficulty: ttd.into(),
343+
total_difficulty: ttd,
365344
fork_block: genesis.config.merge_netsplit_block,
366345
},
367346
);
@@ -379,7 +358,7 @@ impl From<EthersGenesis> for ChainSpec {
379358

380359
Self {
381360
chain: genesis.config.chain_id.into(),
382-
genesis: genesis_block,
361+
genesis,
383362
genesis_hash: None,
384363
fork_timestamps: ForkTimestamps::from_hardforks(&hardforks),
385364
hardforks,
@@ -417,21 +396,26 @@ impl ForkTimestamps {
417396
#[serde(untagged)]
418397
pub enum AllGenesisFormats {
419398
/// The geth genesis format
420-
Geth(EthersGenesis),
399+
Geth(Genesis),
421400
/// The reth genesis format
422401
Reth(ChainSpec),
423402
}
424403

425-
impl From<EthersGenesis> for AllGenesisFormats {
426-
fn from(genesis: EthersGenesis) -> Self {
404+
impl From<Genesis> for AllGenesisFormats {
405+
fn from(genesis: Genesis) -> Self {
427406
Self::Geth(genesis)
428407
}
429408
}
430409

410+
impl From<ChainSpec> for AllGenesisFormats {
411+
fn from(genesis: ChainSpec) -> Self {
412+
Self::Reth(genesis)
413+
}
414+
}
415+
431416
impl From<Arc<ChainSpec>> for AllGenesisFormats {
432-
fn from(mut genesis: Arc<ChainSpec>) -> Self {
433-
let cloned_genesis = Arc::make_mut(&mut genesis).clone();
434-
Self::Reth(cloned_genesis)
417+
fn from(genesis: Arc<ChainSpec>) -> Self {
418+
Arc::try_unwrap(genesis).unwrap_or_else(|arc| (*arc).clone()).into()
435419
}
436420
}
437421

@@ -1195,7 +1179,7 @@ mod tests {
11951179
}
11961180
"#;
11971181

1198-
let genesis: ethers_core::utils::Genesis = serde_json::from_str(geth_genesis).unwrap();
1182+
let genesis: Genesis = serde_json::from_str(geth_genesis).unwrap();
11991183
let chainspec = ChainSpec::from(genesis);
12001184

12011185
// assert a bunch of hardforks that should be set
@@ -1334,6 +1318,8 @@ mod tests {
13341318
}
13351319
}
13361320
"#;
1321+
1322+
let _genesis = serde_json::from_str::<Genesis>(hive_json).unwrap();
13371323
let genesis = serde_json::from_str::<AllGenesisFormats>(hive_json).unwrap();
13381324
let chainspec: ChainSpec = genesis.into();
13391325
assert_eq!(chainspec.genesis_hash, None);

0 commit comments

Comments
 (0)