Skip to content

Support evm-tracing for the ethtx-forward #1541

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

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

27 changes: 25 additions & 2 deletions pallet/ethtx-forwarder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ fp-ethereum = { workspace = true }
fp-evm = { workspace = true }
pallet-evm = { workspace = true }

# moonbeam optional
moonbeam-evm-tracer = { workspace = true, optional = true }
xcm-primitives = { workspace = true, optional = true }

# polkadot-sdk
frame-support = { workspace = true }
frame-system = { workspace = true }
Expand All @@ -25,6 +29,7 @@ sp-runtime = { workspace = true }
sp-std = { workspace = true }

[dev-dependencies]
# crates.io
array-bytes = { workspace = true }
libsecp256k1 = { workspace = true, features = ["std"] }
sha3 = { workspace = true }
Expand Down Expand Up @@ -52,6 +57,10 @@ std = [
"fp-evm/std",
"pallet-evm/std",

# moonbeam optional
"moonbeam-evm-tracer?/std",
"xcm-primitives?/std",

# polkadot-sdk
"frame-support/std",
"frame-system/std",
Expand All @@ -61,15 +70,29 @@ std = [
]

runtime-benchmarks = [
# frontier
"pallet-evm/runtime-benchmarks",

# moonbeam
"xcm-primitives?/runtime-benchmarks",

# polkadot-sdk
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-evm/runtime-benchmarks",
]

try-runtime = [
# frontier
"pallet-evm/try-runtime",

# polkadot-sdk
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-evm/try-runtime",
"sp-runtime/try-runtime",
]

evm-tracing = [
# moonbeam
"moonbeam-evm-tracer",
"xcm-primitives",
]
68 changes: 61 additions & 7 deletions pallet/ethtx-forwarder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use fp_ethereum::{TransactionData, ValidatedTransaction};
use fp_evm::{CheckEvmTransaction, CheckEvmTransactionConfig, TransactionValidationError};
use pallet_evm::{FeeCalculator, GasWeightMapping};
// polkadot-sdk
#[cfg(feature = "evm-tracing")]
use frame_support::dispatch::{DispatchErrorWithPostInfo, PostDispatchInfo};
use frame_support::{traits::EnsureOrigin, PalletError};
use sp_core::{Get, H160, H256, U256};
use sp_runtime::{traits::BadOrigin, DispatchError, RuntimeDebug};
Expand Down Expand Up @@ -86,9 +88,6 @@ pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::origin]
pub type Origin = ForwardEthOrigin;

Expand All @@ -106,6 +105,8 @@ pub mod pallet {
ValidationError(TxErrorWrapper),
}

#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::call]
impl<T: Config> Pallet<T>
where
Expand All @@ -121,13 +122,16 @@ pub mod pallet {
request: ForwardRequest,
) -> DispatchResultWithPostInfo {
let source = ensure_forward_transact(origin)?;

let transaction = Self::validated_transaction(source, request)?;
T::ValidatedTransaction::apply(source, transaction).map(|(post_info, _)| post_info)

#[cfg(feature = "evm-tracing")]
return Self::trace_tx(source, transaction);
#[cfg(not(feature = "evm-tracing"))]
return T::ValidatedTransaction::apply(source, transaction)
.map(|(post_info, _)| post_info);
}
}
}

impl<T: Config> Pallet<T> {
/// Calculates the fee for submitting such an EVM transaction.
///
Expand Down Expand Up @@ -254,6 +258,57 @@ impl<T: Config> Pallet<T> {

Ok(transaction)
}

#[cfg(feature = "evm-tracing")]
fn trace_tx(
source: H160,
transaction: Transaction,
) -> Result<PostDispatchInfo, DispatchErrorWithPostInfo> {
// moonbeam
use moonbeam_evm_tracer::tracer::EvmTracer;
// polkadot-sdk
use frame_support::storage::unhashed;
use xcm_primitives::{EthereumXcmTracingStatus, ETHEREUM_XCM_TRACING_STORAGE_KEY};

match unhashed::get(ETHEREUM_XCM_TRACING_STORAGE_KEY) {
Some(EthereumXcmTracingStatus::Block) => {
EvmTracer::emit_new();

let mut res = Ok(PostDispatchInfo::default());

EvmTracer::new().trace(|| {
res = T::ValidatedTransaction::apply(source, transaction)
.map(|(post_info, _)| post_info);
});

res
},
Some(EthereumXcmTracingStatus::Transaction(traced_transaction_hash)) =>
if transaction.hash() == traced_transaction_hash {
let mut res = Ok(PostDispatchInfo::default());

EvmTracer::new().trace(|| {
res = T::ValidatedTransaction::apply(source, transaction)
.map(|(post_info, _)| post_info);
});
unhashed::put::<EthereumXcmTracingStatus>(
ETHEREUM_XCM_TRACING_STORAGE_KEY,
&EthereumXcmTracingStatus::TransactionExited,
);

res
} else {
T::ValidatedTransaction::apply(source, transaction)
.map(|(post_info, _)| post_info)
},
Some(EthereumXcmTracingStatus::TransactionExited) => Ok(PostDispatchInfo {
actual_weight: None,
pays_fee: frame_support::pallet_prelude::Pays::No,
}),
None =>
T::ValidatedTransaction::apply(source, transaction).map(|(post_info, _)| post_info),
}
}
}

// TODO: replace it with upstream error type
Expand All @@ -271,7 +326,6 @@ pub enum TxErrorWrapper {
InvalidSignature,
UnknownError,
}

impl From<TransactionValidationError> for TxErrorWrapper {
fn from(validation_error: TransactionValidationError) -> Self {
match validation_error {
Expand Down
2 changes: 2 additions & 0 deletions runtime/crab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ on-chain-release-build = [
]

evm-tracing = [
# darwinia
"darwinia-ethtx-forwarder/evm-tracing",
# moonbeam optional
"moonbeam-evm-tracer",
]
Expand Down
2 changes: 2 additions & 0 deletions runtime/darwinia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ on-chain-release-build = [
]

evm-tracing = [
# darwinia
"darwinia-ethtx-forwarder/evm-tracing",
# moonbeam optional
"moonbeam-evm-tracer",
]
Expand Down
2 changes: 2 additions & 0 deletions runtime/koi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ on-chain-release-build = [
]

evm-tracing = [
# darwinia
"darwinia-ethtx-forwarder/evm-tracing",
# moonbeam optional
"moonbeam-evm-tracer",
]
Expand Down