Skip to content

Commit

Permalink
Added benchmarks for signal pallet. Fixups for Control & Flow benchma…
Browse files Browse the repository at this point in the history
…rks related code.
  • Loading branch information
vayesy committed Jun 23, 2022
1 parent aaba6c5 commit db6d211
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 109 deletions.
68 changes: 48 additions & 20 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,37 +89,52 @@ 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), org_id)
}: _(
RawOrigin::Signed(caller),
org_id
)


}
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
23 changes: 14 additions & 9 deletions flow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ 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
};
Expand Down Expand Up @@ -1029,17 +1029,22 @@ impl<T: Config> FlowBenchmarkingTrait<T::AccountId, T::BlockNumber, T::Hash> for
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.clone(),
*org_id,
caller.clone(),
name,
T::MinContribution::get(),
T::MinContribution::get(),
frame_system::Pallet::<T>::block_number() + 200_u32.into(),
Default::default(),
Default::default(),
target,
deposit,
expiry,
protocol,
governance,
cid,
token_name,
token_symbol
Expand All @@ -1053,7 +1058,7 @@ impl<T: Config> FlowBenchmarkingTrait<T::AccountId, T::BlockNumber, T::Hash> for
frame_system::RawOrigin::Signed(account_id.clone()).into(),
campaign_id.clone(),
T::MinContribution::get()
)?
)?;
}
Ok(())
}
Expand Down
Loading

0 comments on commit db6d211

Please sign in to comment.