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

refactor: allow chain-specific configuration of Evm #1378

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b9f75a0
refactor: allow chain-specific configuration of Evm
Wodann May 1, 2024
9f4abf7
refactor: rename Transaction::transact_to and clarify docs
Wodann Jun 18, 2024
ae14441
refactor: remove trait bounds on Transaction super trait
Wodann Jun 18, 2024
13aabf4
refactor: remove trait bounds from Block supertrait
Wodann Jun 18, 2024
2a84559
fix: clippy warnings
Wodann Jun 19, 2024
1c17cd0
fix: cargo doc
Wodann Jun 19, 2024
904f0f5
refactor: limit trait bounds on HaltReason
Wodann Jun 19, 2024
8a1530c
refactor: allow moving of kind
Wodann Jun 19, 2024
29f3be6
refactor: rename Transaction::nonce to nonce_opt to signal that it's …
Wodann Jun 19, 2024
9f5e2d3
refactor: replace AccessList with alloy version
Wodann Jun 19, 2024
3baf771
refactor: rename gas_priority_fee to max_priority_fee_per_gas
Wodann Jun 20, 2024
4710a4b
refactor: correct trait bound on ExecutionResult::clone
Wodann Jun 20, 2024
5bf0156
Clone
Wodann Jun 20, 2024
612bb40
refactor: only allow optional nonce check via CfgEnv
Wodann Jun 20, 2024
94d529b
fix: use alloy-eips with fix for serde
Wodann Jun 21, 2024
e68f3af
build: bump to alloy-eips v0.1.2
Wodann Jun 24, 2024
da97359
build: Cargo.lock file
Wodann Jun 24, 2024
c903557
fix: revme
Wodann Jun 24, 2024
23525aa
refactor: derive DummyHost
Wodann Jun 24, 2024
729b236
refactor: derive Clone for ExecutionResult
Wodann Jun 24, 2024
a35aaa6
refactor: remove EVMErrorForChain
Wodann Jun 24, 2024
11a4fe6
refactor: remove EVMErrorForChain
Wodann Jun 24, 2024
b3b9b71
refactor: derive Clone for CfgEnvWithChainSpec
Wodann Jun 24, 2024
1c908e1
refactor: use EVMResultGeneric
Wodann Jun 24, 2024
a4e49cc
refactor: use EVMResultGeneric
Wodann Jun 24, 2024
8e3e3be
refactor: use EVMResultGeneric
Wodann Jun 24, 2024
adf87c4
refactor: use EVMResultGeneric
Wodann Jun 24, 2024
4761941
refactor: use EVMResultGeneric
Wodann Jun 24, 2024
78fe3f2
refactor: use EVMResultGeneric
Wodann Jun 24, 2024
e65e829
refactor: add convenience EVMErrorForChain type alias
Wodann Jun 24, 2024
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
16 changes: 15 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 12 additions & 8 deletions bins/revme/src/cmd/evmrunner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use revm::{
db::BenchmarkDB,
inspector_handle_register,
inspectors::TracerEip3155,
primitives::{Address, Bytecode, TxKind},
Evm,
primitives::{address, Address, Bytecode, TxKind},
Database, Evm,
};
use std::io::Error as IoError;
use std::path::PathBuf;
Expand Down Expand Up @@ -61,6 +61,8 @@ pub struct Cmd {
impl Cmd {
/// Run statetest command.
pub fn run(&self) -> Result<(), Errors> {
const CALLER: Address = address!("0000000000000000000000000000000000000001");

let bytecode_str: Cow<'_, str> = if let Some(path) = &self.path {
// check if path exists.
if !path.exists() {
Expand All @@ -75,19 +77,21 @@ impl Cmd {
let input = hex::decode(self.input.trim())
.map_err(|_| Errors::InvalidInput)?
.into();

let mut db = BenchmarkDB::new_bytecode(Bytecode::new_raw(bytecode.into()));

let nonce = db.basic(CALLER).unwrap().map_or(0, |account| account.nonce);

// BenchmarkDB is dummy state that implements Database trait.
// the bytecode is deployed at zero address.
let mut evm = Evm::builder()
.with_db(BenchmarkDB::new_bytecode(Bytecode::new_raw(
bytecode.into(),
)))
.with_db(db)
.modify_tx_env(|tx| {
// execution globals block hash/gas_limit/coinbase/timestamp..
tx.caller = "0x0000000000000000000000000000000000000001"
.parse()
.unwrap();
tx.caller = CALLER;
tx.transact_to = TxKind::Call(Address::ZERO);
tx.data = input;
tx.nonce = nonce;
})
.build();

Expand Down
25 changes: 13 additions & 12 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ use std::{
use thiserror::Error;
use walkdir::{DirEntry, WalkDir};

#[cfg(feature = "optimism")]
type TestChainSpec = revm::optimism::OptimismChainSpec;
#[cfg(not(feature = "optimism"))]
type TestChainSpec = revm::primitives::EthChainSpec;

#[derive(Debug, Error)]
#[error("Test {name} failed: {kind}")]
pub struct TestError {
Expand Down Expand Up @@ -130,8 +135,8 @@ fn check_evm_execution<EXT>(
test: &Test,
expected_output: Option<&Bytes>,
test_name: &str,
exec_result: &EVMResultGeneric<ExecutionResult, Infallible>,
evm: &Evm<'_, EXT, &mut State<EmptyDB>>,
exec_result: &EVMResultGeneric<ExecutionResult<TestChainSpec>, TestChainSpec, Infallible>,
evm: &Evm<'_, TestChainSpec, EXT, &mut State<EmptyDB>>,
print_json_outcome: bool,
) -> Result<(), TestError> {
let logs_root = log_rlp_hash(exec_result.as_ref().map(|r| r.logs()).unwrap_or_default());
Expand All @@ -155,7 +160,7 @@ fn check_evm_execution<EXT>(
Err(e) => e.to_string(),
},
"postLogsHash": logs_root,
"fork": evm.handler.cfg().spec_id,
"fork": evm.handler.spec_id(),
"test": test_name,
"d": test.indexes.data,
"g": test.indexes.gas,
Expand Down Expand Up @@ -273,7 +278,7 @@ pub fn execute_test_suite(
cache_state.insert_account_with_storage(address, acc_info, info.storage);
}

let mut env = Box::<Env>::default();
let mut env = Box::<Env<TestChainSpec>>::default();
// for mainnet
env.cfg.chain_id = 1;
// env.cfg.spec_id is set down the road
Expand Down Expand Up @@ -343,6 +348,8 @@ pub fn execute_test_suite(
.get(test.indexes.data)
.unwrap()
.clone();

env.tx.nonce = u64::try_from(unit.transaction.nonce).unwrap();
env.tx.value = unit.transaction.value[test.indexes.value];

env.tx.access_list = unit
Expand All @@ -360,10 +367,7 @@ pub fn execute_test_suite(
env.tx.transact_to = to;

let mut cache = cache_state.clone();
cache.set_state_clear_flag(SpecId::enabled(
spec_id,
revm::primitives::SpecId::SPURIOUS_DRAGON,
));
cache.set_state_clear_flag(SpecId::enabled(spec_id, SpecId::SPURIOUS_DRAGON));
let mut state = revm::db::State::builder()
.with_cached_prestate(cache)
.with_bundle_update()
Expand Down Expand Up @@ -429,10 +433,7 @@ pub fn execute_test_suite(

// re build to run with tracing
let mut cache = cache_state.clone();
cache.set_state_clear_flag(SpecId::enabled(
spec_id,
revm::primitives::SpecId::SPURIOUS_DRAGON,
));
cache.set_state_clear_flag(SpecId::enabled(spec_id, SpecId::SPURIOUS_DRAGON));
let state = revm::db::State::builder()
.with_cached_prestate(cache)
.with_bundle_update()
Expand Down
11 changes: 3 additions & 8 deletions crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ all = "warn"
[dependencies]
revm-primitives = { path = "../primitives", version = "5.0.0", default-features = false }

derive-where = { version = "1.2.7", default-features = false }
paste = { version = "1.0", optional = true }
phf = { version = "0.11", default-features = false, optional = true, features = [
"macros",
Expand Down Expand Up @@ -56,14 +57,6 @@ portable = ["revm-primitives/portable"]
parse = ["dep:paste", "dep:phf"]

optimism = ["revm-primitives/optimism"]
# Optimism default handler enabled Optimism handler register by default in EvmBuilder.
optimism-default-handler = [
"optimism",
"revm-primitives/optimism-default-handler",
]
negate-optimism-default-handler = [
"revm-primitives/negate-optimism-default-handler",
]

dev = [
"memory_limit",
Expand All @@ -73,6 +66,7 @@ dev = [
"optional_gas_refund",
"optional_no_base_fee",
"optional_beneficiary_reward",
"optional_nonce_check",
]
memory_limit = ["revm-primitives/memory_limit"]
optional_balance_check = ["revm-primitives/optional_balance_check"]
Expand All @@ -81,3 +75,4 @@ optional_eip3607 = ["revm-primitives/optional_eip3607"]
optional_gas_refund = ["revm-primitives/optional_gas_refund"]
optional_no_base_fee = ["revm-primitives/optional_no_base_fee"]
optional_beneficiary_reward = ["revm-primitives/optional_beneficiary_reward"]
optional_nonce_check = ["revm-primitives/optional_nonce_check"]
4 changes: 1 addition & 3 deletions crates/interpreter/src/gas/calc.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use revm_primitives::AccessListItem;

use super::constants::*;
use crate::{
num_words,
primitives::{SpecId, U256},
primitives::{AccessListItem, SpecId, U256},
SelfDestructResult,
};

Expand Down
14 changes: 10 additions & 4 deletions crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ use crate::primitives::{Address, Bytes, Env, Log, B256, U256};

mod dummy;
pub use dummy::DummyHost;
use revm_primitives::ChainSpec;

/// EVM context host.
pub trait Host {
/// Chain specification.
type ChainSpecT: ChainSpec;

/// Returns a reference to the environment.
fn env(&self) -> &Env;
fn env(&self) -> &Env<Self::ChainSpecT>;

/// Returns a mutable reference to the environment.
fn env_mut(&mut self) -> &mut Env;
fn env_mut(&mut self) -> &mut Env<Self::ChainSpecT>;

/// Load an account.
///
Expand Down Expand Up @@ -84,13 +88,15 @@ pub struct SelfDestructResult {

#[cfg(test)]
mod tests {
use revm_primitives::EthChainSpec;

use super::*;

fn assert_host<H: Host + ?Sized>() {}

#[test]
fn object_safety() {
assert_host::<DummyHost>();
assert_host::<dyn Host>();
assert_host::<DummyHost<EthChainSpec>>();
assert_host::<dyn Host<ChainSpecT = EthChainSpec>>();
}
}
37 changes: 27 additions & 10 deletions crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
use derive_where::derive_where;

use crate::{
primitives::{hash_map::Entry, Address, Bytes, Env, HashMap, Log, B256, KECCAK_EMPTY, U256},
primitives::{
hash_map::Entry, Address, Bytes, ChainSpec, Env, HashMap, Log, B256, KECCAK_EMPTY, U256,
},
Host, SStoreResult, SelfDestructResult,
};
use std::vec::Vec;

use super::LoadAccountResult;

/// A dummy [Host] implementation.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DummyHost {
pub env: Env,
#[derive_where(Clone, Debug, Default; ChainSpecT::Block, ChainSpecT::Transaction)]
pub struct DummyHost<ChainSpecT>
where
ChainSpecT: ChainSpec,
{
pub env: Env<ChainSpecT>,
pub storage: HashMap<U256, U256>,
pub transient_storage: HashMap<U256, U256>,
pub log: Vec<Log>,
}

impl DummyHost {
impl<ChainSpecT> DummyHost<ChainSpecT>
where
ChainSpecT: ChainSpec,
{
/// Create a new dummy host with the given [`Env`].
#[inline]
pub fn new(env: Env) -> Self {
pub fn new(env: Env<ChainSpecT>) -> Self {
Self {
env,
..Default::default()
storage: HashMap::new(),
transient_storage: HashMap::new(),
log: Vec::new(),
}
}

Expand All @@ -33,14 +45,19 @@ impl DummyHost {
}
}

impl Host for DummyHost {
impl<ChainSpecT> Host for DummyHost<ChainSpecT>
where
ChainSpecT: ChainSpec,
{
type ChainSpecT = ChainSpecT;

#[inline]
fn env(&self) -> &Env {
fn env(&self) -> &Env<ChainSpecT> {
&self.env
}

#[inline]
fn env_mut(&mut self) -> &mut Env {
fn env_mut(&mut self) -> &mut Env<ChainSpecT> {
&mut self.env
}

Expand Down
Loading
Loading