-
Notifications
You must be signed in to change notification settings - Fork 382
Refactor 🔧: Add helpers & split Moonbase XCM tests #3367
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
Open
pLabarta
wants to merge
14
commits into
master
Choose a base branch
from
pablo/refactor-moonbase-xcm-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,855
−4,949
Open
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c5fdc31
refactor: split moonbase xcm tests
pLabarta 079cd79
add: create new helpers for xcm tests
pLabarta 8a9c8ed
add: implement new xcm testing helpers
pLabarta c5ff09b
refactor: reorganize xcm test helpers
pLabarta b891bd0
chore: edit some comments
pLabarta 3780390
fix: rm unused imports/pub
pLabarta b2e8d63
fix: add copyright
pLabarta e36e994
fix: make statemint setup struct pub again
pLabarta cffc457
rm: shorten redundant comments
pLabarta 8870197
rm: useless reset helper
pLabarta a2e342a
rm: unused imports
pLabarta 6662ca1
refactor: inline single use helpers
pLabarta fa35ace
fix: missing imports & rm unused
pLabarta 57f14f3
Merge branch 'master' into pablo/refactor-moonbase-xcm-tests
pLabarta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright 2019-2025 PureStake Inc. | ||
| // This file is part of Moonbeam. | ||
|
|
||
| // Moonbeam 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. | ||
|
|
||
| // Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| mod xcm_mock; | ||
| mod xcm_testing; |
313 changes: 313 additions & 0 deletions
313
runtime/moonbase/tests/xcm_testing/automatic_versioning.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,313 @@ | ||||
| // Copyright 2019-2025 PureStake Inc. | ||||
| // This file is part of Moonbeam. | ||||
|
|
||||
| // Moonbeam 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. | ||||
|
|
||||
| // Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>. | ||||
|
|
||||
| use crate::xcm_mock::*; | ||||
| use crate::{ | ||||
| xcm_mock::parachain::PolkadotXcm, | ||||
| xcm_testing::{add_supported_asset, currency_to_asset}, | ||||
| }; | ||||
| use frame_support::{assert_ok, weights::Weight}; | ||||
| use moonbase_runtime::xcm_config::AssetType; | ||||
| use sp_std::boxed::Box; | ||||
| use xcm::{ | ||||
| latest::prelude::{ | ||||
| AccountKey20, Location, PalletInstance, Parachain, QueryResponse, Response, WeightLimit, | ||||
| Xcm, | ||||
| }, | ||||
| VersionedAssets, | ||||
| }; | ||||
| use xcm::{VersionedLocation, WrapVersion}; | ||||
| use xcm_primitives::{split_location_into_chain_part_and_beneficiary, DEFAULT_PROOF_SIZE}; | ||||
| use xcm_simulator::TestExt; | ||||
|
|
||||
| #[test] | ||||
| fn test_automatic_versioning_on_runtime_upgrade_with_relay() { | ||||
| MockNet::reset(); | ||||
|
|
||||
| let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent()); | ||||
| let asset_metadata = parachain::AssetMetadata { | ||||
| name: b"RelayToken".to_vec(), | ||||
| symbol: b"Relay".to_vec(), | ||||
| decimals: 12, | ||||
| }; | ||||
| // register relay asset in parachain A and set XCM version to 1 | ||||
| ParaA::execute_with(|| { | ||||
| parachain::XcmVersioner::set_version(1); | ||||
| assert_ok!(AssetManager::register_foreign_asset( | ||||
| parachain::RuntimeOrigin::root(), | ||||
| source_location.clone(), | ||||
| asset_metadata, | ||||
| 1u128, | ||||
| true | ||||
| )); | ||||
| assert_ok!(add_supported_asset(source_location, 0)); | ||||
| }); | ||||
|
|
||||
| let response = Response::Version(2); | ||||
| let querier: Location = [].into(); | ||||
|
|
||||
| // This is irrelevant, nothing will be done with this message, | ||||
| // but we need to pass a message as an argument to trigger the storage change | ||||
| let mock_message: Xcm<()> = Xcm(vec![QueryResponse { | ||||
| query_id: 0, | ||||
| response, | ||||
| max_weight: Weight::zero(), | ||||
| querier: Some(querier), | ||||
| }]); | ||||
| // The router is mocked, and we cannot use WrapVersion in ChildParachainRouter. So we will force | ||||
| // it directly here | ||||
| // Actually send relay asset to parachain | ||||
| let dest: Location = AccountKey20 { | ||||
| network: None, | ||||
| key: PARAALICE, | ||||
| } | ||||
| .into(); | ||||
|
|
||||
| Relay::execute_with(|| { | ||||
| // This sets the default version, for not known destinations | ||||
| assert_ok!(RelayChainPalletXcm::force_default_xcm_version( | ||||
| relay_chain::RuntimeOrigin::root(), | ||||
| Some(3) | ||||
| )); | ||||
|
|
||||
| // Wrap version, which sets VersionedStorage | ||||
| // This is necessary because the mock router does not use wrap_version, but | ||||
| // this is not necessary in prod | ||||
| assert_ok!(<RelayChainPalletXcm as WrapVersion>::wrap_version( | ||||
| &Parachain(1).into(), | ||||
| mock_message | ||||
| )); | ||||
|
|
||||
| // Transfer assets. Since it is an unknown destination, it will query for version | ||||
| assert_ok!(RelayChainPalletXcm::limited_reserve_transfer_assets( | ||||
| relay_chain::RuntimeOrigin::signed(RELAYALICE), | ||||
| Box::new(Parachain(1).into()), | ||||
| Box::new(VersionedLocation::from(dest).clone()), | ||||
| Box::new(([], 123).into()), | ||||
| 0, | ||||
| WeightLimit::Unlimited | ||||
| )); | ||||
|
|
||||
| // Let's advance the relay. This should trigger the subscription message | ||||
| relay_chain::relay_roll_to(2); | ||||
|
|
||||
| // queries should have been updated | ||||
| assert!(RelayChainPalletXcm::query(0).is_some()); | ||||
| }); | ||||
|
|
||||
| let expected_supported_version: relay_chain::RuntimeEvent = | ||||
| pallet_xcm::Event::SupportedVersionChanged { | ||||
| location: Location { | ||||
| parents: 0, | ||||
| interior: [Parachain(1)].into(), | ||||
| }, | ||||
| version: 1, | ||||
| } | ||||
| .into(); | ||||
|
|
||||
| Relay::execute_with(|| { | ||||
| // Assert that the events vector contains the version change | ||||
| assert!(relay_chain::relay_events().contains(&expected_supported_version)); | ||||
| }); | ||||
|
|
||||
| // ParaA changes version to 2, and calls on_runtime_upgrade. This should notify the targets | ||||
| // of the new version change | ||||
| ParaA::execute_with(|| { | ||||
| // Set version | ||||
| parachain::XcmVersioner::set_version(2); | ||||
| // Do runtime upgrade | ||||
| parachain::on_runtime_upgrade(); | ||||
| // Initialize block, to call on_initialize and notify targets | ||||
| parachain::para_roll_to(2); | ||||
| // Expect the event in the parachain | ||||
| assert!(parachain::para_events().iter().any(|e| matches!( | ||||
| e, | ||||
| parachain::RuntimeEvent::PolkadotXcm(pallet_xcm::Event::VersionChangeNotified { | ||||
| result: 2, | ||||
| .. | ||||
| }) | ||||
| ))); | ||||
| }); | ||||
|
|
||||
| // This event should have been seen in the relay | ||||
| let expected_supported_version_2: relay_chain::RuntimeEvent = | ||||
| pallet_xcm::Event::SupportedVersionChanged { | ||||
| location: Location { | ||||
| parents: 0, | ||||
| interior: [Parachain(1)].into(), | ||||
| }, | ||||
| version: 2, | ||||
| } | ||||
| .into(); | ||||
|
|
||||
| Relay::execute_with(|| { | ||||
| // Assert that the events vector contains the new version change | ||||
| assert!(relay_chain::relay_events().contains(&expected_supported_version_2)); | ||||
| }); | ||||
| } | ||||
|
|
||||
| #[test] | ||||
| fn test_automatic_versioning_on_runtime_upgrade_with_para_b() { | ||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Original moonbeam/runtime/moonbase/tests/xcm_tests.rs Line 2169 in fb5af73
|
||||
| MockNet::reset(); | ||||
|
|
||||
| let para_a_balances = Location::new(1, [Parachain(1), PalletInstance(1u8)]); | ||||
| let source_location: AssetType = para_a_balances | ||||
| .try_into() | ||||
| .expect("Location convertion to AssetType should succeed"); | ||||
| let source_id: parachain::AssetId = source_location.clone().into(); | ||||
|
|
||||
| let asset_metadata = parachain::AssetMetadata { | ||||
| name: b"ParaAToken".to_vec(), | ||||
| symbol: b"ParaA".to_vec(), | ||||
| decimals: 18, | ||||
| }; | ||||
| let response = Response::Version(2); | ||||
| let querier: Location = [].into(); | ||||
|
|
||||
| // This is irrelevant, nothing will be done with this message, | ||||
| // but we need to pass a message as an argument to trigger the storage change | ||||
| let mock_message: Xcm<()> = Xcm(vec![QueryResponse { | ||||
| query_id: 0, | ||||
| response, | ||||
| max_weight: Weight::zero(), | ||||
| querier: Some(querier), | ||||
| }]); | ||||
|
|
||||
| ParaA::execute_with(|| { | ||||
| // advertised version | ||||
| parachain::XcmVersioner::set_version(2); | ||||
| }); | ||||
|
|
||||
| ParaB::execute_with(|| { | ||||
| // Let's try with v0 | ||||
| parachain::XcmVersioner::set_version(0); | ||||
|
|
||||
| assert_ok!(AssetManager::register_foreign_asset( | ||||
| parachain::RuntimeOrigin::root(), | ||||
| source_location.clone(), | ||||
| asset_metadata, | ||||
| 1u128, | ||||
| true | ||||
| )); | ||||
| assert_ok!(add_supported_asset(source_location, 0)); | ||||
| }); | ||||
|
|
||||
| ParaA::execute_with(|| { | ||||
| // This sets the default version, for not known destinations | ||||
| assert_ok!(ParachainPalletXcm::force_default_xcm_version( | ||||
| parachain::RuntimeOrigin::root(), | ||||
| Some(3) | ||||
| )); | ||||
| // Wrap version, which sets VersionedStorage | ||||
| assert_ok!(<ParachainPalletXcm as WrapVersion>::wrap_version( | ||||
| &Location::new(1, [Parachain(2)]).into(), | ||||
| mock_message | ||||
| )); | ||||
|
|
||||
| parachain::para_roll_to(2); | ||||
|
|
||||
| // queries should have been updated | ||||
| assert!(ParachainPalletXcm::query(0).is_some()); | ||||
| }); | ||||
|
|
||||
| let expected_supported_version: parachain::RuntimeEvent = | ||||
| pallet_xcm::Event::SupportedVersionChanged { | ||||
| location: Location { | ||||
| parents: 1, | ||||
| interior: [Parachain(2)].into(), | ||||
| }, | ||||
| version: 0, | ||||
| } | ||||
| .into(); | ||||
|
|
||||
| ParaA::execute_with(|| { | ||||
| // Assert that the events vector contains the version change | ||||
| assert!(parachain::para_events().contains(&expected_supported_version)); | ||||
| }); | ||||
|
|
||||
| // Let's ensure talking in v0 works | ||||
| let dest = Location { | ||||
| parents: 1, | ||||
| interior: [ | ||||
| Parachain(2), | ||||
| AccountKey20 { | ||||
| network: None, | ||||
| key: PARAALICE.into(), | ||||
| }, | ||||
| ] | ||||
| .into(), | ||||
| } | ||||
| .into(); | ||||
| let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap(); | ||||
| ParaA::execute_with(|| { | ||||
| let asset = currency_to_asset(parachain::CurrencyId::SelfReserve, 100); | ||||
| // free execution, full amount received | ||||
| assert_ok!(PolkadotXcm::transfer_assets( | ||||
| parachain::RuntimeOrigin::signed(PARAALICE.into()), | ||||
| Box::new(VersionedLocation::from(chain_part)), | ||||
| Box::new(VersionedLocation::from(beneficiary)), | ||||
| Box::new(VersionedAssets::from(vec![asset])), | ||||
| 0, | ||||
| WeightLimit::Limited(Weight::from_parts(80u64, DEFAULT_PROOF_SIZE)) | ||||
| )); | ||||
| // free execution, full amount received | ||||
| assert_eq!( | ||||
| ParaBalances::free_balance(&PARAALICE.into()), | ||||
| INITIAL_BALANCE - 100 | ||||
| ); | ||||
| }); | ||||
|
|
||||
| ParaB::execute_with(|| { | ||||
| // free execution, full amount received | ||||
| assert_eq!(Assets::balance(source_id, &PARAALICE.into()), 100); | ||||
| }); | ||||
|
|
||||
| // ParaB changes version to 2, and calls on_runtime_upgrade. This should notify the targets | ||||
| // of the new version change | ||||
| ParaB::execute_with(|| { | ||||
| // Set version | ||||
| parachain::XcmVersioner::set_version(2); | ||||
| // Do runtime upgrade | ||||
| parachain::on_runtime_upgrade(); | ||||
| // Initialize block, to call on_initialize and notify targets | ||||
| parachain::para_roll_to(2); | ||||
| // Expect the event in the parachain | ||||
| assert!(parachain::para_events().iter().any(|e| matches!( | ||||
| e, | ||||
| parachain::RuntimeEvent::PolkadotXcm(pallet_xcm::Event::VersionChangeNotified { | ||||
| result: 2, | ||||
| .. | ||||
| }) | ||||
| ))); | ||||
| }); | ||||
|
|
||||
| // This event should have been seen in para A | ||||
| let expected_supported_version_2: parachain::RuntimeEvent = | ||||
| pallet_xcm::Event::SupportedVersionChanged { | ||||
| location: Location { | ||||
| parents: 1, | ||||
| interior: [Parachain(2)].into(), | ||||
| }, | ||||
| version: 2, | ||||
| } | ||||
| .into(); | ||||
|
|
||||
| // Para A should have received the version change | ||||
| ParaA::execute_with(|| { | ||||
| // Assert that the events vector contains the new version change | ||||
| assert!(parachain::para_events().contains(&expected_supported_version_2)); | ||||
| }); | ||||
| } | ||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Original
moonbeam/runtime/moonbase/tests/xcm_tests.rs
Line 2043 in fb5af73