Skip to content

Commit

Permalink
membership: Resturcture pallet into separate files
Browse files Browse the repository at this point in the history
Signed-off-by: Shreevatsa N <i.mnshreevatsa@gmail.com>
  • Loading branch information
vatsa287 committed May 22, 2024
1 parent ec46106 commit 92824db
Show file tree
Hide file tree
Showing 3 changed files with 528 additions and 0 deletions.
181 changes: 181 additions & 0 deletions substrate/frame/membership/src/benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Membership pallet benchmarking.

#![cfg(feature = "runtime-benchmarks")]

use super::{Pallet as Membership, *};
use frame_benchmarking::v1::{account, benchmarks_instance_pallet, whitelist, BenchmarkError};
use frame_support::{assert_ok, traits::EnsureOrigin};
use frame_system::RawOrigin;

const SEED: u32 = 0;

fn set_members<T: Config<I>, I: 'static>(members: Vec<T::AccountId>, prime: Option<usize>) {
let reset_origin = T::ResetOrigin::try_successful_origin()
.expect("ResetOrigin has no successful origin required for the benchmark");
let prime_origin = T::PrimeOrigin::try_successful_origin()
.expect("PrimeOrigin has no successful origin required for the benchmark");

assert_ok!(<Membership<T, I>>::reset_members(reset_origin, members.clone()));
if let Some(prime) = prime.map(|i| members[i].clone()) {
let prime_lookup = T::Lookup::unlookup(prime);
assert_ok!(<Membership<T, I>>::set_prime(prime_origin, prime_lookup));
} else {
assert_ok!(<Membership<T, I>>::clear_prime(prime_origin));
}
}

benchmarks_instance_pallet! {
add_member {
let m in 1 .. (T::MaxMembers::get() - 1);

let members = (0..m).map(|i| account("member", i, SEED)).collect::<Vec<T::AccountId>>();
set_members::<T, I>(members, None);
let new_member = account::<T::AccountId>("add", m, SEED);
let new_member_lookup = T::Lookup::unlookup(new_member.clone());
}: {
assert_ok!(<Membership<T, I>>::add_member(
T::AddOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
new_member_lookup,
));
} verify {
assert!(<Members<T, I>>::get().contains(&new_member));
#[cfg(test)] crate::tests::clean();
}

// the case of no prime or the prime being removed is surely cheaper than the case of
// reporting a new prime via `MembershipChanged`.
remove_member {
let m in 2 .. T::MaxMembers::get();

let members = (0..m).map(|i| account("member", i, SEED)).collect::<Vec<T::AccountId>>();
set_members::<T, I>(members.clone(), Some(members.len() - 1));

let to_remove = members.first().cloned().unwrap();
let to_remove_lookup = T::Lookup::unlookup(to_remove.clone());
}: {
assert_ok!(<Membership<T, I>>::remove_member(
T::RemoveOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
to_remove_lookup,
));
} verify {
assert!(!<Members<T, I>>::get().contains(&to_remove));
// prime is rejigged
assert!(<Prime<T, I>>::get().is_some() && T::MembershipChanged::get_prime().is_some());
#[cfg(test)] crate::tests::clean();
}

// we remove a non-prime to make sure it needs to be set again.
swap_member {
let m in 2 .. T::MaxMembers::get();

let members = (0..m).map(|i| account("member", i, SEED)).collect::<Vec<T::AccountId>>();
set_members::<T, I>(members.clone(), Some(members.len() - 1));
let add = account::<T::AccountId>("member", m, SEED);
let add_lookup = T::Lookup::unlookup(add.clone());
let remove = members.first().cloned().unwrap();
let remove_lookup = T::Lookup::unlookup(remove.clone());
}: {
assert_ok!(<Membership<T, I>>::swap_member(
T::SwapOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
remove_lookup,
add_lookup,
));
} verify {
assert!(!<Members<T, I>>::get().contains(&remove));
assert!(<Members<T, I>>::get().contains(&add));
// prime is rejigged
assert!(<Prime<T, I>>::get().is_some() && T::MembershipChanged::get_prime().is_some());
#[cfg(test)] crate::tests::clean();
}

// er keep the prime common between incoming and outgoing to make sure it is rejigged.
reset_members {
let m in 1 .. T::MaxMembers::get();

let members = (1..m+1).map(|i| account("member", i, SEED)).collect::<Vec<T::AccountId>>();
set_members::<T, I>(members.clone(), Some(members.len() - 1));
let mut new_members = (m..2*m).map(|i| account("member", i, SEED)).collect::<Vec<T::AccountId>>();
}: {
assert_ok!(<Membership<T, I>>::reset_members(
T::ResetOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
new_members.clone(),
));
} verify {
new_members.sort();
assert_eq!(<Members<T, I>>::get(), new_members);
// prime is rejigged
assert!(<Prime<T, I>>::get().is_some() && T::MembershipChanged::get_prime().is_some());
#[cfg(test)] crate::tests::clean();
}

change_key {
let m in 1 .. T::MaxMembers::get();

// worse case would be to change the prime
let members = (0..m).map(|i| account("member", i, SEED)).collect::<Vec<T::AccountId>>();
let prime = members.last().cloned().unwrap();
set_members::<T, I>(members.clone(), Some(members.len() - 1));

let add = account::<T::AccountId>("member", m, SEED);
let add_lookup = T::Lookup::unlookup(add.clone());
whitelist!(prime);
}: {
assert_ok!(<Membership<T, I>>::change_key(RawOrigin::Signed(prime.clone()).into(), add_lookup));
} verify {
assert!(!<Members<T, I>>::get().contains(&prime));
assert!(<Members<T, I>>::get().contains(&add));
// prime is rejigged
assert_eq!(<Prime<T, I>>::get().unwrap(), add);
#[cfg(test)] crate::tests::clean();
}

set_prime {
let m in 1 .. T::MaxMembers::get();
let members = (0..m).map(|i| account("member", i, SEED)).collect::<Vec<T::AccountId>>();
let prime = members.last().cloned().unwrap();
let prime_lookup = T::Lookup::unlookup(prime.clone());
set_members::<T, I>(members, None);
}: {
assert_ok!(<Membership<T, I>>::set_prime(
T::PrimeOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
prime_lookup,
));
} verify {
assert!(<Prime<T, I>>::get().is_some());
assert!(<T::MembershipChanged>::get_prime().is_some());
#[cfg(test)] crate::tests::clean();
}

clear_prime {
let members = (0..T::MaxMembers::get()).map(|i| account("member", i, SEED)).collect::<Vec<T::AccountId>>();
let prime = members.last().cloned().unwrap();
set_members::<T, I>(members, None);
}: {
assert_ok!(<Membership<T, I>>::clear_prime(
T::PrimeOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?,
));
} verify {
assert!(<Prime<T, I>>::get().is_none());
assert!(<T::MembershipChanged>::get_prime().is_none());
#[cfg(test)] crate::tests::clean();
}

impl_benchmark_test_suite!(Membership, crate::tests::new_bench_ext(), crate::tests::Test);
}
122 changes: 122 additions & 0 deletions substrate/frame/membership/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Test utilities

