Skip to content

Commit

Permalink
evm tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Feb 22, 2024
1 parent 5ffbaf9 commit 4b4e65b
Show file tree
Hide file tree
Showing 15 changed files with 804 additions and 51 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions modules/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ serde_json = { workspace = true, features = ["alloc"], optional = true }
hex = { workspace = true, features = ["alloc"], optional = true }
num = { workspace = true, features = ["alloc"] }
bn = { workspace = true }
environmental = { version = "1.1.4", default-features = false, optional = true }

frame-support = { workspace = true }
frame-system = { workspace = true }
Expand Down Expand Up @@ -60,7 +61,7 @@ pallet-utility = { workspace = true, features = ["std"] }
default = ["std"]
std = [
"serde/std",

"environmental/std",
"parity-scale-codec/std",
"frame-support/std",
"frame-system/std",
Expand Down Expand Up @@ -100,7 +101,7 @@ try-runtime = [
"pallet-balances/try-runtime",
"pallet-timestamp/try-runtime",
]
tracing = ["module-evm-utility/tracing"]
tracing = ["environmental", "primitives/tracing", "module-evm-utility/tracing"]
wasm-bench = [
"wasm-bencher/wasm-bench",
"hex",
Expand Down
1 change: 1 addition & 0 deletions modules/evm/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ std = [
"sp-core/std",
"primitives/std",
]
tracing = ["primitives/tracing"]
22 changes: 22 additions & 0 deletions modules/evm/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,27 @@ sp_api::decl_runtime_apis! {
fn get_estimate_resources_request(data: Vec<u8>) -> Result<EstimateResourcesRequest, sp_runtime::DispatchError>;

fn block_limits() -> BlockLimits;

#[cfg(feature = "tracing")]
fn trace_call(
from: H160,
to: H160,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<primitives::evm::tracing::CallTrace>, sp_runtime::DispatchError>;

#[cfg(feature = "tracing")]
fn trace_vm(
from: H160,
to: H160,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
) -> Result<Vec<primitives::evm::tracing::Step>, sp_runtime::DispatchError>;
}
}
2 changes: 2 additions & 0 deletions modules/evm/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
pub mod stack;
pub mod state;
pub mod storage_meter;
#[cfg(feature = "tracing")]
pub mod tracing;

use crate::{BalanceOf, CallInfo, Config, CreateInfo};
use module_evm_utility::evm;
Expand Down
71 changes: 22 additions & 49 deletions modules/evm/src/runner/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,18 @@ use sp_std::{
};

macro_rules! event {
($x:expr) => {};
}

#[cfg(feature = "tracing")]
mod tracing {
pub struct Tracer;
impl module_evm_utility::evm::tracing::EventListener for Tracer {
fn event(&mut self, event: module_evm_utility::evm::tracing::Event) {
frame_support::log::debug!(
target: "evm", "evm tracing: {:?}", event
);
}
}
impl module_evm_utility::evm_runtime::tracing::EventListener for Tracer {
fn event(&mut self, event: module_evm_utility::evm_runtime::tracing::Event) {
frame_support::log::debug!(
target: "evm", "evm_runtime tracing: {:?}", event
);
}
}
impl module_evm_utility::evm_gasometer::tracing::EventListener for Tracer {
fn event(&mut self, event: module_evm_utility::evm_gasometer::tracing::Event) {
frame_support::log::debug!(
target: "evm", "evm_gasometer tracing: {:?}", event
);
($event:expr) => {{
#[cfg(feature = "tracing")]
{
use crate::runner::tracing::{self, Event::*, EventListener};
tracing::call_tracer_with(|tracer| {
EventListener::event(tracer, $event);
});
}
}
}};
}

#[cfg(feature = "tracing")]
use tracing::*;

macro_rules! emit_exit {
($reason:expr) => {{
let reason = $reason;
event!(Exit {
reason: &reason,
return_value: &Vec::new(),
});
reason
}};
($reason:expr, $return_value:expr) => {{
let reason = $reason;
let return_value = $return_value;
Expand Down Expand Up @@ -209,6 +180,12 @@ impl<'config> StackSubstateMetadata<'config> {
}

pub fn spit_child(&self, gas_limit: u64, is_static: bool) -> Self {
event!(Enter {
depth: match self.depth {
None => 0,
Some(n) => n + 1,
} as u32
});
Self {
gasometer: Gasometer::new(gas_limit, self.gasometer.config()),
storage_meter: StorageMeter::new(self.storage_meter.available_storage()),
Expand Down Expand Up @@ -516,7 +493,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> StackExecu
value,
init_code: &init_code,
gas_limit,
address: self.create_address(CreateScheme::Legacy { caller }),
address: self.create_address(CreateScheme::Legacy { caller }).unwrap_or_default(),
});

if let Err(e) = self.record_create_transaction_cost(&init_code, &access_list) {
Expand Down Expand Up @@ -554,11 +531,13 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> StackExecu
init_code: &init_code,
salt,
gas_limit,
address: self.create_address(CreateScheme::Create2 {
caller,
code_hash,
salt,
}),
address: self
.create_address(CreateScheme::Create2 {
caller,
code_hash,
salt,
})
.unwrap_or_default(),
});

if let Err(e) = self.record_create_transaction_cost(&init_code, &access_list) {
Expand Down Expand Up @@ -1063,13 +1042,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> StackExecu

let mut runtime = Runtime::new(Rc::new(code), Rc::new(input), context, self.config);

#[cfg(not(feature = "tracing"))]
let reason = self.execute(&mut runtime);
#[cfg(feature = "tracing")]
//let reason = module_evm_utility::evm::tracing::using(&mut Tracer, || self.execute(&mut runtime));
let reason = module_evm_utility::evm_runtime::tracing::using(&mut Tracer, || self.execute(&mut runtime));
//let reason = module_evm_utility::evm_gasometer::tracing::using(&mut Tracer, || self.execute(&mut
// runtime));

log::debug!(target: "evm", "Call execution using address {}: {:?}", code_address, reason);

Expand Down
Loading

0 comments on commit 4b4e65b

Please sign in to comment.