Skip to content

Commit b3fbb88

Browse files
authored
feat: Evm structure (Cached Instructions and Precompiles) (bluealloy#2049)
* feat: frame ctx inside Evm * feat: full Evm * Inspector trait wip * wip working inspector * rename * Inspector and Op support. Cleanup * traits, examples and cleanup * Cleanup all unused Getters * Cleanup and some renames * fix tests, clippy fmt * rm bench * no_std * doc * doc links * support inspector selfdestruct and logs * add erc20 example and cleanup
1 parent 83a3087 commit b3fbb88

Some content is hidden

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

90 files changed

+2721
-3892
lines changed

Cargo.lock

Lines changed: 2 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ members = [
1515
"crates/specification",
1616
"crates/context",
1717
"crates/context/interface",
18-
"crates/handler/interface",
1918
"crates/handler",
2019

2120
# variants
@@ -27,7 +26,7 @@ members = [
2726

2827
# examples
2928
"examples/block_traces",
30-
"examples/cheatcode_inspector",
29+
#"examples/cheatcode_inspector",
3130
"examples/contract_deployment",
3231
"examples/database_components",
3332
"examples/uniswap_get_reserves",
@@ -54,8 +53,6 @@ statetest-types = { path = "crates/statetest-types", package = "revm-statetest-t
5453
context = { path = "crates/context", package = "revm-context", version = "1.0.0", default-features = false }
5554
context-interface = { path = "crates/context/interface", package = "revm-context-interface", version = "1.0.0", default-features = false }
5655
handler = { path = "crates/handler", package = "revm-handler", version = "1.0.0", default-features = false }
57-
handler-interface = { path = "crates/handler/interface", package = "revm-handler-interface", version = "1.0.0", default-features = false }
58-
5956
# misc
6057
cfg-if = { version = "1.0", default-features = false }
6158
auto_impl = { version = "1.2.0" }

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET};
44
use revm::{
55
bytecode::Bytecode,
66
primitives::{bytes, hex, Bytes, TxKind},
7-
Context, ExecuteEvm,
7+
Context, ExecuteEvm, MainBuilder, MainContext,
88
};
99

1010
const BYTES: &str = include_str!("analysis.hex");
@@ -13,21 +13,22 @@ pub fn run() {
1313
let bytecode = Bytecode::new_raw(Bytes::from(hex::decode(BYTES).unwrap()));
1414

1515
// BenchmarkDB is dummy state that implements Database trait.
16-
let mut context = Context::builder()
16+
let context = Context::mainnet()
1717
.with_db(BenchmarkDB::new_bytecode(bytecode))
1818
.modify_tx_chained(|tx| {
1919
// Execution globals block hash/gas_limit/coinbase/timestamp..
2020
tx.caller = BENCH_CALLER;
2121
tx.kind = TxKind::Call(BENCH_TARGET);
22-
//evm.env.tx.data = Bytes::from(hex::decode("30627b7c").unwrap());
2322
tx.data = bytes!("8035F0CE");
2423
});
2524

25+
let mut evm = context.build_mainnet();
26+
2627
let time = Instant::now();
27-
let _ = context.exec_previous();
28+
let _ = evm.transact_previous();
2829
println!("First init: {:?}", time.elapsed());
2930

3031
let time = Instant::now();
31-
let _ = context.exec_previous();
32+
let _ = evm.transact_previous();
3233
println!("Run: {:?}", time.elapsed());
3334
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use revm::{
1414
database_interface::EmptyDB,
1515
primitives::{hex, keccak256, Address, Bytes, TxKind, B256, U256},
1616
state::{AccountInfo, Bytecode},
17-
transact_main, Context,
17+
Context, ExecuteEvm, MainBuilder, MainContext,
1818
};
1919

2020
use std::fs::File;
@@ -36,15 +36,18 @@ pub fn run() {
3636

3737
let db = init_db();
3838

39-
let mut context = Context::builder().with_db(db).modify_tx_chained(|tx| {
40-
tx.caller = BENCH_CALLER;
41-
tx.kind = TxKind::Call(BURNTPIX_MAIN_ADDRESS);
42-
tx.data = run_call_data.clone().into();
43-
tx.gas_limit = u64::MAX;
44-
});
39+
let mut evm = Context::mainnet()
40+
.with_db(db)
41+
.modify_tx_chained(|tx| {
42+
tx.caller = BENCH_CALLER;
43+
tx.kind = TxKind::Call(BURNTPIX_MAIN_ADDRESS);
44+
tx.data = run_call_data.clone().into();
45+
tx.gas_limit = u64::MAX;
46+
})
47+
.build_mainnet();
4548

4649
let started = Instant::now();
47-
let tx_result = transact_main(&mut context).unwrap().result;
50+
let tx_result = evm.transact_previous().unwrap().result;
4851
let return_data = match tx_result {
4952
ExecutionResult::Success {
5053
output, gas_used, ..

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET};
22
use revm::{
33
bytecode::Bytecode,
44
primitives::{bytes, hex, Bytes, TxKind},
5-
transact_main, Context,
5+
Context, ExecuteEvm, MainBuilder, MainContext,
66
};
77

88
pub fn simple_example(bytecode: Bytecode) {
9-
let mut context = Context::builder()
9+
let mut evm = Context::mainnet()
1010
.with_db(BenchmarkDB::new_bytecode(bytecode.clone()))
1111
.modify_tx_chained(|tx| {
1212
// Execution globals block hash/gas_limit/coinbase/timestamp..
1313
tx.caller = BENCH_CALLER;
1414
tx.kind = TxKind::Call(BENCH_TARGET);
1515
tx.data = bytes!("30627b7c");
1616
tx.gas_limit = 1_000_000_000;
17-
});
18-
let _ = transact_main(&mut context).unwrap();
17+
})
18+
.build_mainnet();
19+
let _ = evm.transact_previous().unwrap();
1920
}
2021

2122
pub fn run() {
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
use std::time::Instant;
2-
31
use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET};
42
use revm::{
53
bytecode::Bytecode,
64
primitives::{TxKind, U256},
7-
Context, ExecuteEvm,
5+
Context, ExecuteEvm, MainBuilder, MainContext,
86
};
7+
use std::time::Instant;
98

109
pub fn run() {
11-
let mut context = Context::builder()
10+
let time = Instant::now();
11+
let mut evm = Context::mainnet()
1212
.with_db(BenchmarkDB::new_bytecode(Bytecode::new()))
1313
.modify_tx_chained(|tx| {
1414
// Execution globals block hash/gas_limit/coinbase/timestamp..
1515
tx.caller = BENCH_CALLER;
1616
tx.kind = TxKind::Call(BENCH_TARGET);
1717
tx.value = U256::from(10);
18-
});
18+
})
19+
.build_mainnet();
20+
println!("Init: {:?}", time.elapsed());
21+
1922
let time = Instant::now();
20-
let _ = context.exec_previous();
21-
println!("First init: {:?}", time.elapsed());
23+
let _ = evm.transact_previous();
24+
println!("First run: {:?}", time.elapsed());
2225

2326
let time = Instant::now();
24-
let _ = context.exec_previous();
25-
println!("Run: {:?}", time.elapsed());
27+
let _ = evm.transact_previous();
28+
println!("Second run: {:?}", time.elapsed());
2629
}

bins/revme/src/cmd/evmrunner.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clap::Parser;
22
use database::BenchmarkDB;
3-
use inspector::{exec::InspectEvm, inspectors::TracerEip3155};
3+
use inspector::inspectors::TracerEip3155;
44
use revm::{
55
bytecode::{Bytecode, BytecodeDecodeError},
66
primitives::{address, hex, Address, TxKind},
7-
transact_main, Context, Database, ExecuteEvm,
7+
Context, Database, ExecuteEvm, InspectEvm, MainBuilder, MainContext,
88
};
99
use std::io::Error as IoError;
1010
use std::path::PathBuf;
@@ -82,30 +82,31 @@ impl Cmd {
8282

8383
// BenchmarkDB is dummy state that implements Database trait.
8484
// The bytecode is deployed at zero address.
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-
});
85+
let mut evm = Context::mainnet()
86+
.with_db(db)
87+
.modify_tx_chained(|tx| {
88+
tx.caller = CALLER;
89+
tx.kind = TxKind::Call(Address::ZERO);
90+
tx.data = input;
91+
tx.nonce = nonce;
92+
})
93+
.build_mainnet_with_inspector(TracerEip3155::new(Box::new(std::io::stdout())));
9194

9295
if self.bench {
9396
// Microbenchmark
9497
let bench_options = microbench::Options::default().time(Duration::from_secs(3));
9598

9699
microbench::bench(&bench_options, "Run bytecode", || {
97-
let _ = ctx.exec_previous().unwrap();
100+
let _ = evm.transact_previous().unwrap();
98101
});
99102

100103
return Ok(());
101104
}
102105

103106
let out = if self.trace {
104-
let inspector = TracerEip3155::new(Box::new(std::io::stdout()));
105-
ctx.inspect_previous(inspector)
106-
.map_err(|_| Errors::EVMError)?
107+
evm.inspect_previous().map_err(|_| Errors::EVMError)?
107108
} else {
108-
let out = transact_main(&mut ctx).map_err(|_| Errors::EVMError)?;
109+
let out = evm.transact_previous().map_err(|_| Errors::EVMError)?;
109110
println!("Result: {:#?}", out.result);
110111
out
111112
};

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{
44
};
55
use database::State;
66
use indicatif::{ProgressBar, ProgressDrawTarget};
7-
use inspector::{exec::InspectCommitEvm, inspectors::TracerEip3155};
7+
use inspector::inspectors::TracerEip3155;
88
use revm::{
99
bytecode::Bytecode,
1010
context::{block::BlockEnv, cfg::CfgEnv, tx::TxEnv},
@@ -16,7 +16,7 @@ use revm::{
1616
database_interface::EmptyDB,
1717
primitives::{keccak256, Bytes, TxKind, B256},
1818
specification::{eip4844::TARGET_BLOB_GAS_PER_BLOCK_CANCUN, hardfork::SpecId},
19-
Context, ExecuteCommitEvm,
19+
Context, ExecuteCommitEvm, InspectCommitEvm, MainBuilder, MainContext,
2020
};
2121
use serde_json::json;
2222
use statetest_types::{SpecName, Test, TestSuite};
@@ -411,28 +411,30 @@ pub fn execute_test_suite(
411411
.with_cached_prestate(cache)
412412
.with_bundle_update()
413413
.build();
414-
let mut ctx = Context::builder()
414+
let mut evm = Context::mainnet()
415415
.with_block(&block)
416416
.with_tx(&tx)
417417
.with_cfg(&cfg)
418-
.with_db(&mut state);
418+
.with_db(&mut state)
419+
.build_mainnet();
419420

420421
// Do the deed
421422
let (e, exec_result) = if trace {
422-
let mut ctx = Context::builder()
423+
let mut evm = Context::mainnet()
423424
.with_block(&block)
424425
.with_tx(&tx)
425426
.with_cfg(&cfg)
426-
.with_db(&mut state);
427+
.with_db(&mut state)
428+
.build_mainnet_with_inspector(
429+
TracerEip3155::buffered(stderr()).without_summary(),
430+
);
427431

428432
let timer = Instant::now();
429-
let res = ctx.inspect_commit_previous(
430-
TracerEip3155::buffered(stderr()).without_summary(),
431-
);
433+
let res = evm.inspect_commit_previous();
432434
*elapsed.lock().unwrap() += timer.elapsed();
433435

434436
let spec = cfg.spec();
435-
let db = &mut ctx.journaled_state.database;
437+
let db = &mut evm.data.ctx.journaled_state.database;
436438
// Dump state and traces if test failed
437439
let output = check_evm_execution(
438440
&test,
@@ -449,11 +451,11 @@ pub fn execute_test_suite(
449451
(e, res)
450452
} else {
451453
let timer = Instant::now();
452-
let res = ctx.exec_commit_previous();
454+
let res = evm.transact_commit_previous();
453455
*elapsed.lock().unwrap() += timer.elapsed();
454456

455457
let spec = cfg.spec();
456-
let db = ctx.journaled_state.database;
458+
let db = evm.data.ctx.journaled_state.database;
457459
// Dump state and traces if test failed
458460
let output = check_evm_execution(
459461
&test,
@@ -491,19 +493,24 @@ pub fn execute_test_suite(
491493

492494
println!("\nTraces:");
493495

494-
let mut ctx = Context::builder()
496+
let mut evm = Context::mainnet()
495497
.with_db(&mut state)
496498
.with_block(&block)
497499
.with_tx(&tx)
498-
.with_cfg(&cfg);
500+
.with_cfg(&cfg)
501+
.build_mainnet_with_inspector(
502+
TracerEip3155::buffered(stderr()).without_summary(),
503+
);
499504

500-
let _ = ctx
501-
.inspect_commit_previous(TracerEip3155::buffered(stderr()).without_summary());
505+
let _ = evm.inspect_commit_previous();
502506

503507
println!("\nExecution result: {exec_result:#?}");
504508
println!("\nExpected exception: {:?}", test.expect_exception);
505509
println!("\nState before: {cache_state:#?}");
506-
println!("\nState after: {:#?}", ctx.journaled_state.database.cache);
510+
println!(
511+
"\nState after: {:#?}",
512+
evm.data.ctx.journaled_state.database.cache
513+
);
507514
println!("\nSpecification: {:?}", cfg.spec);
508515
println!("\nTx: {tx:#?}");
509516
println!("Block: {block:#?}");

0 commit comments

Comments
 (0)