Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add GenesisConfig to identity pallet #14774

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
7 changes: 4 additions & 3 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
use grandpa_primitives::AuthorityId as GrandpaId;
use kitchensink_runtime::{
constants::currency::*, wasm_binary_unwrap, BabeConfig, BalancesConfig, Block, CouncilConfig,
DemocracyConfig, ElectionsConfig, ImOnlineConfig, IndicesConfig, MaxNominations,
NominationPoolsConfig, SessionConfig, SessionKeys, SocietyConfig, StakerStatus, StakingConfig,
SudoConfig, SystemConfig, TechnicalCommitteeConfig,
DemocracyConfig, ElectionsConfig, IdentityConfig, ImOnlineConfig, IndicesConfig,
MaxNominations, NominationPoolsConfig, SessionConfig, SessionKeys, SocietyConfig, StakerStatus,
StakingConfig, SudoConfig, SystemConfig, TechnicalCommitteeConfig,
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use sc_chain_spec::ChainSpecExtension;
Expand Down Expand Up @@ -298,6 +298,7 @@ pub fn testnet_genesis(
balances: BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect(),
},
identity: IdentityConfig { registrars: vec![] },
indices: IndicesConfig { indices: vec![] },
session: SessionConfig {
keys: initial_authorities
Expand Down
12 changes: 9 additions & 3 deletions bin/node/testing/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
use crate::keyring::*;
use kitchensink_runtime::{
constants::currency::*, wasm_binary_unwrap, AccountId, AssetsConfig, BabeConfig,
BalancesConfig, GluttonConfig, GrandpaConfig, IndicesConfig, RuntimeGenesisConfig,
SessionConfig, SocietyConfig, StakerStatus, StakingConfig, SystemConfig,
BalancesConfig, GluttonConfig, GrandpaConfig, IdentityConfig, IndicesConfig,
RuntimeGenesisConfig, SessionConfig, SocietyConfig, StakerStatus, StakingConfig, SystemConfig,
BABE_GENESIS_EPOCH_CONFIG,
};
use sp_keyring::{Ed25519Keyring, Sr25519Keyring};
use sp_runtime::Perbill;
use sp_runtime::{BoundedVec, Perbill};

/// Create genesis runtime configuration for tests.
pub fn config(code: Option<&[u8]>) -> RuntimeGenesisConfig {
Expand All @@ -52,6 +52,12 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec<AccountId>) -> Run
code: code.map(|x| x.to_vec()).unwrap_or_else(|| wasm_binary_unwrap().to_vec()),
..Default::default()
},
identity: IdentityConfig {
identities: vec![
(alice(), BoundedVec::try_from("Alice".to_string().as_bytes().to_vec()).unwrap()),
(bob(), BoundedVec::try_from("Bob".to_string().as_bytes().to_vec()).unwrap()),
],
},
indices: IndicesConfig { indices: vec![] },
balances: BalancesConfig { balances: endowed },
session: SessionConfig {
Expand Down
50 changes: 50 additions & 0 deletions frame/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup
#[frame_support::pallet]
pub mod pallet {
use super::*;
use enumflags2::BitFlags;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

Expand Down Expand Up @@ -201,6 +202,55 @@ pub mod pallet {
ValueQuery,
>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub registrars: Vec<(T::AccountId, Vec<(T::AccountId, BoundedVec<u8, ConstU32<32>>)>)>,
}

impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig { registrars: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
for (registrar, identities) in &self.registrars {
<Registrars<T>>::put(
BoundedVec::try_from(vec![Some(RegistrarInfo {
account: registrar.clone(),
fee: Zero::zero(),
fields: IdentityFields(<BitFlags<IdentityField>>::all()),
})])
.unwrap(),
);
for (account, name) in identities {
let judgements =
BoundedVec::try_from(vec![(0, Judgement::KnownGood); 1]).unwrap();
<IdentityOf<T>>::insert(
account,
Registration {
info: IdentityInfo {
display: Data::Raw(name.clone()),
twitter: Data::None,
riot: Data::None,
email: Data::None,
pgp_fingerprint: None,
image: Data::None,
legal: Data::None,
web: Data::None,
additional: BoundedVec::default(),
},
judgements,
deposit: Zero::zero(),
},
);
}
}
}
}

#[pallet::error]
pub enum Error<T> {
/// Too many subs-accounts.
Expand Down
29 changes: 29 additions & 0 deletions frame/identity/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,32 @@ fn test_has_identity() {
));
});
}

#[test]
fn test_genesis_config_should_register_identities() {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_identity::GenesisConfig::<Test> {
identities: vec![
(1, BoundedVec::try_from("One".to_string().as_bytes().to_vec()).unwrap()),
(2, BoundedVec::try_from("Two".to_string().as_bytes().to_vec()).unwrap()),
(3, BoundedVec::try_from("Three".to_string().as_bytes().to_vec()).unwrap()),
],
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext: sp_io::TestExternalities = t.into();
ext.execute_with(|| {
assert_eq!(
Identity::identity(1).unwrap().info.display,
Data::Raw(b"One".to_vec().try_into().unwrap())
);
assert_eq!(
Identity::identity(2).unwrap().info.display,
Data::Raw(b"Two".to_vec().try_into().unwrap())
);
assert_eq!(
Identity::identity(3).unwrap().info.display,
Data::Raw(b"Three".to_vec().try_into().unwrap())
);
});
}