Skip to content

Commit

Permalink
use single tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Mar 25, 2024
1 parent 595b931 commit c0fbdc7
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 73 deletions.
2 changes: 1 addition & 1 deletion modules/evm/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ sp_api::decl_runtime_apis! {
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<primitives::evm::tracing::Step>, sp_runtime::DispatchError>;
) -> Result<primitives::evm::tracing::VMTrace, sp_runtime::DispatchError>;
}
}
2 changes: 1 addition & 1 deletion modules/evm/src/runner/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ macro_rules! event {
#[cfg(feature = "tracing")]
{
use crate::runner::tracing::{self, Event::*, EventListener};
tracing::call_tracer_with(|tracer| {
tracing::with(|tracer| {
EventListener::event(tracer, $event);
});
}
Expand Down
86 changes: 40 additions & 46 deletions modules/evm/src/runner/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use module_evm_utility::{
use sp_core::{H160, H256, U256};
use sp_std::prelude::*;

pub use primitives::evm::tracing::{CallTrace, CallType, Step};
pub use primitives::evm::tracing::{CallTrace, CallType, Step, VMTrace};

#[derive(Debug, Copy, Clone)]
pub enum Event<'a> {
Expand Down Expand Up @@ -87,18 +87,20 @@ pub enum Event<'a> {
},
}

pub struct CallTracer {
pub struct Tracer {
events: Vec<CallTrace>,
stack: Vec<CallTrace>,
steps: Vec<Step>,
opcode: Option<Opcode>,
snapshot: Option<evm_gasometer::Snapshot>,
}

impl CallTracer {
impl Tracer {
pub fn new() -> Self {
Self {
events: Vec::new(),
stack: Vec::new(),
steps: Vec::new(),
opcode: None,
snapshot: None,
}
Expand All @@ -108,6 +110,10 @@ impl CallTracer {
self.events.drain(..).rev().collect()
}

pub fn steps(&self) -> Vec<Step> {
self.steps.clone()
}

fn call_type(&self) -> CallType {
match self.opcode {
Some(Opcode::CALLCODE) => CallType::CALLCODE,
Expand All @@ -121,8 +127,30 @@ impl CallTracer {

fn evm_runtime_event(&mut self, event: evm_runtime::tracing::Event) {
match event {
evm_runtime::tracing::Event::Step { opcode, .. } => {
evm_runtime::tracing::Event::Step {
context: _,
opcode,
position,
stack,
memory,
} => {
self.opcode = Some(opcode);
self.steps.push(Step {
op: opcode.stringify().as_bytes().to_vec(),
pc: position.clone().unwrap_or_default() as u64,
depth: self.stack.last().map(|x| x.depth).unwrap_or_default(),
gas: if let Some(snapshot) = self.snapshot {
snapshot.gas()
} else {
0
},
stack: stack.data().clone(),
memory: if memory.is_empty() {
None
} else {
Some(memory.data().clone())
},
})
}
_ => {}
};
Expand All @@ -139,41 +167,11 @@ impl CallTracer {
}
}

pub struct OpcodeTracer {
pub steps: Vec<Step>,
}

impl OpcodeTracer {
pub fn new() -> Self {
Self { steps: Vec::new() }
}
}

impl evm_runtime::tracing::EventListener for OpcodeTracer {
fn event(&mut self, event: evm_runtime::tracing::Event) {
match event {
evm_runtime::tracing::Event::Step {
context: _,
opcode,
position,
stack,
memory,
} => self.steps.push(Step {
op: opcode.stringify().as_bytes().to_vec(),
pc: position.clone().unwrap_or_default() as u64,
stack: stack.data().clone(),
memory: memory.data().clone(),
}),
_ => {}
}
}
}

pub trait EventListener {
fn event(&mut self, event: Event);
}

impl EventListener for CallTracer {
impl EventListener for Tracer {
fn event(&mut self, event: Event) {
match event {
Event::Call {
Expand Down Expand Up @@ -341,7 +339,7 @@ pub struct EvmRuntimeTracer;

impl evm_runtime::tracing::EventListener for EvmRuntimeTracer {
fn event(&mut self, event: evm_runtime::tracing::Event) {
call_tracer::with(|tracer| {
tracer::with(|tracer| {
tracer.evm_runtime_event(event);
});
}
Expand All @@ -351,28 +349,24 @@ pub struct EvmGasometerTracer;

impl evm_gasometer::tracing::EventListener for EvmGasometerTracer {
fn event(&mut self, event: evm_gasometer::tracing::Event) {
call_tracer::with(|tracer| {
tracer::with(|tracer| {
tracer.evm_gasometer_event(event);
});
}
}

environmental::environmental!(call_tracer: CallTracer);
environmental::environmental!(tracer: Tracer);

pub fn call_tracer_using<R, F: FnOnce() -> R>(new: &mut CallTracer, f: F) -> R {
call_tracer::using(new, || {
pub fn using<R, F: FnOnce() -> R>(new: &mut Tracer, f: F) -> R {
tracer::using(new, || {
evm_gasometer::tracing::using(&mut EvmGasometerTracer, || {
evm_runtime::tracing::using(&mut EvmRuntimeTracer, f)
})
})
}

pub(crate) fn call_tracer_with<F: FnOnce(&mut CallTracer)>(f: F) {
call_tracer::with(f);
}

pub fn opcode_tracer_using<R, F: FnOnce() -> R>(new: &mut OpcodeTracer, f: F) -> R {
evm_runtime::tracing::using(new, f)
pub(crate) fn with<F: FnOnce(&mut Tracer)>(f: F) {
tracer::with(f);
}

trait Stringify {
Expand Down
16 changes: 15 additions & 1 deletion primitives/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,21 @@ pub mod tracing {
pub op: Vec<u8>,
#[codec(compact)]
pub pc: u64,
#[codec(compact)]
pub depth: u32,
#[codec(compact)]
pub gas: u64,
pub stack: Vec<H256>,
pub memory: Vec<u8>,
pub memory: Option<Vec<u8>>,
}

#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct VMTrace {
#[codec(compact)]
pub gas: u64,
pub return_value: H256,
pub struct_logs: Vec<Step>,
}
}
22 changes: 14 additions & 8 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2235,8 +2235,8 @@ sp_api::impl_runtime_apis! {
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<module_evm::runner::tracing::CallTrace>, sp_runtime::DispatchError> {
let mut tracer = module_evm::runner::tracing::CallTracer::new();
module_evm::runner::tracing::call_tracer_using(&mut tracer, || {
let mut tracer = module_evm::runner::tracing::Tracer::new();
module_evm::runner::tracing::using(&mut tracer, || {
if to == H160::zero() {
<Runtime as module_evm::Config>::Runner::rpc_create(
from,
Expand Down Expand Up @@ -2271,9 +2271,11 @@ sp_api::impl_runtime_apis! {
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<module_evm::runner::tracing::Step>, sp_runtime::DispatchError> {
let mut tracer = module_evm::runner::tracing::OpcodeTracer::new();
module_evm::runner::tracing::opcode_tracer_using(&mut tracer, || {
) -> Result<module_evm::runner::tracing::VMTrace, sp_runtime::DispatchError> {
use sp_core::H256;
use sp_runtime::traits::UniqueSaturatedInto;
let mut tracer = module_evm::runner::tracing::Tracer::new();
module_evm::runner::tracing::using(&mut tracer, || {
if to == H160::zero() {
<Runtime as module_evm::Config>::Runner::rpc_create(
from,
Expand All @@ -2283,7 +2285,7 @@ sp_api::impl_runtime_apis! {
storage_limit,
access_list.unwrap_or_default().into_iter().map(|v| (v.address, v.storage_keys)).collect(),
<Runtime as module_evm::Config>::config(),
).map(drop)
).map(|res| (H256::from(res.value), UniqueSaturatedInto::<u64>::unique_saturated_into(res.used_gas)))
} else {
<Runtime as module_evm::Config>::Runner::rpc_call(
from,
Expand All @@ -2295,9 +2297,13 @@ sp_api::impl_runtime_apis! {
storage_limit,
access_list.unwrap_or_default().into_iter().map(|v| (v.address, v.storage_keys)).collect(),
<Runtime as module_evm::Config>::config(),
).map(drop)
).map(|res| (H256::from_slice(&res.value), UniqueSaturatedInto::<u64>::unique_saturated_into(res.used_gas)))
}
}).map(|_| tracer.steps)
}).map(|(return_value, gas) | module_evm::runner::tracing::VMTrace {
gas,
return_value,
struct_logs: tracer.steps(),
})
}
}

Expand Down
22 changes: 14 additions & 8 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,8 +2237,8 @@ impl_runtime_apis! {
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<module_evm::runner::tracing::CallTrace>, sp_runtime::DispatchError> {
let mut tracer = module_evm::runner::tracing::CallTracer::new();
module_evm::runner::tracing::call_tracer_using(&mut tracer, || {
let mut tracer = module_evm::runner::tracing::Tracer::new();
module_evm::runner::tracing::using(&mut tracer, || {
if to == H160::zero() {
<Runtime as module_evm::Config>::Runner::rpc_create(
from,
Expand Down Expand Up @@ -2273,9 +2273,11 @@ impl_runtime_apis! {
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<module_evm::runner::tracing::Step>, sp_runtime::DispatchError> {
let mut tracer = module_evm::runner::tracing::OpcodeTracer::new();
module_evm::runner::tracing::opcode_tracer_using(&mut tracer, || {
) -> Result<module_evm::runner::tracing::VMTrace, sp_runtime::DispatchError> {
use sp_core::H256;
use sp_runtime::traits::UniqueSaturatedInto;
let mut tracer = module_evm::runner::tracing::Tracer::new();
module_evm::runner::tracing::using(&mut tracer, || {
if to == H160::zero() {
<Runtime as module_evm::Config>::Runner::rpc_create(
from,
Expand All @@ -2285,7 +2287,7 @@ impl_runtime_apis! {
storage_limit,
access_list.unwrap_or_default().into_iter().map(|v| (v.address, v.storage_keys)).collect(),
<Runtime as module_evm::Config>::config(),
).map(drop)
).map(|res| (H256::from(res.value), UniqueSaturatedInto::<u64>::unique_saturated_into(res.used_gas)))
} else {
<Runtime as module_evm::Config>::Runner::rpc_call(
from,
Expand All @@ -2297,9 +2299,13 @@ impl_runtime_apis! {
storage_limit,
access_list.unwrap_or_default().into_iter().map(|v| (v.address, v.storage_keys)).collect(),
<Runtime as module_evm::Config>::config(),
).map(drop)
).map(|res| (H256::from_slice(&res.value), UniqueSaturatedInto::<u64>::unique_saturated_into(res.used_gas)))
}
}).map(|_| tracer.steps)
}).map(|(return_value, gas) | module_evm::runner::tracing::VMTrace {
gas,
return_value,
struct_logs: tracer.steps(),
})
}
}

Expand Down
22 changes: 14 additions & 8 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2441,8 +2441,8 @@ impl_runtime_apis! {
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<module_evm::runner::tracing::CallTrace>, sp_runtime::DispatchError> {
let mut tracer = module_evm::runner::tracing::CallTracer::new();
module_evm::runner::tracing::call_tracer_using(&mut tracer, || {
let mut tracer = module_evm::runner::tracing::Tracer::new();
module_evm::runner::tracing::using(&mut tracer, || {
if to == H160::zero() {
<Runtime as module_evm::Config>::Runner::rpc_create(
from,
Expand Down Expand Up @@ -2477,9 +2477,11 @@ impl_runtime_apis! {
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<module_evm::runner::tracing::Step>, sp_runtime::DispatchError> {
let mut tracer = module_evm::runner::tracing::OpcodeTracer::new();
module_evm::runner::tracing::opcode_tracer_using(&mut tracer, || {
) -> Result<module_evm::runner::tracing::VMTrace, sp_runtime::DispatchError> {
use sp_core::H256;
use sp_runtime::traits::UniqueSaturatedInto;
let mut tracer = module_evm::runner::tracing::Tracer::new();
module_evm::runner::tracing::using(&mut tracer, || {
if to == H160::zero() {
<Runtime as module_evm::Config>::Runner::rpc_create(
from,
Expand All @@ -2489,7 +2491,7 @@ impl_runtime_apis! {
storage_limit,
access_list.unwrap_or_default().into_iter().map(|v| (v.address, v.storage_keys)).collect(),
<Runtime as module_evm::Config>::config(),
).map(drop)
).map(|res| (H256::from(res.value), UniqueSaturatedInto::<u64>::unique_saturated_into(res.used_gas)))
} else {
<Runtime as module_evm::Config>::Runner::rpc_call(
from,
Expand All @@ -2501,9 +2503,13 @@ impl_runtime_apis! {
storage_limit,
access_list.unwrap_or_default().into_iter().map(|v| (v.address, v.storage_keys)).collect(),
<Runtime as module_evm::Config>::config(),
).map(drop)
).map(|res| (H256::from_slice(&res.value), UniqueSaturatedInto::<u64>::unique_saturated_into(res.used_gas)))
}
}).map(|_| tracer.steps)
}).map(|(return_value, gas) | module_evm::runner::tracing::VMTrace {
gas,
return_value,
struct_logs: tracer.steps(),
})
}
}

Expand Down

0 comments on commit c0fbdc7

Please sign in to comment.