Skip to content

Commit

Permalink
Add flow benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
vayesy committed Jun 16, 2022
1 parent e5d3570 commit eeaaef4
Show file tree
Hide file tree
Showing 4 changed files with 397 additions and 49 deletions.
16 changes: 11 additions & 5 deletions flow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ frame-system = { git = "https://github.com/paritytech/substrate", branch = "polk
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13", default-features = false, optional = true }

orml-traits = { path = "../../orml/traits", default-features = false }
orml-tokens = { path = "../../orml/tokens", optional = true }
orml-currencies = { path = "../../orml/currencies", optional = true }

gamedao-traits = { package = "gamedao-traits", path = "../traits", default-features = false }
gamedao-control = { package = "gamedao-control", path = "../control", optional = true }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
Expand All @@ -33,14 +37,14 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0
pallet-timestamp = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }

orml-tokens = { path = "../../orml/tokens", default-features = false }
orml-currencies = { path = "../../orml/currencies", default-features = false }

gamedao-control = { package = "gamedao-control", path = "../control", default-features = true }

[features]
default = ["std"]
runtime-benchmarks = ["frame-benchmarking"]
runtime-benchmarks = [
"frame-benchmarking",
"gamedao-traits/frame-benchmarking",
]
std = [
"codec/std",
"serde/std",
Expand All @@ -53,10 +57,12 @@ std = [
"sp-core/std",
"sp-std/std",
"sp-runtime/std",

"orml-traits/std",
"orml-tokens/std",
"orml-currencies/std",

"gamedao-traits/std",
"gamedao-control/std",
"gamedao-control/std",
]
try-runtime = ["frame-support/try-runtime"]
142 changes: 137 additions & 5 deletions flow/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,148 @@
//! Benchmarking setup for gamedao-flow
use super::*;

#[allow(unused)]
use crate::Pallet as Flow;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_system::RawOrigin;
use sp_std::vec;
use frame_support::traits::Hooks;
use sp_runtime::{DispatchError, traits::{Saturating}};
use sp_std::{vec};


fn fund_account<T: Config>(account_id: &T::AccountId, multiplier: Option<u32>) -> Result<(), DispatchError> {
let amount = T::MinContribution::get().saturating_mul(multiplier.unwrap_or(10).into());
T::Currency::deposit(T::ProtocolTokenId::get(), account_id, amount)?;
T::Currency::deposit(T::PaymentTokenId::get(), account_id, amount)?;
Ok(())
}

/// Fund accounts with tokens, needed for org interactions
fn fund_accounts<T: Config>(account_ids: &Vec<T::AccountId>, multiplier: Option<u32>) -> Result<(), DispatchError> {
for account_id in account_ids {
fund_account::<T>(&account_id, multiplier)?;
}
Ok(())
}

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];
Flow::<T>::create_campaign(
RawOrigin::Signed(caller.clone()).into(),
org_id,
caller.clone(),
name,
T::MinContribution::get(),
T::MinContribution::get(),
frame_system::Pallet::<T>::block_number() + 200_u32.into(),
Default::default(),
Default::default(),
cid,
token_name,
token_symbol
)?;
let nonce = Nonce::<T>::get() - 1u128;
Ok(T::Hashing::hash_of(&nonce))
}


benchmarks! {

simple_benchmark {}: {}
// <T as orml_tokens::Config>::Currency::set_balance(GameCurrencyId, &creator, 1000);
create_campaign {
let b in 0 .. T::MaxCampaignsPerBlock::get()-1; // already created campaigns at current block

let caller: T::AccountId = whitelisted_caller();
fund_account::<T>(&caller, None)?;
let org_id = T::Control::create_org(caller.clone().into())?;
let treasury_id = T::Control::org_treasury_account(&org_id);
fund_account::<T>(&treasury_id, None)?;
for i in 0..b {
create_campaign_call::<T>(caller.clone(), org_id.clone())?;
}
let count_before = CampaignsCount::<T>::get();

}: _(
RawOrigin::Signed(caller.clone()),
org_id,
caller.clone(),
vec![0; T::MaxNameLength::get() as usize],
T::MinContribution::get(),
T::MinContribution::get(),
200_u32.into(),
Default::default(),
Default::default(),
vec![0; T::MaxNameLength::get() as usize],
vec![0; 5],
vec![0; 32]
)
verify {
assert!(CampaignsCount::<T>::get() == count_before + 1);
}

update_state {
let caller: T::AccountId = whitelisted_caller();
fund_account::<T>(&caller, None)?;
let org_id = T::Control::create_org(caller.clone().into())?;
let treasury_id = T::Control::org_treasury_account(&org_id);
fund_account::<T>(&treasury_id, None)?;
let campaign_id = create_campaign_call::<T>(caller.clone(), org_id.clone())?;
let new_state = FlowState::Paused;
}: _(
RawOrigin::Signed(caller.clone()),
campaign_id,
new_state.clone()
)
verify {
assert!(CampaignState::<T>::get(&campaign_id) == new_state);
}

contribute {
let owner: T::AccountId = whitelisted_caller();
let contributor: T::AccountId = account("contributor", 0, 0);
fund_accounts::<T>(&vec![owner.clone(), contributor.clone()], None)?;
let org_id = T::Control::create_org(owner.clone().into())?;
let treasury_id = T::Control::org_treasury_account(&org_id);
fund_account::<T>(&treasury_id, None)?;
let campaign_id = create_campaign_call::<T>(owner, org_id)?;
}: _(
RawOrigin::Signed(contributor.clone()),
campaign_id.clone(),
T::MinContribution::get()
)
verify {
assert!(CampaignContribution::<T>::contains_key((&campaign_id, &contributor)));
}

on_initialize {
let b in 0 .. T::MaxContributorsProcessing::get(); // number of contributions in current block
let c in 0 .. T::MaxCampaignsPerBlock::get(); // number of campaigns in current block

let owner: T::AccountId = whitelisted_caller();
fund_account::<T>(&owner, None)?;
let org_id = T::Control::create_org(owner.clone().into())?;
let treasury_id = T::Control::org_treasury_account(&org_id);
fund_account::<T>(&treasury_id, None)?;
for _ in 0..c {
let campaign_id = create_campaign_call::<T>(owner.clone(), org_id.clone())?;
for i in 0..b {
let account: T::AccountId = account("contributor", i, 0);
fund_account::<T>(&account, None)?;
Pallet::<T>::contribute(RawOrigin::Signed(account).into(), campaign_id.clone(), T::MinContribution::get())?;
}
}
let nonce = Nonce::<T>::get().saturating_sub(1_u128);
let campaign_id = T::Hashing::hash_of(&nonce);
let campaign = Campaigns::<T>::get(&campaign_id);
let mut block_number = campaign.expiry;
frame_system::Pallet::<T>::set_block_number(block_number.clone());
Pallet::<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);
}

}

Expand Down
Loading

0 comments on commit eeaaef4

Please sign in to comment.