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

Evm/code stored seperately #889

Merged
merged 17 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 16 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: 3 additions & 1 deletion examples/demo-stf/src/genesis_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "experimental")]
use reth_primitives::Bytes;
use sov_chain_state::ChainStateConfig;
#[cfg(feature = "experimental")]
use sov_evm::{AccountData, EvmConfig, SpecId};
Expand Down Expand Up @@ -81,7 +83,7 @@ fn get_evm_config(genesis_addresses: Vec<reth_primitives::Address>) -> EvmConfig
address,
balance: AccountData::balance(u64::MAX),
code_hash: AccountData::empty_code(),
code: vec![],
code: Bytes::default(),
nonce: 0,
})
.collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use reth_primitives::{
};
use reth_rpc_types::CallRequest;
use revm::primitives::{
AccountInfo as ReVmAccountInfo, BlockEnv as ReVmBlockEnv, Bytecode, CreateScheme, TransactTo,
TxEnv, U256,
AccountInfo as ReVmAccountInfo, BlockEnv as ReVmBlockEnv, CreateScheme, TransactTo, TxEnv, U256,
};

use super::primitive_types::{
Expand All @@ -19,7 +18,7 @@ impl From<AccountInfo> for ReVmAccountInfo {
Self {
nonce: info.nonce,
balance: info.balance,
code: Some(Bytecode::new_raw(Bytes::from(info.code))),
code: None,
code_hash: info.code_hash,
}
}
Expand All @@ -30,7 +29,6 @@ impl From<ReVmAccountInfo> for AccountInfo {
Self {
balance: info.balance,
code_hash: info.code_hash,
code: info.code.unwrap_or_default().bytes().to_vec(),
nonce: info.nonce,
}
}
Expand Down
17 changes: 14 additions & 3 deletions module-system/module-implementations/sov-evm/src/evm/db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::convert::Infallible;

use reth_primitives::Address;
use reth_primitives::{Address, Bytes, H256};
use revm::primitives::{AccountInfo as ReVmAccountInfo, Bytecode, B160, B256, U256};
use revm::Database;
use sov_modules_api::WorkingSet;
Expand All @@ -10,16 +10,19 @@ use super::DbAccount;

pub(crate) struct EvmDb<'a, C: sov_modules_api::Context> {
pub(crate) accounts: sov_modules_api::StateMap<Address, DbAccount, BcsCodec>,
pub(crate) code: sov_modules_api::StateMap<H256, Bytes, BcsCodec>,
pub(crate) working_set: &'a mut WorkingSet<C>,
}

