Skip to content

Commit

Permalink
collective pallet: sort genesis members and enforce max len constraint (
Browse files Browse the repository at this point in the history
paritytech#13988)

* insert members in sorted order

* improve variable name

* enforce genesis members length constraint
  • Loading branch information
liamaharon authored and nathanwhit committed Jul 19, 2023
1 parent 68e06ec commit e780197
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
6 changes: 6 additions & 0 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ pub mod pallet {
self.members.len(),
"Members cannot contain duplicate accounts."
);
assert!(
self.members.len() <= T::MaxMembers::get() as usize,
"Members length cannot exceed MaxMembers.",
);

Pallet::<T, I>::initialize_members(&self.members)
}
Expand Down Expand Up @@ -1107,6 +1111,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();
<Members<T, I>>::put(members);
}
}
Expand Down
40 changes: 36 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 Expand Up @@ -1424,6 +1443,19 @@ fn disapprove_proposal_works() {
})
}

#[should_panic(expected = "Members length cannot exceed MaxMembers.")]
#[test]
fn genesis_build_panics_with_too_many_members() {
let max_members: u32 = MaxMembers::get();
let too_many_members = (1..=max_members as u64 + 1).collect::<Vec<AccountId>>();
pallet_collective::GenesisConfig::<Test> {
members: too_many_members,
phantom: Default::default(),
}
.build_storage()
.unwrap();
}

#[test]
#[should_panic(expected = "Members cannot contain duplicate accounts.")]
fn genesis_build_panics_with_duplicate_members() {
Expand Down

0 comments on commit e780197

Please sign in to comment.