Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

improve xcm-emulator macros #2985

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
pub use codec::Encode;
pub use frame_support::{
assert_err, assert_ok,
instances::Instance1,
pallet_prelude::Weight,
sp_runtime::{AccountId32, DispatchError, DispatchResult, MultiAddress},
traits::{fungibles::Inspect, OriginTrait},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
pub use codec::Encode;
pub use frame_support::{
assert_err, assert_ok,
instances::Instance1,
pallet_prelude::Weight,
sp_runtime::{AccountId32, DispatchError, DispatchResult, MultiAddress},
traits::{fungibles::Inspect, OriginTrait},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
pub use codec::Encode;
pub use frame_support::{
assert_err, assert_ok,
instances::{Instance1, Instance2},
pallet_prelude::Weight,
sp_runtime::{AccountId32, DispatchError, DispatchResult, ModuleError, MultiAddress},
traits::{fungibles::Inspect, OriginTrait},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub use bp_messages::LaneId;
pub use codec::Encode;
pub use frame_support::{
assert_err, assert_ok,
instances::Instance1,
pallet_prelude::Weight,
sp_runtime::{AccountId32, DispatchError, DispatchResult, MultiAddress},
traits::{fungibles::Inspect, OriginTrait},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
pub use codec::Encode;
pub use frame_support::{
assert_err, assert_ok,
instances::Instance1,
pallet_prelude::Weight,
sp_runtime::{AccountId32, DispatchError, DispatchResult, MultiAddress},
traits::{fungibles::Inspect, OriginTrait},
Expand Down
33 changes: 18 additions & 15 deletions parachains/integration-tests/emulated/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use pallet_bridge_messages::{Config, Instance1, Instance2, OutboundLanes, Pallet
use sp_core::Get;
use xcm_emulator::{BridgeMessage, BridgeMessageDispatchError, BridgeMessageHandler, Chain};

pub use paste;
pub use parachains_common::{AccountId, Balance};

pub struct BridgeHubMessageHandler<S, T, I> {
_marker: std::marker::PhantomData<(S, T, I)>,
}
Expand Down Expand Up @@ -129,10 +132,10 @@ where
#[macro_export]
macro_rules! impl_accounts_helpers_for_relay_chain {
( $chain:ident ) => {
$crate::paste::paste! {
$crate::impls::paste::paste! {
impl $chain {
/// Fund a set of accounts with a balance
pub fn fund_accounts(accounts: Vec<(AccountId, Balance)>) {
pub fn fund_accounts(accounts: Vec<($crate::impls::AccountId, $crate::impls::Balance)>) {
Self::execute_with(|| {
for account in accounts {
assert_ok!(<Self as [<$chain Pallet>]>::Balances::force_set_balance(
Expand All @@ -144,7 +147,7 @@ macro_rules! impl_accounts_helpers_for_relay_chain {
});
}
/// Fund a sovereign account based on its Parachain Id
pub fn fund_para_sovereign(amount: Balance, para_id: ParaId) -> sp_runtime::AccountId32 {
pub fn fund_para_sovereign(amount: $crate::impls::Balance, para_id: ParaId) -> sp_runtime::AccountId32 {
let sovereign_account = Self::sovereign_account_id_of_child_para(para_id);
Self::fund_accounts(vec![(sovereign_account.clone(), amount)]);
sovereign_account
Expand All @@ -157,7 +160,7 @@ macro_rules! impl_accounts_helpers_for_relay_chain {
#[macro_export]
macro_rules! impl_assert_events_helpers_for_relay_chain {
( $chain:ident ) => {
$crate::paste::paste! {
$crate::impls::paste::paste! {
type [<$chain RuntimeEvent>] = <$chain as Chain>::RuntimeEvent;

impl $chain {
Expand Down Expand Up @@ -247,7 +250,7 @@ macro_rules! impl_assert_events_helpers_for_relay_chain {
#[macro_export]
macro_rules! impl_hrmp_channels_helpers_for_relay_chain {
( $chain:ident ) => {
$crate::paste::paste! {
$crate::impls::paste::paste! {
impl $chain {
/// Init open channel request with another Parachain
pub fn init_open_channel_call(
Expand Down Expand Up @@ -305,10 +308,10 @@ macro_rules! impl_hrmp_channels_helpers_for_relay_chain {
#[macro_export]
macro_rules! impl_accounts_helpers_for_parachain {
( $chain:ident ) => {
$crate::paste::paste! {
$crate::impls::paste::paste! {
impl $chain {
/// Fund a set of accounts with a balance
pub fn fund_accounts(accounts: Vec<(AccountId, Balance)>) {
pub fn fund_accounts(accounts: Vec<($crate::impls::AccountId, $crate::impls::Balance)>) {
Self::execute_with(|| {
for account in accounts {
assert_ok!(<Self as [<$chain Pallet>]>::Balances::force_set_balance(
Expand All @@ -327,7 +330,7 @@ macro_rules! impl_accounts_helpers_for_parachain {
#[macro_export]
macro_rules! impl_assert_events_helpers_for_parachain {
( $chain:ident ) => {
$crate::paste::paste! {
$crate::impls::paste::paste! {
type [<$chain RuntimeEvent>] = <$chain as Chain>::RuntimeEvent;

impl $chain {
Expand Down Expand Up @@ -474,14 +477,14 @@ macro_rules! impl_assert_events_helpers_for_parachain {
#[macro_export]
macro_rules! impl_assets_helpers_for_parachain {
( $chain:ident, $relay_chain:ident ) => {
$crate::paste::paste! {
$crate::impls::paste::paste! {
impl $chain {
/// Returns the encoded call for `force_create` from the assets pallet
pub fn force_create_asset_call(
asset_id: u32,
owner: AccountId,
owner: $crate::impls::AccountId,
is_sufficient: bool,
min_balance: Balance,
min_balance: $crate::impls::Balance,
) -> DoubleEncoded<()> {
<Self as Chain>::RuntimeCall::Assets(pallet_assets::Call::<
<Self as Chain>::Runtime,
Expand All @@ -500,9 +503,9 @@ macro_rules! impl_assets_helpers_for_parachain {
pub fn force_create_asset_xcm(
origin_kind: OriginKind,
asset_id: u32,
owner: AccountId,
owner: $crate::impls::AccountId,
is_sufficient: bool,
min_balance: Balance,
min_balance: $crate::impls::Balance,
) -> VersionedXcm<()> {
let call = Self::force_create_asset_call(asset_id, owner, is_sufficient, min_balance);
xcm_transact_unpaid_execution(call, origin_kind)
Expand All @@ -512,7 +515,7 @@ macro_rules! impl_assets_helpers_for_parachain {
pub fn mint_asset(
signed_origin: <Self as Chain>::RuntimeOrigin,
id: u32,
beneficiary: AccountId,
beneficiary: $crate::impls::AccountId,
amount_to_mint: u128,
) {
Self::execute_with(|| {
Expand Down Expand Up @@ -543,7 +546,7 @@ macro_rules! impl_assets_helpers_for_parachain {
id: u32,
min_balance: u128,
is_sufficient: bool,
asset_owner: AccountId,
asset_owner: $crate::impls::AccountId,
amount_to_mint: u128,
) {
// Init values for Relay Chain
Expand Down
23 changes: 1 addition & 22 deletions parachains/integration-tests/emulated/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,16 @@ pub use lazy_static;
pub mod constants;
pub mod impls;

pub use codec::Encode;
pub use constants::{
accounts::{ALICE, BOB},
asset_hub_kusama, asset_hub_polkadot, asset_hub_westend, bridge_hub_kusama,
bridge_hub_polkadot, bridge_hub_rococo, collectives, kusama, penpal, polkadot, rococo, westend,
PROOF_SIZE_THRESHOLD, REF_TIME_THRESHOLD,
};
use frame_support::{
assert_ok, instances::Instance1, parameter_types, sp_tracing, traits::fungibles::Inspect,
};
use frame_support::traits::fungibles::Inspect;
pub use impls::{RococoWococoMessageHandler, WococoRococoMessageHandler};
pub use parachains_common::{AccountId, Balance};
pub use paste;
use polkadot_parachain::primitives::HrmpChannelId;
pub use polkadot_runtime_parachains::inclusion::{AggregateMessageOrigin, UmpQueueId};
pub use sp_core::{sr25519, storage::Storage, Get};
use xcm_emulator::{
assert_expected_events, bx, decl_test_bridges, decl_test_networks, decl_test_parachains,
decl_test_relay_chains, decl_test_sender_receiver_accounts_parameter_types,
helpers::weight_within_threshold, BridgeMessageHandler, Chain, DefaultMessageProcessor, ParaId,
Parachain, RelayChain, TestExt,
};

pub use xcm::{
prelude::{
AccountId32, All, BuyExecution, DepositAsset, MultiAsset, MultiAssets, MultiLocation,
OriginKind, Outcome, RefundSurplus, Transact, UnpaidExecution, VersionedXcm, Weight,
WeightLimit, WithdrawAsset, Xcm, X1,
},
v3::Error,
DoubleEncoded,
};

decl_test_relay_chains! {
Expand Down
1 change: 1 addition & 0 deletions xcm/xcm-emulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-message-queue = { git = "https://github.com/paritytech/substrate", branch = "master" }

Expand Down
25 changes: 15 additions & 10 deletions xcm/xcm-emulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub use frame_support::{
pub use frame_system::{AccountInfo, Config as SystemConfig, Pallet as SystemPallet};
pub use pallet_balances::AccountData;
pub use sp_arithmetic::traits::Bounded;
pub use sp_core::{sr25519, storage::Storage, Pair, H256};
pub use sp_io::TestExternalities;
pub use sp_core::{parameter_types, sr25519, storage::Storage, Pair, H256};
pub use sp_io::{self, TestExternalities};
pub use sp_std::{cell::RefCell, collections::vec_deque::VecDeque, fmt::Debug};
pub use sp_trie::StorageProof;

Expand All @@ -68,6 +68,7 @@ pub use polkadot_runtime_parachains::{
dmp,
inclusion::{AggregateMessageOrigin, UmpQueueId},
};
pub use sp_tracing;

// Polkadot
pub use xcm::{
Expand Down Expand Up @@ -358,6 +359,7 @@ macro_rules! decl_test_relay_chains {
}
),
+
$(,)?
) => {
$(
#[derive(Clone)]
Expand Down Expand Up @@ -443,7 +445,7 @@ macro_rules! __impl_test_ext_for_relay_chain {
ext.execute_with(|| {
#[allow(clippy::no_effect)]
$on_init;
sp_tracing::try_init_simple();
$crate::sp_tracing::try_init_simple();

let mut block_number = <Self as Chain>::System::block_number();
block_number = std::cmp::max(1, block_number);
Expand Down Expand Up @@ -582,6 +584,7 @@ macro_rules! decl_test_parachains {
}
),
+
$(,)?
) => {
$(
#[derive(Clone)]
Expand Down Expand Up @@ -684,7 +687,7 @@ macro_rules! __impl_test_ext_for_parachain {
ext.execute_with(|| {
#[allow(clippy::no_effect)]
$on_init;
sp_tracing::try_init_simple();
$crate::sp_tracing::try_init_simple();

let mut block_number = <Self as Chain>::System::block_number();
block_number = std::cmp::max(1, block_number);
Expand Down Expand Up @@ -789,8 +792,8 @@ macro_rules! __impl_test_ext_for_parachain {
);

// get xcmp messages
<Self as Parachain>::ParachainSystem::on_finalize(block_number);
let collation_info = <Self as Parachain>::ParachainSystem::collect_collation_info(&mock_header);
<Self as $crate::Parachain>::ParachainSystem::on_finalize(block_number);
let collation_info = <Self as $crate::Parachain>::ParachainSystem::collect_collation_info(&mock_header);

// send upward messages
let relay_block_number = <$name as NetworkComponent>::Network::relay_block_number();
Expand All @@ -809,7 +812,7 @@ macro_rules! __impl_test_ext_for_parachain {
// get bridge messages
type NetworkBridge = <<$name as NetworkComponent>::Network as Network>::Bridge;

let bridge_messages = <NetworkBridge as Bridge>::Handler::get_source_outbound_messages();
let bridge_messages = <<NetworkBridge as Bridge>::Handler as $crate::BridgeMessageHandler>::get_source_outbound_messages();

// send bridged messages
for msg in bridge_messages {
Expand Down Expand Up @@ -857,6 +860,7 @@ macro_rules! decl_test_networks {
}
),
+
$(,)?
) => {
$(
pub struct $name;
Expand Down Expand Up @@ -1014,8 +1018,8 @@ macro_rules! decl_test_networks {
match dispatch_result {
Err(e) => panic!("Error {:?} processing bridged message: {:?}", e, msg.clone()),
Ok(()) => {
<<Self::Bridge as $crate::Bridge>::Source as TestExt>::ext_wrapper(|| {
<<Self::Bridge as Bridge>::Handler as BridgeMessageHandler>::notify_source_message_delivery(msg.id);
<<Self::Bridge as $crate::Bridge>::Source as $crate::TestExt>::ext_wrapper(|| {
<<Self::Bridge as Bridge>::Handler as $crate::BridgeMessageHandler>::notify_source_message_delivery(msg.id);
});
$crate::log::debug!(target: concat!("bridge::", stringify!($name)) , "Bridged message processed {:?}", msg.clone());
}
Expand Down Expand Up @@ -1096,6 +1100,7 @@ macro_rules! decl_test_bridges {
}
),
+
$(,)?
) => {
$(
#[derive(Debug)]
Expand Down Expand Up @@ -1233,7 +1238,7 @@ macro_rules! bx {
macro_rules! decl_test_sender_receiver_accounts_parameter_types {
( $( $chain:ident { sender: $sender:expr, receiver: $receiver:expr }),+ ) => {
$crate::paste::paste! {
parameter_types! {
$crate::parameter_types! {
$(
pub [<$chain Sender>]: $crate::AccountId = <$chain>::account_id_of($sender);
pub [<$chain Receiver>]: $crate::AccountId = <$chain>::account_id_of($receiver);
Expand Down