git.delta.rocks / unique-network / refs/commits / 4dd148686c6b

difftreelog

source

pallets/identity/src/benchmarking.rs20.1 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")]38#![allow(clippy::no_effect)]3940use frame_benchmarking::{account, benchmarks, whitelisted_caller};41use frame_support::{42	assert_ok, ensure,43	traits::{EnsureOrigin, Get},44};45use frame_system::RawOrigin;46use sp_runtime::traits::Bounded;4748use super::*;49use crate::Pallet as Identity;5051const SEED: u32 = 0;5253fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {54	frame_system::Pallet::<T>::assert_last_event(generic_event.into());55}5657// Adds `r` registrars to the Identity Pallet. These registrars will have set fees and fields.58fn add_registrars<T: Config>(r: u32) -> Result<(), &'static str> {59	for i in 0..r {60		let registrar: T::AccountId = account("registrar", i, SEED);61		let registrar_lookup = T::Lookup::unlookup(registrar.clone());62		let _ = T::Currency::make_free_balance_be(&registrar, BalanceOf::<T>::max_value());63		let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();64		Identity::<T>::add_registrar(registrar_origin, registrar_lookup)?;65		Identity::<T>::set_fee(RawOrigin::Signed(registrar.clone()).into(), i, 10u32.into())?;66		let fields = IdentityFields(67			IdentityField::Display68				| IdentityField::Legal69				| IdentityField::Web70				| IdentityField::Riot71				| IdentityField::Email72				| IdentityField::PgpFingerprint73				| IdentityField::Image74				| IdentityField::Twitter,75		);76		Identity::<T>::set_fields(RawOrigin::Signed(registrar.clone()).into(), i, fields)?;77	}7879	assert_eq!(Registrars::<T>::get().len(), r as usize);80	Ok(())81}8283// Create `s` sub-accounts for the identity of `who` and return them.84// Each will have 32 bytes of raw data added to it.85fn create_sub_accounts<T: Config>(86	who: &T::AccountId,87	s: u32,88) -> Result<Vec<(T::AccountId, Data)>, &'static str> {89	let mut subs = Vec::new();90	let who_origin = RawOrigin::Signed(who.clone());91	let data = Data::Raw(vec![0; 32].try_into().unwrap());9293	for i in 0..s {94		let sub_account = account("sub", i, SEED);95		subs.push((sub_account, data.clone()));96	}9798	// Set identity so `set_subs` does not fail.99	if IdentityOf::<T>::get(who).is_none() {100		let _ = T::Currency::make_free_balance_be(who, BalanceOf::<T>::max_value() / 2u32.into());101		let info = create_identity_info::<T>(1);102		Identity::<T>::set_identity(who_origin.into(), Box::new(info))?;103	}104105	Ok(subs)106}107108// Adds `s` sub-accounts to the identity of `who`. Each will have 32 bytes of raw data added to it.109// This additionally returns the vector of sub-accounts so it can be modified if needed.110fn add_sub_accounts<T: Config>(111	who: &T::AccountId,112	s: u32,113) -> Result<Vec<(T::AccountId, Data)>, &'static str> {114	let who_origin = RawOrigin::Signed(who.clone());115	let subs = create_sub_accounts::<T>(who, s)?;116117	Identity::<T>::set_subs(who_origin.into(), subs.clone())?;118119	Ok(subs)120}121122// This creates an `IdentityInfo` object with `num_fields` extra fields.123// All data is pre-populated with some arbitrary bytes.124fn create_identity_info<T: Config>(num_fields: u32) -> IdentityInfo<T::MaxAdditionalFields> {125	let data = Data::Raw(vec![0; 32].try_into().unwrap());126127	IdentityInfo {128		additional: vec![(data.clone(), data.clone()); num_fields as usize]129			.try_into()130			.unwrap(),131		display: data.clone(),132		legal: data.clone(),133		web: data.clone(),134		riot: data.clone(),135		email: data.clone(),136		pgp_fingerprint: Some([0; 20]),137		image: data.clone(),138		twitter: data,139	}140}141142/// `Currency::minimum_balance` was used originally, but in unique-chain, we have143/// zero existential deposit, thus triggering zero bond assertion.144fn balance_unit<T: Config>() -> <T::Currency as Currency<T::AccountId>>::Balance {145	200u32.into()146}147148benchmarks! {149	add_registrar {150		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;151		ensure!(Registrars::<T>::get().len() as u32 == r, "Registrars not set up correctly.");152		let origin = T::RegistrarOrigin::try_successful_origin().unwrap();153		let account = T::Lookup::unlookup(account("registrar", r + 1, SEED));154	}: _<T::RuntimeOrigin>(origin, account)155	verify {156		ensure!(Registrars::<T>::get().len() as u32 == r + 1, "Registrars not added.");157	}158159	set_identity {160		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;161		let x in 0 .. T::MaxAdditionalFields::get();162		let caller = {163			// The target user164			let caller: T::AccountId = whitelisted_caller();165			let caller_lookup = T::Lookup::unlookup(caller.clone());166			let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into();167			let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());168169			// Add an initial identity170			let initial_info = create_identity_info::<T>(1);171			Identity::<T>::set_identity(caller_origin.clone(), Box::new(initial_info.clone()))?;172173			// User requests judgement from all the registrars, and they approve174			for i in 0..r {175				let registrar: T::AccountId = account("registrar", i, SEED);176				let registrar_lookup = T::Lookup::unlookup(registrar.clone());177				let balance_to_use =  balance_unit::<T>() * 10u32.into();178				let _ = T::Currency::make_free_balance_be(&registrar, balance_to_use);179180				Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;181				Identity::<T>::provide_judgement(182					RawOrigin::Signed(registrar).into(),183					i,184					caller_lookup.clone(),185					Judgement::Reasonable,186					T::Hashing::hash_of(&initial_info),187				)?;188			}189			caller190		};191	}: _(RawOrigin::Signed(caller.clone()), Box::new(create_identity_info::<T>(x)))192	verify {193		assert_last_event::<T>(Event::<T>::IdentitySet { who: caller }.into());194	}195196	// We need to split `set_subs` into two benchmarks to accurately isolate the potential197	// writes caused by new or old sub accounts. The actual weight should simply be198	// the sum of these two weights.199	set_subs_new {200		let caller: T::AccountId = whitelisted_caller();201		// Create a new subs vec with s sub accounts202		let s in 0 .. T::MaxSubAccounts::get() => ();203		let subs = create_sub_accounts::<T>(&caller, s)?;204		ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Caller already has subs");205	}: set_subs(RawOrigin::Signed(caller.clone()), subs)206	verify {207		ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not added");208	}209210	set_subs_old {211		let caller: T::AccountId = whitelisted_caller();212		// Give them p many previous sub accounts.213		let p in 0 .. T::MaxSubAccounts::get() => {214			let _ = add_sub_accounts::<T>(&caller, p)?;215		};216		// Remove all subs.217		let subs = create_sub_accounts::<T>(&caller, 0)?;218		ensure!(219			SubsOf::<T>::get(&caller).1.len() as u32 == p,220			"Caller does have subs",221		);222	}: set_subs(RawOrigin::Signed(caller.clone()), subs)223	verify {224		ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Subs not removed");225	}226227	clear_identity {228		let caller: T::AccountId = whitelisted_caller();229		let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));230		let caller_lookup = <T::Lookup as StaticLookup>::unlookup(caller.clone());231		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());232233		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;234		let s in 0 .. T::MaxSubAccounts::get() => {235			// Give them s many sub accounts236			let caller: T::AccountId = whitelisted_caller();237			let _ = add_sub_accounts::<T>(&caller, s)?;238		};239		let x in 0 .. T::MaxAdditionalFields::get();240241		// Create their main identity with x additional fields242		let info = create_identity_info::<T>(x);243		let caller: T::AccountId = whitelisted_caller();244		let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));245		Identity::<T>::set_identity(caller_origin.clone(), Box::new(info.clone()))?;246247		// User requests judgement from all the registrars, and they approve248		for i in 0..r {249			let registrar: T::AccountId = account("registrar", i, SEED);250			let balance_to_use =  balance_unit::<T>() * 10u32.into();251			let _ = T::Currency::make_free_balance_be(&registrar, balance_to_use);252253			Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;254			Identity::<T>::provide_judgement(255				RawOrigin::Signed(registrar).into(),256				i,257				caller_lookup.clone(),258				Judgement::Reasonable,259				T::Hashing::hash_of(&info),260			)?;261		}262		ensure!(IdentityOf::<T>::contains_key(&caller), "Identity does not exist.");263	}: _(RawOrigin::Signed(caller.clone()))264	verify {265		ensure!(!IdentityOf::<T>::contains_key(&caller), "Identity not cleared.");266	}267268	request_judgement {269		let caller: T::AccountId = whitelisted_caller();270		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());271272		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;273		let x in 0 .. T::MaxAdditionalFields::get() => {274			// Create their main identity with x additional fields275			let info = create_identity_info::<T>(x);276			let caller: T::AccountId = whitelisted_caller();277			let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller));278			Identity::<T>::set_identity(caller_origin, Box::new(info))?;279		};280	}: _(RawOrigin::Signed(caller.clone()), r - 1, 10u32.into())281	verify {282		assert_last_event::<T>(Event::<T>::JudgementRequested { who: caller, registrar_index: r-1 }.into());283	}284285	cancel_request {286		let caller: T::AccountId = whitelisted_caller();287		let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));288		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());289290		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;291		let x in 0 .. T::MaxAdditionalFields::get() => {292			// Create their main identity with x additional fields293			let info = create_identity_info::<T>(x);294			let caller: T::AccountId = whitelisted_caller();295			let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller));296			Identity::<T>::set_identity(caller_origin, Box::new(info))?;297		};298299		Identity::<T>::request_judgement(caller_origin, r - 1, 10u32.into())?;300	}: _(RawOrigin::Signed(caller.clone()), r - 1)301	verify {302		assert_last_event::<T>(Event::<T>::JudgementUnrequested { who: caller, registrar_index: r-1 }.into());303	}304305	set_fee {306		let caller: T::AccountId = whitelisted_caller();307		let caller_lookup = T::Lookup::unlookup(caller.clone());308309		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;310311		let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();312		Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;313		let registrars = Registrars::<T>::get();314		ensure!(registrars[r as usize].as_ref().unwrap().fee == 0u32.into(), "Fee already set.");315	}: _(RawOrigin::Signed(caller), r, 100u32.into())316	verify {317		let registrars = Registrars::<T>::get();318		ensure!(registrars[r as usize].as_ref().unwrap().fee == 100u32.into(), "Fee not changed.");319	}320321	set_account_id {322		let caller: T::AccountId = whitelisted_caller();323		let caller_lookup = T::Lookup::unlookup(caller.clone());324		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());325326		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;327328		let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();329		Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;330		let registrars = Registrars::<T>::get();331		ensure!(registrars[r as usize].as_ref().unwrap().account == caller, "id not set.");332		let new_account = T::Lookup::unlookup(account("new", 0, SEED));333	}: _(RawOrigin::Signed(caller), r, new_account)334	verify {335		let registrars = Registrars::<T>::get();336		ensure!(registrars[r as usize].as_ref().unwrap().account == account("new", 0, SEED), "id not changed.");337	}338339	set_fields {340		let caller: T::AccountId = whitelisted_caller();341		let caller_lookup = T::Lookup::unlookup(caller.clone());342		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());343344		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;345346		let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();347		Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;348		let fields = IdentityFields(349			IdentityField::Display | IdentityField::Legal | IdentityField::Web | IdentityField::Riot350			| IdentityField::Email | IdentityField::PgpFingerprint | IdentityField::Image | IdentityField::Twitter351		);352		let registrars = Registrars::<T>::get();353		ensure!(registrars[r as usize].as_ref().unwrap().fields == Default::default(), "fields already set.");354	}: _(RawOrigin::Signed(caller), r, fields)355	verify {356		let registrars = Registrars::<T>::get();357		ensure!(registrars[r as usize].as_ref().unwrap().fields != Default::default(), "fields not set.");358	}359360	provide_judgement {361		// The user362		let user: T::AccountId = account("user", r, SEED);363		let user_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(user.clone()));364		let user_lookup = <T::Lookup as StaticLookup>::unlookup(user.clone());365		let _ = T::Currency::make_free_balance_be(&user, BalanceOf::<T>::max_value());366367		let caller: T::AccountId = whitelisted_caller();368		let caller_lookup = T::Lookup::unlookup(caller.clone());369		let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());370371		let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;372		let x in 0 .. T::MaxAdditionalFields::get();373374		let info = create_identity_info::<T>(x);375		let info_hash = T::Hashing::hash_of(&info);376		Identity::<T>::set_identity(user_origin.clone(), Box::new(info))?;377378		let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();379		Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;380		Identity::<T>::request_judgement(user_origin, r, 10u32.into())?;381	}: _(RawOrigin::Signed(caller), r, user_lookup, Judgement::Reasonable, info_hash)382	verify {383		assert_last_event::<T>(Event::<T>::JudgementGiven { target: user, registrar_index: r }.into())384	}385386	kill_identity {387		let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;388		let s in 0 .. T::MaxSubAccounts::get();389		let x in 0 .. T::MaxAdditionalFields::get();390391		let target: T::AccountId = account("target", 0, SEED);392		let target_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(target.clone()).into();393		let target_lookup = T::Lookup::unlookup(target.clone());394		let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());395396		let info = create_identity_info::<T>(x);397		Identity::<T>::set_identity(target_origin.clone(), Box::new(info.clone()))?;398		let _ = add_sub_accounts::<T>(&target, s)?;399400		// User requests judgement from all the registrars, and they approve401		for i in 0..r {402			let registrar: T::AccountId = account("registrar", i, SEED);403			let balance_to_use =  balance_unit::<T>() * 10u32.into();404			let _ = T::Currency::make_free_balance_be(&registrar, balance_to_use);405406			Identity::<T>::request_judgement(target_origin.clone(), i, 10u32.into())?;407			Identity::<T>::provide_judgement(408				RawOrigin::Signed(registrar).into(),409				i,410				target_lookup.clone(),411				Judgement::Reasonable,412				T::Hashing::hash_of(&info),413			)?;414		}415		ensure!(IdentityOf::<T>::contains_key(&target), "Identity not set");416		let origin = T::ForceOrigin::try_successful_origin().unwrap();417	}: _<T::RuntimeOrigin>(origin, target_lookup)418	verify {419		ensure!(!IdentityOf::<T>::contains_key(&target), "Identity not removed");420	}421422	force_insert_identities {423		let x in 0 .. T::MaxAdditionalFields::get();424		let n in 0..600;425		use frame_benchmarking::account;426		let identities = (0..n).map(|i| (427			account("caller", i, SEED),428			Registration::<BalanceOf<T>, T::MaxRegistrars, T::MaxAdditionalFields> {429				judgements: Default::default(),430				deposit: Default::default(),431				info: create_identity_info::<T>(x),432			},433		)).collect::<Vec<_>>();434		let origin = T::ForceOrigin::try_successful_origin().unwrap();435	}: _<T::RuntimeOrigin>(origin, identities)436437	force_remove_identities {438		let x in 0 .. T::MaxAdditionalFields::get();439		let n in 0..600;440		use frame_benchmarking::account;441		let origin = T::ForceOrigin::try_successful_origin().unwrap();442		let identities = (0..n).map(|i| (443			account("caller", i, SEED),444			Registration::<BalanceOf<T>, T::MaxRegistrars, T::MaxAdditionalFields> {445				judgements: Default::default(),446				deposit: Default::default(),447				info: create_identity_info::<T>(x),448			},449		)).collect::<Vec<_>>();450		assert_ok!(451			Identity::<T>::force_insert_identities(origin.clone(), identities.clone()),452		);453		let identities = identities.into_iter().map(|(acc, _)| acc).collect::<Vec<_>>();454	}: _<T::RuntimeOrigin>(origin, identities)455456	force_set_subs {457		let s in 0 .. T::MaxSubAccounts::get();458		let n in 0..600;459		use frame_benchmarking::account;460		let identities = (0..n).map(|i| {461			let caller: T::AccountId = account("caller", i, SEED);462			(463				caller.clone(),464				(465					BalanceOf::<T>::max_value(),466					create_sub_accounts::<T>(&caller, s).unwrap().try_into().unwrap(),467				),468			)469		}).collect::<Vec<_>>();470		let origin = T::ForceOrigin::try_successful_origin().unwrap();471	}: _<T::RuntimeOrigin>(origin, identities)472473	add_sub {474		let s in 0 .. T::MaxSubAccounts::get() - 1;475476		let caller: T::AccountId = whitelisted_caller();477		let _ = add_sub_accounts::<T>(&caller, s)?;478		let sub = account("new_sub", 0, SEED);479		let data = Data::Raw(vec![0; 32].try_into().unwrap());480		ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not set.");481	}: _(RawOrigin::Signed(caller.clone()), T::Lookup::unlookup(sub), data)482	verify {483		ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s + 1, "Subs not added.");484	}485486	rename_sub {487		let s in 1 .. T::MaxSubAccounts::get();488489		let caller: T::AccountId = whitelisted_caller();490		let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);491		let data = Data::Raw(vec![1; 32].try_into().unwrap());492		ensure!(SuperOf::<T>::get(&sub).unwrap().1 != data, "data already set");493	}: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()), data.clone())494	verify {495		ensure!(SuperOf::<T>::get(&sub).unwrap().1 == data, "data not set");496	}497498	remove_sub {499		let s in 1 .. T::MaxSubAccounts::get();500501		let caller: T::AccountId = whitelisted_caller();502		let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);503		ensure!(SuperOf::<T>::contains_key(&sub), "Sub doesn't exists");504	}: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()))505	verify {506		ensure!(!SuperOf::<T>::contains_key(&sub), "Sub not removed");507	}508509	quit_sub {510		let s in 0 .. T::MaxSubAccounts::get() - 1;511512		let caller: T::AccountId = whitelisted_caller();513		let sup = account("super", 0, SEED);514		let _ = add_sub_accounts::<T>(&sup, s)?;515		let sup_origin = RawOrigin::Signed(sup).into();516		Identity::<T>::add_sub(sup_origin, T::Lookup::unlookup(caller.clone()), Data::Raw(vec![0; 32].try_into().unwrap()))?;517		ensure!(SuperOf::<T>::contains_key(&caller), "Sub doesn't exists");518	}: _(RawOrigin::Signed(caller.clone()))519	verify {520		ensure!(!SuperOf::<T>::contains_key(&caller), "Sub not removed");521	}522523	impl_benchmark_test_suite!(Identity, crate::tests::new_test_ext(), crate::tests::Test);524}