From 42b7bebae7e59c1104dd61b1755569e197904aa2 Mon Sep 17 00:00:00 2001 From: Fahrrader Date: Wed, 18 Jan 2023 19:15:18 +0000 Subject: [PATCH] feat(identity): force set subs --- --- a/Makefile +++ b/Makefile @@ -146,5 +146,5 @@ make _bench PALLET=app-promotion PALLET_DIR=app-promotion .PHONY: bench -# Disabled: bench-scheduler, bench-collator-selection, bench-rmrk-core, bench-rmrk-equip -bench: bench-evm-migration bench-unique bench-structure bench-fungible bench-refungible bench-nonfungible bench-configuration bench-foreign-assets bench-identity +# Disabled: bench-scheduler, bench-collator-selection, bench-identity, bench-rmrk-core, bench-rmrk-equip +bench: bench-evm-migration bench-unique bench-structure bench-fungible bench-refungible bench-nonfungible bench-configuration bench-foreign-assets --- a/pallets/identity/src/benchmarking.rs +++ b/pallets/identity/src/benchmarking.rs @@ -417,7 +417,7 @@ let n in 0..600; use frame_benchmarking::account; let identities = (0..n).map(|i| ( - account("caller", i, 0), + account("caller", i, SEED), Registration::, T::MaxRegistrars, T::MaxAdditionalFields> { judgements: Default::default(), deposit: Default::default(), @@ -433,7 +433,7 @@ use frame_benchmarking::account; let origin = T::ForceOrigin::successful_origin(); let identities = (0..n).map(|i| ( - account("caller", i, 0), + account("caller", i, SEED), Registration::, T::MaxRegistrars, T::MaxAdditionalFields> { judgements: Default::default(), deposit: Default::default(), @@ -446,6 +446,20 @@ let identities = identities.into_iter().map(|(acc, _)| acc).collect::>(); }: _(origin, identities) + force_set_subs { + let s in 0 .. T::MaxSubAccounts::get(); + let n in 0..600; + use frame_benchmarking::account; + let identities = (0..n).map(|i| ( + account("caller", i, SEED), + ( + BalanceOf::::max_value(), + create_sub_accounts::(&caller, s)?.try_into().unwrap(), + ), + )).collect::>(); + let origin = T::ForceOrigin::successful_origin(); + }: _(origin, identities) + add_sub { let s in 0 .. T::MaxSubAccounts::get() - 1; --- a/pallets/identity/src/lib.rs +++ b/pallets/identity/src/lib.rs @@ -314,6 +314,8 @@ main: T::AccountId, deposit: BalanceOf, }, + /// A number of identities were forcibly updated with new sub-identities. + SubIdentitiesInserted { amount: u32 }, } #[pallet::call] @@ -1137,13 +1139,64 @@ ) -> DispatchResult { T::ForceOrigin::ensure_origin(origin)?; for identity in identities.clone() { - IdentityOf::::set(identity, None); + let (_, sub_ids) = >::take(&identity); + >::remove(&identity); + for sub in sub_ids.iter() { + >::remove(sub); + } } Self::deposit_event(Event::IdentitiesRemoved { amount: identities.len() as u32, }); Ok(()) } + + /// Set sub-identities to be associated with the provided accounts as force origin. + /// + /// This is not meant to operate in tandem with the identity pallet as is, + /// and be instead used to keep identities made and verified externally, + /// forbidden from interacting with an ordinary user, since it ignores any safety mechanism. + #[pallet::call_index(17)] + #[pallet::weight(T::WeightInfo::force_set_subs( + T::MaxSubAccounts::get(), // S + subs.len() as u32, // N + ))] + pub fn force_set_subs( + origin: OriginFor, + subs: Vec<( + T::AccountId, + ( + BalanceOf, + BoundedVec<(T::AccountId, Data), T::MaxSubAccounts>, + ), + )>, + ) -> DispatchResult { + T::ForceOrigin::ensure_origin(origin)?; + for identity in subs.clone() { + let account = identity.0; + let (_, old_subs) = >::get(&account); + for old_sub in old_subs { + >::remove(old_sub); + } + + let mut ids = BoundedVec::::default(); + for (id, name) in identity.1 .1 { + >::insert(&id, (account.clone(), name)); + ids.try_push(id) + .expect("subs length is less than T::MaxSubAccounts; qed"); + } + + if ids.is_empty() { + >::remove(&account); + } else { + >::insert(account, (identity.1 .0, ids)); + } + } + Self::deposit_event(Event::SubIdentitiesInserted { + amount: subs.len() as u32, + }); + Ok(()) + } } } --- a/pallets/identity/src/weights.rs +++ b/pallets/identity/src/weights.rs @@ -78,6 +78,7 @@ fn kill_identity(r: u32, s: u32, x: u32, ) -> Weight; fn force_insert_identities(x: u32, n: u32, ) -> Weight; fn force_remove_identities(x: u32, n: u32, ) -> Weight; + fn force_set_subs(s: u32, n: u32, ) -> Weight; fn add_sub(s: u32, ) -> Weight; fn rename_sub(s: u32, ) -> Weight; fn remove_sub(s: u32, ) -> Weight; @@ -273,6 +274,20 @@ .saturating_add(T::DbWeight::get().reads(1 as u64)) .saturating_add(T::DbWeight::get().writes(1 as u64).saturating_mul(n as u64)) } + // Storage: Identity IdentityOf (r:1 w:1) + // todo:collator + /// The range of component `s` is `[0, 100]`. + /// The range of component `n` is `[0, 600]`. + fn force_set_subs(s: u32, n: u32) -> Weight { + // Minimum execution time: 41_872 nanoseconds. + Weight::from_ref_time(40_230_216 as u64) + // Standard Error: 2_342 + .saturating_add(Weight::from_ref_time(145_168 as u64)) + // Standard Error: 457 + .saturating_add(Weight::from_ref_time(291_732 as u64).saturating_mul(s as u64)) + .saturating_add(T::DbWeight::get().reads(1 as u64)) + .saturating_add(T::DbWeight::get().writes(1 as u64).saturating_mul(n as u64)) + } // Storage: Identity IdentityOf (r:1 w:0) // Storage: Identity SuperOf (r:1 w:1) // Storage: Identity SubsOf (r:1 w:1) @@ -509,6 +524,20 @@ .saturating_add(RocksDbWeight::get().reads(1 as u64)) .saturating_add(RocksDbWeight::get().writes(1 as u64).saturating_mul(n as u64)) } + // Storage: Identity IdentityOf (r:1 w:1) + // todo:collator + /// The range of component `xs is `[0, 100]`. + /// The range of component `n` is `[0, 600]`. + fn force_set_subs(s: u32, n: u32) -> Weight { + // Minimum execution time: 41_872 nanoseconds. + Weight::from_ref_time(40_230_216 as u64) + // Standard Error: 2_342 + .saturating_add(Weight::from_ref_time(145_168 as u64)) + // Standard Error: 457 + .saturating_add(Weight::from_ref_time(291_732 as u64).saturating_mul(s as u64)) + .saturating_add(RocksDbWeight::get().reads(1 as u64)) + .saturating_add(RocksDbWeight::get().writes(1 as u64).saturating_mul(n as u64)) + } // Storage: Identity IdentityOf (r:1 w:0) // Storage: Identity SuperOf (r:1 w:1) // Storage: Identity SubsOf (r:1 w:1) -- gitstuff