Skip to content

Commit

Permalink
feat: Restructuring Part3 inspector crate (#1788)
Browse files Browse the repository at this point in the history
* feat: Restructuring Part3 inspector crate

* fix serde include

* fix docs
  • Loading branch information
rakita authored Sep 19, 2024
1 parent fa1e546 commit 66adad0
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 93 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"crates/revm",
"crates/primitives",
"crates/interpreter",
"crates/inspector",
"crates/precompile",
"crates/optimism",
"crates/database",
Expand Down Expand Up @@ -39,6 +40,7 @@ state = { path = "crates/state", package = "revm-state", version = "1.0.0", defa
wiring = { path = "crates/wiring", package = "revm-wiring", version = "1.0.0", default-features = false }
revm = { path = "crates/revm", version = "14.0.1", default-features = false }
interpreter = { path = "crates/interpreter", package = "revm-interpreter", version = "10.0.1", default-features = false }
inspector = { path = "crates/inspector", package = "revm-inspector", version = "1.0.0", default-features = false }
precompile = { path = "crates/precompile", package = "revm-precompile", version = "11.0.1", default-features = false }

[workspace.package]
Expand Down
11 changes: 3 additions & 8 deletions bins/revme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ license.workspace = true
repository.workspace = true

[dependencies]
database.workspace = true
# revm
revm = { workspace = true, features = [
"std",
"serde-json",
"hashbrown",
"c-kzg",
"blst",
] }
database.workspace = true
revm = { workspace = true, features = ["std", "hashbrown", "c-kzg", "blst"] }
inspector = { workspace = true, features = ["std", "serde", "serde-json"] }

hash-db = "0.15"
hex = "0.4"
Expand Down
3 changes: 1 addition & 2 deletions bins/revme/src/cmd/evmrunner.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use clap::Parser;
use database::BenchmarkDB;
use inspector::{inspector_handle_register, inspectors::TracerEip3155};
use revm::{
bytecode::{Bytecode, BytecodeDecodeError},
inspector_handle_register,
inspectors::TracerEip3155,
primitives::{address, Address, TxKind},
wiring::EthereumWiring,
Database, Evm,
Expand Down
3 changes: 1 addition & 2 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use super::{
};
use database::State;
use indicatif::{ProgressBar, ProgressDrawTarget};
use inspector::{inspector_handle_register, inspectors::TracerEip3155};
use revm::{
bytecode::Bytecode,
database_interface::EmptyDB,
inspector_handle_register,
inspectors::TracerEip3155,
interpreter::analysis::to_analysed,
primitives::{keccak256, Bytes, TxKind, B256},
specification::{eip7702::AuthorizationList, hardfork::SpecId},
Expand Down
19 changes: 16 additions & 3 deletions crates/inspector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,28 @@ rust_2018_idioms = "deny"
all = "warn"

[dependencies]
# revm
revm.workspace = true

# mics
auto_impl = { version = "1.2", default-features = false }
derive-where = { version = "1.2.7", default-features = false }

# Optional
serde = { version = "1.0", default-features = false, features = [
"derive",
"rc",
], optional = true }
serde_json = { version = "1.0", default-features = false, features = [
"alloc",
], optional = true }

[dev-dependencies]
database = { workspace = true, features = ["serde"] }

[features]
default = ["std"]
std = ["serde?/std"]
serde = ["dep:serde"]
serde-json = ["serde"]
# Preserve ordeder of json
std = ["serde?/std", "serde_json?/std", "serde_json?/preserve_order"]
serde = ["dep:serde", "revm/serde", "database/serde"]
serde-json = ["serde", "dep:serde_json"]
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! Custom print inspector, it has step level information of execution.
//! It is a great tool if some debugging is needed.
use crate::{inspectors::GasInspector, EvmContext, EvmWiring, Inspector};
use interpreter::{CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter, OpCode};
use primitives::{Address, U256};
use crate::{inspectors::GasInspector, Inspector};
use revm::{
interpreter::{CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter, OpCode},
primitives::{Address, U256},
EvmContext, EvmWiring,
};

/// Custom print [Inspector], it has step level information of execution.
///
Expand Down Expand Up @@ -111,13 +114,17 @@ impl<EvmWiringT: EvmWiring> Inspector<EvmWiringT> for CustomPrintTracer {
#[cfg(test)]
mod test {
use super::*;
use crate::{inspector_handle_register, Evm};
use bytecode::Bytecode;
use crate::inspector_handle_register;

use database::InMemoryDB;
use primitives::{address, bytes, keccak256, Bytes, TxKind, U256};
use specification::hardfork::SpecId;
use state::AccountInfo;
use wiring::EthereumWiring;
use revm::{
bytecode::Bytecode,
primitives::{address, bytes, keccak256, Bytes, TxKind, U256},
specification::hardfork::SpecId,
state::AccountInfo,
wiring::EthereumWiring,
Evm,
};

#[test]
fn gas_calculation_underflow() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::{inspectors::GasInspector, EvmContext, EvmWiring, Inspector};
use crate::{inspectors::GasInspector, Inspector};
use derive_where::derive_where;

use interpreter::{
CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter, InterpreterResult, OpCode,
use revm::{
interpreter::{
CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter, InterpreterResult,
OpCode,
},
primitives::{hex, HashMap, B256, U256},
wiring::Transaction,
EvmContext, EvmWiring,
};
use primitives::{hex, HashMap, B256, U256};
use serde::Serialize;
use std::io::Write;
use wiring::Transaction;

/// [EIP-3155](https://eips.ethereum.org/EIPS/eip-3155) tracer [Inspector].
#[derive_where(Debug)]
Expand Down
21 changes: 14 additions & 7 deletions crates/revm/src/inspector/gas.rs → crates/inspector/src/gas.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! GasIspector. Helper Inspector to calculate gas for others.
use crate::{EvmContext, EvmWiring, Inspector};
use interpreter::{CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter};
use crate::Inspector;
use revm::{
interpreter::{CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter},
EvmContext, EvmWiring,
};

/// Helper [Inspector] that keeps track of gas.
#[allow(dead_code)]
Expand Down Expand Up @@ -70,12 +73,16 @@ impl<EvmWiringT: EvmWiring> Inspector<EvmWiringT> for GasInspector {
#[cfg(test)]
mod tests {
use super::*;
use crate::{inspector::inspector_handle_register, Evm, EvmWiring};
use bytecode::Bytecode;
use crate::inspector_handle_register;
use database::BenchmarkDB;
use interpreter::{opcode, Interpreter};
use primitives::{address, Bytes, Log, TxKind};
use wiring::{DefaultEthereumWiring, EthereumWiring, EvmWiring as PrimitiveEvmWiring};
use revm::{
bytecode::Bytecode,
interpreter::{opcode, Interpreter},
primitives::{address, Bytes, Log, TxKind},
wiring::EvmWiring as PrimitiveEvmWiring,
wiring::{DefaultEthereumWiring, EthereumWiring},
Evm, EvmWiring,
};

type TestEvmWiring = DefaultEthereumWiring;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{
handler::register::EvmHandler, Context, EvmWiring, FrameOrResult, FrameResult, Inspector,
JournalEntry,
};
use crate::Inspector;
use core::cell::RefCell;
use interpreter::{opcode, opcode::DynInstruction, InstructionResult, Interpreter};
use revm::{
handler::register::EvmHandler,
interpreter::{opcode, opcode::DynInstruction, InstructionResult, Interpreter},
wiring::result::EVMResultGeneric,
Context, EvmWiring, FrameOrResult, FrameResult, JournalEntry,
};
use std::{rc::Rc, sync::Arc, vec::Vec};
use wiring::result::EVMResultGeneric;

/// Provides access to an `Inspector` instance.
pub trait GetInspector<EvmWiringT: EvmWiring> {
Expand Down Expand Up @@ -261,15 +262,16 @@ fn inspector_instruction<EvmWiringT>(
#[cfg(test)]
mod tests {
use super::*;
use crate::{
inspector::inspector_handle_register, inspectors::NoOpInspector, Evm, EvmContext, EvmWiring,
};
use bytecode::Bytecode;
use crate::{inspector_handle_register, inspectors::NoOpInspector};
use database::BenchmarkDB;
use database_interface::EmptyDB;
use interpreter::{opcode, CallInputs, CallOutcome, CreateInputs, CreateOutcome};
use primitives::{address, Bytes, TxKind};
use wiring::{DefaultEthereumWiring, EthereumWiring, EvmWiring as PrimitiveEvmWiring};
use revm::{
bytecode::Bytecode,
database_interface::EmptyDB,
interpreter::{opcode, CallInputs, CallOutcome, CreateInputs, CreateOutcome},
primitives::{address, Bytes, TxKind},
wiring::{DefaultEthereumWiring, EthereumWiring, EvmWiring as PrimitiveEvmWiring},
Evm, EvmContext, EvmWiring,
};

type TestEvmWiring = DefaultEthereumWiring;

Expand Down
39 changes: 10 additions & 29 deletions crates/revm/src/inspector.rs → crates/inspector/src/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
#[cfg(feature = "std")]
mod customprinter;
#[cfg(all(feature = "std", feature = "serde-json"))]
mod eip3155;
mod gas;
mod handler_register;
mod noop;

pub use handler_register::{inspector_handle_register, GetInspector};

use interpreter::{
CallInputs, CallOutcome, CreateInputs, CreateOutcome, EOFCreateInputs, Interpreter,
};

use crate::{EvmContext, EvmWiring};
use auto_impl::auto_impl;
use primitives::{Address, Log, U256};

/// [Inspector] implementations.
pub mod inspectors {
#[cfg(feature = "std")]
pub use super::customprinter::CustomPrintTracer;
#[cfg(all(feature = "std", feature = "serde-json"))]
pub use super::eip3155::TracerEip3155;
pub use super::gas::GasInspector;
pub use super::noop::NoOpInspector;
}
use revm::{
interpreter::{
CallInputs, CallOutcome, CreateInputs, CreateOutcome, EOFCreateInputs, Interpreter,
},
primitives::{Address, Log, U256},
EvmContext, EvmWiring,
};

/// EVM [Interpreter] callbacks.
#[auto_impl(&mut, Box)]
pub trait Inspector<EvmWiringT: EvmWiring> {
/// Called before the interpreter is initialized.
///
/// If `interp.instruction_result` is set to anything other than [crate::interpreter::InstructionResult::Continue] then the execution of the interpreter
/// If `interp.instruction_result` is set to anything other than [revm::interpreter::InstructionResult::Continue] then the execution of the interpreter
/// is skipped.
#[inline]
fn initialize_interp(
Expand Down Expand Up @@ -59,7 +40,7 @@ pub trait Inspector<EvmWiringT: EvmWiring> {

/// Called after `step` when the instruction has been executed.
///
/// Setting `interp.instruction_result` to anything other than [crate::interpreter::InstructionResult::Continue] alters the execution
/// Setting `interp.instruction_result` to anything other than [revm::interpreter::InstructionResult::Continue] alters the execution
/// of the interpreter.
#[inline]
fn step_end(&mut self, interp: &mut Interpreter, context: &mut EvmContext<EvmWiringT>) {
Expand All @@ -77,7 +58,7 @@ pub trait Inspector<EvmWiringT: EvmWiring> {

/// Called whenever a call to a contract is about to start.
///
/// InstructionResulting anything other than [crate::interpreter::InstructionResult::Continue] overrides the result of the call.
/// InstructionResulting anything other than [revm::interpreter::InstructionResult::Continue] overrides the result of the call.
#[inline]
fn call(
&mut self,
Expand Down
22 changes: 22 additions & 0 deletions crates/inspector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,25 @@

#[cfg(not(feature = "std"))]
extern crate alloc as std;

#[cfg(feature = "std")]
mod customprinter;
#[cfg(all(feature = "std", feature = "serde-json"))]
mod eip3155;
mod gas;
mod handler_register;
mod inspector;
mod noop;

pub use handler_register::{inspector_handle_register, GetInspector};
pub use inspector::Inspector;

/// [Inspector] implementations.
pub mod inspectors {
#[cfg(feature = "std")]
pub use super::customprinter::CustomPrintTracer;
#[cfg(all(feature = "std", feature = "serde-json"))]
pub use super::eip3155::TracerEip3155;
pub use super::gas::GasInspector;
pub use super::noop::NoOpInspector;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{EvmWiring, Inspector};
use crate::Inspector;
use revm::EvmWiring;

/// Dummy [Inspector], helpful as standalone replacement.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down
1 change: 0 additions & 1 deletion crates/optimism/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ default = ["std", "c-kzg", "secp256k1", "portable", "blst"]
std = ["serde?/std", "revm/std", "precompile/std"]
hashbrown = ["revm/hashbrown"]
serde = ["dep:serde", "revm/serde"]
serde-json = ["serde", "revm/serde-json"]
portable = ["revm/portable"]

dev = [
Expand Down
Loading

0 comments on commit 66adad0

Please sign in to comment.