From 02d3208cf2ff65ea3b61971e0ad06d929d291477 Mon Sep 17 00:00:00 2001 From: Diego Date: Tue, 3 Dec 2024 00:40:17 +0900 Subject: [PATCH] Dancelight xcm test working --- .../dancelight/src/tests/common/mod.rs | 1 - .../src/tests/common/xcm/mocknets.rs | 136 +++------------ .../dancelight/src/tests/common/xcm/mod.rs | 7 +- .../xcm/reserver_transfers_polkadot_xcm.rs | 156 ++++++++++++++++++ 4 files changed, 182 insertions(+), 118 deletions(-) create mode 100644 solo-chains/runtime/dancelight/src/tests/common/xcm/reserver_transfers_polkadot_xcm.rs diff --git a/solo-chains/runtime/dancelight/src/tests/common/mod.rs b/solo-chains/runtime/dancelight/src/tests/common/mod.rs index 40a186899..c29deae89 100644 --- a/solo-chains/runtime/dancelight/src/tests/common/mod.rs +++ b/solo-chains/runtime/dancelight/src/tests/common/mod.rs @@ -756,7 +756,6 @@ pub const CHARLIE: [u8; 32] = [6u8; 32]; pub const DAVE: [u8; 32] = [7u8; 32]; pub const EVE: [u8; 32] = [8u8; 32]; pub const FERDIE: [u8; 32] = [9u8; 32]; -pub const RANDOM: [u8; 32] = [3u8; 32]; fn take_new_inherent_data() -> Option { let data: Option = diff --git a/solo-chains/runtime/dancelight/src/tests/common/xcm/mocknets.rs b/solo-chains/runtime/dancelight/src/tests/common/xcm/mocknets.rs index 70429b10f..e4d738069 100644 --- a/solo-chains/runtime/dancelight/src/tests/common/xcm/mocknets.rs +++ b/solo-chains/runtime/dancelight/src/tests/common/xcm/mocknets.rs @@ -17,60 +17,28 @@ pub use sp_core::Get; use { super::constants::{ accounts::{ALICE, BOB, RANDOM}, - frontier_template, rococo, simple_template, westend, + frontier_template, simple_template, }, crate::tests::common::ExtBuilder, - emulated_integration_tests_common::{ - impl_assert_events_helpers_for_parachain, xcm_emulator::decl_test_parachains, - }, + dancelight_runtime_constants::currency::UNITS as UNIT, + emulated_integration_tests_common::xcm_emulator::decl_test_parachains, frame_support::parameter_types, xcm_emulator::{decl_test_networks, decl_test_relay_chains, Chain}, }; decl_test_relay_chains! { - #[api_version(11)] - pub struct Westend { - genesis = westend::genesis(), - on_init = (), - runtime = westend_runtime, - core = { - SovereignAccountOf: westend_runtime::xcm_config::LocationConverter, - }, - pallets = { - System: westend_runtime::System, - Balances: westend_runtime::Balances, - XcmPallet: westend_runtime::XcmPallet, - Sudo: westend_runtime::Sudo, - } - }, - #[api_version(11)] - pub struct Rococo { - genesis = rococo::genesis(), - on_init = (), - runtime = rococo_runtime, - core = { - SovereignAccountOf: rococo_runtime::xcm_config::LocationConverter, - }, - pallets = { - System: rococo_runtime::System, - Session: rococo_runtime::Session, - Configuration: rococo_runtime::Configuration, - Balances: rococo_runtime::Balances, - Registrar: rococo_runtime::Registrar, - ParasSudoWrapper: rococo_runtime::ParasSudoWrapper, - OnDemandAssignmentProvider: rococo_runtime::OnDemandAssignmentProvider, - XcmPallet: rococo_runtime::XcmPallet, - Sudo: rococo_runtime::Sudo, - } - }, #[api_version(11)] pub struct Dancelight { genesis = ExtBuilder::default() .with_balances(vec![ // Alice gets 10k extra tokens for her mapping deposit - (crate::AccountId::from(crate::tests::common::ALICE), 210_000 * dancelight_runtime_constants::currency::UNITS), - (crate::AccountId::from(crate::tests::common::BOB), 100_000 * dancelight_runtime_constants::currency::UNITS), + (crate::AccountId::from(crate::tests::common::ALICE), 210_000 * UNIT), + (crate::AccountId::from(crate::tests::common::BOB), 100_000 * UNIT), ]) + .with_relay_config(runtime_parachains::configuration::HostConfiguration { + max_downward_message_size: 1024 * 1024, + ..Default::default() + }) .with_safe_xcm_version(3) .build_storage(), on_init = (), @@ -93,7 +61,8 @@ decl_test_relay_chains! { } decl_test_parachains! { - pub struct FrontierTemplate { + // Dancelight parachains + pub struct FrontierTemplateDancelight { genesis = frontier_template::genesis(), on_init = (), runtime = container_chain_template_frontier_runtime, @@ -113,47 +82,7 @@ decl_test_parachains! { ForeignAssetsCreator: container_chain_template_frontier_runtime::ForeignAssetsCreator, } }, - pub struct SimpleTemplate { - genesis = simple_template::genesis(), - on_init = (), - runtime = container_chain_template_simple_runtime, - core = { - XcmpMessageHandler: container_chain_template_simple_runtime::XcmpQueue, - LocationToAccountId: container_chain_template_simple_runtime::xcm_config::LocationToAccountId, - ParachainInfo: container_chain_template_simple_runtime::ParachainInfo, - MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin, - }, - pallets = { - System: container_chain_template_simple_runtime::System, - Balances: container_chain_template_simple_runtime::Balances, - ParachainSystem: container_chain_template_simple_runtime::ParachainSystem, - PolkadotXcm: container_chain_template_simple_runtime::PolkadotXcm, - ForeignAssets: container_chain_template_simple_runtime::ForeignAssets, - AssetRate: container_chain_template_simple_runtime::AssetRate, - ForeignAssetsCreator: container_chain_template_simple_runtime::ForeignAssetsCreator, - } - }, - pub struct FrontierTemplateRococo { - genesis = frontier_template::genesis(), - on_init = (), - runtime = container_chain_template_frontier_runtime, - core = { - XcmpMessageHandler: container_chain_template_frontier_runtime::XcmpQueue, - LocationToAccountId: container_chain_template_frontier_runtime::xcm_config::LocationToAccountId, - ParachainInfo: container_chain_template_frontier_runtime::ParachainInfo, - MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin, - }, - pallets = { - System: container_chain_template_frontier_runtime::System, - Balances: container_chain_template_frontier_runtime::Balances, - ParachainSystem: container_chain_template_frontier_runtime::ParachainSystem, - PolkadotXcm: container_chain_template_frontier_runtime::PolkadotXcm, - ForeignAssets: container_chain_template_frontier_runtime::ForeignAssets, - AssetRate: container_chain_template_frontier_runtime::AssetRate, - ForeignAssetsCreator: container_chain_template_frontier_runtime::ForeignAssetsCreator, - } - }, - pub struct SimpleTemplateRococo { + pub struct SimpleTemplateDancelight { genesis = simple_template::genesis(), on_init = (), runtime = container_chain_template_simple_runtime, @@ -175,50 +104,25 @@ decl_test_parachains! { } } -impl_assert_events_helpers_for_parachain!(FrontierTemplate); -impl_assert_events_helpers_for_parachain!(SimpleTemplate); - decl_test_networks! { - pub struct WestendMockNet { - relay_chain = Westend, + pub struct DancelightMockNet { + relay_chain = Dancelight, parachains = vec![ - FrontierTemplate, - SimpleTemplate, - ], - bridge = () - }, - pub struct RococoMockNet { - relay_chain = Rococo, - parachains = vec![ - FrontierTemplateRococo, - SimpleTemplateRococo, + FrontierTemplateDancelight, + SimpleTemplateDancelight, ], bridge = () } } parameter_types! { - // Westend - pub WestendSender: cumulus_primitives_core::relay_chain::AccountId = WestendRelay::account_id_of(ALICE); - pub WestendReceiver: cumulus_primitives_core::relay_chain::AccountId = WestendRelay::account_id_of(BOB); - pub WestendEmptyReceiver: cumulus_primitives_core::relay_chain::AccountId = WestendRelay::account_id_of(RANDOM); - - // Rococo - pub RococoSender: cumulus_primitives_core::relay_chain::AccountId = RococoRelay::account_id_of(ALICE); - pub RococoReceiver: cumulus_primitives_core::relay_chain::AccountId = RococoRelay::account_id_of(BOB); - pub RococoEmptyReceiver: cumulus_primitives_core::relay_chain::AccountId = RococoRelay::account_id_of(RANDOM); - // Dancelight pub DancelightSender: crate::AccountId = crate::AccountId::from(crate::tests::common::ALICE); pub DancelightReceiver: crate::AccountId = crate::AccountId::from(crate::tests::common::BOB); - pub DancelightEmptyReceiver: crate::AccountId = crate::AccountId::from(crate::tests::common::RANDOM); + pub DancelightEmptyReceiver: crate::AccountId = DancelightRelay::account_id_of(RANDOM); // SimpleTemplate - pub SimpleTemplateSender: container_chain_template_simple_runtime::AccountId = SimpleTemplatePara::account_id_of(ALICE); - pub SimpleTemplateReceiver: container_chain_template_simple_runtime::AccountId = SimpleTemplatePara::account_id_of(BOB); - pub SimpleTemplateEmptyReceiver: container_chain_template_simple_runtime::AccountId = SimpleTemplatePara::account_id_of(RANDOM); - - pub EthereumSender: container_chain_template_frontier_runtime::AccountId = frontier_template::pre_funded_accounts()[0]; - pub EthereumReceiver: container_chain_template_frontier_runtime::AccountId = frontier_template::pre_funded_accounts()[1]; - pub EthereumEmptyReceiver: container_chain_template_frontier_runtime::AccountId = [1u8; 20].into(); + pub SimpleTemplateDancelightSender: container_chain_template_simple_runtime::AccountId = SimpleTemplateDancelightPara::account_id_of(ALICE); + pub SimpleTemplateDancelightReceiver: container_chain_template_simple_runtime::AccountId = SimpleTemplateDancelightPara::account_id_of(BOB); + pub SimpleTemplateDancelightEmptyReceiver: container_chain_template_simple_runtime::AccountId = SimpleTemplateDancelightPara::account_id_of(RANDOM); } diff --git a/solo-chains/runtime/dancelight/src/tests/common/xcm/mod.rs b/solo-chains/runtime/dancelight/src/tests/common/xcm/mod.rs index 462f34d63..803d03416 100644 --- a/solo-chains/runtime/dancelight/src/tests/common/xcm/mod.rs +++ b/solo-chains/runtime/dancelight/src/tests/common/xcm/mod.rs @@ -15,5 +15,10 @@ // along with Tanssi. If not, see mod constants; -mod expected_event_checker; mod mocknets; +mod reserver_transfers_polkadot_xcm; + +#[macro_use] +mod expected_event_checker; + +pub use xcm_emulator::{bx, TestExt}; diff --git a/solo-chains/runtime/dancelight/src/tests/common/xcm/reserver_transfers_polkadot_xcm.rs b/solo-chains/runtime/dancelight/src/tests/common/xcm/reserver_transfers_polkadot_xcm.rs new file mode 100644 index 000000000..995c6ccd4 --- /dev/null +++ b/solo-chains/runtime/dancelight/src/tests/common/xcm/reserver_transfers_polkadot_xcm.rs @@ -0,0 +1,156 @@ +// Copyright (C) Moondance Labs Ltd. +// This file is part of Tanssi. + +// Tanssi 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. + +// Tanssi 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 Tanssi. If not, see + +use { + crate::{ + assert_expected_events, + tests::common::xcm::{ + mocknets::{ + DancelightRelay as Dancelight, DancelightSender, + SimpleTemplateDancelightEmptyReceiver as SimpleTemplateEmptyReceiver, + SimpleTemplateDancelightPara as SimpleTemplateDancelight, + }, + *, + }, + }, + frame_support::{ + assert_ok, + weights::{Weight, WeightToFee}, + }, + mocknets::{DancelightRelayPallet, SimpleTemplateDancelightParaPallet}, + sp_runtime::FixedU128, + xcm::{ + latest::prelude::{Junctions::*, *}, + VersionedLocation, + }, + xcm_emulator::Chain, +}; + +#[allow(unused_assignments)] +#[test] +fn transfer_assets_from_dancelight_to_one_of_its_parachains() { + // Dancelight origin (sender) + let dancelight_alice_origin = + ::RuntimeOrigin::signed(DancelightSender::get()); + + // Destination location from the dancelight relay viewpoint + let simple_template_dest: VersionedLocation = Location { + parents: 0, + interior: X1([Parachain(constants::simple_template::PARA_ID)].into()), + } + .into(); + + // Beneficiary location from the simple template parachain viewpoint + let simple_template_beneficiary: VersionedLocation = Location { + parents: 0, + interior: X1([AccountId32 { + network: None, + id: SimpleTemplateEmptyReceiver::get().into(), + }] + .into()), + } + .into(); + + let amount_to_send: crate::Balance = crate::ExistentialDeposit::get() * 1000; + + // Asset located in Dancelight relay + let assets: Assets = (Here, amount_to_send).into(); + let fee_asset_item = 0; + let dancelight_token_asset_id = 1u16; + + // Register the asset first in simple template chain + SimpleTemplateDancelight::execute_with(|| { + let root_origin = ::RuntimeOrigin::root(); + + // Create foreign asset from relay chain + assert_ok!( + ::ForeignAssetsCreator::create_foreign_asset( + root_origin.clone(), + Location { + parents: 1, + interior: Here.into(), + }, + dancelight_token_asset_id, + SimpleTemplateEmptyReceiver::get(), + true, + 1 + ) + ); + + // Create asset rate + assert_ok!( + ::AssetRate::create( + root_origin, + bx!(1), + FixedU128::from_u32(1) + ) + ); + }); + + // Send XCM transfer from Dancelight + Dancelight::execute_with(|| { + assert_ok!( + ::XcmPallet::limited_reserve_transfer_assets( + dancelight_alice_origin, + bx!(simple_template_dest), + bx!(simple_template_beneficiary), + bx!(assets.into()), + fee_asset_item, + WeightLimit::Unlimited, + ) + ); + }); + + // Verify token reception in Simple Template chain + SimpleTemplateDancelight::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + let mut outcome_weight = Weight::default(); + + // Check message processing + assert_expected_events!( + SimpleTemplateDancelight, + vec![ + RuntimeEvent::MessageQueue( + pallet_message_queue::Event::Processed { + success: true, + weight_used, + .. + }) => { + weight_used: { + outcome_weight = *weight_used; + weight_used.all_gte(Weight::from_parts(0,0)) + }, + }, + ] + ); + + type ForeignAssets = + ::ForeignAssets; + + // Calculate native balance based on weight + let native_balance = + container_chain_template_simple_runtime::WeightToFee::weight_to_fee(&outcome_weight); + + // Verify receiver's balance + assert_eq!( + >::balance( + dancelight_token_asset_id, + &SimpleTemplateEmptyReceiver::get(), + ), + amount_to_send - native_balance + ); + }); +}