Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks to Signal pallet #47

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 49 additions & 21 deletions control/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
use crate::*;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_system::RawOrigin;
use sp_runtime::{DispatchError, traits::{Saturating}};
use sp_runtime::{DispatchError, traits::SaturatedConversion};
use sp_std::vec;


const SEED: u32 = 0;
const DEPOSIT_AMOUNT: u128 = 10_000_000_000_000_000_000;


/// Fund account with tokens, needed for org and campaign interactions
fn fund_account<T: Config>(account_id: &T::AccountId) -> Result<(), DispatchError> {
let balance_amount: T::Balance = DEPOSIT_AMOUNT.saturated_into();
T::Currency::deposit(T::ProtocolTokenId::get(), account_id, balance_amount)?;
T::Currency::deposit(T::PaymentTokenId::get(), account_id, balance_amount)?;
Ok(())
}

/// Fund accounts with tokens, needed for org interactions
fn fund_accounts<T: Config>(account_ids: &Vec<T::AccountId>) -> Result<(), DispatchError> {
let multiplier: u32 = 2;
let amount = T::MinimumDeposit::get().saturating_mul(multiplier.into());
for account_id in account_ids {
T::Currency::deposit(T::ProtocolTokenId::get(), account_id, amount)?;
T::Currency::deposit(T::PaymentTokenId::get(), account_id, amount)?;
fund_account::<T>(&account_id)?;
}
Ok(())
}
Expand All @@ -25,7 +31,7 @@ benchmarks! {

create_org {
let caller: T::AccountId = whitelisted_caller();
fund_accounts::<T>(&vec![caller.clone()])?;
fund_account::<T>(&caller)?;
let org_nonce = Nonce::<T>::get();
}: _(
RawOrigin::Signed(caller.clone()),
Expand All @@ -47,26 +53,33 @@ benchmarks! {

disable_org {
let caller: T::AccountId = whitelisted_caller();
fund_accounts::<T>(&vec![caller.clone()])?;
fund_account::<T>(&caller)?;
let org_id = <Pallet::<T> as ControlBenchmarkingTrait<T::AccountId, T::Hash>>::create_org(caller.clone()).unwrap();
}: _(RawOrigin::Root, org_id)
}: _(
RawOrigin::Root,
org_id
)
verify {
assert!(OrgState::<T>::get(org_id) == ControlState::Inactive);
}

enable_org {
let caller: T::AccountId = whitelisted_caller();
fund_accounts::<T>(&vec![caller.clone()])?;
fund_account::<T>(&caller)?;
let org_id = <Pallet::<T> as ControlBenchmarkingTrait<T::AccountId, T::Hash>>::create_org(caller.clone()).unwrap();
Pallet::<T>::disable_org(RawOrigin::Root.into(), org_id);
}: _(RawOrigin::Root, org_id)
Pallet::<T>::disable_org(RawOrigin::Root.into(), org_id)?;
}: _(
RawOrigin::Root,
org_id
)
verify {
assert!(OrgState::<T>::get(org_id) == ControlState::Active);
}

add_member {
let r in 1 .. T::MaxMembersPerDAO::get()-1;
let r in 1 .. T::MaxMembersPerDAO::get()-1; // Limit members per org

// Prepare org creator and members
let creator: T::AccountId = whitelisted_caller();
let member: T::AccountId = account("member", 0, SEED);
let accounts: Vec<T::AccountId> = (1..r)
Expand All @@ -76,38 +89,53 @@ benchmarks! {
.collect();
fund_accounts::<T>(&vec![creator.clone(), member.clone()])?;
fund_accounts::<T>(&accounts)?;

// Create org and fill with members
let org_id = <Pallet::<T> as ControlBenchmarkingTrait<T::AccountId, T::Hash>>::create_org(creator.clone()).unwrap();
Pallet::<T>::fill_org_with_members(&org_id, &accounts)?;
}: _(RawOrigin::Signed(creator), org_id, member.clone())
}: _(
RawOrigin::Signed(creator),
org_id,
member.clone()
)
verify {
assert!(OrgMembers::<T>::get(&org_id).contains(&member));
}

remove_member {
let r in 1 .. T::MaxMembersPerDAO::get();
let r in 1 .. T::MaxMembersPerDAO::get(); // Limit members per org

// Prepare org creator and members
let creator: T::AccountId = whitelisted_caller();
fund_accounts::<T>(&vec![creator.clone()])?;
fund_account::<T>(&creator)?;
let org_id = <Pallet::<T> as ControlBenchmarkingTrait<T::AccountId, T::Hash>>::create_org(creator.clone()).unwrap();
let origin = RawOrigin::Signed(creator.clone());
let accounts: Vec<T::AccountId> = (1..r+1)
.collect::<Vec<u32>>()
.iter()
.map(|i| account("member", *i, SEED))
.collect();
fund_accounts::<T>(&accounts)?;

// Add members to org
Pallet::<T>::fill_org_with_members(&org_id, &accounts)?;
}: _(origin, org_id, accounts[0].clone())
}: _(
RawOrigin::Signed(creator),
org_id,
accounts[0].clone()
)
verify {
assert!(!OrgMembers::<T>::get(&org_id).contains(&accounts[0]));
}

