From 028286bad169075a89dfd268cfd4c53decae3303 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 14 Sep 2023 01:25:39 -0300 Subject: [PATCH 01/56] Add relay in possible destinations for fee factor --- .../assets/asset-hub-kusama/src/xcm_config.rs | 5 ++++- cumulus/primitives/utility/src/lib.rs | 11 ++++++++++- polkadot/runtime/common/src/xcm_sender.rs | 14 +++++++------- polkadot/runtime/parachains/src/dmp.rs | 9 ++++++--- polkadot/runtime/parachains/src/lib.rs | 7 ++++++- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs index 480407effd0f..23003d9253b1 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs @@ -539,11 +539,14 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +pub type PriceForParentDelivery = + ExponentialPrice; // TODO: Track the fee factor in `ParachainSystem` + /// The means for routing XCM messages which are not for local execution into the right message /// queues. pub type XcmRouter = WithUniqueTopic<( // Two routers - use UMP to communicate with the relay chain: - cumulus_primitives_utility::ParentAsUmp, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, )>; diff --git a/cumulus/primitives/utility/src/lib.rs b/cumulus/primitives/utility/src/lib.rs index 024598a99198..9d6c388d5eff 100644 --- a/cumulus/primitives/utility/src/lib.rs +++ b/cumulus/primitives/utility/src/lib.rs @@ -28,7 +28,7 @@ use frame_support::{ }, weights::Weight, }; -use polkadot_runtime_common::xcm_sender::ConstantPrice; +use polkadot_runtime_common::xcm_sender::{ConstantPrice, ExponentialPrice}; use sp_runtime::{traits::Saturating, SaturatedConversion}; use sp_std::{marker::PhantomData, prelude::*}; use xcm::{latest::prelude::*, WrapVersion}; @@ -51,6 +51,15 @@ impl> PriceForParentDelivery for ConstantPrice { } } +impl, B: Get, M: Get, F: FeeTracker> PriceForParentDelivery for ExponentialPrice { + fn price_for_parent_delivery(message: &Xcm<()>) -> MultiAssets { + let message_fee = (message.encoded_size() as u128).saturating_mul(M::get()); + let fee_sum = B::get().saturating_add(message_fee); + let amount = F::get_fee_factor(TransportDestination::Relay).saturating_mul_int(fee_sum); + (A::get(), amount).into() + } +} + /// Xcm router which recognises the `Parent` destination and handles it by sending the message into /// the given UMP `UpwardMessageSender` implementation. Thus this essentially adapts an /// `UpwardMessageSender` trait impl into a `SendXcm` trait impl. diff --git a/polkadot/runtime/common/src/xcm_sender.rs b/polkadot/runtime/common/src/xcm_sender.rs index c67dc3b3ab23..c611da0e1dc3 100644 --- a/polkadot/runtime/common/src/xcm_sender.rs +++ b/polkadot/runtime/common/src/xcm_sender.rs @@ -22,7 +22,7 @@ use parity_scale_codec::Encode; use primitives::Id as ParaId; use runtime_parachains::{ configuration::{self, HostConfiguration}, - dmp, FeeTracker, + dmp, FeeTracker, TransportDestination, }; use sp_runtime::FixedPointNumber; use sp_std::{marker::PhantomData, prelude::*}; @@ -70,7 +70,7 @@ impl, B: Get, M: Get, F: FeeTracker> PriceForParacha fn price_for_parachain_delivery(para: ParaId, msg: &Xcm<()>) -> MultiAssets { let msg_fee = (msg.encoded_size() as u128).saturating_mul(M::get()); let fee_sum = B::get().saturating_add(msg_fee); - let amount = F::get_fee_factor(para).saturating_mul_int(fee_sum); + let amount = F::get_fee_factor(TransportDestination::Para(para)).saturating_mul_int(fee_sum); (A::get(), amount).into() } } @@ -211,7 +211,7 @@ impl EnsureForParachain for () { mod tests { use super::*; use frame_support::parameter_types; - use runtime_parachains::FeeTracker; + use runtime_parachains::{FeeTracker, TransportDestination}; use sp_runtime::FixedU128; parameter_types! { @@ -222,7 +222,7 @@ mod tests { struct TestFeeTracker; impl FeeTracker for TestFeeTracker { - fn get_fee_factor(_: ParaId) -> FixedU128 { + fn get_fee_factor(_: TransportDestination) -> FixedU128 { FixedU128::from_rational(101, 100) } } @@ -238,21 +238,21 @@ mod tests { // F * (B + msg_length * M) // message_length = 1 - let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + m); + let result: u128 = TestFeeTracker::get_fee_factor(TransportDestination::Para(id)).saturating_mul_int(b + m); assert_eq!( TestExponentialPrice::price_for_parachain_delivery(id, &Xcm(vec![])), (FeeAssetId::get(), result).into() ); // message size = 2 - let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (2 * m)); + let result: u128 = TestFeeTracker::get_fee_factor(TransportDestination::Para(id)).saturating_mul_int(b + (2 * m)); assert_eq!( TestExponentialPrice::price_for_parachain_delivery(id, &Xcm(vec![ClearOrigin])), (FeeAssetId::get(), result).into() ); // message size = 4 - let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (4 * m)); + let result: u128 = TestFeeTracker::get_fee_factor(TransportDestination::Para(id)).saturating_mul_int(b + (4 * m)); assert_eq!( TestExponentialPrice::price_for_parachain_delivery( id, diff --git a/polkadot/runtime/parachains/src/dmp.rs b/polkadot/runtime/parachains/src/dmp.rs index 69836a96d617..b54eca90fcc9 100644 --- a/polkadot/runtime/parachains/src/dmp.rs +++ b/polkadot/runtime/parachains/src/dmp.rs @@ -44,7 +44,7 @@ use crate::{ configuration::{self, HostConfiguration}, - initializer, FeeTracker, + initializer, FeeTracker, TransportDestination, }; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::BlockNumberFor; @@ -362,7 +362,10 @@ impl Pallet { } impl FeeTracker for Pallet { - fn get_fee_factor(para: ParaId) -> FixedU128 { - DeliveryFeeFactor::::get(para) + fn get_fee_factor(destination: TransportDestination) -> FixedU128 { + match destination { + TransportDestination::Relay => u128::MAX, // DMP doesn't transport to Relay + TransportDestination::Para(para) => DeliveryFeeFactor::::get(para), + } } } diff --git a/polkadot/runtime/parachains/src/lib.rs b/polkadot/runtime/parachains/src/lib.rs index 718e6535b5cc..3f4f3eefeb7e 100644 --- a/polkadot/runtime/parachains/src/lib.rs +++ b/polkadot/runtime/parachains/src/lib.rs @@ -57,10 +57,15 @@ pub use paras::ParaLifecycle; use primitives::{HeadData, Id as ParaId, ValidationCode}; use sp_runtime::{DispatchResult, FixedU128}; +pub enum TransportDestination { + Relay, + Para(ParaId), +} + /// Trait for tracking message delivery fees on a transport protocol. pub trait FeeTracker { /// The evolving exponential fee factor which will be used to calculate the delivery fees. - fn get_fee_factor(para: ParaId) -> FixedU128; + fn get_fee_factor(destination: TransportDestination) -> FixedU128; } /// Schedule a para to be initialized at the start of the next session with the given genesis data. From 9384e682c14f5a95ba7ea91a1b1d4b4e586fc218 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 14 Sep 2023 16:11:08 -0300 Subject: [PATCH 02/56] Add associated type to FeeTracker --- cumulus/pallets/xcmp-queue/src/lib.rs | 6 ++++-- polkadot/runtime/common/src/xcm_sender.rs | 13 +++++++------ polkadot/runtime/parachains/src/dmp.rs | 11 +++++------ polkadot/runtime/parachains/src/lib.rs | 8 ++------ 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/cumulus/pallets/xcmp-queue/src/lib.rs b/cumulus/pallets/xcmp-queue/src/lib.rs index 7eb565574607..3affc5407112 100644 --- a/cumulus/pallets/xcmp-queue/src/lib.rs +++ b/cumulus/pallets/xcmp-queue/src/lib.rs @@ -1231,7 +1231,9 @@ impl SendXcm for Pallet { } impl FeeTracker for Pallet { - fn get_fee_factor(para: ParaId) -> FixedU128 { - >::get(para) + type Id = ParaId; + + fn get_fee_factor(id: Self::Id) -> FixedU128 { + >::get(id) } } diff --git a/polkadot/runtime/common/src/xcm_sender.rs b/polkadot/runtime/common/src/xcm_sender.rs index c611da0e1dc3..f382589de23f 100644 --- a/polkadot/runtime/common/src/xcm_sender.rs +++ b/polkadot/runtime/common/src/xcm_sender.rs @@ -22,7 +22,7 @@ use parity_scale_codec::Encode; use primitives::Id as ParaId; use runtime_parachains::{ configuration::{self, HostConfiguration}, - dmp, FeeTracker, TransportDestination, + dmp, FeeTracker, }; use sp_runtime::FixedPointNumber; use sp_std::{marker::PhantomData, prelude::*}; @@ -211,7 +211,7 @@ impl EnsureForParachain for () { mod tests { use super::*; use frame_support::parameter_types; - use runtime_parachains::{FeeTracker, TransportDestination}; + use runtime_parachains::FeeTracker; use sp_runtime::FixedU128; parameter_types! { @@ -222,7 +222,8 @@ mod tests { struct TestFeeTracker; impl FeeTracker for TestFeeTracker { - fn get_fee_factor(_: TransportDestination) -> FixedU128 { + type Id = (); + fn get_fee_factor(_: Self::Id) -> FixedU128 { FixedU128::from_rational(101, 100) } } @@ -238,21 +239,21 @@ mod tests { // F * (B + msg_length * M) // message_length = 1 - let result: u128 = TestFeeTracker::get_fee_factor(TransportDestination::Para(id)).saturating_mul_int(b + m); + let result: u128 = TestFeeTracker::get_fee_factor(()).saturating_mul_int(b + m); assert_eq!( TestExponentialPrice::price_for_parachain_delivery(id, &Xcm(vec![])), (FeeAssetId::get(), result).into() ); // message size = 2 - let result: u128 = TestFeeTracker::get_fee_factor(TransportDestination::Para(id)).saturating_mul_int(b + (2 * m)); + let result: u128 = TestFeeTracker::get_fee_factor(()).saturating_mul_int(b + (2 * m)); assert_eq!( TestExponentialPrice::price_for_parachain_delivery(id, &Xcm(vec![ClearOrigin])), (FeeAssetId::get(), result).into() ); // message size = 4 - let result: u128 = TestFeeTracker::get_fee_factor(TransportDestination::Para(id)).saturating_mul_int(b + (4 * m)); + let result: u128 = TestFeeTracker::get_fee_factor(()).saturating_mul_int(b + (4 * m)); assert_eq!( TestExponentialPrice::price_for_parachain_delivery( id, diff --git a/polkadot/runtime/parachains/src/dmp.rs b/polkadot/runtime/parachains/src/dmp.rs index b54eca90fcc9..b84a9003f729 100644 --- a/polkadot/runtime/parachains/src/dmp.rs +++ b/polkadot/runtime/parachains/src/dmp.rs @@ -44,7 +44,7 @@ use crate::{ configuration::{self, HostConfiguration}, - initializer, FeeTracker, TransportDestination, + initializer, FeeTracker, }; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::BlockNumberFor; @@ -362,10 +362,9 @@ impl Pallet { } impl FeeTracker for Pallet { - fn get_fee_factor(destination: TransportDestination) -> FixedU128 { - match destination { - TransportDestination::Relay => u128::MAX, // DMP doesn't transport to Relay - TransportDestination::Para(para) => DeliveryFeeFactor::::get(para), - } + type Id = ParaId; + + fn get_fee_factor(id: Self::Id) -> FixedU128 { + DeliveryFeeFactor::::get(id) } } diff --git a/polkadot/runtime/parachains/src/lib.rs b/polkadot/runtime/parachains/src/lib.rs index 3f4f3eefeb7e..3bdf0d4502c0 100644 --- a/polkadot/runtime/parachains/src/lib.rs +++ b/polkadot/runtime/parachains/src/lib.rs @@ -57,15 +57,11 @@ pub use paras::ParaLifecycle; use primitives::{HeadData, Id as ParaId, ValidationCode}; use sp_runtime::{DispatchResult, FixedU128}; -pub enum TransportDestination { - Relay, - Para(ParaId), -} - /// Trait for tracking message delivery fees on a transport protocol. pub trait FeeTracker { + type Id; // A destination id /// The evolving exponential fee factor which will be used to calculate the delivery fees. - fn get_fee_factor(destination: TransportDestination) -> FixedU128; + fn get_fee_factor(id: Self::Id) -> FixedU128; } /// Schedule a para to be initialized at the start of the next session with the given genesis data. From 69f1c522593a394e922d1b4e68efb30d81fefd78 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 14 Sep 2023 19:58:51 -0300 Subject: [PATCH 03/56] Add delivery fe calculation to ParachainSystem --- cumulus/pallets/parachain-system/src/lib.rs | 68 ++++++++++++++++++++- polkadot/runtime/common/src/xcm_sender.rs | 2 +- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index a8e9a0bf9ae4..145ad9b36c96 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -177,6 +177,12 @@ where check_version: bool, } +mod ump_constants { + const THRESHOLD_FACTOR: u32 = 2; // The price starts to increase when queue is half full + const EXPONENTIAL_FEE_BASE: FixedU128 = FixedU128::from_rational(105, 100); // 1.05 + const MESSAGE_SIZE_FEE_BASE: FixedU128 = FixedU128::from_rational(1, 1000); // 0.001 +} + #[frame_support::pallet] pub mod pallet { use super::*; @@ -720,7 +726,7 @@ pub mod pallet { StorageValue<_, Vec>, ValueQuery>; /// Storage field that keeps track of bandwidth used by the unincluded segment along with the - /// latest the latest HRMP watermark. Used for limiting the acceptance of new blocks with + /// latest HRMP watermark. Used for limiting the acceptance of new blocks with /// respect to relay chain constraints. #[pallet::storage] pub(super) type AggregatedUnincludedSegment = @@ -857,6 +863,17 @@ pub mod pallet { pub(super) type PendingUpwardMessages = StorageValue<_, Vec, ValueQuery>; + /// Initialization value for the delivery fee factor for UMP. + #[pallet::type_value] + pub fn UpwardInitialDeliveryFeeFactor() -> FixedU128 { + FixedU128::from_u32(1) + } + + /// The factor to multiply the base delivery fee by for UMP. + #[pallet::storage] + pub(super) type UpwardDeliveryFeeFactor = + StorageValue<_, FixedU128, ValueQuery, UpwardInitialDeliveryFeeFactor>; + /// The number of HRMP messages we observed in `on_initialize` and thus used that number for /// announcing the weight of `on_initialize` and `on_finalize`. #[pallet::storage] @@ -971,6 +988,36 @@ impl Pallet { let segment = UnincludedSegment::::get(); crate::unincluded_segment::size_after_included(included_hash, &segment) } + + /// Raise the delivery fee factor by a multiplicative factor and stores the resulting value. + /// + /// Returns the new delivery fee factor after the increment. + pub(crate) fn increment_ump_fee_factor(message_size_factor: FixedU128) -> FixedU128 { + >::mutate(|f| { + *f = f.saturating_mul(ump_constants::EXPONENTIAL_FEE_BASE + message_size_factor); + *f + }) + } + + /// Reduce the delivery fee factor by a multiplicative factor and stores the resulting value. + /// + /// Does not reduce the fee factor below the initial value, which is currently set as 1. + /// + /// Returns the new delivery fee factor after the decrement. + pub(crate) fn decrement_ump_fee_factor() -> FixedU128 { + >::mutate(|f| { + *f = InitialFactor::get().max(*f / ump_constants::EXPONENTIAL_FEE_BASE); + *f + }) + } +} + +impl FeeTracker for Pallet { + type Id = (); + + fn get_fee_factor(_: Self::Id) -> FixedU128 { + UpwardDeliveryFeeFactor::::get() + } } impl GetChannelInfo for Pallet { @@ -1466,6 +1513,7 @@ impl frame_system::SetCode for ParachainSetCode { impl Pallet { pub fn send_upward_message(message: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError> { + let message_len = message.len(); // Check if the message fits into the relay-chain constraints. // // Note, that we are using `host_configuration` here which may be from the previous @@ -1479,7 +1527,7 @@ impl Pallet { // // However, changing this setting is expected to be rare. if let Some(cfg) = Self::host_configuration() { - if message.len() > cfg.max_upward_message_size as usize { + if message_len > cfg.max_upward_message_size as usize { return Err(MessageSendError::TooBig) } } else { @@ -1493,6 +1541,22 @@ impl Pallet { // // Thus fall through here. }; + + // The threshold to increase fee is a factor of the max length in the queue + // TODO: Should this be `config.max_upward_queue_size` or `MAX_POSSIBLE_ALLOCATION`? + let threshold = config.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); + // We check the threshold against total size and not number of messages since messages could be big or small + let pending_messages = PendingUpwardMessages::::get(); + let total_size = pending_messages.iter().fold(0, |size_so_far, current_message| { + size_so_far + current_message.len() + }); + if total_size > threshold { + let message_size_factor = + FixedU128::from_u32(message_len.saturating_div(1024) as u32) + .saturating_mul(ump_constants::MESSAGE_SIZE_FEE_BASE); + Self::increment_ump_fee_factor(message_size_factor); + } + >::append(message.clone()); // The relay ump does not use using_encoded diff --git a/polkadot/runtime/common/src/xcm_sender.rs b/polkadot/runtime/common/src/xcm_sender.rs index f382589de23f..2de5b7470b3a 100644 --- a/polkadot/runtime/common/src/xcm_sender.rs +++ b/polkadot/runtime/common/src/xcm_sender.rs @@ -70,7 +70,7 @@ impl, B: Get, M: Get, F: FeeTracker> PriceForParacha fn price_for_parachain_delivery(para: ParaId, msg: &Xcm<()>) -> MultiAssets { let msg_fee = (msg.encoded_size() as u128).saturating_mul(M::get()); let fee_sum = B::get().saturating_add(msg_fee); - let amount = F::get_fee_factor(TransportDestination::Para(para)).saturating_mul_int(fee_sum); + let amount = F::get_fee_factor(para).saturating_mul_int(fee_sum); (A::get(), amount).into() } } From 4d7b6be7da22406568c41ac4764d28312690f63e Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 14 Sep 2023 20:30:34 -0300 Subject: [PATCH 04/56] Increase delivery fee when sending a message --- Cargo.lock | 2 + cumulus/pallets/parachain-system/Cargo.toml | 4 ++ cumulus/pallets/parachain-system/src/lib.rs | 50 ++++++++++----------- cumulus/pallets/xcmp-queue/src/lib.rs | 6 +-- cumulus/primitives/utility/Cargo.toml | 6 ++- cumulus/primitives/utility/src/lib.rs | 5 ++- polkadot/runtime/common/src/xcm_sender.rs | 5 +-- polkadot/runtime/parachains/src/dmp.rs | 6 +-- polkadot/runtime/parachains/src/lib.rs | 5 +-- 9 files changed, 46 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ba0c44592969..a4a55f07ea78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3550,6 +3550,7 @@ dependencies = [ "log", "parity-scale-codec", "polkadot-parachain-primitives", + "polkadot-runtime-parachains", "sc-client-api", "scale-info", "sp-core", @@ -3732,6 +3733,7 @@ dependencies = [ "log", "parity-scale-codec", "polkadot-runtime-common", + "polkadot-runtime-parachains", "sp-io", "sp-runtime", "sp-std", diff --git a/cumulus/pallets/parachain-system/Cargo.toml b/cumulus/pallets/parachain-system/Cargo.toml index 5470dce47480..ade712ea3b53 100644 --- a/cumulus/pallets/parachain-system/Cargo.toml +++ b/cumulus/pallets/parachain-system/Cargo.toml @@ -29,6 +29,7 @@ sp-version = { path = "../../../substrate/primitives/version", default-features # Polkadot polkadot-parachain-primitives = { path = "../../../polkadot/parachain", default-features = false, features = [ "wasm-api" ]} +polkadot-runtime-parachains = { path = "../../../polkadot/runtime/parachains", default-features = false } xcm = { package = "staging-xcm", path = "../../../polkadot/xcm", default-features = false} # Cumulus @@ -63,6 +64,7 @@ std = [ "frame-system/std", "log/std", "polkadot-parachain-primitives/std", + "polkadot-runtime-parachains/std", "scale-info/std", "sp-core/std", "sp-externalities/std", @@ -80,6 +82,7 @@ runtime-benchmarks = [ "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", "polkadot-parachain-primitives/runtime-benchmarks", + "polkadot-runtime-parachains/runtime-benchmarks", "sp-runtime/runtime-benchmarks", ] @@ -87,6 +90,7 @@ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", "sp-runtime/try-runtime", + "polkadot-runtime-parachains/runtime-benchmarks", ] parameterized-consensus-hook = [] diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 145ad9b36c96..a11a44c5c15b 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -45,6 +45,7 @@ use frame_support::{ }; use frame_system::{ensure_none, ensure_root, pallet_prelude::HeaderFor}; use polkadot_parachain_primitives::primitives::RelayChainBlockNumber; +use polkadot_runtime_parachains::FeeTracker; use scale_info::TypeInfo; use sp_runtime::{ traits::{Block as BlockT, BlockNumberProvider, Hash}, @@ -52,7 +53,7 @@ use sp_runtime::{ InvalidTransaction, TransactionLongevity, TransactionSource, TransactionValidity, ValidTransaction, }, - DispatchError, RuntimeDebug, + DispatchError, RuntimeDebug, FixedU128, Saturating, }; use sp_std::{cmp, collections::btree_map::BTreeMap, prelude::*}; use xcm::latest::XcmHash; @@ -178,9 +179,11 @@ where } mod ump_constants { - const THRESHOLD_FACTOR: u32 = 2; // The price starts to increase when queue is half full - const EXPONENTIAL_FEE_BASE: FixedU128 = FixedU128::from_rational(105, 100); // 1.05 - const MESSAGE_SIZE_FEE_BASE: FixedU128 = FixedU128::from_rational(1, 1000); // 0.001 + use super::FixedU128; + + pub const THRESHOLD_FACTOR: u32 = 2; // The price starts to increase when queue is half full + pub const EXPONENTIAL_FEE_BASE: FixedU128 = FixedU128::from_rational(105, 100); // 1.05 + pub const MESSAGE_SIZE_FEE_BASE: FixedU128 = FixedU128::from_rational(1, 1000); // 0.001 } #[frame_support::pallet] @@ -993,7 +996,7 @@ impl Pallet { /// /// Returns the new delivery fee factor after the increment. pub(crate) fn increment_ump_fee_factor(message_size_factor: FixedU128) -> FixedU128 { - >::mutate(|f| { + >::mutate(|f| { *f = f.saturating_mul(ump_constants::EXPONENTIAL_FEE_BASE + message_size_factor); *f }) @@ -1005,17 +1008,15 @@ impl Pallet { /// /// Returns the new delivery fee factor after the decrement. pub(crate) fn decrement_ump_fee_factor() -> FixedU128 { - >::mutate(|f| { - *f = InitialFactor::get().max(*f / ump_constants::EXPONENTIAL_FEE_BASE); + >::mutate(|f| { + *f = UpwardInitialDeliveryFeeFactor::get().max(*f / ump_constants::EXPONENTIAL_FEE_BASE); *f }) } } impl FeeTracker for Pallet { - type Id = (); - - fn get_fee_factor(_: Self::Id) -> FixedU128 { + fn get_fee_factor(_: ()) -> FixedU128 { UpwardDeliveryFeeFactor::::get() } } @@ -1530,6 +1531,20 @@ impl Pallet { if message_len > cfg.max_upward_message_size as usize { return Err(MessageSendError::TooBig) } + // The threshold to increase fee is a factor of the max length in the queue + // TODO: Should this be `config.max_upward_queue_size` or `MAX_POSSIBLE_ALLOCATION`? + let threshold = cfg.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); + // We check the threshold against total size and not number of messages since messages could be big or small + let pending_messages = PendingUpwardMessages::::get(); + let total_size = pending_messages.iter().fold(0, |size_so_far, current_message| { + size_so_far + current_message.len() + }); + if total_size > threshold as usize { + let message_size_factor = + FixedU128::from_u32(message_len.saturating_div(1024) as u32) + .saturating_mul(ump_constants::MESSAGE_SIZE_FEE_BASE); + Self::increment_ump_fee_factor(message_size_factor); + } } else { // This storage field should carry over from the previous block. So if it's None // then it must be that this is an edge-case where a message is attempted to be @@ -1542,21 +1557,6 @@ impl Pallet { // Thus fall through here. }; - // The threshold to increase fee is a factor of the max length in the queue - // TODO: Should this be `config.max_upward_queue_size` or `MAX_POSSIBLE_ALLOCATION`? - let threshold = config.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); - // We check the threshold against total size and not number of messages since messages could be big or small - let pending_messages = PendingUpwardMessages::::get(); - let total_size = pending_messages.iter().fold(0, |size_so_far, current_message| { - size_so_far + current_message.len() - }); - if total_size > threshold { - let message_size_factor = - FixedU128::from_u32(message_len.saturating_div(1024) as u32) - .saturating_mul(ump_constants::MESSAGE_SIZE_FEE_BASE); - Self::increment_ump_fee_factor(message_size_factor); - } - >::append(message.clone()); // The relay ump does not use using_encoded diff --git a/cumulus/pallets/xcmp-queue/src/lib.rs b/cumulus/pallets/xcmp-queue/src/lib.rs index 3affc5407112..f9d109e30651 100644 --- a/cumulus/pallets/xcmp-queue/src/lib.rs +++ b/cumulus/pallets/xcmp-queue/src/lib.rs @@ -1230,10 +1230,8 @@ impl SendXcm for Pallet { } } -impl FeeTracker for Pallet { - type Id = ParaId; - - fn get_fee_factor(id: Self::Id) -> FixedU128 { +impl FeeTracker for Pallet { + fn get_fee_factor(id: ParaId) -> FixedU128 { >::get(id) } } diff --git a/cumulus/primitives/utility/Cargo.toml b/cumulus/primitives/utility/Cargo.toml index 47c5442574fb..19cb703796d7 100644 --- a/cumulus/primitives/utility/Cargo.toml +++ b/cumulus/primitives/utility/Cargo.toml @@ -15,8 +15,9 @@ sp-runtime = { path = "../../../substrate/primitives/runtime", default-features sp-std = { path = "../../../substrate/primitives/std", default-features = false} # Polkadot -polkadot-runtime-common = { path = "../../../polkadot/runtime/common", default-features = false} -xcm = { package = "staging-xcm", path = "../../../polkadot/xcm", default-features = false} +polkadot-runtime-common = { path = "../../../polkadot/runtime/common", default-features = false } +polkadot-runtime-parachains = { path = "../../../polkadot/runtime/parachains", default-features = false } +xcm = { package = "staging-xcm", path = "../../../polkadot/xcm", default-features = false } xcm-executor = { package = "staging-xcm-executor", path = "../../../polkadot/xcm/xcm-executor", default-features = false} xcm-builder = { package = "staging-xcm-builder", path = "../../../polkadot/xcm/xcm-builder", default-features = false} @@ -31,6 +32,7 @@ std = [ "cumulus-primitives-core/std", "frame-support/std", "polkadot-runtime-common/std", + "polkadot-runtime-parachains/std", "sp-io/std", "sp-runtime/std", "sp-std/std", diff --git a/cumulus/primitives/utility/src/lib.rs b/cumulus/primitives/utility/src/lib.rs index 9d6c388d5eff..d206b3ab820e 100644 --- a/cumulus/primitives/utility/src/lib.rs +++ b/cumulus/primitives/utility/src/lib.rs @@ -29,7 +29,8 @@ use frame_support::{ weights::Weight, }; use polkadot_runtime_common::xcm_sender::{ConstantPrice, ExponentialPrice}; -use sp_runtime::{traits::Saturating, SaturatedConversion}; +use polkadot_runtime_parachains::FeeTracker; +use sp_runtime::{traits::Saturating, SaturatedConversion, FixedPointNumber}; use sp_std::{marker::PhantomData, prelude::*}; use xcm::{latest::prelude::*, WrapVersion}; use xcm_builder::TakeRevenue; @@ -55,7 +56,7 @@ impl, B: Get, M: Get, F: FeeTracker> PriceForParentD fn price_for_parent_delivery(message: &Xcm<()>) -> MultiAssets { let message_fee = (message.encoded_size() as u128).saturating_mul(M::get()); let fee_sum = B::get().saturating_add(message_fee); - let amount = F::get_fee_factor(TransportDestination::Relay).saturating_mul_int(fee_sum); + let amount = F::get_fee_factor(()).saturating_mul_int(fee_sum); (A::get(), amount).into() } } diff --git a/polkadot/runtime/common/src/xcm_sender.rs b/polkadot/runtime/common/src/xcm_sender.rs index 2de5b7470b3a..5e10f31c3eda 100644 --- a/polkadot/runtime/common/src/xcm_sender.rs +++ b/polkadot/runtime/common/src/xcm_sender.rs @@ -64,7 +64,7 @@ impl> PriceForParachainDelivery for ConstantPrice { /// - `M`: The fee to pay for each and every byte of the message after encoding it. /// - `F`: A fee factor multiplier. It can be understood as the exponent term in the formula. pub struct ExponentialPrice(sp_std::marker::PhantomData<(A, B, M, F)>); -impl, B: Get, M: Get, F: FeeTracker> PriceForParachainDelivery +impl, B: Get, M: Get, F: FeeTracker> PriceForParachainDelivery for ExponentialPrice { fn price_for_parachain_delivery(para: ParaId, msg: &Xcm<()>) -> MultiAssets { @@ -222,8 +222,7 @@ mod tests { struct TestFeeTracker; impl FeeTracker for TestFeeTracker { - type Id = (); - fn get_fee_factor(_: Self::Id) -> FixedU128 { + fn get_fee_factor(_: ()) -> FixedU128 { FixedU128::from_rational(101, 100) } } diff --git a/polkadot/runtime/parachains/src/dmp.rs b/polkadot/runtime/parachains/src/dmp.rs index b84a9003f729..cd85d11901e0 100644 --- a/polkadot/runtime/parachains/src/dmp.rs +++ b/polkadot/runtime/parachains/src/dmp.rs @@ -361,10 +361,8 @@ impl Pallet { } } -impl FeeTracker for Pallet { - type Id = ParaId; - - fn get_fee_factor(id: Self::Id) -> FixedU128 { +impl FeeTracker for Pallet { + fn get_fee_factor(id: ParaId) -> FixedU128 { DeliveryFeeFactor::::get(id) } } diff --git a/polkadot/runtime/parachains/src/lib.rs b/polkadot/runtime/parachains/src/lib.rs index 3bdf0d4502c0..eddc6a45e850 100644 --- a/polkadot/runtime/parachains/src/lib.rs +++ b/polkadot/runtime/parachains/src/lib.rs @@ -58,10 +58,9 @@ use primitives::{HeadData, Id as ParaId, ValidationCode}; use sp_runtime::{DispatchResult, FixedU128}; /// Trait for tracking message delivery fees on a transport protocol. -pub trait FeeTracker { - type Id; // A destination id +pub trait FeeTracker { /// The evolving exponential fee factor which will be used to calculate the delivery fees. - fn get_fee_factor(id: Self::Id) -> FixedU128; + fn get_fee_factor(id: Id) -> FixedU128; } /// Schedule a para to be initialized at the start of the next session with the given genesis data. From 72151bf792236b1c8925e5bef032895233032d7f Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 14 Sep 2023 20:49:59 -0300 Subject: [PATCH 05/56] Refactor threshold function --- cumulus/pallets/parachain-system/src/lib.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index a11a44c5c15b..784acb9d719b 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -335,6 +335,14 @@ pub mod pallet { UpwardMessages::::put(&up[..num as usize]); *up = up.split_off(num as usize); + let threshold = Self::get_ump_threshold(host_config.max_upward_queue_size); + let remaining_total_size = up.iter().fold(0, |size_so_far, current_message| { + size_so_far + current_message.len() + }); + if remaining_total_size <= threshold as usize { + Self::decrement_ump_fee_factor(); + } + (num, total_size) }); @@ -1013,6 +1021,15 @@ impl Pallet { *f }) } + + /// Get the threshold for UMP used to increase or decrease fees. + /// The threshold is a factor of the max possible size of the queue. + /// It should be checked against the total size in the queue and not the + /// number of messages, since messages could be big or small + // TODO: Should this be `config.max_upward_queue_size` or `MAX_POSSIBLE_ALLOCATION`? + fn get_ump_threshold(max_upward_queue_size: u32) -> u32 { + max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR) + } } impl FeeTracker for Pallet { @@ -1531,9 +1548,7 @@ impl Pallet { if message_len > cfg.max_upward_message_size as usize { return Err(MessageSendError::TooBig) } - // The threshold to increase fee is a factor of the max length in the queue - // TODO: Should this be `config.max_upward_queue_size` or `MAX_POSSIBLE_ALLOCATION`? - let threshold = cfg.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); + let threshold = Self::get_ump_threshold(cfg.max_upward_queue_size); // We check the threshold against total size and not number of messages since messages could be big or small let pending_messages = PendingUpwardMessages::::get(); let total_size = pending_messages.iter().fold(0, |size_so_far, current_message| { From 998807315c7d1fb05a14dfc6da65690e7063ded8 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Thu, 14 Sep 2023 23:53:53 +0000 Subject: [PATCH 06/56] ".git/.scripts/commands/fmt/fmt.sh" --- cumulus/pallets/parachain-system/src/lib.rs | 20 +++++++++++--------- cumulus/primitives/utility/src/lib.rs | 6 ++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 784acb9d719b..d60218858511 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -53,7 +53,7 @@ use sp_runtime::{ InvalidTransaction, TransactionLongevity, TransactionSource, TransactionValidity, ValidTransaction, }, - DispatchError, RuntimeDebug, FixedU128, Saturating, + DispatchError, FixedU128, RuntimeDebug, Saturating, }; use sp_std::{cmp, collections::btree_map::BTreeMap, prelude::*}; use xcm::latest::XcmHash; @@ -336,9 +336,9 @@ pub mod pallet { *up = up.split_off(num as usize); let threshold = Self::get_ump_threshold(host_config.max_upward_queue_size); - let remaining_total_size = up.iter().fold(0, |size_so_far, current_message| { - size_so_far + current_message.len() - }); + let remaining_total_size = up + .iter() + .fold(0, |size_so_far, current_message| size_so_far + current_message.len()); if remaining_total_size <= threshold as usize { Self::decrement_ump_fee_factor(); } @@ -1017,7 +1017,8 @@ impl Pallet { /// Returns the new delivery fee factor after the decrement. pub(crate) fn decrement_ump_fee_factor() -> FixedU128 { >::mutate(|f| { - *f = UpwardInitialDeliveryFeeFactor::get().max(*f / ump_constants::EXPONENTIAL_FEE_BASE); + *f = + UpwardInitialDeliveryFeeFactor::get().max(*f / ump_constants::EXPONENTIAL_FEE_BASE); *f }) } @@ -1549,11 +1550,12 @@ impl Pallet { return Err(MessageSendError::TooBig) } let threshold = Self::get_ump_threshold(cfg.max_upward_queue_size); - // We check the threshold against total size and not number of messages since messages could be big or small + // We check the threshold against total size and not number of messages since messages + // could be big or small let pending_messages = PendingUpwardMessages::::get(); - let total_size = pending_messages.iter().fold(0, |size_so_far, current_message| { - size_so_far + current_message.len() - }); + let total_size = pending_messages + .iter() + .fold(0, |size_so_far, current_message| size_so_far + current_message.len()); if total_size > threshold as usize { let message_size_factor = FixedU128::from_u32(message_len.saturating_div(1024) as u32) diff --git a/cumulus/primitives/utility/src/lib.rs b/cumulus/primitives/utility/src/lib.rs index d206b3ab820e..5818cb6c84b5 100644 --- a/cumulus/primitives/utility/src/lib.rs +++ b/cumulus/primitives/utility/src/lib.rs @@ -30,7 +30,7 @@ use frame_support::{ }; use polkadot_runtime_common::xcm_sender::{ConstantPrice, ExponentialPrice}; use polkadot_runtime_parachains::FeeTracker; -use sp_runtime::{traits::Saturating, SaturatedConversion, FixedPointNumber}; +use sp_runtime::{traits::Saturating, FixedPointNumber, SaturatedConversion}; use sp_std::{marker::PhantomData, prelude::*}; use xcm::{latest::prelude::*, WrapVersion}; use xcm_builder::TakeRevenue; @@ -52,7 +52,9 @@ impl> PriceForParentDelivery for ConstantPrice { } } -impl, B: Get, M: Get, F: FeeTracker> PriceForParentDelivery for ExponentialPrice { +impl, B: Get, M: Get, F: FeeTracker> PriceForParentDelivery + for ExponentialPrice +{ fn price_for_parent_delivery(message: &Xcm<()>) -> MultiAssets { let message_fee = (message.encoded_size() as u128).saturating_mul(M::get()); let fee_sum = B::get().saturating_add(message_fee); From fea53bacfe0decf569ad5ddeacf3e5e51637aeb4 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 14 Sep 2023 21:24:38 -0300 Subject: [PATCH 07/56] Fix TestFeeTracker --- polkadot/runtime/common/src/xcm_sender.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/polkadot/runtime/common/src/xcm_sender.rs b/polkadot/runtime/common/src/xcm_sender.rs index 98bf97a302c3..ef7adc1c9501 100644 --- a/polkadot/runtime/common/src/xcm_sender.rs +++ b/polkadot/runtime/common/src/xcm_sender.rs @@ -221,8 +221,8 @@ mod tests { } struct TestFeeTracker; - impl FeeTracker for TestFeeTracker { - fn get_fee_factor(_: ()) -> FixedU128 { + impl FeeTracker for TestFeeTracker { + fn get_fee_factor(_: ParaId) -> FixedU128 { FixedU128::from_rational(101, 100) } } @@ -238,21 +238,21 @@ mod tests { // F * (B + msg_length * M) // message_length = 1 - let result: u128 = TestFeeTracker::get_fee_factor(()).saturating_mul_int(b + m); + let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + m); assert_eq!( TestExponentialPrice::price_for_parachain_delivery(id, &Xcm(vec![])), (FeeAssetId::get(), result).into() ); // message size = 2 - let result: u128 = TestFeeTracker::get_fee_factor(()).saturating_mul_int(b + (2 * m)); + let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (2 * m)); assert_eq!( TestExponentialPrice::price_for_parachain_delivery(id, &Xcm(vec![ClearOrigin])), (FeeAssetId::get(), result).into() ); // message size = 4 - let result: u128 = TestFeeTracker::get_fee_factor(()).saturating_mul_int(b + (4 * m)); + let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (4 * m)); assert_eq!( TestExponentialPrice::price_for_parachain_delivery( id, From 1928602e64ea7d45eb6be12bc819abba7cc0ede3 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 14 Sep 2023 22:47:38 -0300 Subject: [PATCH 08/56] Fix UMP ExponentialPrice for asset hub kusama --- .../assets/asset-hub-kusama/src/xcm_config.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs index 23003d9253b1..2dc54594afeb 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs @@ -16,9 +16,9 @@ use super::{ AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, PoolAssets, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - TrustBackedAssetsInstance, WeightToFee, XcmpQueue, + TrustBackedAssetsInstance, WeightToFee, XcmpQueue, TransactionByteFee, }; -use crate::ForeignAssets; +use crate::{ForeignAssets, CENTS}; use assets_common::{ local_and_foreign_assets::MatchesLocalAndForeignAssetsMultiLocation, matching::{ @@ -38,6 +38,7 @@ use parachains_common::{ TREASURY_PALLET_ID, }; use polkadot_parachain_primitives::primitives::Sibling; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use sp_runtime::traits::{AccountIdConversion, ConvertInto}; use xcm::latest::prelude::*; use xcm_builder::{ @@ -539,8 +540,15 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +parameter_types! { + /// The asset ID for the asset that we use to pay for message delivery fees. + pub FeeAssetId: AssetId = Concrete(KsmLocation::get()); + /// The base fee for the message delivery fees. + pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3); +} + pub type PriceForParentDelivery = - ExponentialPrice; // TODO: Track the fee factor in `ParachainSystem` + ExponentialPrice; /// The means for routing XCM messages which are not for local execution into the right message /// queues. From 6e11751749b9e8d93ff6c6b983f2363491c82117 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 26 Sep 2023 20:51:04 +0200 Subject: [PATCH 09/56] Update cumulus/pallets/parachain-system/src/lib.rs Co-authored-by: Keith Yeung --- cumulus/pallets/parachain-system/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 519a6302486d..bae0cafe6826 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -1553,9 +1553,7 @@ impl Pallet { // We check the threshold against total size and not number of messages since messages // could be big or small let pending_messages = PendingUpwardMessages::::get(); - let total_size = pending_messages - .iter() - .fold(0, |size_so_far, current_message| size_so_far + current_message.len()); + let total_size = pending_messages.iter().map(UpwardMessage::len).sum(); if total_size > threshold as usize { let message_size_factor = FixedU128::from_u32(message_len.saturating_div(1024) as u32) From d4b1ae21761fcc5805b6a462ee6a63818f8cff74 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 26 Sep 2023 20:51:26 +0200 Subject: [PATCH 10/56] Update cumulus/pallets/parachain-system/src/lib.rs Co-authored-by: Keith Yeung --- cumulus/pallets/parachain-system/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index bae0cafe6826..9376654ec7a7 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -336,9 +336,7 @@ pub mod pallet { *up = up.split_off(num as usize); let threshold = Self::get_ump_threshold(host_config.max_upward_queue_size); - let remaining_total_size = up - .iter() - .fold(0, |size_so_far, current_message| size_so_far + current_message.len()); + let remaining_total_size = up.iter().map(UpwardMessage::len).sum(); if remaining_total_size <= threshold as usize { Self::decrement_ump_fee_factor(); } From 6f6b0fa3c4f6063ba80601d9aa19a760de2a5a8b Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 26 Sep 2023 16:14:10 -0300 Subject: [PATCH 11/56] Fix type issues --- cumulus/pallets/parachain-system/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 9376654ec7a7..644c1172879b 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -336,7 +336,7 @@ pub mod pallet { *up = up.split_off(num as usize); let threshold = Self::get_ump_threshold(host_config.max_upward_queue_size); - let remaining_total_size = up.iter().map(UpwardMessage::len).sum(); + let remaining_total_size: usize = up.iter().map(UpwardMessage::len).sum(); if remaining_total_size <= threshold as usize { Self::decrement_ump_fee_factor(); } @@ -1551,7 +1551,7 @@ impl Pallet { // We check the threshold against total size and not number of messages since messages // could be big or small let pending_messages = PendingUpwardMessages::::get(); - let total_size = pending_messages.iter().map(UpwardMessage::len).sum(); + let total_size: usize = pending_messages.iter().map(UpwardMessage::len).sum(); if total_size > threshold as usize { let message_size_factor = FixedU128::from_u32(message_len.saturating_div(1024) as u32) From 27c9216770080ff2f151712d1cf769f9f5acdc46 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 26 Sep 2023 17:43:38 -0300 Subject: [PATCH 12/56] Remove one-liner function --- cumulus/pallets/parachain-system/src/lib.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 644c1172879b..c21eeff789f5 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -181,7 +181,10 @@ where mod ump_constants { use super::FixedU128; - pub const THRESHOLD_FACTOR: u32 = 2; // The price starts to increase when queue is half full + /// `max_upward_queue_size / THRESHOLD_FACTOR` is the threshold after which delivery + /// starts getting exponentially more expensive. + /// The price starts to increase when queue is half full + pub const THRESHOLD_FACTOR: u32 = 2; pub const EXPONENTIAL_FEE_BASE: FixedU128 = FixedU128::from_rational(105, 100); // 1.05 pub const MESSAGE_SIZE_FEE_BASE: FixedU128 = FixedU128::from_rational(1, 1000); // 0.001 } @@ -335,7 +338,7 @@ pub mod pallet { UpwardMessages::::put(&up[..num as usize]); *up = up.split_off(num as usize); - let threshold = Self::get_ump_threshold(host_config.max_upward_queue_size); + let threshold = host_config.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); let remaining_total_size: usize = up.iter().map(UpwardMessage::len).sum(); if remaining_total_size <= threshold as usize { Self::decrement_ump_fee_factor(); @@ -1020,15 +1023,6 @@ impl Pallet { *f }) } - - /// Get the threshold for UMP used to increase or decrease fees. - /// The threshold is a factor of the max possible size of the queue. - /// It should be checked against the total size in the queue and not the - /// number of messages, since messages could be big or small - // TODO: Should this be `config.max_upward_queue_size` or `MAX_POSSIBLE_ALLOCATION`? - fn get_ump_threshold(max_upward_queue_size: u32) -> u32 { - max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR) - } } impl FeeTracker for Pallet { @@ -1547,7 +1541,7 @@ impl Pallet { if message_len > cfg.max_upward_message_size as usize { return Err(MessageSendError::TooBig) } - let threshold = Self::get_ump_threshold(cfg.max_upward_queue_size); + let threshold = cfg.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); // We check the threshold against total size and not number of messages since messages // could be big or small let pending_messages = PendingUpwardMessages::::get(); From 5bc24dae3981fe748104b157c1f2293945337243 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 27 Sep 2023 08:16:14 -0300 Subject: [PATCH 13/56] Change xcm delivery fee helpers to work with UMP --- .../src/tests/reserve_transfer.rs | 20 +++++------ .../asset-hub-kusama/src/tests/teleport.rs | 8 ++--- .../src/tests/reserve_transfer.rs | 12 +++---- .../asset-hub-polkadot/src/tests/teleport.rs | 6 ++-- .../src/tests/reserve_transfer.rs | 6 ++-- .../collectives-polkadot/Cargo.toml | 1 - .../src/tests/fellowship.rs | 6 ++-- .../collectives-polkadot/src/xcm_helpers.rs | 6 ++-- .../runtimes/assets/test-utils/Cargo.toml | 2 -- .../assets/test-utils/src/test_cases.rs | 21 ++++++++++-- .../assets/test-utils/src/xcm_helpers.rs | 33 ++++++++++++------- 11 files changed, 71 insertions(+), 50 deletions(-) diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/reserve_transfer.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/reserve_transfer.rs index 331e227ad19d..160415e11ffd 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/reserve_transfer.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/reserve_transfer.rs @@ -14,11 +14,11 @@ // limitations under the License. use crate::*; -use asset_hub_kusama_runtime::{ - xcm_config::TreasuryAccount as AssetHubKusamaTreasuryAccount, PriceForSiblingParachainDelivery, +use asset_hub_kusama_runtime::xcm_config::{ + TreasuryAccount as AssetHubKusamaTreasuryAccount, XcmConfig as AssetHubKusamaXcmConfig, }; use kusama_runtime::xcm_config::{ - PriceForChildParachainDelivery, TreasuryAccount as KusamaTreasuryAccount, + XcmConfig as KusamaXcmConfig, TreasuryAccount as KusamaTreasuryAccount, }; fn relay_origin_assertions(t: RelayToSystemParaTest) { @@ -27,7 +27,7 @@ fn relay_origin_assertions(t: RelayToSystemParaTest) { Kusama::assert_xcm_pallet_attempted_complete(Some(Weight::from_parts(630_092_000, 6_196))); let delivery_fees_amount = - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( t.args.assets.clone(), 0, t.args.weight_limit, @@ -80,7 +80,7 @@ fn system_para_to_para_assertions(t: SystemParaToParaTest) { ))); let delivery_fees_amount = - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( t.args.assets.clone(), 0, t.args.weight_limit, @@ -124,7 +124,7 @@ fn system_para_to_para_assets_assertions(t: SystemParaToParaTest) { ))); let delivery_fees_amount = - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( t.args.assets.clone(), 0, t.args.weight_limit, @@ -249,7 +249,7 @@ fn limited_reserve_transfer_native_asset_from_relay_to_system_para_fails() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Kusama::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, @@ -318,7 +318,7 @@ fn reserve_transfer_native_asset_from_relay_to_system_para_fails() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Kusama::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, @@ -390,7 +390,7 @@ fn limited_reserve_transfer_native_asset_from_system_para_to_para() { let sender_balance_after = test.sender.balance; let delivery_fees = AssetHubKusama::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, @@ -432,7 +432,7 @@ fn reserve_transfer_native_asset_from_system_para_to_para() { let sender_balance_after = test.sender.balance; let delivery_fees = AssetHubKusama::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/teleport.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/teleport.rs index 7bcae88915cc..ace58cbed663 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/teleport.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/teleport.rs @@ -17,7 +17,7 @@ use crate::*; use kusama_runtime::xcm_config::{ - PriceForChildParachainDelivery, TreasuryAccount as KusamaTreasuryAccount, + XcmConfig, TreasuryAccount as KusamaTreasuryAccount, }; fn relay_origin_assertions(t: RelayToSystemParaTest) { @@ -26,7 +26,7 @@ fn relay_origin_assertions(t: RelayToSystemParaTest) { Kusama::assert_xcm_pallet_attempted_complete(Some(Weight::from_parts(631_531_000, 7_186))); let delivery_fees_amount = - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( t.args.assets.clone(), 0, t.args.weight_limit, @@ -200,7 +200,7 @@ fn limited_teleport_native_assets_from_relay_to_system_para_works() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Kusama::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, @@ -313,7 +313,7 @@ fn teleport_native_assets_from_relay_to_system_para_works() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Kusama::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/reserve_transfer.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/reserve_transfer.rs index 9b1df387f97e..7a664cafd9a5 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/reserve_transfer.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/reserve_transfer.rs @@ -14,8 +14,8 @@ // limitations under the License. use crate::*; -use asset_hub_polkadot_runtime::PriceForSiblingParachainDelivery; -use polkadot_runtime::xcm_config::PriceForChildParachainDelivery; +use asset_hub_polkadot_runtime::xcm_config::XcmConfig as AssetHubPolkadotXcmConfig; +use polkadot_runtime::xcm_config::XcmConfig as PolkadotXcmConfig; fn relay_origin_assertions(t: RelayToSystemParaTest) { type RuntimeEvent = ::RuntimeEvent; @@ -188,7 +188,7 @@ fn limited_reserve_transfer_native_asset_from_relay_to_system_para_fails() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Polkadot::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, @@ -257,7 +257,7 @@ fn reserve_transfer_native_asset_from_relay_to_system_para_fails() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Polkadot::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, @@ -329,7 +329,7 @@ fn limited_reserve_transfer_native_asset_from_system_para_to_para() { let sender_balance_after = test.sender.balance; let delivery_fees = AssetHubPolkadot::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, @@ -371,7 +371,7 @@ fn reserve_transfer_native_asset_from_system_para_to_para() { let sender_balance_after = test.sender.balance; let delivery_fees = AssetHubPolkadot::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/teleport.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/teleport.rs index 67a622031b6b..43702c9424c1 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/teleport.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/teleport.rs @@ -16,7 +16,7 @@ #![allow(dead_code)] // use crate::*; -use polkadot_runtime::xcm_config::PriceForChildParachainDelivery; +use polkadot_runtime::xcm_config::XcmConfig; fn relay_origin_assertions(t: RelayToSystemParaTest) { type RuntimeEvent = ::RuntimeEvent; @@ -179,7 +179,7 @@ fn limited_teleport_native_assets_from_relay_to_system_para_works() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Polkadot::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, @@ -292,7 +292,7 @@ fn teleport_native_assets_from_relay_to_system_para_works() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Polkadot::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/reserve_transfer.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/reserve_transfer.rs index edf3d3e7a629..66e5cc9b72a4 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/reserve_transfer.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/reserve_transfer.rs @@ -14,7 +14,7 @@ // limitations under the License. use crate::*; -use asset_hub_westend_runtime::PriceForSiblingParachainDelivery; +use asset_hub_westend_runtime::xcm_config::XcmConfig; fn relay_origin_assertions(t: RelayToSystemParaTest) { type RuntimeEvent = ::RuntimeEvent; @@ -308,7 +308,7 @@ fn limited_reserve_transfer_native_asset_from_system_para_to_para() { let sender_balance_after = test.sender.balance; let delivery_fees = AssetHubWestend::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, @@ -350,7 +350,7 @@ fn reserve_transfer_native_asset_from_system_para_to_para() { let sender_balance_after = test.sender.balance; let delivery_fees = AssetHubWestend::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::( + xcm_helpers::transfer_assets_delivery_fees::( test.args.assets.clone(), 0, test.args.weight_limit, diff --git a/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/Cargo.toml b/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/Cargo.toml index 2700f0022a08..10801f409322 100644 --- a/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/Cargo.toml +++ b/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/Cargo.toml @@ -23,7 +23,6 @@ pallet-salary = { path = "../../../../../../substrate/frame/salary", default-fea polkadot-core-primitives = { path = "../../../../../../polkadot/core-primitives", default-features = false} polkadot-parachain-primitives = { path = "../../../../../../polkadot/parachain", default-features = false} polkadot-runtime-parachains = { path = "../../../../../../polkadot/runtime/parachains" } -polkadot-runtime-common = { path = "../../../../../../polkadot/runtime/common" } polkadot-runtime = { path = "../../../../../../polkadot/runtime/polkadot" } xcm = { package = "staging-xcm", path = "../../../../../../polkadot/xcm", default-features = false} pallet-xcm = { path = "../../../../../../polkadot/xcm/pallet-xcm", default-features = false} diff --git a/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/src/tests/fellowship.rs b/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/src/tests/fellowship.rs index 21f5116c8a4c..0c51585c840d 100644 --- a/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/src/tests/fellowship.rs +++ b/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/src/tests/fellowship.rs @@ -18,9 +18,7 @@ use crate::*; use collectives_polkadot_runtime::fellowship::FellowshipSalaryPaymaster; use integration_tests_common::constants::{collectives, asset_hub_polkadot}; -use asset_hub_polkadot_runtime::{ - PriceForSiblingParachainDelivery as AssetHubPolkadotPriceForParachainDelivery, -}; +use asset_hub_polkadot_runtime::xcm_config::XcmConfig as AssetHubPolkadotXcmConfig; use frame_support::traits::{ fungibles::{Create, Mutate}, fungible, @@ -50,7 +48,7 @@ fn pay_salary() { )); // Make sure we have enough assets for delivery let querier = (Parent, Parachain(collectives::PARA_ID)).into(); - let delivery_fees = xcm_helpers::query_response_delivery_fees::(querier); + let delivery_fees = xcm_helpers::query_response_delivery_fees::(querier); assert_ok!(>::mint_into(&pay_from, delivery_fees + asset_hub_polkadot::ED)); assert_ok!(>::mint_into(asset_id, &pay_from, pay_amount * 2)); }); diff --git a/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/src/xcm_helpers.rs b/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/src/xcm_helpers.rs index 60ab4b0233c8..0d41052f7044 100644 --- a/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/src/xcm_helpers.rs +++ b/cumulus/parachains/integration-tests/emulated/collectives/collectives-polkadot/src/xcm_helpers.rs @@ -16,10 +16,9 @@ //! XCM helpers for getting delivery fees for tests -use polkadot_runtime_common::xcm_sender::PriceForParachainDelivery; use xcm::latest::prelude::*; -pub fn query_response_delivery_fees(querier: MultiLocation) -> u128 { +pub fn query_response_delivery_fees(querier: MultiLocation) -> u128 { // Message to calculate delivery fees, it's encoded size is what's important. // This message reports that there was no error, if an error is reported, the encoded size would be different. let message = Xcm(vec![ @@ -31,8 +30,7 @@ pub fn query_response_delivery_fees(querier: Multi }, SetTopic([0u8; 32]), // Dummy topic ]); - let Parachain(para_id) = querier.interior().last().unwrap() else { unreachable!("Location is parachain") }; - let delivery_fees = P::price_for_parachain_delivery((*para_id).into(), &message); + let Ok((_, delivery_fees)) = validate_send::(querier, message) else { unreachable!("message can be sent; qed") }; let Fungible(delivery_fees_amount) = delivery_fees.inner()[0].fun else { unreachable!("Asset is fungible") }; delivery_fees_amount } diff --git a/cumulus/parachains/runtimes/assets/test-utils/Cargo.toml b/cumulus/parachains/runtimes/assets/test-utils/Cargo.toml index db4e4a11e759..2ec0162f2aef 100644 --- a/cumulus/parachains/runtimes/assets/test-utils/Cargo.toml +++ b/cumulus/parachains/runtimes/assets/test-utils/Cargo.toml @@ -38,7 +38,6 @@ xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-f xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false } pallet-xcm = { path = "../../../../../polkadot/xcm/pallet-xcm", default-features = false } polkadot-parachain-primitives = { path = "../../../../../polkadot/parachain", default-features = false } -polkadot-runtime-common = { path = "../../../../../polkadot/runtime/common", default-features = false } [dev-dependencies] hex-literal = "0.4.1" @@ -67,7 +66,6 @@ std = [ "parachains-common/std", "parachains-runtimes-test-utils/std", "polkadot-parachain-primitives/std", - "polkadot-runtime-common/std", "sp-consensus-aura/std", "sp-core/std", "sp-io/std", diff --git a/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs b/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs index c2ec5401e795..d1b30457cca6 100644 --- a/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs +++ b/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs @@ -177,6 +177,22 @@ pub fn teleports_for_native_asset_works< target_account_balance_before_teleport - existential_deposit ); + // Make sure account can pay delivery fees + let delivery_fees = xcm_helpers::transfer_assets_delivery_fees::< + XcmConfig::XcmSender, + >( + (native_asset_id, native_asset_to_teleport_away.into()).into(), + 0, + Unlimited, + dest_beneficiary, + dest, + ); + >::mint_into( + &target_account, + delivery_fees.into(), + ) + .unwrap(); + assert_ok!(RuntimeHelper::::do_teleport_assets::( RuntimeHelper::::origin_of(target_account.clone()), dest, @@ -186,6 +202,7 @@ pub fn teleports_for_native_asset_works< included_head.clone(), &alice, )); + // check balances assert_eq!( >::free_balance(&target_account), @@ -235,7 +252,7 @@ pub fn teleports_for_native_asset_works< )); let delivery_fees = xcm_helpers::transfer_assets_delivery_fees::< - ::PriceForSiblingDelivery, + XcmConfig::XcmSender, >( (native_asset_id, native_asset_to_teleport_away.into()).into(), 0, @@ -555,7 +572,7 @@ pub fn teleports_for_foreign_assets_works< // Make sure the target account has enough native asset to pay for delivery fees let delivery_fees = xcm_helpers::transfer_assets_delivery_fees::< - ::PriceForSiblingDelivery, + XcmConfig::XcmSender, >( (foreign_asset_id_multilocation, asset_to_teleport_away).into(), 0, diff --git a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs index 004adcea3214..ba736c63363e 100644 --- a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs +++ b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs @@ -14,30 +14,41 @@ // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . -use polkadot_runtime_common::xcm_sender::PriceForParachainDelivery; +//! Helpers for calculating XCM delivery fees. + use xcm::latest::prelude::*; /// Returns the delivery fees amount for pallet xcm's `teleport_assets` and /// `reserve_transfer_assets` extrinsics. -pub fn transfer_assets_delivery_fees( +/// It assumes delivery fees are only paid in one asset and that asset is known. +pub fn transfer_assets_parent_delivery_fees( assets: MultiAssets, fee_asset_item: u32, weight_limit: WeightLimit, beneficiary: MultiLocation, destination: MultiLocation, ) -> u128 { - // Approximation of the actual message sent by the extrinsic. - // The assets are not reanchored and the topic is a dummy one. - // However, it should have the same encoded size, which is what matters for delivery fees. - let message = Xcm(vec![ + let message = teleport_assets_dummy_message(assets, fee_asset_item, weight_limit, beneficiary); + let Ok((_, delivery_fees)) = validate_send::(destination, message) else { unreachable!("message can be sent; qed") }; + let Fungible(delivery_fees_amount) = delivery_fees.inner()[0].fun else { unreachable!("asset is fungible; qed") }; + delivery_fees_amount +} + +/// Approximates the actual message sent by the teleport extrinsic. +/// The assets are not reanchored and the topic is a dummy one. +/// However, it should have the same encoded size, which is what matters for delivery fees. +/// Also has same encoded size as the one created by the reserve transfer assets extrinsic. +fn teleport_assets_dummy_message( + assets: MultiAssets, + fee_asset_item: u32, + weight_limit: WeightLimit, + beneficiary: MultiLocation, +) -> Xcm<()> { + Xcm(vec![ ReceiveTeleportedAsset(assets.clone()), // Same encoded size as `ReserveAssetDeposited` ClearOrigin, BuyExecution { fees: assets.get(fee_asset_item as usize).unwrap().clone(), weight_limit }, DepositAsset { assets: Wild(AllCounted(assets.len() as u32)), beneficiary }, SetTopic([0u8; 32]), // Dummy topic - ]); - let Parachain(para_id) = destination.interior().last().unwrap() else { unreachable!("Location is parachain") }; - let delivery_fees = P::price_for_parachain_delivery((*para_id).into(), &message); - let Fungible(delivery_fees_amount) = delivery_fees.inner()[0].fun else { unreachable!("Asset is fungible") }; - delivery_fees_amount + ]) } From d6d4f192ebbd0ada44e2e9503f50bf74b7540a92 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 27 Sep 2023 09:28:42 -0300 Subject: [PATCH 14/56] Fix feature formatting --- cumulus/pallets/parachain-system/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/pallets/parachain-system/Cargo.toml b/cumulus/pallets/parachain-system/Cargo.toml index ade712ea3b53..0693b1f23981 100644 --- a/cumulus/pallets/parachain-system/Cargo.toml +++ b/cumulus/pallets/parachain-system/Cargo.toml @@ -89,8 +89,8 @@ runtime-benchmarks = [ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", - "sp-runtime/try-runtime", "polkadot-runtime-parachains/runtime-benchmarks", + "sp-runtime/try-runtime", ] parameterized-consensus-hook = [] From e82e0bbe494de9834f6b7508a632c1422f043484 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 27 Sep 2023 09:35:01 -0300 Subject: [PATCH 15/56] Fix feature propagation for try-runtime --- cumulus/pallets/parachain-system/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/pallets/parachain-system/Cargo.toml b/cumulus/pallets/parachain-system/Cargo.toml index 0693b1f23981..64e238ecab69 100644 --- a/cumulus/pallets/parachain-system/Cargo.toml +++ b/cumulus/pallets/parachain-system/Cargo.toml @@ -89,7 +89,7 @@ runtime-benchmarks = [ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", - "polkadot-runtime-parachains/runtime-benchmarks", + "polkadot-runtime-parachains/try-runtime", "sp-runtime/try-runtime", ] From fb538d509fa303eed358d2add78cadbf2ef65b08 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 27 Sep 2023 14:25:34 -0300 Subject: [PATCH 16/56] Add UMP pricing to all runtimes --- .../asset-hub-kusama/src/tests/teleport.rs | 33 +++++++++++++++---- .../asset-hub-polkadot/src/tests/teleport.rs | 31 ++++++++++++++--- .../asset-hub-westend/src/tests/teleport.rs | 25 ++++++++++++-- .../asset-hub-polkadot/src/xcm_config.rs | 8 +++-- .../asset-hub-westend/src/xcm_config.rs | 8 +++-- .../assets/test-utils/src/xcm_helpers.rs | 2 +- .../bridge-hub-kusama/src/xcm_config.rs | 7 +++- .../bridge-hub-polkadot/src/xcm_config.rs | 7 +++- .../bridge-hub-rococo/src/xcm_config.rs | 8 +++-- .../collectives-polkadot/src/lib.rs | 19 +++++++++-- .../collectives-polkadot/src/xcm_config.rs | 7 +++- .../contracts-rococo/src/xcm_config.rs | 7 +++- 12 files changed, 135 insertions(+), 27 deletions(-) diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/teleport.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/teleport.rs index 5de1b0b5f2b1..4e83e84a4f4a 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/teleport.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-kusama/src/tests/teleport.rs @@ -16,7 +16,8 @@ #![allow(dead_code)] // use crate::*; -use kusama_runtime::xcm_config::{TreasuryAccount as KusamaTreasuryAccount, XcmConfig}; +use kusama_runtime::xcm_config::{TreasuryAccount as KusamaTreasuryAccount, XcmConfig as KusamaXcmConfig}; +use asset_hub_kusama_runtime::xcm_config::XcmConfig as AssetHubKusamaXcmConfig; fn relay_origin_assertions(t: RelayToSystemParaTest) { type RuntimeEvent = ::RuntimeEvent; @@ -24,7 +25,7 @@ fn relay_origin_assertions(t: RelayToSystemParaTest) { Kusama::assert_xcm_pallet_attempted_complete(Some(Weight::from_parts(631_531_000, 7_186))); let delivery_fees_amount = xcm_helpers::transfer_assets_delivery_fees::< - ::XcmSender, + ::XcmSender, >( t.args.assets.clone(), 0, @@ -199,7 +200,7 @@ fn limited_teleport_native_assets_from_relay_to_system_para_works() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Kusama::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( test.args.assets.clone(), 0, test.args.weight_limit, @@ -246,8 +247,18 @@ fn limited_teleport_native_assets_back_from_system_para_to_relay_works() { let sender_balance_after = test.sender.balance; let receiver_balance_after = test.receiver.balance; + let delivery_fees = AssetHubKusama::execute_with(|| { + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + test.args.assets.clone(), + 0, + test.args.weight_limit, + test.args.beneficiary, + test.args.dest, + ) + }); + // Sender's balance is reduced - assert_eq!(sender_balance_before - amount_to_send, sender_balance_after); + assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after); // Receiver's balance is increased assert!(receiver_balance_after > receiver_balance_before); } @@ -281,8 +292,18 @@ fn limited_teleport_native_assets_from_system_para_to_relay_fails() { let sender_balance_after = test.sender.balance; let receiver_balance_after = test.receiver.balance; + let delivery_fees = AssetHubKusama::execute_with(|| { + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + test.args.assets.clone(), + 0, + test.args.weight_limit, + test.args.beneficiary, + test.args.dest, + ) + }); + // Sender's balance is reduced - assert_eq!(sender_balance_before - amount_to_send, sender_balance_after); + assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after); // Receiver's balance does not change assert_eq!(receiver_balance_after, receiver_balance_before); } @@ -312,7 +333,7 @@ fn teleport_native_assets_from_relay_to_system_para_works() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Kusama::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( test.args.assets.clone(), 0, test.args.weight_limit, diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/teleport.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/teleport.rs index 52e3e67c5505..ee0c06bbd962 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/teleport.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-polkadot/src/tests/teleport.rs @@ -16,7 +16,8 @@ #![allow(dead_code)] // use crate::*; -use polkadot_runtime::xcm_config::XcmConfig; +use polkadot_runtime::xcm_config::XcmConfig as PolkadotXcmConfig; +use asset_hub_polkadot_runtime::xcm_config::XcmConfig as AssetHubPolkadotXcmConfig; fn relay_origin_assertions(t: RelayToSystemParaTest) { type RuntimeEvent = ::RuntimeEvent; @@ -179,7 +180,7 @@ fn limited_teleport_native_assets_from_relay_to_system_para_works() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Polkadot::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( test.args.assets.clone(), 0, test.args.weight_limit, @@ -226,8 +227,18 @@ fn limited_teleport_native_assets_back_from_system_para_to_relay_works() { let sender_balance_after = test.sender.balance; let receiver_balance_after = test.receiver.balance; + let delivery_fees = AssetHubPolkadot::execute_with(|| { + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + test.args.assets.clone(), + 0, + test.args.weight_limit, + test.args.beneficiary, + test.args.dest, + ) + }); + // Sender's balance is reduced - assert_eq!(sender_balance_before - amount_to_send, sender_balance_after); + assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after); // Receiver's balance is increased assert!(receiver_balance_after > receiver_balance_before); } @@ -261,8 +272,18 @@ fn limited_teleport_native_assets_from_system_para_to_relay_fails() { let sender_balance_after = test.sender.balance; let receiver_balance_after = test.receiver.balance; + let delivery_fees = AssetHubPolkadot::execute_with(|| { + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + test.args.assets.clone(), + 0, + test.args.weight_limit, + test.args.beneficiary, + test.args.dest, + ) + }); + // Sender's balance is reduced - assert_eq!(sender_balance_before - amount_to_send, sender_balance_after); + assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after); // Receiver's balance does not change assert_eq!(receiver_balance_after, receiver_balance_before); } @@ -292,7 +313,7 @@ fn teleport_native_assets_from_relay_to_system_para_works() { let receiver_balance_after = test.receiver.balance; let delivery_fees = Polkadot::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( test.args.assets.clone(), 0, test.args.weight_limit, diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs index 8de73a7420c6..f7bc1d0c500d 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs @@ -16,6 +16,7 @@ #![allow(dead_code)] // use crate::*; +use asset_hub_westend_runtime::xcm_config::XcmConfig as AssetHubWestendXcmConfig; fn relay_origin_assertions(t: RelayToSystemParaTest) { type RuntimeEvent = ::RuntimeEvent; @@ -215,8 +216,18 @@ fn limited_teleport_native_assets_back_from_system_para_to_relay_works() { let sender_balance_after = test.sender.balance; let receiver_balance_after = test.receiver.balance; + let delivery_fees = AssetHubWestend::execute_with(|| { + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + test.args.assets.clone(), + 0, + test.args.weight_limit, + test.args.beneficiary, + test.args.dest, + ) + }); + // Sender's balance is reduced - assert_eq!(sender_balance_before - amount_to_send, sender_balance_after); + assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after); // Receiver's balance is increased assert!(receiver_balance_after > receiver_balance_before); } @@ -250,8 +261,18 @@ fn limited_teleport_native_assets_from_system_para_to_relay_fails() { let sender_balance_after = test.sender.balance; let receiver_balance_after = test.receiver.balance; + let delivery_fees = AssetHubWestend::execute_with(|| { + xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( + test.args.assets.clone(), + 0, + test.args.weight_limit, + test.args.beneficiary, + test.args.dest, + ) + }); + // Sender's balance is reduced - assert_eq!(sender_balance_before - amount_to_send, sender_balance_after); + assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after); // Receiver's balance does not change assert_eq!(receiver_balance_after, receiver_balance_before); } diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs index 9e36f84206f5..70dddba22b50 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs @@ -16,7 +16,7 @@ use super::{ AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ForeignAssets, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - TrustBackedAssetsInstance, WeightToFee, XcmpQueue, + TrustBackedAssetsInstance, WeightToFee, XcmpQueue, TransactionByteFee, FeeAssetId, BaseDeliveryFee, }; use assets_common::matching::{ FromSiblingParachain, IsForeignConcreteAsset, StartsWith, StartsWithExplicitGlobalConsensus, @@ -32,6 +32,7 @@ use parachains_common::{ xcm_config::{AssetFeeAsExistentialDepositMultiplier, RelayOrOtherSystemParachains}, TREASURY_PALLET_ID, }; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_constants::system_parachain::SystemParachains; use sp_runtime::traits::{AccountIdConversion, ConvertInto}; @@ -463,11 +464,14 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +pub type PriceForParentDelivery = + ExponentialPrice; + /// The means for routing XCM messages which are not for local execution into the right message /// queues. pub type XcmRouter = WithUniqueTopic<( // Two routers - use UMP to communicate with the relay chain: - cumulus_primitives_utility::ParentAsUmp, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, )>; diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs index 45c3db663a0e..2d7beefd91f5 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs @@ -16,7 +16,7 @@ use super::{ AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, PoolAssets, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - TrustBackedAssetsInstance, WeightToFee, XcmpQueue, + TrustBackedAssetsInstance, WeightToFee, XcmpQueue, FeeAssetId, BaseDeliveryFee, TransactionByteFee, }; use crate::ForeignAssets; use assets_common::{ @@ -36,6 +36,7 @@ use parachains_common::{ xcm_config::{AssetFeeAsExistentialDepositMultiplier, RelayOrOtherSystemParachains}, TREASURY_PALLET_ID, }; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; use sp_runtime::traits::{AccountIdConversion, ConvertInto}; use westend_runtime_constants::system_parachain::SystemParachains; @@ -541,11 +542,14 @@ impl xcm_executor::Config for XcmConfig { /// Local origins on this chain are allowed to dispatch XCM sends/executions. pub type LocalOriginToLocation = SignedToAccountId32; +pub type PriceForParentDelivery = + ExponentialPrice; + /// The means for routing XCM messages which are not for local execution into the right message /// queues. pub type XcmRouter = WithUniqueTopic<( // Two routers - use UMP to communicate with the relay chain: - cumulus_primitives_utility::ParentAsUmp, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, )>; diff --git a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs index 0e661b9c6eb9..cbb5e4641193 100644 --- a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs +++ b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs @@ -29,7 +29,7 @@ pub fn transfer_assets_delivery_fees( destination: MultiLocation, ) -> u128 { let message = teleport_assets_dummy_message(assets, fee_asset_item, weight_limit, beneficiary); - let Ok((_, delivery_fees)) = validate_send::(destination, message) else { unreachable!("message can be sent; qed") }; + let Ok((_, delivery_fees)) = validate_send::(destination, message) else { unreachable!("message can be validated; qed") }; let Fungible(delivery_fees_amount) = delivery_fees.inner()[0].fun else { unreachable!("asset is fungible; qed") }; delivery_fees_amount } diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs index b2fa6e2d9c91..61198f15f208 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs @@ -17,6 +17,7 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, + FeeAssetId, BaseDeliveryFee, TransactionByteFee, }; use frame_support::{ match_types, parameter_types, @@ -30,6 +31,7 @@ use parachains_common::{ xcm_config::{ConcreteNativeAssetFrom, RelayOrOtherSystemParachains}, TREASURY_PALLET_ID, }; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; use sp_runtime::traits::AccountIdConversion; use xcm::latest::prelude::*; @@ -230,11 +232,14 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +pub type PriceForParentDelivery = + ExponentialPrice; + /// The means for routing XCM messages which are not for local execution into the right message /// queues. pub type XcmRouter = WithUniqueTopic<( // Two routers - use UMP to communicate with the relay chain: - cumulus_primitives_utility::ParentAsUmp, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, )>; diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs index 2493ad5a7bf2..5e6c2a19d0d6 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs @@ -17,6 +17,7 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, + FeeAssetId, BaseDeliveryFee, TransactionByteFee, }; use frame_support::{ match_types, parameter_types, @@ -29,6 +30,7 @@ use parachains_common::{ xcm_config::{ConcreteNativeAssetFrom, RelayOrOtherSystemParachains}, TREASURY_PALLET_ID, }; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_constants::system_parachain::SystemParachains; use sp_runtime::traits::AccountIdConversion; @@ -234,11 +236,14 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +pub type PriceForParentDelivery = + ExponentialPrice; + /// The means for routing XCM messages which are not for local execution into the right message /// queues. pub type XcmRouter = WithUniqueTopic<( // Two routers - use UMP to communicate with the relay chain: - cumulus_primitives_utility::ParentAsUmp, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, )>; diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs index 3baa90dadcf0..dec2bc7e2b70 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs @@ -18,7 +18,7 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, BridgeGrandpaRococoInstance, BridgeGrandpaWococoInstance, DeliveryRewardInBalance, ParachainInfo, ParachainSystem, PolkadotXcm, RequiredStakeForStakeAndSlash, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - WeightToFee, XcmpQueue, + WeightToFee, XcmpQueue, FeeAssetId, BaseDeliveryFee, TransactionByteFee, }; use crate::{ bridge_hub_rococo_config::ToBridgeHubWococoHaulBlobExporter, @@ -35,6 +35,7 @@ use parachains_common::{ xcm_config::{ConcreteNativeAssetFrom, RelayOrOtherSystemParachains}, TREASURY_PALLET_ID, }; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; use rococo_runtime_constants::system_parachain::SystemParachains; use sp_core::Get; @@ -272,6 +273,9 @@ impl xcm_executor::Config for XcmConfig { type Aliasers = Nothing; } +pub type PriceForParentDelivery = + ExponentialPrice; + /// Converts a local signed origin into an XCM multilocation. /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; @@ -280,7 +284,7 @@ pub type LocalOriginToLocation = SignedToAccountId32, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, )>; diff --git a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs index cc4428061c5c..f512edd85417 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs @@ -98,7 +98,7 @@ pub use sp_runtime::BuildStorage; // Polkadot imports use pallet_xcm::{EnsureXcm, IsVoiceOfBody}; use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate}; -use xcm::latest::BodyId; +use xcm::latest::prelude::*; use xcm_executor::XcmExecutor; use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}; @@ -401,6 +401,20 @@ impl parachain_info::Config for Runtime {} impl cumulus_pallet_aura_ext::Config for Runtime {} +parameter_types! { + /// The asset ID for the asset that we use to pay for message delivery fees. + pub FeeAssetId: AssetId = Concrete(xcm_config::DotLocation::get()); + /// The base fee for the message delivery fees. + pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3); +} + +pub type PriceForSiblingParachainDelivery = polkadot_runtime_common::xcm_sender::ExponentialPrice< + FeeAssetId, + BaseDeliveryFee, + TransactionByteFee, + XcmpQueue, +>; + impl cumulus_pallet_xcmp_queue::Config for Runtime { type RuntimeEvent = RuntimeEvent; type XcmExecutor = XcmExecutor; @@ -410,8 +424,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { type ControllerOrigin = EitherOfDiverse, Fellows>; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; type WeightInfo = weights::cumulus_pallet_xcmp_queue::WeightInfo; - // TODO: This should have the price set - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = PriceForSiblingParachainDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { diff --git a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs index d2d667fe85ae..c293b9711254 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs @@ -16,6 +16,7 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, Fellows, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, + FeeAssetId, BaseDeliveryFee, TransactionByteFee, }; use frame_support::{ match_types, parameter_types, @@ -29,6 +30,7 @@ use parachains_common::{ xcm_config::{ConcreteNativeAssetFrom, RelayOrOtherSystemParachains}, TREASURY_PALLET_ID, }; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_constants::{ system_parachain::SystemParachains, xcm::body::FELLOWSHIP_ADMIN_INDEX, @@ -288,11 +290,14 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +pub type PriceForParentDelivery = + ExponentialPrice; + /// The means for routing XCM messages which are not for local execution into the right message /// queues. pub type XcmRouter = WithUniqueTopic<( // Two routers - use UMP to communicate with the relay chain: - cumulus_primitives_utility::ParentAsUmp, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, )>; diff --git a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs index 3bf2b3e50816..e2a8df75214a 100644 --- a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs @@ -16,6 +16,7 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, + FeeAssetId, BaseDeliveryFee, TransactionByteFee, }; use frame_support::{ match_types, parameter_types, @@ -26,6 +27,7 @@ use frame_system::EnsureRoot; use pallet_xcm::{EnsureXcm, IsMajorityOfBody, XcmPassthrough}; use parachains_common::{xcm_config::RelayOrOtherSystemParachains, TREASURY_PALLET_ID}; use polkadot_parachain_primitives::primitives::Sibling; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use rococo_runtime_constants::system_parachain::SystemParachains; use sp_runtime::traits::AccountIdConversion; use xcm::latest::prelude::*; @@ -184,11 +186,14 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +pub type PriceForParentDelivery = + ExponentialPrice; + /// The means for routing XCM messages which are not for local execution into the right message /// queues. pub type XcmRouter = WithUniqueTopic<( // Two routers - use UMP to communicate with the relay chain: - cumulus_primitives_utility::ParentAsUmp, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, )>; From 8ce5488ade90ada2d93933d4d5c0eff25d350766 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 27 Sep 2023 14:31:12 -0300 Subject: [PATCH 17/56] Fix feature propagation --- cumulus/primitives/utility/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/cumulus/primitives/utility/Cargo.toml b/cumulus/primitives/utility/Cargo.toml index 90fc7e508d68..168b13a65ea1 100644 --- a/cumulus/primitives/utility/Cargo.toml +++ b/cumulus/primitives/utility/Cargo.toml @@ -47,4 +47,5 @@ runtime-benchmarks = [ "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", + "polkadot-runtime-parachains/runtime-benchmarks", ] From cf790cadd8fb3bd05d0e6f5f2659d2e5d837e0a4 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 27 Sep 2023 14:47:15 -0300 Subject: [PATCH 18/56] Fix contracts-rococo --- .../contracts/contracts-rococo/src/lib.rs | 6 +++++- .../contracts-rococo/src/xcm_config.rs | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs index 70392c5ecbcc..1ea3eaa2e478 100644 --- a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs @@ -224,13 +224,17 @@ impl pallet_balances::Config for Runtime { type MaxFreezes = ConstU32<0>; } +parameter_types! { + pub const TransactionByteFee: Balance = MILLICENTS; +} + impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter>; type WeightToFee = WeightToFee; /// Relay Chain `TransactionByteFee` / 10 - type LengthToFee = ConstantMultiplier>; + type LengthToFee = ConstantMultiplier; type FeeMultiplierUpdate = SlowAdjustingFeeUpdate; type OperationalFeeMultiplier = ConstU8<5>; } diff --git a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs index e2a8df75214a..f87d46a09951 100644 --- a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs @@ -16,7 +16,7 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, - FeeAssetId, BaseDeliveryFee, TransactionByteFee, + TransactionByteFee, }; use frame_support::{ match_types, parameter_types, @@ -41,6 +41,7 @@ use xcm_builder::{ WithComputedOrigin, WithUniqueTopic, XcmFeesToAccount, }; use xcm_executor::XcmExecutor; +use crate::common::rococo::currency::CENTS; parameter_types! { pub const RelayLocation: MultiLocation = MultiLocation::parent(); @@ -241,6 +242,20 @@ impl cumulus_pallet_xcm::Config for Runtime { type XcmExecutor = XcmExecutor; } +parameter_types! { + /// The asset ID for the asset that we use to pay for message delivery fees. + pub FeeAssetId: AssetId = Concrete(RelayLocation::get()); + /// The base fee for the message delivery fees. + pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3); +} + +pub type PriceForSiblingParachainDelivery = polkadot_runtime_common::xcm_sender::ExponentialPrice< + FeeAssetId, + BaseDeliveryFee, + TransactionByteFee, + XcmpQueue, +>; + impl cumulus_pallet_xcmp_queue::Config for Runtime { type RuntimeEvent = RuntimeEvent; type XcmExecutor = XcmExecutor; @@ -253,7 +268,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { >; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; type WeightInfo = cumulus_pallet_xcmp_queue::weights::SubstrateWeight; - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = PriceForSiblingParachainDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { From 55f8adb9f03bc3ade8721d0f2979be0f456966a2 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 27 Sep 2023 14:49:02 -0300 Subject: [PATCH 19/56] Format features --- cumulus/primitives/utility/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/primitives/utility/Cargo.toml b/cumulus/primitives/utility/Cargo.toml index 168b13a65ea1..7ecd7a1788a4 100644 --- a/cumulus/primitives/utility/Cargo.toml +++ b/cumulus/primitives/utility/Cargo.toml @@ -44,8 +44,8 @@ std = [ runtime-benchmarks = [ "frame-support/runtime-benchmarks", "polkadot-runtime-common/runtime-benchmarks", + "polkadot-runtime-parachains/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", - "polkadot-runtime-parachains/runtime-benchmarks", ] From cd0195814511a4ecf644f6d3333593648926b328 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 28 Sep 2023 16:00:37 +0200 Subject: [PATCH 20/56] Update cumulus/pallets/parachain-system/src/lib.rs Co-authored-by: Liam Aharon --- cumulus/pallets/parachain-system/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 89064b63f067..8f851685bbe2 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -1001,7 +1001,7 @@ impl Pallet { crate::unincluded_segment::size_after_included(included_hash, &segment) } - /// Raise the delivery fee factor by a multiplicative factor and stores the resulting value. + /// Raises the delivery fee factor by a multiplicative factor and stores the resulting value. /// /// Returns the new delivery fee factor after the increment. pub(crate) fn increment_ump_fee_factor(message_size_factor: FixedU128) -> FixedU128 { From 429810464bb7881e6029752a48353cc589995af1 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 28 Sep 2023 16:00:49 +0200 Subject: [PATCH 21/56] Update cumulus/pallets/parachain-system/src/lib.rs Co-authored-by: Liam Aharon --- cumulus/pallets/parachain-system/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 8f851685bbe2..6480a495bddd 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -1011,7 +1011,7 @@ impl Pallet { }) } - /// Reduce the delivery fee factor by a multiplicative factor and stores the resulting value. + /// Reduces the delivery fee factor by a multiplicative factor and stores the resulting value. /// /// Does not reduce the fee factor below the initial value, which is currently set as 1. /// From 376cf3fc9716704d69b80c04b86b6f6362961ed7 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 28 Sep 2023 16:01:54 +0200 Subject: [PATCH 22/56] Update cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs Co-authored-by: Liam Aharon --- cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs b/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs index 872c0dbeb7f7..e8e525535f64 100644 --- a/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs +++ b/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs @@ -177,7 +177,7 @@ pub fn teleports_for_native_asset_works< target_account_balance_before_teleport - existential_deposit ); - // Make sure account can pay delivery fees + // Mint funds into account to ensure it has enough balance to pay delivery fees let delivery_fees = xcm_helpers::transfer_assets_delivery_fees::< XcmConfig::XcmSender, >( From f7ad1df19ff4b58b59698f4e43f4b450fa0e6b0c Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Fri, 29 Sep 2023 13:38:00 -0300 Subject: [PATCH 23/56] Change FeeTracker Id to associated type --- cumulus/pallets/parachain-system/src/lib.rs | 4 +++- cumulus/pallets/xcmp-queue/src/lib.rs | 6 ++++-- polkadot/runtime/parachains/src/dmp.rs | 6 ++++-- polkadot/runtime/parachains/src/lib.rs | 6 ++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 6480a495bddd..7b97bdc78482 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -1026,7 +1026,9 @@ impl Pallet { } impl FeeTracker for Pallet { - fn get_fee_factor(_: ()) -> FixedU128 { + type Id = (); + + fn get_fee_factor(_: Self::Id) -> FixedU128 { UpwardDeliveryFeeFactor::::get() } } diff --git a/cumulus/pallets/xcmp-queue/src/lib.rs b/cumulus/pallets/xcmp-queue/src/lib.rs index f9d109e30651..3affc5407112 100644 --- a/cumulus/pallets/xcmp-queue/src/lib.rs +++ b/cumulus/pallets/xcmp-queue/src/lib.rs @@ -1230,8 +1230,10 @@ impl SendXcm for Pallet { } } -impl FeeTracker for Pallet { - fn get_fee_factor(id: ParaId) -> FixedU128 { +impl FeeTracker for Pallet { + type Id = ParaId; + + fn get_fee_factor(id: Self::Id) -> FixedU128 { >::get(id) } } diff --git a/polkadot/runtime/parachains/src/dmp.rs b/polkadot/runtime/parachains/src/dmp.rs index cd85d11901e0..b84a9003f729 100644 --- a/polkadot/runtime/parachains/src/dmp.rs +++ b/polkadot/runtime/parachains/src/dmp.rs @@ -361,8 +361,10 @@ impl Pallet { } } -impl FeeTracker for Pallet { - fn get_fee_factor(id: ParaId) -> FixedU128 { +impl FeeTracker for Pallet { + type Id = ParaId; + + fn get_fee_factor(id: Self::Id) -> FixedU128 { DeliveryFeeFactor::::get(id) } } diff --git a/polkadot/runtime/parachains/src/lib.rs b/polkadot/runtime/parachains/src/lib.rs index eddc6a45e850..73e62020c921 100644 --- a/polkadot/runtime/parachains/src/lib.rs +++ b/polkadot/runtime/parachains/src/lib.rs @@ -58,9 +58,11 @@ use primitives::{HeadData, Id as ParaId, ValidationCode}; use sp_runtime::{DispatchResult, FixedU128}; /// Trait for tracking message delivery fees on a transport protocol. -pub trait FeeTracker { +pub trait FeeTracker { + /// Type used for assigning different fee factors to different destinations + type Id; /// The evolving exponential fee factor which will be used to calculate the delivery fees. - fn get_fee_factor(id: Id) -> FixedU128; + fn get_fee_factor(id: Self::Id) -> FixedU128; } /// Schedule a para to be initialized at the start of the next session with the given genesis data. From e86b2ba4ffe233d6a2c95b90974fa7a9316bdddf Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Fri, 29 Sep 2023 13:38:09 -0300 Subject: [PATCH 24/56] Unify parachain and parent delivery pricing traits --- Cargo.lock | 2 + cumulus/pallets/xcmp-queue/src/lib.rs | 6 +- .../assets/asset-hub-kusama/src/lib.rs | 10 +- .../assets/asset-hub-polkadot/src/lib.rs | 9 +- .../assets/asset-hub-westend/src/lib.rs | 10 +- .../bridge-hubs/bridge-hub-kusama/src/lib.rs | 14 ++- .../bridge-hub-polkadot/src/lib.rs | 14 ++- .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 15 ++- .../runtimes/testing/penpal/src/lib.rs | 4 +- .../testing/rococo-parachain/Cargo.toml | 3 + .../testing/rococo-parachain/src/lib.rs | 4 +- cumulus/primitives/utility/Cargo.toml | 4 +- cumulus/primitives/utility/src/lib.rs | 103 ++++++++++++------ polkadot/runtime/common/src/xcm_sender.rs | 61 +++++++---- .../src/fungible/benchmarking.rs | 18 ++- 15 files changed, 194 insertions(+), 83 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3607854dfc02..677d70c5cbe7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3751,6 +3751,7 @@ dependencies = [ "cumulus-primitives-core", "frame-support", "log", + "pallet-xcm-benchmarks", "parity-scale-codec", "polkadot-runtime-common", "polkadot-runtime-parachains", @@ -14104,6 +14105,7 @@ dependencies = [ "parachains-common", "parity-scale-codec", "polkadot-parachain-primitives", + "polkadot-runtime-common", "scale-info", "sp-api", "sp-block-builder", diff --git a/cumulus/pallets/xcmp-queue/src/lib.rs b/cumulus/pallets/xcmp-queue/src/lib.rs index 3affc5407112..2ca0effde64a 100644 --- a/cumulus/pallets/xcmp-queue/src/lib.rs +++ b/cumulus/pallets/xcmp-queue/src/lib.rs @@ -47,7 +47,7 @@ use frame_support::{ traits::{EnsureOrigin, Get}, weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight}, }; -use polkadot_runtime_common::xcm_sender::PriceForParachainDelivery; +use polkadot_runtime_common::xcm_sender::PriceForMessageDelivery; use polkadot_runtime_parachains::FeeTracker; use rand_chacha::{ rand_core::{RngCore, SeedableRng}, @@ -110,7 +110,7 @@ pub mod pallet { type ControllerOriginConverter: ConvertOrigin; /// The price for delivering an XCM to a sibling parachain destination. - type PriceForSiblingDelivery: PriceForParachainDelivery; + type PriceForSiblingDelivery: PriceForMessageDelivery; /// The weight information of this pallet. type WeightInfo: WeightInfo; @@ -1203,7 +1203,7 @@ impl SendXcm for Pallet { MultiLocation { parents: 1, interior: X1(Parachain(id)) } => { let xcm = msg.take().ok_or(SendError::MissingArgument)?; let id = ParaId::from(*id); - let price = T::PriceForSiblingDelivery::price_for_parachain_delivery(id, &xcm); + let price = T::PriceForSiblingDelivery::price_for_delivery(id, &xcm); let versioned_xcm = T::VersionWrapper::wrap_version(&d, xcm) .map_err(|()| SendError::DestinationUnsupported)?; Ok(((id, versioned_xcm), price)) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs index 0cbd4c51aee5..e4daa053ba36 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs @@ -79,7 +79,7 @@ use sp_runtime::RuntimeDebug; use xcm::opaque::v3::MultiLocation; use xcm_config::{ FellowshipLocation, ForeignAssetsConvertedConcreteId, GovernanceLocation, KsmLocation, - PoolAssetsConvertedConcreteId, TrustBackedAssetsConvertedConcreteId, XcmConfig, + PoolAssetsConvertedConcreteId, TrustBackedAssetsConvertedConcreteId, XcmConfig, PriceForParentDelivery, }; #[cfg(any(feature = "std", test))] @@ -1224,25 +1224,21 @@ impl_runtime_apis! { use xcm::latest::prelude::*; use xcm_config::{KsmLocation, MaxAssetsIntoHolding}; use pallet_xcm_benchmarks::asset_instance_from; - use cumulus_primitives_core::ParaId; parameter_types! { pub ExistentialDepositMultiAsset: Option = Some(( xcm_config::KsmLocation::get(), ExistentialDeposit::get() ).into()); - pub ToParachain: ParaId = kusama_runtime_constants::system_parachain::BRIDGE_HUB_ID.into(); } impl pallet_xcm_benchmarks::Config for Runtime { type XcmConfig = xcm_config::XcmConfig; type AccountIdConverter = xcm_config::LocationToAccountId; - type DeliveryHelper = polkadot_runtime_common::xcm_sender::ToParachainDeliveryHelper< + type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< XcmConfig, ExistentialDepositMultiAsset, - PriceForSiblingParachainDelivery, - ToParachain, - (), + PriceForParentDelivery, >; fn valid_destination() -> Result { Ok(KsmLocation::get()) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs index d0ad8562817d..1fab9a07edda 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs @@ -110,6 +110,7 @@ use sp_runtime::RuntimeDebug; use xcm_config::{ DotLocation, FellowshipLocation, ForeignAssetsConvertedConcreteId, GovernanceLocation, TrustBackedAssetsConvertedConcreteId, XcmConfig, XcmOriginToTransactDispatchOrigin, + PriceForParentDelivery, }; #[cfg(any(feature = "std", test))] @@ -1100,25 +1101,21 @@ impl_runtime_apis! { use xcm::latest::prelude::*; use xcm_config::{DotLocation, MaxAssetsIntoHolding}; use pallet_xcm_benchmarks::asset_instance_from; - use cumulus_primitives_core::ParaId; parameter_types! { pub ExistentialDepositMultiAsset: Option = Some(( xcm_config::DotLocation::get(), ExistentialDeposit::get() ).into()); - pub ToParachain: ParaId = polkadot_runtime_constants::system_parachain::BRIDGE_HUB_ID.into(); } impl pallet_xcm_benchmarks::Config for Runtime { type XcmConfig = xcm_config::XcmConfig; type AccountIdConverter = xcm_config::LocationToAccountId; - type DeliveryHelper = polkadot_runtime_common::xcm_sender::ToParachainDeliveryHelper< + type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< XcmConfig, ExistentialDepositMultiAsset, - PriceForSiblingParachainDelivery, - ToParachain, - (), + PriceForParentDelivery, >; fn valid_destination() -> Result { Ok(DotLocation::get()) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index 30bff588452d..b36408e941dd 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -78,7 +78,7 @@ use xcm::opaque::v3::MultiLocation; use xcm_config::{ ForeignAssetsConvertedConcreteId, PoolAssetsConvertedConcreteId, TrustBackedAssetsConvertedConcreteId, WestendLocation, XcmConfig, - XcmOriginToTransactDispatchOrigin, + XcmOriginToTransactDispatchOrigin, PriceForParentDelivery, }; #[cfg(any(feature = "std", test))] @@ -1238,25 +1238,21 @@ impl_runtime_apis! { use xcm::latest::prelude::*; use xcm_config::{MaxAssetsIntoHolding, WestendLocation}; use pallet_xcm_benchmarks::asset_instance_from; - use cumulus_primitives_core::ParaId; parameter_types! { pub ExistentialDepositMultiAsset: Option = Some(( xcm_config::WestendLocation::get(), ExistentialDeposit::get() ).into()); - pub ToParachain: ParaId = westend_runtime_constants::system_parachain::BRIDGE_HUB_ID.into(); } impl pallet_xcm_benchmarks::Config for Runtime { type XcmConfig = xcm_config::XcmConfig; type AccountIdConverter = xcm_config::LocationToAccountId; - type DeliveryHelper = polkadot_runtime_common::xcm_sender::ToParachainDeliveryHelper< + type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< XcmConfig, ExistentialDepositMultiAsset, - PriceForSiblingParachainDelivery, - ToParachain, - (), + PriceForParentDelivery, >; fn valid_destination() -> Result { Ok(WestendLocation::get()) diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs index 7947194f780a..9198861efdc3 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs @@ -58,6 +58,7 @@ pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; pub use sp_runtime::{MultiAddress, Perbill, Permill}; use xcm_config::{ FellowshipLocation, GovernanceLocation, XcmConfig, XcmOriginToTransactDispatchOrigin, + PriceForParentDelivery, }; #[cfg(any(feature = "std", test))] @@ -685,10 +686,21 @@ impl_runtime_apis! { use xcm::latest::prelude::*; use xcm_config::KsmRelayLocation; + parameter_types! { + pub ExistentialDepositMultiAsset: Option = Some(( + xcm_config::KsmRelayLocation::get(), + ExistentialDeposit::get() + ).into()); + } + impl pallet_xcm_benchmarks::Config for Runtime { type XcmConfig = xcm_config::XcmConfig; type AccountIdConverter = xcm_config::LocationToAccountId; - type DeliveryHelper = (); // No requirements for UMP + type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< + XcmConfig, + ExistentialDepositMultiAsset, + PriceForParentDelivery, + >; fn valid_destination() -> Result { Ok(KsmRelayLocation::get()) } diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs index 06328ed55fc2..a50a54890234 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs @@ -58,6 +58,7 @@ pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; pub use sp_runtime::{MultiAddress, Perbill, Permill}; use xcm_config::{ FellowshipLocation, GovernanceLocation, XcmConfig, XcmOriginToTransactDispatchOrigin, + PriceForParentDelivery, }; #[cfg(any(feature = "std", test))] @@ -685,10 +686,21 @@ impl_runtime_apis! { use xcm::latest::prelude::*; use xcm_config::DotRelayLocation; + parameter_types! { + pub ExistentialDepositMultiAsset: Option = Some(( + xcm_config::DotRelayLocation::get(), + ExistentialDeposit::get() + ).into()); + } + impl pallet_xcm_benchmarks::Config for Runtime { type XcmConfig = xcm_config::XcmConfig; type AccountIdConverter = xcm_config::LocationToAccountId; - type DeliveryHelper = (); // No requirements for UMP + type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< + XcmConfig, + ExistentialDepositMultiAsset, + PriceForParentDelivery, + >; fn valid_destination() -> Result { Ok(DotRelayLocation::get()) } 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 226725208c1c..f1b189c04825 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 @@ -57,7 +57,7 @@ use frame_system::{ }; pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; pub use sp_runtime::{MultiAddress, Perbill, Permill}; -use xcm_config::{XcmConfig, XcmOriginToTransactDispatchOrigin}; +use xcm_config::{XcmConfig, XcmOriginToTransactDispatchOrigin, PriceForParentDelivery}; use bp_parachains::SingleParaStoredHeaderDataBuilder; use bp_runtime::HeaderId; @@ -966,10 +966,21 @@ impl_runtime_apis! { use xcm::latest::prelude::*; use xcm_config::RelayLocation; + parameter_types! { + pub ExistentialDepositMultiAsset: Option = Some(( + xcm_config::RelayLocation::get(), + ExistentialDeposit::get() + ).into()); + } + impl pallet_xcm_benchmarks::Config for Runtime { type XcmConfig = xcm_config::XcmConfig; type AccountIdConverter = xcm_config::LocationToAccountId; - type DeliveryHelper = (); // No requirements for UMP + type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< + XcmConfig, + ExistentialDepositMultiAsset, + PriceForParentDelivery, + >; fn valid_destination() -> Result { Ok(RelayLocation::get()) } diff --git a/cumulus/parachains/runtimes/testing/penpal/src/lib.rs b/cumulus/parachains/runtimes/testing/penpal/src/lib.rs index fe0f19c30632..9d7b5c4411c1 100644 --- a/cumulus/parachains/runtimes/testing/penpal/src/lib.rs +++ b/cumulus/parachains/runtimes/testing/penpal/src/lib.rs @@ -33,6 +33,7 @@ mod weights; pub mod xcm_config; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use cumulus_primitives_core::ParaId; use frame_support::{ construct_runtime, dispatch::DispatchClass, @@ -66,6 +67,7 @@ use sp_std::prelude::*; use sp_version::NativeVersion; use sp_version::RuntimeVersion; use xcm_config::{AssetsToBlockAuthor, XcmConfig, XcmOriginToTransactDispatchOrigin}; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; #[cfg(any(feature = "std", test))] pub use sp_runtime::BuildStorage; @@ -488,7 +490,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { type ControllerOrigin = EnsureRoot; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; type WeightInfo = (); - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = NoPriceForMessageDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { diff --git a/cumulus/parachains/runtimes/testing/rococo-parachain/Cargo.toml b/cumulus/parachains/runtimes/testing/rococo-parachain/Cargo.toml index 029d5d10f986..28b92ccf99b6 100644 --- a/cumulus/parachains/runtimes/testing/rococo-parachain/Cargo.toml +++ b/cumulus/parachains/runtimes/testing/rococo-parachain/Cargo.toml @@ -42,6 +42,7 @@ polkadot-parachain-primitives = { path = "../../../../../polkadot/parachain", de xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false} xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false} xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false} +polkadot-runtime-common = { path = "../../../../../polkadot/runtime/common", default-features = false } # Cumulus cumulus-pallet-aura-ext = { path = "../../../../pallets/aura-ext", default-features = false } @@ -103,6 +104,7 @@ std = [ "xcm-builder/std", "xcm-executor/std", "xcm/std", + "polkadot-runtime-common/std", ] runtime-benchmarks = [ "cumulus-pallet-parachain-system/runtime-benchmarks", @@ -121,6 +123,7 @@ runtime-benchmarks = [ "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", + "polkadot-runtime-common/runtime-benchmarks", ] experimental = [ "pallet-aura/experimental" ] diff --git a/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs b/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs index 50c5a445c25f..7c82787e0d05 100644 --- a/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs +++ b/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs @@ -35,6 +35,8 @@ use sp_std::prelude::*; #[cfg(feature = "std")] use sp_version::NativeVersion; use sp_version::RuntimeVersion; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; +use cumulus_primitives_core::ParaId; // A few exports that help ease life for downstream crates. pub use frame_support::{ @@ -511,7 +513,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { type ControllerOrigin = EnsureRoot; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; type WeightInfo = cumulus_pallet_xcmp_queue::weights::SubstrateWeight; - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = NoPriceForMessageDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { diff --git a/cumulus/primitives/utility/Cargo.toml b/cumulus/primitives/utility/Cargo.toml index 7ecd7a1788a4..406483682703 100644 --- a/cumulus/primitives/utility/Cargo.toml +++ b/cumulus/primitives/utility/Cargo.toml @@ -20,7 +20,7 @@ polkadot-runtime-parachains = { path = "../../../polkadot/runtime/parachains", d xcm = { package = "staging-xcm", path = "../../../polkadot/xcm", default-features = false } xcm-executor = { package = "staging-xcm-executor", path = "../../../polkadot/xcm/xcm-executor", default-features = false} xcm-builder = { package = "staging-xcm-builder", path = "../../../polkadot/xcm/xcm-builder", default-features = false} - +pallet-xcm-benchmarks = { path = "../../../polkadot/xcm/pallet-xcm-benchmarks", default-features = false } # Cumulus cumulus-primitives-core = { path = "../core", default-features = false } @@ -39,6 +39,7 @@ std = [ "xcm-builder/std", "xcm-executor/std", "xcm/std", + "pallet-xcm-benchmarks/std", ] runtime-benchmarks = [ @@ -48,4 +49,5 @@ runtime-benchmarks = [ "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", + "pallet-xcm-benchmarks/runtime-benchmarks", ] diff --git a/cumulus/primitives/utility/src/lib.rs b/cumulus/primitives/utility/src/lib.rs index 5818cb6c84b5..f545eb134ac1 100644 --- a/cumulus/primitives/utility/src/lib.rs +++ b/cumulus/primitives/utility/src/lib.rs @@ -28,41 +28,13 @@ use frame_support::{ }, weights::Weight, }; -use polkadot_runtime_common::xcm_sender::{ConstantPrice, ExponentialPrice}; -use polkadot_runtime_parachains::FeeTracker; -use sp_runtime::{traits::Saturating, FixedPointNumber, SaturatedConversion}; +use polkadot_runtime_common::xcm_sender::PriceForMessageDelivery; +use sp_runtime::{traits::Saturating, SaturatedConversion}; use sp_std::{marker::PhantomData, prelude::*}; use xcm::{latest::prelude::*, WrapVersion}; use xcm_builder::TakeRevenue; use xcm_executor::traits::{MatchesFungibles, TransactAsset, WeightTrader}; -pub trait PriceForParentDelivery { - fn price_for_parent_delivery(message: &Xcm<()>) -> MultiAssets; -} - -impl PriceForParentDelivery for () { - fn price_for_parent_delivery(_: &Xcm<()>) -> MultiAssets { - MultiAssets::new() - } -} - -impl> PriceForParentDelivery for ConstantPrice { - fn price_for_parent_delivery(_: &Xcm<()>) -> MultiAssets { - T::get() - } -} - -impl, B: Get, M: Get, F: FeeTracker> PriceForParentDelivery - for ExponentialPrice -{ - fn price_for_parent_delivery(message: &Xcm<()>) -> MultiAssets { - let message_fee = (message.encoded_size() as u128).saturating_mul(M::get()); - let fee_sum = B::get().saturating_add(message_fee); - let amount = F::get_fee_factor(()).saturating_mul_int(fee_sum); - (A::get(), amount).into() - } -} - /// Xcm router which recognises the `Parent` destination and handles it by sending the message into /// the given UMP `UpwardMessageSender` implementation. Thus this essentially adapts an /// `UpwardMessageSender` trait impl into a `SendXcm` trait impl. @@ -75,7 +47,7 @@ impl SendXcm for ParentAsUmp where T: UpwardMessageSender, W: WrapVersion, - P: PriceForParentDelivery, + P: PriceForMessageDelivery, { type Ticket = Vec; @@ -88,7 +60,7 @@ where if d.contains_parents_only(1) { // An upward message for the relay chain. let xcm = msg.take().ok_or(SendError::MissingArgument)?; - let price = P::price_for_parent_delivery(&xcm); + let price = P::price_for_delivery((), &xcm); let versioned_xcm = W::wrap_version(&d, xcm).map_err(|()| SendError::DestinationUnsupported)?; let data = versioned_xcm.encode(); @@ -552,3 +524,70 @@ mod tests { assert_eq!(trader.buy_weight(weight_to_buy, payment, &ctx), Err(XcmError::NotWithdrawable)); } } + +/// Implementation of `pallet_xcm_benchmarks::EnsureDelivery` which helps to ensure delivery to the +/// parent relay chain. Deposits existential deposit for origin (if needed). +/// Deposits estimated fee to the origin account (if needed). +/// Allows to trigger additional logic for specific `ParaId` (e.g. open HRMP channel) (if neeeded). +#[cfg(feature = "runtime-benchmarks")] +pub struct ToParentDeliveryHelper< + XcmConfig, + ExistentialDeposit, + PriceForDelivery, +>( + sp_std::marker::PhantomData<( + XcmConfig, + ExistentialDeposit, + PriceForDelivery, + )>, +); + +#[cfg(feature = "runtime-benchmarks")] +impl< + XcmConfig: xcm_executor::Config, + ExistentialDeposit: Get>, + PriceForDelivery: PriceForMessageDelivery, + > pallet_xcm_benchmarks::EnsureDelivery + for ToParentDeliveryHelper< + XcmConfig, + ExistentialDeposit, + PriceForDelivery, + > +{ + fn ensure_successful_delivery( + origin_ref: &MultiLocation, + _dest: &MultiLocation, + fee_reason: xcm_executor::traits::FeeReason, + ) -> (Option, Option) { + use xcm_executor::{ + traits::FeeManager, + FeesMode, + }; + + let mut fees_mode = None; + if !XcmConfig::FeeManager::is_waived(Some(origin_ref), fee_reason) { + // if not waived, we need to set up accounts for paying and receiving fees + + // mint ED to origin if needed + if let Some(ed) = ExistentialDeposit::get() { + XcmConfig::AssetTransactor::deposit_asset(&ed, &origin_ref, None).unwrap(); + } + + // overestimate delivery fee + let overestimated_xcm = vec![ClearOrigin; 128].into(); + let overestimated_fees = PriceForDelivery::price_for_delivery( + (), + &overestimated_xcm, + ); + + // mint overestimated fee to origin + for fee in overestimated_fees.inner() { + XcmConfig::AssetTransactor::deposit_asset(&fee, &origin_ref, None).unwrap(); + } + + // expected worst case - direct withdraw + fees_mode = Some(FeesMode { jit_withdraw: true }); + } + (fees_mode, None) + } +} diff --git a/polkadot/runtime/common/src/xcm_sender.rs b/polkadot/runtime/common/src/xcm_sender.rs index ef7adc1c9501..207ef3432a2c 100644 --- a/polkadot/runtime/common/src/xcm_sender.rs +++ b/polkadot/runtime/common/src/xcm_sender.rs @@ -31,25 +31,40 @@ use SendError::*; /// Simple value-bearing trait for determining/expressing the assets required to be paid for a /// messages to be delivered to a parachain. -pub trait PriceForParachainDelivery { +pub trait PriceForMessageDelivery { + /// Type used for charging different prices to different destinations + type Id; /// Return the assets required to deliver `message` to the given `para` destination. - fn price_for_parachain_delivery(para: ParaId, message: &Xcm<()>) -> MultiAssets; + fn price_for_delivery(id: Self::Id, message: &Xcm<()>) -> MultiAssets; } -impl PriceForParachainDelivery for () { - fn price_for_parachain_delivery(_: ParaId, _: &Xcm<()>) -> MultiAssets { +impl PriceForMessageDelivery for () { + type Id = (); + + fn price_for_delivery(_: Self::Id, _: &Xcm<()>) -> MultiAssets { + MultiAssets::new() + } +} + +pub struct NoPriceForMessageDelivery(PhantomData); +impl PriceForMessageDelivery for NoPriceForMessageDelivery { + type Id = Id; + + fn price_for_delivery(_: Self::Id, _: &Xcm<()>) -> MultiAssets { MultiAssets::new() } } /// Implementation of [`PriceForParachainDelivery`] which returns a fixed price. pub struct ConstantPrice(sp_std::marker::PhantomData); -impl> PriceForParachainDelivery for ConstantPrice { - fn price_for_parachain_delivery(_: ParaId, _: &Xcm<()>) -> MultiAssets { +impl> PriceForMessageDelivery for ConstantPrice { + type Id = (); + + fn price_for_delivery(_: Self::Id, _: &Xcm<()>) -> MultiAssets { T::get() } } -/// Implementation of [`PriceForParachainDelivery`] which returns an exponentially increasing price. +/// Implementation of [`PriceForMessageDelivery`] which returns an exponentially increasing price. /// The formula for the fee is based on the sum of a base fee plus a message length fee, multiplied /// by a specified factor. In mathematical form: /// @@ -64,13 +79,15 @@ impl> PriceForParachainDelivery for ConstantPrice { /// - `M`: The fee to pay for each and every byte of the message after encoding it. /// - `F`: A fee factor multiplier. It can be understood as the exponent term in the formula. pub struct ExponentialPrice(sp_std::marker::PhantomData<(A, B, M, F)>); -impl, B: Get, M: Get, F: FeeTracker> PriceForParachainDelivery +impl, B: Get, M: Get, F: FeeTracker> PriceForMessageDelivery for ExponentialPrice { - fn price_for_parachain_delivery(para: ParaId, msg: &Xcm<()>) -> MultiAssets { + type Id = F::Id; + + fn price_for_delivery(id: Self::Id, msg: &Xcm<()>) -> MultiAssets { let msg_fee = (msg.encoded_size() as u128).saturating_mul(M::get()); let fee_sum = B::get().saturating_add(msg_fee); - let amount = F::get_fee_factor(para).saturating_mul_int(fee_sum); + let amount = F::get_fee_factor(id).saturating_mul_int(fee_sum); (A::get(), amount).into() } } @@ -78,8 +95,10 @@ impl, B: Get, M: Get, F: FeeTracker> PriceFo /// XCM sender for relay chain. It only sends downward message. pub struct ChildParachainRouter(PhantomData<(T, W, P)>); -impl +impl SendXcm for ChildParachainRouter + where + P: PriceForMessageDelivery { type Ticket = (HostConfiguration>, ParaId, Vec); @@ -99,7 +118,7 @@ impl>::config(); let para = id.into(); - let price = P::price_for_parachain_delivery(para, &xcm); + let price = P::price_for_delivery(para, &xcm); let blob = W::wrap_version(&d, xcm).map_err(|()| DestinationUnsupported)?.encode(); >::can_queue_downward_message(&config, ¶, &blob) .map_err(Into::::into)?; @@ -142,7 +161,7 @@ pub struct ToParachainDeliveryHelper< impl< XcmConfig: xcm_executor::Config, ExistentialDeposit: Get>, - PriceForDelivery: PriceForParachainDelivery, + PriceForDelivery: PriceForMessageDelivery, Parachain: Get, ToParachainHelper: EnsureForParachain, > pallet_xcm_benchmarks::EnsureDelivery @@ -175,7 +194,7 @@ impl< // overestimate delivery fee let overestimated_xcm = vec![ClearOrigin; 128].into(); - let overestimated_fees = PriceForDelivery::price_for_parachain_delivery( + let overestimated_fees = PriceForDelivery::price_for_delivery( Parachain::get(), &overestimated_xcm, ); @@ -202,7 +221,7 @@ pub trait EnsureForParachain { } #[cfg(feature = "runtime-benchmarks")] impl EnsureForParachain for () { - fn ensure(_para_id: ParaId) { + fn ensure(_: ParaId) { // doing nothing } } @@ -221,8 +240,10 @@ mod tests { } struct TestFeeTracker; - impl FeeTracker for TestFeeTracker { - fn get_fee_factor(_: ParaId) -> FixedU128 { + impl FeeTracker for TestFeeTracker { + type Id = ParaId; + + fn get_fee_factor(_: Self::Id) -> FixedU128 { FixedU128::from_rational(101, 100) } } @@ -240,21 +261,21 @@ mod tests { // message_length = 1 let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + m); assert_eq!( - TestExponentialPrice::price_for_parachain_delivery(id, &Xcm(vec![])), + TestExponentialPrice::price_for_delivery(id, &Xcm(vec![])), (FeeAssetId::get(), result).into() ); // message size = 2 let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (2 * m)); assert_eq!( - TestExponentialPrice::price_for_parachain_delivery(id, &Xcm(vec![ClearOrigin])), + TestExponentialPrice::price_for_delivery(id, &Xcm(vec![ClearOrigin])), (FeeAssetId::get(), result).into() ); // message size = 4 let result: u128 = TestFeeTracker::get_fee_factor(id).saturating_mul_int(b + (4 * m)); assert_eq!( - TestExponentialPrice::price_for_parachain_delivery( + TestExponentialPrice::price_for_delivery( id, &Xcm(vec![SetAppendix(Xcm(vec![ClearOrigin]))]) ), diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs index 89c6e5e4325c..556e634e6687 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs @@ -141,16 +141,32 @@ benchmarks_instance_pallet! { } initiate_reserve_withdraw { + let (sender_account, sender_location) = account_and_location::(1); + let sender_account_balance_before = T::TransactAsset::balance(&sender_account); let holding = T::worst_case_holding(1); let assets_filter = MultiAssetFilter::Definite(holding.clone()); let reserve = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; - let mut executor = new_executor::(Default::default()); + use crate::EnsureDelivery; + let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( + &sender_location, + &reserve, + FeeReason::InitiateReserveWithdraw + ); + let mut executor = new_executor::(sender_location); executor.set_holding(holding.into()); + if let Some(expected_fees_mode) = expected_fees_mode { + executor.set_fees_mode(expected_fees_mode); + } + if let Some(expected_assets_in_holding) = expected_assets_in_holding { + executor.set_holding(expected_assets_in_holding.into()); + } let instruction = Instruction::InitiateReserveWithdraw { assets: assets_filter, reserve, xcm: Xcm(vec![]) }; let xcm = Xcm(vec![instruction]); }: { executor.bench_process(xcm)?; } verify { + // Check we charged the delivery fees + assert!(T::TransactAsset::balance(&sender_account) <= sender_account_balance_before); // The execute completing successfully is as good as we can check. // TODO: Potentially add new trait to XcmSender to detect a queued outgoing message. #4426 } From 5b49f9dda809fec0849acf2c228ec1c5f49e3041 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Sat, 30 Sep 2023 03:21:28 -0300 Subject: [PATCH 25/56] Fix CI --- .../parachains/runtimes/assets/asset-hub-kusama/src/lib.rs | 4 ++-- .../parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs | 3 +-- .../parachains/runtimes/assets/asset-hub-westend/src/lib.rs | 4 ++-- .../runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs | 3 +-- .../runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs | 3 +-- .../runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs | 4 ++-- 6 files changed, 9 insertions(+), 12 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs index e4daa053ba36..ae36572e97e8 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs @@ -79,7 +79,7 @@ use sp_runtime::RuntimeDebug; use xcm::opaque::v3::MultiLocation; use xcm_config::{ FellowshipLocation, ForeignAssetsConvertedConcreteId, GovernanceLocation, KsmLocation, - PoolAssetsConvertedConcreteId, TrustBackedAssetsConvertedConcreteId, XcmConfig, PriceForParentDelivery, + PoolAssetsConvertedConcreteId, TrustBackedAssetsConvertedConcreteId, XcmConfig, }; #[cfg(any(feature = "std", test))] @@ -1238,7 +1238,7 @@ impl_runtime_apis! { type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< XcmConfig, ExistentialDepositMultiAsset, - PriceForParentDelivery, + xcm_config::PriceForParentDelivery, >; fn valid_destination() -> Result { Ok(KsmLocation::get()) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs index 1fab9a07edda..c4f1cf47495c 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs @@ -110,7 +110,6 @@ use sp_runtime::RuntimeDebug; use xcm_config::{ DotLocation, FellowshipLocation, ForeignAssetsConvertedConcreteId, GovernanceLocation, TrustBackedAssetsConvertedConcreteId, XcmConfig, XcmOriginToTransactDispatchOrigin, - PriceForParentDelivery, }; #[cfg(any(feature = "std", test))] @@ -1115,7 +1114,7 @@ impl_runtime_apis! { type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< XcmConfig, ExistentialDepositMultiAsset, - PriceForParentDelivery, + xcm_config::PriceForParentDelivery, >; fn valid_destination() -> Result { Ok(DotLocation::get()) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index b36408e941dd..9acc22972f0f 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -78,7 +78,7 @@ use xcm::opaque::v3::MultiLocation; use xcm_config::{ ForeignAssetsConvertedConcreteId, PoolAssetsConvertedConcreteId, TrustBackedAssetsConvertedConcreteId, WestendLocation, XcmConfig, - XcmOriginToTransactDispatchOrigin, PriceForParentDelivery, + XcmOriginToTransactDispatchOrigin, }; #[cfg(any(feature = "std", test))] @@ -1252,7 +1252,7 @@ impl_runtime_apis! { type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< XcmConfig, ExistentialDepositMultiAsset, - PriceForParentDelivery, + xcm_config::PriceForParentDelivery, >; fn valid_destination() -> Result { Ok(WestendLocation::get()) diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs index 9198861efdc3..8a7228b18856 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs @@ -58,7 +58,6 @@ pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; pub use sp_runtime::{MultiAddress, Perbill, Permill}; use xcm_config::{ FellowshipLocation, GovernanceLocation, XcmConfig, XcmOriginToTransactDispatchOrigin, - PriceForParentDelivery, }; #[cfg(any(feature = "std", test))] @@ -699,7 +698,7 @@ impl_runtime_apis! { type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< XcmConfig, ExistentialDepositMultiAsset, - PriceForParentDelivery, + xcm_config::PriceForParentDelivery, >; fn valid_destination() -> Result { Ok(KsmRelayLocation::get()) diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs index a50a54890234..c660a68b7469 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs @@ -58,7 +58,6 @@ pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; pub use sp_runtime::{MultiAddress, Perbill, Permill}; use xcm_config::{ FellowshipLocation, GovernanceLocation, XcmConfig, XcmOriginToTransactDispatchOrigin, - PriceForParentDelivery, }; #[cfg(any(feature = "std", test))] @@ -699,7 +698,7 @@ impl_runtime_apis! { type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< XcmConfig, ExistentialDepositMultiAsset, - PriceForParentDelivery, + xcm_config::PriceForParentDelivery, >; fn valid_destination() -> Result { Ok(DotRelayLocation::get()) 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 f1b189c04825..7f73f0ae09a1 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 @@ -57,7 +57,7 @@ use frame_system::{ }; pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; pub use sp_runtime::{MultiAddress, Perbill, Permill}; -use xcm_config::{XcmConfig, XcmOriginToTransactDispatchOrigin, PriceForParentDelivery}; +use xcm_config::{XcmConfig, XcmOriginToTransactDispatchOrigin}; use bp_parachains::SingleParaStoredHeaderDataBuilder; use bp_runtime::HeaderId; @@ -979,7 +979,7 @@ impl_runtime_apis! { type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< XcmConfig, ExistentialDepositMultiAsset, - PriceForParentDelivery, + xcm_config::PriceForParentDelivery, >; fn valid_destination() -> Result { Ok(RelayLocation::get()) From d15d78c889390c4eff1a89067820210ab2dca66d Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Mon, 2 Oct 2023 11:44:47 +0200 Subject: [PATCH 26/56] Update cumulus/pallets/parachain-system/src/lib.rs Co-authored-by: Liam Aharon --- cumulus/pallets/parachain-system/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 7b97bdc78482..c9b21b6194ef 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -181,7 +181,7 @@ where mod ump_constants { use super::FixedU128; - /// `max_upward_queue_size / THRESHOLD_FACTOR` is the threshold after which delivery + /// `host_config.max_upward_queue_size / THRESHOLD_FACTOR` is the threshold after which delivery /// starts getting exponentially more expensive. /// The price starts to increase when queue is half full pub const THRESHOLD_FACTOR: u32 = 2; From 2c5c41947ba890b83d2bfd718373aaedcbe4f5a7 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Mon, 2 Oct 2023 08:40:56 -0300 Subject: [PATCH 27/56] Merge branch 'kckyeung/xcm-fees-manager' into cis-ump-pricing --- cumulus/pallets/xcmp-queue/src/lib.rs | 28 +-- .../assets/asset-hub-kusama/src/xcm_config.rs | 2 +- .../asset-hub-polkadot/src/xcm_config.rs | 2 +- .../bridge-hub-kusama/src/xcm_config.rs | 2 +- .../bridge-hub-polkadot/src/xcm_config.rs | 2 +- .../collectives-polkadot/src/xcm_config.rs | 2 +- polkadot/runtime/parachains/src/dmp.rs | 23 +- polkadot/runtime/parachains/src/dmp/tests.rs | 2 +- polkadot/runtime/parachains/src/lib.rs | 3 +- polkadot/runtime/rococo/src/xcm_config.rs | 4 +- polkadot/runtime/westend/src/lib.rs | 1 + polkadot/runtime/westend/src/xcm_config.rs | 4 +- .../xcm/xcm-builder/src/tests/pay/mock.rs | 12 +- substrate/client/service/src/error.rs | 4 +- substrate/frame/alliance/src/mock.rs | 25 +- substrate/frame/bags-list/src/mock.rs | 26 +- substrate/frame/bags-list/src/tests.rs | 6 +- substrate/frame/balances/src/lib.rs | 19 +- .../test-staking-e2e/src/mock.rs | 12 +- .../frame/examples/kitchensink/src/tests.rs | 8 - substrate/frame/examples/split/src/mock.rs | 8 - substrate/frame/fast-unstake/src/mock.rs | 28 +-- substrate/frame/multisig/src/tests.rs | 16 +- substrate/frame/nomination-pools/src/lib.rs | 155 +++++++++--- .../frame/nomination-pools/src/migration.rs | 87 +++++++ substrate/frame/nomination-pools/src/mock.rs | 48 +++- substrate/frame/nomination-pools/src/tests.rs | 225 +++++++++++++++--- substrate/frame/proxy/src/tests.rs | 11 - substrate/frame/staking/src/lib.rs | 10 +- substrate/frame/staking/src/mock.rs | 1 + substrate/frame/support/procedural/src/lib.rs | 8 +- .../support/procedural/src/no_bound/clone.rs | 12 +- .../support/procedural/src/no_bound/debug.rs | 4 +- .../procedural/src/no_bound/default.rs | 10 +- .../procedural/src/no_bound/partial_eq.rs | 2 +- substrate/frame/support/src/tests/mod.rs | 14 ++ substrate/primitives/staking/src/lib.rs | 2 + 37 files changed, 545 insertions(+), 283 deletions(-) diff --git a/cumulus/pallets/xcmp-queue/src/lib.rs b/cumulus/pallets/xcmp-queue/src/lib.rs index 2ca0effde64a..a139564a93e0 100644 --- a/cumulus/pallets/xcmp-queue/src/lib.rs +++ b/cumulus/pallets/xcmp-queue/src/lib.rs @@ -669,7 +669,7 @@ impl Pallet { messages_processed: &mut u8, max_weight: Weight, max_individual_weight: Weight, - should_decrement_fee_factor: bool, + should_decrease_fee_factor: bool, ) -> (Weight, bool) { let data = >::get(sender, sent_at); let mut last_remaining_fragments; @@ -735,8 +735,8 @@ impl Pallet { remaining_fragments = &b""[..]; } - if should_decrement_fee_factor { - Self::decrement_fee_factor(sender); + if should_decrease_fee_factor { + Self::decrease_fee_factor(sender); } } }, @@ -892,7 +892,7 @@ impl Pallet { } else { // Process up to one block's worth for now. let weight_remaining = weight_available.saturating_sub(weight_used); - let should_decrement_fee_factor = + let should_decrease_fee_factor = (status[index].message_metadata.len() as u32) <= suspend_threshold; let (weight_processed, is_empty) = Self::process_xcmp_message( sender, @@ -900,7 +900,7 @@ impl Pallet { &mut messages_processed, weight_remaining, xcmp_max_individual_weight, - should_decrement_fee_factor, + should_decrease_fee_factor, ); if is_empty { status[index].message_metadata.remove(0); @@ -971,22 +971,22 @@ impl Pallet { }); } - /// Raise the delivery fee factor by a multiplicative factor and stores the resulting value. + /// Increases the delivery fee factor by a multiplicative factor and stores the resulting value. /// - /// Returns the new delivery fee factor after the increment. - pub(crate) fn increment_fee_factor(para: ParaId, message_size_factor: FixedU128) -> FixedU128 { + /// Returns the new delivery fee factor after the increase. + pub(crate) fn increase_fee_factor(para: ParaId, message_size_factor: FixedU128) -> FixedU128 { >::mutate(para, |f| { - *f = f.saturating_mul(EXPONENTIAL_FEE_BASE + message_size_factor); + *f = f.saturating_mul(EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor)); *f }) } - /// Reduce the delivery fee factor by a multiplicative factor and stores the resulting value. + /// Decreases the delivery fee factor by a multiplicative factor and stores the resulting value. /// /// Does not reduce the fee factor below the initial value, which is currently set as 1. /// - /// Returns the new delivery fee factor after the decrement. - pub(crate) fn decrement_fee_factor(para: ParaId) -> FixedU128 { + /// Returns the new delivery fee factor after the decrease. + pub(crate) fn decrease_fee_factor(para: ParaId) -> FixedU128 { >::mutate(para, |f| { *f = InitialFactor::get().max(*f / EXPONENTIAL_FEE_BASE); *f @@ -1050,9 +1050,9 @@ impl XcmpMessageHandler for Pallet { // Update the delivery fee factor, if applicable. if count > suspend_threshold { let message_size_factor = - FixedU128::from_u32(data_ref.len().saturating_div(1024) as u32) + FixedU128::from((data_ref.len() / 1024) as u128) .saturating_mul(MESSAGE_SIZE_FEE_BASE); - Self::increment_fee_factor(sender, message_size_factor); + Self::increase_fee_factor(sender, message_size_factor); } }, Err(_) => status.push(InboundChannelDetails { diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs index 2ecb25a13135..1a4f8a7c6995 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs @@ -30,7 +30,6 @@ use frame_support::{ traits::{ConstU32, Contains, Everything, Nothing, PalletInfoAccess}, }; use frame_system::EnsureRoot; -use rococo_runtime_constants::system_parachain::SystemParachains; use pallet_xcm::XcmPassthrough; use parachains_common::{ impls::ToStakingPot, @@ -39,6 +38,7 @@ use parachains_common::{ }; use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_common::xcm_sender::ExponentialPrice; +use rococo_runtime_constants::system_parachain::SystemParachains; use sp_runtime::traits::{AccountIdConversion, ConvertInto}; use xcm::latest::prelude::*; use xcm_builder::{ diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs index 911647b9a8ef..67fde497005f 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs @@ -34,8 +34,8 @@ use parachains_common::{ }; use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; -use westend_runtime_constants::system_parachain::SystemParachains; use sp_runtime::traits::{AccountIdConversion, ConvertInto}; +use westend_runtime_constants::system_parachain::SystemParachains; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs index d3bc9cc8d559..5fd91b60e84c 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs @@ -24,7 +24,6 @@ use frame_support::{ traits::{ConstU32, Contains, Everything, Nothing}, }; use frame_system::EnsureRoot; -use rococo_runtime_constants::system_parachain::SystemParachains; use pallet_xcm::XcmPassthrough; use parachains_common::{ impls::ToStakingPot, @@ -33,6 +32,7 @@ use parachains_common::{ }; use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; +use rococo_runtime_constants::system_parachain::SystemParachains; use sp_runtime::traits::AccountIdConversion; use xcm::latest::prelude::*; use xcm_builder::{ diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs index fb0a88a7661a..c6bc60bd2e10 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs @@ -32,8 +32,8 @@ use parachains_common::{ }; use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; -use westend_runtime_constants::system_parachain::SystemParachains; use sp_runtime::traits::AccountIdConversion; +use westend_runtime_constants::system_parachain::SystemParachains; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, diff --git a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs index 4d699e928f2f..b5b411a9b975 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs @@ -32,10 +32,10 @@ use parachains_common::{ }; use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; +use sp_runtime::traits::AccountIdConversion; use westend_runtime_constants::{ system_parachain::SystemParachains, xcm::body::FELLOWSHIP_ADMIN_INDEX, }; -use sp_runtime::traits::AccountIdConversion; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, diff --git a/polkadot/runtime/parachains/src/dmp.rs b/polkadot/runtime/parachains/src/dmp.rs index b84a9003f729..d6981866bd01 100644 --- a/polkadot/runtime/parachains/src/dmp.rs +++ b/polkadot/runtime/parachains/src/dmp.rs @@ -243,10 +243,9 @@ impl Pallet { let threshold = Self::dmq_max_length(config.max_downward_message_size).saturating_div(THRESHOLD_FACTOR); if q_len > (threshold as usize) { - let message_size_factor = - FixedU128::from_u32(serialized_len.saturating_div(1024) as u32) - .saturating_mul(MESSAGE_SIZE_FEE_BASE); - Self::increment_fee_factor(para, message_size_factor); + let message_size_factor = FixedU128::from((serialized_len / 1024) as u128) + .saturating_mul(MESSAGE_SIZE_FEE_BASE); + Self::increase_fee_factor(para, message_size_factor); } Ok(()) @@ -304,7 +303,7 @@ impl Pallet { let threshold = Self::dmq_max_length(config.max_downward_message_size).saturating_div(THRESHOLD_FACTOR); if q_len <= (threshold as usize) { - Self::decrement_fee_factor(para); + Self::decrease_fee_factor(para); } T::DbWeight::get().reads_writes(1, 1) } @@ -338,22 +337,22 @@ impl Pallet { DownwardMessageQueues::::get(&recipient) } - /// Raise the delivery fee factor by a multiplicative factor and stores the resulting value. + /// Increases the delivery fee factor by a multiplicative factor and stores the resulting value. /// - /// Returns the new delivery fee factor after the increment. - pub(crate) fn increment_fee_factor(para: ParaId, message_size_factor: FixedU128) -> FixedU128 { + /// Returns the new delivery fee factor after the increase. + pub(crate) fn increase_fee_factor(para: ParaId, message_size_factor: FixedU128) -> FixedU128 { >::mutate(para, |f| { - *f = f.saturating_mul(EXPONENTIAL_FEE_BASE + message_size_factor); + *f = f.saturating_mul(EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor)); *f }) } - /// Reduce the delivery fee factor by a multiplicative factor and stores the resulting value. + /// Decreases the delivery fee factor by a multiplicative factor and stores the resulting value. /// /// Does not reduce the fee factor below the initial value, which is currently set as 1. /// - /// Returns the new delivery fee factor after the decrement. - pub(crate) fn decrement_fee_factor(para: ParaId) -> FixedU128 { + /// Returns the new delivery fee factor after the decrease. + pub(crate) fn decrease_fee_factor(para: ParaId) -> FixedU128 { >::mutate(para, |f| { *f = InitialFactor::get().max(*f / EXPONENTIAL_FEE_BASE); *f diff --git a/polkadot/runtime/parachains/src/dmp/tests.rs b/polkadot/runtime/parachains/src/dmp/tests.rs index a65984840da5..f9197b156a1e 100644 --- a/polkadot/runtime/parachains/src/dmp/tests.rs +++ b/polkadot/runtime/parachains/src/dmp/tests.rs @@ -233,7 +233,7 @@ fn verify_dmq_mqc_head_is_externally_accessible() { } #[test] -fn verify_fee_increment_and_decrement() { +fn verify_fee_increase_and_decrease() { let a = ParaId::from(123); let mut genesis = default_genesis_config(); genesis.configuration.config.max_downward_message_size = 16777216; diff --git a/polkadot/runtime/parachains/src/lib.rs b/polkadot/runtime/parachains/src/lib.rs index 73e62020c921..9483623bd33e 100644 --- a/polkadot/runtime/parachains/src/lib.rs +++ b/polkadot/runtime/parachains/src/lib.rs @@ -61,7 +61,8 @@ use sp_runtime::{DispatchResult, FixedU128}; pub trait FeeTracker { /// Type used for assigning different fee factors to different destinations type Id; - /// The evolving exponential fee factor which will be used to calculate the delivery fees. + /// Returns the evolving exponential fee factor which will be used to calculate the delivery + /// fees. fn get_fee_factor(id: Self::Id) -> FixedU128; } diff --git a/polkadot/runtime/rococo/src/xcm_config.rs b/polkadot/runtime/rococo/src/xcm_config.rs index 25e8066f1caf..408f9e1260b7 100644 --- a/polkadot/runtime/rococo/src/xcm_config.rs +++ b/polkadot/runtime/rococo/src/xcm_config.rs @@ -106,10 +106,10 @@ pub type PriceForChildParachainDelivery = /// The XCM router. When we want to send an XCM message, we use this type. It amalgamates all of our /// individual routers. -pub type XcmRouter = WithUniqueTopic<( +pub type XcmRouter = WithUniqueTopic< // Only one router so far - use DMP to communicate with child parachains. ChildParachainRouter, -)>; +>; parameter_types! { pub const Roc: MultiAssetFilter = Wild(AllOf { fun: WildFungible, id: Concrete(TokenLocation::get()) }); diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 61930122dae5..730fadc98601 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1508,6 +1508,7 @@ pub mod migrations { paras_registrar::migration::VersionCheckedMigrateToV1, pallet_nomination_pools::migration::versioned_migrations::V5toV6, pallet_referenda::migration::v1::MigrateV0ToV1, + pallet_nomination_pools::migration::versioned_migrations::V6ToV7, ); } diff --git a/polkadot/runtime/westend/src/xcm_config.rs b/polkadot/runtime/westend/src/xcm_config.rs index a85cf43c6ff9..4a1f54e3db65 100644 --- a/polkadot/runtime/westend/src/xcm_config.rs +++ b/polkadot/runtime/westend/src/xcm_config.rs @@ -100,10 +100,10 @@ pub type PriceForChildParachainDelivery = /// The XCM router. When we want to send an XCM message, we use this type. It amalgamates all of our /// individual routers. -pub type XcmRouter = WithUniqueTopic<( +pub type XcmRouter = WithUniqueTopic< // Only one router so far - use DMP to communicate with child parachains. ChildParachainRouter, -)>; +>; parameter_types! { pub const AssetHub: MultiLocation = Parachain(ASSET_HUB_ID).into_location(); diff --git a/polkadot/xcm/xcm-builder/src/tests/pay/mock.rs b/polkadot/xcm/xcm-builder/src/tests/pay/mock.rs index c663b0a4d76f..5b6fa3ee5a0b 100644 --- a/polkadot/xcm/xcm-builder/src/tests/pay/mock.rs +++ b/polkadot/xcm/xcm-builder/src/tests/pay/mock.rs @@ -49,22 +49,12 @@ construct_runtime!( } ); -parameter_types! { - pub const BlockHashCount: BlockNumber = 250; -} - #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { type Block = Block; - type BlockHashCount = BlockHashCount; - type BaseCallFilter = frame_support::traits::Everything; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type RuntimeEvent = RuntimeEvent; - type PalletInfo = PalletInfo; - type OnSetCode = (); type AccountData = pallet_balances::AccountData; type AccountId = AccountId; + type BlockHashCount = ConstU32<256>; type Lookup = sp_runtime::traits::IdentityLookup; } diff --git a/substrate/client/service/src/error.rs b/substrate/client/service/src/error.rs index c871342c771e..c0a2adf2d198 100644 --- a/substrate/client/service/src/error.rs +++ b/substrate/client/service/src/error.rs @@ -54,10 +54,10 @@ pub enum Error { #[error("Tasks executor hasn't been provided.")] TaskExecutorRequired, - #[error("Prometheus metrics error")] + #[error("Prometheus metrics error: {0}")] Prometheus(#[from] prometheus_endpoint::PrometheusError), - #[error("Application")] + #[error("Application: {0}")] Application(#[from] Box), #[error("Other: {0}")] diff --git a/substrate/frame/alliance/src/mock.rs b/substrate/frame/alliance/src/mock.rs index f04e7e414ed9..82dbbe9c0bb9 100644 --- a/substrate/frame/alliance/src/mock.rs +++ b/substrate/frame/alliance/src/mock.rs @@ -26,7 +26,7 @@ pub use sp_runtime::{ use sp_std::convert::{TryFrom, TryInto}; pub use frame_support::{ - assert_noop, assert_ok, ord_parameter_types, parameter_types, + assert_noop, assert_ok, derive_impl, ord_parameter_types, parameter_types, traits::{EitherOfDiverse, SortedMembers}, BoundedVec, }; @@ -45,30 +45,11 @@ parameter_types! { pub BlockWeights: frame_system::limits::BlockWeights = frame_system::limits::BlockWeights::simple_max(Weight::MAX); } + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = BlockWeights; - type BlockLength = (); - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type Nonce = u64; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = AccountId; - type Lookup = IdentityLookup; type Block = Block; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type DbWeight = (); - type Version = (); - type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = (); - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; } parameter_types! { diff --git a/substrate/frame/bags-list/src/mock.rs b/substrate/frame/bags-list/src/mock.rs index ae50adabd508..9946a2198ac4 100644 --- a/substrate/frame/bags-list/src/mock.rs +++ b/substrate/frame/bags-list/src/mock.rs @@ -20,11 +20,11 @@ use super::*; use crate::{self as bags_list}; use frame_election_provider_support::VoteWeight; -use frame_support::parameter_types; +use frame_support::{derive_impl, parameter_types}; use sp_runtime::BuildStorage; use std::collections::HashMap; -pub type AccountId = u32; +pub type AccountId = ::AccountId; pub type Balance = u32; parameter_types! { @@ -48,30 +48,10 @@ impl frame_election_provider_support::ScoreProvider for StakingMock { } } +#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Runtime { - type SS58Prefix = (); - type BaseCallFilter = frame_support::traits::Everything; - type RuntimeOrigin = RuntimeOrigin; - type Nonce = u64; - type RuntimeCall = RuntimeCall; - type Hash = sp_core::H256; - type Hashing = sp_runtime::traits::BlakeTwo256; - type AccountId = AccountId; - type Lookup = sp_runtime::traits::IdentityLookup; type Block = Block; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = (); - type DbWeight = (); - type BlockLength = (); - type BlockWeights = (); - type Version = (); - type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; } parameter_types! { diff --git a/substrate/frame/bags-list/src/tests.rs b/substrate/frame/bags-list/src/tests.rs index 9e8508698d8e..0b382a4fcefa 100644 --- a/substrate/frame/bags-list/src/tests.rs +++ b/substrate/frame/bags-list/src/tests.rs @@ -163,7 +163,7 @@ mod pallet { assert_eq!(Bag::::get(10).unwrap(), Bag::new(Some(1), Some(3), 10)); assert_eq!(Bag::::get(1_000).unwrap(), Bag::new(Some(2), Some(2), 1_000)); - assert_eq!(get_list_as_ids(), vec![2u32, 1, 4, 3]); + assert_eq!(get_list_as_ids(), vec![2u64, 1, 4, 3]); // when StakingMock::set_score_of(&2, 10); @@ -272,10 +272,10 @@ mod pallet { // given assert_eq!(List::::get_bags(), vec![(20, vec![10, 11, 12])]); // 11 now has more weight than 10 and can be moved before it. - StakingMock::set_score_of(&11u32, 17); + StakingMock::set_score_of(&11u64, 17); // when - assert_ok!(BagsList::put_in_front_of_other(RuntimeOrigin::signed(42), 11u32, 10)); + assert_ok!(BagsList::put_in_front_of_other(RuntimeOrigin::signed(42), 11u64, 10)); // then assert_eq!(List::::get_bags(), vec![(20, vec![11, 10, 12])]); diff --git a/substrate/frame/balances/src/lib.rs b/substrate/frame/balances/src/lib.rs index 5da6600d8796..a2cacc45369a 100644 --- a/substrate/frame/balances/src/lib.rs +++ b/substrate/frame/balances/src/lib.rs @@ -216,7 +216,7 @@ pub mod pallet { /// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`]. pub mod config_preludes { use super::*; - use frame_support::derive_impl; + use frame_support::{derive_impl, traits::ConstU64}; pub struct TestDefaultConfig; @@ -227,12 +227,17 @@ pub mod pallet { impl DefaultConfig for TestDefaultConfig { #[inject_runtime_type] type RuntimeEvent = (); + #[inject_runtime_type] + type RuntimeHoldReason = (); type Balance = u64; + type ExistentialDeposit = ConstU64<1>; type ReserveIdentifier = (); type FreezeIdentifier = (); + type DustRemoval = (); + type MaxLocks = (); type MaxReserves = (); type MaxFreezes = (); @@ -249,6 +254,10 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; + /// The overarching hold reason. + #[pallet::no_default_bounds] + type RuntimeHoldReason: Parameter + Member + MaxEncodedLen + Ord + Copy; + /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; @@ -266,7 +275,7 @@ pub mod pallet { + FixedPointOperand; /// Handler for the unbalanced reduction when removing a dust account. - #[pallet::no_default] + #[pallet::no_default_bounds] type DustRemoval: OnUnbalanced>; /// The minimum amount required to keep an account open. MUST BE GREATER THAN ZERO! @@ -278,7 +287,7 @@ pub mod pallet { /// /// Bottom line: Do yourself a favour and make it at least one! #[pallet::constant] - #[pallet::no_default] + #[pallet::no_default_bounds] type ExistentialDeposit: Get; /// The means of storing the balances of an account. @@ -290,10 +299,6 @@ pub mod pallet { /// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/` type ReserveIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy; - /// The overarching hold reason. - #[pallet::no_default] - type RuntimeHoldReason: Parameter + Member + MaxEncodedLen + Ord + Copy; - /// The ID type for freezes. type FreezeIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy; diff --git a/substrate/frame/election-provider-multi-phase/test-staking-e2e/src/mock.rs b/substrate/frame/election-provider-multi-phase/test-staking-e2e/src/mock.rs index ec646c311978..2e3cb15f9a43 100644 --- a/substrate/frame/election-provider-multi-phase/test-staking-e2e/src/mock.rs +++ b/substrate/frame/election-provider-multi-phase/test-staking-e2e/src/mock.rs @@ -65,8 +65,7 @@ type Block = frame_system::mocking::MockBlockU32; type Extrinsic = testing::TestXt; frame_support::construct_runtime!( - pub enum Runtime - { + pub enum Runtime { System: frame_system, ElectionProviderMultiPhase: pallet_election_provider_multi_phase, Staking: pallet_staking, @@ -89,15 +88,8 @@ pub(crate) type Moment = u32; #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Runtime { type Block = Block; - type BlockHashCount = ConstU32<10>; - type BaseCallFilter = frame_support::traits::Everything; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type RuntimeEvent = RuntimeEvent; - type PalletInfo = PalletInfo; - type OnSetCode = (); - type AccountData = pallet_balances::AccountData; + type BlockHashCount = ConstU32<10>; } const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); diff --git a/substrate/frame/examples/kitchensink/src/tests.rs b/substrate/frame/examples/kitchensink/src/tests.rs index b2af7c8983f5..abded83e4820 100644 --- a/substrate/frame/examples/kitchensink/src/tests.rs +++ b/substrate/frame/examples/kitchensink/src/tests.rs @@ -39,15 +39,7 @@ frame_support::construct_runtime!( /// details. #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; type Block = Block; - type BlockHashCount = ConstU64<10>; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type RuntimeEvent = RuntimeEvent; - type PalletInfo = PalletInfo; - type OnSetCode = (); - type AccountData = pallet_balances::AccountData; } diff --git a/substrate/frame/examples/split/src/mock.rs b/substrate/frame/examples/split/src/mock.rs index bee3633ef68f..caab4f1ae902 100644 --- a/substrate/frame/examples/split/src/mock.rs +++ b/substrate/frame/examples/split/src/mock.rs @@ -17,7 +17,6 @@ use crate as pallet_template; use frame_support::{derive_impl, sp_runtime::BuildStorage}; -use sp_core::ConstU64; type Block = frame_system::mocking::MockBlock; @@ -35,13 +34,6 @@ frame_support::construct_runtime!( #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { type Block = Block; - type BlockHashCount = ConstU64<10>; - type BaseCallFilter = frame_support::traits::Everything; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type RuntimeEvent = RuntimeEvent; - type PalletInfo = PalletInfo; - type OnSetCode = (); } impl pallet_template::Config for Test { diff --git a/substrate/frame/fast-unstake/src/mock.rs b/substrate/frame/fast-unstake/src/mock.rs index dc24a823c0db..cf274c784f9f 100644 --- a/substrate/frame/fast-unstake/src/mock.rs +++ b/substrate/frame/fast-unstake/src/mock.rs @@ -17,7 +17,7 @@ use crate::{self as fast_unstake}; use frame_support::{ - assert_ok, + assert_ok, derive_impl, pallet_prelude::*, parameter_types, traits::{ConstU64, Currency}, @@ -32,7 +32,6 @@ use pallet_staking::{Exposure, IndividualExposure, StakerStatus}; use sp_std::prelude::*; pub type AccountId = u128; -pub type Nonce = u32; pub type BlockNumber = u64; pub type Balance = u128; pub type T = Runtime; @@ -44,30 +43,13 @@ parameter_types! { ); } +#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Runtime { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = BlockWeights; - type BlockLength = (); - type DbWeight = (); - type RuntimeOrigin = RuntimeOrigin; - type Nonce = Nonce; - type RuntimeCall = RuntimeCall; - type Hash = sp_core::H256; - type Hashing = sp_runtime::traits::BlakeTwo256; - type AccountId = AccountId; - type Lookup = IdentityLookup; type Block = Block; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = (); - type Version = (); - type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = (); - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; + // we use U128 account id in order to get a better iteration order out of a map. + type AccountId = AccountId; + type Lookup = IdentityLookup; } impl pallet_timestamp::Config for Runtime { diff --git a/substrate/frame/multisig/src/tests.rs b/substrate/frame/multisig/src/tests.rs index e7fc5b3e4aae..179827255291 100644 --- a/substrate/frame/multisig/src/tests.rs +++ b/substrate/frame/multisig/src/tests.rs @@ -31,8 +31,7 @@ use sp_runtime::{BuildStorage, TokenError}; type Block = frame_system::mocking::MockBlockU32; frame_support::construct_runtime!( - pub enum Test - { + pub enum Test { System: frame_system::{Pallet, Call, Config, Storage, Event}, Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, Multisig: pallet_multisig::{Pallet, Call, Storage, Event}, @@ -43,24 +42,15 @@ frame_support::construct_runtime!( impl frame_system::Config for Test { type Block = Block; type BlockHashCount = ConstU32<250>; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type RuntimeEvent = RuntimeEvent; - type BaseCallFilter = TestBaseCallFilter; - type PalletInfo = PalletInfo; - type OnSetCode = (); - type AccountData = pallet_balances::AccountData; + // This pallet wishes to overwrite this. + type BaseCallFilter = TestBaseCallFilter; } #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)] impl pallet_balances::Config for Test { - type RuntimeEvent = RuntimeEvent; - type RuntimeHoldReason = (); type ReserveIdentifier = [u8; 8]; - type DustRemoval = (); type AccountStore = System; - type ExistentialDeposit = ConstU64<1>; } pub struct TestBaseCallFilter; diff --git a/substrate/frame/nomination-pools/src/lib.rs b/substrate/frame/nomination-pools/src/lib.rs index 2ec9b537d320..909a930e3821 100644 --- a/substrate/frame/nomination-pools/src/lib.rs +++ b/substrate/frame/nomination-pools/src/lib.rs @@ -538,6 +538,31 @@ impl PoolMember { } } + /// Total balance of the member, both active and unbonding. + /// Doesn't mutate state. + #[cfg(any(feature = "try-runtime", feature = "fuzzing", test, debug_assertions))] + fn total_balance(&self) -> BalanceOf { + let pool = BondedPool::::get(self.pool_id).unwrap(); + let active_balance = pool.points_to_balance(self.active_points()); + + let sub_pools = match SubPoolsStorage::::get(self.pool_id) { + Some(sub_pools) => sub_pools, + None => return active_balance, + }; + + let unbonding_balance = self.unbonding_eras.iter().fold( + BalanceOf::::zero(), + |accumulator, (era, unlocked_points)| { + // if the `SubPools::with_era` has already been merged into the + // `SubPools::no_era` use this pool instead. + let era_pool = sub_pools.with_era.get(era).unwrap_or(&sub_pools.no_era); + accumulator + (era_pool.point_to_balance(*unlocked_points)) + }, + ); + + active_balance + unbonding_balance + } + /// Total points of this member, both active and unbonding. fn total_points(&self) -> BalanceOf { self.active_points().saturating_add(self.unbonding_points()) @@ -1189,11 +1214,11 @@ impl BondedPool { Ok(()) } - /// Bond exactly `amount` from `who`'s funds into this pool. + /// Bond exactly `amount` from `who`'s funds into this pool. Increases the [`TotalValueLocked`] + /// by `amount`. /// - /// If the bond type is `Create`, `Staking::bond` is called, and `who` - /// is allowed to be killed. Otherwise, `Staking::bond_extra` is called and `who` - /// cannot be killed. + /// If the bond is [`BondType::Create`], [`Staking::bond`] is called, and `who` is allowed to be + /// killed. Otherwise, [`Staking::bond_extra`] is called and `who` cannot be killed. /// /// Returns `Ok(points_issues)`, `Err` otherwise. fn try_bond_funds( @@ -1224,6 +1249,9 @@ impl BondedPool { // found, we exit early. BondType::Later => T::Staking::bond_extra(&bonded_account, amount)?, } + TotalValueLocked::::mutate(|tvl| { + tvl.saturating_accrue(amount); + }); Ok(points_issued) } @@ -1239,6 +1267,27 @@ impl BondedPool { }); }; } + + /// Withdraw all the funds that are already unlocked from staking for the + /// [`BondedPool::bonded_account`]. + /// + /// Also reduces the [`TotalValueLocked`] by the difference of the + /// [`T::Staking::total_stake`] of the [`BondedPool::bonded_account`] that might occur by + /// [`T::Staking::withdraw_unbonded`]. + /// + /// Returns the result of [`T::Staking::withdraw_unbonded`] + fn withdraw_from_staking(&self, num_slashing_spans: u32) -> Result { + let bonded_account = self.bonded_account(); + + let prev_total = T::Staking::total_stake(&bonded_account.clone()).unwrap_or_default(); + let outcome = T::Staking::withdraw_unbonded(bonded_account.clone(), num_slashing_spans); + let diff = prev_total + .defensive_saturating_sub(T::Staking::total_stake(&bonded_account).unwrap_or_default()); + TotalValueLocked::::mutate(|tvl| { + tvl.saturating_reduce(diff); + }); + outcome + } } /// A reward pool. @@ -1437,9 +1486,7 @@ impl UnbondPool { } /// Dissolve some points from the unbonding pool, reducing the balance of the pool - /// proportionally. - /// - /// This is the opposite of `issue`. + /// proportionally. This is the opposite of `issue`. /// /// Returns the actual amount of `Balance` that was removed from the pool. fn dissolve(&mut self, points: BalanceOf) -> BalanceOf { @@ -1525,7 +1572,7 @@ pub mod pallet { use sp_runtime::Perbill; /// The current storage version. - const STORAGE_VERSION: StorageVersion = StorageVersion::new(6); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(7); #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] @@ -1602,6 +1649,14 @@ pub mod pallet { type MaxUnbonding: Get; } + /// The sum of funds across all pools. + /// + /// This might be lower but never higher than the sum of `total_balance` of all [`PoolMembers`] + /// because calling `pool_withdraw_unbonded` might decrease the total stake of the pool's + /// `bonded_account` without adjusting the pallet-internal `UnbondingPool`'s. + #[pallet::storage] + pub type TotalValueLocked = StorageValue<_, BalanceOf, ValueQuery>; + /// Minimum amount to bond to join a pool. #[pallet::storage] pub type MinJoinBond = StorageValue<_, BalanceOf, ValueQuery>; @@ -1825,9 +1880,9 @@ pub mod pallet { CannotWithdrawAny, /// The amount does not meet the minimum bond to either join or create a pool. /// - /// The depositor can never unbond to a value less than - /// `Pallet::depositor_min_bond`. The caller does not have nominating - /// permissions for the pool. Members can never unbond to a value below `MinJoinBond`. + /// The depositor can never unbond to a value less than `Pallet::depositor_min_bond`. The + /// caller does not have nominating permissions for the pool. Members can never unbond to a + /// value below `MinJoinBond`. MinimumBondNotMet, /// The transaction could not be executed due to overflow risk for the pool. OverflowRisk, @@ -2114,7 +2169,7 @@ pub mod pallet { /// Call `withdraw_unbonded` for the pools account. This call can be made by any account. /// - /// This is useful if their are too many unlocking chunks to call `unbond`, and some + /// This is useful if there are too many unlocking chunks to call `unbond`, and some /// can be cleared by withdrawing. In the case there are too many unlocking chunks, the user /// would probably see an error like `NoMoreChunks` emitted from the staking system when /// they attempt to unbond. @@ -2127,10 +2182,12 @@ pub mod pallet { ) -> DispatchResult { let _ = ensure_signed(origin)?; let pool = BondedPool::::get(pool_id).ok_or(Error::::PoolNotFound)?; + // For now we only allow a pool to withdraw unbonded if its not destroying. If the pool // is destroying then `withdraw_unbonded` can be used. ensure!(pool.state != PoolState::Destroying, Error::::NotDestroying); - T::Staking::withdraw_unbonded(pool.bonded_account(), num_slashing_spans)?; + pool.withdraw_from_staking(num_slashing_spans)?; + Ok(()) } @@ -2180,9 +2237,8 @@ pub mod pallet { ensure!(!withdrawn_points.is_empty(), Error::::CannotWithdrawAny); // Before calculating the `balance_to_unbond`, we call withdraw unbonded to ensure the - // `transferable_balance` is correct. - let stash_killed = - T::Staking::withdraw_unbonded(bonded_pool.bonded_account(), num_slashing_spans)?; + // `transferrable_balance` is correct. + let stash_killed = bonded_pool.withdraw_from_staking(num_slashing_spans)?; // defensive-only: the depositor puts enough funds into the stash so that it will only // be destroyed when they are leaving. @@ -2846,12 +2902,9 @@ impl Pallet { }, (false, false) => { // Equivalent to (current_points / current_balance) * new_funds - balance( - u256(current_points) - .saturating_mul(u256(new_funds)) - // We check for zero above - .div(u256(current_balance)), - ) + balance(u256(current_points).saturating_mul(u256(new_funds))) + // We check for zero above + .div(current_balance) }, } } @@ -2871,9 +2924,12 @@ impl Pallet { } // Equivalent of (current_balance / current_points) * points - balance(u256(current_balance).saturating_mul(u256(points))) - // We check for zero above - .div(current_points) + balance( + u256(current_balance) + .saturating_mul(u256(points)) + // We check for zero above + .div(u256(current_points)), + ) } /// If the member has some rewards, transfer a payout from the reward pool to the member. @@ -3242,6 +3298,7 @@ impl Pallet { let mut pools_members = BTreeMap::::new(); let mut pools_members_pending_rewards = BTreeMap::>::new(); let mut all_members = 0u32; + let mut total_balance_members = Default::default(); PoolMembers::::iter().try_for_each(|(_, d)| -> Result<(), TryRuntimeError> { let bonded_pool = BondedPools::::get(d.pool_id).unwrap(); ensure!(!d.total_points().is_zero(), "No member should have zero points"); @@ -3257,6 +3314,7 @@ impl Pallet { let pending_rewards = d.pending_rewards(current_rc).unwrap(); *pools_members_pending_rewards.entry(d.pool_id).or_default() += pending_rewards; } // else this pool has been heavily slashed and cannot have any rewards anymore. + total_balance_members += d.total_balance(); Ok(()) })?; @@ -3280,6 +3338,7 @@ impl Pallet { Ok(()) })?; + let mut expected_tvl: BalanceOf = Default::default(); BondedPools::::iter().try_for_each(|(id, inner)| -> Result<(), TryRuntimeError> { let bonded_pool = BondedPool { id, inner }; ensure!( @@ -3300,13 +3359,28 @@ impl Pallet { "depositor must always have MinCreateBond stake in the pool, except for when the \ pool is being destroyed and the depositor is the last member", ); + + expected_tvl += + T::Staking::total_stake(&bonded_pool.bonded_account()).unwrap_or_default(); + Ok(()) })?; + ensure!( MaxPoolMembers::::get().map_or(true, |max| all_members <= max), Error::::MaxPoolMembers ); + ensure!( + TotalValueLocked::::get() == expected_tvl, + "TVL deviates from the actual sum of funds of all Pools." + ); + + ensure!( + TotalValueLocked::::get() <= total_balance_members, + "TVL must be equal to or less than the total balance of all PoolMembers." + ); + if level <= 1 { return Ok(()) } @@ -3424,20 +3498,30 @@ impl Pallet { } impl sp_staking::OnStakingUpdate> for Pallet { + /// Reduces the balances of the [`SubPools`], that belong to the pool involved in the + /// slash, to the amount that is defined in the `slashed_unlocking` field of + /// [`sp_staking::OnStakingUpdate::on_slash`] + /// + /// Emits the `PoolsSlashed` event. fn on_slash( pool_account: &T::AccountId, // Bonded balance is always read directly from staking, therefore we don't need to update // anything here. slashed_bonded: BalanceOf, slashed_unlocking: &BTreeMap>, + total_slashed: BalanceOf, ) { - if let Some(pool_id) = ReversePoolIdLookup::::get(pool_account) { - let mut sub_pools = match SubPoolsStorage::::get(pool_id).defensive() { - Some(sub_pools) => sub_pools, - None => return, - }; - for (era, slashed_balance) in slashed_unlocking.iter() { - if let Some(pool) = sub_pools.with_era.get_mut(era) { + let Some(pool_id) = ReversePoolIdLookup::::get(pool_account) else { return }; + // As the slashed account belongs to a `BondedPool` the `TotalValueLocked` decreases and + // an event is emitted. + TotalValueLocked::::mutate(|tvl| { + tvl.defensive_saturating_reduce(total_slashed); + }); + + if let Some(mut sub_pools) = SubPoolsStorage::::get(pool_id) { + // set the reduced balance for each of the `SubPools` + slashed_unlocking.iter().for_each(|(era, slashed_balance)| { + if let Some(pool) = sub_pools.with_era.get_mut(era).defensive() { pool.balance = *slashed_balance; Self::deposit_event(Event::::UnbondingPoolSlashed { era: *era, @@ -3445,10 +3529,11 @@ impl sp_staking::OnStakingUpdate> for Pall balance: *slashed_balance, }); } - } - - Self::deposit_event(Event::::PoolSlashed { pool_id, balance: slashed_bonded }); + }); SubPoolsStorage::::insert(pool_id, sub_pools); + } else if !slashed_unlocking.is_empty() { + defensive!("Expected SubPools were not found"); } + Self::deposit_event(Event::::PoolSlashed { pool_id, balance: slashed_bonded }); } } diff --git a/substrate/frame/nomination-pools/src/migration.rs b/substrate/frame/nomination-pools/src/migration.rs index 606123daa9c6..eef2a976f1a2 100644 --- a/substrate/frame/nomination-pools/src/migration.rs +++ b/substrate/frame/nomination-pools/src/migration.rs @@ -27,6 +27,16 @@ use sp_runtime::TryRuntimeError; pub mod versioned_migrations { use super::*; + /// Migration V6 to V7 wrapped in a [`frame_support::migrations::VersionedMigration`], ensuring + /// the migration is only performed when on-chain version is 6. + pub type V6ToV7 = frame_support::migrations::VersionedMigration< + 6, + 7, + v7::VersionUncheckedMigrateV6ToV7, + crate::pallet::Pallet, + ::DbWeight, + >; + /// Wrapper over `MigrateToV6` with convenience version checks. pub type V5toV6 = frame_support::migrations::VersionedMigration< 5, @@ -37,6 +47,83 @@ pub mod versioned_migrations { >; } +/// This migration accumulates and initializes the [`TotalValueLocked`] for all pools. +/// +/// WARNING: This migration works under the assumption that the [`BondedPools`] cannot be inflated +/// arbitrarily. Otherwise this migration could fail due to too high weight. +mod v7 { + use super::*; + + pub struct VersionUncheckedMigrateV6ToV7(sp_std::marker::PhantomData); + impl VersionUncheckedMigrateV6ToV7 { + fn calculate_tvl_by_total_stake() -> BalanceOf { + BondedPools::::iter() + .map(|(id, inner)| { + T::Staking::total_stake( + &BondedPool { id, inner: inner.clone() }.bonded_account(), + ) + .unwrap_or_default() + }) + .reduce(|acc, total_balance| acc + total_balance) + .unwrap_or_default() + } + } + + impl OnRuntimeUpgrade for VersionUncheckedMigrateV6ToV7 { + fn on_runtime_upgrade() -> Weight { + let migrated = BondedPools::::count(); + // The TVL should be the sum of all the funds that are actively staked and in the + // unbonding process of the account of each pool. + let tvl: BalanceOf = Self::calculate_tvl_by_total_stake(); + + TotalValueLocked::::set(tvl); + + log!(info, "Upgraded {} pools with a TVL of {:?}", migrated, tvl); + + // reads: migrated * (BondedPools + Staking::total_stake) + count + onchain + // version + // + // writes: current version + TVL + T::DbWeight::get().reads_writes(migrated.saturating_mul(2).saturating_add(2).into(), 2) + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, TryRuntimeError> { + Ok(Vec::new()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_data: Vec) -> Result<(), TryRuntimeError> { + // check that the `TotalValueLocked` written is actually the sum of `total_stake` of the + // `BondedPools`` + let tvl: BalanceOf = Self::calculate_tvl_by_total_stake(); + ensure!( + TotalValueLocked::::get() == tvl, + "TVL written is not equal to `Staking::total_stake` of all `BondedPools`." + ); + + // calculate the sum of `total_balance` of all `PoolMember` as the upper bound for the + // `TotalValueLocked`. + let total_balance_members: BalanceOf = PoolMembers::::iter() + .map(|(_, member)| member.total_balance()) + .reduce(|acc, total_balance| acc + total_balance) + .unwrap_or_default(); + + ensure!( + TotalValueLocked::::get() <= total_balance_members, + "TVL is greater than the balance of all PoolMembers." + ); + + ensure!( + Pallet::::on_chain_storage_version() >= 7, + "nomination-pools::migration::v7: wrong storage version" + ); + + Ok(()) + } + } +} + mod v6 { use super::*; diff --git a/substrate/frame/nomination-pools/src/mock.rs b/substrate/frame/nomination-pools/src/mock.rs index 3884518a992b..d806ba071bd4 100644 --- a/substrate/frame/nomination-pools/src/mock.rs +++ b/substrate/frame/nomination-pools/src/mock.rs @@ -20,7 +20,7 @@ use crate::{self as pools}; use frame_support::{assert_ok, parameter_types, traits::fungible::Mutate, PalletId}; use frame_system::RawOrigin; use sp_runtime::{BuildStorage, FixedU128}; -use sp_staking::Stake; +use sp_staking::{OnStakingUpdate, Stake}; pub type BlockNumber = u64; pub type AccountId = u128; @@ -46,7 +46,8 @@ parameter_types! { pub static CurrentEra: EraIndex = 0; pub static BondingDuration: EraIndex = 3; pub storage BondedBalanceMap: BTreeMap = Default::default(); - pub storage UnbondingBalanceMap: BTreeMap = Default::default(); + // map from a user to a vec of eras and amounts being unlocked in each era. + pub storage UnbondingBalanceMap: BTreeMap> = Default::default(); #[derive(Clone, PartialEq)] pub static MaxUnbonding: u32 = 8; pub static StakingMinBond: Balance = 10; @@ -60,6 +61,19 @@ impl StakingMock { x.insert(who, bonded); BondedBalanceMap::set(&x) } + /// Mimics a slash towards a pool specified by `pool_id`. + /// This reduces the bonded balance of a pool by `amount` and calls [`Pools::on_slash`] to + /// enact changes in the nomination-pool pallet. + /// + /// Does not modify any [`SubPools`] of the pool as [`Default::default`] is passed for + /// `slashed_unlocking`. + pub fn slash_by(pool_id: PoolId, amount: Balance) { + let acc = Pools::create_bonded_account(pool_id); + let bonded = BondedBalanceMap::get(); + let pre_total = bonded.get(&acc).unwrap(); + Self::set_bonded_balance(acc, pre_total - amount); + Pools::on_slash(&acc, pre_total - amount, &Default::default(), amount); + } } impl sp_staking::StakingInterface for StakingMock { @@ -105,8 +119,11 @@ impl sp_staking::StakingInterface for StakingMock { let mut x = BondedBalanceMap::get(); *x.get_mut(who).unwrap() = x.get_mut(who).unwrap().saturating_sub(amount); BondedBalanceMap::set(&x); + + let era = Self::current_era(); + let unlocking_at = era + Self::bonding_duration(); let mut y = UnbondingBalanceMap::get(); - *y.entry(*who).or_insert(Self::Balance::zero()) += amount; + y.entry(*who).or_insert(Default::default()).push((unlocking_at, amount)); UnbondingBalanceMap::set(&y); Ok(()) } @@ -116,11 +133,13 @@ impl sp_staking::StakingInterface for StakingMock { } fn withdraw_unbonded(who: Self::AccountId, _: u32) -> Result { - // Simulates removing unlocking chunks and only having the bonded balance locked - let mut x = UnbondingBalanceMap::get(); - x.remove(&who); - UnbondingBalanceMap::set(&x); + let mut unbonding_map = UnbondingBalanceMap::get(); + let staker_map = unbonding_map.get_mut(&who).ok_or("Nothing to unbond")?; + + let current_era = Self::current_era(); + staker_map.retain(|(unlocking_at, _amount)| *unlocking_at > current_era); + UnbondingBalanceMap::set(&unbonding_map); Ok(UnbondingBalanceMap::get().is_empty() && BondedBalanceMap::get().is_empty()) } @@ -144,14 +163,17 @@ impl sp_staking::StakingInterface for StakingMock { } fn stake(who: &Self::AccountId) -> Result, DispatchError> { - match ( - UnbondingBalanceMap::get().get(who).copied(), - BondedBalanceMap::get().get(who).copied(), - ) { + match (UnbondingBalanceMap::get().get(who), BondedBalanceMap::get().get(who).copied()) { (None, None) => Err(DispatchError::Other("balance not found")), - (Some(v), None) => Ok(Stake { total: v, active: 0 }), + (Some(v), None) => Ok(Stake { + total: v.into_iter().fold(0u128, |acc, &x| acc.saturating_add(x.1)), + active: 0, + }), (None, Some(v)) => Ok(Stake { total: v, active: v }), - (Some(a), Some(b)) => Ok(Stake { total: a + b, active: b }), + (Some(a), Some(b)) => Ok(Stake { + total: a.into_iter().fold(0u128, |acc, &x| acc.saturating_add(x.1)) + b, + active: b, + }), } } diff --git a/substrate/frame/nomination-pools/src/tests.rs b/substrate/frame/nomination-pools/src/tests.rs index 67183e25689e..2749e89ecff3 100644 --- a/substrate/frame/nomination-pools/src/tests.rs +++ b/substrate/frame/nomination-pools/src/tests.rs @@ -59,6 +59,9 @@ fn test_setup_works() { assert_eq!(StakingMock::bonding_duration(), 3); assert!(Metadata::::contains_key(1)); + // initial member. + assert_eq!(TotalValueLocked::::get(), 10); + let last_pool = LastPoolId::::get(); assert_eq!( BondedPool::::get(last_pool).unwrap(), @@ -218,10 +221,7 @@ mod bonded_pool { // slash half of the pool's balance. expected result of `fn api_points_to_balance` // to be 1/2 of the pool's balance. - StakingMock::set_bonded_balance( - default_bonded_account(), - Pools::depositor_min_bond() / 2, - ); + StakingMock::slash_by(1, Pools::depositor_min_bond() / 2); assert_eq!(Pallet::::api_points_to_balance(1, 10), 5); // if pool does not exist, points to balance ratio is 0. @@ -238,10 +238,7 @@ mod bonded_pool { // slash half of the pool's balance. expect result of `fn api_balance_to_points` // to be 2 * of the balance to add to the pool. - StakingMock::set_bonded_balance( - default_bonded_account(), - Pools::depositor_min_bond() / 2, - ); + StakingMock::slash_by(1, Pools::depositor_min_bond() / 2); assert_eq!(Pallet::::api_balance_to_points(1, 10), 20); // if pool does not exist, balance to points ratio is 0. @@ -637,12 +634,12 @@ mod join { // Given Currency::set_balance(&11, ExistentialDeposit::get() + 2); assert!(!PoolMembers::::contains_key(11)); + assert_eq!(TotalValueLocked::::get(), 10); // When assert_ok!(Pools::join(RuntimeOrigin::signed(11), 2, 1)); // Then - assert_eq!( pool_events_since_last_call(), vec![ @@ -651,6 +648,7 @@ mod join { Event::Bonded { member: 11, pool_id: 1, bonded: 2, joined: true }, ] ); + assert_eq!(TotalValueLocked::::get(), 12); assert_eq!( PoolMembers::::get(11).unwrap(), @@ -660,7 +658,7 @@ mod join { // Given // The bonded balance is slashed in half - StakingMock::set_bonded_balance(Pools::create_bonded_account(1), 6); + StakingMock::slash_by(1, 6); // And Currency::set_balance(&12, ExistentialDeposit::get() + 12); @@ -672,8 +670,12 @@ mod join { // Then assert_eq!( pool_events_since_last_call(), - vec![Event::Bonded { member: 12, pool_id: 1, bonded: 12, joined: true }] + vec![ + Event::PoolSlashed { pool_id: 1, balance: 6 }, + Event::Bonded { member: 12, pool_id: 1, bonded: 12, joined: true } + ] ); + assert_eq!(TotalValueLocked::::get(), 18); assert_eq!( PoolMembers::::get(12).unwrap(), @@ -2359,11 +2361,15 @@ mod unbond { .min_join_bond(10) .add_members(vec![(20, 20)]) .build_and_execute(|| { + assert_eq!(TotalValueLocked::::get(), 30); // can unbond to above limit assert_ok!(Pools::unbond(RuntimeOrigin::signed(20), 20, 5)); assert_eq!(PoolMembers::::get(20).unwrap().active_points(), 15); assert_eq!(PoolMembers::::get(20).unwrap().unbonding_points(), 5); + // tvl remains unchanged. + assert_eq!(TotalValueLocked::::get(), 30); + // cannot go to below 10: assert_noop!( Pools::unbond(RuntimeOrigin::signed(20), 20, 10), @@ -2669,8 +2675,9 @@ mod unbond { .add_members(vec![(40, 40), (550, 550)]) .build_and_execute(|| { let ed = Currency::minimum_balance(); - // Given a slash from 600 -> 100 - StakingMock::set_bonded_balance(default_bonded_account(), 100); + // Given a slash from 600 -> 500 + StakingMock::slash_by(1, 500); + // and unclaimed rewards of 600. Currency::set_balance(&default_reward_account(), ed + 600); @@ -2702,8 +2709,9 @@ mod unbond { Event::Bonded { member: 10, pool_id: 1, bonded: 10, joined: true }, Event::Bonded { member: 40, pool_id: 1, bonded: 40, joined: true }, Event::Bonded { member: 550, pool_id: 1, bonded: 550, joined: true }, + Event::PoolSlashed { pool_id: 1, balance: 100 }, Event::PaidOut { member: 40, pool_id: 1, payout: 40 }, - Event::Unbonded { member: 40, pool_id: 1, points: 6, balance: 6, era: 3 } + Event::Unbonded { member: 40, pool_id: 1, balance: 6, points: 6, era: 3 } ] ); @@ -2863,6 +2871,7 @@ mod unbond { ); // When the root kicks then its ok + // Account with ID 100 is kicked. assert_ok!(Pools::fully_unbond(RuntimeOrigin::signed(900), 100)); assert_eq!( @@ -2883,6 +2892,7 @@ mod unbond { ); // When the bouncer kicks then its ok + // Account with ID 200 is kicked. assert_ok!(Pools::fully_unbond(RuntimeOrigin::signed(902), 200)); assert_eq!( @@ -2921,7 +2931,7 @@ mod unbond { ); assert_eq!( *UnbondingBalanceMap::get().get(&default_bonded_account()).unwrap(), - 100 + 200 + vec![(3, 100), (3, 200)], ); }); } @@ -3020,7 +3030,10 @@ mod unbond { } ); assert_eq!(StakingMock::active_stake(&default_bonded_account()).unwrap(), 0); - assert_eq!(*UnbondingBalanceMap::get().get(&default_bonded_account()).unwrap(), 10); + assert_eq!( + *UnbondingBalanceMap::get().get(&default_bonded_account()).unwrap(), + vec![(6, 10)] + ); }); } @@ -3298,7 +3311,7 @@ mod unbond { assert_eq!(PoolMembers::::get(10).unwrap().unbonding_points(), 0); // slash the default pool - StakingMock::set_bonded_balance(Pools::create_bonded_account(1), 5); + StakingMock::slash_by(1, 5); // cannot unbond even 7, because the value of shares is now less. assert_noop!( @@ -3368,21 +3381,58 @@ mod pool_withdraw_unbonded { #[test] fn pool_withdraw_unbonded_works() { - ExtBuilder::default().build_and_execute(|| { - // Given 10 unbonded directly against the pool account - assert_ok!(StakingMock::unbond(&default_bonded_account(), 5)); - // and the pool account only has 10 balance - assert_eq!(StakingMock::active_stake(&default_bonded_account()), Ok(5)); - assert_eq!(StakingMock::total_stake(&default_bonded_account()), Ok(10)); - assert_eq!(Currency::free_balance(&default_bonded_account()), 10); + ExtBuilder::default().add_members(vec![(20, 10)]).build_and_execute(|| { + // Given 10 unbond'ed directly against the pool account + + assert_ok!(Pools::unbond(RuntimeOrigin::signed(20), 20, 5)); + + assert_eq!(StakingMock::active_stake(&default_bonded_account()), Ok(15)); + assert_eq!(StakingMock::total_stake(&default_bonded_account()), Ok(20)); + assert_eq!(Balances::free_balance(&default_bonded_account()), 20); // When + CurrentEra::set(StakingMock::current_era() + StakingMock::bonding_duration() + 1); assert_ok!(Pools::pool_withdraw_unbonded(RuntimeOrigin::signed(10), 1, 0)); - // Then there unbonding balance is no longer locked - assert_eq!(StakingMock::active_stake(&default_bonded_account()), Ok(5)); - assert_eq!(StakingMock::total_stake(&default_bonded_account()), Ok(5)); - assert_eq!(Currency::free_balance(&default_bonded_account()), 10); + // Then their unbonding balance is no longer locked + assert_eq!(StakingMock::active_stake(&default_bonded_account()), Ok(15)); + assert_eq!(StakingMock::total_stake(&default_bonded_account()), Ok(15)); + assert_eq!(Balances::free_balance(&default_bonded_account()), 20); + }); + } + #[test] + fn pool_withdraw_unbonded_creates_tvl_diff() { + ExtBuilder::default().add_members(vec![(20, 10)]).build_and_execute(|| { + // Given 10 unbond'ed directly against the pool account + assert_ok!(Pools::unbond(RuntimeOrigin::signed(20), 20, 5)); + + assert_eq!(StakingMock::active_stake(&default_bonded_account()), Ok(15)); + assert_eq!(StakingMock::total_stake(&default_bonded_account()), Ok(20)); + assert_eq!(Balances::free_balance(&default_bonded_account()), 20); + assert_eq!(TotalValueLocked::::get(), 20); + + // When + CurrentEra::set(StakingMock::current_era() + StakingMock::bonding_duration() + 1); + assert_ok!(Pools::pool_withdraw_unbonded(RuntimeOrigin::signed(10), 1, 0)); + assert_eq!(TotalValueLocked::::get(), 15); + + let member_balance = PoolMembers::::iter() + .map(|(_, member)| member.total_balance()) + .reduce(|acc, total_balance| acc + total_balance) + .unwrap_or_default(); + + // Then their unbonding balance is no longer locked + assert_eq!(StakingMock::active_stake(&default_bonded_account()), Ok(15)); + assert_eq!(StakingMock::total_stake(&default_bonded_account()), Ok(15)); + assert_eq!(Currency::free_balance(&default_bonded_account()), 20); + + // The difference between TVL and member_balance is exactly the difference between + // `total_stake` and the `free_balance`. + // This relation is not guaranteed in the wild as arbitrary transfers towards + // `free_balance` can be made to the pool that are not accounted for. + let non_locked_balance = Balances::free_balance(&default_bonded_account()) - + StakingMock::total_stake(&default_bonded_account()).unwrap(); + assert_eq!(member_balance, TotalValueLocked::::get() + non_locked_balance); }); } } @@ -3412,24 +3462,33 @@ mod withdraw_unbonded { let unbond_pool = sub_pools.with_era.get_mut(&3).unwrap(); // Sanity check assert_eq!(*unbond_pool, UnbondPool { points: 550 + 40, balance: 550 + 40 }); + assert_eq!(TotalValueLocked::::get(), 600); // Simulate a slash to the pool with_era(current_era), decreasing the balance by // half { unbond_pool.balance /= 2; // 295 SubPoolsStorage::::insert(1, sub_pools); + + // Adjust the TVL for this non-api usage (direct sub-pool modification) + TotalValueLocked::::mutate(|x| *x -= 295); + // Update the equivalent of the unbonding chunks for the `StakingMock` let mut x = UnbondingBalanceMap::get(); - *x.get_mut(&default_bonded_account()).unwrap() /= 5; + x.get_mut(&default_bonded_account()) + .unwrap() + .get_mut(current_era as usize) + .unwrap() + .1 /= 2; UnbondingBalanceMap::set(&x); + Currency::set_balance( &default_bonded_account(), Currency::free_balance(&default_bonded_account()) / 2, // 300 ); - StakingMock::set_bonded_balance( - default_bonded_account(), - StakingMock::active_stake(&default_bonded_account()).unwrap() / 2, - ); + assert_eq!(StakingMock::active_stake(&default_bonded_account()).unwrap(), 10); + StakingMock::slash_by(1, 5); + assert_eq!(StakingMock::active_stake(&default_bonded_account()).unwrap(), 5); }; // Advance the current_era to ensure all `with_era` pools will be merged into @@ -3465,6 +3524,7 @@ mod withdraw_unbonded { era: 3 }, Event::Unbonded { member: 40, pool_id: 1, points: 40, balance: 40, era: 3 }, + Event::PoolSlashed { pool_id: 1, balance: 5 } ] ); assert_eq!( @@ -3552,7 +3612,7 @@ mod withdraw_unbonded { // Given // current bond is 600, we slash it all to 300. - StakingMock::set_bonded_balance(default_bonded_account(), 300); + StakingMock::slash_by(1, 300); Currency::set_balance(&default_bonded_account(), 300); assert_eq!(StakingMock::total_stake(&default_bonded_account()), Ok(300)); @@ -3572,6 +3632,7 @@ mod withdraw_unbonded { Event::Bonded { member: 10, pool_id: 1, bonded: 10, joined: true }, Event::Bonded { member: 40, pool_id: 1, bonded: 40, joined: true }, Event::Bonded { member: 550, pool_id: 1, bonded: 550, joined: true }, + Event::PoolSlashed { pool_id: 1, balance: 300 }, Event::Unbonded { member: 40, pool_id: 1, balance: 20, points: 20, era: 3 }, Event::Unbonded { member: 550, @@ -4051,6 +4112,7 @@ mod withdraw_unbonded { #[test] fn full_multi_step_withdrawing_non_depositor() { ExtBuilder::default().add_members(vec![(100, 100)]).build_and_execute(|| { + assert_eq!(TotalValueLocked::::get(), 110); // given assert_ok!(Pools::unbond(RuntimeOrigin::signed(100), 100, 75)); assert_eq!( @@ -4058,6 +4120,9 @@ mod withdraw_unbonded { member_unbonding_eras!(3 => 75) ); + // tvl unchanged. + assert_eq!(TotalValueLocked::::get(), 110); + // progress one era and unbond the leftover. CurrentEra::set(1); assert_ok!(Pools::unbond(RuntimeOrigin::signed(100), 100, 25)); @@ -4070,6 +4135,8 @@ mod withdraw_unbonded { Pools::withdraw_unbonded(RuntimeOrigin::signed(100), 100, 0), Error::::CannotWithdrawAny ); + // tvl unchanged. + assert_eq!(TotalValueLocked::::get(), 110); // now the 75 should be free. CurrentEra::set(3); @@ -4089,6 +4156,8 @@ mod withdraw_unbonded { PoolMembers::::get(100).unwrap().unbonding_eras, member_unbonding_eras!(4 => 25) ); + // tvl updated + assert_eq!(TotalValueLocked::::get(), 35); // the 25 should be free now, and the member removed. CurrentEra::set(4); @@ -4398,6 +4467,7 @@ mod create { let next_pool_stash = Pools::create_bonded_account(2); let ed = Currency::minimum_balance(); + assert_eq!(TotalValueLocked::::get(), 10); assert!(!BondedPools::::contains_key(2)); assert!(!RewardPools::::contains_key(2)); assert!(!PoolMembers::::contains_key(11)); @@ -4411,6 +4481,7 @@ mod create { 456, 789 )); + assert_eq!(TotalValueLocked::::get(), 10 + StakingMock::minimum_nominator_bond()); assert_eq!(Currency::free_balance(&11), 0); assert_eq!( @@ -4701,9 +4772,10 @@ mod set_state { // Given unsafe_set_state(1, PoolState::Open); - let mut bonded_pool = BondedPool::::get(1).unwrap(); - bonded_pool.points = 100; - bonded_pool.put(); + // slash the pool to the point that `max_points_to_balance` ratio is + // surpassed. Making this pool destroyable by anyone. + StakingMock::slash_by(1, 10); + // When assert_ok!(Pools::set_state(RuntimeOrigin::signed(11), 1, PoolState::Destroying)); // Then @@ -4729,6 +4801,7 @@ mod set_state { pool_events_since_last_call(), vec![ Event::StateChanged { pool_id: 1, new_state: PoolState::Destroying }, + Event::PoolSlashed { pool_id: 1, balance: 0 }, Event::StateChanged { pool_id: 1, new_state: PoolState::Destroying }, Event::StateChanged { pool_id: 1, new_state: PoolState::Destroying } ] @@ -4927,8 +5000,10 @@ mod bond_extra { assert_eq!(PoolMembers::::get(10).unwrap().points, 10); assert_eq!(PoolMembers::::get(20).unwrap().points, 20); assert_eq!(BondedPools::::get(1).unwrap().points, 30); + assert_eq!(Currency::free_balance(&10), 35); assert_eq!(Currency::free_balance(&20), 20); + assert_eq!(TotalValueLocked::::get(), 30); // when assert_ok!(Pools::bond_extra(RuntimeOrigin::signed(10), BondExtra::Rewards)); @@ -4936,6 +5011,8 @@ mod bond_extra { // then assert_eq!(Currency::free_balance(&10), 35); + assert_eq!(TotalValueLocked::::get(), 31); + // 10's share of the reward is 1/3, since they gave 10/30 of the total shares. assert_eq!(PoolMembers::::get(10).unwrap().points, 10 + 1); assert_eq!(BondedPools::::get(1).unwrap().points, 30 + 1); @@ -4945,6 +5022,8 @@ mod bond_extra { // then assert_eq!(Currency::free_balance(&20), 20); + assert_eq!(TotalValueLocked::::get(), 33); + // 20's share of the rewards is the other 2/3 of the rewards, since they have 20/30 of // the shares assert_eq!(PoolMembers::::get(20).unwrap().points, 20 + 2); @@ -5354,7 +5433,7 @@ mod reward_counter_precision { ); // slash this pool by 99% of that. - StakingMock::set_bonded_balance(default_bonded_account(), DOT + pool_bond / 100); + StakingMock::slash_by(1, pool_bond * 99 / 100); // some whale now joins with the other half ot the total issuance. This will trigger an // overflow. This test is actually a bit too lenient because all the reward counters are @@ -6868,3 +6947,73 @@ mod commission { }) } } +mod slash { + use super::*; + + #[test] + fn slash_no_subpool_is_tracked() { + let bonded = |points, member_counter| BondedPool:: { + id: 1, + inner: BondedPoolInner { + commission: Commission::default(), + member_counter, + points, + roles: DEFAULT_ROLES, + state: PoolState::Open, + }, + }; + ExtBuilder::default().with_check(0).build_and_execute(|| { + // Given + Currency::set_balance(&11, ExistentialDeposit::get() + 2); + assert!(!PoolMembers::::contains_key(11)); + assert_eq!(TotalValueLocked::::get(), 10); + + // When + assert_ok!(Pools::join(RuntimeOrigin::signed(11), 2, 1)); + + // Then + assert_eq!( + pool_events_since_last_call(), + vec![ + Event::Created { depositor: 10, pool_id: 1 }, + Event::Bonded { member: 10, pool_id: 1, bonded: 10, joined: true }, + Event::Bonded { member: 11, pool_id: 1, bonded: 2, joined: true }, + ] + ); + assert_eq!(TotalValueLocked::::get(), 12); + + assert_eq!( + PoolMembers::::get(11).unwrap(), + PoolMember:: { pool_id: 1, points: 2, ..Default::default() } + ); + assert_eq!(BondedPool::::get(1).unwrap(), bonded(12, 2)); + + // Given + // The bonded balance is slashed in half + StakingMock::slash_by(1, 6); + + // And + Currency::set_balance(&12, ExistentialDeposit::get() + 12); + assert!(!PoolMembers::::contains_key(12)); + + // When + assert_ok!(Pools::join(RuntimeOrigin::signed(12), 12, 1)); + + // Then + assert_eq!( + pool_events_since_last_call(), + vec![ + Event::PoolSlashed { pool_id: 1, balance: 6 }, + Event::Bonded { member: 12, pool_id: 1, bonded: 12, joined: true } + ] + ); + assert_eq!(TotalValueLocked::::get(), 18); + + assert_eq!( + PoolMembers::::get(12).unwrap(), + PoolMember:: { pool_id: 1, points: 24, ..Default::default() } + ); + assert_eq!(BondedPool::::get(1).unwrap(), bonded(12 + 24, 3)); + }); + } +} diff --git a/substrate/frame/proxy/src/tests.rs b/substrate/frame/proxy/src/tests.rs index 0667be6e1e52..89bd8b68f091 100644 --- a/substrate/frame/proxy/src/tests.rs +++ b/substrate/frame/proxy/src/tests.rs @@ -45,25 +45,14 @@ frame_support::construct_runtime!( #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { type Block = Block; - type BlockHashCount = ConstU64<250>; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type RuntimeEvent = RuntimeEvent; - type PalletInfo = PalletInfo; - type OnSetCode = (); - type BaseCallFilter = BaseFilter; type AccountData = pallet_balances::AccountData; } #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)] impl pallet_balances::Config for Test { - type RuntimeEvent = RuntimeEvent; - type RuntimeHoldReason = (); type ReserveIdentifier = [u8; 8]; - type DustRemoval = (); type AccountStore = System; - type ExistentialDeposit = ConstU64<1>; } impl pallet_utility::Config for Test { diff --git a/substrate/frame/staking/src/lib.rs b/substrate/frame/staking/src/lib.rs index e59b2a3324a6..dcf57a464323 100644 --- a/substrate/frame/staking/src/lib.rs +++ b/substrate/frame/staking/src/lib.rs @@ -671,8 +671,14 @@ impl StakingLedger { // clean unlocking chunks that are set to zero. self.unlocking.retain(|c| !c.value.is_zero()); - T::EventListeners::on_slash(&self.stash, self.active, &slashed_unlocking); - pre_slash_total.saturating_sub(self.total) + let final_slashed_amount = pre_slash_total.saturating_sub(self.total); + T::EventListeners::on_slash( + &self.stash, + self.active, + &slashed_unlocking, + final_slashed_amount, + ); + final_slashed_amount } } diff --git a/substrate/frame/staking/src/mock.rs b/substrate/frame/staking/src/mock.rs index cf08f8be1f27..c41144278f2c 100644 --- a/substrate/frame/staking/src/mock.rs +++ b/substrate/frame/staking/src/mock.rs @@ -278,6 +278,7 @@ impl OnStakingUpdate for EventListenerMock { _pool_account: &AccountId, slashed_bonded: Balance, slashed_chunks: &BTreeMap, + _total_slashed: Balance, ) { LedgerSlashPerEra::set((slashed_bonded, slashed_chunks.clone())); } diff --git a/substrate/frame/support/procedural/src/lib.rs b/substrate/frame/support/procedural/src/lib.rs index 466ceca42961..da4cb41fe4f2 100644 --- a/substrate/frame/support/procedural/src/lib.rs +++ b/substrate/frame/support/procedural/src/lib.rs @@ -442,8 +442,8 @@ pub fn derive_runtime_debug_no_bound(input: TokenStream) -> TokenStream { quote::quote!( const _: () = { - impl #impl_generics core::fmt::Debug for #name #ty_generics #where_clause { - fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result { + impl #impl_generics ::core::fmt::Debug for #name #ty_generics #where_clause { + fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> core::fmt::Result { fmt.write_str("") } } @@ -473,7 +473,7 @@ pub fn derive_eq_no_bound(input: TokenStream) -> TokenStream { quote::quote_spanned!(name.span() => const _: () = { - impl #impl_generics core::cmp::Eq for #name #ty_generics #where_clause {} + impl #impl_generics ::core::cmp::Eq for #name #ty_generics #where_clause {} }; ) .into() @@ -877,6 +877,8 @@ pub fn inject_runtime_type(_: TokenStream, tokens: TokenStream) -> TokenStream { if item.ident != "RuntimeCall" && item.ident != "RuntimeEvent" && item.ident != "RuntimeOrigin" && + item.ident != "RuntimeHoldReason" && + item.ident != "RuntimeFreezeReason" && item.ident != "PalletInfo" { return syn::Error::new_spanned( diff --git a/substrate/frame/support/procedural/src/no_bound/clone.rs b/substrate/frame/support/procedural/src/no_bound/clone.rs index bbea2feffa96..2c9037984f59 100644 --- a/substrate/frame/support/procedural/src/no_bound/clone.rs +++ b/substrate/frame/support/procedural/src/no_bound/clone.rs @@ -32,7 +32,7 @@ pub fn derive_clone_no_bound(input: proc_macro::TokenStream) -> proc_macro::Toke syn::Fields::Named(named) => { let fields = named.named.iter().map(|i| &i.ident).map(|i| { quote::quote_spanned!(i.span() => - #i: core::clone::Clone::clone(&self.#i) + #i: ::core::clone::Clone::clone(&self.#i) ) }); @@ -42,7 +42,7 @@ pub fn derive_clone_no_bound(input: proc_macro::TokenStream) -> proc_macro::Toke let fields = unnamed.unnamed.iter().enumerate().map(|(i, _)| syn::Index::from(i)).map(|i| { quote::quote_spanned!(i.span() => - core::clone::Clone::clone(&self.#i) + ::core::clone::Clone::clone(&self.#i) ) }); @@ -59,8 +59,8 @@ pub fn derive_clone_no_bound(input: proc_macro::TokenStream) -> proc_macro::Toke syn::Fields::Named(named) => { let captured = named.named.iter().map(|i| &i.ident); let cloned = captured.clone().map(|i| { - quote::quote_spanned!(i.span() => - #i: core::clone::Clone::clone(#i) + ::quote::quote_spanned!(i.span() => + #i: ::core::clone::Clone::clone(#i) ) }); quote::quote!( @@ -75,7 +75,7 @@ pub fn derive_clone_no_bound(input: proc_macro::TokenStream) -> proc_macro::Toke .map(|(i, f)| syn::Ident::new(&format!("_{}", i), f.span())); let cloned = captured.clone().map(|i| { quote::quote_spanned!(i.span() => - core::clone::Clone::clone(#i) + ::core::clone::Clone::clone(#i) ) }); quote::quote!( @@ -98,7 +98,7 @@ pub fn derive_clone_no_bound(input: proc_macro::TokenStream) -> proc_macro::Toke quote::quote!( const _: () = { - impl #impl_generics core::clone::Clone for #name #ty_generics #where_clause { + impl #impl_generics ::core::clone::Clone for #name #ty_generics #where_clause { fn clone(&self) -> Self { #impl_ } diff --git a/substrate/frame/support/procedural/src/no_bound/debug.rs b/substrate/frame/support/procedural/src/no_bound/debug.rs index ae182829a49e..88f5dfe7bec4 100644 --- a/substrate/frame/support/procedural/src/no_bound/debug.rs +++ b/substrate/frame/support/procedural/src/no_bound/debug.rs @@ -112,8 +112,8 @@ pub fn derive_debug_no_bound(input: proc_macro::TokenStream) -> proc_macro::Toke quote::quote!( const _: () = { - impl #impl_generics core::fmt::Debug for #input_ident #ty_generics #where_clause { - fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result { + impl #impl_generics ::core::fmt::Debug for #input_ident #ty_generics #where_clause { + fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { #impl_ } } diff --git a/substrate/frame/support/procedural/src/no_bound/default.rs b/substrate/frame/support/procedural/src/no_bound/default.rs index 35d0eaeecf56..ddaab26c4415 100644 --- a/substrate/frame/support/procedural/src/no_bound/default.rs +++ b/substrate/frame/support/procedural/src/no_bound/default.rs @@ -32,7 +32,7 @@ pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::To Fields::Named(named) => { let fields = named.named.iter().map(|field| &field.ident).map(|ident| { quote_spanned! {ident.span() => - #ident: core::default::Default::default() + #ident: ::core::default::Default::default() } }); @@ -41,7 +41,7 @@ pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::To Fields::Unnamed(unnamed) => { let fields = unnamed.unnamed.iter().map(|field| { quote_spanned! {field.span()=> - core::default::Default::default() + ::core::default::Default::default() } }); @@ -105,7 +105,7 @@ pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::To let fields = named.named.iter().map(|field| &field.ident).map(|ident| { quote_spanned! {ident.span()=> - #ident: core::default::Default::default() + #ident: ::core::default::Default::default() } }); @@ -114,7 +114,7 @@ pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::To Fields::Unnamed(unnamed) => { let fields = unnamed.unnamed.iter().map(|field| { quote_spanned! {field.span()=> - core::default::Default::default() + ::core::default::Default::default() } }); @@ -149,7 +149,7 @@ pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::To quote!( const _: () = { - impl #impl_generics core::default::Default for #name #ty_generics #where_clause { + impl #impl_generics ::core::default::Default for #name #ty_generics #where_clause { fn default() -> Self { #impl_ } diff --git a/substrate/frame/support/procedural/src/no_bound/partial_eq.rs b/substrate/frame/support/procedural/src/no_bound/partial_eq.rs index 27f5e98810ec..1a4a4e50b39e 100644 --- a/substrate/frame/support/procedural/src/no_bound/partial_eq.rs +++ b/substrate/frame/support/procedural/src/no_bound/partial_eq.rs @@ -128,7 +128,7 @@ pub fn derive_partial_eq_no_bound(input: proc_macro::TokenStream) -> proc_macro: quote::quote!( const _: () = { - impl #impl_generics core::cmp::PartialEq for #name #ty_generics #where_clause { + impl #impl_generics ::core::cmp::PartialEq for #name #ty_generics #where_clause { fn eq(&self, other: &Self) -> bool { #impl_ } diff --git a/substrate/frame/support/src/tests/mod.rs b/substrate/frame/support/src/tests/mod.rs index db458880db68..3690159c5994 100644 --- a/substrate/frame/support/src/tests/mod.rs +++ b/substrate/frame/support/src/tests/mod.rs @@ -647,3 +647,17 @@ fn check_storage_parameter_type_works() { assert_eq!(300, StorageParameter::get()); }) } + +#[test] +fn derive_partial_eq_no_bound_core_mod() { + mod core {} + + #[derive( + crate::PartialEqNoBound, + crate::CloneNoBound, + crate::DebugNoBound, + crate::DefaultNoBound, + crate::EqNoBound, + )] + struct Test; +} diff --git a/substrate/primitives/staking/src/lib.rs b/substrate/primitives/staking/src/lib.rs index 1621af164b37..8b5797d7918f 100644 --- a/substrate/primitives/staking/src/lib.rs +++ b/substrate/primitives/staking/src/lib.rs @@ -121,10 +121,12 @@ pub trait OnStakingUpdate { /// * `slashed_active` - The new bonded balance of the staker after the slash was applied. /// * `slashed_unlocking` - A map of slashed eras, and the balance of that unlocking chunk after /// the slash is applied. Any era not present in the map is not affected at all. + /// * `slashed_total` - The aggregated balance that was lost due to the slash. fn on_slash( _stash: &AccountId, _slashed_active: Balance, _slashed_unlocking: &BTreeMap, + _slashed_total: Balance, ) { } } From 6cf3c4218c2737c3aa09a8b7e9e6db1f67ffc917 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Mon, 2 Oct 2023 09:14:05 -0300 Subject: [PATCH 28/56] Address feedback --- cumulus/pallets/parachain-system/src/lib.rs | 54 ++++++++++++--------- cumulus/pallets/xcmp-queue/src/lib.rs | 36 ++++++-------- cumulus/pallets/xcmp-queue/src/mock.rs | 3 +- polkadot/runtime/parachains/src/dmp.rs | 32 +++++------- polkadot/runtime/parachains/src/lib.rs | 10 ++++ 5 files changed, 68 insertions(+), 67 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index c9b21b6194ef..23ecfc5a97b8 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -183,9 +183,12 @@ mod ump_constants { /// `host_config.max_upward_queue_size / THRESHOLD_FACTOR` is the threshold after which delivery /// starts getting exponentially more expensive. - /// The price starts to increase when queue is half full + /// `2` means the price starts to increase when queue is half full. pub const THRESHOLD_FACTOR: u32 = 2; + /// The base number the delivery fee factor gets multiplied by every time it is increased. + /// Also the number it gets divided by when decreased. pub const EXPONENTIAL_FEE_BASE: FixedU128 = FixedU128::from_rational(105, 100); // 1.05 + /// The base number message size in KB is multiplied by before increasing the fee factor. pub const MESSAGE_SIZE_FEE_BASE: FixedU128 = FixedU128::from_rational(1, 1000); // 0.001 } @@ -252,6 +255,9 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { + /// Handles actually sending upward messages by moving them from [`PendingUpwardMessages`] to [`UpwardMessages`]. + /// Decreases the delivery fee factor if after sending messages, the queue total size is less than the threshold + /// (see [`ump_constants::THRESHOLD_FACTOR`]). fn on_finalize(_: BlockNumberFor) { >::kill(); >::kill(); @@ -338,10 +344,13 @@ pub mod pallet { UpwardMessages::::put(&up[..num as usize]); *up = up.split_off(num as usize); + // If the total size of the pending messages is less than the threshold, + // we decrease the fee factor, since the queue is less congested. + // This makes delivery of new messages cheaper. let threshold = host_config.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); let remaining_total_size: usize = up.iter().map(UpwardMessage::len).sum(); if remaining_total_size <= threshold as usize { - Self::decrement_ump_fee_factor(); + Self::decrease_fee_factor(()); } (num, total_size) @@ -1000,23 +1009,23 @@ impl Pallet { let segment = UnincludedSegment::::get(); crate::unincluded_segment::size_after_included(included_hash, &segment) } +} - /// Raises the delivery fee factor by a multiplicative factor and stores the resulting value. - /// - /// Returns the new delivery fee factor after the increment. - pub(crate) fn increment_ump_fee_factor(message_size_factor: FixedU128) -> FixedU128 { +impl FeeTracker for Pallet { + type Id = (); + + fn get_fee_factor(_: Self::Id) -> FixedU128 { + UpwardDeliveryFeeFactor::::get() + } + + fn increase_fee_factor(_: Self::Id, message_size_factor: FixedU128) -> FixedU128 { >::mutate(|f| { - *f = f.saturating_mul(ump_constants::EXPONENTIAL_FEE_BASE + message_size_factor); + *f = f.saturating_mul(ump_constants::EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor)); *f }) } - /// Reduces the delivery fee factor by a multiplicative factor and stores the resulting value. - /// - /// Does not reduce the fee factor below the initial value, which is currently set as 1. - /// - /// Returns the new delivery fee factor after the decrement. - pub(crate) fn decrement_ump_fee_factor() -> FixedU128 { + fn decrease_fee_factor(_: Self::Id) -> FixedU128 { >::mutate(|f| { *f = UpwardInitialDeliveryFeeFactor::get().max(*f / ump_constants::EXPONENTIAL_FEE_BASE); @@ -1025,14 +1034,6 @@ impl Pallet { } } -impl FeeTracker for Pallet { - type Id = (); - - fn get_fee_factor(_: Self::Id) -> FixedU128 { - UpwardDeliveryFeeFactor::::get() - } -} - impl GetChannelInfo for Pallet { fn get_channel_status(id: ParaId) -> ChannelStatus { // Note, that we are using `relevant_messaging_state` which may be from the previous @@ -1525,6 +1526,10 @@ impl frame_system::SetCode for ParachainSetCode { } impl Pallet { + /// Puts a message in the [`PendingUpwardMessages`] storage item. + /// The message will be later sent in [`on_finalize`]. + /// Checks host configuration to see if message is too big. + /// Increases the delivery fee factor if the queue is sufficiently (see [`ump_constants::THRESHOLD_FACTOR`]) congested. pub fn send_upward_message(message: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError> { let message_len = message.len(); // Check if the message fits into the relay-chain constraints. @@ -1545,14 +1550,15 @@ impl Pallet { } let threshold = cfg.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); // We check the threshold against total size and not number of messages since messages - // could be big or small + // could be big or small. let pending_messages = PendingUpwardMessages::::get(); let total_size: usize = pending_messages.iter().map(UpwardMessage::len).sum(); if total_size > threshold as usize { + // We increase the fee factor by a factor based on the new message's size in KB let message_size_factor = - FixedU128::from_u32(message_len.saturating_div(1024) as u32) + FixedU128::from((message_len / 1024) as u128) .saturating_mul(ump_constants::MESSAGE_SIZE_FEE_BASE); - Self::increment_ump_fee_factor(message_size_factor); + Self::increase_fee_factor((), message_size_factor); } } else { // This storage field should carry over from the previous block. So if it's None diff --git a/cumulus/pallets/xcmp-queue/src/lib.rs b/cumulus/pallets/xcmp-queue/src/lib.rs index a139564a93e0..5d97cc708c9d 100644 --- a/cumulus/pallets/xcmp-queue/src/lib.rs +++ b/cumulus/pallets/xcmp-queue/src/lib.rs @@ -970,28 +970,6 @@ impl Pallet { } }); } - - /// Increases the delivery fee factor by a multiplicative factor and stores the resulting value. - /// - /// Returns the new delivery fee factor after the increase. - pub(crate) fn increase_fee_factor(para: ParaId, message_size_factor: FixedU128) -> FixedU128 { - >::mutate(para, |f| { - *f = f.saturating_mul(EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor)); - *f - }) - } - - /// Decreases the delivery fee factor by a multiplicative factor and stores the resulting value. - /// - /// Does not reduce the fee factor below the initial value, which is currently set as 1. - /// - /// Returns the new delivery fee factor after the decrease. - pub(crate) fn decrease_fee_factor(para: ParaId) -> FixedU128 { - >::mutate(para, |f| { - *f = InitialFactor::get().max(*f / EXPONENTIAL_FEE_BASE); - *f - }) - } } impl XcmpMessageHandler for Pallet { @@ -1236,4 +1214,18 @@ impl FeeTracker for Pallet { fn get_fee_factor(id: Self::Id) -> FixedU128 { >::get(id) } + + fn increase_fee_factor(id: Self::Id, message_size_factor: FixedU128) -> FixedU128 { + >::mutate(id, |f| { + *f = f.saturating_mul(EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor)); + *f + }) + } + + fn decrease_fee_factor(id: Self::Id) -> FixedU128 { + >::mutate(id, |f| { + *f = InitialFactor::get().max(*f / EXPONENTIAL_FEE_BASE); + *f + }) + } } diff --git a/cumulus/pallets/xcmp-queue/src/mock.rs b/cumulus/pallets/xcmp-queue/src/mock.rs index a3f10fa5428c..e716a45e1c85 100644 --- a/cumulus/pallets/xcmp-queue/src/mock.rs +++ b/cumulus/pallets/xcmp-queue/src/mock.rs @@ -31,6 +31,7 @@ use sp_runtime::{ use xcm::prelude::*; use xcm_builder::{CurrencyAdapter, FixedWeightBounds, IsConcrete, NativeAsset, ParentIsPreset}; use xcm_executor::traits::ConvertOrigin; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; type Block = frame_system::mocking::MockBlock; @@ -205,7 +206,7 @@ impl Config for Test { type ControllerOrigin = EnsureRoot; type ControllerOriginConverter = SystemParachainAsSuperuser; type WeightInfo = (); - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = NoPriceForMessageDelivery; } pub fn new_test_ext() -> sp_io::TestExternalities { diff --git a/polkadot/runtime/parachains/src/dmp.rs b/polkadot/runtime/parachains/src/dmp.rs index d6981866bd01..78994a9e34ba 100644 --- a/polkadot/runtime/parachains/src/dmp.rs +++ b/polkadot/runtime/parachains/src/dmp.rs @@ -336,34 +336,26 @@ impl Pallet { ) -> Vec>> { DownwardMessageQueues::::get(&recipient) } +} - /// Increases the delivery fee factor by a multiplicative factor and stores the resulting value. - /// - /// Returns the new delivery fee factor after the increase. - pub(crate) fn increase_fee_factor(para: ParaId, message_size_factor: FixedU128) -> FixedU128 { - >::mutate(para, |f| { +impl FeeTracker for Pallet { + type Id = ParaId; + + fn get_fee_factor(id: Self::Id) -> FixedU128 { + DeliveryFeeFactor::::get(id) + } + + fn increase_fee_factor(id: Self::Id, message_size_factor: FixedU128) -> FixedU128 { + >::mutate(id, |f| { *f = f.saturating_mul(EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor)); *f }) } - /// Decreases the delivery fee factor by a multiplicative factor and stores the resulting value. - /// - /// Does not reduce the fee factor below the initial value, which is currently set as 1. - /// - /// Returns the new delivery fee factor after the decrease. - pub(crate) fn decrease_fee_factor(para: ParaId) -> FixedU128 { - >::mutate(para, |f| { + fn decrease_fee_factor(id: Self::Id) -> FixedU128 { + >::mutate(id, |f| { *f = InitialFactor::get().max(*f / EXPONENTIAL_FEE_BASE); *f }) } } - -impl FeeTracker for Pallet { - type Id = ParaId; - - fn get_fee_factor(id: Self::Id) -> FixedU128 { - DeliveryFeeFactor::::get(id) - } -} diff --git a/polkadot/runtime/parachains/src/lib.rs b/polkadot/runtime/parachains/src/lib.rs index 9483623bd33e..bc35ade4c6eb 100644 --- a/polkadot/runtime/parachains/src/lib.rs +++ b/polkadot/runtime/parachains/src/lib.rs @@ -64,6 +64,16 @@ pub trait FeeTracker { /// Returns the evolving exponential fee factor which will be used to calculate the delivery /// fees. fn get_fee_factor(id: Self::Id) -> FixedU128; + /// Increases the delivery fee factor by a factor based on message size and records the result. + /// + /// Returns the new delivery fee factor after the increase. + fn increase_fee_factor(id: Self::Id, message_size_factor: FixedU128) -> FixedU128; + /// Decreases the delivery fee factor by a constant factor and records the result. + /// + /// Does not reduce the fee factor below the initial value, which is currently set as 1. + /// + /// Returns the new delivery fee factor after the decrease. + fn decrease_fee_factor(id: Self::Id) -> FixedU128; } /// Schedule a para to be initialized at the start of the next session with the given genesis data. From df10d99c0acf9cea288c0bf6578252730d156387 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 3 Oct 2023 03:21:29 -0300 Subject: [PATCH 29/56] Use previously unused import --- cumulus/primitives/utility/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/primitives/utility/src/lib.rs b/cumulus/primitives/utility/src/lib.rs index f545eb134ac1..7b3929c36488 100644 --- a/cumulus/primitives/utility/src/lib.rs +++ b/cumulus/primitives/utility/src/lib.rs @@ -264,7 +264,7 @@ pub struct XcmFeesTo32ByteAccount, - ReceiverAccount: frame_support::traits::Get>, + ReceiverAccount: Get>, > TakeRevenue for XcmFeesTo32ByteAccount { fn take_revenue(revenue: MultiAsset) { From 5fdfd316013cb0d31cb13fce647240b9034a3081 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 3 Oct 2023 03:45:04 -0300 Subject: [PATCH 30/56] Fix tests --- polkadot/runtime/common/src/xcm_sender.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/polkadot/runtime/common/src/xcm_sender.rs b/polkadot/runtime/common/src/xcm_sender.rs index 207ef3432a2c..1bd9a1ffdaa7 100644 --- a/polkadot/runtime/common/src/xcm_sender.rs +++ b/polkadot/runtime/common/src/xcm_sender.rs @@ -246,6 +246,14 @@ mod tests { fn get_fee_factor(_: Self::Id) -> FixedU128 { FixedU128::from_rational(101, 100) } + + fn increase_fee_factor(_: Self::Id, _: FixedU128) -> FixedU128 { + FixedU128::from_rational(101, 100) + } + + fn decrease_fee_factor(_: Self::Id) -> FixedU128 { + FixedU128::from_rational(101, 100) + } } type TestExponentialPrice = From 732dd427bdb4fdee12c68a831cdb3e12ad08bde6 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 3 Oct 2023 04:24:35 -0300 Subject: [PATCH 31/56] Fixes --- cumulus/parachain-template/runtime/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cumulus/parachain-template/runtime/src/lib.rs b/cumulus/parachain-template/runtime/src/lib.rs index b9bf97d7786f..d52333847089 100644 --- a/cumulus/parachain-template/runtime/src/lib.rs +++ b/cumulus/parachain-template/runtime/src/lib.rs @@ -10,6 +10,7 @@ mod weights; pub mod xcm_config; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use cumulus_primitives_core::ParaId; use smallvec::smallvec; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; @@ -412,7 +413,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { type ControllerOrigin = EnsureRoot; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; type WeightInfo = (); - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = NoPriceForMessageDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { From c51b5191ea2ca535a080f2e165ffcb1ccbfb9fe8 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 3 Oct 2023 04:32:00 -0300 Subject: [PATCH 32/56] Fixes --- cumulus/parachain-template/runtime/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/cumulus/parachain-template/runtime/src/lib.rs b/cumulus/parachain-template/runtime/src/lib.rs index d52333847089..b6bf8419ec46 100644 --- a/cumulus/parachain-template/runtime/src/lib.rs +++ b/cumulus/parachain-template/runtime/src/lib.rs @@ -11,6 +11,7 @@ pub mod xcm_config; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use cumulus_primitives_core::ParaId; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use smallvec::smallvec; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; From 125971f573ac55191c00f21af42f2b640e39b422 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Tue, 3 Oct 2023 11:21:10 +0000 Subject: [PATCH 33/56] ".git/.scripts/commands/bench/bench.sh" --subcommand=xcm --runtime=westend --target_dir=polkadot --pallet=pallet_xcm_benchmarks::fungible --- .../xcm/pallet_xcm_benchmarks_fungible.rs | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/polkadot/runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_fungible.rs b/polkadot/runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_fungible.rs index b92749bfa15b..60e13ca2ad8d 100644 --- a/polkadot/runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_fungible.rs +++ b/polkadot/runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_fungible.rs @@ -17,10 +17,10 @@ //! Autogenerated weights for `pallet_xcm_benchmarks::fungible` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2023-07-17, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2023-10-03, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` -//! HOSTNAME: `runner-gghbxkbs-project-163-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz` -//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("westend-dev"), DB CACHE: 1024 +//! HOSTNAME: `runner-nbnwcyh-project-674-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz` +//! WASM-EXECUTION: Compiled, CHAIN: Some("westend-dev"), DB CACHE: 1024 // Executed Command: // target/production/polkadot @@ -31,12 +31,12 @@ // --extrinsic=* // --wasm-execution=compiled // --heap-pages=4096 -// --json-file=/builds/parity/mirrors/polkadot/.git/.artifacts/bench.json +// --json-file=/builds/parity/mirrors/polkadot-sdk/.git/.artifacts/bench.json // --pallet=pallet_xcm_benchmarks::fungible // --chain=westend-dev -// --header=./file_header.txt -// --template=./xcm/pallet-xcm-benchmarks/template.hbs -// --output=./runtime/westend/src/weights/xcm/ +// --header=./polkadot/file_header.txt +// --template=./polkadot/xcm/pallet-xcm-benchmarks/template.hbs +// --output=./polkadot/runtime/westend/src/weights/xcm/ #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] @@ -55,8 +55,8 @@ impl WeightInfo { // Proof Size summary in bytes: // Measured: `101` // Estimated: `3593` - // Minimum execution time: 24_887_000 picoseconds. - Weight::from_parts(25_361_000, 3593) + // Minimum execution time: 24_815_000 picoseconds. + Weight::from_parts(25_098_000, 3593) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -66,8 +66,8 @@ impl WeightInfo { // Proof Size summary in bytes: // Measured: `101` // Estimated: `6196` - // Minimum execution time: 52_408_000 picoseconds. - Weight::from_parts(53_387_000, 6196) + // Minimum execution time: 51_268_000 picoseconds. + Weight::from_parts(51_857_000, 6196) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(2)) } @@ -83,10 +83,10 @@ impl WeightInfo { /// Proof: `Dmp::DownwardMessageQueueHeads` (`max_values`: None, `max_size`: None, mode: `Measured`) pub(crate) fn transfer_reserve_asset() -> Weight { // Proof Size summary in bytes: - // Measured: `177` + // Measured: `210` // Estimated: `6196` - // Minimum execution time: 74_753_000 picoseconds. - Weight::from_parts(76_838_000, 6196) + // Minimum execution time: 74_113_000 picoseconds. + Weight::from_parts(74_721_000, 6196) .saturating_add(T::DbWeight::get().reads(6)) .saturating_add(T::DbWeight::get().writes(4)) } @@ -109,10 +109,10 @@ impl WeightInfo { /// Proof: `Dmp::DownwardMessageQueueHeads` (`max_values`: None, `max_size`: None, mode: `Measured`) pub(crate) fn initiate_reserve_withdraw() -> Weight { // Proof Size summary in bytes: - // Measured: `76` - // Estimated: `3541` - // Minimum execution time: 29_272_000 picoseconds. - Weight::from_parts(30_061_000, 3541) + // Measured: `109` + // Estimated: `3574` + // Minimum execution time: 28_919_000 picoseconds. + Weight::from_parts(29_703_000, 3574) .saturating_add(T::DbWeight::get().reads(4)) .saturating_add(T::DbWeight::get().writes(2)) } @@ -122,8 +122,8 @@ impl WeightInfo { // Proof Size summary in bytes: // Measured: `103` // Estimated: `3593` - // Minimum execution time: 23_112_000 picoseconds. - Weight::from_parts(23_705_000, 3593) + // Minimum execution time: 21_685_000 picoseconds. + Weight::from_parts(22_528_000, 3593) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -133,8 +133,8 @@ impl WeightInfo { // Proof Size summary in bytes: // Measured: `0` // Estimated: `3593` - // Minimum execution time: 26_077_000 picoseconds. - Weight::from_parts(26_486_000, 3593) + // Minimum execution time: 25_192_000 picoseconds. + Weight::from_parts(25_445_000, 3593) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -150,10 +150,10 @@ impl WeightInfo { /// Proof: `Dmp::DownwardMessageQueueHeads` (`max_values`: None, `max_size`: None, mode: `Measured`) pub(crate) fn deposit_reserve_asset() -> Weight { // Proof Size summary in bytes: - // Measured: `76` + // Measured: `109` // Estimated: `3593` - // Minimum execution time: 51_022_000 picoseconds. - Weight::from_parts(52_498_000, 3593) + // Minimum execution time: 49_349_000 picoseconds. + Weight::from_parts(50_476_000, 3593) .saturating_add(T::DbWeight::get().reads(5)) .saturating_add(T::DbWeight::get().writes(3)) } @@ -169,10 +169,10 @@ impl WeightInfo { /// Proof: `Dmp::DownwardMessageQueueHeads` (`max_values`: None, `max_size`: None, mode: `Measured`) pub(crate) fn initiate_teleport() -> Weight { // Proof Size summary in bytes: - // Measured: `76` + // Measured: `109` // Estimated: `3593` - // Minimum execution time: 53_062_000 picoseconds. - Weight::from_parts(54_300_000, 3593) + // Minimum execution time: 51_386_000 picoseconds. + Weight::from_parts(52_141_000, 3593) .saturating_add(T::DbWeight::get().reads(5)) .saturating_add(T::DbWeight::get().writes(3)) } From 9f8ef9411d6da49bb901f5fc0135cf819536cca8 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 4 Oct 2023 05:54:12 -0300 Subject: [PATCH 34/56] Fix benchmarks --- cumulus/primitives/utility/src/lib.rs | 8 +- .../src/fungible/benchmarking.rs | 14 +- .../src/generic/benchmarking.rs | 125 ++++++++++++++++-- polkadot/xcm/src/v3/mod.rs | 4 +- 4 files changed, 127 insertions(+), 24 deletions(-) diff --git a/cumulus/primitives/utility/src/lib.rs b/cumulus/primitives/utility/src/lib.rs index 7b3929c36488..c712dd24f590 100644 --- a/cumulus/primitives/utility/src/lib.rs +++ b/cumulus/primitives/utility/src/lib.rs @@ -31,7 +31,7 @@ use frame_support::{ use polkadot_runtime_common::xcm_sender::PriceForMessageDelivery; use sp_runtime::{traits::Saturating, SaturatedConversion}; use sp_std::{marker::PhantomData, prelude::*}; -use xcm::{latest::prelude::*, WrapVersion}; +use xcm::{latest::{prelude::*, MAX_ITEMS_IN_MULTIASSETS, MAX_INSTRUCTIONS_TO_DECODE}, WrapVersion}; use xcm_builder::TakeRevenue; use xcm_executor::traits::{MatchesFungibles, TransactAsset, WeightTrader}; @@ -574,7 +574,11 @@ impl< } // overestimate delivery fee - let overestimated_xcm = vec![ClearOrigin; 128].into(); + let mut max_assets: Vec = Vec::new(); + for i in 0..MAX_ITEMS_IN_MULTIASSETS { + max_assets.push((GeneralIndex(i as u128), 100u128).into()); + } + let overestimated_xcm = vec![WithdrawAsset(max_assets.into()); MAX_INSTRUCTIONS_TO_DECODE as usize].into(); let overestimated_fees = PriceForDelivery::price_for_delivery( (), &overestimated_xcm, diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs index 556e634e6687..83e7b8acf6d4 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs @@ -15,7 +15,7 @@ // along with Polkadot. If not, see . use super::*; -use crate::{account_and_location, new_executor, AssetTransactorOf, XcmCallOf}; +use crate::{account_and_location, new_executor, AssetTransactorOf, XcmCallOf, EnsureDelivery}; use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError, BenchmarkResult}; use frame_support::{ pallet_prelude::Get, @@ -23,7 +23,7 @@ use frame_support::{ }; use sp_runtime::traits::{Bounded, Zero}; use sp_std::{prelude::*, vec}; -use xcm::latest::prelude::*; +use xcm::latest::{prelude::*, MAX_ITEMS_IN_MULTIASSETS}; use xcm_executor::traits::{ConvertLocation, FeeReason, TransactAsset}; benchmarks_instance_pallet! { @@ -87,7 +87,6 @@ benchmarks_instance_pallet! { let dest_location = T::valid_destination()?; let dest_account = T::AccountIdConverter::convert_location(&dest_location).unwrap(); - use crate::EnsureDelivery; let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( &sender_location, &dest_location, @@ -142,16 +141,17 @@ benchmarks_instance_pallet! { initiate_reserve_withdraw { let (sender_account, sender_location) = account_and_location::(1); - let sender_account_balance_before = T::TransactAsset::balance(&sender_account); let holding = T::worst_case_holding(1); - let assets_filter = MultiAssetFilter::Definite(holding.clone()); + let assets_filter = MultiAssetFilter::Definite(holding.clone().into_inner().into_iter().take(MAX_ITEMS_IN_MULTIASSETS).collect::>().into()); let reserve = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; - use crate::EnsureDelivery; + let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( &sender_location, &reserve, - FeeReason::InitiateReserveWithdraw + FeeReason::InitiateReserveWithdraw, ); + let sender_account_balance_before = T::TransactAsset::balance(&sender_account); + let mut executor = new_executor::(sender_location); executor.set_holding(holding.into()); if let Some(expected_fees_mode) = expected_fees_mode { diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs index 4583ecdba89f..8a68eefdd053 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs @@ -15,7 +15,7 @@ // along with Polkadot. If not, see . use super::*; -use crate::{new_executor, XcmCallOf}; +use crate::{new_executor, XcmCallOf, account_and_location, AssetTransactorOf, EnsureDelivery}; use codec::Encode; use frame_benchmarking::{benchmarks, BenchmarkError}; use frame_support::dispatch::GetDispatchInfo; @@ -24,18 +24,33 @@ use xcm::{ latest::{prelude::*, MaxDispatchErrorLen, MaybeErrorCode, Weight}, DoubleEncoded, }; -use xcm_executor::{ExecutorError, FeesMode}; +use xcm_executor::{ExecutorError, FeesMode, traits::{ConvertLocation, FeeReason, TransactAsset}}; benchmarks! { report_holding { + let (sender_account, sender_location) = account_and_location::(1); let holding = T::worst_case_holding(0); + let destination = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; - let mut executor = new_executor::(Default::default()); + let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( + &sender_location, + &destination, + FeeReason::ReportHolding, + ); + let sender_account_balance_before = T::TransactAsset::balance(&sender_account); + + let mut executor = new_executor::(sender_location); executor.set_holding(holding.clone().into()); + if let Some(expected_fees_mode) = expected_fees_mode { + executor.set_fees_mode(expected_fees_mode); + } + if let Some(expected_assets_in_holding) = expected_assets_in_holding { + executor.set_holding(expected_assets_in_holding.into()); + } let instruction = Instruction::>::ReportHolding { response_info: QueryResponseInfo { - destination: T::valid_destination()?, + destination, query_id: Default::default(), max_weight: Weight::MAX, }, @@ -44,11 +59,11 @@ benchmarks! { }; let xcm = Xcm(vec![instruction]); - } : { executor.bench_process(xcm)?; } verify { - // The completion of execution above is enough to validate this is completed. + // Check we charged the delivery fees + assert!(T::TransactAsset::balance(&sender_account) <= sender_account_balance_before); } // This benchmark does not use any additional orders or instructions. This should be managed @@ -182,11 +197,26 @@ benchmarks! { } report_error { - let mut executor = new_executor::(Default::default()); - executor.set_error(Some((0u32, XcmError::Unimplemented))); + let (sender_account, sender_location) = account_and_location::(1); let query_id = Default::default(); - let destination = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; let max_weight = Default::default(); + let destination = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; + + let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( + &sender_location, + &destination, + FeeReason::ReportError, + ); + let sender_account_balance_before = T::TransactAsset::balance(&sender_account); + + let mut executor = new_executor::(sender_location); + if let Some(expected_fees_mode) = expected_fees_mode { + executor.set_fees_mode(expected_fees_mode); + } + if let Some(expected_assets_in_holding) = expected_assets_in_holding { + executor.set_holding(expected_assets_in_holding.into()); + } + executor.set_error(Some((0u32, XcmError::Unimplemented))); let instruction = Instruction::ReportError(QueryResponseInfo { query_id, destination, max_weight @@ -195,7 +225,8 @@ benchmarks! { }: { executor.bench_process(xcm)?; } verify { - // the execution succeeding is all we need to verify this xcm was successful + // Check we charged the delivery fees + assert!(T::TransactAsset::balance(&sender_account) <= sender_account_balance_before); } claim_asset { @@ -360,10 +391,18 @@ benchmarks! { } query_pallet { + let (sender_account, sender_location) = account_and_location::(1); let query_id = Default::default(); let destination = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; let max_weight = Default::default(); - let mut executor = new_executor::(Default::default()); + + let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( + &sender_location, + &destination, + FeeReason::QueryPallet, + ); + let sender_account_balance_before = T::TransactAsset::balance(&sender_account); + let mut executor = new_executor::(sender_location); let instruction = Instruction::QueryPallet { module_name: b"frame_system".to_vec(), @@ -373,6 +412,8 @@ benchmarks! { }: { executor.bench_process(xcm)?; } verify { + // Check we charged the delivery fees + assert!(T::TransactAsset::balance(&sender_account) <= sender_account_balance_before); // TODO: Potentially add new trait to XcmSender to detect a queued outgoing message. #4426 } @@ -394,11 +435,25 @@ benchmarks! { } report_transact_status { + let (sender_account, sender_location) = account_and_location::(1); let query_id = Default::default(); let destination = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; let max_weight = Default::default(); - let mut executor = new_executor::(Default::default()); + let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( + &sender_location, + &destination, + FeeReason::ReportTransactStatus, + ); + let sender_account_balance_before = T::TransactAsset::balance(&sender_account); + + let mut executor = new_executor::(sender_location); + if let Some(expected_fees_mode) = expected_fees_mode { + executor.set_fees_mode(expected_fees_mode); + } + if let Some(expected_assets_in_holding) = expected_assets_in_holding { + executor.set_holding(expected_assets_in_holding.into()); + } executor.set_transact_status(b"MyError".to_vec().into()); let instruction = Instruction::ReportTransactStatus(QueryResponseInfo { @@ -410,6 +465,8 @@ benchmarks! { }: { executor.bench_process(xcm)?; } verify { + // Check we charged the delivery fees + assert!(T::TransactAsset::balance(&sender_account) <= sender_account_balance_before); // TODO: Potentially add new trait to XcmSender to detect a queued outgoing message. #4426 } @@ -491,6 +548,15 @@ benchmarks! { let inner_xcm = Xcm(vec![ClearOrigin; x as usize]); // Get `origin`, `network` and `destination` from configured runtime. let (origin, network, destination) = T::export_message_origin_and_destination()?; + + let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( + &origin, + &destination, + FeeReason::ExportMessage, + ); + let sender_account = T::AccountIdConverter::convert_location(&origin).unwrap(); + let sender_account_balance_before = T::TransactAsset::balance(&sender_account); + let mut executor = new_executor::(origin); let xcm = Xcm(vec![ExportMessage { network, destination, xcm: inner_xcm, @@ -498,7 +564,8 @@ benchmarks! { }: { executor.bench_process(xcm)?; } verify { - // The execute completing successfully is as good as we can check. + // Check we charged the delivery fees + assert!(T::TransactAsset::balance(&sender_account) <= sender_account_balance_before); // TODO: Potentially add new trait to XcmSender to detect a queued outgoing message. #4426 } @@ -517,14 +584,30 @@ benchmarks! { lock_asset { let (unlocker, owner, asset) = T::unlockable_asset()?; + let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( + &owner, + &unlocker, + FeeReason::LockAsset, + ); + let sender_account = T::AccountIdConverter::convert_location(&owner); + let sender_account_balance_before = T::TransactAsset::balance(&sender_account); + let mut executor = new_executor::(owner); executor.set_holding(asset.clone().into()); + if let Some(expected_fees_mode) = expected_fees_mode { + executor.set_fees_mode(expected_fees_mode); + } + if let Some(expected_assets_in_holding) = expected_assets_in_holding { + executor.set_holding(expected_assets_in_holding.into()); + } let instruction = Instruction::LockAsset { asset, unlocker }; let xcm = Xcm(vec![instruction]); }: { executor.bench_process(xcm)?; } verify { + // Check delivery fees + assert!(T::TransactAsset::balance(&sender_account) <= sender_account_balance_before); // TODO: Potentially add new trait to XcmSender to detect a queued outgoing message. #4426 } @@ -595,13 +678,29 @@ benchmarks! { .enact() .map_err(|_| BenchmarkError::Skip)?; + let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( + &owner, + &locker, + FeeReason::RequestUnlock, + ); + let sender_account = T::AccountIdConverter::convert_location(&owner); + let sender_account_balance_before = T::TransactAsset::balance(&sender_account); + // ... then request for an unlock with the RequestUnlock instruction. let mut executor = new_executor::(owner); + if let Some(expected_fees_mode) = expected_fees_mode { + executor.set_fees_mode(expected_fees_mode); + } + if let Some(expected_assets_in_holding) = expected_assets_in_holding { + executor.set_holding(expected_assets_in_holding.into()); + } let instruction = Instruction::RequestUnlock { asset, locker }; let xcm = Xcm(vec![instruction]); }: { executor.bench_process(xcm)?; } verify { + // Check we charged the delivery fees + assert!(T::TransactAsset::balance(&sender_account) <= sender_account_balance_before); // TODO: Potentially add new trait to XcmSender to detect a queued outgoing message. #4426 } diff --git a/polkadot/xcm/src/v3/mod.rs b/polkadot/xcm/src/v3/mod.rs index 6f1e8e783113..a3490108e8b4 100644 --- a/polkadot/xcm/src/v3/mod.rs +++ b/polkadot/xcm/src/v3/mod.rs @@ -45,7 +45,7 @@ pub use junction::{BodyId, BodyPart, Junction, NetworkId}; pub use junctions::Junctions; pub use multiasset::{ AssetId, AssetInstance, Fungibility, MultiAsset, MultiAssetFilter, MultiAssets, - WildFungibility, WildMultiAsset, + WildFungibility, WildMultiAsset, MAX_ITEMS_IN_MULTIASSETS, }; pub use multilocation::{ Ancestor, AncestorThen, InteriorMultiLocation, MultiLocation, Parent, ParentThen, @@ -70,7 +70,7 @@ pub type QueryId = u64; #[scale_info(bounds(), skip_type_params(Call))] pub struct Xcm(pub Vec>); -const MAX_INSTRUCTIONS_TO_DECODE: u8 = 100; +pub const MAX_INSTRUCTIONS_TO_DECODE: u8 = 100; environmental::environmental!(instructions_count: u8); From 7c9c66774d745c453cb8d0200cdc9ee6c90afa30 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 4 Oct 2023 05:54:17 -0300 Subject: [PATCH 35/56] Fix benchmarks --- .../assets/asset-hub-kusama/src/lib.rs | 1 + .../assets/asset-hub-polkadot/src/lib.rs | 1 + .../assets/asset-hub-westend/src/lib.rs | 1 + .../bridge-hubs/bridge-hub-kusama/src/lib.rs | 1 + .../bridge-hub-polkadot/src/lib.rs | 1 + .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 1 + polkadot/runtime/rococo/src/lib.rs | 1 + .../src/generic/benchmarking.rs | 32 +++++++++++++------ .../pallet-xcm-benchmarks/src/generic/mod.rs | 5 +++ 9 files changed, 34 insertions(+), 10 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs index ae36572e97e8..cbabad9a4a55 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs @@ -1295,6 +1295,7 @@ impl_runtime_apis! { } impl pallet_xcm_benchmarks::generic::Config for Runtime { + type TransactAsset = Balances; type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs index c4f1cf47495c..55797c52e362 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs @@ -1171,6 +1171,7 @@ impl_runtime_apis! { } impl pallet_xcm_benchmarks::generic::Config for Runtime { + type TransactAsset = Balances; type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index 48cf1f805d5d..97b4ae986c2c 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -1347,6 +1347,7 @@ impl_runtime_apis! { } impl pallet_xcm_benchmarks::generic::Config for Runtime { + type TransactAsset = Balances; type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs index 8a7228b18856..ee45c0c61b20 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs @@ -740,6 +740,7 @@ impl_runtime_apis! { } impl pallet_xcm_benchmarks::generic::Config for Runtime { + type TransactAsset = Balances; type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs index c660a68b7469..786675063693 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs @@ -740,6 +740,7 @@ impl_runtime_apis! { } impl pallet_xcm_benchmarks::generic::Config for Runtime { + type TransactAsset = Balances; type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { 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 4c05211e7919..0052207856a3 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 @@ -1054,6 +1054,7 @@ impl_runtime_apis! { } impl pallet_xcm_benchmarks::generic::Config for Runtime { + type TransactAsset = Balances; type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index c020d4fc9a8e..56223c7ee20e 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -2017,6 +2017,7 @@ sp_api::impl_runtime_apis! { } impl pallet_xcm_benchmarks::generic::Config for Runtime { + type TransactAsset = Balances; type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs index 8a68eefdd053..fee663e89142 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs @@ -15,16 +15,16 @@ // along with Polkadot. If not, see . use super::*; -use crate::{new_executor, XcmCallOf, account_and_location, AssetTransactorOf, EnsureDelivery}; +use crate::{new_executor, XcmCallOf, account_and_location, EnsureDelivery}; use codec::Encode; use frame_benchmarking::{benchmarks, BenchmarkError}; -use frame_support::dispatch::GetDispatchInfo; +use frame_support::{dispatch::GetDispatchInfo, traits::fungible::Inspect}; use sp_std::vec; use xcm::{ latest::{prelude::*, MaxDispatchErrorLen, MaybeErrorCode, Weight}, DoubleEncoded, }; -use xcm_executor::{ExecutorError, FeesMode, traits::{ConvertLocation, FeeReason, TransactAsset}}; +use xcm_executor::{ExecutorError, FeesMode, traits::{ConvertLocation, FeeReason}}; benchmarks! { report_holding { @@ -35,7 +35,7 @@ benchmarks! { let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( &sender_location, &destination, - FeeReason::ReportHolding, + FeeReason::Report, ); let sender_account_balance_before = T::TransactAsset::balance(&sender_account); @@ -205,7 +205,7 @@ benchmarks! { let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( &sender_location, &destination, - FeeReason::ReportError, + FeeReason::Report, ); let sender_account_balance_before = T::TransactAsset::balance(&sender_account); @@ -403,6 +403,12 @@ benchmarks! { ); let sender_account_balance_before = T::TransactAsset::balance(&sender_account); let mut executor = new_executor::(sender_location); + if let Some(expected_fees_mode) = expected_fees_mode { + executor.set_fees_mode(expected_fees_mode); + } + if let Some(expected_assets_in_holding) = expected_assets_in_holding { + executor.set_holding(expected_assets_in_holding.into()); + } let instruction = Instruction::QueryPallet { module_name: b"frame_system".to_vec(), @@ -443,7 +449,7 @@ benchmarks! { let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( &sender_location, &destination, - FeeReason::ReportTransactStatus, + FeeReason::Report, ); let sender_account_balance_before = T::TransactAsset::balance(&sender_account); @@ -551,13 +557,19 @@ benchmarks! { let (expected_fees_mode, expected_assets_in_holding) = T::DeliveryHelper::ensure_successful_delivery( &origin, - &destination, - FeeReason::ExportMessage, + &destination.into(), + FeeReason::Export(network), ); let sender_account = T::AccountIdConverter::convert_location(&origin).unwrap(); let sender_account_balance_before = T::TransactAsset::balance(&sender_account); let mut executor = new_executor::(origin); + if let Some(expected_fees_mode) = expected_fees_mode { + executor.set_fees_mode(expected_fees_mode); + } + if let Some(expected_assets_in_holding) = expected_assets_in_holding { + executor.set_holding(expected_assets_in_holding.into()); + } let xcm = Xcm(vec![ExportMessage { network, destination, xcm: inner_xcm, }]); @@ -589,7 +601,7 @@ benchmarks! { &unlocker, FeeReason::LockAsset, ); - let sender_account = T::AccountIdConverter::convert_location(&owner); + let sender_account = T::AccountIdConverter::convert_location(&owner).unwrap(); let sender_account_balance_before = T::TransactAsset::balance(&sender_account); let mut executor = new_executor::(owner); @@ -683,7 +695,7 @@ benchmarks! { &locker, FeeReason::RequestUnlock, ); - let sender_account = T::AccountIdConverter::convert_location(&owner); + let sender_account = T::AccountIdConverter::convert_location(&owner).unwrap(); let sender_account_balance_before = T::TransactAsset::balance(&sender_account); // ... then request for an unlock with the RequestUnlock instruction. diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mod.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mod.rs index f207c238a39f..cbdfa8d0112c 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mod.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mod.rs @@ -38,6 +38,11 @@ pub mod pallet { + From> + Encode; + /// The type of `fungible` that is being used under the hood. + /// + /// This is useful for testing and checking. + type TransactAsset: frame_support::traits::fungible::Mutate; + /// The response which causes the most runtime weight. fn worst_case_response() -> (u64, Response); From d04933ccf03c4204c7ff8b92219963b6a8c37cc4 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 4 Oct 2023 08:26:22 -0300 Subject: [PATCH 36/56] Fix benchmarks --- polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs index 7099542dd415..e8df9b4218bd 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs @@ -40,6 +40,7 @@ frame_support::construct_runtime!( pub enum Test { System: frame_system::{Pallet, Call, Config, Storage, Event}, + Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, XcmGenericBenchmarks: generic::{Pallet}, } ); @@ -139,6 +140,7 @@ impl xcm_executor::Config for XcmConfig { impl crate::Config for Test { type XcmConfig = XcmConfig; + type TransactAsset = Balances; type AccountIdConverter = AccountIdConverter; type DeliveryHelper = (); fn valid_destination() -> Result { From d4d726ce56caa2eee6e1f2ffa90a94f029310391 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 4 Oct 2023 12:50:40 -0300 Subject: [PATCH 37/56] Fix benchmarks --- .../pallet-xcm-benchmarks/src/generic/mock.rs | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs index e8df9b4218bd..9bca66851cd5 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs @@ -23,7 +23,7 @@ use frame_support::{ traits::{Everything, OriginTrait}, weights::Weight, }; -use sp_core::H256; +use sp_core::{H256, ConstU32}; use sp_runtime::traits::{BlakeTwo256, IdentityLookup, TrailingZeroInput}; use xcm_builder::{ test_utils::{ @@ -138,9 +138,28 @@ impl xcm_executor::Config for XcmConfig { type Aliasers = Aliasers; } +parameter_types! { + pub const ExistentialDeposit: u64 = 7; +} + +impl pallet_balances::Config for Test { + type MaxLocks = (); + type MaxReserves = (); + type ReserveIdentifier = [u8; 8]; + type Balance = u64; + type DustRemoval = (); + type RuntimeEvent = RuntimeEvent; + type ExistentialDeposit = ExistentialDeposit; + type AccountStore = System; + type WeightInfo = (); + type RuntimeHoldReason = RuntimeHoldReason; + type FreezeIdentifier = (); + type MaxHolds = ConstU32<0>; + type MaxFreezes = ConstU32<0>; +} + impl crate::Config for Test { type XcmConfig = XcmConfig; - type TransactAsset = Balances; type AccountIdConverter = AccountIdConverter; type DeliveryHelper = (); fn valid_destination() -> Result { @@ -158,6 +177,7 @@ impl crate::Config for Test { } impl generic::Config for Test { + type TransactAsset = Balances; type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { @@ -190,7 +210,7 @@ impl generic::Config for Test { fn unlockable_asset() -> Result<(MultiLocation, MultiLocation, MultiAsset), BenchmarkError> { let assets: MultiAsset = (Concrete(Here.into()), 100).into(); - Ok((Default::default(), Default::default(), assets)) + Ok((Default::default(), account_id_junction::(1).into(), assets)) } fn export_message_origin_and_destination( From 13f25c345aa6593beb9da5e35eaecbd9a06a3a2e Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Fri, 6 Oct 2023 05:35:05 -0300 Subject: [PATCH 38/56] Relocate unused imports --- cumulus/primitives/utility/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cumulus/primitives/utility/src/lib.rs b/cumulus/primitives/utility/src/lib.rs index c712dd24f590..ec448cc558ad 100644 --- a/cumulus/primitives/utility/src/lib.rs +++ b/cumulus/primitives/utility/src/lib.rs @@ -31,7 +31,7 @@ use frame_support::{ use polkadot_runtime_common::xcm_sender::PriceForMessageDelivery; use sp_runtime::{traits::Saturating, SaturatedConversion}; use sp_std::{marker::PhantomData, prelude::*}; -use xcm::{latest::{prelude::*, MAX_ITEMS_IN_MULTIASSETS, MAX_INSTRUCTIONS_TO_DECODE}, WrapVersion}; +use xcm::{latest::prelude::*, WrapVersion}; use xcm_builder::TakeRevenue; use xcm_executor::traits::{MatchesFungibles, TransactAsset, WeightTrader}; @@ -563,6 +563,7 @@ impl< traits::FeeManager, FeesMode, }; + use xcm::latest::{MAX_ITEMS_IN_MULTIASSETS, MAX_INSTRUCTIONS_TO_DECODE}; let mut fees_mode = None; if !XcmConfig::FeeManager::is_waived(Some(origin_ref), fee_reason) { From e74319cd51f5e53949da87ecc722d18342504b94 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Fri, 6 Oct 2023 07:31:26 -0300 Subject: [PATCH 39/56] Fixes --- .../runtimes/collectives/collectives-polkadot/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs index be2e5d27114f..38662f0c48d1 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs @@ -45,6 +45,7 @@ pub mod fellowship; pub use ambassador::pallet_ambassador_origins; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use cumulus_primitives_core::ParaId; use fellowship::{ migration::import_kusama_fellowship, pallet_fellowship_origins, Fellows, FellowshipCollectiveInstance, @@ -424,7 +425,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { type ControllerOrigin = EitherOfDiverse, Fellows>; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; type WeightInfo = weights::cumulus_pallet_xcmp_queue::WeightInfo; - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { From 00eb9a047fea134f64a6d2814454d2c720ac8a16 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Mon, 9 Oct 2023 05:47:48 -0300 Subject: [PATCH 40/56] Another usage of NoPriceForMessageDelivery --- cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs index 5795bb643133..eb67d8fff1ab 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs @@ -34,6 +34,7 @@ use assets_common::{ AssetIdForTrustBackedAssetsConvert, MultiLocationForAssetId, }; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use cumulus_primitives_core::ParaId; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ @@ -645,7 +646,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { >; type ControllerOriginConverter = xcm_config::XcmOriginToTransactDispatchOrigin; type WeightInfo = weights::cumulus_pallet_xcmp_queue::WeightInfo; - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = NoPriceForMessageDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { From dfc06ad69954d749a46c69c929a87c52ee788914 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Mon, 9 Oct 2023 06:08:26 -0300 Subject: [PATCH 41/56] More correct type usage --- .../parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs | 4 +++- .../runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs | 4 +++- .../runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs index a7d38724739f..f12749603259 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs @@ -66,6 +66,8 @@ use assets_common::{ foreign_creators::ForeignCreators, matching::FromSiblingParachain, MultiLocationForAssetId, }; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; +use cumulus_primitives_core::ParaId; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ @@ -581,7 +583,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { EnsureXcm>, >; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = NoPriceForMessageDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs index 312587c4dbad..190987b1cfeb 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/lib.rs @@ -26,6 +26,8 @@ mod weights; pub mod xcm_config; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use cumulus_primitives_core::ParaId; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ @@ -314,7 +316,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { type ControllerOrigin = RootOrFellows; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; type WeightInfo = weights::cumulus_pallet_xcmp_queue::WeightInfo; - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = NoPriceForMessageDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs index 6b59958f4d73..dc23135f05d7 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/lib.rs @@ -26,6 +26,8 @@ mod weights; pub mod xcm_config; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use cumulus_primitives_core::ParaId; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ @@ -314,7 +316,7 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { type ControllerOrigin = RootOrFellows; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; type WeightInfo = weights::cumulus_pallet_xcmp_queue::WeightInfo; - type PriceForSiblingDelivery = (); + type PriceForSiblingDelivery = NoPriceForMessageDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { From bc54235ea55fb3484a49969a3cb6bda856a04034 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 10 Oct 2023 18:30:34 +0200 Subject: [PATCH 42/56] Fix import issues --- .../runtimes/assets/asset-hub-kusama/src/lib.rs | 1 + .../runtimes/assets/asset-hub-kusama/src/xcm_config.rs | 1 + .../runtimes/assets/asset-hub-polkadot/src/xcm_config.rs | 9 ++++++++- .../bridge-hubs/bridge-hub-kusama/src/xcm_config.rs | 9 ++++++++- .../bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs | 9 ++++++++- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs index eb67d8fff1ab..94010023b60f 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs @@ -35,6 +35,7 @@ use assets_common::{ }; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use cumulus_primitives_core::ParaId; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs index 3c198b545300..0ff3fc3cd1fe 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs @@ -35,6 +35,7 @@ use parachains_common::{ impls::ToStakingPot, xcm_config::AssetFeeAsExistentialDepositMultiplier, }; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; use sp_runtime::traits::ConvertInto; use xcm::latest::prelude::*; diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs index 9664f66981e5..1ce2c78b3103 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs @@ -16,7 +16,7 @@ use super::{ AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ForeignAssets, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - TrustBackedAssetsInstance, WeightToFee, XcmpQueue, TransactionByteFee, FeeAssetId, BaseDeliveryFee, + TrustBackedAssetsInstance, WeightToFee, XcmpQueue, CENTS, TransactionByteFee, }; use assets_common::matching::{ FromSiblingParachain, IsForeignConcreteAsset, StartsWith, StartsWithExplicitGlobalConsensus, @@ -456,6 +456,13 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +parameter_types! { + /// The asset ID for the asset that we use to pay for message delivery fees. + pub FeeAssetId: AssetId = Concrete(DotLocation::get()); + /// The base fee for the message delivery fees. + pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3); +} + pub type PriceForParentDelivery = ExponentialPrice; diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs index 7ba11bb6c7bf..339eddaa948a 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs @@ -17,7 +17,7 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, - FeeAssetId, BaseDeliveryFee, TransactionByteFee, + TransactionByteFee, CENTS, }; use frame_support::{ match_types, parameter_types, @@ -222,6 +222,13 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +parameter_types! { + /// The asset ID for the asset that we use to pay for message delivery fees. + pub FeeAssetId: AssetId = Concrete(KsmRelayLocation::get()); + /// The base fee for the message delivery fees. + pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3); +} + pub type PriceForParentDelivery = ExponentialPrice; diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs index 9430287401d5..0f12f98156aa 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs @@ -17,7 +17,7 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, - FeeAssetId, BaseDeliveryFee, TransactionByteFee, + TransactionByteFee, CENTS, }; use frame_support::{ match_types, parameter_types, @@ -226,6 +226,13 @@ impl xcm_executor::Config for XcmConfig { /// Forms the basis for local origins sending/executing XCMs. pub type LocalOriginToLocation = SignedToAccountId32; +parameter_types! { + /// The asset ID for the asset that we use to pay for message delivery fees. + pub FeeAssetId: AssetId = Concrete(DotRelayLocation::get()); + /// The base fee for the message delivery fees. + pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3); +} + pub type PriceForParentDelivery = ExponentialPrice; From a0a48af2e756f18b7891140b386b1f91f36c61ee Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 10 Oct 2023 18:39:26 +0200 Subject: [PATCH 43/56] Format features --- .../emulated/assets/asset-hub-westend/Cargo.toml | 2 +- .../parachains/runtimes/testing/rococo-parachain/Cargo.toml | 4 ++-- cumulus/primitives/utility/Cargo.toml | 4 ++-- polkadot/runtime/common/Cargo.toml | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/Cargo.toml b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/Cargo.toml index 120684571463..f4f75dd31c54 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/Cargo.toml +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/Cargo.toml @@ -61,7 +61,7 @@ runtime-benchmarks = [ "polkadot-runtime-common/runtime-benchmarks", "polkadot-runtime-parachains/runtime-benchmarks", "sp-runtime/runtime-benchmarks", - "xcm-executor/runtime-benchmarks", "westend-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", + "xcm-executor/runtime-benchmarks", ] diff --git a/cumulus/parachains/runtimes/testing/rococo-parachain/Cargo.toml b/cumulus/parachains/runtimes/testing/rococo-parachain/Cargo.toml index 28b92ccf99b6..a662a5e80665 100644 --- a/cumulus/parachains/runtimes/testing/rococo-parachain/Cargo.toml +++ b/cumulus/parachains/runtimes/testing/rococo-parachain/Cargo.toml @@ -87,6 +87,7 @@ std = [ "parachain-info/std", "parachains-common/std", "polkadot-parachain-primitives/std", + "polkadot-runtime-common/std", "scale-info/std", "sp-api/std", "sp-block-builder/std", @@ -104,7 +105,6 @@ std = [ "xcm-builder/std", "xcm-executor/std", "xcm/std", - "polkadot-runtime-common/std", ] runtime-benchmarks = [ "cumulus-pallet-parachain-system/runtime-benchmarks", @@ -120,10 +120,10 @@ runtime-benchmarks = [ "pallet-xcm/runtime-benchmarks", "parachains-common/runtime-benchmarks", "polkadot-parachain-primitives/runtime-benchmarks", + "polkadot-runtime-common/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", - "polkadot-runtime-common/runtime-benchmarks", ] experimental = [ "pallet-aura/experimental" ] diff --git a/cumulus/primitives/utility/Cargo.toml b/cumulus/primitives/utility/Cargo.toml index 406483682703..691a4599b2c4 100644 --- a/cumulus/primitives/utility/Cargo.toml +++ b/cumulus/primitives/utility/Cargo.toml @@ -31,6 +31,7 @@ std = [ "codec/std", "cumulus-primitives-core/std", "frame-support/std", + "pallet-xcm-benchmarks/std", "polkadot-runtime-common/std", "polkadot-runtime-parachains/std", "sp-io/std", @@ -39,15 +40,14 @@ std = [ "xcm-builder/std", "xcm-executor/std", "xcm/std", - "pallet-xcm-benchmarks/std", ] runtime-benchmarks = [ "frame-support/runtime-benchmarks", + "pallet-xcm-benchmarks/runtime-benchmarks", "polkadot-runtime-common/runtime-benchmarks", "polkadot-runtime-parachains/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", - "pallet-xcm-benchmarks/runtime-benchmarks", ] diff --git a/polkadot/runtime/common/Cargo.toml b/polkadot/runtime/common/Cargo.toml index 3abcfec4b6a0..99f8345dd4e9 100644 --- a/polkadot/runtime/common/Cargo.toml +++ b/polkadot/runtime/common/Cargo.toml @@ -107,8 +107,8 @@ std = [ "sp-session/std", "sp-staking/std", "sp-std/std", - "xcm-executor/std", "xcm-builder/std", + "xcm-executor/std", "xcm/std", ] runtime-benchmarks = [ @@ -132,8 +132,8 @@ runtime-benchmarks = [ "runtime-parachains/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "sp-staking/runtime-benchmarks", - "xcm-executor/runtime-benchmarks", "xcm-builder/runtime-benchmarks", + "xcm-executor/runtime-benchmarks", ] try-runtime = [ "frame-election-provider-support/try-runtime", From f51af537f7e0813557edd9c801763999b9683aa5 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 10 Oct 2023 18:55:19 +0200 Subject: [PATCH 44/56] Fix stuff --- .../parachains/runtimes/assets/asset-hub-kusama/src/lib.rs | 7 +++++++ polkadot/runtime/westend/src/lib.rs | 1 + 2 files changed, 8 insertions(+) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs index 94010023b60f..5c51a3a52324 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs @@ -1213,6 +1213,13 @@ impl_runtime_apis! { use xcm_config::{KsmLocation, MaxAssetsIntoHolding}; use pallet_xcm_benchmarks::asset_instance_from; + parameter_types! { + pub ExistentialDepositMultiAsset: Option = Some(( + KsmLocation::get(), + ExistentialDeposit::get() + ).into()); + } + impl pallet_xcm_benchmarks::Config for Runtime { type XcmConfig = xcm_config::XcmConfig; type AccountIdConverter = xcm_config::LocationToAccountId; diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 98eb22aba946..94c3e0df9f25 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -2230,6 +2230,7 @@ sp_api::impl_runtime_apis! { } impl pallet_xcm_benchmarks::generic::Config for Runtime { + type TransactAsset = Balances; type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { From 2dfeb40ed512c359a0e5430396e61488fd4c3d53 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 10 Oct 2023 19:03:43 +0200 Subject: [PATCH 45/56] Fix doc error --- polkadot/runtime/common/src/xcm_sender.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polkadot/runtime/common/src/xcm_sender.rs b/polkadot/runtime/common/src/xcm_sender.rs index 1bd9a1ffdaa7..b56452550a42 100644 --- a/polkadot/runtime/common/src/xcm_sender.rs +++ b/polkadot/runtime/common/src/xcm_sender.rs @@ -54,7 +54,7 @@ impl PriceForMessageDelivery for NoPriceForMessageDelivery { } } -/// Implementation of [`PriceForParachainDelivery`] which returns a fixed price. +/// Implementation of [`PriceForMessageDelivery`] which returns a fixed price. pub struct ConstantPrice(sp_std::marker::PhantomData); impl> PriceForMessageDelivery for ConstantPrice { type Id = (); From 8a4c839817dcf3fb09365649d8af43879990debf Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 10 Oct 2023 19:15:32 +0200 Subject: [PATCH 46/56] Fix docs --- cumulus/pallets/parachain-system/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 758d2edaacdd..ecb90cdcdbcb 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -178,7 +178,7 @@ where check_version: bool, } -mod ump_constants { +pub mod ump_constants { use super::FixedU128; /// `host_config.max_upward_queue_size / THRESHOLD_FACTOR` is the threshold after which delivery @@ -884,7 +884,7 @@ pub mod pallet { /// Upward messages that are still pending and not yet send to the relay chain. #[pallet::storage] - pub(super) type PendingUpwardMessages = + pub type PendingUpwardMessages = StorageValue<_, Vec, ValueQuery>; /// Initialization value for the delivery fee factor for UMP. @@ -1530,7 +1530,7 @@ impl frame_system::SetCode for ParachainSetCode { impl Pallet { /// Puts a message in the [`PendingUpwardMessages`] storage item. - /// The message will be later sent in [`on_finalize`]. + /// The message will be later sent in `on_finalize`. /// Checks host configuration to see if message is too big. /// Increases the delivery fee factor if the queue is sufficiently (see [`ump_constants::THRESHOLD_FACTOR`]) congested. pub fn send_upward_message(message: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError> { From 555f2e24b81a9b8d7782e8a70ad8ff92dbbb7e1b Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 11 Oct 2023 10:36:02 +0200 Subject: [PATCH 47/56] Remove links to non-public items --- cumulus/pallets/parachain-system/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index ecb90cdcdbcb..94528e679a63 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -255,7 +255,7 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { - /// Handles actually sending upward messages by moving them from [`PendingUpwardMessages`] to [`UpwardMessages`]. + /// Handles actually sending upward messages by moving them from `PendingUpwardMessages` to `UpwardMessages`. /// Decreases the delivery fee factor if after sending messages, the queue total size is less than the threshold /// (see [`ump_constants::THRESHOLD_FACTOR`]). fn on_finalize(_: BlockNumberFor) { @@ -884,7 +884,7 @@ pub mod pallet { /// Upward messages that are still pending and not yet send to the relay chain. #[pallet::storage] - pub type PendingUpwardMessages = + pub(super) type PendingUpwardMessages = StorageValue<_, Vec, ValueQuery>; /// Initialization value for the delivery fee factor for UMP. @@ -1529,7 +1529,7 @@ impl frame_system::SetCode for ParachainSetCode { } impl Pallet { - /// Puts a message in the [`PendingUpwardMessages`] storage item. + /// Puts a message in the `PendingUpwardMessages` storage item. /// The message will be later sent in `on_finalize`. /// Checks host configuration to see if message is too big. /// Increases the delivery fee factor if the queue is sufficiently (see [`ump_constants::THRESHOLD_FACTOR`]) congested. From 985d52d9570e8d87030176aafdd4f7fabc6f7af4 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 11 Oct 2023 11:10:08 +0200 Subject: [PATCH 48/56] Fmt --- cumulus/pallets/parachain-system/src/lib.rs | 25 +++++++----- cumulus/pallets/xcmp-queue/src/mock.rs | 2 +- .../src/tests/reserve_transfer.rs | 22 ++++------ .../asset-hub-westend/src/tests/teleport.rs | 40 ++++++------------- .../assets/asset-hub-kusama/src/xcm_config.rs | 9 ++--- .../assets/asset-hub-polkadot/src/lib.rs | 2 +- .../asset-hub-polkadot/src/xcm_config.rs | 9 ++--- .../asset-hub-westend/src/xcm_config.rs | 9 +++-- .../assets/test-utils/src/test_cases.rs | 17 ++++---- .../assets/test-utils/src/xcm_helpers.rs | 8 +++- .../bridge-hub-kusama/src/xcm_config.rs | 11 ++--- .../bridge-hub-polkadot/src/xcm_config.rs | 11 ++--- .../bridge-hub-rococo/src/xcm_config.rs | 10 ++--- .../collectives-polkadot/src/lib.rs | 3 +- .../collectives-polkadot/src/xcm_config.rs | 13 +++--- .../contracts-rococo/src/xcm_config.rs | 5 +-- .../runtimes/testing/penpal/src/lib.rs | 2 +- .../testing/rococo-parachain/src/lib.rs | 4 +- cumulus/primitives/utility/src/lib.rs | 33 ++++----------- polkadot/runtime/common/src/xcm_sender.rs | 14 +++---- .../src/fungible/benchmarking.rs | 2 +- .../src/generic/benchmarking.rs | 7 +++- .../pallet-xcm-benchmarks/src/generic/mock.rs | 2 +- .../procedural/src/pallet/expand/warnings.rs | 4 +- 24 files changed, 109 insertions(+), 155 deletions(-) diff --git a/cumulus/pallets/parachain-system/src/lib.rs b/cumulus/pallets/parachain-system/src/lib.rs index 94528e679a63..3b1d0525d15f 100644 --- a/cumulus/pallets/parachain-system/src/lib.rs +++ b/cumulus/pallets/parachain-system/src/lib.rs @@ -255,9 +255,9 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { - /// Handles actually sending upward messages by moving them from `PendingUpwardMessages` to `UpwardMessages`. - /// Decreases the delivery fee factor if after sending messages, the queue total size is less than the threshold - /// (see [`ump_constants::THRESHOLD_FACTOR`]). + /// Handles actually sending upward messages by moving them from `PendingUpwardMessages` to + /// `UpwardMessages`. Decreases the delivery fee factor if after sending messages, the queue + /// total size is less than the threshold (see [`ump_constants::THRESHOLD_FACTOR`]). fn on_finalize(_: BlockNumberFor) { >::kill(); >::kill(); @@ -347,7 +347,9 @@ pub mod pallet { // If the total size of the pending messages is less than the threshold, // we decrease the fee factor, since the queue is less congested. // This makes delivery of new messages cheaper. - let threshold = host_config.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); + let threshold = host_config + .max_upward_queue_size + .saturating_div(ump_constants::THRESHOLD_FACTOR); let remaining_total_size: usize = up.iter().map(UpwardMessage::len).sum(); if remaining_total_size <= threshold as usize { Self::decrease_fee_factor(()); @@ -1023,7 +1025,9 @@ impl FeeTracker for Pallet { fn increase_fee_factor(_: Self::Id, message_size_factor: FixedU128) -> FixedU128 { >::mutate(|f| { - *f = f.saturating_mul(ump_constants::EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor)); + *f = f.saturating_mul( + ump_constants::EXPONENTIAL_FEE_BASE.saturating_add(message_size_factor), + ); *f }) } @@ -1532,7 +1536,8 @@ impl Pallet { /// Puts a message in the `PendingUpwardMessages` storage item. /// The message will be later sent in `on_finalize`. /// Checks host configuration to see if message is too big. - /// Increases the delivery fee factor if the queue is sufficiently (see [`ump_constants::THRESHOLD_FACTOR`]) congested. + /// Increases the delivery fee factor if the queue is sufficiently (see + /// [`ump_constants::THRESHOLD_FACTOR`]) congested. pub fn send_upward_message(message: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError> { let message_len = message.len(); // Check if the message fits into the relay-chain constraints. @@ -1551,16 +1556,16 @@ impl Pallet { if message_len > cfg.max_upward_message_size as usize { return Err(MessageSendError::TooBig) } - let threshold = cfg.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); + let threshold = + cfg.max_upward_queue_size.saturating_div(ump_constants::THRESHOLD_FACTOR); // We check the threshold against total size and not number of messages since messages // could be big or small. let pending_messages = PendingUpwardMessages::::get(); let total_size: usize = pending_messages.iter().map(UpwardMessage::len).sum(); if total_size > threshold as usize { // We increase the fee factor by a factor based on the new message's size in KB - let message_size_factor = - FixedU128::from((message_len / 1024) as u128) - .saturating_mul(ump_constants::MESSAGE_SIZE_FEE_BASE); + let message_size_factor = FixedU128::from((message_len / 1024) as u128) + .saturating_mul(ump_constants::MESSAGE_SIZE_FEE_BASE); Self::increase_fee_factor((), message_size_factor); } } else { diff --git a/cumulus/pallets/xcmp-queue/src/mock.rs b/cumulus/pallets/xcmp-queue/src/mock.rs index e716a45e1c85..e7ae1a68703e 100644 --- a/cumulus/pallets/xcmp-queue/src/mock.rs +++ b/cumulus/pallets/xcmp-queue/src/mock.rs @@ -23,6 +23,7 @@ use frame_support::{ traits::{ConstU32, Everything, Nothing, OriginTrait}, }; use frame_system::EnsureRoot; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use sp_core::H256; use sp_runtime::{ traits::{BlakeTwo256, IdentityLookup}, @@ -31,7 +32,6 @@ use sp_runtime::{ use xcm::prelude::*; use xcm_builder::{CurrencyAdapter, FixedWeightBounds, IsConcrete, NativeAsset, ParentIsPreset}; use xcm_executor::traits::ConvertOrigin; -use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; type Block = frame_system::mocking::MockBlock; diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/reserve_transfer.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/reserve_transfer.rs index 5d2298659a47..bacf46df9122 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/reserve_transfer.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/reserve_transfer.rs @@ -14,8 +14,8 @@ // limitations under the License. use crate::*; -use westend_runtime::xcm_config::XcmConfig as WestendXcmConfig; use asset_hub_westend_runtime::xcm_config::XcmConfig; +use westend_runtime::xcm_config::XcmConfig as WestendXcmConfig; fn relay_origin_assertions(t: RelayToSystemParaTest) { type RuntimeEvent = ::RuntimeEvent; @@ -182,13 +182,9 @@ fn limited_reserve_transfer_native_asset_from_relay_to_system_para_fails() { test.assert(); let delivery_fees = Westend::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( - test.args.assets.clone(), - 0, - test.args.weight_limit, - test.args.beneficiary, - test.args.dest, - ) + xcm_helpers::transfer_assets_delivery_fees::< + ::XcmSender, + >(test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest) }); let sender_balance_after = test.sender.balance; @@ -251,13 +247,9 @@ fn reserve_transfer_native_asset_from_relay_to_system_para_fails() { test.assert(); let delivery_fees = Westend::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( - test.args.assets.clone(), - 0, - test.args.weight_limit, - test.args.beneficiary, - test.args.dest, - ) + xcm_helpers::transfer_assets_delivery_fees::< + ::XcmSender, + >(test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest) }); let sender_balance_after = test.sender.balance; diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs index 2da973437fbe..1d61111904cd 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs @@ -176,13 +176,9 @@ fn limited_teleport_native_assets_from_relay_to_system_para_works() { test.assert(); let delivery_fees = Westend::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( - test.args.assets.clone(), - 0, - test.args.weight_limit, - test.args.beneficiary, - test.args.dest, - ) + xcm_helpers::transfer_assets_delivery_fees::< + ::XcmSender, + >(test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest) }); let sender_balance_after = test.sender.balance; @@ -227,13 +223,9 @@ fn limited_teleport_native_assets_back_from_system_para_to_relay_works() { let receiver_balance_after = test.receiver.balance; let delivery_fees = AssetHubWestend::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( - test.args.assets.clone(), - 0, - test.args.weight_limit, - test.args.beneficiary, - test.args.dest, - ) + xcm_helpers::transfer_assets_delivery_fees::< + ::XcmSender, + >(test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest) }); // Sender's balance is reduced @@ -272,13 +264,9 @@ fn limited_teleport_native_assets_from_system_para_to_relay_fails() { let receiver_balance_after = test.receiver.balance; let delivery_fees = AssetHubWestend::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( - test.args.assets.clone(), - 0, - test.args.weight_limit, - test.args.beneficiary, - test.args.dest, - ) + xcm_helpers::transfer_assets_delivery_fees::< + ::XcmSender, + >(test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest) }); // Sender's balance is reduced @@ -309,13 +297,9 @@ fn teleport_native_assets_from_relay_to_system_para_works() { test.assert(); let delivery_fees = Westend::execute_with(|| { - xcm_helpers::transfer_assets_delivery_fees::<::XcmSender>( - test.args.assets.clone(), - 0, - test.args.weight_limit, - test.args.beneficiary, - test.args.dest, - ) + xcm_helpers::transfer_assets_delivery_fees::< + ::XcmSender, + >(test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest) }); let sender_balance_after = test.sender.balance; diff --git a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs index 0ff3fc3cd1fe..c53db960cc81 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/xcm_config.rs @@ -16,7 +16,7 @@ use super::{ AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, PoolAssets, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - TrustBackedAssetsInstance, WeightToFee, XcmpQueue, TransactionByteFee, + TransactionByteFee, TrustBackedAssetsInstance, WeightToFee, XcmpQueue, }; use crate::{ForeignAssets, CENTS}; use assets_common::{ @@ -31,12 +31,9 @@ use frame_support::{ }; use frame_system::EnsureRoot; use pallet_xcm::XcmPassthrough; -use parachains_common::{ - impls::ToStakingPot, - xcm_config::AssetFeeAsExistentialDepositMultiplier, -}; -use polkadot_runtime_common::xcm_sender::ExponentialPrice; +use parachains_common::{impls::ToStakingPot, xcm_config::AssetFeeAsExistentialDepositMultiplier}; use polkadot_parachain_primitives::primitives::Sibling; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use sp_runtime::traits::ConvertInto; use xcm::latest::prelude::*; use xcm_builder::{ diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs index f12749603259..b5d800c92015 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs @@ -66,8 +66,8 @@ use assets_common::{ foreign_creators::ForeignCreators, matching::FromSiblingParachain, MultiLocationForAssetId, }; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; -use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use cumulus_primitives_core::ParaId; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs index 1ce2c78b3103..92c17ed872c2 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/xcm_config.rs @@ -16,7 +16,7 @@ use super::{ AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ForeignAssets, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - TrustBackedAssetsInstance, WeightToFee, XcmpQueue, CENTS, TransactionByteFee, + TransactionByteFee, TrustBackedAssetsInstance, WeightToFee, XcmpQueue, CENTS, }; use assets_common::matching::{ FromSiblingParachain, IsForeignConcreteAsset, StartsWith, StartsWithExplicitGlobalConsensus, @@ -27,12 +27,9 @@ use frame_support::{ }; use frame_system::EnsureRoot; use pallet_xcm::XcmPassthrough; -use parachains_common::{ - impls::ToStakingPot, - xcm_config::AssetFeeAsExistentialDepositMultiplier, -}; -use polkadot_runtime_common::xcm_sender::ExponentialPrice; +use parachains_common::{impls::ToStakingPot, xcm_config::AssetFeeAsExistentialDepositMultiplier}; use polkadot_parachain_primitives::primitives::Sibling; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use sp_runtime::traits::ConvertInto; use xcm::latest::prelude::*; use xcm_builder::{ diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs index 05dba45f5c51..5ba1a2cc2b12 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs @@ -14,9 +14,10 @@ // limitations under the License. use super::{ - AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ParachainInfo, - ParachainSystem, PolkadotXcm, PoolAssets, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - TrustBackedAssetsInstance, WeightToFee, XcmpQueue, FeeAssetId, BaseDeliveryFee, TransactionByteFee, + AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, BaseDeliveryFee, + FeeAssetId, ParachainInfo, ParachainSystem, PolkadotXcm, PoolAssets, Runtime, RuntimeCall, + RuntimeEvent, RuntimeOrigin, TransactionByteFee, TrustBackedAssetsInstance, WeightToFee, + XcmpQueue, }; use crate::ForeignAssets; use assets_common::{ @@ -36,8 +37,8 @@ use parachains_common::{ xcm_config::{AssetFeeAsExistentialDepositMultiplier, RelayOrOtherSystemParachains}, TREASURY_PALLET_ID, }; -use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use sp_runtime::traits::{AccountIdConversion, ConvertInto}; use westend_runtime_constants::system_parachain; use xcm::latest::prelude::*; diff --git a/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs b/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs index e8e525535f64..215d29185349 100644 --- a/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs +++ b/cumulus/parachains/runtimes/assets/test-utils/src/test_cases.rs @@ -178,15 +178,14 @@ pub fn teleports_for_native_asset_works< ); // Mint funds into account to ensure it has enough balance to pay delivery fees - let delivery_fees = xcm_helpers::transfer_assets_delivery_fees::< - XcmConfig::XcmSender, - >( - (native_asset_id, native_asset_to_teleport_away.into()).into(), - 0, - Unlimited, - dest_beneficiary, - dest, - ); + let delivery_fees = + xcm_helpers::transfer_assets_delivery_fees::( + (native_asset_id, native_asset_to_teleport_away.into()).into(), + 0, + Unlimited, + dest_beneficiary, + dest, + ); >::mint_into( &target_account, delivery_fees.into(), diff --git a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs index f036f9283dc3..cda7a64ed5bc 100644 --- a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs +++ b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs @@ -30,9 +30,13 @@ pub fn transfer_assets_delivery_fees( destination: MultiLocation, ) -> u128 { let message = teleport_assets_dummy_message(assets, fee_asset_item, weight_limit, beneficiary); - let Ok((_, delivery_fees)) = validate_send::(destination, message) else { unreachable!("message can be sent; qed") }; + let Ok((_, delivery_fees)) = validate_send::(destination, message) else { + unreachable!("message can be sent; qed") + }; if let Some(delivery_fee) = delivery_fees.inner().first() { - let Fungible(delivery_fee_amount) = delivery_fee.fun else { unreachable!("asset is fungible; qed"); }; + let Fungible(delivery_fee_amount) = delivery_fee.fun else { + unreachable!("asset is fungible; qed"); + }; delivery_fee_amount } else { 0 diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs index 339eddaa948a..addc2fc5c3d5 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/xcm_config.rs @@ -16,8 +16,8 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, - Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, - TransactionByteFee, CENTS, + Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, TransactionByteFee, WeightToFee, XcmpQueue, + CENTS, }; use frame_support::{ match_types, parameter_types, @@ -25,12 +25,9 @@ use frame_support::{ }; use frame_system::EnsureRoot; use pallet_xcm::XcmPassthrough; -use parachains_common::{ - impls::ToStakingPot, - xcm_config::ConcreteNativeAssetFrom, -}; -use polkadot_runtime_common::xcm_sender::ExponentialPrice; +use parachains_common::{impls::ToStakingPot, xcm_config::ConcreteNativeAssetFrom}; use polkadot_parachain_primitives::primitives::Sibling; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs index 0f12f98156aa..6d1ffb193eb3 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/xcm_config.rs @@ -16,8 +16,8 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, - Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, - TransactionByteFee, CENTS, + Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, TransactionByteFee, WeightToFee, XcmpQueue, + CENTS, }; use frame_support::{ match_types, parameter_types, @@ -25,12 +25,9 @@ use frame_support::{ }; use frame_system::EnsureRoot; use pallet_xcm::XcmPassthrough; -use parachains_common::{ - impls::ToStakingPot, - xcm_config::ConcreteNativeAssetFrom, -}; -use polkadot_runtime_common::xcm_sender::ExponentialPrice; +use parachains_common::{impls::ToStakingPot, xcm_config::ConcreteNativeAssetFrom}; use polkadot_parachain_primitives::primitives::Sibling; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs index dec2bc7e2b70..e392ca056d0e 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs @@ -15,10 +15,10 @@ // along with Cumulus. If not, see . use super::{ - AccountId, AllPalletsWithSystem, Balances, BridgeGrandpaRococoInstance, - BridgeGrandpaWococoInstance, DeliveryRewardInBalance, ParachainInfo, ParachainSystem, - PolkadotXcm, RequiredStakeForStakeAndSlash, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - WeightToFee, XcmpQueue, FeeAssetId, BaseDeliveryFee, TransactionByteFee, + AccountId, AllPalletsWithSystem, Balances, BaseDeliveryFee, BridgeGrandpaRococoInstance, + BridgeGrandpaWococoInstance, DeliveryRewardInBalance, FeeAssetId, ParachainInfo, + ParachainSystem, PolkadotXcm, RequiredStakeForStakeAndSlash, Runtime, RuntimeCall, + RuntimeEvent, RuntimeOrigin, TransactionByteFee, WeightToFee, XcmpQueue, }; use crate::{ bridge_hub_rococo_config::ToBridgeHubWococoHaulBlobExporter, @@ -35,8 +35,8 @@ use parachains_common::{ xcm_config::{ConcreteNativeAssetFrom, RelayOrOtherSystemParachains}, TREASURY_PALLET_ID, }; -use polkadot_runtime_common::xcm_sender::ExponentialPrice; use polkadot_parachain_primitives::primitives::Sibling; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use rococo_runtime_constants::system_parachain::SystemParachains; use sp_core::Get; use sp_runtime::traits::AccountIdConversion; diff --git a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs index 38662f0c48d1..cec4152bcc31 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/lib.rs @@ -425,7 +425,8 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { type ControllerOrigin = EitherOfDiverse, Fellows>; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; type WeightInfo = weights::cumulus_pallet_xcmp_queue::WeightInfo; - type PriceForSiblingDelivery = polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; + type PriceForSiblingDelivery = + polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; } impl cumulus_pallet_dmp_queue::Config for Runtime { diff --git a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs index 17ff75278407..9cf7ee4fe243 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-polkadot/src/xcm_config.rs @@ -14,9 +14,9 @@ // limitations under the License. use super::{ - AccountId, AllPalletsWithSystem, Balances, Fellows, ParachainInfo, ParachainSystem, - PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, - FeeAssetId, BaseDeliveryFee, TransactionByteFee, + AccountId, AllPalletsWithSystem, Balances, BaseDeliveryFee, FeeAssetId, Fellows, ParachainInfo, + ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, + TransactionByteFee, WeightToFee, XcmpQueue, }; use frame_support::{ match_types, parameter_types, @@ -25,12 +25,9 @@ use frame_support::{ }; use frame_system::EnsureRoot; use pallet_xcm::XcmPassthrough; -use parachains_common::{ - impls::ToStakingPot, - xcm_config::ConcreteNativeAssetFrom, -}; -use polkadot_runtime_common::xcm_sender::ExponentialPrice; +use parachains_common::{impls::ToStakingPot, xcm_config::ConcreteNativeAssetFrom}; use polkadot_parachain_primitives::primitives::Sibling; +use polkadot_runtime_common::xcm_sender::ExponentialPrice; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, diff --git a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs index f87d46a09951..52d72ba84e0a 100644 --- a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/xcm_config.rs @@ -15,9 +15,9 @@ use super::{ AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, - Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue, - TransactionByteFee, + Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, TransactionByteFee, WeightToFee, XcmpQueue, }; +use crate::common::rococo::currency::CENTS; use frame_support::{ match_types, parameter_types, traits::{ConstU32, EitherOfDiverse, Everything, Nothing}, @@ -41,7 +41,6 @@ use xcm_builder::{ WithComputedOrigin, WithUniqueTopic, XcmFeesToAccount, }; use xcm_executor::XcmExecutor; -use crate::common::rococo::currency::CENTS; parameter_types! { pub const RelayLocation: MultiLocation = MultiLocation::parent(); diff --git a/cumulus/parachains/runtimes/testing/penpal/src/lib.rs b/cumulus/parachains/runtimes/testing/penpal/src/lib.rs index 9d7b5c4411c1..86389425eb5e 100644 --- a/cumulus/parachains/runtimes/testing/penpal/src/lib.rs +++ b/cumulus/parachains/runtimes/testing/penpal/src/lib.rs @@ -51,6 +51,7 @@ use frame_system::{ limits::{BlockLength, BlockWeights}, EnsureRoot, EnsureSigned, }; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use smallvec::smallvec; use sp_api::impl_runtime_apis; pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; @@ -67,7 +68,6 @@ use sp_std::prelude::*; use sp_version::NativeVersion; use sp_version::RuntimeVersion; use xcm_config::{AssetsToBlockAuthor, XcmConfig, XcmOriginToTransactDispatchOrigin}; -use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; #[cfg(any(feature = "std", test))] pub use sp_runtime::BuildStorage; diff --git a/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs b/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs index 7c82787e0d05..dcea349f3a0e 100644 --- a/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs +++ b/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs @@ -23,6 +23,8 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use cumulus_primitives_core::ParaId; +use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use sp_api::impl_runtime_apis; use sp_core::OpaqueMetadata; use sp_runtime::{ @@ -35,8 +37,6 @@ use sp_std::prelude::*; #[cfg(feature = "std")] use sp_version::NativeVersion; use sp_version::RuntimeVersion; -use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; -use cumulus_primitives_core::ParaId; // A few exports that help ease life for downstream crates. pub use frame_support::{ diff --git a/cumulus/primitives/utility/src/lib.rs b/cumulus/primitives/utility/src/lib.rs index ec448cc558ad..c4ce67194855 100644 --- a/cumulus/primitives/utility/src/lib.rs +++ b/cumulus/primitives/utility/src/lib.rs @@ -530,16 +530,8 @@ mod tests { /// Deposits estimated fee to the origin account (if needed). /// Allows to trigger additional logic for specific `ParaId` (e.g. open HRMP channel) (if neeeded). #[cfg(feature = "runtime-benchmarks")] -pub struct ToParentDeliveryHelper< - XcmConfig, - ExistentialDeposit, - PriceForDelivery, ->( - sp_std::marker::PhantomData<( - XcmConfig, - ExistentialDeposit, - PriceForDelivery, - )>, +pub struct ToParentDeliveryHelper( + sp_std::marker::PhantomData<(XcmConfig, ExistentialDeposit, PriceForDelivery)>, ); #[cfg(feature = "runtime-benchmarks")] @@ -548,22 +540,15 @@ impl< ExistentialDeposit: Get>, PriceForDelivery: PriceForMessageDelivery, > pallet_xcm_benchmarks::EnsureDelivery - for ToParentDeliveryHelper< - XcmConfig, - ExistentialDeposit, - PriceForDelivery, - > + for ToParentDeliveryHelper { fn ensure_successful_delivery( origin_ref: &MultiLocation, _dest: &MultiLocation, fee_reason: xcm_executor::traits::FeeReason, ) -> (Option, Option) { - use xcm_executor::{ - traits::FeeManager, - FeesMode, - }; - use xcm::latest::{MAX_ITEMS_IN_MULTIASSETS, MAX_INSTRUCTIONS_TO_DECODE}; + use xcm::latest::{MAX_INSTRUCTIONS_TO_DECODE, MAX_ITEMS_IN_MULTIASSETS}; + use xcm_executor::{traits::FeeManager, FeesMode}; let mut fees_mode = None; if !XcmConfig::FeeManager::is_waived(Some(origin_ref), fee_reason) { @@ -579,11 +564,9 @@ impl< for i in 0..MAX_ITEMS_IN_MULTIASSETS { max_assets.push((GeneralIndex(i as u128), 100u128).into()); } - let overestimated_xcm = vec![WithdrawAsset(max_assets.into()); MAX_INSTRUCTIONS_TO_DECODE as usize].into(); - let overestimated_fees = PriceForDelivery::price_for_delivery( - (), - &overestimated_xcm, - ); + let overestimated_xcm = + vec![WithdrawAsset(max_assets.into()); MAX_INSTRUCTIONS_TO_DECODE as usize].into(); + let overestimated_fees = PriceForDelivery::price_for_delivery((), &overestimated_xcm); // mint overestimated fee to origin for fee in overestimated_fees.inner() { diff --git a/polkadot/runtime/common/src/xcm_sender.rs b/polkadot/runtime/common/src/xcm_sender.rs index b56452550a42..4d31c92cdd3a 100644 --- a/polkadot/runtime/common/src/xcm_sender.rs +++ b/polkadot/runtime/common/src/xcm_sender.rs @@ -95,10 +95,10 @@ impl, B: Get, M: Get, F: FeeTracker> PriceForMessage /// XCM sender for relay chain. It only sends downward message. pub struct ChildParachainRouter(PhantomData<(T, W, P)>); -impl - SendXcm for ChildParachainRouter - where - P: PriceForMessageDelivery +impl SendXcm + for ChildParachainRouter +where + P: PriceForMessageDelivery, { type Ticket = (HostConfiguration>, ParaId, Vec); @@ -194,10 +194,8 @@ impl< // overestimate delivery fee let overestimated_xcm = vec![ClearOrigin; 128].into(); - let overestimated_fees = PriceForDelivery::price_for_delivery( - Parachain::get(), - &overestimated_xcm, - ); + let overestimated_fees = + PriceForDelivery::price_for_delivery(Parachain::get(), &overestimated_xcm); // mint overestimated fee to origin for fee in overestimated_fees.inner() { diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs index 699567019520..d32eb8d4a52f 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/fungible/benchmarking.rs @@ -15,7 +15,7 @@ // along with Polkadot. If not, see . use super::*; -use crate::{account_and_location, new_executor, AssetTransactorOf, XcmCallOf, EnsureDelivery}; +use crate::{account_and_location, new_executor, AssetTransactorOf, EnsureDelivery, XcmCallOf}; use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError, BenchmarkResult}; use frame_support::{ pallet_prelude::Get, diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs index fee663e89142..c6b76e0ffade 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs @@ -15,7 +15,7 @@ // along with Polkadot. If not, see . use super::*; -use crate::{new_executor, XcmCallOf, account_and_location, EnsureDelivery}; +use crate::{account_and_location, new_executor, EnsureDelivery, XcmCallOf}; use codec::Encode; use frame_benchmarking::{benchmarks, BenchmarkError}; use frame_support::{dispatch::GetDispatchInfo, traits::fungible::Inspect}; @@ -24,7 +24,10 @@ use xcm::{ latest::{prelude::*, MaxDispatchErrorLen, MaybeErrorCode, Weight}, DoubleEncoded, }; -use xcm_executor::{ExecutorError, FeesMode, traits::{ConvertLocation, FeeReason}}; +use xcm_executor::{ + traits::{ConvertLocation, FeeReason}, + ExecutorError, FeesMode, +}; benchmarks! { report_holding { diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs index 9bca66851cd5..4cb95ed5ac6c 100644 --- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs +++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mock.rs @@ -23,7 +23,7 @@ use frame_support::{ traits::{Everything, OriginTrait}, weights::Weight, }; -use sp_core::{H256, ConstU32}; +use sp_core::{ConstU32, H256}; use sp_runtime::traits::{BlakeTwo256, IdentityLookup, TrailingZeroInput}; use xcm_builder::{ test_utils::{ diff --git a/substrate/frame/support/procedural/src/pallet/expand/warnings.rs b/substrate/frame/support/procedural/src/pallet/expand/warnings.rs index ae5890878a2f..6ce2097c2684 100644 --- a/substrate/frame/support/procedural/src/pallet/expand/warnings.rs +++ b/substrate/frame/support/procedural/src/pallet/expand/warnings.rs @@ -34,7 +34,7 @@ pub(crate) fn weight_witness_warning( return } let CallWeightDef::Immediate(w) = &method.weight else { - return; + return }; let partial_warning = Warning::new_deprecated("UncheckedWeightWitness") @@ -67,7 +67,7 @@ pub(crate) fn weight_constant_warning( return } let syn::Expr::Lit(lit) = weight else { - return; + return }; let warning = Warning::new_deprecated("ConstantWeight") From 939b361cc410abaf42a041b281595715c035d9e2 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 11 Oct 2023 14:53:10 +0200 Subject: [PATCH 49/56] Fix treasury tests --- Cargo.lock | 1 + .../assets/asset-hub-westend/Cargo.toml | 1 + .../asset-hub-westend/src/tests/teleport.rs | 16 +++- .../asset-hub-westend/src/tests/treasury.rs | 31 +++++++- .../assets/asset-hub-polkadot/src/lib.rs | 13 +++- .../assets/test-utils/src/xcm_helpers.rs | 76 ++++++++++++++++--- 6 files changed, 123 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d6edca7ff0ec..4783f35a8a77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -869,6 +869,7 @@ dependencies = [ "staging-xcm-builder", "staging-xcm-executor", "westend-runtime", + "westend-runtime-constants", "xcm-emulator", ] diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/Cargo.toml b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/Cargo.toml index f4f75dd31c54..fd8d78c6e8e5 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/Cargo.toml +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/Cargo.toml @@ -31,6 +31,7 @@ xcm-builder = { package = "staging-xcm-builder", path = "../../../../../../polk xcm-executor = { package = "staging-xcm-executor", path = "../../../../../../polkadot/xcm/xcm-executor", default-features = false} pallet-xcm = { path = "../../../../../../polkadot/xcm/pallet-xcm", default-features = false} westend-runtime = { path = "../../../../../../polkadot/runtime/westend", default-features = false } +westend-runtime-constants = { path = "../../../../../../polkadot/runtime/westend/constants", default-features = false } # Cumulus parachains-common = { path = "../../../../common" } diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs index 1d61111904cd..a302344bb07c 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/teleport.rs @@ -340,11 +340,17 @@ fn teleport_native_assets_back_from_system_para_to_relay_works() { test.set_dispatchable::(system_para_teleport_assets); test.assert(); + let delivery_fees = AssetHubWestend::execute_with(|| { + xcm_helpers::transfer_assets_delivery_fees::< + ::XcmSender, + >(test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest) + }); + let sender_balance_after = test.sender.balance; let receiver_balance_after = test.receiver.balance; // Sender's balance is reduced - assert_eq!(sender_balance_before - amount_to_send, sender_balance_after); + assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after); // Receiver's balance is increased assert!(receiver_balance_after > receiver_balance_before); } @@ -375,11 +381,17 @@ fn teleport_native_assets_from_system_para_to_relay_fails() { test.set_dispatchable::(system_para_teleport_assets); test.assert(); + let delivery_fees = AssetHubWestend::execute_with(|| { + xcm_helpers::transfer_assets_delivery_fees::< + ::XcmSender, + >(test.args.assets.clone(), 0, test.args.weight_limit, test.args.beneficiary, test.args.dest) + }); + let sender_balance_after = test.sender.balance; let receiver_balance_after = test.receiver.balance; // Sender's balance is reduced - assert_eq!(sender_balance_before - amount_to_send, sender_balance_after); + assert_eq!(sender_balance_before - amount_to_send - delivery_fees, sender_balance_after); // Receiver's balance does not change assert_eq!(receiver_balance_after, receiver_balance_before); } diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs index cf06f58682da..beff6f2c5818 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs @@ -14,10 +14,16 @@ // limitations under the License. use crate::*; -use frame_support::traits::fungibles::{Create, Inspect, Mutate}; +use frame_support::traits::{ + fungible::Mutate as SingleFunMutate, + fungibles::{Create, Inspect, Mutate}, +}; use integration_tests_common::constants::accounts::{ALICE, BOB}; use polkadot_runtime_common::impls::VersionedLocatableAsset; use xcm_executor::traits::ConvertLocation; +use westend_runtime::xcm_config::XcmConfig as WestendXcmConfig; +use asset_hub_westend_runtime::xcm_config::XcmConfig as AssetHubWestendXcmConfig; +use westend_runtime_constants::system_parachain::ASSET_HUB_ID; #[test] fn create_and_claim_treasury_spend() { @@ -45,6 +51,7 @@ fn create_and_claim_treasury_spend() { AssetHubWestend::execute_with(|| { type Assets = ::Assets; + type Balances = ::Balances; // create an asset class and mint some assets to the treasury account. assert_ok!(>::create( @@ -53,6 +60,13 @@ fn create_and_claim_treasury_spend() { true, SPEND_AMOUNT / 2 )); + // Asset hub is going to send a response back to the relay chain, + // so we need enough funds to pay for delivery fees + let delivery_fees = xcm_helpers::query_response_delivery_fees::< + ::XcmSender, + >(MultiLocation::parent()); + dbg!(&delivery_fees); + assert_ok!(>::mint_into(&treasury_account, delivery_fees)); assert_ok!(>::mint_into(ASSET_ID, &treasury_account, SPEND_AMOUNT * 4)); // beneficiary has zero balance. assert_eq!(>::balance(ASSET_ID, &alice,), 0u128,); @@ -62,6 +76,21 @@ fn create_and_claim_treasury_spend() { type RuntimeEvent = ::RuntimeEvent; type Treasury = ::Treasury; type AssetRate = ::AssetRate; + type Balances = ::Balances; + + // Relay will send a message to asset hub, + // so we need enough funds to pay for the delivery fees + let interior: Junctions = Junction::AccountId32 { id: bob.clone().into(), network: None }.into(); + let destination: MultiLocation = Parachain(ASSET_HUB_ID).into(); + let beneficiary: MultiLocation = Junction::AccountId32 { id: alice.clone().into(), network: None }.into(); + let asset: MultiAsset = (Here, 100u128).into(); + let delivery_fees = xcm_helpers::pay_over_xcm_delivery_fees::< + ::XcmSender, + >(interior, destination, beneficiary, asset); + assert_ok!(>::mint_into( + &bob, + delivery_fees.into(), + )); // create a conversion rate from `asset_kind` to the native currency. assert_ok!(AssetRate::create(root.clone(), Box::new(asset_kind.clone()), 2.into())); diff --git a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs index b5d800c92015..b58d094deec6 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-polkadot/src/lib.rs @@ -1092,10 +1092,21 @@ impl_runtime_apis! { use xcm_config::{DotLocation, MaxAssetsIntoHolding}; use pallet_xcm_benchmarks::asset_instance_from; + parameter_types! { + pub ExistentialDepositMultiAsset: Option = Some(( + xcm_config::DotLocation::get(), + ExistentialDeposit::get() + ).into()); + } + impl pallet_xcm_benchmarks::Config for Runtime { type XcmConfig = xcm_config::XcmConfig; type AccountIdConverter = xcm_config::LocationToAccountId; - type DeliveryHelper = (); + type DeliveryHelper = cumulus_primitives_utility::ToParentDeliveryHelper< + XcmConfig, + ExistentialDepositMultiAsset, + xcm_config::PriceForParentDelivery, + >; fn valid_destination() -> Result { Ok(DotLocation::get()) } diff --git a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs index cda7a64ed5bc..bf792c5faf6c 100644 --- a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs +++ b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs @@ -30,17 +30,53 @@ pub fn transfer_assets_delivery_fees( destination: MultiLocation, ) -> u128 { let message = teleport_assets_dummy_message(assets, fee_asset_item, weight_limit, beneficiary); - let Ok((_, delivery_fees)) = validate_send::(destination, message) else { - unreachable!("message can be sent; qed") - }; - if let Some(delivery_fee) = delivery_fees.inner().first() { - let Fungible(delivery_fee_amount) = delivery_fee.fun else { - unreachable!("asset is fungible; qed"); - }; - delivery_fee_amount - } else { - 0 - } + get_fungible_delivery_fees::(destination, message) +} + +/// Returns the delivery fees amount for a query response as a result of the execution +/// of a `ExpectError` instruction with no error. +pub fn query_response_delivery_fees(querier: MultiLocation) -> u128 { + // Message to calculate delivery fees, it's encoded size is what's important. + // This message reports that there was no error, if an error is reported, the encoded size would be different. + let message = Xcm(vec![ + SetFeesMode { jit_withdraw: true }, + QueryResponse { + query_id: 0, // Dummy query id + response: Response::ExecutionResult(None), + max_weight: Weight::zero(), + querier: Some(querier), + }, + SetTopic([0u8; 32]), // Dummy topic + ]); + get_fungible_delivery_fees::(querier, message) +} + +/// Returns the delivery fees amount for the execution of `PayOverXcm` +pub fn pay_over_xcm_delivery_fees( + interior: Junctions, + destination: MultiLocation, + beneficiary: MultiLocation, + asset: MultiAsset, +) -> u128 { + // This is a dummy message. + // The encoded size is all that matters for delivery fees. + let message = Xcm(vec![ + DescendOrigin(interior), + UnpaidExecution { weight_limit: Unlimited, check_origin: None }, + SetAppendix(Xcm(vec![ + SetFeesMode { jit_withdraw: true }, + ReportError(QueryResponseInfo { + destination, + query_id: 0, + max_weight: Weight::zero(), + }), + ])), + TransferAsset { + beneficiary, + assets: vec![asset].into(), + }, + ]); + get_fungible_delivery_fees::(destination, message) } /// Approximates the actual message sent by the teleport extrinsic. @@ -61,3 +97,21 @@ fn teleport_assets_dummy_message( SetTopic([0u8; 32]), // Dummy topic ]) } + +/// Given a message, a sender, and a destination, it returns the delivery fees +fn get_fungible_delivery_fees( + destination: MultiLocation, + message: Xcm<()>, +) -> u128 { + let Ok((_, delivery_fees)) = validate_send::(destination, message) else { + unreachable!("message can be sent; qed") + }; + if let Some(delivery_fee) = delivery_fees.inner().first() { + let Fungible(delivery_fee_amount) = delivery_fee.fun else { + unreachable!("asset is fungible; qed"); + }; + delivery_fee_amount + } else { + 0 + } +} From 94ba563669d04b95866914460b458a6cdec3ff0f Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Wed, 11 Oct 2023 13:03:41 +0000 Subject: [PATCH 50/56] ".git/.scripts/commands/fmt/fmt.sh" --- .../asset-hub-westend/src/tests/treasury.rs | 22 ++++++----- .../assets/test-utils/src/xcm_helpers.rs | 37 +++++++------------ 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs index beff6f2c5818..b8d29b9d6dc2 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs @@ -14,16 +14,16 @@ // limitations under the License. use crate::*; +use asset_hub_westend_runtime::xcm_config::XcmConfig as AssetHubWestendXcmConfig; use frame_support::traits::{ fungible::Mutate as SingleFunMutate, fungibles::{Create, Inspect, Mutate}, }; use integration_tests_common::constants::accounts::{ALICE, BOB}; use polkadot_runtime_common::impls::VersionedLocatableAsset; -use xcm_executor::traits::ConvertLocation; use westend_runtime::xcm_config::XcmConfig as WestendXcmConfig; -use asset_hub_westend_runtime::xcm_config::XcmConfig as AssetHubWestendXcmConfig; use westend_runtime_constants::system_parachain::ASSET_HUB_ID; +use xcm_executor::traits::ConvertLocation; #[test] fn create_and_claim_treasury_spend() { @@ -66,7 +66,10 @@ fn create_and_claim_treasury_spend() { ::XcmSender, >(MultiLocation::parent()); dbg!(&delivery_fees); - assert_ok!(>::mint_into(&treasury_account, delivery_fees)); + assert_ok!(>::mint_into( + &treasury_account, + delivery_fees + )); assert_ok!(>::mint_into(ASSET_ID, &treasury_account, SPEND_AMOUNT * 4)); // beneficiary has zero balance. assert_eq!(>::balance(ASSET_ID, &alice,), 0u128,); @@ -80,17 +83,18 @@ fn create_and_claim_treasury_spend() { // Relay will send a message to asset hub, // so we need enough funds to pay for the delivery fees - let interior: Junctions = Junction::AccountId32 { id: bob.clone().into(), network: None }.into(); + let interior: Junctions = + Junction::AccountId32 { id: bob.clone().into(), network: None }.into(); let destination: MultiLocation = Parachain(ASSET_HUB_ID).into(); - let beneficiary: MultiLocation = Junction::AccountId32 { id: alice.clone().into(), network: None }.into(); + let beneficiary: MultiLocation = + Junction::AccountId32 { id: alice.clone().into(), network: None }.into(); let asset: MultiAsset = (Here, 100u128).into(); let delivery_fees = xcm_helpers::pay_over_xcm_delivery_fees::< ::XcmSender, >(interior, destination, beneficiary, asset); - assert_ok!(>::mint_into( - &bob, - delivery_fees.into(), - )); + assert_ok!( + >::mint_into(&bob, delivery_fees.into(),) + ); // create a conversion rate from `asset_kind` to the native currency. assert_ok!(AssetRate::create(root.clone(), Box::new(asset_kind.clone()), 2.into())); diff --git a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs index bf792c5faf6c..0aebe38fef53 100644 --- a/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs +++ b/cumulus/parachains/runtimes/assets/test-utils/src/xcm_helpers.rs @@ -37,17 +37,18 @@ pub fn transfer_assets_delivery_fees( /// of a `ExpectError` instruction with no error. pub fn query_response_delivery_fees(querier: MultiLocation) -> u128 { // Message to calculate delivery fees, it's encoded size is what's important. - // This message reports that there was no error, if an error is reported, the encoded size would be different. - let message = Xcm(vec![ + // This message reports that there was no error, if an error is reported, the encoded size would + // be different. + let message = Xcm(vec![ SetFeesMode { jit_withdraw: true }, - QueryResponse { - query_id: 0, // Dummy query id - response: Response::ExecutionResult(None), - max_weight: Weight::zero(), - querier: Some(querier), - }, - SetTopic([0u8; 32]), // Dummy topic - ]); + QueryResponse { + query_id: 0, // Dummy query id + response: Response::ExecutionResult(None), + max_weight: Weight::zero(), + querier: Some(querier), + }, + SetTopic([0u8; 32]), // Dummy topic + ]); get_fungible_delivery_fees::(querier, message) } @@ -65,16 +66,9 @@ pub fn pay_over_xcm_delivery_fees( UnpaidExecution { weight_limit: Unlimited, check_origin: None }, SetAppendix(Xcm(vec![ SetFeesMode { jit_withdraw: true }, - ReportError(QueryResponseInfo { - destination, - query_id: 0, - max_weight: Weight::zero(), - }), + ReportError(QueryResponseInfo { destination, query_id: 0, max_weight: Weight::zero() }), ])), - TransferAsset { - beneficiary, - assets: vec![asset].into(), - }, + TransferAsset { beneficiary, assets: vec![asset].into() }, ]); get_fungible_delivery_fees::(destination, message) } @@ -99,10 +93,7 @@ fn teleport_assets_dummy_message( } /// Given a message, a sender, and a destination, it returns the delivery fees -fn get_fungible_delivery_fees( - destination: MultiLocation, - message: Xcm<()>, -) -> u128 { +fn get_fungible_delivery_fees(destination: MultiLocation, message: Xcm<()>) -> u128 { let Ok((_, delivery_fees)) = validate_send::(destination, message) else { unreachable!("message can be sent; qed") }; From 324c20f408bd97497b8814fca3cf55e9dde55171 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 11 Oct 2023 16:31:05 +0200 Subject: [PATCH 51/56] Waive delivery fees for westend treasury location --- Cargo.lock | 1 + .../asset-hub-westend/src/tests/treasury.rs | 35 +------------------ .../assets/asset-hub-westend/Cargo.toml | 1 + .../asset-hub-westend/src/xcm_config.rs | 23 +++++++++++- 4 files changed, 25 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4783f35a8a77..12487cc76124 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -943,6 +943,7 @@ dependencies = [ "staging-xcm-builder", "staging-xcm-executor", "substrate-wasm-builder", + "westend-runtime", "westend-runtime-constants", ] diff --git a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs index b8d29b9d6dc2..cf06f58682da 100644 --- a/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs +++ b/cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs @@ -14,15 +14,9 @@ // limitations under the License. use crate::*; -use asset_hub_westend_runtime::xcm_config::XcmConfig as AssetHubWestendXcmConfig; -use frame_support::traits::{ - fungible::Mutate as SingleFunMutate, - fungibles::{Create, Inspect, Mutate}, -}; +use frame_support::traits::fungibles::{Create, Inspect, Mutate}; use integration_tests_common::constants::accounts::{ALICE, BOB}; use polkadot_runtime_common::impls::VersionedLocatableAsset; -use westend_runtime::xcm_config::XcmConfig as WestendXcmConfig; -use westend_runtime_constants::system_parachain::ASSET_HUB_ID; use xcm_executor::traits::ConvertLocation; #[test] @@ -51,7 +45,6 @@ fn create_and_claim_treasury_spend() { AssetHubWestend::execute_with(|| { type Assets = ::Assets; - type Balances = ::Balances; // create an asset class and mint some assets to the treasury account. assert_ok!(>::create( @@ -60,16 +53,6 @@ fn create_and_claim_treasury_spend() { true, SPEND_AMOUNT / 2 )); - // Asset hub is going to send a response back to the relay chain, - // so we need enough funds to pay for delivery fees - let delivery_fees = xcm_helpers::query_response_delivery_fees::< - ::XcmSender, - >(MultiLocation::parent()); - dbg!(&delivery_fees); - assert_ok!(>::mint_into( - &treasury_account, - delivery_fees - )); assert_ok!(>::mint_into(ASSET_ID, &treasury_account, SPEND_AMOUNT * 4)); // beneficiary has zero balance. assert_eq!(>::balance(ASSET_ID, &alice,), 0u128,); @@ -79,22 +62,6 @@ fn create_and_claim_treasury_spend() { type RuntimeEvent = ::RuntimeEvent; type Treasury = ::Treasury; type AssetRate = ::AssetRate; - type Balances = ::Balances; - - // Relay will send a message to asset hub, - // so we need enough funds to pay for the delivery fees - let interior: Junctions = - Junction::AccountId32 { id: bob.clone().into(), network: None }.into(); - let destination: MultiLocation = Parachain(ASSET_HUB_ID).into(); - let beneficiary: MultiLocation = - Junction::AccountId32 { id: alice.clone().into(), network: None }.into(); - let asset: MultiAsset = (Here, 100u128).into(); - let delivery_fees = xcm_helpers::pay_over_xcm_delivery_fees::< - ::XcmSender, - >(interior, destination, beneficiary, asset); - assert_ok!( - >::mint_into(&bob, delivery_fees.into(),) - ); // create a conversion rate from `asset_kind` to the native currency. assert_ok!(AssetRate::create(root.clone(), Box::new(asset_kind.clone()), 2.into())); diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml index f7f6fdf68e46..8d57775f282b 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml @@ -60,6 +60,7 @@ polkadot-core-primitives = { path = "../../../../../polkadot/core-primitives", d polkadot-parachain-primitives = { path = "../../../../../polkadot/parachain", default-features = false} polkadot-runtime-common = { path = "../../../../../polkadot/runtime/common", default-features = false} westend-runtime-constants = { path = "../../../../../polkadot/runtime/westend/constants", default-features = false} +westend-runtime = { path = "../../../../../polkadot/runtime/westend", default-features = false } xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false} xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false} xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false} diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs index 5ba1a2cc2b12..adb435eb27c5 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs @@ -41,6 +41,7 @@ use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_common::xcm_sender::ExponentialPrice; use sp_runtime::traits::{AccountIdConversion, ConvertInto}; use westend_runtime_constants::system_parachain; +use westend_runtime::Treasury as WestendTreasury; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, @@ -502,6 +503,26 @@ match_types! { }; } +parameter_types! { + pub RelayTreasuryLocation: MultiLocation = (Parent, PalletInstance(::index() as u8)).into(); +} + +pub struct RelayTreasury; +impl Contains for RelayTreasury { + fn contains(location: &MultiLocation) -> bool { + let relay_treasury_location = RelayTreasuryLocation::get(); + matches!(location, relay_treasury_location) + } +} + +/// Locations that will not be charged fees in the executor, +/// either execution or delivery. +/// We only waive fees for system functions, which these locations represent. +pub type WaivedLocations = ( + RelayOrOtherSystemParachains, + RelayTreasury +); + pub struct XcmConfig; impl xcm_executor::Config for XcmConfig { type RuntimeCall = RuntimeCall; @@ -550,7 +571,7 @@ impl xcm_executor::Config for XcmConfig { type AssetExchanger = (); type FeeManager = XcmFeesToAccount< Self, - RelayOrOtherSystemParachains, + WaivedLocations, AccountId, TreasuryAccount, >; From 551e2f6e4d8476ff77efc3303d796401475d5b7c Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Wed, 11 Oct 2023 14:41:27 +0000 Subject: [PATCH 52/56] ".git/.scripts/commands/fmt/fmt.sh" --- .../assets/asset-hub-westend/src/xcm_config.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs index adb435eb27c5..c823d2c43d32 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs @@ -40,8 +40,8 @@ use parachains_common::{ use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_common::xcm_sender::ExponentialPrice; use sp_runtime::traits::{AccountIdConversion, ConvertInto}; -use westend_runtime_constants::system_parachain; use westend_runtime::Treasury as WestendTreasury; +use westend_runtime_constants::system_parachain; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, @@ -518,10 +518,7 @@ impl Contains for RelayTreasury { /// Locations that will not be charged fees in the executor, /// either execution or delivery. /// We only waive fees for system functions, which these locations represent. -pub type WaivedLocations = ( - RelayOrOtherSystemParachains, - RelayTreasury -); +pub type WaivedLocations = (RelayOrOtherSystemParachains, RelayTreasury); pub struct XcmConfig; impl xcm_executor::Config for XcmConfig { @@ -569,12 +566,7 @@ impl xcm_executor::Config for XcmConfig { type MaxAssetsIntoHolding = MaxAssetsIntoHolding; type AssetLocker = (); type AssetExchanger = (); - type FeeManager = XcmFeesToAccount< - Self, - WaivedLocations, - AccountId, - TreasuryAccount, - >; + type FeeManager = XcmFeesToAccount; type MessageExporter = (); type UniversalAliases = Nothing; type CallDispatcher = WithOriginFilter; From 9b5fc1519c47ad4c688f6bc1b5ece2412d86596f Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 12 Oct 2023 10:43:22 +0200 Subject: [PATCH 53/56] Fix feature propagation --- cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml index 8d57775f282b..800992839dc0 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml @@ -150,6 +150,7 @@ try-runtime = [ "parachain-info/try-runtime", "polkadot-runtime-common/try-runtime", "sp-runtime/try-runtime", + "westend-runtime/try-runtime", ] std = [ "assets-common/std", From 20bd27fcb839cdd84581317a8986c04ad6a1536c Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 12 Oct 2023 10:47:04 +0200 Subject: [PATCH 54/56] Fix feature propagation --- cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml index 800992839dc0..608969f2975d 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml @@ -119,6 +119,7 @@ runtime-benchmarks = [ "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", + "westend-runtime/runtime-benchmarks", ] try-runtime = [ "cumulus-pallet-aura-ext/try-runtime", @@ -215,6 +216,7 @@ std = [ "xcm-builder/std", "xcm-executor/std", "xcm/std", + "westend-runtime/std", ] experimental = [ "pallet-aura/experimental" ] From a172a7cd5838090b700b4eb3be656b1e7e531374 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 12 Oct 2023 10:52:45 +0200 Subject: [PATCH 55/56] Fix fmt manifest --- .../parachains/runtimes/assets/asset-hub-westend/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml index 608969f2975d..cb5fba1684cc 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml @@ -117,9 +117,9 @@ runtime-benchmarks = [ "polkadot-parachain-primitives/runtime-benchmarks", "polkadot-runtime-common/runtime-benchmarks", "sp-runtime/runtime-benchmarks", + "westend-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", - "westend-runtime/runtime-benchmarks", ] try-runtime = [ "cumulus-pallet-aura-ext/try-runtime", @@ -213,10 +213,10 @@ std = [ "sp-version/std", "substrate-wasm-builder", "westend-runtime-constants/std", + "westend-runtime/std", "xcm-builder/std", "xcm-executor/std", "xcm/std", - "westend-runtime/std", ] experimental = [ "pallet-aura/experimental" ] From 87c715816aaf3adb5bb62f5788133612e12cce4d Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 12 Oct 2023 14:39:48 +0200 Subject: [PATCH 56/56] Remove unused things --- .../runtimes/assets/asset-hub-westend/src/xcm_config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs index c823d2c43d32..24cd063e3b2d 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs @@ -511,7 +511,7 @@ pub struct RelayTreasury; impl Contains for RelayTreasury { fn contains(location: &MultiLocation) -> bool { let relay_treasury_location = RelayTreasuryLocation::get(); - matches!(location, relay_treasury_location) + *location == relay_treasury_location } }