Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,8 @@ parameter_types! {
pub const TermDuration: BlockNumber = 7 * DAYS;
pub const DesiredMembers: u32 = 13;
pub const DesiredRunnersUp: u32 = 7;
pub const MaxVoters: u32 = 10 * 1000;
pub const MaxCandidates: u32 = 1000;
pub const ElectionsPhragmenPalletId: LockIdentifier = *b"phrelect";
}

Expand All @@ -988,6 +990,8 @@ impl pallet_elections_phragmen::Config for Runtime {
type DesiredMembers = DesiredMembers;
type DesiredRunnersUp = DesiredRunnersUp;
type TermDuration = TermDuration;
type MaxVoters = MaxVoters;
type MaxCandidates = MaxCandidates;
type WeightInfo = pallet_elections_phragmen::weights::SubstrateWeight<Runtime>;
}

Expand Down
31 changes: 17 additions & 14 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,6 @@ pub mod migrations;
/// The maximum votes allowed per voter.
pub const MAXIMUM_VOTE: usize = 16;

// Some safe temp values to make the wasm execution sane while we still use this pallet.
#[cfg(test)]
pub(crate) const MAX_CANDIDATES: u32 = 100;
#[cfg(not(test))]
pub(crate) const MAX_CANDIDATES: u32 = 1000;

#[cfg(test)]
pub(crate) const MAX_VOTERS: u32 = 1000;
#[cfg(not(test))]
pub(crate) const MAX_VOTERS: u32 = 10 * 1000;

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
Expand Down Expand Up @@ -259,6 +248,14 @@ pub mod pallet {
#[pallet::constant]
type TermDuration: Get<Self::BlockNumber>;

/// Maximum number of candidated in this election
Comment thread
Szegoo marked this conversation as resolved.
Outdated
#[pallet::constant]
type MaxCandidates: Get<u32>;

/// Maximum number of voters to allow
Comment thread
Szegoo marked this conversation as resolved.
Outdated
#[pallet::constant]
type MaxVoters: Get<u32>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -395,9 +392,10 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

let max_candidate = <T as Config>::MaxCandidates::get();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let max_candidate = <T as Config>::MaxCandidates::get();

let actual_count = <Candidates<T>>::decode_len().unwrap_or(0) as u32;
ensure!(actual_count <= candidate_count, Error::<T>::InvalidWitnessData);
ensure!(actual_count <= MAX_CANDIDATES, Error::<T>::TooManyCandidates);
ensure!(actual_count <= max_candidate, Error::<T>::TooManyCandidates);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ensure!(actual_count <= max_candidate, Error::<T>::TooManyCandidates);
ensure!(actual_count <= <T as Config>::MaxCandidates::get(), Error::<T>::TooManyCandidates);


let index = Self::is_candidate(&who).err().ok_or(Error::<T>::DuplicatedCandidate)?;

Expand Down Expand Up @@ -913,10 +911,11 @@ impl<T: Config> Pallet<T> {

let mut num_edges: u32 = 0;

let max_voters = <T as Config>::MaxVoters::get() as usize;
// used for prime election.
let mut voters_and_stakes = Vec::new();
match Voting::<T>::iter().try_for_each(|(voter, Voter { stake, votes, .. })| {
if voters_and_stakes.len() < MAX_VOTERS as usize {
if voters_and_stakes.len() < max_voters {
voters_and_stakes.push((voter, stake, votes));
Ok(())
} else {
Expand All @@ -930,7 +929,7 @@ impl<T: Config> Pallet<T> {
"Failed to run election. Number of voters exceeded",
);
Self::deposit_event(Event::ElectionError);
return T::DbWeight::get().reads(3 + MAX_VOTERS as u64)
return T::DbWeight::get().reads(3 + max_voters as u64)
},
}

Expand Down Expand Up @@ -1266,6 +1265,8 @@ mod tests {

parameter_types! {
pub const ElectionsPhragmenPalletId: LockIdentifier = *b"phrelect";
pub const PhragmenMaxVoters: u32 = 1000;
pub const PhragménMaxCandidates: u32 = 10 * 1000;
}

impl Config for Test {
Expand All @@ -1284,6 +1285,8 @@ mod tests {
type LoserCandidate = ();
type KickedMember = ();
type WeightInfo = ();
type MaxVoters = PhragmenMaxVoters;
type MaxCandidates = PhragménMaxCandidates;
}

pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
Expand Down