Skip to content
Snippets Groups Projects
Commit 80b78ae2 authored by Éloïs's avatar Éloïs
Browse files

fix(idty): make genesis build deterministic

parent e48056f7
No related branches found
No related tags found
No related merge requests found
...@@ -176,18 +176,15 @@ pub fn new_test_ext(initial_identities_len: usize) -> sp_io::TestExternalities { ...@@ -176,18 +176,15 @@ pub fn new_test_ext(initial_identities_len: usize) -> sp_io::TestExternalities {
system: SystemConfig::default(), system: SystemConfig::default(),
identity: IdentityConfig { identity: IdentityConfig {
identities: (1..=initial_identities_len) identities: (1..=initial_identities_len)
.map(|i| { .map(|i| pallet_identity::GenesisIdty {
( index: i as u32,
i as u64, owner_key: i as u64,
( name: pallet_identity::IdtyName::from(NAMES[i - 1]),
pallet_identity::IdtyName::from(NAMES[i - 1]), value: pallet_identity::IdtyValue {
pallet_identity::IdtyValue { next_creatable_identity_on: 0,
next_creatable_identity_on: 0, removable_on: 0,
removable_on: 0, status: pallet_identity::IdtyStatus::Validated,
status: pallet_identity::IdtyStatus::Validated, },
},
),
)
}) })
.collect(), .collect(),
}, },
......
...@@ -93,12 +93,18 @@ pub mod pallet { ...@@ -93,12 +93,18 @@ pub mod pallet {
// GENESIS STUFF // // GENESIS STUFF //
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
#[derive(Encode, Decode, Clone, PartialEq, Eq)]
pub struct GenesisIdty<T: Config> {
pub index: T::IdtyIndex,
pub owner_key: T::AccountId,
pub name: IdtyName,
pub value: IdtyValue<T::BlockNumber>,
}
#[pallet::genesis_config] #[pallet::genesis_config]
pub struct GenesisConfig<T: Config> { pub struct GenesisConfig<T: Config> {
pub identities: sp_std::collections::btree_map::BTreeMap< pub identities: Vec<GenesisIdty<T>>,
T::AccountId,
(IdtyName, IdtyValue<T::BlockNumber>),
>,
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
...@@ -114,28 +120,31 @@ pub mod pallet { ...@@ -114,28 +120,31 @@ pub mod pallet {
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> { impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) { fn build(&self) {
let mut names = sp_std::collections::btree_set::BTreeSet::new(); let mut names = sp_std::collections::btree_set::BTreeSet::new();
for (idty_name, idty_value) in self.identities.values() { for idty in &self.identities {
assert!( assert!(
!names.contains(&idty_name), !names.contains(&idty.name),
"Idty name {:?} is present twice", "Idty name {:?} is present twice",
&idty_name &idty.name
); );
assert!(idty_value.removable_on == T::BlockNumber::zero()); assert!(idty.value.removable_on == T::BlockNumber::zero());
names.insert(idty_name); names.insert(idty.name.clone());
} }
<IdentitiesCount<T>>::put(self.identities.len() as u64); let mut identities = self.identities.clone();
for (owner_key, (idty_name, idty_value)) in &self.identities { identities.sort_unstable_by(|a, b| a.index.cmp(&b.index));
<IdentitiesCount<T>>::put(identities.len() as u64);
for idty in identities.into_iter() {
let idty_index = Pallet::<T>::get_next_idty_index(); let idty_index = Pallet::<T>::get_next_idty_index();
if idty_value.removable_on > T::BlockNumber::zero() { if idty.value.removable_on > T::BlockNumber::zero() {
<IdentitiesRemovableOn<T>>::append( <IdentitiesRemovableOn<T>>::append(
idty_value.removable_on, idty.value.removable_on,
(idty_index, idty_value.status), (idty_index, idty.value.status),
) )
} }
<Identities<T>>::insert(idty_index, idty_value); <Identities<T>>::insert(idty_index, idty.value.clone());
IdentitiesNames::<T>::insert(idty_name, ()); IdentitiesNames::<T>::insert(idty.name.clone(), ());
IdentityIndexOf::<T>::insert(owner_key, idty_index); IdentityIndexOf::<T>::insert(idty.owner_key, idty_index);
} }
} }
} }
......
...@@ -15,30 +15,30 @@ ...@@ -15,30 +15,30 @@
// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
use crate::mock::*; use crate::mock::*;
use crate::{Error, IdtyName, IdtyValue}; use crate::{Error, GenesisIdty, IdtyName, IdtyValue};
//use frame_support::assert_err; //use frame_support::assert_err;
use frame_support::assert_ok; use frame_support::assert_ok;
use frame_system::{EventRecord, Phase}; use frame_system::{EventRecord, Phase};
use maplit::btreemap;
use std::collections::BTreeMap;
type IdtyVal = IdtyValue<u64>; type IdtyVal = IdtyValue<u64>;
fn alice() -> (IdtyName, IdtyVal) { fn alice() -> GenesisIdty<Test> {
( GenesisIdty {
IdtyName::from("Alice"), index: 1,
IdtyVal { owner_key: 1,
name: IdtyName::from("Alice"),
value: IdtyVal {
next_creatable_identity_on: 0, next_creatable_identity_on: 0,
removable_on: 0, removable_on: 0,
status: crate::IdtyStatus::Validated, status: crate::IdtyStatus::Validated,
}, },
) }
} }
#[test] #[test]
fn test_no_identity() { fn test_no_identity() {
new_test_ext(IdentityConfig { new_test_ext(IdentityConfig {
identities: BTreeMap::new(), identities: Vec::new(),
}) })
.execute_with(|| { .execute_with(|| {
assert_eq!(Identity::identities_count(), 0); assert_eq!(Identity::identities_count(), 0);
...@@ -48,7 +48,7 @@ fn test_no_identity() { ...@@ -48,7 +48,7 @@ fn test_no_identity() {
#[test] #[test]
fn test_create_identity_ok() { fn test_create_identity_ok() {
new_test_ext(IdentityConfig { new_test_ext(IdentityConfig {
identities: btreemap![1 => alice()], identities: vec![alice()],
}) })
.execute_with(|| { .execute_with(|| {
// We need to initialize at least one block before any call // We need to initialize at least one block before any call
...@@ -72,7 +72,7 @@ fn test_create_identity_ok() { ...@@ -72,7 +72,7 @@ fn test_create_identity_ok() {
#[test] #[test]
fn test_idty_creation_period() { fn test_idty_creation_period() {
new_test_ext(IdentityConfig { new_test_ext(IdentityConfig {
identities: btreemap![1 => alice()], identities: vec![alice()],
}) })
.execute_with(|| { .execute_with(|| {
// We need to initialize at least one block before any call // We need to initialize at least one block before any call
......
...@@ -14,8 +14,6 @@ ...@@ -14,8 +14,6 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
pub use pallet_identity::IdtyName;
use super::AccountId; use super::AccountId;
use frame_support::pallet_prelude::*; use frame_support::pallet_prelude::*;
use scale_info::TypeInfo; use scale_info::TypeInfo;
......
...@@ -24,6 +24,7 @@ pub mod handlers; ...@@ -24,6 +24,7 @@ pub mod handlers;
mod pallets_config; mod pallets_config;
pub mod providers; pub mod providers;
pub use pallet_identity::{GenesisIdty, IdtyName, IdtyStatus, IdtyValue};
pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic; pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
/// Some way of identifying an account on the chain. We intentionally make it equivalent /// Some way of identifying an account on the chain. We intentionally make it equivalent
......
...@@ -31,7 +31,6 @@ pub use common_runtime::{ ...@@ -31,7 +31,6 @@ pub use common_runtime::{
}; };
pub use pallet_balances::Call as BalancesCall; pub use pallet_balances::Call as BalancesCall;
pub use pallet_duniter_test_parameters::Parameters as GenesisParameters; pub use pallet_duniter_test_parameters::Parameters as GenesisParameters;
pub use pallet_identity::{IdtyStatus, IdtyValue};
pub use pallet_im_online::sr25519::AuthorityId as ImOnlineId; pub use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_session::historical as session_historical; use pallet_session::historical as session_historical;
pub use pallet_timestamp::Call as TimestampCall; pub use pallet_timestamp::Call as TimestampCall;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment