Skip to content

Commit

Permalink
refactor: prefer using revm helpers (#168)
Browse files Browse the repository at this point in the history
Those are now available as `OpCode` methods
  • Loading branch information
klkvr authored Jul 18, 2024
1 parent 41efb73 commit a128cf5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 105 deletions.
23 changes: 0 additions & 23 deletions src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,6 @@ where
}
}

/// Returns true if the opcode may modify memory.
///
/// <https://bluealloy.github.io/revm/crates/interpreter/memory.html#opcodes>
/// <https://github.com/crytic/evm-opcodes>
#[inline]
pub const fn may_modify_memory(opcode: OpCode) -> bool {
matches!(
opcode,
OpCode::EXTCODECOPY
| OpCode::MLOAD
| OpCode::MSTORE
| OpCode::MSTORE8
| OpCode::MCOPY
| OpCode::CODECOPY
| OpCode::CALLDATACOPY
| OpCode::RETURNDATACOPY
| OpCode::CALL
| OpCode::CALLCODE
| OpCode::DELEGATECALL
| OpCode::STATICCALL
)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
69 changes: 0 additions & 69 deletions src/tracing/builder/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use alloy_primitives::{Address, U64};
use alloy_rpc_types::{trace::parity::*, TransactionInfo};
use revm::{
db::DatabaseRef,
interpreter::{opcode, OpCode},
primitives::{Account, ExecutionResult, ResultAndState, SpecId, KECCAK_EMPTY},
};
use std::collections::{HashSet, VecDeque};
Expand Down Expand Up @@ -542,71 +541,3 @@ where

Ok(())
}

/// Returns the number of items pushed on the stack by a given opcode.
/// This used to determine how many stack etries to put in the `push` element
/// in a parity vmTrace.
/// The value is obvious for most opcodes, but SWAP* and DUP* are a bit weird,
/// and we handle those as they are handled in parity vmtraces.
/// For reference: <https://github.com/ledgerwatch/erigon/blob/9b74cf0384385817459f88250d1d9c459a18eab1/turbo/jsonrpc/trace_adhoc.go#L451>
pub(crate) const fn stack_push_count(step_op: OpCode) -> usize {
let step_op = step_op.get();
match step_op {
opcode::PUSH0..=opcode::PUSH32 => 1,
opcode::SWAP1..=opcode::SWAP16 => (step_op - opcode::SWAP1) as usize + 2,
opcode::DUP1..=opcode::DUP16 => (step_op - opcode::DUP1) as usize + 2,
opcode::CALLDATALOAD
| opcode::SLOAD
| opcode::MLOAD
| opcode::CALLDATASIZE
| opcode::LT
| opcode::GT
| opcode::DIV
| opcode::SDIV
| opcode::SAR
| opcode::AND
| opcode::EQ
| opcode::CALLVALUE
| opcode::ISZERO
| opcode::ADD
| opcode::EXP
| opcode::CALLER
| opcode::KECCAK256
| opcode::SUB
| opcode::ADDRESS
| opcode::GAS
| opcode::MUL
| opcode::RETURNDATASIZE
| opcode::NOT
| opcode::SHR
| opcode::SHL
| opcode::EXTCODESIZE
| opcode::SLT
| opcode::OR
| opcode::NUMBER
| opcode::PC
| opcode::TIMESTAMP
| opcode::BALANCE
| opcode::SELFBALANCE
| opcode::MULMOD
| opcode::ADDMOD
| opcode::BASEFEE
| opcode::BLOCKHASH
| opcode::BYTE
| opcode::XOR
| opcode::ORIGIN
| opcode::CODESIZE
| opcode::MOD
| opcode::SIGNEXTEND
| opcode::GASLIMIT
| opcode::DIFFICULTY
| opcode::SGT
| opcode::GASPRICE
| opcode::MSIZE
| opcode::EXTCODEHASH
| opcode::SMOD
| opcode::CHAINID
| opcode::COINBASE => 1,
_ => 0,
}
}
21 changes: 8 additions & 13 deletions src/tracing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use self::parity::stack_push_count;
use crate::{
opcode::may_modify_memory,
tracing::{
arena::PushTraceKind,
types::{
CallKind, CallTraceNode, RecordedMemory, StorageChange, StorageChangeReason,
TraceMemberOrder,
},
utils::gas_used,
use crate::tracing::{
arena::PushTraceKind,
types::{
CallKind, CallTraceNode, RecordedMemory, StorageChange, StorageChangeReason,
TraceMemberOrder,
},
utils::gas_used,
};
use alloy_primitives::{Address, Bytes, Log, U256};
use revm::{
Expand Down Expand Up @@ -380,7 +376,7 @@ impl TracingInspector {
let memory = self.config.record_memory_snapshots.then(|| {
if self.config.record_opcodes_filter.is_none() {
if let Some(prev) = trace.trace.steps.last() {
if !may_modify_memory(prev.op) {
if !prev.op.modifies_memory() {
if let Some(memory) = &prev.memory {
return memory.clone();
}
Expand Down Expand Up @@ -446,8 +442,7 @@ impl TracingInspector {
let step = &mut self.traces.arena[trace_idx].trace.steps[step_idx];

if self.config.record_stack_snapshots.is_pushes() {
let num_pushed = stack_push_count(step.op);
let start = interp.stack.len() - num_pushed;
let start = interp.stack.len() - step.op.outputs() as usize;
step.push_stack = Some(interp.stack.data()[start..].to_vec());
}

Expand Down

0 comments on commit a128cf5

Please sign in to comment.