Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove AllGenesisFormats #9013

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/chainspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
pub use alloy_chains::{Chain, ChainKind, NamedChain};
pub use info::ChainInfo;
pub use spec::{
AllGenesisFormats, BaseFeeParams, BaseFeeParamsKind, ChainSpec, ChainSpecBuilder,
DepositContract, ForkBaseFeeParams, DEV, GOERLI, HOLESKY, MAINNET, SEPOLIA,
BaseFeeParams, BaseFeeParamsKind, ChainSpec, ChainSpecBuilder, DepositContract,
ForkBaseFeeParams, DEV, GOERLI, HOLESKY, MAINNET, SEPOLIA,
};
#[cfg(feature = "optimism")]
pub use spec::{BASE_MAINNET, BASE_SEPOLIA, OP_MAINNET, OP_SEPOLIA};
Expand Down
75 changes: 6 additions & 69 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,43 +1102,6 @@ impl From<Genesis> for ChainSpec {
}
}

/// A helper type for compatibility with geth's config
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum AllGenesisFormats {
/// The reth genesis format
Reth(ChainSpec),
/// The geth genesis format
Geth(Genesis),
}

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(genesis: Arc<ChainSpec>) -> Self {
Arc::try_unwrap(genesis).unwrap_or_else(|arc| (*arc).clone()).into()
}
}

impl From<AllGenesisFormats> for ChainSpec {
fn from(genesis: AllGenesisFormats) -> Self {
match genesis {
AllGenesisFormats::Geth(genesis) => genesis.into(),
AllGenesisFormats::Reth(genesis) => genesis,
}
}
}

/// A helper to build custom chain specs
#[derive(Debug, Default, Clone)]
pub struct ChainSpecBuilder {
Expand Down Expand Up @@ -2653,8 +2616,7 @@ Post-merge hard forks (timestamp based):
}
"#;

