Skip to content

Commit

Permalink
Implement genesis for the Evm module
Browse files Browse the repository at this point in the history
  • Loading branch information
bkolad committed Jul 6, 2023
1 parent 1fff0a3 commit a6e39b6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
2 changes: 1 addition & 1 deletion module-system/module-implementations/sov-evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ tempfile = { workspace = true }
bytes = { workspace = true }

[features]
default = ["native"]
default = ["native", "experimental"]
serde = ["dep:serde", "dep:serde_json"]
native = ["serde", "sov-state/native", "dep:jsonrpsee", "sov-modules-api/native"]
experimental = []
28 changes: 28 additions & 0 deletions module-system/module-implementations/sov-evm/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
use anyhow::Result;
use sov_state::WorkingSet;

use crate::evm::db_init::InitEvmDb;
use crate::evm::AccountInfo;
use crate::Evm;

impl<C: sov_modules_api::Context> Evm<C> {
pub(crate) fn init_module(
&self,
config: &<Self as sov_modules_api::Module>::Config,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<()> {
let mut evm_db = self.get_db(working_set);

for acc in &config.data {
evm_db.insert_account_info(
acc.address,
AccountInfo {
balance: acc.balance,
code_hash: acc.code_hash,
code: acc.code.clone(),
nonce: acc.nonce,
},
)
}
Ok(())
}
}
25 changes: 20 additions & 5 deletions module-system/module-implementations/sov-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod query;
#[cfg(test)]
mod tests;
#[cfg(feature = "experimental")]
pub use experimental::Evm;
pub use experimental::{AccountData, Evm, EvmConfig};

#[cfg(feature = "experimental")]
mod experimental {
Expand All @@ -22,6 +22,21 @@ mod experimental {
use super::evm::db::EvmDb;
use super::evm::transaction::BlockEnv;
use super::evm::{DbAccount, EthAddress};
use crate::evm::Bytes32;

#[derive(Clone)]
pub struct AccountData {
pub address: EthAddress,
pub balance: Bytes32,
pub code_hash: Bytes32,
pub code: Vec<u8>,
pub nonce: u64,
}

#[derive(Clone)]
pub struct EvmConfig {
pub data: Vec<AccountData>,
}

#[allow(dead_code)]
#[derive(ModuleInfo, Clone)]
Expand All @@ -39,16 +54,16 @@ mod experimental {
impl<C: sov_modules_api::Context> sov_modules_api::Module for Evm<C> {
type Context = C;

type Config = ();
type Config = EvmConfig;

type CallMessage = super::call::CallMessage;

fn genesis(
&self,
_config: &Self::Config,
_working_set: &mut WorkingSet<C::Storage>,
config: &Self::Config,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<(), Error> {
todo!()
Ok(self.init_module(config, working_set)?)
}

fn call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use sov_modules_api::{Context, Module, PublicKey, Spec};
use sov_state::{ProverStorage, WorkingSet};

use crate::call::CallMessage;
use crate::evm::db_init::InitEvmDb;
use crate::evm::test_helpers::SimpleStorageContract;
use crate::evm::transaction::EvmTransaction;
use crate::evm::{AccountInfo, EthAddress};
use crate::Evm;
use crate::evm::EthAddress;
use crate::{AccountData, Evm, EvmConfig};

type C = DefaultContext;

Expand Down Expand Up @@ -56,17 +55,18 @@ fn evm_test() {
let caller = [0; 20];

let evm = Evm::<C>::default();
let mut evm_db = evm.get_db(working_set);

evm_db.insert_account_info(
caller,
AccountInfo {
balance: U256::from(1000000000).to_le_bytes(),
code_hash: KECCAK_EMPTY.to_fixed_bytes(),
code: vec![],
nonce: 0,
},
);

let data = AccountData {
address: caller,
balance: U256::from(1000000000).to_le_bytes(),
code_hash: KECCAK_EMPTY.to_fixed_bytes(),
code: vec![],
nonce: 0,
};

let config = EvmConfig { data: vec![data] };

evm.genesis(&config, working_set).unwrap();

let contract_addr = hex::decode("bd770416a3345f91e4b34576cb804a576fa48eb1")
.unwrap()
Expand Down

0 comments on commit a6e39b6

Please sign in to comment.