Skip to content

Commit cb1a2a3

Browse files
authored
Fix Session Benchmarks (#7476)
* Always remove validator bfore creating new ones * remove comment * update tests and docs
1 parent b40c905 commit cb1a2a3

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/benchmarking.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,17 @@ fn add_slashing_spans<T: Trait>(who: &T::AccountId, spans: u32) {
4545
SlashingSpans::<T>::insert(who, slashing_spans);
4646
}
4747

48-
// This function generates one validator being nominated by n nominators, and returns the validator
49-
// stash account and the nominators' stash and controller. It also starts an era and creates pending payouts.
48+
// This function clears all existing validators and nominators from the set, and generates one new
49+
// validator being nominated by n nominators, and returns the validator stash account and the
50+
// nominators' stash and controller. It also starts an era and creates pending payouts.
5051
pub fn create_validator_with_nominators<T: Trait>(
5152
n: u32,
5253
upper_bound: u32,
5354
dead: bool,
5455
destination: RewardDestination<T::AccountId>
5556
) -> Result<(T::AccountId, Vec<(T::AccountId, T::AccountId)>), &'static str> {
57+
// Clean up any existing state.
58+
clear_validators_and_nominators::<T>();
5659
let mut points_total = 0;
5760
let mut points_individual = Vec::new();
5861

@@ -286,8 +289,6 @@ benchmarks! {
286289

287290
payout_stakers_dead_controller {
288291
let n in 1 .. T::MaxNominatorRewardedPerValidator::get() as u32;
289-
// Clean up existing validators
290-
Validators::<T>::remove_all();
291292
let (validator, nominators) = create_validator_with_nominators::<T>(
292293
n,
293294
T::MaxNominatorRewardedPerValidator::get() as u32,
@@ -321,8 +322,6 @@ benchmarks! {
321322

322323
payout_stakers_alive_staked {
323324
let n in 1 .. T::MaxNominatorRewardedPerValidator::get() as u32;
324-
// Clean up existing validators
325-
Validators::<T>::remove_all();
326325
let (validator, nominators) = create_validator_with_nominators::<T>(
327326
n,
328327
T::MaxNominatorRewardedPerValidator::get() as u32,
@@ -708,7 +707,7 @@ mod tests {
708707

709708
#[test]
710709
fn create_validators_with_nominators_for_era_works() {
711-
ExtBuilder::default().has_stakers(false).build().execute_with(|| {
710+
ExtBuilder::default().has_stakers(true).build().execute_with(|| {
712711
let v = 10;
713712
let n = 100;
714713

@@ -725,7 +724,7 @@ mod tests {
725724

726725
#[test]
727726
fn create_validator_with_nominators_works() {
728-
ExtBuilder::default().has_stakers(false).build().execute_with(|| {
727+
ExtBuilder::default().has_stakers(true).build().execute_with(|| {
729728
let n = 10;
730729

731730
let (validator_stash, nominators) = create_validator_with_nominators::<Test>(
@@ -749,7 +748,7 @@ mod tests {
749748

750749
#[test]
751750
fn add_slashing_spans_works() {
752-
ExtBuilder::default().has_stakers(false).build().execute_with(|| {
751+
ExtBuilder::default().has_stakers(true).build().execute_with(|| {
753752
let n = 10;
754753

755754
let (validator_stash, _nominators) = create_validator_with_nominators::<Test>(
@@ -780,7 +779,7 @@ mod tests {
780779

781780
#[test]
782781
fn test_payout_all() {
783-
ExtBuilder::default().has_stakers(false).build().execute_with(|| {
782+
ExtBuilder::default().has_stakers(true).build().execute_with(|| {
784783
let v = 10;
785784
let n = 100;
786785

@@ -799,7 +798,7 @@ mod tests {
799798

800799
#[test]
801800
fn test_benchmarks() {
802-
ExtBuilder::default().has_stakers(false).build().execute_with(|| {
801+
ExtBuilder::default().has_stakers(true).build().execute_with(|| {
803802
assert_ok!(test_benchmark_bond::<Test>());
804803
assert_ok!(test_benchmark_bond_extra::<Test>());
805804
assert_ok!(test_benchmark_unbond::<Test>());

src/testing_utils.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ use sp_npos_elections::*;
2828

2929
const SEED: u32 = 0;
3030

31+
/// This function removes all validators and nominators from storage.
32+
pub fn clear_validators_and_nominators<T: Trait>() {
33+
Validators::<T>::remove_all();
34+
Nominators::<T>::remove_all();
35+
}
36+
3137
/// Grab a funded user.
3238
pub fn create_funded_user<T: Trait>(
3339
string: &'static str,
@@ -97,6 +103,9 @@ pub fn create_validators<T: Trait>(
97103
/// This function generates validators and nominators who are randomly nominating
98104
/// `edge_per_nominator` random validators (until `to_nominate` if provided).
99105
///
106+
/// NOTE: This function will remove any existing validators or nominators to ensure
107+
/// we are working with a clean state.
108+
///
100109
/// Parameters:
101110
/// - `validators`: number of bonded validators
102111
/// - `nominators`: number of bonded nominators.
@@ -113,6 +122,8 @@ pub fn create_validators_with_nominators_for_era<T: Trait>(
113122
randomize_stake: bool,
114123
to_nominate: Option<u32>,
115124
) -> Result<Vec<<T::Lookup as StaticLookup>::Source>, &'static str> {
125+
clear_validators_and_nominators::<T>();
126+
116127
let mut validators_stash: Vec<<T::Lookup as StaticLookup>::Source>
117128
= Vec::with_capacity(validators as usize);
118129
let mut rng = ChaChaRng::from_seed(SEED.using_encoded(blake2_256));

0 commit comments

Comments
 (0)