chore: remove AllGenesisFormats (#9013)

This commit is contained in:
joshieDo
2024-06-21 16:05:37 +02:00
committed by GitHub
parent b567f66fab
commit a493b6270d
4 changed files with 12 additions and 169 deletions

View File

@ -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};

View File

@ -922,43 +922,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 {
@ -2473,8 +2436,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));
@ -2659,13 +2621,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
@ -2682,13 +2638,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
@ -2755,7 +2705,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,
@ -2793,22 +2743,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(_)));
let deserialized_genesis: Genesis = serde_json::from_str(&serialized_genesis).unwrap();
// build chain
let chain_spec = ChainSpecBuilder::default()
.chain(2600.into())
.genesis(genesis)
.cancun_activated()
.build();
// 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]

View File

@ -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
@ -98,7 +99,6 @@ procfs = "0.16.0"
[dev-dependencies]
# test vectors generation
proptest.workspace = true
alloy-genesis.workspace = true
[features]
optimism = [

View File

@ -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::{
@ -76,7 +77,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())
}
@ -139,12 +140,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() {
@ -153,97 +150,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"] {