// This file is part of Substrate. // Copyright (C) 2020-2021 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. //! Identity pallet benchmarking. #![cfg(feature = "runtime-benchmarks")] use super::*; use frame_system::RawOrigin; use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use sp_runtime::traits::Bounded; use frame_support::{ensure, traits::Get}; use crate::Pallet as Identity; const SEED: u32 = 0; fn assert_last_event(generic_event: ::Event) { frame_system::Pallet::::assert_last_event(generic_event.into()); } // Adds `r` registrars to the Identity Pallet. These registrars will have set fees and fields. fn add_registrars(r: u32) -> Result<(), &'static str> { for i in 0..r { let registrar: T::AccountId = account("registrar", i, SEED); let _ = T::Currency::make_free_balance_be(®istrar, BalanceOf::::max_value()); Identity::::add_registrar(RawOrigin::Root.into(), registrar.clone())?; Identity::::set_fee(RawOrigin::Signed(registrar.clone()).into(), i.into(), 10u32.into())?; let fields = IdentityFields( IdentityField::Display | IdentityField::Legal | IdentityField::Web | IdentityField::Riot | IdentityField::Email | IdentityField::PgpFingerprint | IdentityField::Image | IdentityField::Twitter ); Identity::::set_fields(RawOrigin::Signed(registrar.clone()).into(), i.into(), fields)?; } assert_eq!(Registrars::::get().len(), r as usize); Ok(()) } // 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(who: &T::AccountId, s: u32) -> Result, &'static str> { let mut subs = Vec::new(); let who_origin = RawOrigin::Signed(who.clone()); let data = Data::Raw(vec![0; 32]); for i in 0..s { let sub_account = account("sub", i, SEED); subs.push((sub_account, data.clone())); } // Set identity so `set_subs` does not fail. let _ = T::Currency::make_free_balance_be(&who, BalanceOf::::max_value()); let info = create_identity_info::(1); Identity::::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(who: &T::AccountId, s: u32) -> Result, &'static str> { let who_origin = RawOrigin::Signed(who.clone()); let subs = create_sub_accounts::(who, s)?; Identity::::set_subs(who_origin.into(), subs.clone())?; Ok(subs) } // This creates an `IdentityInfo` object with `num_fields` extra fields. // All data is pre-populated with some arbitrary bytes. fn create_identity_info(num_fields: u32) -> IdentityInfo { let data = Data::Raw(vec![0; 32]); let info = IdentityInfo { additional: vec![(data.clone(), data.clone()); num_fields as usize], display: data.clone(), legal: data.clone(), web: data.clone(), riot: data.clone(), email: data.clone(), pgp_fingerprint: Some([0; 20]), image: data.clone(), twitter: data.clone(), }; return info } benchmarks! { add_registrar { let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::(r)?; ensure!(Registrars::::get().len() as u32 == r, "Registrars not set up correctly."); }: _(RawOrigin::Root, account("registrar", r + 1, SEED)) verify { ensure!(Registrars::::get().len() as u32 == r + 1, "Registrars not added."); } set_identity { let r in 1 .. T::MaxRegistrars::get() => add_registrars::(r)?; let x in 1 .. T::MaxAdditionalFields::get(); let caller = { // The target user let caller: T::AccountId = whitelisted_caller(); let caller_lookup: ::Source = T::Lookup::unlookup(caller.clone()); let caller_origin: ::Origin = RawOrigin::Signed(caller.clone()).into(); let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); // Add an initial identity let initial_info = create_identity_info::(1); Identity::::set_identity(caller_origin.clone(), initial_info)?; // User requests judgement from all the registrars, and they approve for i in 0..r { Identity::::request_judgement(caller_origin.clone(), i, 10u32.into())?; Identity::::provide_judgement( RawOrigin::Signed(account("registrar", i, SEED)).into(), i, caller_lookup.clone(), Judgement::Reasonable )?; } caller }; }: _(RawOrigin::Signed(caller.clone()), create_identity_info::(x)) verify { assert_last_event::(Event::::IdentitySet(caller).into()); } // We need to split `set_subs` into two benchmarks to accurately isolate the potential // writes caused by new or old sub accounts. The actual weight should simply be // the sum of these two weights. set_subs_new { let caller: T::AccountId = whitelisted_caller(); // Create a new subs vec with s sub accounts let s in 1 .. T::MaxSubAccounts::get() => (); let subs = create_sub_accounts::(&caller, s)?; ensure!(SubsOf::::get(&caller).1.len() == 0, "Caller already has subs"); }: set_subs(RawOrigin::Signed(caller.clone()), subs) verify { ensure!(SubsOf::::get(&caller).1.len() as u32 == s, "Subs not added"); } set_subs_old { let caller: T::AccountId = whitelisted_caller(); // Give them p many previous sub accounts. let p in 1 .. T::MaxSubAccounts::get() => { let _ = add_sub_accounts::(&caller, p)?; }; // Remove all subs. let subs = create_sub_accounts::(&caller, 0)?; ensure!( SubsOf::::get(&caller).1.len() as u32 == p, "Caller does have subs", ); }: set_subs(RawOrigin::Signed(caller.clone()), subs) verify { ensure!(SubsOf::::get(&caller).1.len() == 0, "Subs not removed"); } clear_identity { let caller: T::AccountId = whitelisted_caller(); let caller_origin = ::Origin::from(RawOrigin::Signed(caller.clone())); let caller_lookup = ::unlookup(caller.clone()); let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); let r in 1 .. T::MaxRegistrars::get() => add_registrars::(r)?; let s in 1 .. T::MaxSubAccounts::get() => { // Give them s many sub accounts let caller: T::AccountId = whitelisted_caller(); let _ = add_sub_accounts::(&caller, s)?; }; let x in 1 .. T::MaxAdditionalFields::get() => { // Create their main identity with x additional fields let info = create_identity_info::(x); let caller: T::AccountId = whitelisted_caller(); let caller_origin = ::Origin::from(RawOrigin::Signed(caller)); Identity::::set_identity(caller_origin, info)?; }; // User requests judgement from all the registrars, and they approve for i in 0..r { Identity::::request_judgement(caller_origin.clone(), i, 10u32.into())?; Identity::::provide_judgement( RawOrigin::Signed(account("registrar", i, SEED)).into(), i, caller_lookup.clone(), Judgement::Reasonable )?; } ensure!(IdentityOf::::contains_key(&caller), "Identity does not exist."); }: _(RawOrigin::Signed(caller.clone())) verify { ensure!(!IdentityOf::::contains_key(&caller), "Identity not cleared."); } request_judgement { let caller: T::AccountId = whitelisted_caller(); let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); let r in 1 .. T::MaxRegistrars::get() => add_registrars::(r)?; let x in 1 .. T::MaxAdditionalFields::get() => { // Create their main identity with x additional fields let info = create_identity_info::(x); let caller: T::AccountId = whitelisted_caller(); let caller_origin = ::Origin::from(RawOrigin::Signed(caller)); Identity::::set_identity(caller_origin, info)?; }; }: _(RawOrigin::Signed(caller.clone()), r - 1, 10u32.into()) verify { assert_last_event::(Event::::JudgementRequested(caller, r-1).into()); } cancel_request { let caller: T::AccountId = whitelisted_caller(); let caller_origin = ::Origin::from(RawOrigin::Signed(caller.clone())); let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); let r in 1 .. T::MaxRegistrars::get() => add_registrars::(r)?; let x in 1 .. T::MaxAdditionalFields::get() => { // Create their main identity with x additional fields let info = create_identity_info::(x); let caller: T::AccountId = whitelisted_caller(); let caller_origin = ::Origin::from(RawOrigin::Signed(caller)); Identity::::set_identity(caller_origin, info)?; }; Identity::::request_judgement(caller_origin, r - 1, 10u32.into())?; }: _(RawOrigin::Signed(caller.clone()), r - 1) verify { assert_last_event::(Event::::JudgementUnrequested(caller, r-1).into()); } set_fee { let caller: T::AccountId = whitelisted_caller(); let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::(r)?; Identity::::add_registrar(RawOrigin::Root.into(), caller.clone())?; let registrars = Registrars::::get(); ensure!(registrars[r as usize].as_ref().unwrap().fee == 0u32.into(), "Fee already set."); }: _(RawOrigin::Signed(caller), r, 100u32.into()) verify { let registrars = Registrars::::get(); ensure!(registrars[r as usize].as_ref().unwrap().fee == 100u32.into(), "Fee not changed."); } set_account_id { let caller: T::AccountId = whitelisted_caller(); let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::(r)?; Identity::::add_registrar(RawOrigin::Root.into(), caller.clone())?; let registrars = Registrars::::get(); ensure!(registrars[r as usize].as_ref().unwrap().account == caller.clone(), "id not set."); }: _(RawOrigin::Signed(caller), r, account("new", 0, SEED)) verify { let registrars = Registrars::::get(); ensure!(registrars[r as usize].as_ref().unwrap().account == account("new", 0, SEED), "id not changed."); } set_fields { let caller: T::AccountId = whitelisted_caller(); let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::(r)?; Identity::::add_registrar(RawOrigin::Root.into(), caller.clone())?; let fields = IdentityFields( IdentityField::Display | IdentityField::Legal | IdentityField::Web | IdentityField::Riot | IdentityField::Email | IdentityField::PgpFingerprint | IdentityField::Image | IdentityField::Twitter ); let registrars = Registrars::::get(); ensure!(registrars[r as usize].as_ref().unwrap().fields == Default::default(), "fields already set."); }: _(RawOrigin::Signed(caller), r, fields) verify { let registrars = Registrars::::get(); ensure!(registrars[r as usize].as_ref().unwrap().fields != Default::default(), "fields not set."); } provide_judgement { // The user let user: T::AccountId = account("user", r, SEED); let user_origin = ::Origin::from(RawOrigin::Signed(user.clone())); let user_lookup = ::unlookup(user.clone()); let _ = T::Currency::make_free_balance_be(&user, BalanceOf::::max_value()); let caller: T::AccountId = whitelisted_caller(); let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::(r)?; let x in 1 .. T::MaxAdditionalFields::get() => { let info = create_identity_info::(x); Identity::::set_identity(user_origin.clone(), info)?; }; Identity::::add_registrar(RawOrigin::Root.into(), caller.clone())?; Identity::::request_judgement(user_origin.clone(), r, 10u32.into())?; }: _(RawOrigin::Signed(caller), r, user_lookup, Judgement::Reasonable) verify { assert_last_event::(Event::::JudgementGiven(user, r).into()) } kill_identity { let r in 1 .. T::MaxRegistrars::get() => add_registrars::(r)?; let s in 1 .. T::MaxSubAccounts::get(); let x in 1 .. T::MaxAdditionalFields::get(); let target: T::AccountId = account("target", 0, SEED); let target_origin: ::Origin = RawOrigin::Signed(target.clone()).into(); let target_lookup: ::Source = T::Lookup::unlookup(target.clone()); let _ = T::Currency::make_free_balance_be(&target, BalanceOf::::max_value()); let info = create_identity_info::(x); Identity::::set_identity(target_origin.clone(), info)?; let _ = add_sub_accounts::(&target, s)?; // User requests judgement from all the registrars, and they approve for i in 0..r { Identity::::request_judgement(target_origin.clone(), i, 10u32.into())?; Identity::::provide_judgement( RawOrigin::Signed(account("registrar", i, SEED)).into(), i, target_lookup.clone(), Judgement::Reasonable )?; } ensure!(IdentityOf::::contains_key(&target), "Identity not set"); }: _(RawOrigin::Root, target_lookup) verify { ensure!(!IdentityOf::::contains_key(&target), "Identity not removed"); } add_sub { let s in 1 .. T::MaxSubAccounts::get() - 1; let caller: T::AccountId = whitelisted_caller(); let _ = add_sub_accounts::(&caller, s)?; let sub = account("new_sub", 0, SEED); let data = Data::Raw(vec![0; 32]); ensure!(SubsOf::::get(&caller).1.len() as u32 == s, "Subs not set."); }: _(RawOrigin::Signed(caller.clone()), T::Lookup::unlookup(sub), data) verify { ensure!(SubsOf::::get(&caller).1.len() as u32 == s + 1, "Subs not added."); } rename_sub { let s in 1 .. T::MaxSubAccounts::get(); let caller: T::AccountId = whitelisted_caller(); let (sub, _) = add_sub_accounts::(&caller, s)?.remove(0); let data = Data::Raw(vec![1; 32]); ensure!(SuperOf::::get(&sub).unwrap().1 != data, "data already set"); }: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()), data.clone()) verify { ensure!(SuperOf::::get(&sub).unwrap().1 == data, "data not set"); } remove_sub { let s in 1 .. T::MaxSubAccounts::get(); let caller: T::AccountId = whitelisted_caller(); let (sub, _) = add_sub_accounts::(&caller, s)?.remove(0); ensure!(SuperOf::::contains_key(&sub), "Sub doesn't exists"); }: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone())) verify { ensure!(!SuperOf::::contains_key(&sub), "Sub not removed"); } quit_sub { let s in 1 .. T::MaxSubAccounts::get() - 1; let caller: T::AccountId = whitelisted_caller(); let sup = account("super", 0, SEED); let _ = add_sub_accounts::(&sup, s)?; let sup_origin = RawOrigin::Signed(sup).into(); Identity::::add_sub(sup_origin, T::Lookup::unlookup(caller.clone()), Data::Raw(vec![0; 32]))?; ensure!(SuperOf::::contains_key(&caller), "Sub doesn't exists"); }: _(RawOrigin::Signed(caller.clone())) verify { ensure!(!SuperOf::::contains_key(&caller), "Sub not removed"); } } impl_benchmark_test_suite!( Identity, crate::tests::new_test_ext(), crate::tests::Test, );