From 3e579d8a44b35edc308e53ae9fcfb29f799d096a Mon Sep 17 00:00:00 2001 From: gilescope Date: Sat, 27 May 2023 21:10:03 +0100 Subject: [PATCH 1/7] mostly there with westend --- Cargo.lock | 30 ++++ Cargo.toml | 2 +- .../emulated/assets/westmint/Cargo.toml | 37 +++++ .../emulated/assets/westmint/src/lib.rs | 34 ++++ .../emulated/assets/westmint/src/tests/mod.rs | 3 + .../westmint/src/tests/reserve_transfer.rs | 63 ++++++++ .../assets/westmint/src/tests/teleport.rs | 60 +++++++ .../assets/westmint/src/tests/transact.rs | 58 +++++++ .../emulated/common/Cargo.toml | 3 + .../emulated/common/src/constants.rs | 149 +++++++++++++++++- .../emulated/common/src/lib.rs | 44 +++++- 11 files changed, 480 insertions(+), 3 deletions(-) create mode 100644 parachains/integration-tests/emulated/assets/westmint/Cargo.toml create mode 100644 parachains/integration-tests/emulated/assets/westmint/src/lib.rs create mode 100644 parachains/integration-tests/emulated/assets/westmint/src/tests/mod.rs create mode 100644 parachains/integration-tests/emulated/assets/westmint/src/tests/reserve_transfer.rs create mode 100644 parachains/integration-tests/emulated/assets/westmint/src/tests/teleport.rs create mode 100644 parachains/integration-tests/emulated/assets/westmint/src/tests/transact.rs diff --git a/Cargo.lock b/Cargo.lock index 822c76a9e4e..99699b0b36d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4878,6 +4878,9 @@ dependencies = [ "sp-weights", "statemine-runtime", "statemint-runtime", + "westend-runtime", + "westend-runtime-constants", + "westmint-runtime", "xcm", "xcm-emulator", "xcm-executor", @@ -15687,6 +15690,33 @@ dependencies = [ "sp-weights", ] +[[package]] +name = "westmint-it" +version = "1.0.0" +dependencies = [ + "frame-support", + "frame-system", + "integration-tests-common", + "pallet-assets", + "pallet-balances", + "pallet-xcm", + "parachains-common", + "parity-scale-codec", + "penpal-runtime", + "polkadot-core-primitives", + "polkadot-parachain", + "polkadot-runtime", + "polkadot-runtime-parachains", + "sp-core", + "sp-runtime", + "sp-weights", + "westend-runtime", + "westmint-runtime", + "xcm", + "xcm-emulator", + "xcm-executor", +] + [[package]] name = "westmint-runtime" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index faec5a0288a..e4bfaf3fe39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ members = [ "parachains/runtimes/glutton/glutton-kusama", "parachains/runtimes/testing/penpal", "parachains/integration-tests/emulated/common", + "parachains/integration-tests/emulated/assets/westmint", "parachains/integration-tests/emulated/assets/statemine", "parachains/integration-tests/emulated/assets/statemint", "test/client", @@ -71,4 +72,3 @@ opt-level = 3 inherits = "release" lto = true codegen-units = 1 - diff --git a/parachains/integration-tests/emulated/assets/westmint/Cargo.toml b/parachains/integration-tests/emulated/assets/westmint/Cargo.toml new file mode 100644 index 00000000000..34a2b992096 --- /dev/null +++ b/parachains/integration-tests/emulated/assets/westmint/Cargo.toml @@ -0,0 +1,37 @@ +[package] +name = "westmint-it" +version = "1.0.0" +authors = ["Parity Technologies "] +edition = "2021" +description = "Westmint parachain runtime integration tests with xcm-emulator" + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.4.0", default-features = false } + +# Substrate +sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" } +frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" } +frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" } +sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" } +sp-weights = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" } +pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" } +pallet-assets = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "master" } + +# Polkadot +polkadot-core-primitives = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-parachain = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-runtime = { git = "https://github.com/paritytech/polkadot", branch = "master" } +westend-runtime = { git = "https://github.com/paritytech/polkadot", branch = "master" } +xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "master" } +xcm-executor = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "master" } +pallet-xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "master" } + +# Cumulus +parachains-common = { path = "../../../../common" } +penpal-runtime = { path = "../../../../runtimes/testing/penpal" } +westmint-runtime = { path = "../../../../runtimes/assets/westmint" } + +# Local +xcm-emulator = { default-features = false, path = "../../../../../xcm/xcm-emulator" } +integration-tests-common = { default-features = false, path = "../../common" } diff --git a/parachains/integration-tests/emulated/assets/westmint/src/lib.rs b/parachains/integration-tests/emulated/assets/westmint/src/lib.rs new file mode 100644 index 00000000000..1cd3e06f251 --- /dev/null +++ b/parachains/integration-tests/emulated/assets/westmint/src/lib.rs @@ -0,0 +1,34 @@ +pub use codec::Encode; +pub use frame_support::{ + assert_ok, instances::Instance1, pallet_prelude::Weight, traits::fungibles::Inspect, +}; +pub use integration_tests_common::{ + constants::{ + accounts::{ALICE, BOB}, + polkadot::ED as POLKADOT_ED, + PROOF_SIZE_THRESHOLD, REF_TIME_THRESHOLD, XCM_V3, + }, + AccountId, BHKusama, BHKusamaPallet, BHKusamaReceiver, BHKusamaSender, BHPolkadot, + BHPolkadotPallet, BHPolkadotReceiver, BHPolkadotSender, Collectives, CollectivesPallet, + CollectivesReceiver, CollectivesSender, Kusama, KusamaMockNet, KusamaPallet, KusamaReceiver, + KusamaSender, PenpalKusama, PenpalKusamaReceiver, PenpalKusamaSender, PenpalPolkadot, + PenpalPolkadotReceiver, PenpalPolkadotSender, Polkadot, PolkadotMockNet, PolkadotPallet, + PolkadotReceiver, PolkadotSender, Statemine, StateminePallet, StatemineReceiver, + StatemineSender, Statemint, StatemintPallet, StatemintReceiver, StatemintSender, Westmint, + WestmintPallet, WestmintReceiver, WestmintSender, +}; +pub use polkadot_core_primitives::InboundDownwardMessage; +pub use xcm::{ + prelude::*, + v3::{ + Error, + NetworkId::{Kusama as KusamaId, Polkadot as PolkadotId}, + }, +}; +pub use xcm_emulator::{ + assert_expected_events, bx, cumulus_pallet_dmp_queue, helpers::weight_within_threshold, + Parachain as Para, RelayChain as Relay, TestExt, +}; + +#[cfg(test)] +mod tests; diff --git a/parachains/integration-tests/emulated/assets/westmint/src/tests/mod.rs b/parachains/integration-tests/emulated/assets/westmint/src/tests/mod.rs new file mode 100644 index 00000000000..996f9fd0aae --- /dev/null +++ b/parachains/integration-tests/emulated/assets/westmint/src/tests/mod.rs @@ -0,0 +1,3 @@ +mod reserve_transfer; +mod teleport; +mod transact; diff --git a/parachains/integration-tests/emulated/assets/westmint/src/tests/reserve_transfer.rs b/parachains/integration-tests/emulated/assets/westmint/src/tests/reserve_transfer.rs new file mode 100644 index 00000000000..43fa60c75cd --- /dev/null +++ b/parachains/integration-tests/emulated/assets/westmint/src/tests/reserve_transfer.rs @@ -0,0 +1,63 @@ +use crate::*; + +#[test] +fn reserve_transfer_native_asset_from_relay_to_assets() { + // Init tests variables + let amount = POLKADOT_ED * 1000; + let relay_sender_balance_before = Westend::account_data_of(WestendSender::get()).free; + let para_receiver_balance_before = Westmint::account_data_of(WestmintReceiver::get()).free; + + let origin = ::RuntimeOrigin::signed(WestendSender::get()); + let assets_para_destination: VersionedMultiLocation = + Westend::child_location_of(Westmint::para_id()).into(); + let beneficiary: VersionedMultiLocation = + AccountId32 { network: None, id: WestmintReceiver::get().into() }.into(); + let native_assets: VersionedMultiAssets = (Here, amount).into(); + let fee_asset_item = 0; + let weight_limit = WeightLimit::Unlimited; + + // Send XCM message from Relay Chain + Westend::execute_with(|| { + assert_ok!(::XcmPallet::limited_reserve_transfer_assets( + origin, + bx!(assets_para_destination), + bx!(beneficiary), + bx!(native_assets), + fee_asset_item, + weight_limit, + )); + + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + Westend, + vec![ + RuntimeEvent::XcmPallet(pallet_xcm::Event::Attempted { outcome: Outcome::Complete(weight) }) => { + weight: weight_within_threshold((REF_TIME_THRESHOLD, PROOF_SIZE_THRESHOLD), Weight::from_parts(2_000_000_000, 0), *weight), + }, + ] + ); + }); + + // Receive XCM message in Assets Parachain + Westmint::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + Westmint, + vec![ + RuntimeEvent::DmpQueue(cumulus_pallet_dmp_queue::Event::ExecutedDownward { + outcome: Outcome::Incomplete(_, Error::UntrustedReserveLocation), + .. + }) => {}, + ] + ); + }); + + // Check if balances are updated accordingly in Relay Chain and Assets Parachain + let relay_sender_balance_after = Westend::account_data_of(WestendSender::get()).free; + let para_sender_balance_after = Westmint::account_data_of(WestmintReceiver::get()).free; + + assert_eq!(relay_sender_balance_before - amount, relay_sender_balance_after); + assert_eq!(para_sender_balance_after, para_receiver_balance_before); +} diff --git a/parachains/integration-tests/emulated/assets/westmint/src/tests/teleport.rs b/parachains/integration-tests/emulated/assets/westmint/src/tests/teleport.rs new file mode 100644 index 00000000000..0ac2f113a73 --- /dev/null +++ b/parachains/integration-tests/emulated/assets/westmint/src/tests/teleport.rs @@ -0,0 +1,60 @@ +use crate::*; + +#[test] +fn teleport_native_assets_from_relay_to_assets_para() { + // Init tests variables + let amount = POLKADOT_ED * 1000; + let relay_sender_balance_before = Westend::account_data_of(WestendSender::get()).free; + let para_receiver_balance_before = Westmint::account_data_of(WestmintReceiver::get()).free; + + let origin = ::RuntimeOrigin::signed(WestendSender::get()); + let assets_para_destination: VersionedMultiLocation = + Westend::child_location_of(Westmint::para_id()).into(); + let beneficiary: VersionedMultiLocation = + AccountId32 { network: None, id: WestmintReceiver::get().into() }.into(); + let native_assets: VersionedMultiAssets = (Here, amount).into(); + let fee_asset_item = 0; + let weight_limit = WeightLimit::Unlimited; + + // Send XCM message from Relay Chain + Westend::execute_with(|| { + assert_ok!(::XcmPallet::limited_teleport_assets( + origin, + bx!(assets_para_destination), + bx!(beneficiary), + bx!(native_assets), + fee_asset_item, + weight_limit, + )); + + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + Westend, + vec![ + RuntimeEvent::XcmPallet(pallet_xcm::Event::Attempted { outcome: Outcome::Complete { .. } }) => {}, + ] + ); + }); + + // Receive XCM message in Assets Parachain + Westmint::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + Westmint, + vec![ + RuntimeEvent::Balances(pallet_balances::Event::Deposit { who, .. }) => { + who: *who == WestmintReceiver::get().into(), + }, + ] + ); + }); + + // Check if balances are updated accordingly in Relay Chain and Assets Parachain + let relay_sender_balance_after = Westend::account_data_of(WestendSender::get()).free; + let para_sender_balance_after = Westmint::account_data_of(WestmintReceiver::get()).free; + + assert_eq!(relay_sender_balance_before - amount, relay_sender_balance_after); + assert!(para_sender_balance_after > para_receiver_balance_before); +} diff --git a/parachains/integration-tests/emulated/assets/westmint/src/tests/transact.rs b/parachains/integration-tests/emulated/assets/westmint/src/tests/transact.rs new file mode 100644 index 00000000000..21ca0cb081c --- /dev/null +++ b/parachains/integration-tests/emulated/assets/westmint/src/tests/transact.rs @@ -0,0 +1,58 @@ +use crate::*; + +#[test] +fn transact_sudo_from_relay_to_assets_para() { + // Init tests variables + // Call to be executed in Assets Parachain + const ASSET_ID: u32 = 1; + + let call = ::RuntimeCall::Assets(pallet_assets::Call::< + ::Runtime, + Instance1, + >::force_create { + id: ASSET_ID.into(), + is_sufficient: true, + min_balance: 1000, + owner: WestmintSender::get().into(), + }) + .encode() + .into(); + + // XcmPallet send arguments + let sudo_origin = ::RuntimeOrigin::root(); + let assets_para_destination: VersionedMultiLocation = + Westend::child_location_of(Westmint::para_id()).into(); + + let weight_limit = WeightLimit::Unlimited; + let require_weight_at_most = Weight::from_parts(1000000000, 200000); + let origin_kind = OriginKind::Superuser; + let check_origin = None; + + let xcm = VersionedXcm::from(Xcm(vec![ + UnpaidExecution { weight_limit, check_origin }, + Transact { require_weight_at_most, origin_kind, call }, + ])); + + // Send XCM message from Relay Chain + Westend::execute_with(|| { + assert_ok!(::XcmPallet::send( + sudo_origin, + bx!(assets_para_destination), + bx!(xcm), + )); + + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + Westend, + vec![ + RuntimeEvent::XcmPallet(pallet_xcm::Event::Sent { .. }) => {}, + ] + ); + }); + + // Receive XCM message in Assets Parachain + Westmint::execute_with(|| { + assert!(::Assets::asset_exists(ASSET_ID)); + }); +} diff --git a/parachains/integration-tests/emulated/common/Cargo.toml b/parachains/integration-tests/emulated/common/Cargo.toml index a13ceac2cff..75a6d128b6d 100644 --- a/parachains/integration-tests/emulated/common/Cargo.toml +++ b/parachains/integration-tests/emulated/common/Cargo.toml @@ -32,6 +32,8 @@ polkadot-runtime = { git = "https://github.com/paritytech/polkadot", branch = "m polkadot-runtime-constants = { git = "https://github.com/paritytech/polkadot", branch = "master" } kusama-runtime = { git = "https://github.com/paritytech/polkadot", branch = "master" } kusama-runtime-constants = { git = "https://github.com/paritytech/polkadot", branch = "master" } +westend-runtime = { git = "https://github.com/paritytech/polkadot", branch = "master" } +westend-runtime-constants = { git = "https://github.com/paritytech/polkadot", branch = "master" } xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "master" } xcm-executor = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "master" } pallet-xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "master" } @@ -43,6 +45,7 @@ cumulus-primitives-core = { path = "../../../../primitives/core" } penpal-runtime = { path = "../../../runtimes/testing/penpal" } statemint-runtime = { path = "../../../runtimes/assets/statemint" } statemine-runtime = { path = "../../../runtimes/assets/statemine" } +westmint-runtime = { path = "../../../runtimes/assets/westmint" } collectives-polkadot-runtime = { path = "../../../runtimes/collectives/collectives-polkadot" } bridge-hub-kusama-runtime = { path = "../../../runtimes/bridge-hubs/bridge-hub-kusama" } bridge-hub-polkadot-runtime = { path = "../../../runtimes/bridge-hubs/bridge-hub-polkadot" } diff --git a/parachains/integration-tests/emulated/common/src/constants.rs b/parachains/integration-tests/emulated/common/src/constants.rs index 694137fe094..293e515e083 100644 --- a/parachains/integration-tests/emulated/common/src/constants.rs +++ b/parachains/integration-tests/emulated/common/src/constants.rs @@ -209,6 +209,100 @@ pub mod polkadot { } } +pub mod westend { + use super::*; + pub const ED: Balance = westend_runtime_constants::currency::EXISTENTIAL_DEPOSIT; + const STASH: u128 = 100 * westend_runtime_constants::currency::UNITS; + + pub fn get_host_config() -> HostConfiguration { + HostConfiguration { + max_upward_queue_count: 10, + max_upward_queue_size: 51200, + max_upward_message_size: 51200, + max_upward_message_num_per_candidate: 10, + max_downward_message_size: 51200, + ..Default::default() + } + } + + fn session_keys( + babe: BabeId, + grandpa: GrandpaId, + im_online: ImOnlineId, + para_validator: ValidatorId, + para_assignment: AssignmentId, + authority_discovery: AuthorityDiscoveryId, + ) -> westend_runtime::SessionKeys { + westend_runtime::SessionKeys { + babe, + grandpa, + im_online, + para_validator, + para_assignment, + authority_discovery, + } + } + + pub fn genesis() -> Storage { + let genesis_config = westend_runtime::GenesisConfig { + system: westend_runtime::SystemConfig { + code: westend_runtime::WASM_BINARY.unwrap().to_vec(), + }, + balances: westend_runtime::BalancesConfig { + balances: accounts::init_balances() + .iter() + .cloned() + .map(|k| (k, ED * 4096)) + .collect(), + }, + session: westend_runtime::SessionConfig { + keys: validators::initial_authorities() + .iter() + .map(|x| { + ( + x.0.clone(), + x.0.clone(), + westend::session_keys( + x.2.clone(), + x.3.clone(), + x.4.clone(), + x.5.clone(), + x.6.clone(), + x.7.clone(), + ), + ) + }) + .collect::>(), + }, + staking: westend_runtime::StakingConfig { + validator_count: validators::initial_authorities().len() as u32, + minimum_validator_count: 1, + stakers: validators::initial_authorities() + .iter() + .map(|x| { + (x.0.clone(), x.1.clone(), STASH, westend_runtime::StakerStatus::Validator) + }) + .collect(), + invulnerables: validators::initial_authorities() + .iter() + .map(|x| x.0.clone()) + .collect(), + force_era: pallet_staking::Forcing::ForceNone, + slash_reward_fraction: Perbill::from_percent(10), + ..Default::default() + }, + babe: westend_runtime::BabeConfig { + authorities: Default::default(), + epoch_config: Some(westend_runtime::BABE_GENESIS_EPOCH_CONFIG), + }, + configuration: westend_runtime::ConfigurationConfig { config: get_host_config() }, + ..Default::default() + }; + + genesis_config.build_storage().unwrap() + } +} + // Kusama pub mod kusama { use super::*; @@ -359,7 +453,60 @@ pub mod statemint { } } -// Statemint +// Westmint +pub mod westmint { + use super::*; + pub const PARA_ID: u32 = 1000; + pub const ED: Balance = westmint_runtime::constants::currency::EXISTENTIAL_DEPOSIT; + + pub fn genesis() -> Storage { + let genesis_config = westmint_runtime::GenesisConfig { + system: westmint_runtime::SystemConfig { + code: westmint_runtime::WASM_BINARY + .expect("WASM binary was not build, please build it!") + .to_vec(), + }, + balances: westmint_runtime::BalancesConfig { + balances: accounts::init_balances() + .iter() + .cloned() + .map(|k| (k, ED * 4096)) + .collect(), + }, + parachain_info: westmint_runtime::ParachainInfoConfig { parachain_id: PARA_ID.into() }, + collator_selection: westmint_runtime::CollatorSelectionConfig { + invulnerables: collators::invulnerables_statemint() + .iter() + .cloned() + .map(|(acc, _)| acc) + .collect(), + candidacy_bond: ED * 16, + ..Default::default() + }, + session: westmint_runtime::SessionConfig { + keys: collators::invulnerables() + .into_iter() + .map(|(acc, aura)| { + ( + acc.clone(), // account id + acc, // validator id + westmint_runtime::SessionKeys { aura }, // session keys + ) + }) + .collect(), + }, + aura: Default::default(), + aura_ext: Default::default(), + parachain_system: Default::default(), + polkadot_xcm: westmint_runtime::PolkadotXcmConfig { + safe_xcm_version: Some(SAFE_XCM_VERSION), + }, + }; + + genesis_config.build_storage().unwrap() + } +} +// Statemine pub mod statemine { use super::*; pub const PARA_ID: u32 = 1000; diff --git a/parachains/integration-tests/emulated/common/src/lib.rs b/parachains/integration-tests/emulated/common/src/lib.rs index 49e919b5de9..908542ad0d8 100644 --- a/parachains/integration-tests/emulated/common/src/lib.rs +++ b/parachains/integration-tests/emulated/common/src/lib.rs @@ -3,7 +3,7 @@ pub mod constants; pub use constants::{ accounts::{ALICE, BOB}, bridge_hub_kusama, bridge_hub_polkadot, collectives, kusama, penpal, polkadot, statemine, - statemint, + statemint, westend, westmint, }; use frame_support::{parameter_types, sp_io, sp_tracing}; pub use parachains_common::{AccountId, AuraId, Balance, BlockNumber, StatemintAuraId}; @@ -16,6 +16,25 @@ use xcm_emulator::{ use xcm_executor::traits::Convert; decl_test_relay_chains! { + pub struct Westend { + genesis = westend::genesis(), + on_init = (), + runtime = { + Runtime: westend_runtime::Runtime, + RuntimeOrigin: westend_runtime::RuntimeOrigin, + RuntimeCall: westend_runtime::RuntimeCall, + RuntimeEvent: westend_runtime::RuntimeEvent, + MessageQueue: westend_runtime::MessageQueue, + XcmConfig: westend_runtime::xcm_config::XcmConfig, + SovereignAccountOf: westend_runtime::xcm_config::LocationConverter, //TODO: rename to SovereignAccountOf, + System: westend_runtime::System, + Balances: westend_runtime::Balances, + }, + pallets_extra = { + XcmPallet: westend_runtime::XcmPallet, + Sudo: westend_runtime::Sudo, + } + }, pub struct Polkadot { genesis = polkadot::genesis(), on_init = (), @@ -55,6 +74,28 @@ decl_test_relay_chains! { } decl_test_parachains! { + // Westend + pub struct Westmint { + genesis = westmint::genesis(), + on_init = (), + runtime = { + Runtime: westmint_runtime::Runtime, + RuntimeOrigin: westmint_runtime::RuntimeOrigin, + RuntimeCall: westmint_runtime::RuntimeCall, + RuntimeEvent: westmint_runtime::RuntimeEvent, + XcmpMessageHandler: westmint_runtime::XcmpQueue, + DmpMessageHandler: westmint_runtime::DmpQueue, + LocationToAccountId: westmint_runtime::xcm_config::LocationToAccountId, + System: westmint_runtime::System, + Balances: westmint_runtime::Balances, + ParachainSystem: westmint_runtime::ParachainSystem, + ParachainInfo: westmint_runtime::ParachainInfo, + }, + pallets_extra = { + PolkadotXcm: westmint_runtime::PolkadotXcm, + Assets: westmint_runtime::Assets, + } + }, // Polkadot pub struct Statemint { genesis = statemint::genesis(), @@ -98,6 +139,7 @@ decl_test_parachains! { Assets: penpal_runtime::Assets, } }, + // Kusama pub struct Statemine { genesis = statemine::genesis(), From ca03cc10ad8e66438b6254c0154998b3ad8de6dc Mon Sep 17 00:00:00 2001 From: gilescope Date: Sun, 28 May 2023 08:18:31 +0100 Subject: [PATCH 2/7] add network --- .../integration-tests/emulated/common/src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/parachains/integration-tests/emulated/common/src/lib.rs b/parachains/integration-tests/emulated/common/src/lib.rs index 908542ad0d8..27736b305e9 100644 --- a/parachains/integration-tests/emulated/common/src/lib.rs +++ b/parachains/integration-tests/emulated/common/src/lib.rs @@ -139,7 +139,7 @@ decl_test_parachains! { Assets: penpal_runtime::Assets, } }, - + // Kusama pub struct Statemine { genesis = statemine::genesis(), @@ -263,6 +263,12 @@ decl_test_networks! { PenpalKusama, BHKusama, ], + }, + pub struct WestendMockNet { + relay_chain = Westend, + parachains = vec![ + Westmint, + ], } } @@ -273,6 +279,12 @@ parameter_types! { // Kusama pub KusamaSender: AccountId = Kusama::account_id_of(ALICE); pub KusamaReceiver: AccountId = Kusama::account_id_of(BOB); + // Westend + pub WestendSender: AccountId = Westend::account_id_of(ALICE); + pub WestendReceiver: AccountId = Westend::account_id_of(BOB); + // Westmint + pub WestmintSender: AccountId = Westmint::account_id_of(ALICE); + pub WestmintReceiver: AccountId = Westmint::account_id_of(BOB); // Statemint pub StatemintSender: AccountId = Statemint::account_id_of(ALICE); pub StatemintReceiver: AccountId = Statemint::account_id_of(BOB); From b51165dd774c2702bf9ebc2797ea20efcc45dcaf Mon Sep 17 00:00:00 2001 From: gilescope Date: Sun, 28 May 2023 09:57:57 +0100 Subject: [PATCH 3/7] initial way to set host api version --- .../emulated/assets/westmint/src/lib.rs | 13 ++-- .../emulated/common/src/lib.rs | 28 +++++++ xcm/xcm-emulator/src/lib.rs | 78 ++++++++++++++++++- 3 files changed, 107 insertions(+), 12 deletions(-) diff --git a/parachains/integration-tests/emulated/assets/westmint/src/lib.rs b/parachains/integration-tests/emulated/assets/westmint/src/lib.rs index 1cd3e06f251..dd2114a9d21 100644 --- a/parachains/integration-tests/emulated/assets/westmint/src/lib.rs +++ b/parachains/integration-tests/emulated/assets/westmint/src/lib.rs @@ -8,14 +8,11 @@ pub use integration_tests_common::{ polkadot::ED as POLKADOT_ED, PROOF_SIZE_THRESHOLD, REF_TIME_THRESHOLD, XCM_V3, }, - AccountId, BHKusama, BHKusamaPallet, BHKusamaReceiver, BHKusamaSender, BHPolkadot, - BHPolkadotPallet, BHPolkadotReceiver, BHPolkadotSender, Collectives, CollectivesPallet, - CollectivesReceiver, CollectivesSender, Kusama, KusamaMockNet, KusamaPallet, KusamaReceiver, - KusamaSender, PenpalKusama, PenpalKusamaReceiver, PenpalKusamaSender, PenpalPolkadot, - PenpalPolkadotReceiver, PenpalPolkadotSender, Polkadot, PolkadotMockNet, PolkadotPallet, - PolkadotReceiver, PolkadotSender, Statemine, StateminePallet, StatemineReceiver, - StatemineSender, Statemint, StatemintPallet, StatemintReceiver, StatemintSender, Westmint, - WestmintPallet, WestmintReceiver, WestmintSender, + AccountId,Collectives, CollectivesPallet, + CollectivesReceiver, CollectivesSender, + + Westmint, + WestmintPallet, WestmintReceiver, WestmintSender,PenpalWestend, }; pub use polkadot_core_primitives::InboundDownwardMessage; pub use xcm::{ diff --git a/parachains/integration-tests/emulated/common/src/lib.rs b/parachains/integration-tests/emulated/common/src/lib.rs index 27736b305e9..db1198e33fa 100644 --- a/parachains/integration-tests/emulated/common/src/lib.rs +++ b/parachains/integration-tests/emulated/common/src/lib.rs @@ -16,6 +16,7 @@ use xcm_emulator::{ use xcm_executor::traits::Convert; decl_test_relay_chains! { + #[api_version(5)] pub struct Westend { genesis = westend::genesis(), on_init = (), @@ -35,6 +36,7 @@ decl_test_relay_chains! { Sudo: westend_runtime::Sudo, } }, + #[api_version(4)] pub struct Polkadot { genesis = polkadot::genesis(), on_init = (), @@ -53,6 +55,7 @@ decl_test_relay_chains! { XcmPallet: polkadot_runtime::XcmPallet, } }, + #[api_version(4)] pub struct Kusama { genesis = kusama::genesis(), on_init = (), @@ -139,6 +142,27 @@ decl_test_parachains! { Assets: penpal_runtime::Assets, } }, + pub struct PenpalWestend { + genesis = penpal::genesis(penpal::PARA_ID), + on_init = (), + runtime = { + Runtime: penpal_runtime::Runtime, + RuntimeOrigin: penpal_runtime::RuntimeOrigin, + RuntimeCall: penpal_runtime::RuntimeCall, + RuntimeEvent: penpal_runtime::RuntimeEvent, + XcmpMessageHandler: penpal_runtime::XcmpQueue, + DmpMessageHandler: penpal_runtime::DmpQueue, + LocationToAccountId: penpal_runtime::xcm_config::LocationToAccountId, + System: penpal_runtime::System, + Balances: penpal_runtime::Balances, + ParachainSystem: penpal_runtime::ParachainSystem, + ParachainInfo: penpal_runtime::ParachainInfo, + }, + pallets_extra = { + PolkadotXcm: penpal_runtime::PolkadotXcm, + Assets: penpal_runtime::Assets, + } + }, // Kusama pub struct Statemine { @@ -268,6 +292,7 @@ decl_test_networks! { relay_chain = Westend, parachains = vec![ Westmint, + PenpalWestend, ], } } @@ -297,6 +322,9 @@ parameter_types! { // Penpal Kusama pub PenpalKusamaSender: AccountId = PenpalKusama::account_id_of(ALICE); pub PenpalKusamaReceiver: AccountId = PenpalKusama::account_id_of(BOB); + // Penpal Westend + pub PenpalWestendSender: AccountId = PenpalWestend::account_id_of(ALICE); + pub PenpalWestendReceiver: AccountId = PenpalWestend::account_id_of(BOB); // Collectives pub CollectivesSender: AccountId = Collectives::account_id_of(ALICE); pub CollectivesReceiver: AccountId = Collectives::account_id_of(BOB); diff --git a/xcm/xcm-emulator/src/lib.rs b/xcm/xcm-emulator/src/lib.rs index a31d7c5b7e5..724e617806a 100644 --- a/xcm/xcm-emulator/src/lib.rs +++ b/xcm/xcm-emulator/src/lib.rs @@ -195,6 +195,7 @@ pub trait Parachain: XcmpMessageHandler + DmpMessageHandler { macro_rules! decl_test_relay_chains { ( $( + #[api_version($api_version:tt)] pub struct $name:ident { genesis = $genesis:expr, on_init = $on_init:expr, @@ -280,7 +281,7 @@ macro_rules! decl_test_relay_chains { } } - $crate::__impl_test_ext_for_relay_chain!($name, $genesis, $on_init); + $crate::__impl_test_ext_for_relay_chain!($name, $genesis, $on_init, $api_version); )+ }; } @@ -288,13 +289,13 @@ macro_rules! decl_test_relay_chains { #[macro_export] macro_rules! __impl_test_ext_for_relay_chain { // entry point: generate ext name - ($name:ident, $genesis:expr, $on_init:expr) => { + ($name:ident, $genesis:expr, $on_init:expr, $api_version:tt) => { $crate::paste::paste! { - $crate::__impl_test_ext_for_relay_chain!(@impl $name, $genesis, $on_init, []); + $crate::__impl_test_ext_for_relay_chain!(@impl $name, $genesis, $on_init, $api_version, []); } }; // impl - (@impl $name:ident, $genesis:expr, $on_init:expr, $ext_name:ident) => { + (@impl $name:ident, $genesis:expr, $on_init:expr, 4, $ext_name:ident) => { thread_local! { pub static $ext_name: $crate::RefCell<$crate::sp_io::TestExternalities> = $crate::RefCell::new(<$name>::build_new_ext($genesis)); @@ -354,6 +355,75 @@ macro_rules! __impl_test_ext_for_relay_chain { r } + fn ext_wrapper(func: impl FnOnce() -> R) -> R { + $ext_name.with(|v| { + v.borrow_mut().execute_with(|| { + func() + }) + }) + } + } + }; + (@impl $name:ident, $genesis:expr, $on_init:expr, 5, $ext_name:ident) => { + thread_local! { + pub static $ext_name: $crate::RefCell<$crate::sp_io::TestExternalities> + = $crate::RefCell::new(<$name>::build_new_ext($genesis)); + } + + impl TestExt for $name { + fn build_new_ext(storage: $crate::Storage) -> $crate::sp_io::TestExternalities { + let mut ext = sp_io::TestExternalities::new(storage); + ext.execute_with(|| { + #[allow(clippy::no_effect)] + $on_init; + sp_tracing::try_init_simple(); + ::System::set_block_number(1); + }); + ext + } + + fn new_ext() -> $crate::sp_io::TestExternalities { + <$name>::build_new_ext($genesis) + } + + fn reset_ext() { + $ext_name.with(|v| *v.borrow_mut() = <$name>::build_new_ext($genesis)); + } + + fn execute_with(execute: impl FnOnce() -> R) -> R { + use $crate::{NetworkComponent}; + // Make sure the Network is initialized + <$name>::init(); + + let r = $ext_name.with(|v| v.borrow_mut().execute_with(execute)); + + // send messages if needed + $ext_name.with(|v| { + v.borrow_mut().execute_with(|| { + use $crate::polkadot_primitives::runtime_api::runtime_decl_for_parachain_host::ParachainHostV5; + + //TODO: mark sent count & filter out sent msg + for para_id in <$name>::para_ids() { + // downward messages + let downward_messages = ::Runtime::dmq_contents(para_id.into()) + .into_iter() + .map(|inbound| (inbound.sent_at, inbound.msg)); + if downward_messages.len() == 0 { + continue; + } + <$name>::send_downward_messages(para_id, downward_messages.into_iter()); + + // Note: no need to handle horizontal messages, as the + // simulator directly sends them to dest (not relayed). + } + }) + }); + + <$name>::process_messages(); + + r + } + fn ext_wrapper(func: impl FnOnce() -> R) -> R { $ext_name.with(|v| { v.borrow_mut().execute_with(|| { From 53714822986a47da6b5843c1ec5e922ebe5c4e8d Mon Sep 17 00:00:00 2001 From: gilescope Date: Sun, 28 May 2023 10:22:43 +0100 Subject: [PATCH 4/7] 3 tests all passing --- .../integration-tests/emulated/assets/westmint/src/lib.rs | 8 +++----- .../assets/westmint/src/tests/reserve_transfer.rs | 2 +- .../integration-tests/emulated/common/src/constants.rs | 6 ++++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/parachains/integration-tests/emulated/assets/westmint/src/lib.rs b/parachains/integration-tests/emulated/assets/westmint/src/lib.rs index dd2114a9d21..3532b794969 100644 --- a/parachains/integration-tests/emulated/assets/westmint/src/lib.rs +++ b/parachains/integration-tests/emulated/assets/westmint/src/lib.rs @@ -8,11 +8,9 @@ pub use integration_tests_common::{ polkadot::ED as POLKADOT_ED, PROOF_SIZE_THRESHOLD, REF_TIME_THRESHOLD, XCM_V3, }, - AccountId,Collectives, CollectivesPallet, - CollectivesReceiver, CollectivesSender, - - Westmint, - WestmintPallet, WestmintReceiver, WestmintSender,PenpalWestend, + AccountId, Collectives, CollectivesPallet, CollectivesReceiver, CollectivesSender, + PenpalWestend, Westend, WestendPallet, WestendReceiver, WestendSender, Westmint, + WestmintPallet, WestmintReceiver, WestmintSender, }; pub use polkadot_core_primitives::InboundDownwardMessage; pub use xcm::{ diff --git a/parachains/integration-tests/emulated/assets/westmint/src/tests/reserve_transfer.rs b/parachains/integration-tests/emulated/assets/westmint/src/tests/reserve_transfer.rs index 43fa60c75cd..e3f47e6f40d 100644 --- a/parachains/integration-tests/emulated/assets/westmint/src/tests/reserve_transfer.rs +++ b/parachains/integration-tests/emulated/assets/westmint/src/tests/reserve_transfer.rs @@ -33,7 +33,7 @@ fn reserve_transfer_native_asset_from_relay_to_assets() { Westend, vec![ RuntimeEvent::XcmPallet(pallet_xcm::Event::Attempted { outcome: Outcome::Complete(weight) }) => { - weight: weight_within_threshold((REF_TIME_THRESHOLD, PROOF_SIZE_THRESHOLD), Weight::from_parts(2_000_000_000, 0), *weight), + weight: weight_within_threshold((REF_TIME_THRESHOLD, PROOF_SIZE_THRESHOLD), Weight::from_parts(731_495_000, 0), *weight), }, ] ); diff --git a/parachains/integration-tests/emulated/common/src/constants.rs b/parachains/integration-tests/emulated/common/src/constants.rs index 293e515e083..01dc990b2ae 100644 --- a/parachains/integration-tests/emulated/common/src/constants.rs +++ b/parachains/integration-tests/emulated/common/src/constants.rs @@ -211,8 +211,10 @@ pub mod polkadot { pub mod westend { use super::*; + use westend_runtime_constants::currency::UNITS as WND; pub const ED: Balance = westend_runtime_constants::currency::EXISTENTIAL_DEPOSIT; - const STASH: u128 = 100 * westend_runtime_constants::currency::UNITS; + const ENDOWMENT: u128 = 1_000_000 * WND; + const STASH: u128 = 100 * WND; pub fn get_host_config() -> HostConfiguration { HostConfiguration { @@ -252,7 +254,7 @@ pub mod westend { balances: accounts::init_balances() .iter() .cloned() - .map(|k| (k, ED * 4096)) + .map(|k| (k, ENDOWMENT)) .collect(), }, session: westend_runtime::SessionConfig { From 7d29d26a1f61e2b0c7343d009298e79b9f865d90 Mon Sep 17 00:00:00 2001 From: gilescope Date: Sun, 28 May 2023 11:12:02 +0100 Subject: [PATCH 5/7] Remove duplication --- xcm/xcm-emulator/src/lib.rs | 75 ++----------------------------------- 1 file changed, 3 insertions(+), 72 deletions(-) diff --git a/xcm/xcm-emulator/src/lib.rs b/xcm/xcm-emulator/src/lib.rs index 724e617806a..03838d29092 100644 --- a/xcm/xcm-emulator/src/lib.rs +++ b/xcm/xcm-emulator/src/lib.rs @@ -291,11 +291,11 @@ macro_rules! __impl_test_ext_for_relay_chain { // entry point: generate ext name ($name:ident, $genesis:expr, $on_init:expr, $api_version:tt) => { $crate::paste::paste! { - $crate::__impl_test_ext_for_relay_chain!(@impl $name, $genesis, $on_init, $api_version, []); + $crate::__impl_test_ext_for_relay_chain!(@impl $name, $genesis, $on_init, [], []); } }; // impl - (@impl $name:ident, $genesis:expr, $on_init:expr, 4, $ext_name:ident) => { + (@impl $name:ident, $genesis:expr, $on_init:expr, $api_version:ident, $ext_name:ident) => { thread_local! { pub static $ext_name: $crate::RefCell<$crate::sp_io::TestExternalities> = $crate::RefCell::new(<$name>::build_new_ext($genesis)); @@ -331,76 +331,7 @@ macro_rules! __impl_test_ext_for_relay_chain { // send messages if needed $ext_name.with(|v| { v.borrow_mut().execute_with(|| { - use $crate::polkadot_primitives::runtime_api::runtime_decl_for_parachain_host::ParachainHostV4; - - //TODO: mark sent count & filter out sent msg - for para_id in <$name>::para_ids() { - // downward messages - let downward_messages = ::Runtime::dmq_contents(para_id.into()) - .into_iter() - .map(|inbound| (inbound.sent_at, inbound.msg)); - if downward_messages.len() == 0 { - continue; - } - <$name>::send_downward_messages(para_id, downward_messages.into_iter()); - - // Note: no need to handle horizontal messages, as the - // simulator directly sends them to dest (not relayed). - } - }) - }); - - <$name>::process_messages(); - - r - } - - fn ext_wrapper(func: impl FnOnce() -> R) -> R { - $ext_name.with(|v| { - v.borrow_mut().execute_with(|| { - func() - }) - }) - } - } - }; - (@impl $name:ident, $genesis:expr, $on_init:expr, 5, $ext_name:ident) => { - thread_local! { - pub static $ext_name: $crate::RefCell<$crate::sp_io::TestExternalities> - = $crate::RefCell::new(<$name>::build_new_ext($genesis)); - } - - impl TestExt for $name { - fn build_new_ext(storage: $crate::Storage) -> $crate::sp_io::TestExternalities { - let mut ext = sp_io::TestExternalities::new(storage); - ext.execute_with(|| { - #[allow(clippy::no_effect)] - $on_init; - sp_tracing::try_init_simple(); - ::System::set_block_number(1); - }); - ext - } - - fn new_ext() -> $crate::sp_io::TestExternalities { - <$name>::build_new_ext($genesis) - } - - fn reset_ext() { - $ext_name.with(|v| *v.borrow_mut() = <$name>::build_new_ext($genesis)); - } - - fn execute_with(execute: impl FnOnce() -> R) -> R { - use $crate::{NetworkComponent}; - // Make sure the Network is initialized - <$name>::init(); - - let r = $ext_name.with(|v| v.borrow_mut().execute_with(execute)); - - // send messages if needed - $ext_name.with(|v| { - v.borrow_mut().execute_with(|| { - use $crate::polkadot_primitives::runtime_api::runtime_decl_for_parachain_host::ParachainHostV5; + use $crate::polkadot_primitives::runtime_api::runtime_decl_for_parachain_host::$api_version; //TODO: mark sent count & filter out sent msg for para_id in <$name>::para_ids() { From ea2889d5e7ed78837331621d3ae9c4751fa81aa7 Mon Sep 17 00:00:00 2001 From: gilescope Date: Sun, 28 May 2023 14:37:25 +0100 Subject: [PATCH 6/7] fix runtime-benchmarks --- parachains/integration-tests/emulated/common/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/parachains/integration-tests/emulated/common/Cargo.toml b/parachains/integration-tests/emulated/common/Cargo.toml index 75a6d128b6d..e0661dcbc23 100644 --- a/parachains/integration-tests/emulated/common/Cargo.toml +++ b/parachains/integration-tests/emulated/common/Cargo.toml @@ -54,4 +54,6 @@ xcm-emulator = { default-features = false, path = "../../../../xcm/xcm-emulator" [features] runtime-benchmarks = [ "kusama-runtime/runtime-benchmarks", + "polkadot-runtime/runtime-benchmarks", + "westend-runtime/runtime-benchmarks", ] From 89447b392a5b95415ad5ba1117be1bd8c537169a Mon Sep 17 00:00:00 2001 From: gilescope Date: Sun, 28 May 2023 22:25:08 +0100 Subject: [PATCH 7/7] Fix typo --- parachains/integration-tests/emulated/common/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/parachains/integration-tests/emulated/common/src/lib.rs b/parachains/integration-tests/emulated/common/src/lib.rs index db1198e33fa..ac82d69042d 100644 --- a/parachains/integration-tests/emulated/common/src/lib.rs +++ b/parachains/integration-tests/emulated/common/src/lib.rs @@ -97,6 +97,7 @@ decl_test_parachains! { pallets_extra = { PolkadotXcm: westmint_runtime::PolkadotXcm, Assets: westmint_runtime::Assets, + ForeignAssets: westmint_runtime::ForeignAssets, } }, // Polkadot @@ -184,7 +185,7 @@ decl_test_parachains! { pallets_extra = { PolkadotXcm: statemine_runtime::PolkadotXcm, Assets: statemine_runtime::Assets, - ForeignAssets: statemine_runtime::Assets, + ForeignAssets: statemine_runtime::ForeignAssets, } }, pub struct PenpalKusama {