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 Flow pallet #45

Merged
merged 2 commits 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
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"]
186 changes: 182 additions & 4 deletions flow/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,194 @@
//! 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 frame_support::traits::Hooks;
use sp_runtime::{DispatchError, traits::{Saturating, SaturatedConversion}};
use sp_std::vec;


const SEED: u32 = 0;
const DEPOSIT_AMOUNT: u128 = 10000000000000000000;
vayesy marked this conversation as resolved.
Show resolved Hide resolved


/// 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(())
}

fn fund_accounts<T: Config>(account_ids: &Vec<T::AccountId>) -> Result<(), DispatchError> {
for account_id in account_ids {
fund_account::<T>(&account_id)?;
}
Ok(())
}

/// 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()));
vayesy marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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> {
vayesy marked this conversation as resolved.
Show resolved Hide resolved
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))
}


benchmarks! {

simple_benchmark {}: {}
// <T as orml_tokens::Config>::Currency::set_balance(GameCurrencyId, &creator, 1000);
create_campaign {
let b in 0 .. T::MaxCampaignsPerOrg::get()-1; // limit to total campaigns per organization

let caller: T::AccountId = whitelisted_caller();
fund_account::<T>(&caller)?;

// Prepare organization and treasury
let per_block_cnt = T::MaxCampaignsPerBlock::get();
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)?;

// Create some campaigns, respecting per block limitation
for i in 0 .. b {
create_campaign_call::<T>(caller.clone(), org_id)?;
if i % per_block_cnt == 0 {
next_block::<T>();
}
}

// Save number of existing campaigns to compare to new count after extrinsic called
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(),
frame_system::Pallet::<T>::block_number() + 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 b in 1 .. T::MaxCampaignsPerOrg::get(); // limit to campaigns per organization

let caller: T::AccountId = whitelisted_caller();
fund_account::<T>(&caller)?;

// Prepare organization and treasury
let per_block_cnt = T::MaxCampaignsPerBlock::get();
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)?;

// Create some campaigns, respecting per block limitation
for i in 0 .. b {
create_campaign_call::<T>(caller.clone(), org_id)?;
if i % per_block_cnt == 0 {
next_block::<T>();
}
}

// Use last campaign to call extrinsic with
let campaign_id = T::Hashing::hash_of(&Nonce::<T>::get().saturating_sub(1_u128));
let new_state = FlowState::Paused;
}: _(
RawOrigin::Signed(caller.clone()),
campaign_id,
new_state.clone()
)
verify {
assert!(CampaignState::<T>::get(&campaign_id) == new_state);
}

contribute {

// Prepare owner and contributor accounts
let owner: T::AccountId = whitelisted_caller();
let contributor: T::AccountId = account("contributor", 0, SEED);
fund_accounts::<T>(&vec![owner.clone(), contributor.clone()])?;

// Prepare organization and treasury
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)?;

// Create campaign to use for contributions
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 c in 1 .. T::MaxContributorsProcessing::get(); // number of contributions in current block

let owner: T::AccountId = whitelisted_caller();
fund_account::<T>(&owner)?;

// Prepare organization and treasury
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)?;

// Create campaign and add some contributions
let campaign_id = create_campaign_call::<T>(owner.clone(), org_id.clone())?;
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())?;
}

// 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());
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