Skip to content

Commit

Permalink
[tests] dedup test code, add more tests, improve naming and docs (par…
Browse files Browse the repository at this point in the history
…itytech#5338)

This is mostly tests cleanup:
- uses helper macro for generating teleport tests,
- adds missing treasury tests,
- improves naming and docs for transfer tests.

- [x] does not need a PRDOC

---------

Co-authored-by: command-bot <>
  • Loading branch information
acatangiu authored Aug 14, 2024
1 parent d944ac2 commit e4f8a6d
Show file tree
Hide file tree
Showing 33 changed files with 376 additions and 1,838 deletions.
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.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use sp_core::storage::Storage;

// Cumulus
use cumulus_primitives_core::ParaId;
use emulated_integration_tests_common::{build_genesis_storage, collators, SAFE_XCM_VERSION};
use emulated_integration_tests_common::{
accounts, build_genesis_storage, collators, SAFE_XCM_VERSION,
};
use parachains_common::Balance;

pub const PARA_ID: u32 = 1004;
Expand All @@ -27,6 +29,9 @@ pub const ED: Balance = testnet_parachains_constants::rococo::currency::EXISTENT
pub fn genesis() -> Storage {
let genesis_config = people_rococo_runtime::RuntimeGenesisConfig {
system: people_rococo_runtime::SystemConfig::default(),
balances: people_rococo_runtime::BalancesConfig {
balances: accounts::init_balances().iter().cloned().map(|k| (k, ED * 4096)).collect(),
},
parachain_info: people_rococo_runtime::ParachainInfoConfig {
parachain_id: ParaId::from(PARA_ID),
..Default::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use sp_core::storage::Storage;

// Cumulus
use cumulus_primitives_core::ParaId;
use emulated_integration_tests_common::{build_genesis_storage, collators, SAFE_XCM_VERSION};
use emulated_integration_tests_common::{
accounts, build_genesis_storage, collators, SAFE_XCM_VERSION,
};
use parachains_common::Balance;

pub const PARA_ID: u32 = 1004;
Expand All @@ -27,6 +29,9 @@ pub const ED: Balance = testnet_parachains_constants::westend::currency::EXISTEN
pub fn genesis() -> Storage {
let genesis_config = people_westend_runtime::RuntimeGenesisConfig {
system: people_westend_runtime::SystemConfig::default(),
balances: people_westend_runtime::BalancesConfig {
balances: accounts::init_balances().iter().cloned().map(|k| (k, ED * 4096)).collect(),
},
parachain_info: people_westend_runtime::ParachainInfoConfig {
parachain_id: ParaId::from(PARA_ID),
..Default::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ macro_rules! test_parachain_is_trusted_teleporter_for_relay {
$crate::macros::paste::paste! {
// init Origin variables
let sender = [<$sender_para Sender>]::get();
let mut para_sender_balance_before =
let para_sender_balance_before =
<$sender_para as $crate::macros::Chain>::account_data_of(sender.clone()).free;
let origin = <$sender_para as $crate::macros::Chain>::RuntimeOrigin::signed(sender.clone());
let assets: Assets = (Parent, $amount).into();
Expand Down Expand Up @@ -302,9 +302,6 @@ macro_rules! test_parachain_is_trusted_teleporter_for_relay {

assert_eq!(para_sender_balance_before - $amount - delivery_fees, para_sender_balance_after);
assert!(relay_receiver_balance_after > relay_receiver_balance_before);

// Update sender balance
para_sender_balance_before = <$sender_para as $crate::macros::Chain>::account_data_of(sender.clone()).free;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ use xcm::{prelude::*, DoubleEncoded};
pub fn xcm_transact_paid_execution(
call: DoubleEncoded<()>,
origin_kind: OriginKind,
native_asset: Asset,
fees: Asset,
beneficiary: AccountId,
) -> VersionedXcm<()> {
let weight_limit = WeightLimit::Unlimited;
let require_weight_at_most = Weight::from_parts(1000000000, 200000);
let native_assets: Assets = native_asset.clone().into();

VersionedXcm::from(Xcm(vec![
WithdrawAsset(native_assets),
BuyExecution { fees: native_asset, weight_limit },
WithdrawAsset(fees.clone().into()),
BuyExecution { fees, weight_limit },
Transact { require_weight_at_most, origin_kind, call },
RefundSurplus,
DepositAsset {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod imports {
pub use emulated_integration_tests_common::{
accounts::DUMMY_EMPTY,
get_account_id_from_seed, test_parachain_is_trusted_teleporter,
test_parachain_is_trusted_teleporter_for_relay, test_relay_is_trusted_teleporter,
xcm_emulator::{
assert_expected_events, bx, Chain, Parachain as Para, RelayChain as Relay, Test,
TestArgs, TestContext, TestExt,
Expand Down Expand Up @@ -91,7 +92,6 @@ mod imports {
pub const ASSET_ID: u32 = 3;
pub const ASSET_MIN_BALANCE: u128 = 1000;

pub type RelayToSystemParaTest = Test<Rococo, AssetHubRococo>;
pub type RelayToParaTest = Test<Rococo, PenpalA>;
pub type ParaToRelayTest = Test<PenpalA, Rococo>;
pub type SystemParaToRelayTest = Test<AssetHubRococo, Rococo>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,9 @@ fn para_to_para_through_relay_limited_reserve_transfer_assets(
)
}

/// Reserve Transfers of native asset from Relay Chain to the System Parachain shouldn't work
/// Reserve Transfers of native asset from Relay Chain to the Asset Hub shouldn't work
#[test]
fn reserve_transfer_native_asset_from_relay_to_system_para_fails() {
fn reserve_transfer_native_asset_from_relay_to_asset_hub_fails() {
// Init values for Relay Chain
let signed_origin = <Rococo as Chain>::RuntimeOrigin::signed(RococoSender::get().into());
let destination = Rococo::child_location_of(AssetHubRococo::para_id());
Expand Down Expand Up @@ -526,10 +526,10 @@ fn reserve_transfer_native_asset_from_relay_to_system_para_fails() {
});
}

/// Reserve Transfers of native asset from System Parachain to Relay Chain shouldn't work
/// Reserve Transfers of native asset from Asset Hub to Relay Chain shouldn't work
#[test]
fn reserve_transfer_native_asset_from_system_para_to_relay_fails() {
// Init values for System Parachain
fn reserve_transfer_native_asset_from_asset_hub_to_relay_fails() {
// Init values for Asset Hub
let signed_origin =
<AssetHubRococo as Chain>::RuntimeOrigin::signed(AssetHubRococoSender::get().into());
let destination = AssetHubRococo::parent_location();
Expand Down Expand Up @@ -691,10 +691,10 @@ fn reserve_transfer_native_asset_from_para_to_relay() {
// =========================================================================
// ======= Reserve Transfers - Native Asset - AssetHub<>Parachain ==========
// =========================================================================
/// Reserve Transfers of native asset from System Parachain to Parachain should work
/// Reserve Transfers of native asset from Asset Hub to Parachain should work
#[test]
fn reserve_transfer_native_asset_from_system_para_to_para() {
// Init values for System Parachain
fn reserve_transfer_native_asset_from_asset_hub_to_para() {
// Init values for Asset Hub
let destination = AssetHubRococo::sibling_location_of(PenpalA::para_id());
let sender = AssetHubRococoSender::get();
let amount_to_send: Balance = ASSET_HUB_ROCOCO_ED * 10000;
Expand Down Expand Up @@ -749,9 +749,9 @@ fn reserve_transfer_native_asset_from_system_para_to_para() {
assert!(receiver_assets_after < receiver_assets_before + amount_to_send);
}

/// Reserve Transfers of native asset from Parachain to System Parachain should work
/// Reserve Transfers of native asset from Parachain to Asset Hub should work
#[test]
fn reserve_transfer_native_asset_from_para_to_system_para() {
fn reserve_transfer_native_asset_from_para_to_asset_hub() {
// Init values for Parachain
let destination = PenpalA::sibling_location_of(AssetHubRococo::para_id());
let sender = PenpalASender::get();
Expand All @@ -768,12 +768,12 @@ fn reserve_transfer_native_asset_from_para_to_system_para() {
amount_to_send * 2,
);

// Init values for System Parachain
// Init values for Asset Hub
let receiver = AssetHubRococoReceiver::get();
let penpal_location_as_seen_by_ahr = AssetHubRococo::sibling_location_of(PenpalA::para_id());
let sov_penpal_on_ahr = AssetHubRococo::sovereign_account_id_of(penpal_location_as_seen_by_ahr);

// fund Parachain's SA on System Parachain with the native tokens held in reserve
// fund Parachain's SA on Asset Hub with the native tokens held in reserve
AssetHubRococo::fund_accounts(vec![(sov_penpal_on_ahr.into(), amount_to_send * 2)]);

// Init Test
Expand Down Expand Up @@ -824,11 +824,11 @@ fn reserve_transfer_native_asset_from_para_to_system_para() {
// ==================================================================================
// ======= Reserve Transfers - Native + Non-system Asset - AssetHub<>Parachain ======
// ==================================================================================
/// Reserve Transfers of a local asset and native asset from System Parachain to Parachain should
/// Reserve Transfers of a local asset and native asset from Asset Hub to Parachain should
/// work
#[test]
fn reserve_transfer_assets_from_system_para_to_para() {
// Init values for System Parachain
fn reserve_transfer_multiple_assets_from_asset_hub_to_para() {
// Init values for Asset Hub
let destination = AssetHubRococo::sibling_location_of(PenpalA::para_id());
let sov_penpal_on_ahr = AssetHubRococo::sovereign_account_id_of(destination.clone());
let sender = AssetHubRococoSender::get();
Expand Down Expand Up @@ -939,13 +939,12 @@ fn reserve_transfer_assets_from_system_para_to_para() {
);
}

/// Reserve Transfers of a random asset and native asset from Parachain to System Para should
/// work
/// Reserve Transfers of a random asset and native asset from Parachain to Asset Hub should work
/// Receiver is empty account to show deposit works as long as transfer includes enough DOT for ED.
/// Once we have https://github.com/paritytech/polkadot-sdk/issues/5298,
/// we should do equivalent test with USDT instead of DOT.
#[test]
fn reserve_transfer_assets_from_para_to_system_para() {
fn reserve_transfer_multiple_assets_from_para_to_asset_hub() {
// Init values for Parachain
let destination = PenpalA::sibling_location_of(AssetHubRococo::para_id());
let sender = PenpalASender::get();
Expand Down Expand Up @@ -982,7 +981,7 @@ fn reserve_transfer_assets_from_para_to_system_para() {

// Beneficiary is a new (empty) account
let receiver = get_account_id_from_seed::<sp_runtime::testing::sr25519::Public>(DUMMY_EMPTY);
// Init values for System Parachain
// Init values for Asset Hub
let penpal_location_as_seen_by_ahr = AssetHubRococo::sibling_location_of(PenpalA::para_id());
let sov_penpal_on_ahr = AssetHubRococo::sovereign_account_id_of(penpal_location_as_seen_by_ahr);
let ah_asset_owner = AssetHubRococoAssetOwner::get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::imports::*;
/// Relay Chain should be able to execute `Transact` instructions in System Parachain
/// when `OriginKind::Superuser`.
#[test]
fn send_transact_as_superuser_from_relay_to_system_para_works() {
fn send_transact_as_superuser_from_relay_to_asset_hub_works() {
AssetHubRococo::force_create_asset_from_relay_as_root(
ASSET_ID,
ASSET_MIN_BALANCE,
Expand All @@ -29,10 +29,10 @@ fn send_transact_as_superuser_from_relay_to_system_para_works() {
}

/// We tests two things here:
/// - Parachain should be able to send XCM paying its fee with system asset in the System Parachain
/// - Parachain should be able to create a new Foreign Asset in the System Parachain
/// - Parachain should be able to send XCM paying its fee at Asset Hub using system asset
/// - Parachain should be able to create a new Foreign Asset at Asset Hub
#[test]
fn send_xcm_from_para_to_system_para_paying_fee_with_system_assets_works() {
fn send_xcm_from_para_to_asset_hub_paying_fee_with_system_asset() {
let para_sovereign_account = AssetHubRococo::sovereign_account_id_of(
AssetHubRococo::sibling_location_of(PenpalA::para_id()),
);
Expand Down Expand Up @@ -83,12 +83,7 @@ fn send_xcm_from_para_to_system_para_paying_fee_with_system_assets_works() {

AssetHubRococo::execute_with(|| {
type RuntimeEvent = <AssetHubRococo as Chain>::RuntimeEvent;

AssetHubRococo::assert_xcmp_queue_success(Some(Weight::from_parts(
15_594_564_000,
562_893,
)));

AssetHubRococo::assert_xcmp_queue_success(None);
assert_expected_events!(
AssetHubRococo,
vec![
Expand All @@ -112,15 +107,15 @@ fn send_xcm_from_para_to_system_para_paying_fee_with_system_assets_works() {
}

/// We tests two things here:
/// - Parachain should be able to send XCM paying its fee with system assets in the System Parachain
/// - Parachain should be able to create a new Asset in the System Parachain
/// - Parachain should be able to send XCM paying its fee at Asset Hub using sufficient asset
/// - Parachain should be able to create a new Asset at Asset Hub
#[test]
fn send_xcm_from_para_to_system_para_paying_fee_with_assets_works() {
fn send_xcm_from_para_to_asset_hub_paying_fee_with_sufficient_asset() {
let para_sovereign_account = AssetHubRococo::sovereign_account_id_of(
AssetHubRococo::sibling_location_of(PenpalA::para_id()),
);

// Force create and mint assets for Parachain's sovereign account
// Force create and mint sufficient assets for Parachain's sovereign account
AssetHubRococo::force_create_and_mint_asset(
ASSET_ID,
ASSET_MIN_BALANCE,
Expand Down Expand Up @@ -167,12 +162,7 @@ fn send_xcm_from_para_to_system_para_paying_fee_with_assets_works() {

AssetHubRococo::execute_with(|| {
type RuntimeEvent = <AssetHubRococo as Chain>::RuntimeEvent;

AssetHubRococo::assert_xcmp_queue_success(Some(Weight::from_parts(
15_594_564_000,
562_893,
)));

AssetHubRococo::assert_xcmp_queue_success(None);
assert_expected_events!(
AssetHubRococo,
vec![
Expand Down
Loading

0 comments on commit e4f8a6d

Please sign in to comment.