impl<'a, C: sov_modules_api::Context> EvmDb<'a, C> {
pub(crate) fn new(
accounts: sov_modules_api::StateMap<Address, DbAccount, BcsCodec>,
code: sov_modules_api::StateMap<H256, Bytes, BcsCodec>,
working_set: &'a mut WorkingSet<C>,
) -> Self {
Self {
accounts,
code,
working_set,
}
}
Expand All @@ -33,8 +36,16 @@ impl<'a, C: sov_modules_api::Context> Database for EvmDb<'a, C> {
Ok(db_account.map(|acc| acc.info.into()))
}

fn code_by_hash(&mut self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
panic!("Should not be called. Contract code is already loaded");
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
// TODO move to new_raw_with_hash for better performance
let bytecode = Bytecode::new_raw(
self.code
.get(&code_hash, self.working_set)
.unwrap_or(Bytes::default())
.into(),
);

Ok(bytecode)
}

fn storage(&mut self, address: B160, index: U256) -> Result<U256, Self::Error> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ impl<'a, C: sov_modules_api::Context> DatabaseCommit for EvmDb<'a, C> {
.get(&address, self.working_set)
.unwrap_or_else(|| DbAccount::new(accounts_prefix, address));

db_account.info = account.info.into();
let account_info = account.info;

if let Some(ref code) = account_info.code {
if !code.is_empty() {
// TODO: would be good to have a contains_key method on the StateMap that would be optimized, so we can check the hash before storing the code
self.code.set(
&account_info.code_hash,
&code.bytecode.as_ref().into(),
self.working_set,
);
}
}

db_account.info = account_info.into();

for (key, value) in account.storage.into_iter() {
let value = value.present_value();
Expand Down
13 changes: 12 additions & 1 deletion module-system/module-implementations/sov-evm/src/evm/db_init.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use reth_primitives::Bytes;
#[cfg(test)]
use revm::db::{CacheDB, EmptyDB};
use revm::primitives::Address;
use revm::primitives::{Address, B256};

use super::db::EvmDb;
use super::{AccountInfo, DbAccount};

/// Initializes database with a predefined account.
pub(crate) trait InitEvmDb {
fn insert_account_info(&mut self, address: Address, acc: AccountInfo);
fn insert_code(&mut self, code_hash: B256, code: Bytes);
}

impl<'a, C: sov_modules_api::Context> InitEvmDb for EvmDb<'a, C> {
Expand All @@ -17,11 +19,20 @@ impl<'a, C: sov_modules_api::Context> InitEvmDb for EvmDb<'a, C> {

self.accounts.set(&sender, &db_account, self.working_set);
}

fn insert_code(&mut self, code_hash: B256, code: Bytes) {
self.code.set(&code_hash, &code, self.working_set)
}
}

#[cfg(test)]
impl InitEvmDb for CacheDB<EmptyDB> {
fn insert_account_info(&mut self, sender: Address, acc: AccountInfo) {
self.insert_account_info(sender, acc.into());
}

fn insert_code(&mut self, code_hash: B256, code: Bytes) {
self.contracts
.insert(code_hash, revm::primitives::Bytecode::new_raw(code.into()));
}
}
3 changes: 0 additions & 3 deletions module-system/module-implementations/sov-evm/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ use sov_state::codec::BcsCodec;
pub(crate) struct AccountInfo {
pub(crate) balance: U256,
pub(crate) code_hash: H256,
// TODO: `code` can be a huge chunk of data. We can use `StateValue` and lazy load it only when needed.
// https://github.com/Sovereign-Labs/sovereign-sdk/issues/425
pub(crate) code: Vec<u8>,
pub(crate) nonce: u64,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ fn simple_contract_execution<DB: Database<Error = Infallible> + DatabaseCommit +
AccountInfo {
balance: U256::from(1000000000),
code_hash: KECCAK_EMPTY,
code: vec![],
nonce: 1,
},
);
Expand Down
7 changes: 5 additions & 2 deletions module-system/module-implementations/sov-evm/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ impl<C: sov_modules_api::Context> Evm<C> {
AccountInfo {
balance: acc.balance,
code_hash: acc.code_hash,
code: acc.code.clone(),
nonce: acc.nonce,
},
)
);

if acc.code.len() > 0 {
evm_db.insert_code(acc.code_hash, acc.code.clone());
}
}

let mut spec = config
Expand Down
17 changes: 7 additions & 10 deletions module-system/module-implementations/sov-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub use revm::primitives::SpecId;
mod experimental {
use std::collections::HashMap;

use reth_primitives::{Address, H256};
use reth_primitives::{Address, Bytes, H256};
use revm::primitives::{SpecId, KECCAK_EMPTY, U256};
use sov_modules_api::{Error, ModuleInfo, WorkingSet};
use sov_state::codec::BcsCodec;
Expand All @@ -43,7 +43,7 @@ mod experimental {
pub address: Address,
pub balance: U256,
pub code_hash: H256,
pub code: Vec<u8>,
pub code: Bytes,
pub nonce: u64,
}

Expand Down Expand Up @@ -102,6 +102,10 @@ mod experimental {
#[state]
pub(crate) accounts: sov_modules_api::StateMap<Address, DbAccount, BcsCodec>,

#[state]
pub(crate) code:
sov_modules_api::StateMap<reth_primitives::H256, reth_primitives::Bytes, BcsCodec>,

#[state]
pub(crate) cfg: sov_modules_api::StateValue<EvmChainConfig, BcsCodec>,

Expand Down Expand Up @@ -134,13 +138,6 @@ mod experimental {

#[state]
pub(crate) receipts: sov_modules_api::AccessoryStateVec<Receipt, BcsCodec>,

#[state]
pub(crate) code: sov_modules_api::AccessoryStateMap<
reth_primitives::H256,
reth_primitives::Bytes,
BcsCodec,
>,
}

impl<C: sov_modules_api::Context> sov_modules_api::Module for Evm<C> {
Expand Down Expand Up @@ -170,7 +167,7 @@ mod experimental {

impl<C: sov_modules_api::Context> Evm<C> {
pub(crate) fn get_db<'a>(&self, working_set: &'a mut WorkingSet<C>) -> EvmDb<'a, C> {
EvmDb::new(self.accounts.clone(), working_set)
EvmDb::new(self.accounts.clone(), self.code.clone(), working_set)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use reth_primitives::{Address, TransactionKind};
use reth_primitives::{Address, Bytes, TransactionKind};
use revm::primitives::{SpecId, KECCAK_EMPTY, U256};
use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
Expand Down Expand Up @@ -59,7 +59,7 @@ fn evm_test() {
address: dev_signer.address(),
balance: U256::from(1000000000),
code_hash: KECCAK_EMPTY,
code: vec![],
code: Bytes::default(),
nonce: 0,
}],
spec: vec![(0, SpecId::LATEST)].into_iter().collect(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ lazy_static! {
address: Address::from([1u8; 20]),
balance: U256::from(1000000000),
code_hash: KECCAK_EMPTY,
code: vec![],
code: Bytes::default(),
nonce: 0,
}],
spec: vec![(0, SpecId::BERLIN), (1, SpecId::LATEST)]
Expand Down Expand Up @@ -71,7 +71,6 @@ fn genesis_data() {
AccountInfo {
balance: account.balance,
code_hash: account.code_hash,
code: account.code.clone(),
nonce: account.nonce,
}
),
Expand Down