Skip to content

Commit 9185195

Browse files
georgepisaltusam0x17joepetrowski
authored
Make IdentityInfo generic in pallet-identity (#1661)
Fixes #179 # Description This PR makes the structure containing identity information used in `pallet-identity` generic through the pallet `Config`. Additionally, the old structure is now available in a separate module called `simple` (pending rename) and is compatible with the new interface. Another change in this PR is that while the `additional` field in `IdentityInfo` stays for backwards compatibility reasons, the associated costs are stil present in the pallet through the `additional` function in the `IdentityInformationProvider` interface. This function is marked as deprecated as it is only a temporary solution to the backwards compatibility problem we had. In short, we could have removed the additional fields in the struct and done a migration, but we chose to wait and do it off-chain through the genesis of the system parachain. After we move the identity pallet to the parachain, additional fields will be migrated into the existing fields and the `additional` key-value store will be removed. Until that happens, this interface will provide the necessary information to properly account for the associated costs. Additionally, this PR fixes an unrelated issue; the `IdentityField` enum used to represent the fields as bitflags couldn't store more than 8 fields, even though it was marked as `#[repr(u64)]`. This was because of the `derive` implementation of `TypeInfo`, which assumed `u8` semantics. The custom implementation of this trait in 0105cc0 fixes the issue. --------- Signed-off-by: georgepisaltu <[email protected]> Co-authored-by: Sam Johnson <[email protected]> Co-authored-by: joe petrowski <[email protected]>
1 parent 35eb133 commit 9185195

File tree

10 files changed

+404
-294
lines changed

10 files changed

+404
-294
lines changed

polkadot/runtime/rococo/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use frame_support::{
7676
};
7777
use frame_system::EnsureRoot;
7878
use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId};
79+
use pallet_identity::simple::IdentityInfo;
7980
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
8081
use pallet_session::historical as session_historical;
8182
use pallet_transaction_payment::{CurrencyAdapter, FeeDetails, RuntimeDispatchInfo};
@@ -610,6 +611,7 @@ impl pallet_identity::Config for Runtime {
610611
type SubAccountDeposit = SubAccountDeposit;
611612
type MaxSubAccounts = MaxSubAccounts;
612613
type MaxAdditionalFields = MaxAdditionalFields;
614+
type IdentityInformation = IdentityInfo<MaxAdditionalFields>;
613615
type MaxRegistrars = MaxRegistrars;
614616
type Slashed = Treasury;
615617
type ForceOrigin = EitherOf<EnsureRoot<Self::AccountId>, GeneralAdmin>;

polkadot/runtime/westend/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use frame_support::{
4040
};
4141
use frame_system::EnsureRoot;
4242
use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId};
43+
use pallet_identity::simple::IdentityInfo;
4344
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
4445
use pallet_session::historical as session_historical;
4546
use pallet_transaction_payment::{CurrencyAdapter, FeeDetails, RuntimeDispatchInfo};
@@ -876,6 +877,7 @@ impl pallet_identity::Config for Runtime {
876877
type SubAccountDeposit = SubAccountDeposit;
877878
type MaxSubAccounts = MaxSubAccounts;
878879
type MaxAdditionalFields = MaxAdditionalFields;
880+
type IdentityInformation = IdentityInfo<MaxAdditionalFields>;
879881
type MaxRegistrars = MaxRegistrars;
880882
type ForceOrigin = EitherOf<EnsureRoot<Self::AccountId>, GeneralAdmin>;
881883
type RegistrarOrigin = EitherOf<EnsureRoot<Self::AccountId>, GeneralAdmin>;

substrate/bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Moment, Nonce};
6060
use pallet_asset_conversion::{NativeOrAssetId, NativeOrAssetIdConverter};
6161
use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600};
6262
use pallet_election_provider_multi_phase::{GeometricDepositBase, SolutionAccuracyOf};
63+
use pallet_identity::simple::IdentityInfo;
6364
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
6465
use pallet_nfts::PalletFeatures;
6566
use pallet_nis::WithMaximumOf;
@@ -1474,6 +1475,7 @@ impl pallet_identity::Config for Runtime {
14741475
type SubAccountDeposit = SubAccountDeposit;
14751476
type MaxSubAccounts = MaxSubAccounts;
14761477
type MaxAdditionalFields = MaxAdditionalFields;
1478+
type IdentityInformation = IdentityInfo<MaxAdditionalFields>;
14771479
type MaxRegistrars = MaxRegistrars;
14781480
type Slashed = Treasury;
14791481
type ForceOrigin = EnsureRootOrHalfCouncil;

substrate/frame/alliance/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ use frame_support::{
112112
},
113113
weights::Weight,
114114
};
115-
use pallet_identity::IdentityField;
115+
use pallet_identity::simple::IdentityField;
116116
use scale_info::TypeInfo;
117117

