Skip to content

Commit

Permalink
Merge pull request #248 from Fair-Squares/fix-representative-genesis-…
Browse files Browse the repository at this point in the history
…config

fix: role pallet genesis configuration
  • Loading branch information
cuteolaf authored Feb 23, 2023
2 parents 64fca39 + b2a8e07 commit 6374b50
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
6 changes: 6 additions & 0 deletions pallets/roles/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl<T: Config> Pallet<T> {

pub fn init_representatives(representatives: Vec<AccountIdOf<T>>) {
let now = <frame_system::Pallet<T>>::block_number();
let rep_count = representatives.len() as u32;
for account in representatives.into_iter() {
AccountsRolesLog::<T>::insert(&account, Accounts::REPRESENTATIVE);
RepresentativeLog::<T>::insert(
Expand All @@ -186,5 +187,10 @@ impl<T: Config> Pallet<T> {
},
);
}
let members = Self::total_members();
TotalMembers::<T>::put(members.saturating_add(rep_count));

let reps = Self::rep_num();
RepNumber::<T>::put(reps.saturating_add(rep_count));
}
}
15 changes: 10 additions & 5 deletions pallets/roles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,17 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
let servicer0 = self.new_admin.clone().unwrap(); // AccountId
let origin = T::Origin::from(RawOrigin::Signed(servicer0.clone())); //Origin
let source = T::Lookup::unlookup(servicer0); //Source
crate::Pallet::<T>::set_manager(origin, source).ok();
if self.new_admin.is_some() {
let servicer0 = self.new_admin.clone().unwrap(); // AccountId
let origin = T::Origin::from(RawOrigin::Signed(servicer0.clone())); //Origin
let source = T::Lookup::unlookup(servicer0); //Source
crate::Pallet::<T>::set_manager(origin, source).ok();
}

if !self.representatives.is_empty() {
crate::Pallet::<T>::init_representatives(self.representatives.clone());
}

crate::Pallet::<T>::init_representatives(self.representatives.clone());
}
}

Expand Down
22 changes: 17 additions & 5 deletions pallets/roles/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use frame_support::{
parameter_types,
traits::{ConstU32, ConstU64, GenesisBuild},
};
use sp_core::H256;
use frame_system as system;
pub use frame_system::RawOrigin;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

Expand Down Expand Up @@ -74,7 +73,7 @@ impl pallet_sudo::Config for Test {
}

parameter_types! {
pub const MaxMembers:u32 =5;
pub const MaxMembers:u32 = 5;
}
impl pallet_roles::Config for Test {
type Event = Event;
Expand All @@ -90,7 +89,20 @@ pub type Acc = pallet_roles::Accounts;
pub fn new_test_ext(root_key: u64) -> sp_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
pallet_sudo::GenesisConfig::<Test> { key: Some(root_key) }
.assimilate_storage(&mut t)
.unwrap();
.assimilate_storage(&mut t)
.unwrap();
t.into()
}

pub const HENRY: u64 = 100;
pub const GABRIEL: u64 = 101;

pub fn new_test_ext_with_genesis(root_key: u64) -> sp_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
pallet_sudo::GenesisConfig::<Test> { key: Some(root_key) }.assimilate_storage(&mut t).unwrap();
pallet_roles::GenesisConfig::<Test> {
new_admin: None,
representatives: vec![GABRIEL, HENRY],
}.assimilate_storage(&mut t).unwrap();
t.into()
}
10 changes: 10 additions & 0 deletions pallets/roles/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,13 @@ fn test_set_manager() {
assert_eq!(Sudo::key(), Some(2));
})
}

#[test]
fn test_genesis_config() {
new_test_ext_with_genesis(4).execute_with(|| {
assert_eq!(RoleModule::total_members(), 2);
assert_eq!(RoleModule::rep_num(), 2);
assert!(RoleModule::reps(HENRY).is_some());
assert!(RoleModule::reps(GABRIEL).is_some());
})
}

0 comments on commit 6374b50

Please sign in to comment.