Skip to content

Commit

Permalink
feat(governance): add benchmarks (#46)
Browse files Browse the repository at this point in the history
Adds benchmarks for the governance pallet.
  • Loading branch information
saiintbrisson authored Jan 3, 2025
1 parent 9665e56 commit 199073c
Show file tree
Hide file tree
Showing 8 changed files with 754 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pallets/governance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ runtime-benchmarks = [
"polkadot-sdk/runtime-benchmarks",
"pallet-torus0/runtime-benchmarks",
"pallet-emission0/runtime-benchmarks",

"pallet-torus0-api/runtime-benchmarks",
]
try-runtime = [
"polkadot-sdk/try-runtime",
Expand All @@ -42,6 +44,7 @@ pallet-torus0.workspace = true
pallet-emission0.workspace = true

pallet-governance-api.workspace = true
pallet-torus0-api.workspace = true

[dev-dependencies]
test-utils.workspace = true
196 changes: 196 additions & 0 deletions pallets/governance/src/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
use pallet_torus0_api::Torus0Api;
use polkadot_sdk::{
frame_benchmarking::{account, benchmarks},
frame_system::{self, RawOrigin},
sp_std::vec,
};

use crate::*;

fn create_application<T: Config>(module_key: &T::AccountId) {
let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.agent_application_cost;
let _ = <T as crate::Config>::Currency::deposit_creating(&module_key, cost);

let min_data_len = T::MinApplicationDataLength::get();
let data = vec![0; min_data_len as usize];

application::submit_application::<T>(module_key.clone(), module_key.clone(), data)
.expect("failed to submit application");
}

benchmarks! {
add_curator {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
}: _(RawOrigin::Root, module_key)

remove_curator {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
crate::roles::manage_role::<T, crate::Curators<T>>(module_key.clone(), true, crate::Error::<T>::AlreadyCurator)
.expect("failed to add curator");
}: _(RawOrigin::Root, module_key)

add_allocator {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
}: _(RawOrigin::Root, module_key)

remove_allocator {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
crate::roles::manage_role::<T, crate::Allocators<T>>(module_key.clone(), true, crate::Error::<T>::AlreadyCurator)
.expect("failed to add allocator");
}: _(RawOrigin::Root, module_key)

add_to_whitelist {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
crate::roles::manage_role::<T, crate::Curators<T>>(module_key.clone(), true, crate::Error::<T>::AlreadyCurator)
.expect("failed to add curator");
}: _(RawOrigin::Signed(module_key.clone()), module_key.clone())

remove_from_whitelist {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
crate::roles::manage_role::<T, crate::Curators<T>>(module_key.clone(), true, crate::Error::<T>::AlreadyCurator)
.expect("failed to add curator");
whitelist::add_to_whitelist::<T>(module_key.clone())
.expect("failed to add to whitelist");
}: _(RawOrigin::Signed(module_key.clone()), module_key.clone())

submit_application {
let module_key: T::AccountId = account("ModuleKey", 0, 2);

let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.agent_application_cost;
let _ = <T as crate::Config>::Currency::deposit_creating(
&module_key,
cost,
);

let min_data_len = T::MinApplicationDataLength::get();
let data = vec![0; min_data_len as usize];
}: _(RawOrigin::Signed(module_key.clone()), module_key.clone(), data)

accept_application {
let module_key: T::AccountId = account("ModuleKey", 0, 2);

crate::roles::manage_role::<T, crate::Curators<T>>(module_key.clone(), true, crate::Error::<T>::AlreadyCurator)
.expect("failed to add curator");

create_application::<T>(&module_key);
}: _(RawOrigin::Signed(module_key.clone()), 0)

deny_application {
let module_key: T::AccountId = account("ModuleKey", 0, 2);

crate::roles::manage_role::<T, crate::Curators<T>>(module_key.clone(), true, crate::Error::<T>::AlreadyCurator)
.expect("failed to add curator");

create_application::<T>(&module_key);
}: _(RawOrigin::Signed(module_key.clone()), 0)

penalize_agent {
let module_key: T::AccountId = account("ModuleKey", 0, 2);

crate::roles::manage_role::<T, crate::Curators<T>>(module_key.clone(), true, crate::Error::<T>::AlreadyCurator)
.expect("failed to add curator");

<pallet_torus0::Pallet::<T> as Torus0Api<
T::AccountId,
<<T as pallet_torus0::Config>::Currency as Currency<T::AccountId>>::Balance,
<<T as pallet_torus0::Config>::Currency as Currency<T::AccountId>>::NegativeImbalance,
>>::force_register_agent(
&module_key,
vec![],
vec![],
vec![],
).expect("failed to register agent");

}: _(RawOrigin::Signed(module_key.clone()), module_key.clone(), 50)

add_global_params_proposal {
let module_key: T::AccountId = account("ModuleKey", 0, 2);

let params = proposal::GlobalParamsData {
min_name_length: 2,
max_name_length: T::MaxAgentNameLengthConstraint::get() as u16 - 1,
max_allowed_agents: 1,
max_allowed_weights: 1,
min_stake_per_weight: 0,
min_weight_control_fee: 1,
min_staking_fee: 1,
dividends_participation_weight: Percent::zero(),
proposal_cost: 0,
};
let data = vec![0];

let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.proposal_cost;
let _ = <T as crate::Config>::Currency::deposit_creating(&module_key, cost);

}: _(RawOrigin::Signed(module_key.clone()), params, data)

add_global_custom_proposal {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
let data = vec![0];

let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.proposal_cost;
let _ = <T as crate::Config>::Currency::deposit_creating(&module_key, cost);

}: _(RawOrigin::Signed(module_key.clone()), data)

add_dao_treasury_transfer_proposal {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
let data = vec![0];

let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.proposal_cost;
let _ = <T as crate::Config>::Currency::deposit_creating(&module_key, cost);

}: _(RawOrigin::Signed(module_key.clone()), 0, module_key.clone(), data)

vote_proposal {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
let data = vec![0];

let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.proposal_cost;
let _ = <T as crate::Config>::Currency::deposit_creating(&module_key, cost);

proposal::add_global_custom_proposal::<T>(module_key.clone(), data)
.expect("failed to create proposal");

pallet_torus0::StakingTo::<T>::set(&module_key, &module_key, Some(1));

voting::disable_delegation::<T>(module_key.clone())
.expect("failed to disable delegation");

}: _(RawOrigin::Signed(module_key.clone()), 0, true)

remove_vote_proposal {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
let data = vec![0];

let config = crate::GlobalGovernanceConfig::<T>::get();
let cost = config.proposal_cost;
let _ = <T as crate::Config>::Currency::deposit_creating(&module_key, cost);

proposal::add_global_custom_proposal::<T>(module_key.clone(), data)
.expect("failed to create proposal");

pallet_torus0::StakingTo::<T>::set(&module_key, &module_key, Some(1));

voting::disable_delegation::<T>(module_key.clone())
.expect("failed to disable delegation");

voting::add_vote::<T>(module_key.clone(), 0, true)
.expect("failed to add vote");

}: _(RawOrigin::Signed(module_key.clone()), 0)

enable_vote_delegation {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
}: _(RawOrigin::Signed(module_key))

disable_vote_delegation {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
}: _(RawOrigin::Signed(module_key))
}
41 changes: 24 additions & 17 deletions pallets/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub mod roles;
pub mod voting;
pub mod whitelist;

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarks;
pub mod weights;

use crate::application::AgentApplication;
use crate::config::GovernanceConfiguration;
use crate::proposal::Proposal;
Expand All @@ -32,6 +36,7 @@ use polkadot_sdk::sp_std::vec::Vec;
pub mod pallet {
#![allow(clippy::too_many_arguments)]
use proposal::GlobalParamsData;
use weights::WeightInfo;

use super::*;

Expand Down Expand Up @@ -130,6 +135,8 @@ pub mod pallet {
+ IsType<<Self as polkadot_sdk::frame_system::Config>::RuntimeEvent>;

type Currency: Currency<Self::AccountId, Balance = u128> + Send + Sync;

type WeightInfo: WeightInfo;
}

#[pallet::pallet]
Expand All @@ -154,63 +161,63 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::add_curator(), DispatchClass::Normal, Pays::Yes))]
pub fn add_curator(origin: OriginFor<T>, key: AccountIdOf<T>) -> DispatchResult {
ensure_root(origin)?;
roles::manage_role::<T, Curators<T>>(key, true, Error::<T>::AlreadyCurator)
}

