Skip to content

Commit

Permalink
feat: Context execution (#2013)
Browse files Browse the repository at this point in the history
* feat: Context execute

* all integrated

* clippy

* derive more no_std

* add correct caller/target address

* no std

* remove std from derive_more

* init first bench

* init precompiles

* rm comments
  • Loading branch information
rakita authored Jan 24, 2025
1 parent c3b6023 commit 78e8a1f
Show file tree
Hide file tree
Showing 58 changed files with 1,791 additions and 1,076 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ handler-interface = { path = "crates/handler/interface", package = "revm-handler
cfg-if = { version = "1.0", default-features = false }
auto_impl = { version = "1.2.0" }
derive-where = { version = "1.2.7", default-features = false }
derive_more = { version = "1.0.0", default-features = false }
criterion = { package = "codspeed-criterion-compat", version = "2.7" }

[workspace.package]
Expand Down
3 changes: 3 additions & 0 deletions bins/revme/benches/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use revme::cmd::{
};

fn evm(c: &mut Criterion) {
// call analysis to init static data.
revme::cmd::bench::analysis::run();

for &bench_name in BenchName::ALL {
let cmd = MainCmd::Bench(bench::Cmd { name: bench_name });
c.bench_function(bench_name.as_str(), |b| {
Expand Down
21 changes: 15 additions & 6 deletions bins/revme/src/cmd/bench/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use database::BenchmarkDB;
use std::time::Instant;

use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET};
use revm::{
bytecode::Bytecode,
primitives::{address, bytes, hex, Bytes, TxKind},
transact_main, Context,
primitives::{bytes, hex, Bytes, TxKind},
Context, ExecuteEvm,
};

const BYTES: &str = include_str!("analysis.hex");
Expand All @@ -15,10 +17,17 @@ pub fn run() {
.with_db(BenchmarkDB::new_bytecode(bytecode))
.modify_tx_chained(|tx| {
// Execution globals block hash/gas_limit/coinbase/timestamp..
tx.caller = address!("1000000000000000000000000000000000000000");
tx.kind = TxKind::Call(address!("0000000000000000000000000000000000000000"));
tx.caller = BENCH_CALLER;
tx.kind = TxKind::Call(BENCH_TARGET);
//evm.env.tx.data = Bytes::from(hex::decode("30627b7c").unwrap());
tx.data = bytes!("8035F0CE");
});
let _ = transact_main(&mut context);

let time = Instant::now();
let _ = context.exec_previous();
println!("First init: {:?}", time.elapsed());

let time = Instant::now();
let _ = context.exec_previous();
println!("Run: {:?}", time.elapsed());
}
6 changes: 3 additions & 3 deletions bins/revme/src/cmd/bench/burntpix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use static_data::{

use alloy_sol_macro::sol;
use alloy_sol_types::SolCall;
use database::CacheDB;
use database::{CacheDB, BENCH_CALLER};
use revm::{
context_interface::result::{ExecutionResult, Output},
database_interface::EmptyDB,
primitives::{address, hex, keccak256, Address, Bytes, TxKind, B256, U256},
primitives::{hex, keccak256, Address, Bytes, TxKind, B256, U256},
state::{AccountInfo, Bytecode},
transact_main, Context,
};
Expand All @@ -37,7 +37,7 @@ pub fn run() {
let db = init_db();

let mut context = Context::builder().with_db(db).modify_tx_chained(|tx| {
tx.caller = address!("1000000000000000000000000000000000000000");
tx.caller = BENCH_CALLER;
tx.kind = TxKind::Call(BURNTPIX_MAIN_ADDRESS);
tx.data = run_call_data.clone().into();
tx.gas_limit = u64::MAX;
Expand Down
8 changes: 4 additions & 4 deletions bins/revme/src/cmd/bench/snailtracer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use database::BenchmarkDB;
use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET};
use revm::{
bytecode::Bytecode,
primitives::{address, bytes, hex, Bytes, TxKind},
primitives::{bytes, hex, Bytes, TxKind},
transact_main, Context,
};

Expand All @@ -10,8 +10,8 @@ pub fn simple_example(bytecode: Bytecode) {
.with_db(BenchmarkDB::new_bytecode(bytecode.clone()))
.modify_tx_chained(|tx| {
// Execution globals block hash/gas_limit/coinbase/timestamp..
tx.caller = address!("1000000000000000000000000000000000000000");
tx.kind = TxKind::Call(address!("0000000000000000000000000000000000000000"));
tx.caller = BENCH_CALLER;
tx.kind = TxKind::Call(BENCH_TARGET);
tx.data = bytes!("30627b7c");
tx.gas_limit = 1_000_000_000;
});
Expand Down
24 changes: 13 additions & 11 deletions bins/revme/src/cmd/bench/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
use database::BenchmarkDB;
use std::time::Instant;

use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET};
use revm::{
bytecode::Bytecode,
primitives::{TxKind, U256},
transact_main, Context,
Context, ExecuteEvm,
};

pub fn run() {
let mut context = Context::builder()
.with_db(BenchmarkDB::new_bytecode(Bytecode::new()))
.modify_tx_chained(|tx| {
// Execution globals block hash/gas_limit/coinbase/timestamp..
tx.caller = "0x0000000000000000000000000000000000000001"
.parse()
.unwrap();
tx.caller = BENCH_CALLER;
tx.kind = TxKind::Call(BENCH_TARGET);
tx.value = U256::from(10);
tx.kind = TxKind::Call(
"0x0000000000000000000000000000000000000000"
.parse()
.unwrap(),
);
});
let _ = transact_main(&mut context);
let time = Instant::now();
let _ = context.exec_previous();
println!("First init: {:?}", time.elapsed());

let time = Instant::now();
let _ = context.exec_previous();
println!("Run: {:?}", time.elapsed());
}
14 changes: 6 additions & 8 deletions bins/revme/src/cmd/evmrunner.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::Parser;
use database::BenchmarkDB;
use inspector::{inspect_main, inspector_context::InspectorContext, inspectors::TracerEip3155};
use inspector::{exec::InspectEvm, inspectors::TracerEip3155};
use revm::{
bytecode::{Bytecode, BytecodeDecodeError},
primitives::{address, hex, Address, TxKind},
transact_main, Context, Database,
transact_main, Context, Database, ExecuteEvm,
};
use std::io::Error as IoError;
use std::path::PathBuf;
Expand Down Expand Up @@ -94,18 +94,16 @@ impl Cmd {
let bench_options = microbench::Options::default().time(Duration::from_secs(3));

microbench::bench(&bench_options, "Run bytecode", || {
let _ = transact_main(&mut ctx).unwrap();
let _ = ctx.exec_previous().unwrap();
});

return Ok(());
}

let out = if self.trace {
inspect_main(&mut InspectorContext::new(
&mut ctx,
TracerEip3155::new(Box::new(std::io::stdout())),
))
.map_err(|_| Errors::EVMError)?
let inspector = TracerEip3155::new(Box::new(std::io::stdout()));
ctx.inspect_previous(inspector)
.map_err(|_| Errors::EVMError)?
} else {
let out = transact_main(&mut ctx).map_err(|_| Errors::EVMError)?;
println!("Result: {:#?}", out.result);
Expand Down
47 changes: 20 additions & 27 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use super::{
};
use database::State;
use indicatif::{ProgressBar, ProgressDrawTarget};
use inspector::{
inspect_main_commit, inspector_context::InspectorContext, inspectors::TracerEip3155,
};
use inspector::{exec::InspectCommitEvm, inspectors::TracerEip3155};
use revm::{
bytecode::Bytecode,
context::{block::BlockEnv, cfg::CfgEnv, tx::TxEnv},
Expand All @@ -18,7 +16,7 @@ use revm::{
database_interface::EmptyDB,
primitives::{keccak256, Bytes, TxKind, B256},
specification::{eip4844::TARGET_BLOB_GAS_PER_BLOCK_CANCUN, hardfork::SpecId},
transact_main_commit, Context,
Context, ExecuteCommitEvm,
};
use serde_json::json;
use statetest_types::{SpecName, Test, TestSuite};
Expand Down Expand Up @@ -421,21 +419,20 @@ pub fn execute_test_suite(

// Do the deed
let (e, exec_result) = if trace {
let mut ctx = &mut InspectorContext::new(
Context::builder()
.with_block(&block)
.with_tx(&tx)
.with_cfg(&cfg)
.with_db(&mut state),
TracerEip3155::new(Box::new(stderr())).without_summary(),
);
let mut ctx = Context::builder()
.with_block(&block)
.with_tx(&tx)
.with_cfg(&cfg)
.with_db(&mut state);

let timer = Instant::now();
let res = inspect_main_commit(&mut ctx);
let res = ctx.inspect_commit_previous(
TracerEip3155::new(Box::new(stderr())).without_summary(),
);
*elapsed.lock().unwrap() += timer.elapsed();

let spec = cfg.spec();
let db = &mut ctx.inner.journaled_state.database;
let db = &mut ctx.journaled_state.database;
// Dump state and traces if test failed
let output = check_evm_execution(
&test,
Expand All @@ -452,7 +449,7 @@ pub fn execute_test_suite(
(e, res)
} else {
let timer = Instant::now();
let res = transact_main_commit(&mut ctx);
let res = ctx.exec_commit_previous();
*elapsed.lock().unwrap() += timer.elapsed();

let spec = cfg.spec();
Expand Down Expand Up @@ -494,24 +491,20 @@ pub fn execute_test_suite(

println!("\nTraces:");

let mut ctx = InspectorContext::new(
Context::builder()
.with_db(&mut state)
.with_block(&block)
.with_tx(&tx)
.with_cfg(&cfg),
let mut ctx = Context::builder()
.with_db(&mut state)
.with_block(&block)
.with_tx(&tx)
.with_cfg(&cfg);

let _ = ctx.inspect_commit_previous(
TracerEip3155::new(Box::new(stderr())).without_summary(),
);

let _ = inspect_main_commit(&mut ctx);

println!("\nExecution result: {exec_result:#?}");
println!("\nExpected exception: {:?}", test.expect_exception);
println!("\nState before: {cache_state:#?}");
println!(
"\nState after: {:#?}",
ctx.inner.journaled_state.database.cache
);
println!("\nState after: {:#?}", ctx.journaled_state.database.cache);
println!("\nSpecification: {:?}", cfg.spec);
println!("\nTx: {tx:#?}");
println!("Block: {block:#?}");
Expand Down
Loading

0 comments on commit 78e8a1f

Please sign in to comment.