From 7eba9a73c473aca4697c478c4fe834350f172f05 Mon Sep 17 00:00:00 2001 From: muharem Date: Mon, 26 Jun 2023 11:16:07 +0200 Subject: [PATCH 1/3] Runtime: Success value and reachable location for Polkadot Collectives benchmarks --- .../collectives-polkadot/src/fellowship/mod.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs b/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs index d3526715344..deb348c9e03 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs @@ -106,12 +106,15 @@ impl pallet_ranked_collective::Config for Runtime type WeightInfo = weights::pallet_ranked_collective::WeightInfo; type RuntimeEvent = RuntimeEvent; // Promotions and the induction of new members are serviced by `FellowshipCore` pallet instance. - type PromoteOrigin = EnsureRootWithSuccess>; + // The maximum value of `u16` set as a success value for the root to ensure the benchmarks will pass. + type PromoteOrigin = EnsureRootWithSuccess>; // Demotion is by any of: // - Root can demote arbitrarily. // - the FellowshipAdmin origin (i.e. token holder referendum); + // + // The maximum value of `u16` set as a success value for the root to ensure the benchmarks will pass. type DemoteOrigin = EitherOf< - EnsureRootWithSuccess>, + EnsureRootWithSuccess>, MapSuccess< EnsureXcm>, Replace>, @@ -182,8 +185,18 @@ pub type FellowshipSalaryInstance = pallet_salary::Instance1; use xcm::prelude::*; +#[cfg(not(feature = "runtime-benchmarks"))] parameter_types! { pub AssetHub: MultiLocation = (Parent, Parachain(1000)).into(); +} + +#[cfg(feature = "runtime-benchmarks")] +parameter_types! { + // The reachable location within the benchmark environment. + pub AssetHub: MultiLocation = Parent.into(); +} + +parameter_types! { pub AssetHubUsdtId: AssetId = (PalletInstance(50), GeneralIndex(1984)).into(); pub UsdtAsset: LocatableAssetId = LocatableAssetId { location: AssetHub::get(), From 74b86f4b90ce4ddde46b5996591332454a96376e Mon Sep 17 00:00:00 2001 From: muharem Date: Mon, 26 Jun 2023 13:01:10 +0200 Subject: [PATCH 2/3] separate promote origin for benches --- .../collectives/collectives-polkadot/src/fellowship/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs b/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs index deb348c9e03..40361ba5ecd 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs @@ -105,9 +105,14 @@ pub type FellowshipCollectiveInstance = pallet_ranked_collective::Instance1; impl pallet_ranked_collective::Config for Runtime { type WeightInfo = weights::pallet_ranked_collective::WeightInfo; type RuntimeEvent = RuntimeEvent; + + #[cfg(not(feature = "runtime-benchmarks"))] // Promotions and the induction of new members are serviced by `FellowshipCore` pallet instance. + type PromoteOrigin = frame_system::EnsureNever; + #[cfg(feature = "runtime-benchmarks")] // The maximum value of `u16` set as a success value for the root to ensure the benchmarks will pass. type PromoteOrigin = EnsureRootWithSuccess>; + // Demotion is by any of: // - Root can demote arbitrarily. // - the FellowshipAdmin origin (i.e. token holder referendum); From 4e4321af86a6716c759237024e9c3741e500f2ae Mon Sep 17 00:00:00 2001 From: muharem Date: Wed, 28 Jun 2023 18:43:17 +0200 Subject: [PATCH 3/3] pay with ensure --- .../src/fellowship/mod.rs | 17 +++-- .../collectives-polkadot/src/impls.rs | 62 ++++++++++++++++++- 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs b/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs index 40361ba5ecd..99613542a2e 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/fellowship/mod.rs @@ -42,6 +42,9 @@ use sp_runtime::traits::{AccountIdConversion, ConstU16, ConvertToValue, Replace, use xcm::latest::BodyId; use xcm_builder::{AliasesIntoAccountId32, LocatableAssetId, PayOverXcm}; +#[cfg(feature = "runtime-benchmarks")] +use crate::impls::benchmarks::{OpenHrmpChannel, PayWithEnsure}; + /// The Fellowship members' ranks. pub mod ranks { use pallet_ranked_collective::Rank; @@ -190,18 +193,8 @@ pub type FellowshipSalaryInstance = pallet_salary::Instance1; use xcm::prelude::*; -#[cfg(not(feature = "runtime-benchmarks"))] parameter_types! { pub AssetHub: MultiLocation = (Parent, Parachain(1000)).into(); -} - -#[cfg(feature = "runtime-benchmarks")] -parameter_types! { - // The reachable location within the benchmark environment. - pub AssetHub: MultiLocation = Parent.into(); -} - -parameter_types! { pub AssetHubUsdtId: AssetId = (PalletInstance(50), GeneralIndex(1984)).into(); pub UsdtAsset: LocatableAssetId = LocatableAssetId { location: AssetHub::get(), @@ -229,7 +222,11 @@ pub type FellowshipSalaryPaymaster = PayOverXcm< impl pallet_salary::Config for Runtime { type WeightInfo = weights::pallet_salary::WeightInfo; type RuntimeEvent = RuntimeEvent; + + #[cfg(not(feature = "runtime-benchmarks"))] type Paymaster = FellowshipSalaryPaymaster; + #[cfg(feature = "runtime-benchmarks")] + type Paymaster = PayWithEnsure>>; type Members = pallet_ranked_collective::Pallet; #[cfg(not(feature = "runtime-benchmarks"))] diff --git a/parachains/runtimes/collectives/collectives-polkadot/src/impls.rs b/parachains/runtimes/collectives/collectives-polkadot/src/impls.rs index 6ccff8bda1c..784f6149b81 100644 --- a/parachains/runtimes/collectives/collectives-polkadot/src/impls.rs +++ b/parachains/runtimes/collectives/collectives-polkadot/src/impls.rs @@ -150,12 +150,17 @@ impl PrivilegeCmp for EqualOrGreatestRootCmp { #[cfg(feature = "runtime-benchmarks")] pub mod benchmarks { use super::*; - use frame_support::traits::fungible; + use crate::ParachainSystem; + use cumulus_primitives_core::{ChannelStatus, GetChannelInfo}; + use frame_support::traits::{ + fungible, + tokens::{Pay, PaymentStatus}, + }; use pallet_ranked_collective::Rank; use parachains_common::{AccountId, Balance}; use sp_runtime::traits::Convert; - /// Rank to salary conversion helper type.` + /// Rank to salary conversion helper type. pub struct RankToSalary(PhantomData); impl Convert for RankToSalary where @@ -165,4 +170,57 @@ pub mod benchmarks { Balance::from(r).saturating_mul(Fungible::minimum_balance()) } } + + /// Trait for setting up any prerequisites for successful execution of benchmarks. + pub trait EnsureSuccessful { + fn ensure_successful(); + } + + /// Implementation of the [`EnsureSuccessful`] trait which opens an HRMP channel between + /// the Collectives and a parachain with a given ID. + pub struct OpenHrmpChannel(PhantomData); + impl> EnsureSuccessful for OpenHrmpChannel { + fn ensure_successful() { + if let ChannelStatus::Closed = ParachainSystem::get_channel_status(I::get().into()) { + ParachainSystem::open_outbound_hrmp_channel_for_benchmarks(I::get().into()) + } + } + } + + /// Type that wraps a type implementing the [`Pay`] trait to decorate its [`Pay::ensure_successful`] + /// function with a provided implementation of the [`EnsureSuccessful`] trait. + pub struct PayWithEnsure(PhantomData<(O, E)>); + impl Pay for PayWithEnsure + where + O: Pay, + E: EnsureSuccessful, + { + type AssetKind = O::AssetKind; + type Balance = O::Balance; + type Beneficiary = O::Beneficiary; + type Error = O::Error; + type Id = O::Id; + + fn pay( + who: &Self::Beneficiary, + asset_kind: Self::AssetKind, + amount: Self::Balance, + ) -> Result { + O::pay(who, asset_kind, amount) + } + fn check_payment(id: Self::Id) -> PaymentStatus { + O::check_payment(id) + } + fn ensure_successful( + who: &Self::Beneficiary, + asset_kind: Self::AssetKind, + amount: Self::Balance, + ) { + E::ensure_successful(); + O::ensure_successful(who, asset_kind, amount) + } + fn ensure_concluded(id: Self::Id) { + O::ensure_concluded(id) + } + } }