#[pallet::call_index(1)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::remove_curator(), DispatchClass::Normal, Pays::Yes))]
pub fn remove_curator(origin: OriginFor<T>, key: AccountIdOf<T>) -> DispatchResult {
ensure_root(origin)?;
roles::manage_role::<T, Curators<T>>(key, false, Error::<T>::NotAllocator)
}

#[pallet::call_index(2)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::add_allocator(), DispatchClass::Normal, Pays::Yes))]
pub fn add_allocator(origin: OriginFor<T>, key: AccountIdOf<T>) -> DispatchResult {
ensure_root(origin)?;
roles::manage_role::<T, Allocators<T>>(key, true, Error::<T>::AlreadyAllocator)
}

#[pallet::call_index(3)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::remove_allocator(), DispatchClass::Normal, Pays::Yes))]
pub fn remove_allocator(origin: OriginFor<T>, key: AccountIdOf<T>) -> DispatchResult {
ensure_root(origin)?;
roles::manage_role::<T, Allocators<T>>(key, false, Error::<T>::NotAllocator)
}

#[pallet::call_index(4)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::add_to_whitelist(), DispatchClass::Normal, Pays::Yes))]
pub fn add_to_whitelist(origin: OriginFor<T>, key: AccountIdOf<T>) -> DispatchResult {
roles::ensure_curator::<T>(origin)?;
whitelist::add_to_whitelist::<T>(key)
}

