diff --git a/substrate/frame/staking/src/benchmarking.rs b/substrate/frame/staking/src/benchmarking.rs index d2f769b069..94a97debe4 100644 --- a/substrate/frame/staking/src/benchmarking.rs +++ b/substrate/frame/staking/src/benchmarking.rs @@ -45,14 +45,17 @@ fn add_slashing_spans(who: &T::AccountId, spans: u32) { SlashingSpans::::insert(who, slashing_spans); } -// This function generates one validator being nominated by n nominators, and returns the validator -// stash account and the nominators' stash and controller. It also starts an era and creates pending payouts. +// This function clears all existing validators and nominators from the set, and generates one new +// validator being nominated by n nominators, and returns the validator stash account and the +// nominators' stash and controller. It also starts an era and creates pending payouts. pub fn create_validator_with_nominators( n: u32, upper_bound: u32, dead: bool, destination: RewardDestination ) -> Result<(T::AccountId, Vec<(T::AccountId, T::AccountId)>), &'static str> { + // Clean up any existing state. + clear_validators_and_nominators::(); let mut points_total = 0; let mut points_individual = Vec::new(); @@ -286,8 +289,6 @@ benchmarks! { payout_stakers_dead_controller { let n in 1 .. T::MaxNominatorRewardedPerValidator::get() as u32; - // Clean up existing validators - Validators::::remove_all(); let (validator, nominators) = create_validator_with_nominators::( n, T::MaxNominatorRewardedPerValidator::get() as u32, @@ -321,8 +322,6 @@ benchmarks! { payout_stakers_alive_staked { let n in 1 .. T::MaxNominatorRewardedPerValidator::get() as u32; - // Clean up existing validators - Validators::::remove_all(); let (validator, nominators) = create_validator_with_nominators::( n, T::MaxNominatorRewardedPerValidator::get() as u32, @@ -708,7 +707,7 @@ mod tests { #[test] fn create_validators_with_nominators_for_era_works() { - ExtBuilder::default().has_stakers(false).build().execute_with(|| { + ExtBuilder::default().has_stakers(true).build().execute_with(|| { let v = 10; let n = 100; @@ -725,7 +724,7 @@ mod tests { #[test] fn create_validator_with_nominators_works() { - ExtBuilder::default().has_stakers(false).build().execute_with(|| { + ExtBuilder::default().has_stakers(true).build().execute_with(|| { let n = 10; let (validator_stash, nominators) = create_validator_with_nominators::( @@ -749,7 +748,7 @@ mod tests { #[test] fn add_slashing_spans_works() { - ExtBuilder::default().has_stakers(false).build().execute_with(|| { + ExtBuilder::default().has_stakers(true).build().execute_with(|| { let n = 10; let (validator_stash, _nominators) = create_validator_with_nominators::( @@ -780,7 +779,7 @@ mod tests { #[test] fn test_payout_all() { - ExtBuilder::default().has_stakers(false).build().execute_with(|| { + ExtBuilder::default().has_stakers(true).build().execute_with(|| { let v = 10; let n = 100; @@ -799,7 +798,7 @@ mod tests { #[test] fn test_benchmarks() { - ExtBuilder::default().has_stakers(false).build().execute_with(|| { + ExtBuilder::default().has_stakers(true).build().execute_with(|| { assert_ok!(test_benchmark_bond::()); assert_ok!(test_benchmark_bond_extra::()); assert_ok!(test_benchmark_unbond::()); diff --git a/substrate/frame/staking/src/testing_utils.rs b/substrate/frame/staking/src/testing_utils.rs index 57ad95bcf5..25cfffeac2 100644 --- a/substrate/frame/staking/src/testing_utils.rs +++ b/substrate/frame/staking/src/testing_utils.rs @@ -28,6 +28,12 @@ use sp_npos_elections::*; const SEED: u32 = 0; +/// This function removes all validators and nominators from storage. +pub fn clear_validators_and_nominators() { + Validators::::remove_all(); + Nominators::::remove_all(); +} + /// Grab a funded user. pub fn create_funded_user( string: &'static str, @@ -97,6 +103,9 @@ pub fn create_validators( /// This function generates validators and nominators who are randomly nominating /// `edge_per_nominator` random validators (until `to_nominate` if provided). /// +/// NOTE: This function will remove any existing validators or nominators to ensure +/// we are working with a clean state. +/// /// Parameters: /// - `validators`: number of bonded validators /// - `nominators`: number of bonded nominators. @@ -113,6 +122,8 @@ pub fn create_validators_with_nominators_for_era( randomize_stake: bool, to_nominate: Option, ) -> Result::Source>, &'static str> { + clear_validators_and_nominators::(); + let mut validators_stash: Vec<::Source> = Vec::with_capacity(validators as usize); let mut rng = ChaChaRng::from_seed(SEED.using_encoded(blake2_256));