Enforce MaxEncodedLen impl for NposSolution (#11103)

* Fail if `MaxVoters` too small

* Fixing benchmarking test, better naming of error

* reverting accidental change

* use fully qualified syntax
no need to interate to calculate len

* Fail directly if too many voters
This commit is contained in:
Georges
2022-03-31 13:54:44 +01:00
committed by GitHub
parent 7bf19fa9c9
commit 0e0d0a677a
6 changed files with 40 additions and 4 deletions
@@ -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!(