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

pallet Collective: fix genesis member sort order #13988

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,8 @@ impl<T: Config<I>, I: 'static> InitializeMembers<T::AccountId> for Pallet<T, I>
fn initialize_members(members: &[T::AccountId]) {
if !members.is_empty() {
assert!(<Members<T, I>>::get().is_empty(), "Members are already initialized!");
let mut members = members.to_vec();
members.sort();
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
<Members<T, I>>::put(members);
}
}
Expand Down
27 changes: 23 additions & 4 deletions frame/collective/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ mod mock_democracy {
}

pub type MaxMembers = ConstU32<100>;
type AccountId = u64;

parameter_types! {
pub const MotionDuration: u64 = 3;
Expand All @@ -105,7 +106,7 @@ impl frame_system::Config for Test {
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
Expand Down Expand Up @@ -161,19 +162,26 @@ impl Config for Test {
type MaxProposalWeight = MaxProposalWeight;
}

pub struct ExtBuilder {}
pub struct ExtBuilder {
collective_members: Vec<AccountId>,
}

impl Default for ExtBuilder {
fn default() -> Self {
Self {}
Self { collective_members: vec![1, 2, 3] }
}
}

impl ExtBuilder {
fn set_collective_members(mut self, collective_members: Vec<AccountId>) -> Self {
self.collective_members = collective_members;
self
}

pub fn build(self) -> sp_io::TestExternalities {
let mut ext: sp_io::TestExternalities = GenesisConfig {
collective: pallet_collective::GenesisConfig {
members: vec![1, 2, 3],
members: self.collective_members,
phantom: Default::default(),
},
collective_majority: pallet_collective::GenesisConfig {
Expand Down Expand Up @@ -219,6 +227,17 @@ fn motions_basic_environment_works() {
});
}

#[test]
fn initialize_members_sorts_members() {
let unsorted_members = vec![3, 2, 4, 1];
let expected_members = vec![1, 2, 3, 4];
ExtBuilder::default()
.set_collective_members(unsorted_members)
.build_and_execute(|| {
assert_eq!(Collective::members(), expected_members);
});
}

#[test]
fn proposal_weight_limit_works() {
ExtBuilder::default().build_and_execute(|| {
Expand Down