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

New substrate event ExecutedFromXcm to link xcm message hash with ethereum tx hash #2874

Merged
merged 4 commits into from
Jul 25, 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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions pallets/ethereum-xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ethereum = { workspace = true, features = [ "with-codec" ] }
serde = { workspace = true, optional = true }

# Parity
environmental = { workspace = true }
ethereum-types = { workspace = true }
parity-scale-codec = { workspace = true }
rlp = { workspace = true }
Expand Down Expand Up @@ -70,6 +71,7 @@ std = [
"pallet-evm/std",
"pallet-timestamp/std",
# Parity
"environmental/std",
"parity-scale-codec/std",
"rlp/std",
"scale-info/std",
Expand Down
44 changes: 42 additions & 2 deletions pallets/ethereum-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ mod mock;
#[cfg(all(feature = "std", test))]
mod tests;

use ethereum_types::{H160, U256};
use ethereum_types::{H160, H256, U256};
use fp_ethereum::{TransactionData, ValidatedTransaction};
use fp_evm::{CheckEvmTransaction, CheckEvmTransactionConfig, TransactionValidationError};
use frame_support::{
dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo},
traits::{EnsureOrigin, Get},
traits::{EnsureOrigin, Get, ProcessMessage},
weights::Weight,
};
use frame_system::pallet_prelude::OriginFor;
Expand Down Expand Up @@ -83,6 +83,25 @@ impl<O: Into<Result<RawOrigin, O>> + From<RawOrigin>> EnsureOrigin<O>
}
}

environmental::environmental!(XCM_MESSAGE_HASH: H256);

