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

feat: migrate to new inspector API #11

Merged
merged 9 commits into from
Feb 5, 2024
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
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ alloy-primitives = "0.6"
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy" }
alloy-rpc-trace-types = { git = "https://github.com/alloy-rs/alloy" }

# revm = "3.4"
revm = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze", default-features = false, features = ["std"] }
# revm = "4.0"
revm = { git = "https://github.com/bluealloy/revm", branch = "main", default-features = false, features = [
"std",
] }

# js-tracing-inspector
boa_engine = { version = "0.17", optional = true }
Expand Down
14 changes: 7 additions & 7 deletions src/access_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloy_primitives::{Address, B256};
use alloy_rpc_types::{AccessList, AccessListItem};
use revm::{
interpreter::{opcode, Interpreter},
Database, EVMData, Inspector,
Database, EvmContext, Inspector,
};
use std::collections::{BTreeSet, HashMap, HashSet};

Expand Down Expand Up @@ -62,11 +62,11 @@ impl<DB> Inspector<DB> for AccessListInspector
where
DB: Database,
{
fn step(&mut self, interpreter: &mut Interpreter<'_>, _data: &mut EVMData<'_, DB>) {
match interpreter.current_opcode() {
fn step(&mut self, interp: &mut Interpreter, _context: &mut EvmContext<DB>) {
match interp.current_opcode() {
opcode::SLOAD | opcode::SSTORE => {
if let Ok(slot) = interpreter.stack().peek(0) {
let cur_contract = interpreter.contract.address;
if let Ok(slot) = interp.stack().peek(0) {
let cur_contract = interp.contract.address;
self.access_list
.entry(cur_contract)
.or_default()
Expand All @@ -78,15 +78,15 @@ where
| opcode::EXTCODESIZE
| opcode::BALANCE
| opcode::SELFDESTRUCT => {
if let Ok(slot) = interpreter.stack().peek(0) {
if let Ok(slot) = interp.stack().peek(0) {
let addr = Address::from_word(B256::from(slot.to_be_bytes()));
if !self.excluded.contains(&addr) {
self.access_list.entry(addr).or_default();
}
}
}
opcode::DELEGATECALL | opcode::CALL | opcode::STATICCALL | opcode::CALLCODE => {
if let Ok(slot) = interpreter.stack().peek(1) {
if let Ok(slot) = interp.stack().peek(1) {
let addr = Address::from_word(B256::from(slot.to_be_bytes()));
if !self.excluded.contains(&addr) {
self.access_list.entry(addr).or_default();
Expand Down
86 changes: 36 additions & 50 deletions src/stack/maybe_owned.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use alloy_primitives::U256;
use alloy_primitives::{Log, U256};
use revm::{
interpreter::{CallInputs, CreateInputs, Gas, InstructionResult, Interpreter},
primitives::{db::Database, Address, Bytes, B256},
EVMData, Inspector,
interpreter::{CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter},
primitives::{db::Database, Address},
EvmContext, Inspector,
};
use std::{
cell::{Ref, RefCell},
ops::Range,
rc::Rc,
};

Expand Down Expand Up @@ -69,102 +70,87 @@ where
DB: Database,
INSP: Inspector<DB>,
{
fn initialize_interp(&mut self, interp: &mut Interpreter<'_>, data: &mut EVMData<'_, DB>) {
fn initialize_interp(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
match self {
MaybeOwnedInspector::Owned(insp) => insp.borrow_mut().initialize_interp(interp, data),
MaybeOwnedInspector::Owned(insp) => {
insp.borrow_mut().initialize_interp(interp, context)
}
MaybeOwnedInspector::Stacked(_) => {}
}
}

fn step(&mut self, interp: &mut Interpreter<'_>, data: &mut EVMData<'_, DB>) {
fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
match self {
MaybeOwnedInspector::Owned(insp) => insp.borrow_mut().step(interp, data),
MaybeOwnedInspector::Owned(insp) => insp.borrow_mut().step(interp, context),
MaybeOwnedInspector::Stacked(_) => {}
}
}

fn log(
&mut self,
evm_data: &mut EVMData<'_, DB>,
address: &Address,
topics: &[B256],
data: &Bytes,
) {
fn log(&mut self, context: &mut EvmContext<DB>, log: &Log) {
match self {
MaybeOwnedInspector::Owned(insp) => {
return insp.borrow_mut().log(evm_data, address, topics, data)
}
MaybeOwnedInspector::Owned(insp) => return insp.borrow_mut().log(context, log),
MaybeOwnedInspector::Stacked(_) => {}
}
}

fn step_end(&mut self, interp: &mut Interpreter<'_>, data: &mut EVMData<'_, DB>) {
fn step_end(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
match self {
MaybeOwnedInspector::Owned(insp) => insp.borrow_mut().step_end(interp, data),
MaybeOwnedInspector::Owned(insp) => insp.borrow_mut().step_end(interp, context),
MaybeOwnedInspector::Stacked(_) => {}
}
}

fn call(
&mut self,
data: &mut EVMData<'_, DB>,
context: &mut EvmContext<DB>,
inputs: &mut CallInputs,
) -> (InstructionResult, Gas, Bytes) {
return_memory_offset: Range<usize>,
) -> Option<CallOutcome> {
match self {
MaybeOwnedInspector::Owned(insp) => return insp.borrow_mut().call(data, inputs),
MaybeOwnedInspector::Stacked(_) => {}
MaybeOwnedInspector::Owned(insp) => {
return insp.borrow_mut().call(context, inputs, return_memory_offset)
}
MaybeOwnedInspector::Stacked(_) => None,
}

(InstructionResult::Continue, Gas::new(0), Bytes::new())
}

fn call_end(
&mut self,
data: &mut EVMData<'_, DB>,
context: &mut EvmContext<DB>,
inputs: &CallInputs,
remaining_gas: Gas,
ret: InstructionResult,
out: Bytes,
) -> (InstructionResult, Gas, Bytes) {
outcome: CallOutcome,
) -> CallOutcome {
match self {
MaybeOwnedInspector::Owned(insp) => {
return insp.borrow_mut().call_end(data, inputs, remaining_gas, ret, out)
insp.borrow_mut().call_end(context, inputs, outcome)
}
MaybeOwnedInspector::Stacked(_) => {}
MaybeOwnedInspector::Stacked(_) => outcome,
}
(ret, remaining_gas, out)
}

fn create(
&mut self,
data: &mut EVMData<'_, DB>,
context: &mut EvmContext<DB>,
inputs: &mut CreateInputs,
) -> (InstructionResult, Option<Address>, Gas, Bytes) {
) -> Option<CreateOutcome> {
match self {
MaybeOwnedInspector::Owned(insp) => return insp.borrow_mut().create(data, inputs),
MaybeOwnedInspector::Stacked(_) => {}
MaybeOwnedInspector::Owned(insp) => return insp.borrow_mut().create(context, inputs),
MaybeOwnedInspector::Stacked(_) => None,
}

(InstructionResult::Continue, None, Gas::new(0), Bytes::default())
}

fn create_end(
&mut self,
data: &mut EVMData<'_, DB>,
context: &mut EvmContext<DB>,
inputs: &CreateInputs,
ret: InstructionResult,
address: Option<Address>,
remaining_gas: Gas,
out: Bytes,
) -> (InstructionResult, Option<Address>, Gas, Bytes) {
outcome: CreateOutcome,
) -> CreateOutcome {
match self {
MaybeOwnedInspector::Owned(insp) => {
return insp.borrow_mut().create_end(data, inputs, ret, address, remaining_gas, out)
return insp.borrow_mut().create_end(context, inputs, outcome)
}
MaybeOwnedInspector::Stacked(_) => {}
MaybeOwnedInspector::Stacked(_) => outcome,
}

(ret, address, remaining_gas, out)
}

fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
Expand Down
101 changes: 43 additions & 58 deletions src/stack/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_primitives::{Address, Log, B256, U256};
use revm::{
inspectors::CustomPrintTracer,
interpreter::{CallInputs, CreateInputs, Gas, InstructionResult, Interpreter},
interpreter::{CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter},
primitives::Env,
Database, EVMData, Inspector,
Database, EvmContext, Inspector,
};
use std::fmt::Debug;
use std::{fmt::Debug, ops::Range};

/// A wrapped [Inspector] that can be reused in the stack
mod maybe_owned;
Expand All @@ -15,7 +15,7 @@ pub use maybe_owned::MaybeOwnedInspector;
/// - Block: Hook on block execution
/// - BlockWithIndex: Hook on block execution transaction index
/// - Transaction: Hook on a specific transaction hash
#[derive(Debug, Default, Clone, Copy)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc looks outdated/wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this was the case previously, tbh I don't know why this is here.
Perhpas the idea was to move the stack from foundry to here, but I'd rather delete this one here, will probably do so.

#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub enum Hook {
#[default]
/// No hook.
Expand All @@ -30,8 +30,9 @@ pub enum Hook {

/// An inspector that calls multiple inspectors in sequence.
///
/// If a call to an inspector returns a value other than [InstructionResult::Continue] (or
/// equivalent) the remaining inspectors are not called.
/// If a call to an inspector returns a value other than
/// [revm::interpreter::InstructionResult::Continue] (or equivalent) the remaining inspectors are
/// not called.
#[derive(Default, Clone)]
pub struct InspectorStack {
/// An inspector that prints the opcode traces to the console.
Expand Down Expand Up @@ -100,111 +101,95 @@ impl<DB> Inspector<DB> for InspectorStack
where
DB: Database,
{
fn initialize_interp(&mut self, interpreter: &mut Interpreter<'_>, data: &mut EVMData<'_, DB>) {
fn initialize_interp(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
call_inspectors!(inspector, [&mut self.custom_print_tracer], {
inspector.initialize_interp(interpreter, data);
inspector.initialize_interp(interp, context);
});
}

fn step(&mut self, interpreter: &mut Interpreter<'_>, data: &mut EVMData<'_, DB>) {
fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
call_inspectors!(inspector, [&mut self.custom_print_tracer], {
inspector.step(interpreter, data);
inspector.step(interp, context);
});
}

fn log(
&mut self,
evm_data: &mut EVMData<'_, DB>,
address: &Address,
topics: &[B256],
data: &Bytes,
) {
fn step_end(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
call_inspectors!(inspector, [&mut self.custom_print_tracer], {
inspector.log(evm_data, address, topics, data);
inspector.step_end(interp, context);
});
}

fn step_end(&mut self, interpreter: &mut Interpreter<'_>, data: &mut EVMData<'_, DB>) {
fn log(&mut self, context: &mut EvmContext<DB>, log: &Log) {
call_inspectors!(inspector, [&mut self.custom_print_tracer], {
inspector.step_end(interpreter, data);
inspector.log(context, log);
});
}

fn call(
&mut self,
data: &mut EVMData<'_, DB>,
context: &mut EvmContext<DB>,
inputs: &mut CallInputs,
) -> (InstructionResult, Gas, Bytes) {
return_memory_offset: Range<usize>,
) -> Option<CallOutcome> {
call_inspectors!(inspector, [&mut self.custom_print_tracer], {
let (status, gas, retdata) = inspector.call(data, inputs);

// Allow inspectors to exit early
if status != InstructionResult::Continue {
return (status, gas, retdata);
if let Some(outcome) = inspector.call(context, inputs, return_memory_offset) {
return Some(outcome);
}
});

(InstructionResult::Continue, Gas::new(inputs.gas_limit), Bytes::new())
None
}

fn call_end(
&mut self,
data: &mut EVMData<'_, DB>,
context: &mut EvmContext<DB>,
inputs: &CallInputs,
remaining_gas: Gas,
ret: InstructionResult,
out: Bytes,
) -> (InstructionResult, Gas, Bytes) {
outcome: CallOutcome,
) -> CallOutcome {
call_inspectors!(inspector, [&mut self.custom_print_tracer], {
let (new_ret, new_gas, new_out) =
inspector.call_end(data, inputs, remaining_gas, ret, out.clone());
let new_ret = inspector.call_end(context, inputs, outcome.clone());

// If the inspector returns a different ret or a revert with a non-empty message,
// we assume it wants to tell us something
if new_ret != ret || (new_ret == InstructionResult::Revert && new_out != out) {
return (new_ret, new_gas, new_out);
if new_ret != outcome {
return new_ret;
}
});

(ret, remaining_gas, out)
outcome
}

fn create(
&mut self,
data: &mut EVMData<'_, DB>,
context: &mut EvmContext<DB>,
inputs: &mut CreateInputs,
) -> (InstructionResult, Option<Address>, Gas, Bytes) {
) -> Option<CreateOutcome> {
call_inspectors!(inspector, [&mut self.custom_print_tracer], {
let (status, addr, gas, retdata) = inspector.create(data, inputs);

// Allow inspectors to exit early
if status != InstructionResult::Continue {
return (status, addr, gas, retdata);
if let Some(out) = inspector.create(context, inputs) {
return Some(out);
}
});

(InstructionResult::Continue, None, Gas::new(inputs.gas_limit), Bytes::new())
None
}

fn create_end(
&mut self,
data: &mut EVMData<'_, DB>,
context: &mut EvmContext<DB>,
inputs: &CreateInputs,
ret: InstructionResult,
address: Option<Address>,
remaining_gas: Gas,
out: Bytes,
) -> (InstructionResult, Option<Address>, Gas, Bytes) {
outcome: CreateOutcome,
) -> CreateOutcome {
call_inspectors!(inspector, [&mut self.custom_print_tracer], {
let (new_ret, new_address, new_gas, new_retdata) =
inspector.create_end(data, inputs, ret, address, remaining_gas, out.clone());
let new_ret = inspector.create_end(context, inputs, outcome.clone());

if new_ret != ret {
return (new_ret, new_address, new_gas, new_retdata);
// If the inspector returns a different ret or a revert with a non-empty message,
// we assume it wants to tell us something
if new_ret != outcome {
return new_ret;
}
});

(ret, address, remaining_gas, out)
outcome
}

fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
Expand Down
Loading
Loading