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 { identities: 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
37 changes: 37 additions & 0 deletions frame/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,43 @@ pub mod pallet {
ValueQuery,
>;

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

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

#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
for (account, name) in &self.identities {
<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: BoundedVec::default(),
Copy link
Member

@ggwpez ggwpez Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much sense does it make not to provide any judgments?
Having no judgement is as good as having no identity at all.
But not sure if its possible without also adding a genesis judge…

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you suggested it, we are now depending upon you to implement a genesis identity judge :)

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())
);
});
}