From 9974a68ca16ddd4d7ee822edb9baf48e6b93906e Mon Sep 17 00:00:00 2001 From: "polka.dom" Date: Tue, 23 Jul 2024 10:42:04 -0700 Subject: [PATCH] Remove pallet::getter macro from pallet-identity (#4586) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As per #3326, removes pallet::getter macro usage from the pallet-identity. The syntax `StorageItem::::get()` should be used instead. Also makes all storage values public cc @muraca --------- Co-authored-by: Oliver Tale-Yazdi Co-authored-by: Bastian Köcher Co-authored-by: command-bot <> --- .../tests/people/people-rococo/src/lib.rs | 6 +- .../people-rococo/src/tests/reap_identity.rs | 10 +- .../tests/people/people-westend/src/lib.rs | 6 +- .../people-westend/src/tests/reap_identity.rs | 10 +- prdoc/pr_4586.prdoc | 22 +++ substrate/bin/node/runtime/src/impls.rs | 7 +- substrate/frame/alliance/src/mock.rs | 6 +- substrate/frame/identity/src/legacy.rs | 2 +- substrate/frame/identity/src/lib.rs | 164 ++++++++++++------ substrate/frame/identity/src/tests.rs | 73 ++++---- 10 files changed, 199 insertions(+), 107 deletions(-) create mode 100644 prdoc/pr_4586.prdoc diff --git a/cumulus/parachains/integration-tests/emulated/tests/people/people-rococo/src/lib.rs b/cumulus/parachains/integration-tests/emulated/tests/people/people-rococo/src/lib.rs index 43cd5bb85d3d..3c0533f775e2 100644 --- a/cumulus/parachains/integration-tests/emulated/tests/people/people-rococo/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/tests/people/people-rococo/src/lib.rs @@ -40,7 +40,7 @@ mod imports { genesis::ED as PEOPLE_ROCOCO_ED, people_rococo_runtime::{ people, xcm_config::XcmConfig as PeopleRococoXcmConfig, - ExistentialDeposit as PeopleRococoExistentialDeposit, + ExistentialDeposit as PeopleRococoExistentialDeposit, Runtime as PeopleRuntime, }, PeopleRococoParaPallet as PeopleRococoPallet, }, @@ -48,8 +48,8 @@ mod imports { genesis::ED as ROCOCO_ED, rococo_runtime::{ xcm_config::XcmConfig as RococoXcmConfig, BasicDeposit, ByteDeposit, - MaxAdditionalFields, MaxSubAccounts, RuntimeOrigin as RococoOrigin, - SubAccountDeposit, + MaxAdditionalFields, MaxSubAccounts, Runtime as RococoRuntime, + RuntimeOrigin as RococoOrigin, SubAccountDeposit, }, RococoRelayPallet as RococoPallet, }, diff --git a/cumulus/parachains/integration-tests/emulated/tests/people/people-rococo/src/tests/reap_identity.rs b/cumulus/parachains/integration-tests/emulated/tests/people/people-rococo/src/tests/reap_identity.rs index 342a8f053f60..10f0c61ed63c 100644 --- a/cumulus/parachains/integration-tests/emulated/tests/people/people-rococo/src/tests/reap_identity.rs +++ b/cumulus/parachains/integration-tests/emulated/tests/people/people-rococo/src/tests/reap_identity.rs @@ -41,7 +41,7 @@ use crate::imports::*; use frame_support::BoundedVec; use pallet_balances::Event as BalancesEvent; -use pallet_identity::{legacy::IdentityInfo, Data, Event as IdentityEvent}; +use pallet_identity::{legacy::IdentityInfo, Data, Event as IdentityEvent, IdentityOf, SubsOf}; use people::{ BasicDeposit as BasicDepositParachain, ByteDeposit as ByteDepositParachain, IdentityInfo as IdentityInfoParachain, SubAccountDeposit as SubAccountDepositParachain, @@ -266,9 +266,9 @@ fn assert_set_id_parachain(id: &Identity) { // No amount should be reserved as deposit amounts are set to 0. let reserved_balance = PeopleRococoBalances::reserved_balance(PeopleRococoSender::get()); assert_eq!(reserved_balance, 0); - assert!(PeopleRococoIdentity::identity(PeopleRococoSender::get()).is_some()); + assert!(IdentityOf::::get(PeopleRococoSender::get()).is_some()); - let (_, sub_accounts) = PeopleRococoIdentity::subs_of(PeopleRococoSender::get()); + let (_, sub_accounts) = SubsOf::::get(PeopleRococoSender::get()); match id.subs { Subs::Zero => assert_eq!(sub_accounts.len(), 0), @@ -314,10 +314,10 @@ fn assert_reap_id_relay(total_deposit: Balance, id: &Identity) { ] ); // Identity should be gone. - assert!(PeopleRococoIdentity::identity(RococoRelaySender::get()).is_none()); + assert!(IdentityOf::::get(RococoRelaySender::get()).is_none()); // Subs should be gone. - let (_, sub_accounts) = RococoIdentity::subs_of(RococoRelaySender::get()); + let (_, sub_accounts) = SubsOf::::get(RococoRelaySender::get()); assert_eq!(sub_accounts.len(), 0); let reserved_balance = RococoBalances::reserved_balance(RococoRelaySender::get()); diff --git a/cumulus/parachains/integration-tests/emulated/tests/people/people-westend/src/lib.rs b/cumulus/parachains/integration-tests/emulated/tests/people/people-westend/src/lib.rs index 92f5d291ac84..689409fe5040 100644 --- a/cumulus/parachains/integration-tests/emulated/tests/people/people-westend/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/tests/people/people-westend/src/lib.rs @@ -40,7 +40,7 @@ mod imports { genesis::ED as PEOPLE_WESTEND_ED, people_westend_runtime::{ people, xcm_config::XcmConfig as PeopleWestendXcmConfig, - ExistentialDeposit as PeopleWestendExistentialDeposit, + ExistentialDeposit as PeopleWestendExistentialDeposit, Runtime as PeopleRuntime, }, PeopleWestendParaPallet as PeopleWestendPallet, }, @@ -48,8 +48,8 @@ mod imports { genesis::ED as WESTEND_ED, westend_runtime::{ xcm_config::XcmConfig as WestendXcmConfig, BasicDeposit, ByteDeposit, - MaxAdditionalFields, MaxSubAccounts, RuntimeOrigin as WestendOrigin, - SubAccountDeposit, + MaxAdditionalFields, MaxSubAccounts, Runtime as WestendRuntime, + RuntimeOrigin as WestendOrigin, SubAccountDeposit, }, WestendRelayPallet as WestendPallet, }, diff --git a/cumulus/parachains/integration-tests/emulated/tests/people/people-westend/src/tests/reap_identity.rs b/cumulus/parachains/integration-tests/emulated/tests/people/people-westend/src/tests/reap_identity.rs index 28d1be853204..cfbf4d7d9580 100644 --- a/cumulus/parachains/integration-tests/emulated/tests/people/people-westend/src/tests/reap_identity.rs +++ b/cumulus/parachains/integration-tests/emulated/tests/people/people-westend/src/tests/reap_identity.rs @@ -41,7 +41,7 @@ use crate::imports::*; use frame_support::BoundedVec; use pallet_balances::Event as BalancesEvent; -use pallet_identity::{legacy::IdentityInfo, Data, Event as IdentityEvent}; +use pallet_identity::{legacy::IdentityInfo, Data, Event as IdentityEvent, IdentityOf, SubsOf}; use people::{ BasicDeposit as BasicDepositParachain, ByteDeposit as ByteDepositParachain, IdentityInfo as IdentityInfoParachain, SubAccountDeposit as SubAccountDepositParachain, @@ -266,9 +266,9 @@ fn assert_set_id_parachain(id: &Identity) { // No amount should be reserved as deposit amounts are set to 0. let reserved_balance = PeopleWestendBalances::reserved_balance(PeopleWestendSender::get()); assert_eq!(reserved_balance, 0); - assert!(PeopleWestendIdentity::identity(PeopleWestendSender::get()).is_some()); + assert!(IdentityOf::::get(PeopleWestendSender::get()).is_some()); - let (_, sub_accounts) = PeopleWestendIdentity::subs_of(PeopleWestendSender::get()); + let (_, sub_accounts) = SubsOf::::get(PeopleWestendSender::get()); match id.subs { Subs::Zero => assert_eq!(sub_accounts.len(), 0), @@ -314,10 +314,10 @@ fn assert_reap_id_relay(total_deposit: Balance, id: &Identity) { ] ); // Identity should be gone. - assert!(PeopleWestendIdentity::identity(WestendRelaySender::get()).is_none()); + assert!(IdentityOf::::get(WestendRelaySender::get()).is_none()); // Subs should be gone. - let (_, sub_accounts) = WestendIdentity::subs_of(WestendRelaySender::get()); + let (_, sub_accounts) = SubsOf::::get(WestendRelaySender::get()); assert_eq!(sub_accounts.len(), 0); let reserved_balance = WestendBalances::reserved_balance(WestendRelaySender::get()); diff --git a/prdoc/pr_4586.prdoc b/prdoc/pr_4586.prdoc new file mode 100644 index 000000000000..46d166d0f977 --- /dev/null +++ b/prdoc/pr_4586.prdoc @@ -0,0 +1,22 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: Removed `pallet::getter` usage from pallet-identity + +doc: + - audience: Runtime Dev + description: | + This PR removed the `pallet::getter`s from `pallet-identity`. + The syntax `StorageItem::::get()` should be used instead. + +crates: + - name: pallet-identity + bump: minor + - name: pallet-alliance + bump: none + - name: kitchensink-runtime + bump: none + - name: people-rococo-integration-tests + bump: none + - name: people-westend-integration-tests + bump: none diff --git a/substrate/bin/node/runtime/src/impls.rs b/substrate/bin/node/runtime/src/impls.rs index 6c121fad624b..43e7a766e0e8 100644 --- a/substrate/bin/node/runtime/src/impls.rs +++ b/substrate/bin/node/runtime/src/impls.rs @@ -63,8 +63,8 @@ impl IdentityVerifier for AllianceIdentityVerifier { } fn has_good_judgement(who: &AccountId) -> bool { - use pallet_identity::Judgement; - crate::Identity::identity(who) + use pallet_identity::{IdentityOf, Judgement}; + IdentityOf::::get(who) .map(|(registration, _)| registration.judgements) .map_or(false, |judgements| { judgements @@ -74,7 +74,8 @@ impl IdentityVerifier for AllianceIdentityVerifier { } fn super_account_id(who: &AccountId) -> Option { - crate::Identity::super_of(who).map(|parent| parent.0) + use pallet_identity::SuperOf; + SuperOf::::get(who).map(|parent| parent.0) } } diff --git a/substrate/frame/alliance/src/mock.rs b/substrate/frame/alliance/src/mock.rs index 1a0a899bcccb..9cd96019781e 100644 --- a/substrate/frame/alliance/src/mock.rs +++ b/substrate/frame/alliance/src/mock.rs @@ -31,7 +31,7 @@ pub use frame_support::{ use frame_system::{EnsureRoot, EnsureSignedBy}; use pallet_identity::{ legacy::{IdentityField, IdentityInfo}, - Data, Judgement, + Data, IdentityOf, Judgement, SuperOf, }; pub use crate as pallet_alliance; @@ -146,7 +146,7 @@ impl IdentityVerifier for AllianceIdentityVerifier { fn has_good_judgement(who: &AccountId) -> bool { if let Some(judgements) = - Identity::identity(who).map(|(registration, _)| registration.judgements) + IdentityOf::::get(who).map(|(registration, _)| registration.judgements) { judgements .iter() @@ -157,7 +157,7 @@ impl IdentityVerifier for AllianceIdentityVerifier { } fn super_account_id(who: &AccountId) -> Option { - Identity::super_of(who).map(|parent| parent.0) + SuperOf::::get(who).map(|parent| parent.0) } } diff --git a/substrate/frame/identity/src/legacy.rs b/substrate/frame/identity/src/legacy.rs index c2107e929038..de5b9f79b559 100644 --- a/substrate/frame/identity/src/legacy.rs +++ b/substrate/frame/identity/src/legacy.rs @@ -173,7 +173,7 @@ impl> Default for IdentityInfo { impl> IdentityInfo { pub(crate) fn fields(&self) -> BitFlags { - let mut res = >::empty(); + let mut res = BitFlags::::empty(); if !self.display.is_none() { res.insert(IdentityField::Display); } diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index 776a08f5e9e8..08e29ddffd12 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -112,6 +112,7 @@ use frame_support::{ traits::{BalanceStatus, Currency, Get, OnUnbalanced, ReservableCurrency, StorageVersion}, BoundedVec, }; +use frame_system::pallet_prelude::*; pub use pallet::*; use sp_runtime::traits::{ AppendZerosInput, Hash, IdentifyAccount, Saturating, StaticLookup, Verify, Zero, @@ -132,7 +133,6 @@ type AccountIdLookupOf = <::Lookup as StaticLookup pub mod pallet { use super::*; use frame_support::pallet_prelude::*; - use frame_system::pallet_prelude::*; #[pallet::config] pub trait Config: frame_system::Config { @@ -215,8 +215,7 @@ pub mod pallet { /// /// TWOX-NOTE: OK ― `AccountId` is a secure hash. #[pallet::storage] - #[pallet::getter(fn identity)] - pub(super) type IdentityOf = StorageMap< + pub type IdentityOf = StorageMap< _, Twox64Concat, T::AccountId, @@ -227,8 +226,7 @@ pub mod pallet { /// The super-identity of an alternative "sub" identity together with its name, within that /// context. If the account is not some other account's sub-identity, then just `None`. #[pallet::storage] - #[pallet::getter(fn super_of)] - pub(super) type SuperOf = + pub type SuperOf = StorageMap<_, Blake2_128Concat, T::AccountId, (T::AccountId, Data), OptionQuery>; /// Alternative "sub" identities of this account. @@ -237,8 +235,7 @@ pub mod pallet { /// /// TWOX-NOTE: OK ― `AccountId` is a secure hash. #[pallet::storage] - #[pallet::getter(fn subs_of)] - pub(super) type SubsOf = StorageMap< + pub type SubsOf = StorageMap< _, Twox64Concat, T::AccountId, @@ -251,8 +248,7 @@ pub mod pallet { /// /// The index into this can be cast to `RegistrarIndex` to get a valid value. #[pallet::storage] - #[pallet::getter(fn registrars)] - pub(super) type Registrars = StorageValue< + pub type Registrars = StorageValue< _, BoundedVec< Option< @@ -269,8 +265,7 @@ pub mod pallet { /// A map of the accounts who are authorized to grant usernames. #[pallet::storage] - #[pallet::getter(fn authority)] - pub(super) type UsernameAuthorities = + pub type UsernameAuthorities = StorageMap<_, Twox64Concat, T::AccountId, AuthorityPropertiesOf, OptionQuery>; /// Reverse lookup from `username` to the `AccountId` that has registered it. The value should @@ -279,8 +274,7 @@ pub mod pallet { /// Multiple usernames may map to the same `AccountId`, but `IdentityOf` will only map to one /// primary username. #[pallet::storage] - #[pallet::getter(fn username)] - pub(super) type AccountOfUsername = + pub type AccountOfUsername = StorageMap<_, Blake2_128Concat, Username, T::AccountId, OptionQuery>; /// Usernames that an authority has granted, but that the account controller has not confirmed @@ -290,7 +284,6 @@ pub mod pallet { /// /// First tuple item is the account and second is the acceptance deadline. #[pallet::storage] - #[pallet::getter(fn preapproved_usernames)] pub type PendingUsernames = StorageMap< _, Blake2_128Concat, @@ -415,7 +408,7 @@ pub mod pallet { T::RegistrarOrigin::ensure_origin(origin)?; let account = T::Lookup::lookup(account)?; - let (i, registrar_count) = >::try_mutate( + let (i, registrar_count) = Registrars::::try_mutate( |registrars| -> Result<(RegistrarIndex, usize), DispatchError> { registrars .try_push(Some(RegistrarInfo { @@ -451,7 +444,7 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; - let (mut id, username) = match >::get(&sender) { + let (mut id, username) = match IdentityOf::::get(&sender) { Some((mut id, maybe_username)) => ( { // Only keep non-positive judgements. @@ -477,7 +470,7 @@ pub mod pallet { id.deposit = new_deposit; let judgements = id.judgements.len(); - >::insert(&sender, (id, username)); + IdentityOf::::insert(&sender, (id, username)); Self::deposit_event(Event::IdentitySet { who: sender }); Ok(Some(T::WeightInfo::set_identity(judgements as u32)).into()) @@ -507,13 +500,13 @@ pub mod pallet { subs: Vec<(T::AccountId, Data)>, ) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; - ensure!(>::contains_key(&sender), Error::::NotFound); + ensure!(IdentityOf::::contains_key(&sender), Error::::NotFound); ensure!( subs.len() <= T::MaxSubAccounts::get() as usize, Error::::TooManySubAccounts ); - let (old_deposit, old_ids) = >::get(&sender); + let (old_deposit, old_ids) = SubsOf::::get(&sender); let new_deposit = Self::subs_deposit(subs.len() as u32); let not_other_sub = @@ -529,19 +522,19 @@ pub mod pallet { // do nothing if they're equal. for s in old_ids.iter() { - >::remove(s); + SuperOf::::remove(s); } let mut ids = BoundedVec::::default(); for (id, name) in subs { - >::insert(&id, (sender.clone(), name)); + SuperOf::::insert(&id, (sender.clone(), name)); ids.try_push(id).expect("subs length is less than T::MaxSubAccounts; qed"); } let new_subs = ids.len(); if ids.is_empty() { - >::remove(&sender); + SubsOf::::remove(&sender); } else { - >::insert(&sender, (new_deposit, ids)); + SubsOf::::insert(&sender, (new_deposit, ids)); } Ok(Some( @@ -568,12 +561,12 @@ pub mod pallet { pub fn clear_identity(origin: OriginFor) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; - let (subs_deposit, sub_ids) = >::take(&sender); + let (subs_deposit, sub_ids) = SubsOf::::take(&sender); let (id, maybe_username) = - >::take(&sender).ok_or(Error::::NoIdentity)?; + IdentityOf::::take(&sender).ok_or(Error::::NoIdentity)?; let deposit = id.total_deposit().saturating_add(subs_deposit); for sub in sub_ids.iter() { - >::remove(sub); + SuperOf::::remove(sub); } if let Some(username) = maybe_username { AccountOfUsername::::remove(username); @@ -604,7 +597,7 @@ pub mod pallet { /// - `max_fee`: The maximum fee that may be paid. This should just be auto-populated as: /// /// ```nocompile - /// Self::registrars().get(reg_index).unwrap().fee + /// Registrars::::get().get(reg_index).unwrap().fee /// ``` /// /// Emits `JudgementRequested` if successful. @@ -616,13 +609,13 @@ pub mod pallet { #[pallet::compact] max_fee: BalanceOf, ) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; - let registrars = >::get(); + let registrars = Registrars::::get(); let registrar = registrars .get(reg_index as usize) .and_then(Option::as_ref) .ok_or(Error::::EmptyIndex)?; ensure!(max_fee >= registrar.fee, Error::::FeeChanged); - let (mut id, username) = >::get(&sender).ok_or(Error::::NoIdentity)?; + let (mut id, username) = IdentityOf::::get(&sender).ok_or(Error::::NoIdentity)?; let item = (reg_index, Judgement::FeePaid(registrar.fee)); match id.judgements.binary_search_by_key(®_index, |x| x.0) { @@ -639,7 +632,7 @@ pub mod pallet { T::Currency::reserve(&sender, registrar.fee)?; let judgements = id.judgements.len(); - >::insert(&sender, (id, username)); + IdentityOf::::insert(&sender, (id, username)); Self::deposit_event(Event::JudgementRequested { who: sender, @@ -666,7 +659,7 @@ pub mod pallet { reg_index: RegistrarIndex, ) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; - let (mut id, username) = >::get(&sender).ok_or(Error::::NoIdentity)?; + let (mut id, username) = IdentityOf::::get(&sender).ok_or(Error::::NoIdentity)?; let pos = id .judgements @@ -681,7 +674,7 @@ pub mod pallet { let err_amount = T::Currency::unreserve(&sender, fee); debug_assert!(err_amount.is_zero()); let judgements = id.judgements.len(); - >::insert(&sender, (id, username)); + IdentityOf::::insert(&sender, (id, username)); Self::deposit_event(Event::JudgementUnrequested { who: sender, @@ -707,7 +700,7 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { let who = ensure_signed(origin)?; - let registrars = >::mutate(|rs| -> Result { + let registrars = Registrars::::mutate(|rs| -> Result { rs.get_mut(index as usize) .and_then(|x| x.as_mut()) .and_then(|r| { @@ -741,7 +734,7 @@ pub mod pallet { let who = ensure_signed(origin)?; let new = T::Lookup::lookup(new)?; - let registrars = >::mutate(|rs| -> Result { + let registrars = Registrars::::mutate(|rs| -> Result { rs.get_mut(index as usize) .and_then(|x| x.as_mut()) .and_then(|r| { @@ -775,7 +768,7 @@ pub mod pallet { let who = ensure_signed(origin)?; let registrars = - >::mutate(|registrars| -> Result { + Registrars::::mutate(|registrars| -> Result { let registrar = registrars .get_mut(index as usize) .and_then(|r| r.as_mut()) @@ -815,13 +808,13 @@ pub mod pallet { let sender = ensure_signed(origin)?; let target = T::Lookup::lookup(target)?; ensure!(!judgement.has_deposit(), Error::::InvalidJudgement); - >::get() + Registrars::::get() .get(reg_index as usize) .and_then(Option::as_ref) .filter(|r| r.account == sender) .ok_or(Error::::InvalidIndex)?; let (mut id, username) = - >::get(&target).ok_or(Error::::InvalidTarget)?; + IdentityOf::::get(&target).ok_or(Error::::InvalidTarget)?; if T::Hashing::hash_of(&id.info) != identity { return Err(Error::::JudgementForDifferentIdentity.into()) @@ -848,7 +841,7 @@ pub mod pallet { } let judgements = id.judgements.len(); - >::insert(&target, (id, username)); + IdentityOf::::insert(&target, (id, username)); Self::deposit_event(Event::JudgementGiven { target, registrar_index: reg_index }); Ok(Some(T::WeightInfo::provide_judgement(judgements as u32)).into()) @@ -880,12 +873,12 @@ pub mod pallet { // Figure out who we're meant to be clearing. let target = T::Lookup::lookup(target)?; // Grab their deposit (and check that they have one). - let (subs_deposit, sub_ids) = >::take(&target); + let (subs_deposit, sub_ids) = SubsOf::::take(&target); let (id, maybe_username) = - >::take(&target).ok_or(Error::::NoIdentity)?; + IdentityOf::::take(&target).ok_or(Error::::NoIdentity)?; let deposit = id.total_deposit().saturating_add(subs_deposit); for sub in sub_ids.iter() { - >::remove(sub); + SuperOf::::remove(sub); } if let Some(username) = maybe_username { AccountOfUsername::::remove(username); @@ -1199,6 +1192,79 @@ pub mod pallet { } impl Pallet { + /// Information that is pertinent to identify the entity behind an account. First item is the + /// registration, second is the account's primary username. + /// + /// TWOX-NOTE: OK ― `AccountId` is a secure hash. + pub fn identity( + who: T::AccountId, + ) -> Option<( + Registration, T::MaxRegistrars, T::IdentityInformation>, + Option>, + )> { + IdentityOf::::get(who) + } + + /// The super-identity of an alternative "sub" identity together with its name, within that + /// context. If the account is not some other account's sub-identity, then just `None`. + pub fn super_of(who: T::AccountId) -> Option<(T::AccountId, Data)> { + SuperOf::::get(who) + } + + /// Alternative "sub" identities of this account. + /// + /// The first item is the deposit, the second is a vector of the accounts. + /// + /// TWOX-NOTE: OK ― `AccountId` is a secure hash. + pub fn subs_of( + who: T::AccountId, + ) -> (BalanceOf, BoundedVec) { + SubsOf::::get(who) + } + + /// The set of registrars. Not expected to get very big as can only be added through a + /// special origin (likely a council motion). + /// + /// The index into this can be cast to `RegistrarIndex` to get a valid value. + pub fn registrars() -> BoundedVec< + Option< + RegistrarInfo< + BalanceOf, + T::AccountId, + ::FieldsIdentifier, + >, + >, + T::MaxRegistrars, + > { + Registrars::::get() + } + + /// A map of the accounts who are authorized to grant usernames. + pub fn authority(who: T::AccountId) -> Option> { + UsernameAuthorities::::get(who) + } + + /// Reverse lookup from `username` to the `AccountId` that has registered it. The value should + /// be a key in the `IdentityOf` map, but it may not if the user has cleared their identity. + /// + /// Multiple usernames may map to the same `AccountId`, but `IdentityOf` will only map to one + /// primary username. + pub fn username(username: Username) -> Option { + AccountOfUsername::::get(username) + } + + /// Usernames that an authority has granted, but that the account controller has not confirmed + /// that they want it. Used primarily in cases where the `AccountId` cannot provide a signature + /// because they are a pure proxy, multisig, etc. In order to confirm it, they should call + /// [`Call::accept_username`]. + /// + /// First tuple item is the account and second is the acceptance deadline. + pub fn preapproved_usernames( + username: Username, + ) -> Option<(T::AccountId, BlockNumberFor)> { + PendingUsernames::::get(username) + } + /// Get the subs of an account. pub fn subs(who: &T::AccountId) -> Vec<(T::AccountId, Data)> { SubsOf::::get(who) @@ -1210,7 +1276,7 @@ impl Pallet { /// Calculate the deposit required for a number of `sub` accounts. fn subs_deposit(subs: u32) -> BalanceOf { - T::SubAccountDeposit::get().saturating_mul(>::from(subs)) + T::SubAccountDeposit::get().saturating_mul(BalanceOf::::from(subs)) } /// Take the `current` deposit that `who` is holding, and update it to a `new` one. @@ -1240,7 +1306,7 @@ impl Pallet { /// Calculate the deposit required for an identity. fn calculate_identity_deposit(info: &T::IdentityInformation) -> BalanceOf { let bytes = info.encoded_size() as u32; - let byte_deposit = T::ByteDeposit::get().saturating_mul(>::from(bytes)); + let byte_deposit = T::ByteDeposit::get().saturating_mul(BalanceOf::::from(bytes)); T::BasicDeposit::get().saturating_add(byte_deposit) } @@ -1294,7 +1360,7 @@ impl Pallet { pub fn insert_username(who: &T::AccountId, username: Username) { // Check if they already have a primary. If so, leave it. If not, set it. // Likewise, check if they have an identity. If not, give them a minimal one. - let (reg, primary_username, new_is_primary) = match >::get(&who) { + let (reg, primary_username, new_is_primary) = match IdentityOf::::get(&who) { // User has an existing Identity and a primary username. Leave it. Some((reg, Some(primary))) => (reg, primary, false), // User has an Identity but no primary. Set the new one as primary. @@ -1349,15 +1415,15 @@ impl Pallet { pub fn reap_identity(who: &T::AccountId) -> Result<(u32, u32, u32), DispatchError> { // `take` any storage items keyed by `target` // identity - let (id, _maybe_username) = >::take(&who).ok_or(Error::::NoIdentity)?; + let (id, _maybe_username) = IdentityOf::::take(&who).ok_or(Error::::NoIdentity)?; let registrars = id.judgements.len() as u32; let encoded_byte_size = id.info.encoded_size() as u32; // subs - let (subs_deposit, sub_ids) = >::take(&who); + let (subs_deposit, sub_ids) = SubsOf::::take(&who); let actual_subs = sub_ids.len() as u32; for sub in sub_ids.iter() { - >::remove(sub); + SuperOf::::remove(sub); } // unreserve any deposits @@ -1387,7 +1453,7 @@ impl Pallet { // Calculate what deposit should be let encoded_byte_size = reg.info.encoded_size() as u32; let byte_deposit = - T::ByteDeposit::get().saturating_mul(>::from(encoded_byte_size)); + T::ByteDeposit::get().saturating_mul(BalanceOf::::from(encoded_byte_size)); let new_id_deposit = T::BasicDeposit::get().saturating_add(byte_deposit); // Update account @@ -1446,7 +1512,7 @@ impl Pallet { ) -> DispatchResult { let mut sub_accounts = BoundedVec::::default(); for (sub, name) in subs { - >::insert(&sub, (who.clone(), name)); + SuperOf::::insert(&sub, (who.clone(), name)); sub_accounts .try_push(sub) .expect("benchmark should not pass more than T::MaxSubAccounts"); diff --git a/substrate/frame/identity/src/tests.rs b/substrate/frame/identity/src/tests.rs index 09edd5de79bb..3adb823ad5da 100644 --- a/substrate/frame/identity/src/tests.rs +++ b/substrate/frame/identity/src/tests.rs @@ -365,7 +365,7 @@ fn adding_registrar_should_work() { let fields = IdentityField::Display | IdentityField::Legal; assert_ok!(Identity::set_fields(RuntimeOrigin::signed(three.clone()), 0, fields.bits())); assert_eq!( - Identity::registrars(), + Registrars::::get(), vec![Some(RegistrarInfo { account: three, fee: 10, fields: fields.bits() })] ); }); @@ -401,7 +401,7 @@ fn registration_should_work() { RuntimeOrigin::signed(ten.clone()), Box::new(ten_info.clone()) )); - assert_eq!(Identity::identity(ten.clone()).unwrap().0.info, ten_info); + assert_eq!(IdentityOf::::get(ten.clone()).unwrap().0.info, ten_info); assert_eq!(Balances::free_balance(ten.clone()), 1000 - id_deposit); assert_ok!(Identity::clear_identity(RuntimeOrigin::signed(ten.clone()))); assert_eq!(Balances::free_balance(ten.clone()), 1000); @@ -484,7 +484,10 @@ fn uninvited_judgement_should_work() { Judgement::Reasonable, identity_hash )); - assert_eq!(Identity::identity(ten).unwrap().0.judgements, vec![(0, Judgement::Reasonable)]); + assert_eq!( + IdentityOf::::get(ten).unwrap().0.judgements, + vec![(0, Judgement::Reasonable)] + ); }); } @@ -505,7 +508,7 @@ fn clearing_judgement_should_work() { BlakeTwo256::hash_of(&infoof_ten()) )); assert_ok!(Identity::clear_identity(RuntimeOrigin::signed(ten.clone()))); - assert_eq!(Identity::identity(ten), None); + assert_eq!(IdentityOf::::get(ten), None); }); } @@ -518,7 +521,7 @@ fn killing_slashing_should_work() { assert_ok!(Identity::set_identity(RuntimeOrigin::signed(ten.clone()), Box::new(ten_info))); assert_noop!(Identity::kill_identity(RuntimeOrigin::signed(one), ten.clone()), BadOrigin); assert_ok!(Identity::kill_identity(RuntimeOrigin::root(), ten.clone())); - assert_eq!(Identity::identity(ten.clone()), None); + assert_eq!(IdentityOf::::get(ten.clone()), None); assert_eq!(Balances::free_balance(ten.clone()), 1000 - id_deposit); assert_noop!( Identity::kill_identity(RuntimeOrigin::root(), ten), @@ -545,11 +548,11 @@ fn setting_subaccounts_should_work() { assert_ok!(Identity::set_subs(RuntimeOrigin::signed(ten.clone()), subs.clone())); assert_eq!(Balances::free_balance(ten.clone()), 1000 - id_deposit - sub_deposit); assert_eq!( - Identity::subs_of(ten.clone()), + SubsOf::::get(ten.clone()), (sub_deposit, vec![twenty.clone()].try_into().unwrap()) ); assert_eq!( - Identity::super_of(twenty.clone()), + SuperOf::::get(twenty.clone()), Some((ten.clone(), Data::Raw(vec![40; 1].try_into().unwrap()))) ); @@ -558,15 +561,15 @@ fn setting_subaccounts_should_work() { assert_ok!(Identity::set_subs(RuntimeOrigin::signed(ten.clone()), subs.clone())); assert_eq!(Balances::free_balance(ten.clone()), 1000 - id_deposit - 2 * sub_deposit); assert_eq!( - Identity::subs_of(ten.clone()), + SubsOf::::get(ten.clone()), (2 * sub_deposit, vec![twenty.clone(), thirty.clone()].try_into().unwrap()) ); assert_eq!( - Identity::super_of(twenty.clone()), + SuperOf::::get(twenty.clone()), Some((ten.clone(), Data::Raw(vec![40; 1].try_into().unwrap()))) ); assert_eq!( - Identity::super_of(thirty.clone()), + SuperOf::::get(thirty.clone()), Some((ten.clone(), Data::Raw(vec![50; 1].try_into().unwrap()))) ); @@ -576,25 +579,25 @@ fn setting_subaccounts_should_work() { // no change in the balance assert_eq!(Balances::free_balance(ten.clone()), 1000 - id_deposit - 2 * sub_deposit); assert_eq!( - Identity::subs_of(ten.clone()), + SubsOf::::get(ten.clone()), (2 * sub_deposit, vec![forty.clone(), thirty.clone()].try_into().unwrap()) ); - assert_eq!(Identity::super_of(twenty.clone()), None); + assert_eq!(SuperOf::::get(twenty.clone()), None); assert_eq!( - Identity::super_of(thirty.clone()), + SuperOf::::get(thirty.clone()), Some((ten.clone(), Data::Raw(vec![50; 1].try_into().unwrap()))) ); assert_eq!( - Identity::super_of(forty.clone()), + SuperOf::::get(forty.clone()), Some((ten.clone(), Data::Raw(vec![60; 1].try_into().unwrap()))) ); // clear assert_ok!(Identity::set_subs(RuntimeOrigin::signed(ten.clone()), vec![])); assert_eq!(Balances::free_balance(ten.clone()), 1000 - id_deposit); - assert_eq!(Identity::subs_of(ten.clone()), (0, BoundedVec::default())); - assert_eq!(Identity::super_of(thirty.clone()), None); - assert_eq!(Identity::super_of(forty), None); + assert_eq!(SubsOf::::get(ten.clone()), (0, BoundedVec::default())); + assert_eq!(SuperOf::::get(thirty.clone()), None); + assert_eq!(SuperOf::::get(forty), None); subs.push((twenty, Data::Raw(vec![40; 1].try_into().unwrap()))); assert_noop!( @@ -620,7 +623,7 @@ fn clearing_account_should_remove_subaccounts_and_refund() { )); assert_ok!(Identity::clear_identity(RuntimeOrigin::signed(ten.clone()))); assert_eq!(Balances::free_balance(ten), 1000); - assert!(Identity::super_of(twenty).is_none()); + assert!(SuperOf::::get(twenty).is_none()); }); } @@ -640,7 +643,7 @@ fn killing_account_should_remove_subaccounts_and_not_refund() { assert_eq!(Balances::free_balance(ten.clone()), 1000 - id_deposit - sub_deposit); assert_ok!(Identity::kill_identity(RuntimeOrigin::root(), ten.clone())); assert_eq!(Balances::free_balance(ten), 1000 - id_deposit - sub_deposit); - assert!(Identity::super_of(twenty).is_none()); + assert!(SuperOf::::get(twenty).is_none()); }); } @@ -857,8 +860,8 @@ fn reap_identity_works() { // reap assert_ok!(Identity::reap_identity(&ten)); // no identity or subs - assert!(Identity::identity(ten.clone()).is_none()); - assert!(Identity::super_of(twenty).is_none()); + assert!(IdentityOf::::get(ten.clone()).is_none()); + assert!(SuperOf::::get(twenty).is_none()); // balance is unreserved assert_eq!(Balances::free_balance(ten), 1000); }); @@ -887,7 +890,7 @@ fn poke_deposit_works() { None::>, ), ); - assert!(Identity::identity(ten.clone()).is_some()); + assert!(IdentityOf::::get(ten.clone()).is_some()); // Set a sub with zero deposit SubsOf::::insert::<_, (u64, BoundedVec, ConstU32<2>>)>( &ten, @@ -906,7 +909,7 @@ fn poke_deposit_works() { assert_eq!(Balances::free_balance(ten.clone()), 1000 - id_deposit - subs_deposit); // new registration deposit is 10 assert_eq!( - Identity::identity(&ten), + IdentityOf::::get(&ten), Some(( Registration { judgements: Default::default(), @@ -917,7 +920,7 @@ fn poke_deposit_works() { )) ); // new subs deposit is 10 vvvvvvvvvvvv - assert_eq!(Identity::subs_of(ten), (subs_deposit, vec![twenty].try_into().unwrap())); + assert_eq!(SubsOf::::get(ten), (subs_deposit, vec![twenty].try_into().unwrap())); }); } @@ -944,7 +947,7 @@ fn poke_deposit_does_not_insert_new_subs_storage() { None::>, ), ); - assert!(Identity::identity(ten.clone()).is_some()); + assert!(IdentityOf::::get(ten.clone()).is_some()); // Balance is free assert_eq!(Balances::free_balance(ten.clone()), 1000); @@ -957,7 +960,7 @@ fn poke_deposit_does_not_insert_new_subs_storage() { assert_eq!(Balances::free_balance(ten.clone()), 1000 - id_deposit); // new registration deposit is 10 assert_eq!( - Identity::identity(&ten), + IdentityOf::::get(&ten), Some(( Registration { judgements: Default::default(), @@ -1048,7 +1051,7 @@ fn set_username_with_signature_without_existing_identity_should_work() { // Even though user has no balance and no identity, they get a default one for free. assert_eq!( - Identity::identity(&who_account), + IdentityOf::::get(&who_account), Some(( Registration { judgements: Default::default(), @@ -1105,7 +1108,7 @@ fn set_username_with_signature_with_existing_identity_should_work() { )); assert_eq!( - Identity::identity(&who_account), + IdentityOf::::get(&who_account), Some(( Registration { judgements: Default::default(), @@ -1188,7 +1191,7 @@ fn set_username_with_bytes_signature_should_work() { // The username in storage should not include ``. As in, it's the original // `username_to_sign`. assert_eq!( - Identity::identity(&who_account), + IdentityOf::::get(&who_account), Some(( Registration { judgements: Default::default(), @@ -1248,7 +1251,7 @@ fn set_username_with_acceptance_should_work() { assert!(PendingUsernames::::get::<&Username>(&full_username).is_none()); // Check Identity storage assert_eq!( - Identity::identity(&who), + IdentityOf::::get(&who), Some(( Registration { judgements: Default::default(), @@ -1402,7 +1405,7 @@ fn setting_primary_should_work() { // First username set as primary. assert_eq!( - Identity::identity(&who_account), + IdentityOf::::get(&who_account), Some(( Registration { judgements: Default::default(), @@ -1427,7 +1430,7 @@ fn setting_primary_should_work() { // The primary is still the first username. assert_eq!( - Identity::identity(&who_account), + IdentityOf::::get(&who_account), Some(( Registration { judgements: Default::default(), @@ -1455,7 +1458,7 @@ fn setting_primary_should_work() { // The primary is now the second username. assert_eq!( - Identity::identity(&who_account), + IdentityOf::::get(&who_account), Some(( Registration { judgements: Default::default(), @@ -1655,7 +1658,7 @@ fn removing_dangling_usernames_should_work() { // The primary should still be the first one. assert_eq!( - Identity::identity(&who_account), + IdentityOf::::get(&who_account), Some(( Registration { judgements: Default::default(), @@ -1689,7 +1692,7 @@ fn removing_dangling_usernames_should_work() { assert_ok!(Identity::clear_identity(RuntimeOrigin::signed(who_account.clone()),)); // Identity is gone - assert!(Identity::identity(who_account.clone()).is_none()); + assert!(IdentityOf::::get(who_account.clone()).is_none()); // The reverse lookup of the primary is gone. assert!(AccountOfUsername::::get::<&Username>(&username_to_sign).is_none());