Skip to content

Commit

Permalink
Updated storage CampaignsByState. Added migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
vayesy committed Jun 21, 2022
1 parent 1b246c9 commit 49374cc
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 173 deletions.
132 changes: 89 additions & 43 deletions flow/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,97 @@ use crate::Pallet as Flow;
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}};
use sp_std::{vec};
use sp_runtime::{DispatchError, traits::{Saturating, SaturatedConversion}};
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)?;
const SEED: u32 = 0;
const DEPOSIT_AMOUNT: u128 = 10000000000000000000;


/// 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>, multiplier: Option<u32>) -> Result<(), DispatchError> {
fn fund_accounts<T: Config>(account_ids: &Vec<T::AccountId>) -> Result<(), DispatchError> {
for account_id in account_ids {
fund_account::<T>(&account_id, multiplier)?;
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()));
}

/// 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,
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
)?;
let nonce = Nonce::<T>::get() - 1u128;
Ok(T::Hashing::hash_of(&nonce))
}


benchmarks! {

create_campaign {
let b in 0 .. T::MaxCampaignsPerBlock::get()-1; // already created campaigns at current block
let b in 0 .. T::MaxCampaignsPerOrg::get()-1; // limit to total campaigns per organization

let caller: T::AccountId = whitelisted_caller();
fund_account::<T>(&caller, None)?;
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, None)?;
for i in 0..b {
create_campaign_call::<T>(caller.clone(), org_id.clone())?;
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>();
}
}
let count_before = CampaignsCount::<T>::get();

// 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(),
200_u32.into(),
frame_system::Pallet::<T>::block_number() + 200_u32.into(),
Default::default(),
Default::default(),
vec![0; T::MaxNameLength::get() as usize],
Expand All @@ -82,12 +107,27 @@ benchmarks! {
}

update_state {
let b in 1 .. T::MaxCampaignsPerOrg::get(); // limit to campaigns per organization

let caller: T::AccountId = whitelisted_caller();
fund_account::<T>(&caller, None)?;
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, None)?;
let campaign_id = create_campaign_call::<T>(caller.clone(), org_id.clone())?;
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()),
Expand All @@ -99,12 +139,18 @@ benchmarks! {
}

contribute {

// Prepare owner and contributor accounts
let owner: T::AccountId = whitelisted_caller();
let contributor: T::AccountId = account("contributor", 0, 0);
fund_accounts::<T>(&vec![owner.clone(), contributor.clone()], None)?;
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, None)?;
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()),
Expand All @@ -116,26 +162,26 @@ benchmarks! {
}

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

let owner: T::AccountId = whitelisted_caller();
fund_account::<T>(&owner, None)?;
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, 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())?;
}
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())?;
}
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;

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

0 comments on commit 49374cc

Please sign in to comment.