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 all 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: 1 addition & 1 deletion frame/election-provider-multi-phase/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ frame_election_provider_support::generate_solution_type!(
VoterIndex = VoterIndex,
TargetIndex = TargetIndex,
Accuracy = PerU16,
MaxVoters = ConstU32::<20>
MaxVoters = ConstU32::<2_000>
>(16)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ pub(crate) fn generate(def: crate::SolutionDef) -> Result<TokenStream2> {
for<'r> FV: Fn(&'r A) -> Option<Self::VoterIndex>,
for<'r> FT: Fn(&'r A) -> Option<Self::TargetIndex>,
{
// Make sure that the voter bound is binding.
// `assignments.len()` actually represents the number of voters
if assignments.len() as u32 > <#max_voters as _feps::Get<u32>>::get() {
return Err(_feps::Error::TooManyVoters);
}
let mut #struct_name: #ident = Default::default();
for _feps::Assignment { who, distribution } in assignments {
match distribution.len() {
Expand All @@ -134,6 +139,7 @@ pub(crate) fn generate(def: crate::SolutionDef) -> Result<TokenStream2> {
}
}
};

Ok(#struct_name)
}

Expand Down
3 changes: 2 additions & 1 deletion frame/election-provider-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@
pub mod onchain;
pub mod traits;
use codec::{Decode, Encode};
use frame_support::{traits::Get, BoundedVec, RuntimeDebug};
use frame_support::{BoundedVec, RuntimeDebug};
use sp_runtime::traits::Bounded;
use sp_std::{fmt::Debug, prelude::*};

/// Re-export the solution generation macro.
pub use frame_election_provider_solution_type::generate_solution_type;
pub use frame_support::traits::Get;
/// Re-export some type as they are used in the interface.
pub use sp_arithmetic::PerThing;
pub use sp_npos_elections::{
Expand Down
2 changes: 1 addition & 1 deletion frame/election-provider-support/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ crate::generate_solution_type! {
VoterIndex = u32,
TargetIndex = u16,
Accuracy = TestAccuracy,
MaxVoters = frame_support::traits::ConstU32::<20>,
MaxVoters = frame_support::traits::ConstU32::<2_500>,
>(16)
}

Expand Down
27 changes: 27 additions & 0 deletions frame/election-provider-support/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ mod solution_type {
assert!(with_compact < without_compact);
}

#[test]
fn from_assignment_fail_too_many_voters() {
let rng = rand::rngs::SmallRng::seed_from_u64(0);

// This will produce 24 voters..
let (voters, assignments, candidates) = generate_random_votes(10, 25, rng);
let voter_index = make_voter_fn(&voters);
let target_index = make_target_fn(&candidates);

// Limit the voters to 20..
generate_solution_type!(
pub struct InnerTestSolution::<
VoterIndex = u32,
TargetIndex = u16,
Accuracy = TestAccuracy,
MaxVoters = frame_support::traits::ConstU32::<20>,
>(16)
);

// 24 > 20, so this should fail.
assert_eq!(
InnerTestSolution::from_assignment(&assignments, &voter_index, &target_index)
.unwrap_err(),
NposError::TooManyVoters,
);
}

#[test]
fn max_encoded_len_too_small() {
generate_solution_type!(
Expand Down
4 changes: 3 additions & 1 deletion primitives/npos-elections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ pub enum Error {
SolutionTargetOverflow,
/// One of the index functions returned none.
SolutionInvalidIndex,
/// One of the page indices was invalid
/// One of the page indices was invalid.
SolutionInvalidPageIndex,
/// An error occurred in some arithmetic operation.
ArithmeticError(&'static str),
/// The data provided to create support map was invalid.
InvalidSupportEdge,
/// The number of voters is bigger than the `MaxVoters` bound.
TooManyVoters,
}

/// A type which is used in the API of this crate as a numeric weight of a vote, most often the
Expand Down