Skip to content

Commit 34bc873

Browse files
authored
feat: EthHandler trait (#2001)
* feat: EthHandler trait * work work * frame contexts * inspector compiled * transact_main and transact_main_commit * Generalize InterpreterTypes * big cleanup, rm all partial handlers * compile * Some cleanup * some cleanup * Erc20 example, need to fix logic * fix erc20 example * cleanup and testing * fix eip7702 order, pass all devnet tests * fix compilation * docs
1 parent 25512fd commit 34bc873

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1848
-2544
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use database::BenchmarkDB;
22
use revm::{
33
bytecode::Bytecode,
4-
handler::EthHandler,
54
primitives::{address, bytes, hex, Bytes, TxKind},
6-
Context, MainEvm,
5+
transact_main, Context,
76
};
87

98
const BYTES: &str = include_str!("analysis.hex");
@@ -12,7 +11,7 @@ pub fn run() {
1211
let bytecode = Bytecode::new_raw(Bytes::from(hex::decode(BYTES).unwrap()));
1312

1413
// BenchmarkDB is dummy state that implements Database trait.
15-
let context = Context::builder()
14+
let mut context = Context::builder()
1615
.with_db(BenchmarkDB::new_bytecode(bytecode))
1716
.modify_tx_chained(|tx| {
1817
// Execution globals block hash/gas_limit/coinbase/timestamp..
@@ -21,6 +20,5 @@ pub fn run() {
2120
//evm.env.tx.data = Bytes::from(hex::decode("30627b7c").unwrap());
2221
tx.data = bytes!("8035F0CE");
2322
});
24-
let mut evm = MainEvm::new(context, EthHandler::default());
25-
let _ = evm.transact().unwrap();
23+
let _ = transact_main(&mut context);
2624
}

bins/revme/src/cmd/bench/burntpix.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ use database::CacheDB;
1212
use revm::{
1313
context_interface::result::{ExecutionResult, Output},
1414
database_interface::EmptyDB,
15-
handler::EthHandler,
1615
primitives::{address, hex, keccak256, Address, Bytes, TxKind, B256, U256},
1716
state::{AccountInfo, Bytecode},
18-
Context, MainEvm,
17+
transact_main, Context,
1918
};
2019

2120
use std::fs::File;
@@ -37,16 +36,15 @@ pub fn run() {
3736

3837
let db = init_db();
3938

40-
let context = Context::builder().with_db(db).modify_tx_chained(|tx| {
39+
let mut context = Context::builder().with_db(db).modify_tx_chained(|tx| {
4140
tx.caller = address!("1000000000000000000000000000000000000000");
4241
tx.kind = TxKind::Call(BURNTPIX_MAIN_ADDRESS);
4342
tx.data = run_call_data.clone().into();
4443
tx.gas_limit = u64::MAX;
4544
});
46-
let mut evm = MainEvm::new(context, EthHandler::default());
4745

4846
let started = Instant::now();
49-
let tx_result = evm.transact().unwrap().result;
47+
let tx_result = transact_main(&mut context).unwrap().result;
5048
let return_data = match tx_result {
5149
ExecutionResult::Success {
5250
output, gas_used, ..

bins/revme/src/cmd/bench/snailtracer.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use database::BenchmarkDB;
22
use revm::{
33
bytecode::Bytecode,
4-
handler::EthHandler,
54
primitives::{address, bytes, hex, Bytes, TxKind},
6-
Context, MainEvm,
5+
transact_main, Context,
76
};
87

98
pub fn simple_example(bytecode: Bytecode) {
10-
let context = Context::builder()
9+
let mut context = Context::builder()
1110
.with_db(BenchmarkDB::new_bytecode(bytecode.clone()))
1211
.modify_tx_chained(|tx| {
1312
// Execution globals block hash/gas_limit/coinbase/timestamp..
@@ -16,8 +15,7 @@ pub fn simple_example(bytecode: Bytecode) {
1615
tx.data = bytes!("30627b7c");
1716
tx.gas_limit = 1_000_000_000;
1817
});
19-
let mut evm = MainEvm::new(context, EthHandler::default());
20-
let _ = evm.transact().unwrap();
18+
let _ = transact_main(&mut context).unwrap();
2119
}
2220

2321
pub fn run() {
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use database::BenchmarkDB;
22
use revm::{
33
bytecode::Bytecode,
4-
handler::EthHandler,
54
primitives::{TxKind, U256},
6-
Context, MainEvm,
5+
transact_main, Context,
76
};
87

98
pub fn run() {
10-
let context = Context::builder()
9+
let mut context = Context::builder()
1110
.with_db(BenchmarkDB::new_bytecode(Bytecode::new()))
1211
.modify_tx_chained(|tx| {
1312
// Execution globals block hash/gas_limit/coinbase/timestamp..
@@ -21,7 +20,5 @@ pub fn run() {
2120
.unwrap(),
2221
);
2322
});
24-
let mut evm = MainEvm::new(context, EthHandler::default());
25-
26-
let _ = evm.transact().unwrap();
23+
let _ = transact_main(&mut context);
2724
}

bins/revme/src/cmd/evmrunner.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
use clap::Parser;
22
use database::BenchmarkDB;
3-
use inspector::{
4-
inspector_context::InspectorContext, inspector_handler, inspectors::TracerEip3155,
5-
InspectorMainEvm,
6-
};
3+
use inspector::{inspect_main, inspector_context::InspectorContext, inspectors::TracerEip3155};
74
use revm::{
85
bytecode::{Bytecode, BytecodeDecodeError},
9-
handler::EthHandler,
106
primitives::{address, hex, Address, TxKind},
11-
Context, Database, EvmExec, MainEvm,
7+
transact_main, Context, Database,
128
};
139
use std::io::Error as IoError;
1410
use std::path::PathBuf;
@@ -86,36 +82,32 @@ impl Cmd {
8682

8783
// BenchmarkDB is dummy state that implements Database trait.
8884
// The bytecode is deployed at zero address.
89-
let mut evm = MainEvm::new(
90-
Context::builder().with_db(db).modify_tx_chained(|tx| {
91-
tx.caller = CALLER;
92-
tx.kind = TxKind::Call(Address::ZERO);
93-
tx.data = input;
94-
tx.nonce = nonce;
95-
}),
96-
EthHandler::default(),
97-
);
85+
let mut ctx = Context::builder().with_db(db).modify_tx_chained(|tx| {
86+
tx.caller = CALLER;
87+
tx.kind = TxKind::Call(Address::ZERO);
88+
tx.data = input;
89+
tx.nonce = nonce;
90+
});
9891

9992
if self.bench {
10093
// Microbenchmark
10194
let bench_options = microbench::Options::default().time(Duration::from_secs(3));
10295

10396
microbench::bench(&bench_options, "Run bytecode", || {
104-
let _ = evm.transact().unwrap();
97+
let _ = transact_main(&mut ctx).unwrap();
10598
});
10699

107100
return Ok(());
108101
}
109102

110103
let out = if self.trace {
111-
let mut evm = InspectorMainEvm::new(
112-
InspectorContext::new(evm.context, TracerEip3155::new(Box::new(std::io::stdout()))),
113-
inspector_handler(),
114-
);
115-
116-
evm.exec().map_err(|_| Errors::EVMError)?
104+
inspect_main(&mut InspectorContext::new(
105+
&mut ctx,
106+
TracerEip3155::new(Box::new(std::io::stdout())),
107+
))
108+
.map_err(|_| Errors::EVMError)?
117109
} else {
118-
let out = evm.transact().map_err(|_| Errors::EVMError)?;
110+
let out = transact_main(&mut ctx).map_err(|_| Errors::EVMError)?;
119111
println!("Result: {:#?}", out.result);
120112
out
121113
};

bins/revme/src/cmd/statetest/runner.rs

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use super::{
55
use database::State;
66
use indicatif::{ProgressBar, ProgressDrawTarget};
77
use inspector::{
8-
inspector_context::InspectorContext, inspector_handler, inspectors::TracerEip3155,
9-
InspectorMainEvm,
8+
inspect_main_commit, inspector_context::InspectorContext, inspectors::TracerEip3155,
109
};
1110
use revm::{
1211
bytecode::Bytecode,
@@ -17,10 +16,9 @@ use revm::{
1716
Cfg,
1817
},
1918
database_interface::EmptyDB,
20-
handler::EthHandler,
2119
primitives::{keccak256, Bytes, TxKind, B256},
2220
specification::{eip4844::TARGET_BLOB_GAS_PER_BLOCK_CANCUN, hardfork::SpecId},
23-
Context, DatabaseCommit, EvmCommit, MainEvm,
21+
transact_main_commit, Context,
2422
};
2523
use serde_json::json;
2624
use statetest_types::{SpecName, Test, TestSuite};
@@ -361,7 +359,6 @@ pub fn execute_test_suite(
361359
}
362360

363361
for (index, test) in tests.into_iter().enumerate() {
364-
// TODO : TX TYPE needs to be set
365362
let Some(tx_type) = unit.transaction.tx_type(test.indexes.data) else {
366363
if test.expect_exception.is_some() {
367364
continue;
@@ -416,35 +413,29 @@ pub fn execute_test_suite(
416413
.with_cached_prestate(cache)
417414
.with_bundle_update()
418415
.build();
419-
let mut evm = MainEvm::new(
420-
Context::builder()
421-
.with_block(&block)
422-
.with_tx(&tx)
423-
.with_cfg(&cfg)
424-
.with_db(&mut state),
425-
EthHandler::default(),
426-
);
416+
let mut ctx = Context::builder()
417+
.with_block(&block)
418+
.with_tx(&tx)
419+
.with_cfg(&cfg)
420+
.with_db(&mut state);
427421

428422
// Do the deed
429423
let (e, exec_result) = if trace {
430-
let mut evm = InspectorMainEvm::new(
431-
InspectorContext::new(
432-
Context::builder()
433-
.with_block(&block)
434-
.with_tx(&tx)
435-
.with_cfg(&cfg)
436-
.with_db(&mut state),
437-
TracerEip3155::new(Box::new(stderr())).without_summary(),
438-
),
439-
inspector_handler(),
424+
let mut ctx = &mut InspectorContext::new(
425+
Context::builder()
426+
.with_block(&block)
427+
.with_tx(&tx)
428+
.with_cfg(&cfg)
429+
.with_db(&mut state),
430+
TracerEip3155::new(Box::new(stderr())).without_summary(),
440431
);
441432

442433
let timer = Instant::now();
443-
let res = evm.exec_commit();
434+
let res = inspect_main_commit(&mut ctx);
444435
*elapsed.lock().unwrap() += timer.elapsed();
445436

446437
let spec = cfg.spec();
447-
let db = evm.context.inner.journaled_state.database;
438+
let db = &mut ctx.inner.journaled_state.database;
448439
// Dump state and traces if test failed
449440
let output = check_evm_execution(
450441
&test,
@@ -461,11 +452,11 @@ pub fn execute_test_suite(
461452
(e, res)
462453
} else {
463454
let timer = Instant::now();
464-
let res = evm.exec_commit();
455+
let res = transact_main_commit(&mut ctx);
465456
*elapsed.lock().unwrap() += timer.elapsed();
466457

467458
let spec = cfg.spec();
468-
let db = evm.context.journaled_state.database;
459+
let db = ctx.journaled_state.database;
469460
// Dump state and traces if test failed
470461
let output = check_evm_execution(
471462
&test,
@@ -503,30 +494,23 @@ pub fn execute_test_suite(
503494

504495
println!("\nTraces:");
505496

506-
let mut evm = InspectorMainEvm::new(
507-
InspectorContext::new(
508-
Context::builder()
509-
.with_db(&mut state)
510-
.with_block(&block)
511-
.with_tx(&tx)
512-
.with_cfg(&cfg),
513-
TracerEip3155::new(Box::new(stderr())).without_summary(),
514-
),
515-
inspector_handler(),
497+
let mut ctx = InspectorContext::new(
498+
Context::builder()
499+
.with_db(&mut state)
500+
.with_block(&block)
501+
.with_tx(&tx)
502+
.with_cfg(&cfg),
503+
TracerEip3155::new(Box::new(stderr())).without_summary(),
516504
);
517505

518-
let res = evm.transact();
519-
let _ = res.map(|r| {
520-
evm.context.inner.journaled_state.database.commit(r.state);
521-
r.result
522-
});
506+
let _ = inspect_main_commit(&mut ctx);
523507

524508
println!("\nExecution result: {exec_result:#?}");
525509
println!("\nExpected exception: {:?}", test.expect_exception);
526510
println!("\nState before: {cache_state:#?}");
527511
println!(
528512
"\nState after: {:#?}",
529-
evm.context.inner.journaled_state.database.cache
513+
ctx.inner.journaled_state.database.cache
530514
);
531515
println!("\nSpecification: {:?}", cfg.spec);
532516
println!("\nTx: {tx:#?}");

crates/bytecode/src/opcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ mod tests {
784784
assert_eq!(
785785
opcode.map(|opcode| opcode.terminating).unwrap_or_default(),
786786
opcodes[i],
787-
"Opcode {:?} terminating chack failed.",
787+
"Opcode {:?} terminating check failed.",
788788
opcode
789789
);
790790
}

crates/context/interface/src/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use auto_impl::auto_impl;
2+
13
/// Some actions on top of context with just Getter traits would require borrowing the context
24
/// with a both mutable and immutable reference.
35
///
46
/// To allow doing this action more efficiently, we introduce a new trait that does this directly.
57
///
68
/// Used for loading access list and applying EIP-7702 authorization list.
9+
#[auto_impl(&mut,Box)]
710
pub trait PerformantContextAccess {
811
type Error;
912

crates/context/interface/src/host.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use auto_impl::auto_impl;
88
use primitives::{Address, Bytes, Log, B256, U256};
99

1010
/// EVM context host.
11-
// TODO : Move to context-interface
1211
#[auto_impl(&mut, Box)]
1312
pub trait Host: TransactionGetter + BlockGetter + CfgGetter {
1413
/// Load an account code.

0 commit comments

Comments
 (0)