Calculate and refund weight for identity pallet (#5680)

* add old_registrar_count as param to estimate weight

* cast count to Weight

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* add weight calculation for set_identity

* remove superfluous weight comment

* add detailed weight estimation for set_subs

* adjust benchmarking code to the new API

* add second parameter to set_subs benchmark

* rename o to p

* calculate weight based on benchmarks

* use try_mutate for registrars

* fix weight number typo

* update weights for set_subs + add weights for clear_identity and request_judgement

* improve naming and docs

* add weight calculation for cancel_request

* fix benchmark

* fix tests

* fix arithmetic overflow in balances triggered by tests

* add weight calcluations for more dispatchables

* add weight calculation for provide_judgement

* mark param as unused

* add MaxRegistrars associated type used for weight estimation

* check that MaxRegistrars is not exceeded

* add remaining weight calculations

* use weight refunds to use more constants in weight estimation

* adjust usage of clear_identity

* refund request_judgement weights and remove param

* refund weights for cancel_request and remove param

* add remaining refunds and remove params

* refund weight for set_subs and remove param

* make comment more specific

* add range note to benchmarking docs

* fix inconsistencies before review

* fix actual weight calculation for add_registrar

* remove duplicate balance ops weights + refund on all dispatchables

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Alexander Popiak
2020-04-24 16:46:58 +02:00
committed by GitHub
parent 72ee7d5797
commit 8a3dcd6862
5 changed files with 341 additions and 78 deletions
+27 -22
View File
@@ -27,9 +27,6 @@ use sp_runtime::traits::Bounded;
use crate::Module as Identity;
// The maximum number of identity registrars we will test.
const MAX_REGISTRARS: u32 = 50;
// Support Functions
fn account<T: Trait>(name: &'static str, index: u32) -> T::AccountId {
let entropy = (name, index).using_encoded(blake2_256);
@@ -53,9 +50,9 @@ fn add_registrars<T: Trait>(r: u32) -> Result<(), &'static str> {
Ok(())
}
// Adds `s` sub-accounts to the identity of `who`. Each wil have 32 bytes of raw data added to it.
// This additionally returns the vector of sub-accounts to it can be modified if needed.
fn add_sub_accounts<T: Trait>(who: &T::AccountId, s: u32) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
// Create `s` sub-accounts for the identity of `who` and return them.
// Each will have 32 bytes of raw data added to it.
fn create_sub_accounts<T: Trait>(who: &T::AccountId, s: u32) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
let mut subs = Vec::new();
let who_origin = RawOrigin::Signed(who.clone());
let data = Data::Raw(vec![0; 32]);
@@ -70,9 +67,18 @@ fn add_sub_accounts<T: Trait>(who: &T::AccountId, s: u32) -> Result<Vec<(T::Acco
let info = create_identity_info::<T>(1);
Identity::<T>::set_identity(who_origin.clone().into(), info)?;
Ok(subs)
}
// Adds `s` sub-accounts to the identity of `who`. Each will have 32 bytes of raw data added to it.
// This additionally returns the vector of sub-accounts so it can be modified if needed.
fn add_sub_accounts<T: Trait>(who: &T::AccountId, s: u32) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
let who_origin = RawOrigin::Signed(who.clone());
let subs = create_sub_accounts::<T>(who, s)?;
Identity::<T>::set_subs(who_origin.into(), subs.clone())?;
return Ok(subs)
Ok(subs)
}
// This creates an `IdentityInfo` object with `num_fields` extra fields.
@@ -98,7 +104,9 @@ fn create_identity_info<T: Trait>(num_fields: u32) -> IdentityInfo {
benchmarks! {
// These are the common parameters along with their instancing.
_ {
let r in 1 .. MAX_REGISTRARS => add_registrars::<T>(r)?;
let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;
// extra parameter for the set_subs bench for previous sub accounts
let p in 1 .. T::MaxSubAccounts::get() => ();
let s in 1 .. T::MaxSubAccounts::get() => {
// Give them s many sub accounts
let caller = account::<T>("caller", 0);
@@ -114,7 +122,7 @@ benchmarks! {
}
add_registrar {
let r in ...;
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
}: _(RawOrigin::Root, account::<T>("registrar", r + 1))
set_identity {
@@ -153,16 +161,13 @@ benchmarks! {
set_subs {
let caller = account::<T>("caller", 0);
// Give them s many sub accounts.
let s in 1 .. T::MaxSubAccounts::get() - 1 => {
let _ = add_sub_accounts::<T>(&caller, s)?;
// Give them p many previous sub accounts.
let p in 1 .. T::MaxSubAccounts::get() => {
let _ = add_sub_accounts::<T>(&caller, p)?;
};
let mut subs = Module::<T>::subs(&caller);
// Create an s + 1 sub account.
let data = Data::Raw(vec![0; 32]);
subs.push((account::<T>("sub", s + 1), data));
// Create a new subs vec with s sub accounts
let s in 1 .. T::MaxSubAccounts::get() => ();
let subs = create_sub_accounts::<T>(&caller, s)?;
}: _(RawOrigin::Signed(caller), subs)
@@ -210,7 +215,7 @@ benchmarks! {
set_fee {
let caller = account::<T>("caller", 0);
let r in ...;
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
Identity::<T>::add_registrar(RawOrigin::Root.into(), caller.clone())?;
}: _(RawOrigin::Signed(caller), r, 10.into())
@@ -219,7 +224,7 @@ benchmarks! {
let caller = account::<T>("caller", 0);
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let r in ...;
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
Identity::<T>::add_registrar(RawOrigin::Root.into(), caller.clone())?;
}: _(RawOrigin::Signed(caller), r, account::<T>("new", 0))
@@ -228,7 +233,7 @@ benchmarks! {
let caller = account::<T>("caller", 0);
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let r in ...;
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
Identity::<T>::add_registrar(RawOrigin::Root.into(), caller.clone())?;
let fields = IdentityFields(
@@ -247,7 +252,7 @@ benchmarks! {
let caller = account::<T>("caller", 0);
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let r in ...;
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
// For this x, it's the user identity that gts the fields, not the caller.
let x in _ .. _ => {
let info = create_identity_info::<T>(x);