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

chore: bump revm to 9.0 #97

Merged
merged 15 commits into from
May 12, 2024
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ alloy-sol-types = "0.7.2"
alloy-primitives = "0.7.2"
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "f1d7085" }
alloy-rpc-types-trace = { git = "https://github.com/alloy-rs/alloy", rev = "f1d7085" }
revm = { version = "8.0", default-features = false, features = ["std"] }
revm = { version = "9.0", default-features = false, features = ["std"] }

anstyle = "1.0"
colorchoice = "1.0"
thiserror = { version = "1" }
thiserror = "1.0"

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

# js-tracer
boa_engine = { version = "0.18", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion src/access_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ where
match interp.current_opcode() {
opcode::SLOAD | opcode::SSTORE => {
if let Ok(slot) = interp.stack().peek(0) {
let cur_contract = interp.contract.address;
let cur_contract = interp.contract.target_address;
self.access_list
.entry(cur_contract)
.or_default()
Expand Down
13 changes: 2 additions & 11 deletions src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,10 @@ where
}
}

fn step_end(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
fn step_end(&mut self, interp: &mut Interpreter, _context: &mut EvmContext<DB>) {
// update gas usage for the last opcode
if let Some((opcode, gas_remaining)) = self.last_opcode_gas_remaining.take() {
let gas_table =
revm::interpreter::instructions::opcode::spec_opcode_gas(context.spec_id());
let opcode_gas_info = gas_table[opcode.get() as usize];

let mut gas_cost = opcode_gas_info.get_gas() as u64;
// if gas cost is 0 then this is dynamic gas and we need to use the tracked gas
if gas_cost == 0 {
gas_cost = gas_remaining.saturating_sub(interp.gas().remaining());
}

let gas_cost = gas_remaining.saturating_sub(interp.gas().remaining());
*self.opcode_gas.entry(opcode).or_default() += gas_cost;
}
}
Expand Down
23 changes: 4 additions & 19 deletions src/tracing/builder/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use alloy_rpc_types::TransactionInfo;
use alloy_rpc_types_trace::parity::*;
use revm::{
db::DatabaseRef,
interpreter::{
opcode::{self, spec_opcode_gas},
OpCode,
},
interpreter::{opcode, OpCode},
primitives::{Account, ExecutionResult, ResultAndState, SpecId, KECCAK_EMPTY},
};
use std::collections::{HashSet, VecDeque};
Expand All @@ -24,21 +21,16 @@ use std::collections::{HashSet, VecDeque};
pub struct ParityTraceBuilder {
/// Recorded trace nodes
nodes: Vec<CallTraceNode>,
/// The spec id of the EVM.
spec_id: Option<SpecId>,

/// How the traces were recorded
_config: TracingInspectorConfig,
}

impl ParityTraceBuilder {
/// Returns a new instance of the builder
pub fn new(
nodes: Vec<CallTraceNode>,
spec_id: Option<SpecId>,
_spec_id: Option<SpecId>,
_config: TracingInspectorConfig,
) -> Self {
Self { nodes, spec_id, _config }
Self { nodes }
}

/// Returns a list of all addresses that appeared as callers.
Expand Down Expand Up @@ -396,16 +388,9 @@ impl ParityTraceBuilder {
store: maybe_storage,
});

let cost = self
.spec_id
.and_then(|spec_id| {
spec_opcode_gas(spec_id).get(step.op.get() as usize).map(|op| op.get_gas())
})
.unwrap_or_default();

VmInstruction {
pc: step.pc,
cost: cost as u64,
cost: step.gas_cost,
ex: maybe_execution,
sub: maybe_sub_call,
op: Some(step.op.to_string()),
Expand Down
65 changes: 33 additions & 32 deletions src/tracing/js/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,29 @@ struct GuardedNullableGc<Val: 'static> {
impl<Val: 'static> GuardedNullableGc<Val> {
/// Creates a garbage collectible value to the given reference.
///
/// SAFETY; the caller must ensure that the guard is dropped before the value is dropped.
fn r#ref(val: &Val) -> (Self, GcGuard<'_, Val>) {
/// # Safety
///
/// The caller must ensure that the guard is dropped before the value is dropped.
fn new_ref(val: &Val) -> (Self, GcGuard<'_, Val>) {
Self::new(Guarded::Ref(val))
}

/// Creates a garbage collectible value to the given reference.
///
/// SAFETY; the caller must ensure that the guard is dropped before the value is dropped.
fn r#owned<'a>(val: Val) -> (Self, GcGuard<'a, Val>) {
/// # Safety
///
/// The caller must ensure that the guard is dropped before the value is dropped.
fn new_owned<'a>(val: Val) -> (Self, GcGuard<'a, Val>) {
Self::new(Guarded::Owned(val))
}

fn new(val: Guarded<'_, Val>) -> (Self, GcGuard<'_, Val>) {
let inner = Rc::new(RefCell::new(Some(val)));
let guard = GcGuard { inner: Rc::clone(&inner) };

// SAFETY: guard enforces that the value is removed from the refcell before it is dropped
let this = Self {
inner: unsafe {
#[allow(clippy::missing_transmute_annotations)]
std::mem::transmute(inner)
},
};
// SAFETY: guard enforces that the value is removed from the refcell before it is dropped.
#[allow(clippy::missing_transmute_annotations)]
let this = Self { inner: unsafe { std::mem::transmute(inner) } };

(this, guard)
}
Expand All @@ -111,10 +111,7 @@ impl<Val: 'static> GuardedNullableGc<Val> {
where
F: FnOnce(&Val) -> R,
{
self.inner.borrow().as_ref().map(|val| match val {
Guarded::Ref(val) => f(val),
Guarded::Owned(val) => f(val),
})
self.inner.borrow().as_ref().map(|guard| f(guard.as_ref()))
}
}

Expand All @@ -131,6 +128,16 @@ enum Guarded<'a, T> {
Owned(T),
}

impl<T> Guarded<'_, T> {
#[inline]
fn as_ref(&self) -> &T {
match self {
Guarded::Ref(val) => val,
Guarded::Owned(val) => val,
}
}
}

