#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame::benchmarking::prelude::*;
use crate::Pallet as Multisig;
const SEED: u32 = 0;
fn setup_multi<T: Config>(
s: u32,
z: u32,
) -> Result<(Vec<T::AccountId>, Box<<T as Config>::RuntimeCall>), &'static str> {
let mut signatories: Vec<T::AccountId> = Vec::new();
for i in 0..s {
let signatory = account("signatory", i, SEED);
let balance = BalanceOf::<T>::max_value();
T::Currency::make_free_balance_be(&signatory, balance);
signatories.push(signatory);
}
signatories.sort();
let call: <T as Config>::RuntimeCall =
frame_system::Call::<T>::remark { remark: vec![0; z as usize] }.into();
Ok((signatories, Box::new(call)))
}
#[benchmarks]
mod benchmarks {
use super::*;
#[benchmark]
fn as_multi_threshold_1(z: Linear<0, 10_000>) -> Result<(), BenchmarkError> {
let max_signatories = T::MaxSignatories::get().into();
let (mut signatories, _) = setup_multi::<T>(max_signatories, z)?;
let call: <T as Config>::RuntimeCall =
frame_system::Call::<T>::remark { remark: vec![0; z as usize] }.into();
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
add_to_whitelist(caller_key.into());
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), signatories, Box::new(call));
Ok(())
}
#[benchmark]
fn as_multi_create(
s: Linear<2, { T::MaxSignatories::get() }>,
z: Linear<0, 10_000>,
) -> Result<(), BenchmarkError> {
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let call_hash = call.using_encoded(blake2_256);
let multi_account_id = Multisig::<T>::multi_account_id(&signatories, s.try_into().unwrap());
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
add_to_whitelist(caller_key.into());
#[extrinsic_call]
as_multi(RawOrigin::Signed(caller), s as u16, signatories, None, call, Weight::zero());
assert!(Multisigs::<T>::contains_key(multi_account_id, call_hash));
Ok(())
}
#[benchmark]
fn as_multi_approve(
s: Linear<3, { T::MaxSignatories::get() }>,
z: Linear<0, 10_000>,
) -> Result<(), BenchmarkError> {
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let call_hash = call.using_encoded(blake2_256);
let multi_account_id = Multisig::<T>::multi_account_id(&signatories, s.try_into().unwrap());
let mut signatories2 = signatories.clone();
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let timepoint = Multisig::<T>::timepoint();
Multisig::<T>::as_multi(
RawOrigin::Signed(caller).into(),
s as u16,
signatories,
None,
call.clone(),
Weight::zero(),
)?;
let caller2 = signatories2.remove(0);
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller2);
add_to_whitelist(caller_key.into());
#[extrinsic_call]
as_multi(
RawOrigin::Signed(caller2),
s as u16,
signatories2,
Some(timepoint),
call,
Weight::zero(),
);
let multisig =
Multisigs::<T>::get(multi_account_id, call_hash).ok_or("multisig not created")?;
assert_eq!(multisig.approvals.len(), 2);
Ok(())
}
#[benchmark]
fn as_multi_complete(
s: Linear<2, { T::MaxSignatories::get() }>,
z: Linear<0, 10_000>,
) -> Result<(), BenchmarkError> {
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let call_hash = call.using_encoded(blake2_256);
let multi_account_id = Multisig::<T>::multi_account_id(&signatories, s.try_into().unwrap());
let mut signatories2 = signatories.clone();
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let timepoint = Multisig::<T>::timepoint();
Multisig::<T>::as_multi(
RawOrigin::Signed(caller).into(),
s as u16,
signatories,
None,
call.clone(),
Weight::zero(),
)?;
for i in 1..s - 1 {
let mut signatories_loop = signatories2.clone();
let caller_loop = signatories_loop.remove(i as usize);
let o = RawOrigin::Signed(caller_loop).into();
Multisig::<T>::as_multi(
o,
s as u16,
signatories_loop,
Some(timepoint),
call.clone(),
Weight::zero(),
)?;
}
let caller2 = signatories2.remove(0);
assert!(Multisigs::<T>::contains_key(&multi_account_id, call_hash));
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller2);
add_to_whitelist(caller_key.into());
#[extrinsic_call]
as_multi(
RawOrigin::Signed(caller2),
s as u16,
signatories2,
Some(timepoint),
call,
Weight::MAX,
);
assert!(!Multisigs::<T>::contains_key(&multi_account_id, call_hash));
Ok(())
}
#[benchmark]
fn approve_as_multi_create(
s: Linear<2, { T::MaxSignatories::get() }>,
z: Linear<0, 10_000>,
) -> Result<(), BenchmarkError> {
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let multi_account_id = Multisig::<T>::multi_account_id(&signatories, s.try_into().unwrap());
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let call_hash = call.using_encoded(blake2_256);
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
add_to_whitelist(caller_key.into());
#[extrinsic_call]
approve_as_multi(
RawOrigin::Signed(caller),
s as u16,
signatories,
None,
call_hash,
Weight::zero(),
);
assert!(Multisigs::<T>::contains_key(multi_account_id, call_hash));
Ok(())
}
#[benchmark]
fn approve_as_multi_approve(
s: Linear<2, { T::MaxSignatories::get() }>,
z: Linear<0, 10_000>,
) -> Result<(), BenchmarkError> {
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let mut signatories2 = signatories.clone();
let multi_account_id = Multisig::<T>::multi_account_id(&signatories, s.try_into().unwrap());
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let call_hash = call.using_encoded(blake2_256);
let timepoint = Multisig::<T>::timepoint();
Multisig::<T>::as_multi(
RawOrigin::Signed(caller).into(),
s as u16,
signatories,
None,
call,
Weight::zero(),
)?;
let caller2 = signatories2.remove(0);
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller2);
add_to_whitelist(caller_key.into());
#[extrinsic_call]
approve_as_multi(
RawOrigin::Signed(caller2),
s as u16,
signatories2,
Some(timepoint),
call_hash,
Weight::zero(),
);
let multisig =
Multisigs::<T>::get(multi_account_id, call_hash).ok_or("multisig not created")?;
assert_eq!(multisig.approvals.len(), 2);
Ok(())
}
#[benchmark]
fn cancel_as_multi(
s: Linear<2, { T::MaxSignatories::get() }>,
z: Linear<0, 10_000>,
) -> Result<(), BenchmarkError> {
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let multi_account_id = Multisig::<T>::multi_account_id(&signatories, s.try_into().unwrap());
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let call_hash = call.using_encoded(blake2_256);
let timepoint = Multisig::<T>::timepoint();
let o = RawOrigin::Signed(caller.clone()).into();
Multisig::<T>::as_multi(o, s as u16, signatories.clone(), None, call, Weight::zero())?;
assert!(Multisigs::<T>::contains_key(&multi_account_id, call_hash));
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
add_to_whitelist(caller_key.into());
#[extrinsic_call]
_(RawOrigin::Signed(caller), s as u16, signatories, timepoint, call_hash);
assert!(!Multisigs::<T>::contains_key(multi_account_id, call_hash));
Ok(())
}
impl_benchmark_test_suite!(Multisig, crate::tests::new_test_ext(), crate::tests::Test);
}