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

Trace json #356

Merged
merged 10 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

target
.vscode
.idea
pkg/


Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bins/revme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ revm = { path = "../../crates/revm", version = "3.0.0", default-features = false
rlp = { version = "0.5", default-features = false }
ruint = { version = "1.7.0", features = ["rlp", "serde"] }
serde = { version = "1.0", features = ["derive", "rc"] }
serde_json = "1.0"
serde_json = { version = "1.0", features = ["preserve_order"] }
sha3 = { version = "0.10", default-features = false }
structopt = "0.3"
thiserror = "1.0"
Expand Down
4 changes: 3 additions & 1 deletion bins/revme/src/statetest/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ pub struct Cmd {
path: Vec<PathBuf>,
#[structopt(short = "s", long)]
single_thread: bool,
#[structopt(long)]
json: bool,
}

impl Cmd {
pub fn run(&self) -> Result<(), TestError> {
for path in &self.path {
println!("Start running tests on: {path:?}");
let test_files = find_all_json_tests(path);
run(test_files, self.single_thread)?
run(test_files, self.single_thread, self.json)?
}
Ok(())
}
Expand Down
58 changes: 50 additions & 8 deletions bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io::stdout;
use std::{
collections::HashMap,
ffi::OsStr,
Expand All @@ -7,9 +8,10 @@ use std::{
};

use indicatif::ProgressBar;

use revm::inspectors::TracerEip3155;
use revm::{
db::AccountState,
inspectors::CustomPrintTracer,
interpreter::CreateScheme,
primitives::{Bytecode, Env, ExecutionResult, SpecId, TransactTo, B160, B256, U256},
};
Expand Down Expand Up @@ -50,7 +52,11 @@ pub fn find_all_json_tests(path: &Path) -> Vec<PathBuf> {
.collect::<Vec<PathBuf>>()
}

pub fn execute_test_suit(path: &Path, elapsed: &Arc<Mutex<Duration>>) -> Result<(), TestError> {
pub fn execute_test_suit(
path: &Path,
elapsed: &Arc<Mutex<Duration>>,
trace: bool,
) -> Result<(), TestError> {
// funky test with `bigint 0x00` value in json :) not possible to happen on mainnet and require custom json parser.
// https://github.com/ethereum/tests/issues/971
if path.file_name() == Some(OsStr::new("ValueOverflow.json")) {
Expand Down Expand Up @@ -247,7 +253,12 @@ pub fn execute_test_suit(path: &Path, elapsed: &Arc<Mutex<Duration>>) -> Result<
// do the deed

let timer = Instant::now();
let out = evm.transact_commit();

let exec_result = if trace {
evm.inspect_commit(TracerEip3155::new(Box::new(stdout()), false, false))
} else {
evm.transact_commit()
};
let timer = timer.elapsed();

*elapsed.lock().unwrap() += timer;
Expand All @@ -268,7 +279,7 @@ pub fn execute_test_suit(path: &Path, elapsed: &Arc<Mutex<Duration>>) -> Result<
})
.map(|(k, v)| (*k, v.clone())),
);
let logs = match &out {
let logs = match &exec_result {
Ok(ExecutionResult::Success { logs, .. }) => logs.clone(),
_ => Vec::new(),
};
Expand All @@ -280,10 +291,33 @@ pub fn execute_test_suit(path: &Path, elapsed: &Arc<Mutex<Duration>>) -> Result<
);
let mut database_cloned = database.clone();
evm.database(&mut database_cloned);
let _ = evm.inspect_commit(CustomPrintTracer::default());
let _ =
evm.inspect_commit(TracerEip3155::new(Box::new(stdout()), false, false));
let db = evm.db().unwrap();
println!("{path:?} UNIT_TEST:{name}\n");
println!("Output: {out:?}");
match &exec_result {
Ok(ExecutionResult::Success {
reason,
gas_used,
gas_refunded,
..
}) => {
println!("Failed reason: {reason:?} {path:?} UNIT_TEST:{name}\n gas:{gas_used:?} ({gas_refunded:?} refunded)");
}
Ok(ExecutionResult::Revert { gas_used, output }) => {
println!(
"Reverted: {output:?} {path:?} UNIT_TEST:{name}\n gas:{gas_used:?}"
);
}
Ok(ExecutionResult::Halt { reason, gas_used }) => {
println!(
"Halted: {reason:?} {path:?} UNIT_TEST:{name}\n gas:{gas_used:?}"
);
}
Err(out) => {
println!("Output: {out:?} {path:?} UNIT_TEST:{name}\n");
}
}
println!("\nApplied state:{db:?}\n");
println!("\nStateroot: {state_root:?}\n");
return Err(TestError::RootMissmatch {
Expand All @@ -299,7 +333,15 @@ pub fn execute_test_suit(path: &Path, elapsed: &Arc<Mutex<Duration>>) -> Result<
Ok(())
}

pub fn run(test_files: Vec<PathBuf>, single_thread: bool) -> Result<(), TestError> {
pub fn run(
test_files: Vec<PathBuf>,
mut single_thread: bool,
trace: bool,
) -> Result<(), TestError> {
if trace {
single_thread = true;
}

let endjob = Arc::new(AtomicBool::new(false));
let console_bar = Arc::new(ProgressBar::new(test_files.len() as u64));
let mut joins: Vec<std::thread::JoinHandle<Result<(), TestError>>> = Vec::new();
Expand Down Expand Up @@ -329,7 +371,7 @@ pub fn run(test_files: Vec<PathBuf>, single_thread: bool) -> Result<(), TestErro
return Ok(());
}
//println!("Test:{:?}\n",test_path);
if let Err(err) = execute_test_suit(&test_path, &elapsed) {
if let Err(err) = execute_test_suit(&test_path, &elapsed, trace) {
endjob.store(true, Ordering::SeqCst);
println!("Test[{index}] named:\n{test_path:?} failed: {err}\n");
return Err(err);
Expand Down
3 changes: 2 additions & 1 deletion crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ auto_impl = { version = "1.0", default-features = false }

# Optional
serde = { version = "1.0", features = ["derive", "rc"], optional = true }
serde_json = { version = "1.0", features = ["preserve_order"], optional = true }

# ethersdb
tokio = { version = "1.23", features = [
Expand Down Expand Up @@ -49,7 +50,7 @@ optional_eip3607 = ["revm-interpreter/optional_eip3607"]
optional_gas_refund = ["revm-interpreter/optional_gas_refund"]
std = ["revm-interpreter/std"]
ethersdb = ["tokio", "futures", "ethers-providers", "ethers-core"]
serde = ["dep:serde", "revm-interpreter/serde"]
serde = ["dep:serde","dep:serde_json", "revm-interpreter/serde"]
# deprecated feature
web3db = []
with-serde = []
4 changes: 4 additions & 0 deletions crates/revm/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ use auto_impl::auto_impl;
pub mod customprinter;
pub mod gas;
pub mod noop;
#[cfg(feature = "std")]
pub mod tracer_eip3155;

/// All Inspectors implementations that revm has.
pub mod inspectors {
#[cfg(feature = "std")]
pub use super::customprinter::CustomPrintTracer;
pub use super::gas::GasInspector;
pub use super::noop::NoOpInspector;
#[cfg(feature = "std")]
pub use super::tracer_eip3155::TracerEip3155;
}

#[auto_impl(&mut, Box)]
Expand Down
12 changes: 12 additions & 0 deletions crates/revm/src/inspector/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ pub struct GasInspector {
was_jumpi: Option<usize>,

gas_remaining: u64,
last_gas_cost: u64,
}

impl GasInspector {
pub fn gas_remaining(&self) -> u64 {
self.gas_remaining
}

pub fn last_gas_cost(&self) -> u64 {
self.last_gas_cost
}
}

impl<DB: Database> Inspector<DB> for GasInspector {
Expand Down Expand Up @@ -89,8 +94,15 @@ impl<DB: Database> Inspector<DB> for GasInspector {
self.full_gas_block = interp.contract.gas_block(previous_pc);
self.was_return = false;
}

let last_gas = self.gas_remaining;
self.gas_remaining =
interp.gas.remaining() + (self.full_gas_block - self.reduced_gas_block);
if last_gas > self.gas_remaining {
self.last_gas_cost = last_gas - self.gas_remaining;
} else {
self.last_gas_cost = 0;
}
InstructionResult::Continue
}

Expand Down
Loading