Skip to content

Commit

Permalink
Updated extrinsic. Added benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
vayesy committed Dec 16, 2022
1 parent fa02aea commit 39843da
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
2 changes: 1 addition & 1 deletion control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "polkado

pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.28" }

orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch= "polkadot-v0.9.28", default-features = false }
gamedao-traits = { package = "gamedao-traits", path = "../traits", default-features = false }
orml-traits = { git = 'https://github.com/open-web3-stack/open-runtime-module-library', branch= 'polkadot-v0.9.28', default-features = false }

# tangram = { package = "module-tangram", path = "../../zero/tangram", default-features = false }

Expand Down
22 changes: 19 additions & 3 deletions control/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(feature = "runtime-benchmarks")]

use crate::*;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_system::RawOrigin;
use sp_runtime::{DispatchError, traits::SaturatedConversion};
use sp_std::vec;
Expand Down Expand Up @@ -88,7 +88,7 @@ benchmarks! {
}

add_member {
let r in 1 .. T::MaxMembers::get();
let r in 1 .. T::MaxMembers::get()-1;

// Prepare org creator and members
let creator: T::AccountId = whitelisted_caller();
Expand All @@ -111,8 +111,24 @@ benchmarks! {
assert!(Members::<T>::get(&org_id).contains(&member));
}

update_member_state {
// Prepare org creator and members
let creator: T::AccountId = whitelisted_caller();
fund_account::<T>(&creator)?;
let org_id = <Pallet::<T> as ControlBenchmarkingTrait<T::AccountId, T::Hash>>::create_org(creator.clone()).unwrap();
let member: T::AccountId = account("member", 0, SEED);
fund_account::<T>(&member)?;

// Add member to org
Pallet::<T>::fill_org_with_members(&org_id, vec![member.clone()])?;
}: _(RawOrigin::Signed(creator), org_id, member.clone(), MemberState::Active)

verify {
assert!(MemberStates::<T>::get(org_id.clone(), member.clone()) == MemberState::Active);
}

remove_member {
let r in 1 .. T::MaxMembers::get();
let r in 1 .. T::MaxMembers::get()-1;

// Prepare org creator and members
let creator: T::AccountId = whitelisted_caller();
Expand Down
30 changes: 14 additions & 16 deletions control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub mod pallet {
MemberUpdated {
org_id: T::Hash,
who: T::AccountId,
state: T::MemberState,
state: MemberState,
block_number: T::BlockNumber,
},
OrgUpdated {
Expand Down Expand Up @@ -482,31 +482,29 @@ pub mod pallet {
Ok(Some(T::WeightInfo::add_member(members_count)).into())
}

/// Approve Membership Application
/// for users which apply to join and their state is `pending`
/// Update member state in the organization
///
/// Parameters:
/// - `org_id`: Org id
/// - `who`: Account to be set `active`
/// - `who`: Account to change state for
/// - `state`: new state value
///
/// Emits `MemberAdded` event when successful.
/// Emits `MemberUpdated` event when successful.
///
/// Weight: `O(log n)`
#[pallet::weight(T::WeightInfo::add_member(T::MaxMembers::get()))]
pub fn approve_member(
#[pallet::weight(T::WeightInfo::update_member_state())]
pub fn update_member_state(
origin: OriginFor<T>,
org_id: T::Hash,
who: T::AccountId
) -> DispatchResultWithPostInfo {
who: T::AccountId,
state: MemberState
) -> DispatchResult {
let org = Orgs::<T>::get(&org_id).ok_or(Error::<T>::OrganizationUnknown)?;
// why do we need so many parameters here?
// afaiu we need org and origin to determine any rights to execute
Self::ensure_membership_permissions(origin, who.clone(), org.prime.clone(), org.org_type.clone(), org.access_model.clone())?;

let current_member_state = MemberStates::<T>::get( org_id.clone(), who.clone() )?;
let current_member_state = MemberStates::<T>::get(org_id.clone(), who.clone());
if current_member_state == MemberState::Pending {
let update = Self::do_update_member( org_id, who.clone(), MemberState::Active )?;
Ok(Some(T::WeightInfo::approve_member(MemberState::Active)).into())
Self::do_update_member(org_id, who.clone(), state)?;
}
Ok(())
}
Expand Down Expand Up @@ -614,8 +612,8 @@ impl<T: Config> Pallet<T> {
org_id: T::Hash,
who: T::AccountId,
state: MemberState
) -> Result<u32, DispatchError> {
MemberStates::<T>::update(&org_id, &who, &state);
) -> Result<(), DispatchError> {
MemberStates::<T>::set(&org_id, &who, state.clone());
let block_number = frame_system::Pallet::<T>::block_number();
Self::deposit_event(Event::MemberUpdated { org_id, who, state, block_number });
Ok(())
Expand Down
61 changes: 38 additions & 23 deletions control/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
//! Autogenerated weights for gamedao_control
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-09-13, STEPS: `20`, REPEAT: 10, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2022-12-16, STEPS: `20`, REPEAT: 10, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 1024
// Executed Command:
// ./target/release/subzero
// ./target/release/subzero-dev
// benchmark
// pallet
// --pallet=gamedao_control
// --extrinsic=*
// --steps=20
// --repeat=10
// --output=gamedao-protocol/control/src/weights.rs
// --output=modules/gamedao-protocol/control/src/weights.rs
// --template=./.maintain/frame-weight-template.hbs

#![cfg_attr(rustfmt, rustfmt_skip)]
Expand All @@ -46,6 +46,7 @@ pub trait WeightInfo {
fn disable_org() -> Weight;
fn enable_org() -> Weight;
fn add_member(r: u32, ) -> Weight;
fn update_member_state() -> Weight;
fn remove_member(r: u32, ) -> Weight;
fn spend_funds() -> Weight;
}
Expand All @@ -63,28 +64,28 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Control MemberStates (r:0 w:1)
// Storage: Control OrgStates (r:0 w:1)
fn create_org() -> Weight {
(54_000_000 as Weight)
(75_523_000 as Weight)
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(10 as Weight))
}
// Storage: Control Orgs (r:1 w:1)
// Storage: Control MemberStates (r:1 w:0)
fn update_org() -> Weight {
(22_000_000 as Weight)
(33_049_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Control Orgs (r:1 w:0)
// Storage: Control OrgStates (r:0 w:1)
fn disable_org() -> Weight {
(18_000_000 as Weight)
(26_400_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Control Orgs (r:1 w:0)
// Storage: Control OrgStates (r:0 w:1)
fn enable_org() -> Weight {
(18_000_000 as Weight)
(25_846_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
Expand All @@ -94,20 +95,27 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Control OrgMemberCount (r:0 w:1)
// Storage: Control MemberStates (r:0 w:1)
fn add_member(r: u32, ) -> Weight {
(27_200_000 as Weight)
(46_146_000 as Weight)
// Standard Error: 2_000
.saturating_add((36_000 as Weight).saturating_mul(r as Weight))
.saturating_add((63_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(3 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
// Storage: Control Orgs (r:1 w:0)
// Storage: Control MemberStates (r:1 w:1)
fn update_member_state() -> Weight {
(26_240_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Control Orgs (r:1 w:0)
// Storage: Control Members (r:1 w:1)
// Storage: Control OrgMemberCount (r:0 w:1)
// Storage: Control MemberStates (r:0 w:1)
fn remove_member(r: u32, ) -> Weight {
(24_323_000 as Weight)
// Standard Error: 2_000
.saturating_add((31_000 as Weight).saturating_mul(r as Weight))
(44_807_000 as Weight)
// Standard Error: 3_000
.saturating_add((66_000 as Weight).saturating_mul(r as Weight))
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
Expand All @@ -116,7 +124,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Tokens Accounts (r:2 w:2)
// Storage: System Account (r:2 w:1)
fn spend_funds() -> Weight {
(45_000_000 as Weight)
(50_764_000 as Weight)
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(3 as Weight))
}
Expand All @@ -134,28 +142,28 @@ impl WeightInfo for () {
// Storage: Control MemberStates (r:0 w:1)
// Storage: Control OrgStates (r:0 w:1)
fn create_org() -> Weight {
(54_000_000 as Weight)
(75_523_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(6 as Weight))
.saturating_add(RocksDbWeight::get().writes(10 as Weight))
}
// Storage: Control Orgs (r:1 w:1)
// Storage: Control MemberStates (r:1 w:0)
fn update_org() -> Weight {
(22_000_000 as Weight)
(33_049_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Control Orgs (r:1 w:0)
// Storage: Control OrgStates (r:0 w:1)
fn disable_org() -> Weight {
(18_000_000 as Weight)
(26_400_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Control Orgs (r:1 w:0)
// Storage: Control OrgStates (r:0 w:1)
fn enable_org() -> Weight {
(18_000_000 as Weight)
(25_846_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
Expand All @@ -165,20 +173,27 @@ impl WeightInfo for () {
// Storage: Control OrgMemberCount (r:0 w:1)
// Storage: Control MemberStates (r:0 w:1)
fn add_member(r: u32, ) -> Weight {
(27_200_000 as Weight)
(46_146_000 as Weight)
// Standard Error: 2_000
.saturating_add((36_000 as Weight).saturating_mul(r as Weight))
.saturating_add((63_000 as Weight).saturating_mul(r as Weight))
.saturating_add(RocksDbWeight::get().reads(3 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
// Storage: Control Orgs (r:1 w:0)
// Storage: Control MemberStates (r:1 w:1)
fn update_member_state() -> Weight {
(26_240_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Control Orgs (r:1 w:0)
// Storage: Control Members (r:1 w:1)
// Storage: Control OrgMemberCount (r:0 w:1)
// Storage: Control MemberStates (r:0 w:1)
fn remove_member(r: u32, ) -> Weight {
(24_323_000 as Weight)
// Standard Error: 2_000
.saturating_add((31_000 as Weight).saturating_mul(r as Weight))
(44_807_000 as Weight)
// Standard Error: 3_000
.saturating_add((66_000 as Weight).saturating_mul(r as Weight))
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
Expand All @@ -187,7 +202,7 @@ impl WeightInfo for () {
// Storage: Tokens Accounts (r:2 w:2)
// Storage: System Account (r:2 w:1)
fn spend_funds() -> Weight {
(45_000_000 as Weight)
(50_764_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(6 as Weight))
.saturating_add(RocksDbWeight::get().writes(3 as Weight))
}
Expand Down

0 comments on commit 39843da

Please sign in to comment.