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

evm tracing #2716

Merged
merged 14 commits into from
Mar 26, 2024
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"]
27 changes: 27 additions & 0 deletions modules/evm/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,30 @@ sp_api::decl_runtime_apis! {
) -> Result<CreateInfo, sp_runtime::DispatchError>;
}
}

#[cfg(feature = "tracing")]
sp_api::decl_runtime_apis! {
pub trait EVMTraceApi<Balance> where
Balance: Codec + MaybeDisplay + MaybeFromStr,
{
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>;

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>;
}
}
3 changes: 3 additions & 0 deletions modules/evm/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub mod state;
pub mod storage_meter;
pub mod tagged_runtime;

#[cfg(feature = "tracing")]
pub mod tracing;

use crate::{BalanceOf, CallInfo, Config, CreateInfo};
use module_evm_utility::evm;
pub use primitives::evm::{EvmAddress, Vicinity};
Expand Down
57 changes: 22 additions & 35 deletions modules/evm/src/runner/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,17 @@ use sp_runtime::traits::Zero;
use sp_std::{collections::btree_set::BTreeSet, rc::Rc, vec::Vec};

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;
Expand Down Expand Up @@ -212,6 +191,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 @@ -560,7 +545,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 Some(limit) = self.config.max_initcode_size {
Expand Down Expand Up @@ -617,11 +602,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
Loading
Loading