let _genesis = serde_json::from_str::<Genesis>(hive_json).unwrap();
let genesis = serde_json::from_str::<AllGenesisFormats>(hive_json).unwrap();
let genesis = serde_json::from_str::<Genesis>(hive_json).unwrap();
let chainspec: ChainSpec = genesis.into();
assert_eq!(chainspec.genesis_hash, None);
assert_eq!(chainspec.chain, Chain::from_named(NamedChain::Optimism));
Expand Down Expand Up @@ -2839,13 +2801,7 @@ Post-merge hard forks (timestamp based):
#[test]
fn test_parse_prague_genesis_all_formats() {
let s = r#"{"config":{"ethash":{},"chainId":1337,"homesteadBlock":0,"eip150Block":0,"eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"berlinBlock":0,"londonBlock":0,"terminalTotalDifficulty":0,"terminalTotalDifficultyPassed":true,"shanghaiTime":0,"cancunTime":4661, "pragueTime": 4662},"nonce":"0x0","timestamp":"0x0","extraData":"0x","gasLimit":"0x4c4b40","difficulty":"0x1","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"658bdf435d810c91414ec09147daa6db62406379":{"balance":"0x487a9a304539440000"},"aa00000000000000000000000000000000000000":{"code":"0x6042","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x1","nonce":"0x1"},"bb00000000000000000000000000000000000000":{"code":"0x600154600354","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x2","nonce":"0x1"}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","baseFeePerGas":"0x3b9aca00"}"#;
let genesis: AllGenesisFormats = serde_json::from_str(s).unwrap();

// this should be the genesis format
let genesis = match genesis {
AllGenesisFormats::Geth(genesis) => genesis,
_ => panic!("expected geth genesis format"),
};
let genesis: Genesis = serde_json::from_str(s).unwrap();

// assert that the alloc was picked up
let acc = genesis
Expand All @@ -2862,13 +2818,7 @@ Post-merge hard forks (timestamp based):
#[test]
fn test_parse_cancun_genesis_all_formats() {
let s = r#"{"config":{"ethash":{},"chainId":1337,"homesteadBlock":0,"eip150Block":0,"eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"berlinBlock":0,"londonBlock":0,"terminalTotalDifficulty":0,"terminalTotalDifficultyPassed":true,"shanghaiTime":0,"cancunTime":4661},"nonce":"0x0","timestamp":"0x0","extraData":"0x","gasLimit":"0x4c4b40","difficulty":"0x1","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"658bdf435d810c91414ec09147daa6db62406379":{"balance":"0x487a9a304539440000"},"aa00000000000000000000000000000000000000":{"code":"0x6042","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x1","nonce":"0x1"},"bb00000000000000000000000000000000000000":{"code":"0x600154600354","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x2","nonce":"0x1"}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","baseFeePerGas":"0x3b9aca00"}"#;
let genesis: AllGenesisFormats = serde_json::from_str(s).unwrap();

// this should be the genesis format
let genesis = match genesis {
AllGenesisFormats::Geth(genesis) => genesis,
_ => panic!("expected geth genesis format"),
};
let genesis: Genesis = serde_json::from_str(s).unwrap();

// assert that the alloc was picked up
let acc = genesis
Expand Down Expand Up @@ -2935,7 +2885,7 @@ Post-merge hard forks (timestamp based):
}

#[test]
fn test_all_genesis_formats_deserialization() {
fn test_genesis_format_deserialization() {
// custom genesis with chain config
let config = ChainConfig {
chain_id: 2600,
Expand Down Expand Up @@ -2973,22 +2923,9 @@ Post-merge hard forks (timestamp based):

// ensure genesis is deserialized correctly
let serialized_genesis = serde_json::to_string(&genesis).unwrap();
let deserialized_genesis: AllGenesisFormats =
serde_json::from_str(&serialized_genesis).unwrap();
assert!(matches!(deserialized_genesis, AllGenesisFormats::Geth(_)));

// build chain
let chain_spec = ChainSpecBuilder::default()
.chain(2600.into())
.genesis(genesis)
.cancun_activated()
.build();
let deserialized_genesis: Genesis = serde_json::from_str(&serialized_genesis).unwrap();

// ensure chain spec is deserialized correctly
let serialized_chain_spec = serde_json::to_string(&chain_spec).unwrap();
let deserialized_chain_spec: AllGenesisFormats =
serde_json::from_str(&serialized_chain_spec).unwrap();
assert!(matches!(deserialized_chain_spec, AllGenesisFormats::Reth(_)))
assert_eq!(genesis, deserialized_genesis);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/node-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ reth-prune-types.workspace = true
reth-stages-types.workspace = true

# ethereum
alloy-genesis.workspace = true
alloy-rpc-types-engine.workspace = true

# async
Expand Down Expand Up @@ -98,7 +99,6 @@ procfs = "0.16.0"
[dev-dependencies]
# test vectors generation
proptest.workspace = true
alloy-genesis.workspace = true

[features]
optimism = [
Expand Down
100 changes: 3 additions & 97 deletions crates/node-core/src/args/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Clap parser utilities

use reth_chainspec::{AllGenesisFormats, ChainSpec};
use alloy_genesis::Genesis;
use reth_chainspec::ChainSpec;
use reth_fs_util as fs;
use reth_primitives::{BlockHashOrNumber, B256};
use std::{
Expand Down Expand Up @@ -105,7 +106,7 @@ pub fn genesis_value_parser(s: &str) -> eyre::Result<Arc<ChainSpec>, eyre::Error
};

// both serialized Genesis and ChainSpec structs supported
let genesis: AllGenesisFormats = serde_json::from_str(&raw)?;
let genesis: Genesis = serde_json::from_str(&raw)?;

Arc::new(genesis.into())
}
Expand Down Expand Up @@ -168,12 +169,8 @@ pub fn parse_socket_address(value: &str) -> eyre::Result<SocketAddr, SocketAddre
#[cfg(test)]
mod tests {
use super::*;
use alloy_genesis::{ChainConfig, Genesis, GenesisAccount};
use proptest::prelude::Rng;
use reth_chainspec::ChainSpecBuilder;
use reth_primitives::{hex, Address, U256};
use secp256k1::rand::thread_rng;
use std::collections::HashMap;

#[test]
fn parse_known_chain_spec() {
Expand All @@ -183,97 +180,6 @@ mod tests {
}
}

#[test]
fn parse_chain_spec_from_memory() {
let custom_genesis_from_json = r#"
{
"nonce": "0x0",
"timestamp": "0x653FEE9E",
"gasLimit": "0x1388",
"difficulty": "0x0",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b": {
"balance": "0x21"
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"config": {
"chainId": 2600,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"terminalTotalDifficulty": 0,
"terminalTotalDifficultyPassed": true,
"shanghaiTime": 0
}
}
"#;

let chain_from_json = genesis_value_parser(custom_genesis_from_json).unwrap();

// using structs
let config = ChainConfig {
chain_id: 2600,
homestead_block: Some(0),
eip150_block: Some(0),
eip155_block: Some(0),
eip158_block: Some(0),
byzantium_block: Some(0),
constantinople_block: Some(0),
petersburg_block: Some(0),
istanbul_block: Some(0),
berlin_block: Some(0),
london_block: Some(0),
shanghai_time: Some(0),
terminal_total_difficulty: Some(U256::ZERO),
terminal_total_difficulty_passed: true,
..Default::default()
};
let genesis = Genesis {
config,
nonce: 0,
timestamp: 1698688670,
gas_limit: 5000,
difficulty: U256::ZERO,
mix_hash: B256::ZERO,
coinbase: Address::ZERO,
number: Some(0),
..Default::default()
};

// seed accounts after genesis struct created
let address = hex!("6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b").into();
let account = GenesisAccount::default().with_balance(U256::from(33));
let genesis = genesis.extend_accounts(HashMap::from([(address, account)]));

let custom_genesis_from_struct = serde_json::to_string(&genesis).unwrap();
let chain_from_struct = genesis_value_parser(&custom_genesis_from_struct).unwrap();
assert_eq!(chain_from_json.genesis(), chain_from_struct.genesis());

// chain spec
let chain_spec = ChainSpecBuilder::default()
.chain(2600.into())
.genesis(genesis)
.cancun_activated()
.build();

let chain_spec_json = serde_json::to_string(&chain_spec).unwrap();
let custom_genesis_from_spec = genesis_value_parser(&chain_spec_json).unwrap();

assert_eq!(custom_genesis_from_spec.chain(), chain_from_struct.chain());
}

#[test]
fn parse_socket_addresses() {
for value in ["localhost:9000", ":9000", "9000"] {
Expand Down
Loading