git.delta.rocks / unique-network / refs/commits / 6f5be65656d0

difftreelog

source

pallets/identity/src/benchmarking.rs19.8 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617// Original license:18// This file is part of Substrate.1920// Copyright (C) 2020-2022 Parity Technologies (UK) Ltd.21// SPDX-License-Identifier: Apache-2.02223// Licensed under the Apache License, Version 2.0 (the "License");24// you may not use this file except in compliance with the License.25// You may obtain a copy of the License at26//27// 	http://www.apache.org/licenses/LICENSE-2.028//29// Unless required by applicable law or agreed to in writing, software30// distributed under the License is distributed on an "AS IS" BASIS,31// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.32// See the License for the specific language governing permissions and33// limitations under the License.3435//! Identity pallet benchmarking.3637#![cfg(feature = "runtime-benchmarks")]3839use super::*;4041use crate::Pallet as Identity;42use frame_benchmarking::{account, benchmarks, whitelisted_caller};43use frame_support::{44	ensure, assert_ok,45	traits::{EnsureOrigin, Get},46};47use frame_system::RawOrigin;48use sp_runtime::traits::Bounded;4950const SEED: u32 = 0;5152fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {53	frame_system::Pallet::<T>::assert_last_event(generic_event.into());54}5556// Adds `r` registrars to the Identity Pallet. These registrars will have set fees and fields.57fn add_registrars<T: Config>(r: u32) -> Result<(), &'static str> {58	for i in 0..r {59		let registrar: T::AccountId = account("registrar", i, SEED);60		let registrar_lookup = T::Lookup::unlookup(registrar.clone());61		let _ = T::Currency::make_free_balance_be(&registrar, BalanceOf::<T>::max_value());62		let registrar_origin = T::RegistrarOrigin::successful_origin();63		Identity::<T>::add_registrar(registrar_origin, registrar_lookup)?;64		Identity::<T>::set_fee(RawOrigin::Signed(registrar.clone()).into(), i, 10u32.into())?;65		let fields = IdentityFields(66			IdentityField::Display67				| IdentityField::Legal68				| IdentityField::Web69				| IdentityField::Riot70				| IdentityField::Email71				| IdentityField::PgpFingerprint72				| IdentityField::Image73				| IdentityField::Twitter,74		);75		Identity::<T>::set_fields(RawOrigin::Signed(registrar.clone()).into(), i, fields)?;76	}7778	assert_eq!(Registrars::<T>::get().len(), r as usize);79	Ok(())80}8182// Create `s` sub-accounts for the identity of `who` and return them.83// Each will have 32 bytes of raw data added to it.84fn create_sub_accounts<T: Config>(85	who: &T::AccountId,86	s: u32,87) -> Result<Vec<(T::AccountId, Data)>, &'static str> {88	let mut subs = Vec::new();89	let who_origin = RawOrigin::Signed(who.clone());90	let data = Data::Raw(vec![0; 32].try_into().unwrap());9192	for i in 0..s {93		let sub_account = account("sub", i, SEED);94		subs.push((sub_account, data.clone()));95	}9697	// Set identity so `set_subs` does not fail.98	if IdentityOf::<T>::get(who).is_none() {99		let _ = T::Currency::make_free_balance_be(who, BalanceOf::<T>::max_value() / 2u32.into());100		let info = create_identity_info::<T>(1);101		Identity::<T>::set_identity(who_origin.into(), Box::new(info))?;102	}103104	Ok(subs)105}106107// Adds `s` sub-accounts to the identity of `who`. Each will have 32 bytes of raw data added to it.108// This additionally returns the vector of sub-accounts so it can be modified if needed.109fn add_sub_accounts<T: Config>(110	who: &T::AccountId,111	s: u32,112) -> Result<Vec<(T::AccountId, Data)>, &'static str> {113	let who_origin = RawOrigin::Signed(who.clone());114	let subs = create_sub_accounts::<T>(who, s)?;115116	Identity::<T>::set_subs(who_origin.into(), subs.clone())?;117118	Ok(subs)119}120121// This creates an `IdentityInfo` object with `num_fields` extra fields.122// All data is pre-populated with some arbitrary bytes.123fn create_identity_info<T: Config>(num_fields: u32) -> IdentityInfo<T::MaxAdditionalFields> {124	let data = Data::Raw(vec![0; 32].try_into().unwrap());125126	IdentityInfo {127		additional: vec![(data.clone(), data.clone()); num_fields as usize]128			.try_into()129			.unwrap(),130		display: data.clone(),131		legal: data.clone(),132		web: data.clone(),133		riot: data.clone(),134		email: data.clone(),135		pgp_fingerprint: Some([0; 20]),136		image: data.clone(),137		twitter: data,138	}139}140141benchmarks! {142	add_registrar {143		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;144		ensure!(Registrars::<T>::get().len() as u32 == r, "Registrars not set up correctly.");145		let origin = T::RegistrarOrigin::successful_origin();146		let account = T::Lookup::unlookup(account("registrar", r + 1, SEED));147	}: _<T::RuntimeOrigin>(origin, account)148	verify {149		ensure!(Registrars::<T>::get().len() as u32 == r + 1, "Registrars not added.");150	}151152	set_identity {153		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;154		let x in 0 .. T::MaxAdditionalFields::get();155		let caller = {156			// The target user157			let caller: T::AccountId = whitelisted_caller();158			let caller_lookup = T::Lookup::unlookup(caller.clone());159			let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into();160			let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());161162			// Add an initial identity163			let initial_info = create_identity_info::<T>(1);164			Identity::<T>::set_identity(caller_origin.clone(), Box::new(initial_info.clone()))?;165166			// User requests judgement from all the registrars, and they approve167			for i in 0..r {168				let registrar: T::AccountId = account("registrar", i, SEED);169				let registrar_lookup = T::Lookup::unlookup(registrar.clone());170				let balance_to_use =  T::Currency::minimum_balance() * 10u32.into();171				let _ = T::Currency::make_free_balance_be(&registrar, balance_to_use);172173				Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;174				Identity::<T>::provide_judgement(175					RawOrigin::Signed(registrar).into(),176					i,177					caller_lookup.clone(),178					Judgement::Reasonable,179					T::Hashing::hash_of(&initial_info),180				)?;181			}182			caller183		};184	}: _(RawOrigin::Signed(caller.clone()), Box::new(create_identity_info::<T>(x)))185	verify {186		assert_last_event::<T>(Event::<T>::IdentitySet { who: caller }.into());187	}188189	// We need to split `set_subs` into two benchmarks to accurately isolate the potential190	// writes caused by new or old sub accounts. The actual weight should simply be191	// the sum of these two weights.192	set_subs_new {193		let caller: T::AccountId = whitelisted_caller();194		// Create a new subs vec with s sub accounts195		let s in 0 .. T::MaxSubAccounts::get() => ();196		let subs = create_sub_accounts::<T>(&caller, s)?;197		ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Caller already has subs");198	}: set_subs(RawOrigin::Signed(caller.clone()), subs)199	verify {200		ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not added");201	}202203	set_subs_old {204		let caller: T::AccountId = whitelisted_caller();205		// Give them p many previous sub accounts.206		let p in 0 .. T::MaxSubAccounts::get() => {207			let _ = add_sub_accounts::<T>(&caller, p)?;208		};209		// Remove all subs.210		let subs = create_sub_accounts::<T>(&caller, 0)?;211		ensure!(212			SubsOf::<T>::get(&caller).1.len() as u32 == p,213			"Caller does have subs",214		);215	}: set_subs(RawOrigin::Signed(caller.clone()), subs)216	verify {217		ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Subs not removed");218	}219220	clear_identity {221		let caller: T::AccountId = whitelisted_caller();222		let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));223		let caller_lookup = <T::Lookup as StaticLookup>::unlookup(caller.clone());224		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());225226		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;227		let s in 0 .. T::MaxSubAccounts::get() => {228			// Give them s many sub accounts229			let caller: T::AccountId = whitelisted_caller();230			let _ = add_sub_accounts::<T>(&caller, s)?;231		};232		let x in 0 .. T::MaxAdditionalFields::get();233234		// Create their main identity with x additional fields235		let info = create_identity_info::<T>(x);236		let caller: T::AccountId = whitelisted_caller();237		let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));238		Identity::<T>::set_identity(caller_origin.clone(), Box::new(info.clone()))?;239240		// User requests judgement from all the registrars, and they approve241		for i in 0..r {242			let registrar: T::AccountId = account("registrar", i, SEED);243			let balance_to_use =  T::Currency::minimum_balance() * 10u32.into();244			let _ = T::Currency::make_free_balance_be(&registrar, balance_to_use);245246			Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;247			Identity::<T>::provide_judgement(248				RawOrigin::Signed(registrar).into(),249				i,250				caller_lookup.clone(),251				Judgement::Reasonable,252				T::Hashing::hash_of(&info),253			)?;254		}255		ensure!(IdentityOf::<T>::contains_key(&caller), "Identity does not exist.");256	}: _(RawOrigin::Signed(caller.clone()))257	verify {258		ensure!(!IdentityOf::<T>::contains_key(&caller), "Identity not cleared.");259	}260261	request_judgement {262		let caller: T::AccountId = whitelisted_caller();263		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());264265		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;266		let x in 0 .. T::MaxAdditionalFields::get() => {267			// Create their main identity with x additional fields268			let info = create_identity_info::<T>(x);269			let caller: T::AccountId = whitelisted_caller();270			let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller));271			Identity::<T>::set_identity(caller_origin, Box::new(info))?;272		};273	}: _(RawOrigin::Signed(caller.clone()), r - 1, 10u32.into())274	verify {275		assert_last_event::<T>(Event::<T>::JudgementRequested { who: caller, registrar_index: r-1 }.into());276	}277278	cancel_request {279		let caller: T::AccountId = whitelisted_caller();280		let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));281		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());282283		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;284		let x in 0 .. T::MaxAdditionalFields::get() => {285			// Create their main identity with x additional fields286			let info = create_identity_info::<T>(x);287			let caller: T::AccountId = whitelisted_caller();288			let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller));289			Identity::<T>::set_identity(caller_origin, Box::new(info))?;290		};291292		Identity::<T>::request_judgement(caller_origin, r - 1, 10u32.into())?;293	}: _(RawOrigin::Signed(caller.clone()), r - 1)294	verify {295		assert_last_event::<T>(Event::<T>::JudgementUnrequested { who: caller, registrar_index: r-1 }.into());296	}297298	set_fee {299		let caller: T::AccountId = whitelisted_caller();300		let caller_lookup = T::Lookup::unlookup(caller.clone());301302		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;303304		let registrar_origin = T::RegistrarOrigin::successful_origin();305		Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;306		let registrars = Registrars::<T>::get();307		ensure!(registrars[r as usize].as_ref().unwrap().fee == 0u32.into(), "Fee already set.");308	}: _(RawOrigin::Signed(caller), r, 100u32.into())309	verify {310		let registrars = Registrars::<T>::get();311		ensure!(registrars[r as usize].as_ref().unwrap().fee == 100u32.into(), "Fee not changed.");312	}313314	set_account_id {315		let caller: T::AccountId = whitelisted_caller();316		let caller_lookup = T::Lookup::unlookup(caller.clone());317		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());318319		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;320321		let registrar_origin = T::RegistrarOrigin::successful_origin();322		Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;323		let registrars = Registrars::<T>::get();324		ensure!(registrars[r as usize].as_ref().unwrap().account == caller, "id not set.");325		let new_account = T::Lookup::unlookup(account("new", 0, SEED));326	}: _(RawOrigin::Signed(caller), r, new_account)327	verify {328		let registrars = Registrars::<T>::get();329		ensure!(registrars[r as usize].as_ref().unwrap().account == account("new", 0, SEED), "id not changed.");330	}331332	set_fields {333		let caller: T::AccountId = whitelisted_caller();334		let caller_lookup = T::Lookup::unlookup(caller.clone());335		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());336337		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;338339		let registrar_origin = T::RegistrarOrigin::successful_origin();340		Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;341		let fields = IdentityFields(342			IdentityField::Display | IdentityField::Legal | IdentityField::Web | IdentityField::Riot343			| IdentityField::Email | IdentityField::PgpFingerprint | IdentityField::Image | IdentityField::Twitter344		);345		let registrars = Registrars::<T>::get();346		ensure!(registrars[r as usize].as_ref().unwrap().fields == Default::default(), "fields already set.");347	}: _(RawOrigin::Signed(caller), r, fields)348	verify {349		let registrars = Registrars::<T>::get();350		ensure!(registrars[r as usize].as_ref().unwrap().fields != Default::default(), "fields not set.");351	}352353	provide_judgement {354		// The user355		let user: T::AccountId = account("user", r, SEED);356		let user_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(user.clone()));357		let user_lookup = <T::Lookup as StaticLookup>::unlookup(user.clone());358		let _ = T::Currency::make_free_balance_be(&user, BalanceOf::<T>::max_value());359360		let caller: T::AccountId = whitelisted_caller();361		let caller_lookup = T::Lookup::unlookup(caller.clone());362		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());363364		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;365		let x in 0 .. T::MaxAdditionalFields::get();366367		let info = create_identity_info::<T>(x);368		let info_hash = T::Hashing::hash_of(&info);369		Identity::<T>::set_identity(user_origin.clone(), Box::new(info))?;370371		let registrar_origin = T::RegistrarOrigin::successful_origin();372		Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;373		Identity::<T>::request_judgement(user_origin, r, 10u32.into())?;374	}: _(RawOrigin::Signed(caller), r, user_lookup, Judgement::Reasonable, info_hash)375	verify {376		assert_last_event::<T>(Event::<T>::JudgementGiven { target: user, registrar_index: r }.into())377	}378379	kill_identity {380		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;381		let s in 0 .. T::MaxSubAccounts::get();382		let x in 0 .. T::MaxAdditionalFields::get();383384		let target: T::AccountId = account("target", 0, SEED);385		let target_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(target.clone()).into();386		let target_lookup = T::Lookup::unlookup(target.clone());387		let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());388389		let info = create_identity_info::<T>(x);390		Identity::<T>::set_identity(target_origin.clone(), Box::new(info.clone()))?;391		let _ = add_sub_accounts::<T>(&target, s)?;392393		// User requests judgement from all the registrars, and they approve394		for i in 0..r {395			let registrar: T::AccountId = account("registrar", i, SEED);396			let balance_to_use =  T::Currency::minimum_balance() * 10u32.into();397			let _ = T::Currency::make_free_balance_be(&registrar, balance_to_use);398399			Identity::<T>::request_judgement(target_origin.clone(), i, 10u32.into())?;400			Identity::<T>::provide_judgement(401				RawOrigin::Signed(registrar).into(),402				i,403				target_lookup.clone(),404				Judgement::Reasonable,405				T::Hashing::hash_of(&info),406			)?;407		}408		ensure!(IdentityOf::<T>::contains_key(&target), "Identity not set");409		let origin = T::ForceOrigin::successful_origin();410	}: _<T::RuntimeOrigin>(origin, target_lookup)411	verify {412		ensure!(!IdentityOf::<T>::contains_key(&target), "Identity not removed");413	}414415	force_insert_identities {416		let x in 0 .. T::MaxAdditionalFields::get();417		let n in 0..600;418		use frame_benchmarking::account;419		let identities = (0..n).map(|i| (420			account("caller", i, SEED),421			Registration::<BalanceOf<T>, T::MaxRegistrars, T::MaxAdditionalFields> {422				judgements: Default::default(),423				deposit: Default::default(),424				info: create_identity_info::<T>(x),425			},426		)).collect::<Vec<_>>();427		let origin = T::ForceOrigin::successful_origin();428	}: _<T::RuntimeOrigin>(origin, identities)429430	force_remove_identities {431		let x in 0 .. T::MaxAdditionalFields::get();432		let n in 0..600;433		use frame_benchmarking::account;434		let origin = T::ForceOrigin::successful_origin();435		let identities = (0..n).map(|i| (436			account("caller", i, SEED),437			Registration::<BalanceOf<T>, T::MaxRegistrars, T::MaxAdditionalFields> {438				judgements: Default::default(),439				deposit: Default::default(),440				info: create_identity_info::<T>(x),441			},442		)).collect::<Vec<_>>();443		assert_ok!(444			Identity::<T>::force_insert_identities(origin.clone(), identities.clone()),445		);446		let identities = identities.into_iter().map(|(acc, _)| acc).collect::<Vec<_>>();447	}: _<T::RuntimeOrigin>(origin, identities)448449	force_set_subs {450		let s in 0 .. T::MaxSubAccounts::get();451		let n in 0..600;452		use frame_benchmarking::account;453		let identities = (0..n).map(|i| {454			let caller: T::AccountId = account("caller", i, SEED);455			(456				caller.clone(),457				(458					BalanceOf::<T>::max_value(),459					create_sub_accounts::<T>(&caller, s).unwrap().try_into().unwrap(),460				),461			)462		}).collect::<Vec<_>>();463		let origin = T::ForceOrigin::successful_origin();464	}: _<T::RuntimeOrigin>(origin, identities)465466	add_sub {467		let s in 0 .. T::MaxSubAccounts::get() - 1;468469		let caller: T::AccountId = whitelisted_caller();470		let _ = add_sub_accounts::<T>(&caller, s)?;471		let sub = account("new_sub", 0, SEED);472		let data = Data::Raw(vec![0; 32].try_into().unwrap());473		ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not set.");474	}: _(RawOrigin::Signed(caller.clone()), T::Lookup::unlookup(sub), data)475	verify {476		ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s + 1, "Subs not added.");477	}478479	rename_sub {480		let s in 1 .. T::MaxSubAccounts::get();481482		let caller: T::AccountId = whitelisted_caller();483		let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);484		let data = Data::Raw(vec![1; 32].try_into().unwrap());485		ensure!(SuperOf::<T>::get(&sub).unwrap().1 != data, "data already set");486	}: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()), data.clone())487	verify {488		ensure!(SuperOf::<T>::get(&sub).unwrap().1 == data, "data not set");489	}490491	remove_sub {492		let s in 1 .. T::MaxSubAccounts::get();493494		let caller: T::AccountId = whitelisted_caller();495		let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);496		ensure!(SuperOf::<T>::contains_key(&sub), "Sub doesn't exists");497	}: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()))498	verify {499		ensure!(!SuperOf::<T>::contains_key(&sub), "Sub not removed");500	}501502	quit_sub {503		let s in 0 .. T::MaxSubAccounts::get() - 1;504505		let caller: T::AccountId = whitelisted_caller();506		let sup = account("super", 0, SEED);507		let _ = add_sub_accounts::<T>(&sup, s)?;508		let sup_origin = RawOrigin::Signed(sup).into();509		Identity::<T>::add_sub(sup_origin, T::Lookup::unlookup(caller.clone()), Data::Raw(vec![0; 32].try_into().unwrap()))?;510		ensure!(SuperOf::<T>::contains_key(&caller), "Sub doesn't exists");511	}: _(RawOrigin::Signed(caller.clone()))512	verify {513		ensure!(!SuperOf::<T>::contains_key(&caller), "Sub not removed");514	}515516	impl_benchmark_test_suite!(Identity, crate::tests::new_test_ext(), crate::tests::Test);517}