check_membership {
let caller: T::AccountId = whitelisted_caller();
fund_accounts::<T>(&vec![caller.clone()])?;
fund_account::<T>(&caller)?;
let org_id = <Pallet::<T> as ControlBenchmarkingTrait<T::AccountId, T::Hash>>::create_org(caller.clone()).unwrap();
}: _(RawOrigin::Signed(caller.clone()), org_id, caller.clone())

}: _(
RawOrigin::Signed(caller.clone()),
org_id,
caller.clone()
)

}

Expand Down
47 changes: 9 additions & 38 deletions flow/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use sp_std::vec;


const SEED: u32 = 0;
const DEPOSIT_AMOUNT: u128 = 10000000000000000000;
const DEPOSIT_AMOUNT: u128 = 10_000_000_000_000_000_000;


/// Fund account with tokens, needed for org and campaign interactions
Expand All @@ -31,36 +31,7 @@ fn fund_accounts<T: Config>(account_ids: &Vec<T::AccountId>) -> Result<(), Dispa
/// Switch to next block number
fn next_block<T: Config>() {
let current_block = frame_system::Pallet::<T>::block_number();
frame_system::Pallet::<T>::set_block_number(current_block.saturating_add(1_u32.into()));
}

/// Execute `create_campaign` extrinsic and return id of created campaign object
fn create_campaign_call<T: Config>(caller: T::AccountId, org_id: T::Hash) -> Result<T::Hash, &'static str> {
let name: Vec<u8> = vec![0; T::MaxNameLength::get() as usize];
let cid: Vec<u8> = vec![0; T::MaxNameLength::get() as usize];
let token_symbol: Vec<u8> = vec![0; 5];
let token_name: Vec<u8> = vec![0; 32];
let target: T::Balance = T::MinContribution::get();
let deposit: T::Balance = T::MinContribution::get();
let expiry: T::BlockNumber = frame_system::Pallet::<T>::block_number() + 200_u32.into();
let protocol: FlowProtocol = FlowProtocol::default();
let governance: FlowGovernance = FlowGovernance::default();
let nonce = Nonce::<T>::get();
Flow::<T>::create_campaign(
RawOrigin::Signed(caller.clone()).into(),
org_id,
caller.clone(),
name,
target,
deposit,
expiry,
protocol,
governance,
cid,
token_name,
token_symbol
)?;
Ok(T::Hashing::hash_of(&nonce))
frame_system::Pallet::<T>::set_block_number(current_block + 1_u32.into());
}