pub struct MessageProcessorWrapper<Inner>(core::marker::PhantomData<Inner>);
impl<Inner: ProcessMessage> ProcessMessage for MessageProcessorWrapper<Inner> {
type Origin = <Inner as ProcessMessage>::Origin;

fn process_message(
message: &[u8],
origin: Self::Origin,
meter: &mut frame_support::weights::WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, frame_support::traits::ProcessMessageError> {
let mut xcm_msg_hash = H256(sp_io::hashing::blake2_256(message));
XCM_MESSAGE_HASH::using(&mut xcm_msg_hash, || {
Inner::process_message(message, origin, meter, id)
})
}
}

pub use self::pallet::*;

#[frame_support::pallet(dev_mode)]
Expand All @@ -92,6 +111,8 @@ pub mod pallet {

#[pallet::config]
pub trait Config: frame_system::Config + pallet_evm::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Invalid transaction error
type InvalidEvmTransactionError: From<TransactionValidationError>;
/// Handler for applying an already validated transaction
Expand Down Expand Up @@ -131,6 +152,16 @@ pub mod pallet {
EthereumXcmExecutionSuspended,
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T> {
/// Ethereum transaction executed from XCM
ExecutedFromXcm {
xcm_msg_hash: H256,
eth_tx_hash: H256,
},
}

#[pallet::call]
impl<T: Config> Pallet<T>
where
Expand Down Expand Up @@ -292,6 +323,7 @@ impl<T: Config> Pallet<T> {
let transaction: Option<Transaction> =
xcm_transaction.into_transaction_v2(current_nonce, T::ChainId::get(), allow_create);
if let Some(transaction) = transaction {
let tx_hash = transaction.hash();
let transaction_data: TransactionData = (&transaction).into();

let (weight_limit, proof_size_base_cost) =
Expand Down Expand Up @@ -339,6 +371,14 @@ impl<T: Config> Pallet<T> {

let (dispatch_info, _) =
T::ValidatedTransaction::apply(source, transaction, maybe_force_create_address)?;

XCM_MESSAGE_HASH::with(|xcm_msg_hash| {
Self::deposit_event(Event::ExecutedFromXcm {
xcm_msg_hash: *xcm_msg_hash,
eth_tx_hash: tx_hash,
});
});

Ok(dispatch_info)
} else {
Err(sp_runtime::DispatchErrorWithPostInfo {
Expand Down
1 change: 1 addition & 0 deletions pallets/ethereum-xcm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ impl xcm_primitives::EnsureProxy<AccountId32> for EthereumXcmEnsureProxy {
}

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type InvalidEvmTransactionError = pallet_ethereum::InvalidTransactionWrapper;
type ValidatedTransaction = pallet_ethereum::ValidatedTransaction<Self>;
type XcmEthereumOrigin = crate::EnsureXcmEthereumTransaction;
Expand Down
3 changes: 2 additions & 1 deletion runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ impl xcm_primitives::EnsureProxy<AccountId> for EthereumXcmEnsureProxy {
}

impl pallet_ethereum_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type InvalidEvmTransactionError = pallet_ethereum::InvalidTransactionWrapper;
type ValidatedTransaction = pallet_ethereum::ValidatedTransaction<Self>;
type XcmEthereumOrigin = pallet_ethereum_xcm::EnsureXcmEthereumTransaction;
Expand Down Expand Up @@ -1420,7 +1421,7 @@ construct_runtime! {
// Previously 35: BaseFee
// Previously 36: pallet_assets::<Instance1>
MoonbeamOrbiters: pallet_moonbeam_orbiters::{Pallet, Call, Storage, Event<T>, Config<T>} = 37,
EthereumXcm: pallet_ethereum_xcm::{Pallet, Call, Storage, Origin} = 38,
EthereumXcm: pallet_ethereum_xcm::{Pallet, Call, Storage, Origin, Event<T>} = 38,
Randomness: pallet_randomness::{Pallet, Call, Storage, Event<T>, Inherent} = 39,
TreasuryCouncilCollective:
pallet_collective::<Instance3>::{Pallet, Call, Storage, Event<T>, Origin<T>, Config<T>} = 40,
Expand Down
5 changes: 3 additions & 2 deletions runtime/moonbase/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,9 @@ impl pallet_message_queue::Config for Runtime {
cumulus_primitives_core::AggregateMessageOrigin,
>;
#[cfg(not(feature = "runtime-benchmarks"))]
type MessageProcessor =
xcm_builder::ProcessXcmMessage<AggregateMessageOrigin, XcmExecutor, RuntimeCall>;
type MessageProcessor = pallet_ethereum_xcm::MessageProcessorWrapper<
xcm_builder::ProcessXcmMessage<AggregateMessageOrigin, XcmExecutor, RuntimeCall>,
>;
type Size = u32;
type HeapSize = MessageQueueHeapSize;
type MaxStale = MessageQueueMaxStale;
Expand Down
1 change: 1 addition & 0 deletions runtime/moonbase/tests/xcm_mock/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ impl xcm_primitives::EnsureProxy<AccountId> for EthereumXcmEnsureProxy {
}

impl pallet_ethereum_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type InvalidEvmTransactionError = pallet_ethereum::InvalidTransactionWrapper;
type ValidatedTransaction = pallet_ethereum::ValidatedTransaction<Self>;
type XcmEthereumOrigin = pallet_ethereum_xcm::EnsureXcmEthereumTransaction;
Expand Down
3 changes: 2 additions & 1 deletion runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ impl xcm_primitives::EnsureProxy<AccountId> for EthereumXcmEnsureProxy {
}

impl pallet_ethereum_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type InvalidEvmTransactionError = pallet_ethereum::InvalidTransactionWrapper;
type ValidatedTransaction = pallet_ethereum::ValidatedTransaction<Self>;
type XcmEthereumOrigin = pallet_ethereum_xcm::EnsureXcmEthereumTransaction;
Expand Down Expand Up @@ -1450,7 +1451,7 @@ construct_runtime! {
XTokens: orml_xtokens::{Pallet, Call, Storage, Event<T>} = 106,
XcmTransactor: pallet_xcm_transactor::{Pallet, Call, Storage, Event<T>} = 107,
// Previously 108: pallet_assets::<Instance1>
EthereumXcm: pallet_ethereum_xcm::{Pallet, Call, Storage, Origin} = 109,
EthereumXcm: pallet_ethereum_xcm::{Pallet, Call, Storage, Origin, Event<T>} = 109,
Erc20XcmBridge: pallet_erc20_xcm_bridge::{Pallet} = 110,
MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event<T>} = 111,
RelayStorageRoots: pallet_relay_storage_roots::{Pallet, Storage} = 112,
Expand Down
5 changes: 3 additions & 2 deletions runtime/moonbeam/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,9 @@ impl pallet_message_queue::Config for Runtime {
cumulus_primitives_core::AggregateMessageOrigin,
>;
#[cfg(not(feature = "runtime-benchmarks"))]
type MessageProcessor =
xcm_builder::ProcessXcmMessage<AggregateMessageOrigin, XcmExecutor, RuntimeCall>;
type MessageProcessor = pallet_ethereum_xcm::MessageProcessorWrapper<
xcm_builder::ProcessXcmMessage<AggregateMessageOrigin, XcmExecutor, RuntimeCall>,
>;
type Size = u32;
type HeapSize = MessageQueueHeapSize;
type MaxStale = MessageQueueMaxStale;
Expand Down
1 change: 1 addition & 0 deletions runtime/moonbeam/tests/xcm_mock/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,7 @@ impl xcm_primitives::EnsureProxy<AccountId> for EthereumXcmEnsureProxy {
}

impl pallet_ethereum_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type InvalidEvmTransactionError = pallet_ethereum::InvalidTransactionWrapper;
type ValidatedTransaction = pallet_ethereum::ValidatedTransaction<Self>;
type XcmEthereumOrigin = pallet_ethereum_xcm::EnsureXcmEthereumTransaction;
Expand Down
3 changes: 2 additions & 1 deletion runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ impl xcm_primitives::EnsureProxy<AccountId> for EthereumXcmEnsureProxy {
}

impl pallet_ethereum_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type InvalidEvmTransactionError = pallet_ethereum::InvalidTransactionWrapper;
type ValidatedTransaction = pallet_ethereum::ValidatedTransaction<Self>;
type XcmEthereumOrigin = pallet_ethereum_xcm::EnsureXcmEthereumTransaction;
Expand Down Expand Up @@ -1453,7 +1454,7 @@ construct_runtime! {
XTokens: orml_xtokens::{Pallet, Call, Storage, Event<T>} = 106,
XcmTransactor: pallet_xcm_transactor::{Pallet, Call, Storage, Event<T>} = 107,
// Previously 108: pallet_assets::<Instance1>
EthereumXcm: pallet_ethereum_xcm::{Pallet, Call, Storage, Origin} = 109,
EthereumXcm: pallet_ethereum_xcm::{Pallet, Call, Storage, Origin, Event<T>} = 109,
Erc20XcmBridge: pallet_erc20_xcm_bridge::{Pallet} = 110,
MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event<T>} = 111,
RelayStorageRoots: pallet_relay_storage_roots::{Pallet, Storage} = 112,
Expand Down
5 changes: 3 additions & 2 deletions runtime/moonriver/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,9 @@ impl pallet_message_queue::Config for Runtime {
cumulus_primitives_core::AggregateMessageOrigin,
>;
#[cfg(not(feature = "runtime-benchmarks"))]
type MessageProcessor =
xcm_builder::ProcessXcmMessage<AggregateMessageOrigin, XcmExecutor, RuntimeCall>;
type MessageProcessor = pallet_ethereum_xcm::MessageProcessorWrapper<
xcm_builder::ProcessXcmMessage<AggregateMessageOrigin, XcmExecutor, RuntimeCall>,
>;
type Size = u32;
type HeapSize = MessageQueueHeapSize;
type MaxStale = MessageQueueMaxStale;
Expand Down
1 change: 1 addition & 0 deletions runtime/moonriver/tests/xcm_mock/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ impl xcm_primitives::EnsureProxy<AccountId> for EthereumXcmEnsureProxy {
}

impl pallet_ethereum_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type InvalidEvmTransactionError = pallet_ethereum::InvalidTransactionWrapper;
type ValidatedTransaction = pallet_ethereum::ValidatedTransaction<Self>;
type XcmEthereumOrigin = pallet_ethereum_xcm::EnsureXcmEthereumTransaction;
Expand Down
Loading