12345678910111213141516171819202122232425262728293031323334353637#![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}555657fn 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(®istrar, BalanceOf::<T>::max_value());62 let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();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}81828384fn 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 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}106107108109fn 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}120121122123fn 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}140141142143fn balance_unit<T: Config>() -> <T::Currency as Currency<T::AccountId>>::Balance {144 200u32.into()145}146147benchmarks! {148 add_registrar {149 let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;150 ensure!(Registrars::<T>::get().len() as u32 == r, "Registrars not set up correctly.");151 let origin = T::RegistrarOrigin::try_successful_origin().unwrap();152 let account = T::Lookup::unlookup(account("registrar", r + 1, SEED));153 }: _<T::RuntimeOrigin>(origin, account)154 verify {155 ensure!(Registrars::<T>::get().len() as u32 == r + 1, "Registrars not added.");156 }157158 set_identity {159 let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;160 let x in 0 .. T::MaxAdditionalFields::get();161 let caller = {162 163 let caller: T::AccountId = whitelisted_caller();164 let caller_lookup = T::Lookup::unlookup(caller.clone());165 let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into();166 let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());167168 169 let initial_info = create_identity_info::<T>(1);170 Identity::<T>::set_identity(caller_origin.clone(), Box::new(initial_info.clone()))?;171172 173 for i in 0..r {174 let registrar: T::AccountId = account("registrar", i, SEED);175 let registrar_lookup = T::Lookup::unlookup(registrar.clone());176 let balance_to_use = balance_unit::<T>() * 10u32.into();177 let _ = T::Currency::make_free_balance_be(®istrar, balance_to_use);178179 Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;180 Identity::<T>::provide_judgement(181 RawOrigin::Signed(registrar).into(),182 i,183 caller_lookup.clone(),184 Judgement::Reasonable,185 T::Hashing::hash_of(&initial_info),186 )?;187 }188 caller189 };190 }: _(RawOrigin::Signed(caller.clone()), Box::new(create_identity_info::<T>(x)))191 verify {192 assert_last_event::<T>(Event::<T>::IdentitySet { who: caller }.into());193 }194195 196 197 198 set_subs_new {199 let caller: T::AccountId = whitelisted_caller();200 201 let s in 0 .. T::MaxSubAccounts::get() => ();202 let subs = create_sub_accounts::<T>(&caller, s)?;203 ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Caller already has subs");204 }: set_subs(RawOrigin::Signed(caller.clone()), subs)205 verify {206 ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not added");207 }208209 set_subs_old {210 let caller: T::AccountId = whitelisted_caller();211 212 let p in 0 .. T::MaxSubAccounts::get() => {213 let _ = add_sub_accounts::<T>(&caller, p)?;214 };215 216 let subs = create_sub_accounts::<T>(&caller, 0)?;217 ensure!(218 SubsOf::<T>::get(&caller).1.len() as u32 == p,219 "Caller does have subs",220 );221 }: set_subs(RawOrigin::Signed(caller.clone()), subs)222 verify {223 ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Subs not removed");224 }225226 clear_identity {227 let caller: T::AccountId = whitelisted_caller();228 let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));229 let caller_lookup = <T::Lookup as StaticLookup>::unlookup(caller.clone());230 let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());231232 let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;233 let s in 0 .. T::MaxSubAccounts::get() => {234 235 let caller: T::AccountId = whitelisted_caller();236 let _ = add_sub_accounts::<T>(&caller, s)?;237 };238 let x in 0 .. T::MaxAdditionalFields::get();239240 241 let info = create_identity_info::<T>(x);242 let caller: T::AccountId = whitelisted_caller();243 let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));244 Identity::<T>::set_identity(caller_origin.clone(), Box::new(info.clone()))?;245246 247 for i in 0..r {248 let registrar: T::AccountId = account("registrar", i, SEED);249 let balance_to_use = balance_unit::<T>() * 10u32.into();250 let _ = T::Currency::make_free_balance_be(®istrar, balance_to_use);251252 Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;253 Identity::<T>::provide_judgement(254 RawOrigin::Signed(registrar).into(),255 i,256 caller_lookup.clone(),257 Judgement::Reasonable,258 T::Hashing::hash_of(&info),259 )?;260 }261 ensure!(IdentityOf::<T>::contains_key(&caller), "Identity does not exist.");262 }: _(RawOrigin::Signed(caller.clone()))263 verify {264 ensure!(!IdentityOf::<T>::contains_key(&caller), "Identity not cleared.");265 }266267 request_judgement {268 let caller: T::AccountId = whitelisted_caller();269 let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());270271 let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;272 let x in 0 .. T::MaxAdditionalFields::get() => {273 274 let info = create_identity_info::<T>(x);275 let caller: T::AccountId = whitelisted_caller();276 let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller));277 Identity::<T>::set_identity(caller_origin, Box::new(info))?;278 };279 }: _(RawOrigin::Signed(caller.clone()), r - 1, 10u32.into())280 verify {281 assert_last_event::<T>(Event::<T>::JudgementRequested { who: caller, registrar_index: r-1 }.into());282 }283284 cancel_request {285 let caller: T::AccountId = whitelisted_caller();286 let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));287 let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());288289 let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;290 let x in 0 .. T::MaxAdditionalFields::get() => {291 292 let info = create_identity_info::<T>(x);293 let caller: T::AccountId = whitelisted_caller();294 let caller_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller));295 Identity::<T>::set_identity(caller_origin, Box::new(info))?;296 };297298 Identity::<T>::request_judgement(caller_origin, r - 1, 10u32.into())?;299 }: _(RawOrigin::Signed(caller.clone()), r - 1)300 verify {301 assert_last_event::<T>(Event::<T>::JudgementUnrequested { who: caller, registrar_index: r-1 }.into());302 }303304 set_fee {305 let caller: T::AccountId = whitelisted_caller();306 let caller_lookup = T::Lookup::unlookup(caller.clone());307308 let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;309310 let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();311 Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;312 let registrars = Registrars::<T>::get();313 ensure!(registrars[r as usize].as_ref().unwrap().fee == 0u32.into(), "Fee already set.");314 }: _(RawOrigin::Signed(caller), r, 100u32.into())315 verify {316 let registrars = Registrars::<T>::get();317 ensure!(registrars[r as usize].as_ref().unwrap().fee == 100u32.into(), "Fee not changed.");318 }319320 set_account_id {321 let caller: T::AccountId = whitelisted_caller();322 let caller_lookup = T::Lookup::unlookup(caller.clone());323 let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());324325 let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;326327 let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();328 Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;329 let registrars = Registrars::<T>::get();330 ensure!(registrars[r as usize].as_ref().unwrap().account == caller, "id not set.");331 let new_account = T::Lookup::unlookup(account("new", 0, SEED));332 }: _(RawOrigin::Signed(caller), r, new_account)333 verify {334 let registrars = Registrars::<T>::get();335 ensure!(registrars[r as usize].as_ref().unwrap().account == account("new", 0, SEED), "id not changed.");336 }337338 set_fields {339 let caller: T::AccountId = whitelisted_caller();340 let caller_lookup = T::Lookup::unlookup(caller.clone());341 let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());342343 let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;344345 let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();346 Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;347 let fields = IdentityFields(348 IdentityField::Display | IdentityField::Legal | IdentityField::Web | IdentityField::Riot349 | IdentityField::Email | IdentityField::PgpFingerprint | IdentityField::Image | IdentityField::Twitter350 );351 let registrars = Registrars::<T>::get();352 ensure!(registrars[r as usize].as_ref().unwrap().fields == Default::default(), "fields already set.");353 }: _(RawOrigin::Signed(caller), r, fields)354 verify {355 let registrars = Registrars::<T>::get();356 ensure!(registrars[r as usize].as_ref().unwrap().fields != Default::default(), "fields not set.");357 }358359 provide_judgement {360 361 let user: T::AccountId = account("user", r, SEED);362 let user_origin = <T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(user.clone()));363 let user_lookup = <T::Lookup as StaticLookup>::unlookup(user.clone());364 let _ = T::Currency::make_free_balance_be(&user, BalanceOf::<T>::max_value());365366 let caller: T::AccountId = whitelisted_caller();367 let caller_lookup = T::Lookup::unlookup(caller.clone());368 let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());369370 let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;371 let x in 0 .. T::MaxAdditionalFields::get();372373 let info = create_identity_info::<T>(x);374 let info_hash = T::Hashing::hash_of(&info);375 Identity::<T>::set_identity(user_origin.clone(), Box::new(info))?;376377 let registrar_origin = T::RegistrarOrigin::try_successful_origin().unwrap();378 Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;379 Identity::<T>::request_judgement(user_origin, r, 10u32.into())?;380 }: _(RawOrigin::Signed(caller), r, user_lookup, Judgement::Reasonable, info_hash)381 verify {382 assert_last_event::<T>(Event::<T>::JudgementGiven { target: user, registrar_index: r }.into())383 }384385 kill_identity {386 let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;387 let s in 0 .. T::MaxSubAccounts::get();388 let x in 0 .. T::MaxAdditionalFields::get();389390 let target: T::AccountId = account("target", 0, SEED);391 let target_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(target.clone()).into();392 let target_lookup = T::Lookup::unlookup(target.clone());393 let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());394395 let info = create_identity_info::<T>(x);396 Identity::<T>::set_identity(target_origin.clone(), Box::new(info.clone()))?;397 let _ = add_sub_accounts::<T>(&target, s)?;398399 400 for i in 0..r {401 let registrar: T::AccountId = account("registrar", i, SEED);402 let balance_to_use = balance_unit::<T>() * 10u32.into();403 let _ = T::Currency::make_free_balance_be(®istrar, balance_to_use);404405 Identity::<T>::request_judgement(target_origin.clone(), i, 10u32.into())?;406 Identity::<T>::provide_judgement(407 RawOrigin::Signed(registrar).into(),408 i,409 target_lookup.clone(),410 Judgement::Reasonable,411 T::Hashing::hash_of(&info),412 )?;413 }414 ensure!(IdentityOf::<T>::contains_key(&target), "Identity not set");415 let origin = T::ForceOrigin::try_successful_origin().unwrap();416 }: _<T::RuntimeOrigin>(origin, target_lookup)417 verify {418 ensure!(!IdentityOf::<T>::contains_key(&target), "Identity not removed");419 }420421 force_insert_identities {422 let x in 0 .. T::MaxAdditionalFields::get();423 let n in 0..600;424 use frame_benchmarking::account;425 let identities = (0..n).map(|i| (426 account("caller", i, SEED),427 Registration::<BalanceOf<T>, T::MaxRegistrars, T::MaxAdditionalFields> {428 judgements: Default::default(),429 deposit: Default::default(),430 info: create_identity_info::<T>(x),431 },432 )).collect::<Vec<_>>();433 let origin = T::ForceOrigin::try_successful_origin().unwrap();434 }: _<T::RuntimeOrigin>(origin, identities)435436 force_remove_identities {437 let x in 0 .. T::MaxAdditionalFields::get();438 let n in 0..600;439 use frame_benchmarking::account;440 let origin = T::ForceOrigin::try_successful_origin().unwrap();441 let identities = (0..n).map(|i| (442 account("caller", i, SEED),443 Registration::<BalanceOf<T>, T::MaxRegistrars, T::MaxAdditionalFields> {444 judgements: Default::default(),445 deposit: Default::default(),446 info: create_identity_info::<T>(x),447 },448 )).collect::<Vec<_>>();449 assert_ok!(450 Identity::<T>::force_insert_identities(origin.clone(), identities.clone()),451 );452 let identities = identities.into_iter().map(|(acc, _)| acc).collect::<Vec<_>>();453 }: _<T::RuntimeOrigin>(origin, identities)454455 force_set_subs {456 let s in 0 .. T::MaxSubAccounts::get();457 let n in 0..600;458 use frame_benchmarking::account;459 let identities = (0..n).map(|i| {460 let caller: T::AccountId = account("caller", i, SEED);461 (462 caller.clone(),463 (464 BalanceOf::<T>::max_value(),465 create_sub_accounts::<T>(&caller, s).unwrap().try_into().unwrap(),466 ),467 )468 }).collect::<Vec<_>>();469 let origin = T::ForceOrigin::try_successful_origin().unwrap();470 }: _<T::RuntimeOrigin>(origin, identities)471472 add_sub {473 let s in 0 .. T::MaxSubAccounts::get() - 1;474475 let caller: T::AccountId = whitelisted_caller();476 let _ = add_sub_accounts::<T>(&caller, s)?;477 let sub = account("new_sub", 0, SEED);478 let data = Data::Raw(vec![0; 32].try_into().unwrap());479 ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not set.");480 }: _(RawOrigin::Signed(caller.clone()), T::Lookup::unlookup(sub), data)481 verify {482 ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s + 1, "Subs not added.");483 }484485 rename_sub {486 let s in 1 .. T::MaxSubAccounts::get();487488 let caller: T::AccountId = whitelisted_caller();489 let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);490 let data = Data::Raw(vec![1; 32].try_into().unwrap());491 ensure!(SuperOf::<T>::get(&sub).unwrap().1 != data, "data already set");492 }: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()), data.clone())493 verify {494 ensure!(SuperOf::<T>::get(&sub).unwrap().1 == data, "data not set");495 }496497 remove_sub {498 let s in 1 .. T::MaxSubAccounts::get();499500 let caller: T::AccountId = whitelisted_caller();501 let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);502 ensure!(SuperOf::<T>::contains_key(&sub), "Sub doesn't exists");503 }: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()))504 verify {505 ensure!(!SuperOf::<T>::contains_key(&sub), "Sub not removed");506 }507508 quit_sub {509 let s in 0 .. T::MaxSubAccounts::get() - 1;510511 let caller: T::AccountId = whitelisted_caller();512 let sup = account("super", 0, SEED);513 let _ = add_sub_accounts::<T>(&sup, s)?;514 let sup_origin = RawOrigin::Signed(sup).into();515 Identity::<T>::add_sub(sup_origin, T::Lookup::unlookup(caller.clone()), Data::Raw(vec![0; 32].try_into().unwrap()))?;516 ensure!(SuperOf::<T>::contains_key(&caller), "Sub doesn't exists");517 }: _(RawOrigin::Signed(caller.clone()))518 verify {519 ensure!(!SuperOf::<T>::contains_key(&caller), "Sub not removed");520 }521522 impl_benchmark_test_suite!(Identity, crate::tests::new_test_ext(), crate::tests::Test);523}