/// Guard the inner value, once this value is dropped the inner value is also removed.
///
/// This type guarantees that it never outlives the wrapped value.
Expand Down Expand Up @@ -232,7 +239,7 @@ pub(crate) struct MemoryRef(GuardedNullableGc<SharedMemory>);
impl MemoryRef {
/// Creates a new stack reference
pub(crate) fn new(mem: &SharedMemory) -> (Self, GcGuard<'_, SharedMemory>) {
let (inner, guard) = GuardedNullableGc::r#ref(mem);
let (inner, guard) = GuardedNullableGc::new_ref(mem);
(Self(inner), guard)
}

Expand Down Expand Up @@ -324,7 +331,7 @@ pub(crate) struct StateRef(GuardedNullableGc<State>);
impl StateRef {
/// Creates a new stack reference
pub(crate) fn new(state: &State) -> (Self, GcGuard<'_, State>) {
let (inner, guard) = GuardedNullableGc::r#ref(state);
let (inner, guard) = GuardedNullableGc::new_ref(state);
(Self(inner), guard)
}

Expand All @@ -349,7 +356,7 @@ where
{
/// Creates a new stack reference
fn new<'a>(db: DB) -> (Self, GcGuard<'a, DB>) {
let (inner, guard) = GuardedNullableGc::owned(db);
let (inner, guard) = GuardedNullableGc::new_owned(db);
(Self(inner), guard)
}
}
Expand Down Expand Up @@ -417,7 +424,7 @@ pub(crate) struct StackRef(GuardedNullableGc<Stack>);
impl StackRef {
/// Creates a new stack reference
pub(crate) fn new(stack: &Stack) -> (Self, GcGuard<'_, Stack>) {
let (inner, guard) = GuardedNullableGc::r#ref(stack);
let (inner, guard) = GuardedNullableGc::new_ref(stack);
(Self(inner), guard)
}

Expand Down Expand Up @@ -790,21 +797,15 @@ impl EvmDbRef {
return JsArrayBuffer::new(0, ctx);
}

let res = self
.inner
.db
.0
.with_inner(|db| db.code_by_hash_ref(code_hash).map(|code| code.bytecode));
let code = match res {
Some(Ok(code)) => code,
_ => {
return Err(JsError::from_native(JsNativeError::error().with_message(format!(
"Failed to read code hash {code_hash:?} from database",
))))
}
let Some(Ok(bytecode)) = self.inner.db.0.with_inner(|db| db.code_by_hash_ref(code_hash))
else {
return Err(JsError::from_native(
JsNativeError::error()
.with_message(format!("Failed to read code hash {code_hash:?} from database")),
));
};

to_buf(code.to_vec(), ctx)
to_buf(bytecode.bytecode().to_vec(), ctx)
}

fn read_state(
Expand Down
12 changes: 6 additions & 6 deletions src/tracing/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl JsInspector {
to = Some(target);
"CALL"
}
TransactTo::Create(_) => "CREATE",
TransactTo::Create => "CREATE",
}
.to_string(),
from: env.tx.caller,
Expand Down Expand Up @@ -446,19 +446,19 @@ where
self.register_precompiles(&context.precompiles);

// determine correct `from` and `to` based on the call scheme
let (from, to) = match inputs.context.scheme {
let (from, to) = match inputs.scheme {
CallScheme::DelegateCall | CallScheme::CallCode => {
(inputs.context.address, inputs.context.code_address)
(inputs.target_address, inputs.bytecode_address)
}
_ => (inputs.context.caller, inputs.context.address),
_ => (inputs.caller, inputs.bytecode_address),
};

let value = inputs.transfer.value;
let value = inputs.transfer_value().unwrap_or_default();
self.push_call(
to,
inputs.input.clone(),
value,
inputs.context.scheme.into(),
inputs.scheme.into(),
from,
inputs.gas_limit,
);
Expand Down
20 changes: 10 additions & 10 deletions src/tracing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl TracingInspector {
&self,
context: &EvmContext<DB>,
to: &Address,
value: U256,
value: &U256,
) -> bool {
if context.precompiles.contains_key(to) {
// only if this is _not_ the root call
Expand Down Expand Up @@ -357,7 +357,7 @@ impl TracingInspector {
depth: context.journaled_state.depth(),
pc: interp.program_counter(),
op,
contract: interp.contract.address,
contract: interp.contract.target_address,
stack,
push_stack: None,
memory_size: memory.len(),
Expand Down Expand Up @@ -477,36 +477,36 @@ where
self.gas_inspector.call(context, inputs);

// determine correct `from` and `to` based on the call scheme
let (from, to) = match inputs.context.scheme {
let (from, to) = match inputs.scheme {
CallScheme::DelegateCall | CallScheme::CallCode => {
(inputs.context.address, inputs.context.code_address)
(inputs.target_address, inputs.bytecode_address)
}
_ => (inputs.context.caller, inputs.context.address),
_ => (inputs.caller, inputs.target_address),
};

let value = if matches!(inputs.context.scheme, CallScheme::DelegateCall) {
let value = if matches!(inputs.scheme, CallScheme::DelegateCall) {
// for delegate calls we need to use the value of the top trace
if let Some(parent) = self.active_trace() {
parent.trace.value
} else {
inputs.transfer.value
inputs.call_value()
}
} else {
inputs.transfer.value
inputs.call_value()
};

// if calls to precompiles should be excluded, check whether this is a call to a precompile
let maybe_precompile = self
.config
.exclude_precompile_calls
.then(|| self.is_precompile_call(context, &to, value));
.then(|| self.is_precompile_call(context, &to, &value));

self.start_trace_on_call(
context,
to,
inputs.input.clone(),
value,
inputs.context.scheme.into(),
inputs.scheme.into(),
from,
inputs.gas_limit,
maybe_precompile,
Expand Down
4 changes: 1 addition & 3 deletions src/tracing/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloy_rpc_types_trace::{
SelfdestructAction, TraceOutput, TransactionTrace,
},
};
use revm::interpreter::{opcode, CallContext, CallScheme, CreateScheme, InstructionResult, OpCode};
use revm::interpreter::{opcode, CallScheme, CreateScheme, InstructionResult, OpCode};
use std::collections::VecDeque;

/// A trace of a call.
Expand Down Expand Up @@ -55,8 +55,6 @@ pub struct CallTrace {
pub gas_limit: u64,
/// The final status of the call.
pub status: InstructionResult,
/// call context of the runtime
pub call_context: Option<Box<CallContext>>,
/// Opcode-level execution steps.
pub steps: Vec<CallTraceStep>,
}
Expand Down
8 changes: 4 additions & 4 deletions src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ where
return None;
}

if !inputs.transfer.value.is_zero() {
if inputs.transfers_value() {
self.transfers.push(TransferOperation {
kind: TransferKind::Call,
from: inputs.transfer.source,
to: inputs.transfer.target,
value: inputs.transfer.value,
from: inputs.caller,
to: inputs.target_address,
value: inputs.call_value(),
});
}

Expand Down
5 changes: 2 additions & 3 deletions tests/it/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use alloy_rpc_types_trace::geth::{
};
use revm::{
db::{CacheDB, EmptyDB},
interpreter::CreateScheme,
primitives::{
BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ExecutionResult, HandlerCfg,
Output, SpecId, TransactTo, TxEnv,
Expand Down Expand Up @@ -66,7 +65,7 @@ fn test_geth_calltracer_logs() {
TxEnv {
caller: deployer,
gas_limit: 1000000,
transact_to: TransactTo::Create(CreateScheme::Create),
transact_to: TransactTo::Create,
data: code.into(),
..Default::default()
},
Expand Down Expand Up @@ -175,7 +174,7 @@ fn test_geth_mux_tracer() {
TxEnv {
caller: deployer,
gas_limit: 1000000,
transact_to: TransactTo::Create(CreateScheme::Create),
transact_to: TransactTo::Create,
data: code.into(),
..Default::default()
},
Expand Down
Loading
Loading