use super::*;
use crate as pallet_membership;

use sp_runtime::{bounded_vec, BuildStorage};

use frame_support::{
derive_impl, ord_parameter_types,
parameter_types,
traits::{ConstU32},
};
use frame_system::EnsureSignedBy;

type Block = frame_system::mocking::MockBlock<Test>;


frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
Membership: pallet_membership,
}
);

parameter_types! {
pub static Members: Vec<u64> = vec![];
pub static Prime: Option<u64> = None;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type Block = Block;
}
ord_parameter_types! {
pub const One: u64 = 1;
pub const Two: u64 = 2;
pub const Three: u64 = 3;
pub const Four: u64 = 4;
pub const Five: u64 = 5;
}

pub struct TestChangeMembers;
impl ChangeMembers<u64> for TestChangeMembers {
fn change_members_sorted(incoming: &[u64], outgoing: &[u64], new: &[u64]) {
let mut old_plus_incoming = Members::get();
old_plus_incoming.extend_from_slice(incoming);
old_plus_incoming.sort();
let mut new_plus_outgoing = new.to_vec();
new_plus_outgoing.extend_from_slice(outgoing);
new_plus_outgoing.sort();
assert_eq!(old_plus_incoming, new_plus_outgoing);

Members::set(new.to_vec());
Prime::set(None);
}
fn set_prime(who: Option<u64>) {
Prime::set(who);
}
fn get_prime() -> Option<u64> {
Prime::get()
}
}

impl InitializeMembers<u64> for TestChangeMembers {
fn initialize_members(members: &[u64]) {
MEMBERS.with(|m| *m.borrow_mut() = members.to_vec());
}
}

impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type AddOrigin = EnsureSignedBy<One, u64>;
type RemoveOrigin = EnsureSignedBy<Two, u64>;
type SwapOrigin = EnsureSignedBy<Three, u64>;
type ResetOrigin = EnsureSignedBy<Four, u64>;
type PrimeOrigin = EnsureSignedBy<Five, u64>;
type MembershipInitialized = TestChangeMembers;
type MembershipChanged = TestChangeMembers;
type MaxMembers = ConstU32<10>;
type WeightInfo = ();
}

pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
// We use default for brevity, but you can configure as desired if needed.
pallet_membership::GenesisConfig::<Test> {
members: bounded_vec![10, 20, 30],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
t.into()
}

#[cfg(feature = "runtime-benchmarks")]
pub(crate) fn new_bench_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
}

#[cfg(feature = "runtime-benchmarks")]
pub(crate) fn clean() {
Members::set(vec![]);
Prime::set(None);
}
Loading

0 comments on commit 92824db

Please sign in to comment.