Expand All @@ -80,7 +51,7 @@ benchmarks! {

// Create some campaigns, respecting per block limitation
for i in 0 .. b {
create_campaign_call::<T>(caller.clone(), org_id)?;
<Flow::<T> as FlowBenchmarkingTrait<T::AccountId, T::BlockNumber, T::Hash>>::create_campaign(&caller, &org_id)?;
if i % per_block_cnt == 0 {
next_block::<T>();
}
Expand Down Expand Up @@ -120,7 +91,7 @@ benchmarks! {

// Create some campaigns, respecting per block limitation
for i in 0 .. b {
create_campaign_call::<T>(caller.clone(), org_id)?;
<Flow::<T> as FlowBenchmarkingTrait<T::AccountId, T::BlockNumber, T::Hash>>::create_campaign(&caller, &org_id)?;
if i % per_block_cnt == 0 {
next_block::<T>();
}
Expand Down Expand Up @@ -151,7 +122,7 @@ benchmarks! {
fund_account::<T>(&treasury_id)?;

// Create campaign to use for contributions
let campaign_id = create_campaign_call::<T>(owner, org_id)?;
let campaign_id = <Flow::<T> as FlowBenchmarkingTrait<T::AccountId, T::BlockNumber, T::Hash>>::create_campaign(&owner, &org_id)?;
}: _(
RawOrigin::Signed(contributor.clone()),
campaign_id.clone(),
Expand All @@ -173,21 +144,21 @@ benchmarks! {
fund_account::<T>(&treasury_id)?;

// Create campaign and add some contributions
let campaign_id = create_campaign_call::<T>(owner.clone(), org_id.clone())?;
let campaign_id = <Flow::<T> as FlowBenchmarkingTrait<T::AccountId, T::BlockNumber, T::Hash>>::create_campaign(&owner, &org_id)?;
for i in 0 .. c {
let account: T::AccountId = account("contributor", i, SEED);
fund_account::<T>(&account)?;
Pallet::<T>::contribute(RawOrigin::Signed(account).into(), campaign_id.clone(), T::MinContribution::get())?;
Flow::<T>::contribute(RawOrigin::Signed(account).into(), campaign_id.clone(), T::MinContribution::get())?;
}

// Trigger on_finalize to prepare object to be used in initialize hook
let mut block_number = Campaigns::<T>::get(&campaign_id).expiry;
frame_system::Pallet::<T>::set_block_number(block_number.clone());
Pallet::<T>::on_finalize(block_number.clone());
Flow::<T>::on_finalize(block_number.clone());
block_number = block_number.saturating_add(1_u32.into());
frame_system::Pallet::<T>::set_block_number(block_number);
}: {
Pallet::<T>::on_initialize(block_number);
Flow::<T>::on_initialize(block_number);
}

}
Expand Down
65 changes: 60 additions & 5 deletions flow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ pub mod weights;
// mod errors;

use frame_support::{
dispatch::{DispatchResult, DispatchResultWithPostInfo},
traits::{Get, BalanceStatus, StorageVersion, UnixTime},
dispatch::{DispatchError, DispatchResult, DispatchResultWithPostInfo},
traits::{Get, BalanceStatus, Hooks, StorageVersion, UnixTime},
transactional,
weights::Weight
};

use scale_info::TypeInfo;
use sp_runtime::{traits::{AtLeast32BitUnsigned, Hash}, Permill};
use sp_std::vec::Vec;
use sp_runtime::{traits::{AtLeast32BitUnsigned, Hash, Saturating}, Permill};
use sp_std::{vec, vec::Vec};

use sp_std::convert::TryFrom;

use codec::HasCompact;
use gamedao_traits::{ControlTrait, ControlBenchmarkingTrait, FlowTrait};
use gamedao_traits::{ControlTrait, ControlBenchmarkingTrait, FlowTrait, FlowBenchmarkingTrait};
use orml_traits::{MultiCurrency, MultiReservableCurrency};

pub use pallet::*;
Expand Down Expand Up @@ -1021,3 +1021,58 @@ impl<T: Config> FlowTrait<T::AccountId, T::Balance, T::Hash> for Pallet<T> {
CampaignState::<T>::get(campaign_id) == FlowState::Success
}
}

impl<T: Config> FlowBenchmarkingTrait<T::AccountId, T::BlockNumber, T::Hash> for Pallet<T> {
/// ** Should be used for benchmarking only!!! **
#[cfg(feature = "runtime-benchmarks")]
fn create_campaign(caller: &T::AccountId, org_id: &T::Hash) -> Result<T::Hash, &'static str> {
let name: Vec<u8> = vec![0; T::MaxNameLength::get() as usize];
let cid: Vec<u8> = vec![0; T::MaxNameLength::get() as usize];
let token_symbol: Vec<u8> = vec![0; 5];
let token_name: Vec<u8> = vec![0; 32];
let target: T::Balance = T::MinContribution::get();
let deposit: T::Balance = T::MinContribution::get();
let expiry: T::BlockNumber = frame_system::Pallet::<T>::block_number() + 200_u32.into();
let protocol: FlowProtocol = FlowProtocol::default();
let governance: FlowGovernance = FlowGovernance::default();
let nonce = Nonce::<T>::get();
Pallet::<T>::create_campaign(
frame_system::RawOrigin::Signed(caller.clone()).into(),
*org_id,
caller.clone(),
name,
target,
deposit,
expiry,
protocol,
governance,
cid,
token_name,
token_symbol
)?;
Ok(T::Hashing::hash_of(&nonce))
}

/// ** Should be used for benchmarking only!!! **
#[cfg(feature = "runtime-benchmarks")]
fn create_contributions(campaign_id: &T::Hash, contributors: &Vec<T::AccountId>) -> Result<(), DispatchError> {
for account_id in contributors {
Pallet::<T>::contribute(
frame_system::RawOrigin::Signed(account_id.clone()).into(),
campaign_id.clone(),
T::MinContribution::get()
)?;
}
Ok(())
}

/// ** Should be used for benchmarking only!!! **
#[cfg(feature = "runtime-benchmarks")]
fn finalize_campaigns_by_block(block_number: T::BlockNumber) {
frame_system::Pallet::<T>::set_block_number(block_number);
Pallet::<T>::on_finalize(block_number);
let next_block = block_number.saturating_add(1_u32.into());
frame_system::Pallet::<T>::set_block_number(next_block);
Pallet::<T>::on_initialize(next_block);
}
}
Loading