118118
pub use pallet::*;

substrate/frame/alliance/src/mock.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub use frame_support::{
3131
BoundedVec,
3232
};
3333
use frame_system::{EnsureRoot, EnsureSignedBy};
34-
use pallet_identity::{Data, IdentityInfo, Judgement};
34+
use pallet_identity::{simple::IdentityInfo, Data, Judgement};
3535

3636
pub use crate as pallet_alliance;
3737

@@ -121,6 +121,7 @@ impl pallet_identity::Config for Test {
121121
type SubAccountDeposit = SubAccountDeposit;
122122
type MaxSubAccounts = MaxSubAccounts;
123123
type MaxAdditionalFields = MaxAdditionalFields;
124+
type IdentityInformation = IdentityInfo<MaxAdditionalFields>;
124125
type MaxRegistrars = MaxRegistrars;
125126
type Slashed = ();
126127
type RegistrarOrigin = EnsureOneOrRoot;

substrate/frame/identity/src/benchmarking.rs

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use super::*;
2323

2424
use crate::Pallet as Identity;
25+
use enumflags2::BitFlag;
2526
use frame_benchmarking::{
2627
account, impl_benchmark_test_suite, v2::*, whitelisted_caller, BenchmarkError,
2728
};
@@ -48,14 +49,9 @@ fn add_registrars<T: Config>(r: u32) -> Result<(), &'static str> {
4849
.expect("RegistrarOrigin has no successful origin required for the benchmark");
4950
Identity::<T>::add_registrar(registrar_origin, registrar_lookup)?;
5051
Identity::<T>::set_fee(RawOrigin::Signed(registrar.clone()).into(), i, 10u32.into())?;
51-
let fields =
52-
IdentityFields(
53-
IdentityField::Display |
54-
IdentityField::Legal | IdentityField::Web |
55-
IdentityField::Riot | IdentityField::Email |
56-
IdentityField::PgpFingerprint |
57-
IdentityField::Image | IdentityField::Twitter,
58-
);
52+
let fields = IdentityFields(
53+
<T::IdentityInformation as IdentityInformationProvider>::IdentityField::all(),
54+
);
5955
Identity::<T>::set_fields(RawOrigin::Signed(registrar.clone()).into(), i, fields)?;
6056
}
6157

