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

feat: introducing EvmWiring, a chain-specific configuration #1672

Merged
merged 64 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
f8fcaa7
refactor: allow chain-specific configuration of Evm
Wodann May 1, 2024
6145d39
refactor: rename Transaction::transact_to and clarify docs
Wodann Jun 18, 2024
7b950c7
refactor: remove trait bounds on Transaction super trait
Wodann Jun 18, 2024
caaf05d
refactor: remove trait bounds from Block supertrait
Wodann Jun 18, 2024
72272c9
fix: clippy warnings
Wodann Jun 19, 2024
0723ff0
fix: cargo doc
Wodann Jun 19, 2024
f5ccf54
refactor: limit trait bounds on HaltReason
Wodann Jun 19, 2024
6a881fa
refactor: allow moving of kind
Wodann Jun 19, 2024
dab9e20
refactor: rename Transaction::nonce to nonce_opt to signal that it's …
Wodann Jun 19, 2024
214c1d5
refactor: replace AccessList with alloy version
Wodann Jun 19, 2024
7303587
refactor: rename gas_priority_fee to max_priority_fee_per_gas
Wodann Jun 20, 2024
a8227c0
refactor: correct trait bound on ExecutionResult::clone
Wodann Jun 20, 2024
fe1ab43
Clone
Wodann Jun 20, 2024
373e197
refactor: only allow optional nonce check via CfgEnv
Wodann Jun 20, 2024
689be7f
fix: revme
Wodann Jun 24, 2024
f53a2a9
refactor: derive DummyHost
Wodann Jun 24, 2024
dbe9e3a
refactor: derive Clone for ExecutionResult
Wodann Jun 24, 2024
77e19ce
refactor: remove EVMErrorForChain
Wodann Jun 24, 2024
6c16bef
refactor: derive Clone for CfgEnvWithChainSpec
Wodann Jun 24, 2024
145a4ce
refactor: use EVMResultGeneric
Wodann Jun 24, 2024
3f29a23
refactor: add convenience EVMErrorForChain type alias
Wodann Jun 24, 2024
3c6c4d3
feat: export OptimismBlock
Wodann Jul 8, 2024
4856bea
refactor: add handler constructor and Context to revm::ChainSpec
Wodann Jul 22, 2024
93089c3
refactor: generalise optimism implementation using traits
Wodann Jul 22, 2024
da8175a
fix: no-default-features
Wodann Jul 23, 2024
0f052b5
fix: CI
Wodann Jul 29, 2024
69e7304
chore: Add default fn to Tx/Block traits
rakita Jul 31, 2024
946d404
Chore: rename ChainSpec to EvmWiring
rakita Jul 31, 2024
57a3feb
chore: clippy comments fix
rakita Jul 31, 2024
b6e90b6
chore: rename EthEvmWiring to EthereumWiring
rakita Jul 31, 2024
69890ae
chore: re add serde, restring HaltReasonTrait
rakita Jul 31, 2024
9a6d2e6
chore: move custom opcode to examples
rakita Jul 31, 2024
da81d97
chore: remove op feature from test wiring
rakita Jul 31, 2024
1ad97fd
nit use Self::EvmWiringT
rakita Jul 31, 2024
6d479da
nit indents
rakita Jul 31, 2024
1bce037
feat(Wiring): Add Database and EXT to EvmWiring
rakita Aug 1, 2024
1c660da
some fixes
rakita Aug 1, 2024
ed99ce9
temp
rakita Aug 5, 2024
45c07c5
feat: make builder compile. EnvWiring and Result Halt
rakita Aug 5, 2024
d73f96c
chore: cleanup rename
rakita Aug 6, 2024
763ba26
nit
rakita Aug 6, 2024
b96b397
fix: make string conversion complete
Wodann Aug 28, 2024
62b45b7
Merge remote-tracking branch 'origin/main' into wiring
rakita Sep 2, 2024
65d4f26
fix compile
rakita Sep 2, 2024
83a0803
Merge remote-tracking branch 'wodann/refactor/opt-spec-id' into wiring
rakita Sep 2, 2024
917a753
compiles
rakita Sep 4, 2024
28ac623
Merge remote-tracking branch 'origin/main' into wiring
rakita Sep 4, 2024
897f8ed
wip builder
rakita Sep 4, 2024
6528d3b
wip
rakita Sep 4, 2024
321972c
Merge remote-tracking branch 'origin/main' into wiring
rakita Sep 5, 2024
0462c5b
fix compile
rakita Sep 5, 2024
c1c57ab
wip
rakita Sep 5, 2024
c4845c2
fix optimism test
rakita Sep 5, 2024
de4e89c
fix docs ci
rakita Sep 5, 2024
492c27a
cleanup
rakita Sep 6, 2024
f1faa47
cleanup
rakita Sep 6, 2024
f97ce47
use core::error::Error
rakita Sep 6, 2024
d64f624
cleanup
rakita Sep 6, 2024
4a7e6e3
use core error
rakita Sep 6, 2024
d51cc59
fix builer
rakita Sep 6, 2024
e7d78ca
fix docs
rakita Sep 6, 2024
763592a
final doc fix
rakita Sep 6, 2024
5fef8e2
rm alloy provider
rakita Sep 6, 2024
1383df7
Merge remote-tracking branch 'origin/main' into wiring
rakita Sep 9, 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::{eof::EofDecodeError, Address, Bytecode, TxKind},
Evm,
primitives::{address, eof::EofDecodeError, Address, Bytecode, TxKind},
Database, Evm,
};
use std::io::Error as IoError;
use std::path::PathBuf;
Expand Down Expand Up @@ -57,6 +57,8 @@ pub struct Cmd {
impl Cmd {
/// Run evm runner 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 @@ -71,19 +73,21 @@ impl Cmd {
let input = hex::decode(self.input.trim())
.map_err(|_| Errors::InvalidInput)?
.into();

let mut db = BenchmarkDB::new_bytecode(Bytecode::new_raw_checked(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_checked(
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
22 changes: 10 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,8 @@ use std::{
use thiserror::Error;
use walkdir::{DirEntry, WalkDir};

type TestEvmWiring = revm::primitives::EthereumWiring;

#[derive(Debug, Error)]
#[error("Test {name} failed: {kind}")]
pub struct TestError {
Expand Down Expand Up @@ -130,8 +132,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<TestEvmWiring>, TestEvmWiring, Infallible>,
evm: &Evm<'_, TestEvmWiring, 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 +157,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 @@ -268,7 +270,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<TestEvmWiring>>::default();
// for mainnet
env.cfg.chain_id = 1;
// env.cfg.spec_id is set down the road
Expand Down Expand Up @@ -336,6 +338,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 @@ -357,10 +361,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 @@ -426,10 +427,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 = "7.1.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 @@ -51,14 +52,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 @@ -68,6 +61,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 @@ -76,5 +70,6 @@ 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"]

kzg-rs = ["revm-primitives/kzg-rs"]
2 changes: 1 addition & 1 deletion crates/interpreter/src/gas/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub const fn selfdestruct_cost(spec_id: SpecId, res: SelfDestructResult) -> u64
/// * Account access gas. after berlin it can be cold or warm.
/// * Transfer value gas. If value is transferred and balance of target account is updated.
/// * If account is not existing and needs to be created. After Spurious dragon
/// this is only accounted if value is transferred.
/// this is only accounted if value is transferred.
#[inline]
pub const fn call_cost(
spec_id: SpecId,
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::EvmWiring;

/// EVM context host.
pub trait Host {
/// Chain specification.
type EvmWiringT: EvmWiring;

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

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

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

#[cfg(test)]
mod tests {
use revm_primitives::{db::EmptyDB, EthereumWiring};

use super::*;

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

#[test]
fn object_safety() {
assert_host::<DummyHost>();
assert_host::<dyn Host>();
assert_host::<DummyHost<EthereumWiring<EmptyDB, ()>>>();
assert_host::<dyn Host<EvmWiringT = EthereumWiring<EmptyDB, ()>>>();
}
}
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, Env, EvmWiring, 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; EvmWiringT::Block, EvmWiringT::Transaction)]
pub struct DummyHost<EvmWiringT>
where
EvmWiringT: EvmWiring,
{
pub env: Env<EvmWiringT>,
pub storage: HashMap<U256, U256>,
pub transient_storage: HashMap<U256, U256>,
pub log: Vec<Log>,
}

impl DummyHost {
impl<EvmWiringT> DummyHost<EvmWiringT>
where
EvmWiringT: EvmWiring,
{
/// Create a new dummy host with the given [`Env`].
#[inline]
pub fn new(env: Env) -> Self {
pub fn new(env: Env<EvmWiringT>) -> 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<EvmWiringT> Host for DummyHost<EvmWiringT>
where
EvmWiringT: EvmWiring,
{
type EvmWiringT = EvmWiringT;

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

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

Expand Down
Loading