From 40310a38cdb1e56ae72d8798e7e8ad28c1494bdb Mon Sep 17 00:00:00 2001 From: ron Date: Fri, 12 Jan 2024 21:10:28 +0800 Subject: [PATCH 1/3] runtime test for create asset in AH --- .../asset-hub-rococo/tests/snowbridge.rs | 41 +++++++++++++++++++ .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 10 +---- 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs new file mode 100644 index 000000000000..dc051a15744c --- /dev/null +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork + +pub use asset_hub_rococo_runtime::{Runtime, RuntimeCall}; +use codec::Encode; +use frame_support::instances::Instance2; +use sp_runtime::MultiAddress; +use xcm::latest::prelude::*; + +#[test] +fn test_foreign_create_asset_call_compatibility() { + assert_eq!( + RuntimeCall::ForeignAssets(pallet_assets::Call::create { + id: MultiLocation::default(), + admin: MultiAddress::Id([0; 32].into()), + min_balance: 1, + }) + .encode(), + snowbridge_router_primitives::inbound::Call::ForeignAssets( + snowbridge_router_primitives::inbound::ForeignAssetsCall::create { + id: MultiLocation::default(), + admin: MultiAddress::Id([0; 32].into()), + min_balance: 1, + } + ) + .encode() + ); +} + +#[test] +fn check_foreign_create_asset_call_with_sane_weight() { + use pallet_assets::WeightInfo; + let actual = >::WeightInfo::create(); + let max_weight = snowbridge_router_primitives::inbound::FOREIGN_CREATE_ASSET_WEIGHT_AT_MOST; + assert!( + actual.all_lte(max_weight), + "max_weight: {:?} should be adjusted to actual {:?}", + max_weight, + actual + ); +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index a64d8b26ec61..6527826627cc 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -500,7 +500,6 @@ parameter_types! { } parameter_types! { - pub const CreateAssetCall: [u8;2] = [53, 0]; pub const CreateAssetDeposit: u128 = (UNITS / 10) + EXISTENTIAL_DEPOSIT; pub const InboundQueuePalletInstance: u8 = parachains_common::rococo::snowbridge::INBOUND_QUEUE_PALLET_INDEX; pub Parameters: PricingParameters = PricingParameters { @@ -560,13 +559,8 @@ impl snowbridge_pallet_inbound_queue::Config for Runtime { type GatewayAddress = EthereumGatewayAddress; #[cfg(feature = "runtime-benchmarks")] type Helper = Runtime; - type MessageConverter = MessageToXcm< - CreateAssetCall, - CreateAssetDeposit, - InboundQueuePalletInstance, - AccountId, - Balance, - >; + type MessageConverter = + MessageToXcm; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; type MaxMessageSize = ConstU32<2048>; From d8787fb36c592b88eed002b67be7986f7cef2029 Mon Sep 17 00:00:00 2001 From: ron Date: Wed, 17 Jan 2024 18:20:55 +0800 Subject: [PATCH 2/3] Revert the change moving call index to primitives --- .../asset-hub-rococo/tests/snowbridge.rs | 32 +++++++++---------- .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 10 ++++-- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs index dc051a15744c..a93746b36032 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs @@ -9,22 +9,22 @@ use xcm::latest::prelude::*; #[test] fn test_foreign_create_asset_call_compatibility() { - assert_eq!( - RuntimeCall::ForeignAssets(pallet_assets::Call::create { - id: MultiLocation::default(), - admin: MultiAddress::Id([0; 32].into()), - min_balance: 1, - }) - .encode(), - snowbridge_router_primitives::inbound::Call::ForeignAssets( - snowbridge_router_primitives::inbound::ForeignAssetsCall::create { - id: MultiLocation::default(), - admin: MultiAddress::Id([0; 32].into()), - min_balance: 1, - } - ) - .encode() - ); + // assert_eq!( + // RuntimeCall::ForeignAssets(pallet_assets::Call::create { + // id: MultiLocation::default(), + // admin: MultiAddress::Id([0; 32].into()), + // min_balance: 1, + // }) + // .encode(), + // snowbridge_router_primitives::inbound::Call::ForeignAssets( + // snowbridge_router_primitives::inbound::ForeignAssetsCall::create { + // id: MultiLocation::default(), + // admin: MultiAddress::Id([0; 32].into()), + // min_balance: 1, + // } + // ) + // .encode() + // ); } #[test] diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index 6527826627cc..a64d8b26ec61 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -500,6 +500,7 @@ parameter_types! { } parameter_types! { + pub const CreateAssetCall: [u8;2] = [53, 0]; pub const CreateAssetDeposit: u128 = (UNITS / 10) + EXISTENTIAL_DEPOSIT; pub const InboundQueuePalletInstance: u8 = parachains_common::rococo::snowbridge::INBOUND_QUEUE_PALLET_INDEX; pub Parameters: PricingParameters = PricingParameters { @@ -559,8 +560,13 @@ impl snowbridge_pallet_inbound_queue::Config for Runtime { type GatewayAddress = EthereumGatewayAddress; #[cfg(feature = "runtime-benchmarks")] type Helper = Runtime; - type MessageConverter = - MessageToXcm; + type MessageConverter = MessageToXcm< + CreateAssetCall, + CreateAssetDeposit, + InboundQueuePalletInstance, + AccountId, + Balance, + >; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; type MaxMessageSize = ConstU32<2048>; From 7a58ab4d79439f61ae581b1a3da142c3ef68cdb1 Mon Sep 17 00:00:00 2001 From: ron Date: Wed, 17 Jan 2024 19:57:35 +0800 Subject: [PATCH 3/3] Refactor CreateAssetCall as structure & Move to common --- Cargo.lock | 1 + cumulus/parachains/common/Cargo.toml | 5 +++ cumulus/parachains/common/src/rococo.rs | 10 +++++- .../asset-hub-rococo/tests/snowbridge.rs | 33 +++++++++---------- .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 5 +-- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b7e0b3585ff7..c9440fd9c3a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11346,6 +11346,7 @@ dependencies = [ "rococo-runtime-constants", "scale-info", "smallvec", + "snowbridge-router-primitives", "sp-consensus-aura", "sp-core", "sp-io", diff --git a/cumulus/parachains/common/Cargo.toml b/cumulus/parachains/common/Cargo.toml index dcaea40d2da0..ef108870cb67 100644 --- a/cumulus/parachains/common/Cargo.toml +++ b/cumulus/parachains/common/Cargo.toml @@ -47,6 +47,9 @@ cumulus-primitives-core = { path = "../../primitives/core", default-features = f cumulus-primitives-utility = { path = "../../primitives/utility", default-features = false } parachain-info = { package = "staging-parachain-info", path = "../pallets/parachain-info", default-features = false } +# Snowbridge +snowbridge-router-primitives = { path = "../../../../parachain/primitives/router", default-features = false } + [dev-dependencies] pallet-authorship = { path = "../../../substrate/frame/authorship", default-features = false } sp-io = { path = "../../../substrate/primitives/io", default-features = false } @@ -75,6 +78,7 @@ std = [ "polkadot-primitives/std", "rococo-runtime-constants/std", "scale-info/std", + "snowbridge-router-primitives/std", "sp-consensus-aura/std", "sp-core/std", "sp-io/std", @@ -96,6 +100,7 @@ runtime-benchmarks = [ "pallet-collator-selection/runtime-benchmarks", "pallet-message-queue/runtime-benchmarks", "polkadot-primitives/runtime-benchmarks", + "snowbridge-router-primitives/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", ] diff --git a/cumulus/parachains/common/src/rococo.rs b/cumulus/parachains/common/src/rococo.rs index 72e16927f38c..5cdb1bb35600 100644 --- a/cumulus/parachains/common/src/rococo.rs +++ b/cumulus/parachains/common/src/rococo.rs @@ -119,7 +119,9 @@ pub mod consensus { } pub mod snowbridge { - use frame_support::parameter_types; + use crate::rococo::currency::{EXISTENTIAL_DEPOSIT, UNITS}; + use frame_support::{parameter_types, weights::Weight}; + use snowbridge_router_primitives::inbound::CreateAssetCallInfo; use xcm::opaque::lts::NetworkId; /// The pallet index of the Ethereum inbound queue pallet in the bridge hub runtime. @@ -128,5 +130,11 @@ pub mod snowbridge { parameter_types! { /// Network and location for the Ethereum chain. pub EthereumNetwork: NetworkId = NetworkId::Ethereum { chain_id: 11155111 }; + pub const CreateAssetCall: CreateAssetCallInfo = CreateAssetCallInfo { + call_index: [53,0], + asset_deposit: (UNITS / 10) + EXISTENTIAL_DEPOSIT, + min_balance: 1, + transact_weight_at_most: Weight::from_parts(400_000_000, 8_000) + }; } } diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs index a93746b36032..4260ec660f95 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/snowbridge.rs @@ -1,37 +1,34 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023 Snowfork -pub use asset_hub_rococo_runtime::{Runtime, RuntimeCall}; +use asset_hub_rococo_runtime::Runtime; +pub use asset_hub_rococo_runtime::RuntimeCall; use codec::Encode; use frame_support::instances::Instance2; +use parachains_common::rococo::snowbridge::CreateAssetCall; +use snowbridge_router_primitives::inbound::CreateAssetCallInfo; use sp_runtime::MultiAddress; use xcm::latest::prelude::*; #[test] fn test_foreign_create_asset_call_compatibility() { - // assert_eq!( - // RuntimeCall::ForeignAssets(pallet_assets::Call::create { - // id: MultiLocation::default(), - // admin: MultiAddress::Id([0; 32].into()), - // min_balance: 1, - // }) - // .encode(), - // snowbridge_router_primitives::inbound::Call::ForeignAssets( - // snowbridge_router_primitives::inbound::ForeignAssetsCall::create { - // id: MultiLocation::default(), - // admin: MultiAddress::Id([0; 32].into()), - // min_balance: 1, - // } - // ) - // .encode() - // ); + let call = &RuntimeCall::ForeignAssets(pallet_assets::Call::create { + id: MultiLocation::default(), + admin: MultiAddress::Id([0; 32].into()), + min_balance: 1, + }) + .encode(); + let call_index = &call[..2]; + let snowbridge_call: CreateAssetCallInfo = CreateAssetCall::get(); + assert_eq!(call_index, snowbridge_call.call_index); } #[test] fn check_foreign_create_asset_call_with_sane_weight() { use pallet_assets::WeightInfo; let actual = >::WeightInfo::create(); - let max_weight = snowbridge_router_primitives::inbound::FOREIGN_CREATE_ASSET_WEIGHT_AT_MOST; + let snowbridge_call: CreateAssetCallInfo = CreateAssetCall::get(); + let max_weight = snowbridge_call.transact_weight_at_most; assert!( actual.all_lte(max_weight), "max_weight: {:?} should be adjusted to actual {:?}", diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index a64d8b26ec61..2204377e240e 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -500,8 +500,6 @@ parameter_types! { } parameter_types! { - pub const CreateAssetCall: [u8;2] = [53, 0]; - pub const CreateAssetDeposit: u128 = (UNITS / 10) + EXISTENTIAL_DEPOSIT; pub const InboundQueuePalletInstance: u8 = parachains_common::rococo::snowbridge::INBOUND_QUEUE_PALLET_INDEX; pub Parameters: PricingParameters = PricingParameters { exchange_rate: FixedU128::from_rational(1, 400), @@ -561,8 +559,7 @@ impl snowbridge_pallet_inbound_queue::Config for Runtime { #[cfg(feature = "runtime-benchmarks")] type Helper = Runtime; type MessageConverter = MessageToXcm< - CreateAssetCall, - CreateAssetDeposit, + parachains_common::rococo::snowbridge::CreateAssetCall, InboundQueuePalletInstance, AccountId, Balance,