@@ -81,7 +77,7 @@ fn create_sub_accounts<T: Config>(
8177
// Set identity so `set_subs` does not fail.
8278
if IdentityOf::<T>::get(who).is_none() {
8379
let _ = T::Currency::make_free_balance_be(who, BalanceOf::<T>::max_value() / 2u32.into());
84-
let info = create_identity_info::<T>(1);
80+
let info = T::IdentityInformation::create_identity_info(1);
8581
Identity::<T>::set_identity(who_origin.into(), Box::new(info))?;
8682
}
8783

@@ -102,24 +98,6 @@ fn add_sub_accounts<T: Config>(
10298
Ok(subs)
10399
}
104100

105-
// This creates an `IdentityInfo` object with `num_fields` extra fields.
106-
// All data is pre-populated with some arbitrary bytes.
107-
fn create_identity_info<T: Config>(num_fields: u32) -> IdentityInfo<T::MaxAdditionalFields> {
108-
let data = Data::Raw(vec![0; 32].try_into().unwrap());
109-
110-
IdentityInfo {
111-
additional: vec![(data.clone(), data.clone()); num_fields as usize].try_into().unwrap(),
112-
display: data.clone(),
113-
legal: data.clone(),
114-
web: data.clone(),
115-
riot: data.clone(),
116-
email: data.clone(),
117-
pgp_fingerprint: Some([0; 20]),
118-
image: data.clone(),
119-
twitter: data,
120-
}
121-
}
122-
123101
#[benchmarks]
124102
mod benchmarks {
125103
use super::*;
@@ -153,7 +131,7 @@ mod benchmarks {
153131
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
154132

155133
// Add an initial identity
156-
let initial_info = create_identity_info::<T>(1);
134+
let initial_info = T::IdentityInformation::create_identity_info(1);
157135
Identity::<T>::set_identity(caller_origin.clone(), Box::new(initial_info.clone()))?;
158136

159137
// User requests judgement from all the registrars, and they approve
@@ -174,7 +152,10 @@ mod benchmarks {
174152
}
175153

176154
#[extrinsic_call]
177-
_(RawOrigin::Signed(caller.clone()), Box::new(create_identity_info::<T>(x)));
155+
_(
156+
RawOrigin::Signed(caller.clone()),
157+
Box::new(T::IdentityInformation::create_identity_info(x)),
158+
);
178159

179160
assert_last_event::<T>(Event::<T>::IdentitySet { who: caller }.into());
180161
Ok(())
@@ -235,7 +216,7 @@ mod benchmarks {
235216
let _ = add_sub_accounts::<T>(&caller, s)?;
236217

237218
// Create their main identity with x additional fields
238-
let info = create_identity_info::<T>(x);
219+
let info = T::IdentityInformation::create_identity_info(x);
239220
Identity::<T>::set_identity(caller_origin.clone(), Box::new(info.clone()))?;
240221

241222
// User requests judgement from all the registrars, and they approve
@@ -275,7 +256,7 @@ mod benchmarks {
275256
add_registrars::<T>(r)?;
276257

277258
// Create their main identity with x additional fields
278-
let info = create_identity_info::<T>(x);
259+
let info = T::IdentityInformation::create_identity_info(x);
279260
let caller_origin =
280261
<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));
281262
Identity::<T>::set_identity(caller_origin.clone(), Box::new(info))?;
@@ -302,7 +283,7 @@ mod benchmarks {
302283
add_registrars::<T>(r)?;
303284

304285
// Create their main identity with x additional fields
305-
let info = create_identity_info::<T>(x);
286+
let info = T::IdentityInformation::create_identity_info(x);
306287
let caller_origin =
307288
<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));
308289
Identity::<T>::set_identity(caller_origin.clone(), Box::new(info))?;
@@ -386,14 +367,9 @@ mod benchmarks {
386367
.expect("RegistrarOrigin has no successful origin required for the benchmark");
387368
Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;
388369

389-
let fields =
390-
IdentityFields(
391-
IdentityField::Display |
392-
IdentityField::Legal | IdentityField::Web |
393-
IdentityField::Riot | IdentityField::Email |
394-
IdentityField::PgpFingerprint |
395-
IdentityField::Image | IdentityField::Twitter,
396-
);
370+
let fields = IdentityFields(
371+
<T::IdentityInformation as IdentityInformationProvider>::IdentityField::all(),
372+
);
397373

398374
let registrars = Registrars::<T>::get();
399375
ensure!(
@@ -431,7 +407,7 @@ mod benchmarks {
431407

432408
add_registrars::<T>(r)?;
433409

434-
let info = create_identity_info::<T>(x);
410+
let info = T::IdentityInformation::create_identity_info(x);
435411
let info_hash = T::Hashing::hash_of(&info);
436412
Identity::<T>::set_identity(user_origin.clone(), Box::new(info))?;
437413

@@ -464,7 +440,7 @@ mod benchmarks {
464440
let target_lookup = T::Lookup::unlookup(target.clone());
465441
let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());
466442

467-
let info = create_identity_info::<T>(x);
443+
let info = T::IdentityInformation::create_identity_info(x);
468444
Identity::<T>::set_identity(target_origin.clone(), Box::new(info.clone()))?;
469445
let _ = add_sub_accounts::<T>(&target, s)?;
470446

0 commit comments

Comments
 (0)