Skip to content

Commit

Permalink
Fix pallet treasury benchmarks (#912)
Browse files Browse the repository at this point in the history
* Fix pallet treasury benchmarks

* fmt

* lint

* Apply suggestions from code review

* Fix spacing

* Add missing comma

* Fix formatting

---------

Co-authored-by: Hernando Castano <[email protected]>
Co-authored-by: Hernando Castano <[email protected]>
  • Loading branch information
3 people authored Jun 26, 2024
1 parent 0f46642 commit 82b81eb
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 102 deletions.
87 changes: 74 additions & 13 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ use frame_support::{
parameter_types,
sp_runtime::RuntimeDebug,
traits::{
fungible::HoldConsideration,
fungible::{self, HoldConsideration},
tokens::{
nonfungibles_v2::Inspect, pay::PayAssetFromAccount, GetSalary, PayFromAccount,
UnityAssetBalanceConversion,
nonfungibles_v2::Inspect, pay::PayAssetFromAccount, GetSalary, Pay, PayFromAccount,
PaymentStatus, Preservation, UnityAssetBalanceConversion,
},
ConstU16, ConstU32, Contains, Currency, EitherOfDiverse, EqualPrivilegeOnly, Imbalance,
InstanceFilter, KeyOwnerProofSystem, LinearStoragePrice, LockIdentifier, OnUnbalanced,
Expand All @@ -71,7 +71,7 @@ use frame_support::{
pub use frame_system::Call as SystemCall;
use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot, EnsureSigned,
EnsureRoot, EnsureSigned, EnsureWithSuccess,
};

#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -100,11 +100,12 @@ use sp_runtime::{
curve::PiecewiseLinear,
generic, impl_opaque_keys,
traits::{
self, BlakeTwo256, Block as BlockT, Bounded, ConvertInto, NumberFor, OpaqueKeys,
SaturatedConversion, StaticLookup,
self, BlakeTwo256, Block as BlockT, Bounded, ConvertInto, IdentityLookup, NumberFor,
OpaqueKeys, SaturatedConversion, StaticLookup,
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, FixedPointNumber, FixedU128, Perbill, Percent, Permill, Perquintill,
ApplyExtrinsicResult, DispatchError, FixedPointNumber, FixedU128, Perbill, Percent, Permill,
Perquintill,
};
use sp_std::prelude::*;
#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -1009,7 +1010,8 @@ impl pallet_membership::Config<pallet_membership::Instance1> for Runtime {

parameter_types! {
pub const ProposalBond: Permill = Permill::from_percent(5);
pub const ProposalBondMinimum: Balance = DOLLARS;
pub const ProposalBondMinimum: Balance = 100 * DOLLARS;
pub const ProposalBondMaximum: Balance = 500 * DOLLARS;
pub const SpendPeriod: BlockNumber = DAYS;
pub const Burn: Permill = Permill::from_percent(50);
pub const TipCountdown: BlockNumber = DAYS;
Expand All @@ -1030,6 +1032,7 @@ parameter_types! {
pub const CuratorDepositMin: Balance = DOLLARS;
pub const CuratorDepositMax: Balance = 100 * DOLLARS;
pub const SpendPayoutPeriod: BlockNumber = 30 * DAYS;
pub const MaxBalance: Balance = Balance::MAX;
}

impl pallet_treasury::Config for Runtime {
Expand All @@ -1041,30 +1044,88 @@ impl pallet_treasury::Config for Runtime {
type BurnDestination = ();
type Currency = Balances;
type MaxApprovals = MaxApprovals;
type OnSlash = ();
type OnSlash = Treasury;
type PalletId = TreasuryPalletId;
type ProposalBond = ProposalBond;
type ProposalBondMaximum = ();
type ProposalBondMaximum = ProposalBondMaximum;
type ProposalBondMinimum = ProposalBondMinimum;
type RejectOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionMoreThan<AccountId, CouncilCollective, 1, 2>,
>;
type RuntimeEvent = RuntimeEvent;
type SpendFunds = Bounties;
type SpendOrigin = frame_support::traits::NeverEnsureOrigin<u128>;
type SpendOrigin = EnsureWithSuccess<EnsureRoot<AccountId>, AccountId, MaxBalance>;
type SpendPeriod = SpendPeriod;
type AssetKind = ();
type Beneficiary = AccountId;
type BeneficiaryLookup = Indices;
type BeneficiaryLookup = IdentityLookup<AccountId>;
type BalanceConverter = UnityAssetBalanceConversion;
type Paymaster = PayFromAccount<Balances, TreasuryAccount>;
type Paymaster = PayFromTreasuryAccount;
type PayoutPeriod = SpendPayoutPeriod;
type WeightInfo = weights::pallet_treasury::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

pub struct PayFromTreasuryAccount;

impl Pay for PayFromTreasuryAccount {
type Balance = Balance;
type Beneficiary = AccountId;
type AssetKind = ();
type Id = ();
type Error = DispatchError;

#[cfg(not(feature = "runtime-benchmarks"))]
fn pay(
who: &Self::Beneficiary,
_asset_kind: Self::AssetKind,
amount: Self::Balance,
) -> Result<Self::Id, Self::Error> {
let _ = <Balances as fungible::Mutate<_>>::transfer(
&TreasuryAccount::get(),
who,
amount,
Preservation::Expendable,
)?;
Ok(())
}

#[cfg(feature = "runtime-benchmarks")]
fn pay(
who: &Self::Beneficiary,
_asset_kind: Self::AssetKind,
amount: Self::Balance,
) -> Result<Self::Id, Self::Error> {
// In case of benchmarks, we adjust the value by multiplying it by 1_000_000_000_000,
// otherwise it fails with BelowMinimum limit error, because treasury benchmarks uses only
// 100 as the amount.
let _ = <Balances as fungible::Mutate<_>>::transfer(
&TreasuryAccount::get(),
who,
amount * 100 * DOLLARS,
Preservation::Expendable,
)?;
Ok(())
}

fn check_payment(_id: Self::Id) -> PaymentStatus {
PaymentStatus::Success
}

#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(_: &Self::Beneficiary, _: Self::AssetKind, amount: Self::Balance) {
<Balances as fungible::Mutate<_>>::mint_into(
&TreasuryAccount::get(),
amount * 100 * DOLLARS,
)
.unwrap();
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_concluded(_: Self::Id) {}
}

impl pallet_bounties::Config for Runtime {
type BountyDepositBase = BountyDepositBase;
type BountyDepositPayoutDelay = BountyDepositPayoutDelay;
Expand Down
Loading

0 comments on commit 82b81eb

Please sign in to comment.