Skip to content

Commit

Permalink
astar-3 tracing patch init
Browse files Browse the repository at this point in the history
  • Loading branch information
akru committed Jul 27, 2023
1 parent 389fc60 commit c456483
Show file tree
Hide file tree
Showing 15 changed files with 1,411 additions and 0 deletions.
7 changes: 7 additions & 0 deletions runtime/astar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ pallet-block-reward = { path = "../../frame/block-reward", default-features = fa
pallet-custom-signatures = { path = "../../frame/custom-signatures", default-features = false }
pallet-precompile-staking = { path = "../../precompiles/staking", default-features = false }

# tracing
moonbeam-evm-tracer = { path = "../../vendor/runtime/evm-tracer", default-features = false }
moonbeam-rpc-primitives-debug = { path = "../../vendor/primitives/debug", default-features = false }
moonbeam-rpc-primitives-txpool = { path = "../../vendor/primitives/txpool", default-features = false }
evm-tracing-events = { path = "../../vendor/primitives/evm-tracing-events", default-features = false }
moonbeam-primitives-ext = { path = "../../vendor/runtime/ext", default-features = false }

[build-dependencies]
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }

Expand Down
89 changes: 89 additions & 0 deletions runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,95 @@ impl_runtime_apis! {
Ok(batches)
}
}

impl moonbeam_rpc_primitives_debug::DebugRuntimeApi<Block> for Runtime {
fn trace_transaction(
extrinsics: Vec<<Block as BlockT>::Extrinsic>,
traced_transaction: &pallet_ethereum::Transaction,
) -> Result<
(),
sp_runtime::DispatchError,
> {
use moonbeam_evm_tracer::tracer::EvmTracer;

// Apply the a subset of extrinsics: all the substrate-specific or ethereum
// transactions that preceded the requested transaction.
for ext in extrinsics.into_iter() {
let _ = match &ext.0.function {
RuntimeCall::Ethereum(pallet_ethereum::Call::transact { transaction }) => {
if transaction == traced_transaction {
EvmTracer::new().trace(|| Executive::apply_extrinsic(ext));
return Ok(());
} else {
Executive::apply_extrinsic(ext)
}
}
_ => Executive::apply_extrinsic(ext),
};
}
Err(sp_runtime::DispatchError::Other(
"Failed to find Ethereum transaction among the extrinsics.",
))
}

fn trace_block(
extrinsics: Vec<<Block as BlockT>::Extrinsic>,
known_transactions: Vec<H256>,
) -> Result<
(),
sp_runtime::DispatchError,
> {
use moonbeam_evm_tracer::tracer::EvmTracer;

let mut config = <Runtime as pallet_evm::Config>::config().clone();
config.estimate = true;

// Apply all extrinsics. Ethereum extrinsics are traced.
for ext in extrinsics.into_iter() {
match &ext.0.function {
RuntimeCall::Ethereum(pallet_ethereum::Call::transact { transaction }) => {
if known_transactions.contains(&transaction.hash()) {
// Each known extrinsic is a new call stack.
EvmTracer::emit_new();
EvmTracer::new().trace(|| Executive::apply_extrinsic(ext));
} else {
let _ = Executive::apply_extrinsic(ext);
}
}
_ => {
let _ = Executive::apply_extrinsic(ext);
}
};
}

Ok(())
}
}

impl moonbeam_rpc_primitives_txpool::TxPoolRuntimeApi<Block> for Runtime {
fn extrinsic_filter(
xts_ready: Vec<<Block as BlockT>::Extrinsic>,
xts_future: Vec<<Block as BlockT>::Extrinsic>,
) -> moonbeam_rpc_primitives_txpool::TxPoolResponse {
moonbeam_rpc_primitives_txpool::TxPoolResponse {
ready: xts_ready
.into_iter()
.filter_map(|xt| match xt.0.function {
RuntimeCall::Ethereum(pallet_ethereum::Call::transact { transaction }) => Some(transaction),
_ => None,
})
.collect(),
future: xts_future
.into_iter()
.filter_map(|xt| match xt.0.function {
RuntimeCall::Ethereum(pallet_ethereum::Call::transact { transaction }) => Some(transaction),
_ => None,
})
.collect(),
}
}
}

}

struct CheckInherents;
Expand Down
41 changes: 41 additions & 0 deletions vendor/primitives/debug/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "moonbeam-rpc-primitives-debug"
authors = ["PureStake"]
edition = "2021"
homepage = "https://moonbeam.network"
license = "GPL-3.0-only"
repository = "https://github.com/PureStake/moonbeam/"
version = "0.1.0"

[dependencies]
environmental = { workspace = true }
ethereum = { workspace = true, features = ["with-codec"] }
ethereum-types = { workspace = true }
hex = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }

# Substrate
parity-scale-codec = { workspace = true }
sp-api = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"environmental/std",
"ethereum-types/std",
"ethereum/std",
"hex",
"serde",
"serde_json",
"sp-api/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
66 changes: 66 additions & 0 deletions vendor/primitives/debug/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]

use ethereum::{TransactionV0 as LegacyTransaction, TransactionV2 as Transaction};
use ethereum_types::H256;
use parity_scale_codec::{Decode, Encode};
use sp_std::vec::Vec;

sp_api::decl_runtime_apis! {
// Api version is virtually 4.
//
// We realized that even using runtime overrides, using the ApiExt interface reads the api
// versions from the state runtime, meaning we cannot just reset the versioning as we see fit.
//
// In order to be able to use ApiExt as part of the RPC handler logic we need to be always
// above the version that exists on chain for this Api, even if this Api is only meant
// to be used overridden.
#[api_version(4)]
pub trait DebugRuntimeApi {
#[changed_in(4)]
fn trace_transaction(
extrinsics: Vec<Block::Extrinsic>,
transaction: &LegacyTransaction,
) -> Result<(), sp_runtime::DispatchError>;

fn trace_transaction(
extrinsics: Vec<Block::Extrinsic>,
transaction: &Transaction,
) -> Result<(), sp_runtime::DispatchError>;

fn trace_block(
extrinsics: Vec<Block::Extrinsic>,
known_transactions: Vec<H256>,
) -> Result<(), sp_runtime::DispatchError>;
}
}

#[derive(Clone, Copy, Eq, PartialEq, Debug, Encode, Decode)]
pub enum TracerInput {
None,
Blockscout,
CallTracer,
}

/// DebugRuntimeApi V2 result. Trace response is stored in client and runtime api call response is
/// empty.
#[derive(Debug)]
pub enum Response {
Single,
Block,
}
36 changes: 36 additions & 0 deletions vendor/primitives/evm-tracing-events/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "evm-tracing-events"
authors = ["PureStake"]
edition = "2021"
homepage = "https://moonbeam.network"
license = "GPL-3.0-only"
repository = "https://github.com/PureStake/moonbeam/"
version = "0.1.0"

[dependencies]
environmental = { workspace = true }

# Substrate
parity-scale-codec = { workspace = true }
sp-runtime-interface = { workspace = true }

# Ethereum
ethereum = { workspace = true, features = ["with-codec"] }
ethereum-types = { workspace = true }
evm = { workspace = true, features = ["with-codec"] }
evm-gasometer = { workspace = true }
evm-runtime = { workspace = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"environmental/std",
"ethereum-types/std",
"ethereum/std",
"evm-gasometer/std",
"evm-runtime/std",
"evm/std",
"sp-runtime-interface/std",
]
evm-tracing = ["evm-gasometer/tracing", "evm-runtime/tracing", "evm/tracing"]
Loading

0 comments on commit c456483

Please sign in to comment.