Skip to content

Commit

Permalink
Read evm genesis from a file (Sovereign-Labs#976)
Browse files Browse the repository at this point in the history
* Evm config

* fix lint

* cleanup

* fix lint
  • Loading branch information
bkolad authored Oct 3, 2023
1 parent baebd03 commit 7c31500
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 29 deletions.
51 changes: 24 additions & 27 deletions examples/demo-stf/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ use std::convert::AsRef;
use std::path::Path;

use anyhow::Context as AnyhowContext;
#[cfg(feature = "experimental")]
use reth_primitives::Bytes;
use serde::de::DeserializeOwned;
use sov_accounts::AccountConfig;
use sov_bank::BankConfig;
use sov_chain_state::ChainStateConfig;
use sov_cli::wallet_state::PrivateKeyAndAddress;
#[cfg(feature = "experimental")]
use sov_evm::{AccountData, EvmConfig, SpecId};
use sov_evm::EvmConfig;
pub use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::Context;
use sov_nft_module::NonFungibleTokenConfig;
Expand All @@ -36,23 +34,23 @@ pub const DEMO_TOKEN_NAME: &str = "sov-demo-token";
/// ```
pub fn get_genesis_config<C: Context, Da: DaSpec>(
sequencer_da_address: Da::Address,
#[cfg(feature = "experimental")] evm_genesis_addresses: Vec<reth_primitives::Address>,
#[cfg(feature = "experimental")] eth_signers: Vec<reth_primitives::Address>,
) -> GenesisConfig<C, Da> {
let token_deployer: PrivateKeyAndAddress<C> = read_private_key();

create_genesis_config(
token_deployer.address.clone(),
sequencer_da_address,
#[cfg(feature = "experimental")]
evm_genesis_addresses,
eth_signers,
)
.expect("Unable to read genesis configuration")
}

fn create_genesis_config<C: Context, Da: DaSpec>(
sequencer_address: C::Address,
sequencer_da_address: Da::Address,
#[cfg(feature = "experimental")] evm_genesis_addresses: Vec<reth_primitives::Address>,
#[cfg(feature = "experimental")] eth_signers: Vec<reth_primitives::Address>,
) -> anyhow::Result<GenesisConfig<C, Da>> {
// This path will be injected as a parameter: #872
let bank_genesis_path = "../test-data/genesis/bank.json";
Expand Down Expand Up @@ -86,6 +84,12 @@ fn create_genesis_config<C: Context, Da: DaSpec>(
let chain_state_path = "../test-data/genesis/chain_state.json";
let chain_state_config: ChainStateConfig = read_json_file(chain_state_path)?;

#[cfg(feature = "experimental")]
let evm_path = "../test-data/genesis/evm.json";

#[cfg(feature = "experimental")]
let evm_config = get_evm_config(evm_path, eth_signers)?;

Ok(GenesisConfig::new(
bank_config,
sequencer_registry_config,
Expand All @@ -94,7 +98,7 @@ fn create_genesis_config<C: Context, Da: DaSpec>(
value_setter_config,
accounts_config,
#[cfg(feature = "experimental")]
get_evm_config(evm_genesis_addresses),
evm_config,
nft_config,
))
}
Expand All @@ -110,28 +114,21 @@ fn read_json_file<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> anyhow::Resul
Ok(config)
}

// TODO: #840
#[cfg(feature = "experimental")]
fn get_evm_config(genesis_addresses: Vec<reth_primitives::Address>) -> EvmConfig {
let data = genesis_addresses
.into_iter()
.map(|address| AccountData {
address,
balance: AccountData::balance(u64::MAX),
code_hash: AccountData::empty_code(),
code: Bytes::default(),
nonce: 0,
})
.collect();

EvmConfig {
data,
chain_id: 1,
limit_contract_code_size: None,
spec: vec![(0, SpecId::SHANGHAI)].into_iter().collect(),
block_timestamp_delta: 1u64,
..Default::default()
fn get_evm_config<P: AsRef<Path>>(
evm_path: P,
signers: Vec<reth_primitives::Address>,
) -> anyhow::Result<EvmConfig> {
let config: EvmConfig = read_json_file(evm_path)?;
let addresses: std::collections::HashSet<reth_primitives::Address> =
config.data.iter().map(|acc| acc.address).collect();

// check if all the eth signer are in genesis.
for signer in signers {
assert!(addresses.contains(&signer));
}

Ok(config)
}

pub fn read_private_key<C: Context>() -> PrivateKeyAndAddress<C> {
Expand Down
24 changes: 24 additions & 0 deletions examples/test-data/genesis/evm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"data":[
{
"address":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
"balance":"0xffffffffffffffff",
"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
"code":"0x",
"nonce":0
}],
"chain_id":1,
"limit_contract_code_size":null,
"spec":{
"0":"SHANGHAI"
},
"coinbase":"0x0000000000000000000000000000000000000000",
"starting_base_fee":7,
"block_gas_limit":30000000,
"genesis_timestamp":0,
"block_timestamp_delta":1,
"base_fee_params":{
"max_change_denominator":8,
"elasticity_multiplier":2
}
}
4 changes: 2 additions & 2 deletions module-system/module-implementations/sov-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod experimental {
pub(crate) const MIN_CREATE_GAS: u64 = 53_000u64;

/// Evm account.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
pub struct AccountData {
/// Account address.
pub address: Address,
Expand All @@ -79,7 +79,7 @@ mod experimental {
}

/// Genesis configuration.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
pub struct EvmConfig {
/// Genesis accounts.
pub data: Vec<AccountData>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::str::FromStr;

use reth_primitives::{Address, Bytes};
use revm::primitives::SpecId;

use crate::{AccountData, EvmConfig};

#[test]
fn test_config_serialization() {
let address = Address::from_str("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266").unwrap();
let config = EvmConfig {
data: vec![AccountData {
address,
balance: AccountData::balance(u64::MAX),
code_hash: AccountData::empty_code(),
code: Bytes::default(),
nonce: 0,
}],
chain_id: 1,
limit_contract_code_size: None,
spec: vec![(0, SpecId::SHANGHAI)].into_iter().collect(),
block_timestamp_delta: 1u64,
..Default::default()
};

let data = r#"
{
"data":[
{
"address":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
"balance":"0xffffffffffffffff",
"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
"code":"0x",
"nonce":0
}],
"chain_id":1,
"limit_contract_code_size":null,
"spec":{
"0":"SHANGHAI"
},
"coinbase":"0x0000000000000000000000000000000000000000",
"starting_base_fee":7,
"block_gas_limit":30000000,
"genesis_timestamp":0,
"block_timestamp_delta":1,
"base_fee_params":{
"max_change_denominator":8,
"elasticity_multiplier":2
}
}"#;

let parsed_config: EvmConfig = serde_json::from_str(data).unwrap();
assert_eq!(config, parsed_config)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod call_tests;
mod cfg_tests;
mod config_tests;
pub(crate) mod dev_signer;
mod genesis_tests;
mod hooks_tests;
Expand Down

0 comments on commit 7c31500

Please sign in to comment.