Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Runtime: Success value and reachable location for Polkadot Collective…
Browse files Browse the repository at this point in the history
…s benchmarks (#2784)

* Runtime: Success value and reachable location for Polkadot Collectives benchmarks

* separate promote origin for benches

* pay with ensure
  • Loading branch information
muharem authored Jun 30, 2023
1 parent e9b07fd commit 63a92d2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,13 +108,21 @@ pub type FellowshipCollectiveInstance = pallet_ranked_collective::Instance1;
impl pallet_ranked_collective::Config<FellowshipCollectiveInstance> for Runtime {
type WeightInfo = weights::pallet_ranked_collective::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;

#[cfg(not(feature = "runtime-benchmarks"))]
// Promotions and the induction of new members are serviced by `FellowshipCore` pallet instance.
type PromoteOrigin = EnsureRootWithSuccess<Self::AccountId, ConstU16<{ ranks::DAN_9 }>>;
type PromoteOrigin = frame_system::EnsureNever<pallet_ranked_collective::Rank>;
#[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<Self::AccountId, ConstU16<65535>>;

// 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<Self::AccountId, ConstU16<{ ranks::DAN_9 }>>,
EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>,
MapSuccess<
EnsureXcm<IsVoiceOfBody<GovernanceLocation, FellowshipAdminBodyId>>,
Replace<ConstU16<{ ranks::DAN_9 }>>,
Expand Down Expand Up @@ -211,7 +222,11 @@ pub type FellowshipSalaryPaymaster = PayOverXcm<
impl pallet_salary::Config<FellowshipSalaryInstance> for Runtime {
type WeightInfo = weights::pallet_salary::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;

#[cfg(not(feature = "runtime-benchmarks"))]
type Paymaster = FellowshipSalaryPaymaster;
#[cfg(feature = "runtime-benchmarks")]
type Paymaster = PayWithEnsure<FellowshipSalaryPaymaster, OpenHrmpChannel<ConstU32<1000>>>;
type Members = pallet_ranked_collective::Pallet<Runtime, FellowshipCollectiveInstance>;

#[cfg(not(feature = "runtime-benchmarks"))]
Expand Down
62 changes: 60 additions & 2 deletions parachains/runtimes/collectives/collectives-polkadot/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,17 @@ impl PrivilegeCmp<OriginCaller> 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<Fungible>(PhantomData<Fungible>);
impl<Fungible> Convert<Rank, Balance> for RankToSalary<Fungible>
where
Expand All @@ -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<I>(PhantomData<I>);
impl<I: Get<u32>> EnsureSuccessful for OpenHrmpChannel<I> {
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<O, E>(PhantomData<(O, E)>);
impl<O, E> Pay for PayWithEnsure<O, E>
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<Self::Id, Self::Error> {
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)
}
}
}

0 comments on commit 63a92d2

Please sign in to comment.