From 3a6de3aad103cb819f366e8416f15dc95d53e580 Mon Sep 17 00:00:00 2001 From: grw-ms Date: Tue, 8 Aug 2023 08:47:25 -0700 Subject: [PATCH 1/9] feat: stub IdentityConfig --- bin/node/cli/src/chain_spec.rs | 7 ++++--- bin/node/testing/src/genesis.rs | 1 + frame/identity/src/lib.rs | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index 57d367e7c49a8..9a0af2f17d4ad 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -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; @@ -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 diff --git a/bin/node/testing/src/genesis.rs b/bin/node/testing/src/genesis.rs index 8063b8ef45708..385a9a45ea0d6 100644 --- a/bin/node/testing/src/genesis.rs +++ b/bin/node/testing/src/genesis.rs @@ -52,6 +52,7 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec) -> Run code: code.map(|x| x.to_vec()).unwrap_or_else(|| wasm_binary_unwrap().to_vec()), ..Default::default() }, + identity: Default::default(), indices: IndicesConfig { indices: vec![] }, balances: BalancesConfig { balances: endowed }, session: SessionConfig { diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index f192ee2b461b3..42d3b044658bf 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -201,6 +201,25 @@ pub mod pallet { ValueQuery, >; + #[pallet::genesis_config] + pub struct GenesisConfig { + pub identities: Vec, + } + + #[cfg(feature = "std")] + impl Default for GenesisConfig { + fn default() -> Self { + GenesisConfig { identities: Default::default() } + } + } + + #[pallet::genesis_build] + impl GenesisBuild for GenesisConfig { + fn build(&self) { + // todo: insert genesis accounts + } + } + #[pallet::error] pub enum Error { /// Too many subs-accounts. From 55ba65c891dd9c7d987854a91e34089686974e78 Mon Sep 17 00:00:00 2001 From: grw-ms Date: Wed, 9 Aug 2023 03:30:26 -0700 Subject: [PATCH 2/9] feat: register identities in GenesisBuild --- bin/node/testing/src/genesis.rs | 8 +++++--- frame/identity/src/lib.rs | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/bin/node/testing/src/genesis.rs b/bin/node/testing/src/genesis.rs index 385a9a45ea0d6..af3172349ace5 100644 --- a/bin/node/testing/src/genesis.rs +++ b/bin/node/testing/src/genesis.rs @@ -21,8 +21,8 @@ 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}; @@ -52,7 +52,9 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec) -> Run code: code.map(|x| x.to_vec()).unwrap_or_else(|| wasm_binary_unwrap().to_vec()), ..Default::default() }, - identity: Default::default(), + identity: IdentityConfig { + identities: vec![(alice(), "Alice".to_string()), (bob(), "Bob".to_string())], + }, indices: IndicesConfig { indices: vec![] }, balances: BalancesConfig { balances: endowed }, session: SessionConfig { diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index 42d3b044658bf..081b598602d51 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -79,6 +79,7 @@ mod types; pub mod weights; use frame_support::traits::{BalanceStatus, Currency, OnUnbalanced, ReservableCurrency}; +use scale_info::prelude::string::String; use sp_runtime::traits::{AppendZerosInput, Hash, Saturating, StaticLookup, Zero}; use sp_std::prelude::*; pub use weights::WeightInfo; @@ -203,7 +204,7 @@ pub mod pallet { #[pallet::genesis_config] pub struct GenesisConfig { - pub identities: Vec, + pub identities: Vec<(T::AccountId, String)>, } #[cfg(feature = "std")] @@ -216,7 +217,26 @@ pub mod pallet { #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { - // todo: insert genesis accounts + for (account, name) in &self.identities { + >::insert( + account, + Registration { + info: IdentityInfo { + display: Data::Raw(BoundedVec::try_from(name.encode()).unwrap()), + 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(), + deposit: Zero::zero(), + }, + ); + } } } From 232fdf0e60ff8d8c3519ea5ca781f3ef1734bee0 Mon Sep 17 00:00:00 2001 From: grw-ms Date: Wed, 16 Aug 2023 05:59:32 -0700 Subject: [PATCH 3/9] use BuildGenesisConfig instead of deprecated GenesisConfig --- frame/identity/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index 081b598602d51..aa400e4f0c52f 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -215,7 +215,7 @@ pub mod pallet { } #[pallet::genesis_build] - impl GenesisBuild for GenesisConfig { + impl BuildGenesisConfig for GenesisConfig { fn build(&self) { for (account, name) in &self.identities { >::insert( From 6a03dc696ef7093434895104135ea9cea8b62366 Mon Sep 17 00:00:00 2001 From: grw-ms Date: Wed, 16 Aug 2023 08:14:02 -0700 Subject: [PATCH 4/9] use String::as_bytes().to_vec() instead of encode(), add test --- frame/identity/src/lib.rs | 4 +++- frame/identity/src/tests.rs | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index aa400e4f0c52f..1dbd6ae03283b 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -222,7 +222,9 @@ pub mod pallet { account, Registration { info: IdentityInfo { - display: Data::Raw(BoundedVec::try_from(name.encode()).unwrap()), + display: Data::Raw( + BoundedVec::try_from(name.as_bytes().to_vec()).unwrap(), + ), twitter: Data::None, riot: Data::None, email: Data::None, diff --git a/frame/identity/src/tests.rs b/frame/identity/src/tests.rs index 1532980574c2a..89a591a7cdeb2 100644 --- a/frame/identity/src/tests.rs +++ b/frame/identity/src/tests.rs @@ -619,3 +619,28 @@ fn test_has_identity() { )); }); } + +#[test] +fn test_genesis_config_should_register_identities() { + let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + pallet_identity::GenesisConfig:: { + identities: vec![(1, "One".to_string()), (2, "Two".to_string()), (3, "Three".to_string())], + } + .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()) + ); + }); +} From 587a6fc3fe67c43b61694b660b277d2a1d3e61dc Mon Sep 17 00:00:00 2001 From: grw-ms Date: Wed, 16 Aug 2023 09:55:11 -0700 Subject: [PATCH 5/9] take Vec instead of String --- frame/identity/src/lib.rs | 7 ++----- frame/identity/src/tests.rs | 6 +++++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index 1dbd6ae03283b..a66f45aa23e3b 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -79,7 +79,6 @@ mod types; pub mod weights; use frame_support::traits::{BalanceStatus, Currency, OnUnbalanced, ReservableCurrency}; -use scale_info::prelude::string::String; use sp_runtime::traits::{AppendZerosInput, Hash, Saturating, StaticLookup, Zero}; use sp_std::prelude::*; pub use weights::WeightInfo; @@ -204,7 +203,7 @@ pub mod pallet { #[pallet::genesis_config] pub struct GenesisConfig { - pub identities: Vec<(T::AccountId, String)>, + pub identities: Vec<(T::AccountId, Vec)>, } #[cfg(feature = "std")] @@ -222,9 +221,7 @@ pub mod pallet { account, Registration { info: IdentityInfo { - display: Data::Raw( - BoundedVec::try_from(name.as_bytes().to_vec()).unwrap(), - ), + display: Data::Raw(BoundedVec::try_from(name.clone()).unwrap()), twitter: Data::None, riot: Data::None, email: Data::None, diff --git a/frame/identity/src/tests.rs b/frame/identity/src/tests.rs index 89a591a7cdeb2..8f43a5fc98b0f 100644 --- a/frame/identity/src/tests.rs +++ b/frame/identity/src/tests.rs @@ -624,7 +624,11 @@ fn test_has_identity() { fn test_genesis_config_should_register_identities() { let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); pallet_identity::GenesisConfig:: { - identities: vec![(1, "One".to_string()), (2, "Two".to_string()), (3, "Three".to_string())], + identities: vec![ + (1, "One".to_string().as_bytes().to_vec()), + (2, "Two".to_string().as_bytes().to_vec()), + (3, "Three".to_string().as_bytes().to_vec()), + ], } .assimilate_storage(&mut t) .unwrap(); From 89774f9a42b16793b9413bd0472da1bac91b2ec4 Mon Sep 17 00:00:00 2001 From: grw-ms Date: Thu, 17 Aug 2023 01:44:27 -0700 Subject: [PATCH 6/9] remove (feature="std") gate --- frame/identity/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index a66f45aa23e3b..73e4a80f5a12c 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -206,7 +206,6 @@ pub mod pallet { pub identities: Vec<(T::AccountId, Vec)>, } - #[cfg(feature = "std")] impl Default for GenesisConfig { fn default() -> Self { GenesisConfig { identities: Default::default() } From b52f93e028315e5eecd386fff5286d3c91a6c5ea Mon Sep 17 00:00:00 2001 From: grw-ms Date: Thu, 17 Aug 2023 01:45:06 -0700 Subject: [PATCH 7/9] take (AccountId, BoundedVec) for GenesisConfig --- frame/identity/src/lib.rs | 4 ++-- frame/identity/src/tests.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index 73e4a80f5a12c..853b19f972f76 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -203,7 +203,7 @@ pub mod pallet { #[pallet::genesis_config] pub struct GenesisConfig { - pub identities: Vec<(T::AccountId, Vec)>, + pub identities: Vec<(T::AccountId, BoundedVec>)>, } impl Default for GenesisConfig { @@ -220,7 +220,7 @@ pub mod pallet { account, Registration { info: IdentityInfo { - display: Data::Raw(BoundedVec::try_from(name.clone()).unwrap()), + display: Data::Raw(name.clone()), twitter: Data::None, riot: Data::None, email: Data::None, diff --git a/frame/identity/src/tests.rs b/frame/identity/src/tests.rs index 8f43a5fc98b0f..6cbee2e75b812 100644 --- a/frame/identity/src/tests.rs +++ b/frame/identity/src/tests.rs @@ -625,9 +625,9 @@ fn test_genesis_config_should_register_identities() { let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); pallet_identity::GenesisConfig:: { identities: vec![ - (1, "One".to_string().as_bytes().to_vec()), - (2, "Two".to_string().as_bytes().to_vec()), - (3, "Three".to_string().as_bytes().to_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) From b5eae62f0fea1280fb6c0a223babdfac04a3c247 Mon Sep 17 00:00:00 2001 From: grw-ms Date: Thu, 17 Aug 2023 01:51:11 -0700 Subject: [PATCH 8/9] update test genesisconfig --- bin/node/testing/src/genesis.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bin/node/testing/src/genesis.rs b/bin/node/testing/src/genesis.rs index af3172349ace5..d631f6b7f1bb1 100644 --- a/bin/node/testing/src/genesis.rs +++ b/bin/node/testing/src/genesis.rs @@ -26,7 +26,7 @@ use kitchensink_runtime::{ 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 { @@ -53,7 +53,10 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec) -> Run ..Default::default() }, identity: IdentityConfig { - identities: vec![(alice(), "Alice".to_string()), (bob(), "Bob".to_string())], + 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 }, From d35a8d45043a424f21670fcf1a4ce61e1c606fea Mon Sep 17 00:00:00 2001 From: grw-ms Date: Mon, 28 Aug 2023 04:23:07 -0700 Subject: [PATCH 9/9] pallet_identity: accept registrars in GenesisConfig not only identities --- bin/node/cli/src/chain_spec.rs | 2 +- frame/identity/src/lib.rs | 53 +++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index 9a0af2f17d4ad..50f77345bc945 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -298,7 +298,7 @@ pub fn testnet_genesis( balances: BalancesConfig { balances: endowed_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect(), }, - identity: IdentityConfig { identities: vec![] }, + identity: IdentityConfig { registrars: vec![] }, indices: IndicesConfig { indices: vec![] }, session: SessionConfig { keys: initial_authorities diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index 853b19f972f76..b3784f9f047a5 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -99,6 +99,7 @@ type AccountIdLookupOf = <::Lookup as StaticLookup #[frame_support::pallet] pub mod pallet { use super::*; + use enumflags2::BitFlags; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; @@ -203,37 +204,49 @@ pub mod pallet { #[pallet::genesis_config] pub struct GenesisConfig { - pub identities: Vec<(T::AccountId, BoundedVec>)>, + pub registrars: Vec<(T::AccountId, Vec<(T::AccountId, BoundedVec>)>)>, } impl Default for GenesisConfig { fn default() -> Self { - GenesisConfig { identities: Default::default() } + GenesisConfig { registrars: Default::default() } } } #[pallet::genesis_build] impl BuildGenesisConfig for GenesisConfig { fn build(&self) { - for (account, name) in &self.identities { - >::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(), - deposit: Zero::zero(), - }, + for (registrar, identities) in &self.registrars { + >::put( + BoundedVec::try_from(vec![Some(RegistrarInfo { + account: registrar.clone(), + fee: Zero::zero(), + fields: IdentityFields(>::all()), + })]) + .unwrap(), ); + for (account, name) in identities { + let judgements = + BoundedVec::try_from(vec![(0, Judgement::KnownGood); 1]).unwrap(); + >::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(), + }, + ); + } } } }