#[pallet::call_index(5)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::remove_from_whitelist(), DispatchClass::Normal, Pays::Yes))]
pub fn remove_from_whitelist(origin: OriginFor<T>, key: AccountIdOf<T>) -> DispatchResult {
roles::ensure_curator::<T>(origin)?;
whitelist::remove_from_whitelist::<T>(key)
}

#[pallet::call_index(6)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::accept_application(), DispatchClass::Normal, Pays::Yes))]
pub fn accept_application(origin: OriginFor<T>, application_id: u32) -> DispatchResult {
roles::ensure_curator::<T>(origin)?;
application::accept_application::<T>(application_id)
}

#[pallet::call_index(7)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::deny_application(), DispatchClass::Normal, Pays::Yes))]
pub fn deny_application(origin: OriginFor<T>, application_id: u32) -> DispatchResult {
roles::ensure_curator::<T>(origin)?;
application::deny_application::<T>(application_id)
}

#[pallet::call_index(8)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::penalize_agent(), DispatchClass::Normal, Pays::Yes))]
pub fn penalize_agent(
origin: OriginFor<T>,
agent_key: AccountIdOf<T>,
Expand All @@ -221,7 +228,7 @@ pub mod pallet {
}

#[pallet::call_index(9)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::submit_application(), DispatchClass::Normal, Pays::Yes))]
pub fn submit_application(
origin: OriginFor<T>,
agent_key: AccountIdOf<T>,
Expand All @@ -232,7 +239,7 @@ pub mod pallet {
}

#[pallet::call_index(10)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::add_global_params_proposal(), DispatchClass::Normal, Pays::Yes))]
pub fn add_global_params_proposal(
origin: OriginFor<T>,
data: GlobalParamsData<T>,
Expand All @@ -243,7 +250,7 @@ pub mod pallet {
}

#[pallet::call_index(11)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::add_global_custom_proposal(), DispatchClass::Normal, Pays::Yes))]
pub fn add_global_custom_proposal(
origin: OriginFor<T>,
metadata: Vec<u8>,
Expand All @@ -253,7 +260,7 @@ pub mod pallet {
}

#[pallet::call_index(12)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::add_dao_treasury_transfer_proposal(), DispatchClass::Normal, Pays::Yes))]
pub fn add_dao_treasury_transfer_proposal(
origin: OriginFor<T>,
value: BalanceOf<T>,
Expand All @@ -270,7 +277,7 @@ pub mod pallet {
}

#[pallet::call_index(13)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::vote_proposal(), DispatchClass::Normal, Pays::Yes))]
pub fn vote_proposal(
origin: OriginFor<T>,
proposal_id: u64,
Expand All @@ -281,21 +288,21 @@ pub mod pallet {
}

#[pallet::call_index(14)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::remove_vote_proposal(), DispatchClass::Normal, Pays::Yes))]
pub fn remove_vote_proposal(origin: OriginFor<T>, proposal_id: u64) -> DispatchResult {
let voter = ensure_signed(origin)?;
voting::remove_vote::<T>(voter, proposal_id)
}

#[pallet::call_index(15)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::enable_vote_delegation(), DispatchClass::Normal, Pays::Yes))]
pub fn enable_vote_delegation(origin: OriginFor<T>) -> DispatchResult {
let delegator = ensure_signed(origin)?;
voting::enable_delegation::<T>(delegator)
}

#[pallet::call_index(16)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::disable_vote_delegation(), DispatchClass::Normal, Pays::Yes))]
pub fn disable_vote_delegation(origin: OriginFor<T>) -> DispatchResult {
let delegator = ensure_signed(origin)?;
voting::disable_delegation::<T>(delegator)
Expand Down
Loading

0 comments on commit 199073c

Please sign in to comment.