From 189ee2634f689605fcd174c2dbeb50538b892daf Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sat, 7 Aug 2021 16:44:23 +0200 Subject: [PATCH 1/4] general cleanup of staking tests --- frame/staking/src/mock.rs | 167 +++--- frame/staking/src/pallet/mod.rs | 8 +- frame/staking/src/tests.rs | 936 +++++++++++++++----------------- 3 files changed, 547 insertions(+), 564 deletions(-) diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 9d50a43754e76..58cf99eb2d774 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -285,33 +285,35 @@ pub(crate) type StakingCall = crate::Call; pub(crate) type TestRuntimeCall = ::Call; pub struct ExtBuilder { - validator_pool: bool, nominate: bool, validator_count: u32, minimum_validator_count: u32, - fair: bool, - num_validators: Option, invulnerables: Vec, has_stakers: bool, initialize_first_session: bool, min_nominator_bond: Balance, min_validator_bond: Balance, + balance_factor: Balance, + status: BTreeMap>, + stakes: BTreeMap, + stakers: Vec<(AccountId, AccountId, Balance, StakerStatus)>, } impl Default for ExtBuilder { fn default() -> Self { Self { - validator_pool: false, nominate: true, validator_count: 2, minimum_validator_count: 0, - fair: true, - num_validators: None, + balance_factor: 1, invulnerables: vec![], has_stakers: true, initialize_first_session: true, min_nominator_bond: ExistentialDeposit::get(), min_validator_bond: ExistentialDeposit::get(), + status: Default::default(), + stakes: Default::default(), + stakers: Default::default(), } } } @@ -321,10 +323,6 @@ impl ExtBuilder { EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = existential_deposit); self } - pub fn validator_pool(mut self, validator_pool: bool) -> Self { - self.validator_pool = validator_pool; - self - } pub fn nominate(mut self, nominate: bool) -> Self { self.nominate = nominate; self @@ -341,14 +339,6 @@ impl ExtBuilder { SLASH_DEFER_DURATION.with(|v| *v.borrow_mut() = eras); self } - pub fn fair(mut self, is_fair: bool) -> Self { - self.fair = is_fair; - self - } - pub fn num_validators(mut self, num_validators: u32) -> Self { - self.num_validators = Some(num_validators); - self - } pub fn invulnerables(mut self, invulnerables: Vec) -> Self { self.invulnerables = invulnerables; self @@ -381,41 +371,60 @@ impl ExtBuilder { self.min_validator_bond = amount; self } + pub fn set_status(mut self, who: AccountId, status: StakerStatus) -> Self { + self.status.insert(who, status); + self + } + pub fn set_stake(mut self, who: AccountId, stake: Balance) -> Self { + self.stakes.insert(who, stake); + self + } + pub fn add_staker( + mut self, + stash: AccountId, + ctrl: AccountId, + stake: Balance, + status: StakerStatus, + ) -> Self { + self.stakers.push((stash, ctrl, stake, status)); + self + } + pub fn balance_factor(mut self, factor: Balance) -> Self { + self.balance_factor = factor; + self + } fn build(self) -> sp_io::TestExternalities { sp_tracing::try_init_simple(); let mut storage = frame_system::GenesisConfig::default().build_storage::().unwrap(); - let balance_factor = if ExistentialDeposit::get() > 1 { 256 } else { 1 }; - - let num_validators = self.num_validators.unwrap_or(self.validator_count); - // Check that the number of validators is sensible. - assert!(num_validators <= 8); - let validators = - (0..num_validators).map(|x| ((x + 1) * 10 + 1) as AccountId).collect::>(); let _ = pallet_balances::GenesisConfig:: { balances: vec![ - (1, 10 * balance_factor), - (2, 20 * balance_factor), - (3, 300 * balance_factor), - (4, 400 * balance_factor), - (10, balance_factor), - (11, balance_factor * 1000), - (20, balance_factor), - (21, balance_factor * 2000), - (30, balance_factor), - (31, balance_factor * 2000), - (40, balance_factor), - (41, balance_factor * 2000), - (50, balance_factor), - (51, balance_factor * 2000), - (60, balance_factor), - (61, balance_factor * 2000), - (70, balance_factor), - (71, balance_factor * 2000), - (80, balance_factor), - (81, balance_factor * 2000), - (100, 2000 * balance_factor), - (101, 2000 * balance_factor), + (1, 10 * self.balance_factor), + (2, 20 * self.balance_factor), + (3, 300 * self.balance_factor), + (4, 400 * self.balance_factor), + // controllers + (10, self.balance_factor), + (20, self.balance_factor), + (30, self.balance_factor), + (40, self.balance_factor), + (50, self.balance_factor), + // stashes + (11, self.balance_factor * 1000), + (21, self.balance_factor * 2000), + (31, self.balance_factor * 2000), + (41, self.balance_factor * 2000), + (51, self.balance_factor * 2000), + // optional nominator + (100, self.balance_factor * 2000), + (101, self.balance_factor * 2000), + // aux accounts + (60, self.balance_factor), + (61, self.balance_factor * 2000), + (70, self.balance_factor), + (71, self.balance_factor * 2000), + (80, self.balance_factor), + (81, self.balance_factor * 2000), // This allows us to have a total_payout different from 0. (999, 1_000_000_000_000), ], @@ -424,24 +433,45 @@ impl ExtBuilder { let mut stakers = vec![]; if self.has_stakers { - let stake_21 = if self.fair { 1000 } else { 2000 }; - let stake_31 = if self.validator_pool { balance_factor * 1000 } else { 1 }; - let status_41 = if self.validator_pool { - StakerStatus::::Validator - } else { - StakerStatus::::Idle - }; - let nominated = if self.nominate { vec![11, 21] } else { vec![] }; stakers = vec![ - // (stash, controller, staked_amount, status) - (11, 10, balance_factor * 1000, StakerStatus::::Validator), - (21, 20, stake_21, StakerStatus::::Validator), - (31, 30, stake_31, StakerStatus::::Validator), - (41, 40, balance_factor * 1000, status_41), - // nominator - (101, 100, balance_factor * 500, StakerStatus::::Nominator(nominated)), + // (stash, ctrl, stake, status) + // these two will be elected in the default test where we elect 2. + (11, 10, self.balance_factor * 1000, StakerStatus::::Validator), + (21, 20, self.balance_factor * 1000, StakerStatus::::Validator), + // a loser validator + (31, 30, self.balance_factor * 500, StakerStatus::::Validator), + // an idle validator + (41, 40, self.balance_factor * 1000, StakerStatus::::Idle), ]; + // optionally add a nominator + if self.nominate { + stakers.push(( + 101, + 100, + self.balance_factor * 500, + StakerStatus::::Nominator(vec![11, 21]), + )) + } + // replace any of the status if needed. + self.status.into_iter().for_each(|(stash, status)| { + if let Some((_, _, _, ref mut prev_status)) = + stakers.iter_mut().find(|s| s.0 == stash) + { + *prev_status = status + } + }); + // replaced any of the stakes if needed. + self.stakes.into_iter().for_each(|(stash, stake)| { + if let Some((_, _, ref mut prev_stake, _)) = + stakers.iter_mut().find(|s| s.0 == stash) + { + *prev_stake = stake + } + }); + // extend stakers if needed. + stakers.extend(self.stakers) } + let _ = staking::GenesisConfig:: { stakers, validator_count: self.validator_count, @@ -455,10 +485,15 @@ impl ExtBuilder { .assimilate_storage(&mut storage); let _ = pallet_session::GenesisConfig:: { - keys: validators - .iter() - .map(|x| (*x, *x, SessionKeys { other: UintAuthorityId(*x as u64) })) - .collect(), + keys: if self.has_stakers { + // genesis election will overwrite this, no worries. + Default::default() + } else { + // set some dummy validators in genesis. + (0..self.validator_count as u64) + .map(|x| (x, x, SessionKeys { other: UintAuthorityId(x as u64) })) + .collect() + }, } .assimilate_storage(&mut storage); diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 444768dbdccfa..e56038e10dc49 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -497,13 +497,13 @@ pub mod pallet { T::Currency::free_balance(&stash) >= balance, "Stash does not have enough balance to bond." ); - let _ = >::bond( + frame_support::assert_ok!(>::bond( T::Origin::from(Some(stash.clone()).into()), T::Lookup::unlookup(controller.clone()), balance, RewardDestination::Staked, - ); - let _ = match status { + )); + frame_support::assert_ok!(match status { StakerStatus::Validator => >::validate( T::Origin::from(Some(controller.clone()).into()), Default::default(), @@ -513,7 +513,7 @@ pub mod pallet { votes.iter().map(|l| T::Lookup::unlookup(l.clone())).collect(), ), _ => Ok(()), - }; + }); } } } diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 3cb7a74e8982b..1de36ad74f199 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -324,77 +324,71 @@ fn rewards_should_work() { #[test] fn staking_should_work() { - ExtBuilder::default() - .nominate(false) - .fair(false) // to give 20 more staked value - .build_and_execute(|| { - // remember + compare this along with the test. - assert_eq_uvec!(validator_controllers(), vec![20, 10]); + ExtBuilder::default().nominate(false).build_and_execute(|| { + // remember + compare this along with the test. + assert_eq_uvec!(validator_controllers(), vec![20, 10]); - // put some money in account that we'll use. - for i in 1..5 { - let _ = Balances::make_free_balance_be(&i, 2000); - } + // put some money in account that we'll use. + for i in 1..5 { + let _ = Balances::make_free_balance_be(&i, 2000); + } - // --- Block 2: - start_session(2); - // add a new candidate for being a validator. account 3 controlled by 4. - assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); - assert_ok!(Staking::validate(Origin::signed(4), ValidatorPrefs::default())); + // --- Block 2: + start_session(2); + // add a new candidate for being a validator. account 3 controlled by 4. + assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); + assert_ok!(Staking::validate(Origin::signed(4), ValidatorPrefs::default())); - // No effects will be seen so far. - assert_eq_uvec!(validator_controllers(), vec![20, 10]); + // No effects will be seen so far. + assert_eq_uvec!(validator_controllers(), vec![20, 10]); - // --- Block 3: - start_session(3); + // --- Block 3: + start_session(3); - // No effects will be seen so far. Era has not been yet triggered. - assert_eq_uvec!(validator_controllers(), vec![20, 10]); + // No effects will be seen so far. Era has not been yet triggered. + assert_eq_uvec!(validator_controllers(), vec![20, 10]); - // --- Block 4: the validators will now be queued. - start_session(4); - assert_eq!(Staking::active_era().unwrap().index, 1); + // --- Block 4: the validators will now be queued. + start_session(4); + assert_eq!(Staking::active_era().unwrap().index, 1); - // --- Block 5: the validators are still in queue. - start_session(5); + // --- Block 5: the validators are still in queue. + start_session(5); - // --- Block 6: the validators will now be changed. - start_session(6); + // --- Block 6: the validators will now be changed. + start_session(6); - assert_eq_uvec!(validator_controllers(), vec![20, 4]); - // --- Block 6: Unstake 4 as a validator, freeing up the balance stashed in 3 - // 4 will chill - Staking::chill(Origin::signed(4)).unwrap(); + assert_eq_uvec!(validator_controllers(), vec![20, 4]); + // --- Block 6: Unstake 4 as a validator, freeing up the balance stashed in 3 + // 4 will chill + Staking::chill(Origin::signed(4)).unwrap(); - // --- Block 7: nothing. 4 is still there. - start_session(7); - assert_eq_uvec!(validator_controllers(), vec![20, 4]); + // --- Block 7: nothing. 4 is still there. + start_session(7); + assert_eq_uvec!(validator_controllers(), vec![20, 4]); - // --- Block 8: - start_session(8); + // --- Block 8: + start_session(8); - // --- Block 9: 4 will not be a validator. - start_session(9); - assert_eq_uvec!(validator_controllers(), vec![20, 10]); + // --- Block 9: 4 will not be a validator. + start_session(9); + assert_eq_uvec!(validator_controllers(), vec![20, 10]); - // Note: the stashed value of 4 is still lock - assert_eq!( - Staking::ledger(&4), - Some(StakingLedger { - stash: 3, - total: 1500, - active: 1500, - unlocking: vec![], - claimed_rewards: vec![0], - }) - ); - // e.g. it cannot reserve more than 500 that it has free from the total 2000 - assert_noop!( - Balances::reserve(&3, 501), - BalancesError::::LiquidityRestrictions - ); - assert_ok!(Balances::reserve(&3, 409)); - }); + // Note: the stashed value of 4 is still lock + assert_eq!( + Staking::ledger(&4), + Some(StakingLedger { + stash: 3, + total: 1500, + active: 1500, + unlocking: vec![], + claimed_rewards: vec![0], + }) + ); + // e.g. it cannot reserve more than 500 that it has free from the total 2000 + assert_noop!(Balances::reserve(&3, 501), BalancesError::::LiquidityRestrictions); + assert_ok!(Balances::reserve(&3, 409)); + }); } #[test] @@ -403,7 +397,6 @@ fn blocking_and_kicking_works() { .minimum_validator_count(1) .validator_count(4) .nominate(true) - .num_validators(3) .build_and_execute(|| { // block validator 10/11 assert_ok!(Staking::validate( @@ -432,7 +425,6 @@ fn less_than_needed_candidates_works() { .minimum_validator_count(1) .validator_count(4) .nominate(false) - .num_validators(3) .build_and_execute(|| { assert_eq!(Staking::validator_count(), 4); assert_eq!(Staking::minimum_validator_count(), 1); @@ -455,8 +447,7 @@ fn no_candidate_emergency_condition() { ExtBuilder::default() .minimum_validator_count(1) .validator_count(15) - .num_validators(4) - .validator_pool(true) + .set_status(41, StakerStatus::Validator) .nominate(false) .build_and_execute(|| { // initial validators @@ -482,7 +473,8 @@ fn no_candidate_emergency_condition() { // Go to far further session to see if validator have changed mock::run_to_block(100); - // Previous ones are elected. chill is not effective in active era (as era hasn't changed) + // Previous ones are elected. chill is not effective in active era (as era hasn't + // changed) assert_eq_uvec!(validator_controllers(), vec![10, 20, 30, 40]); // The chill is still pending. assert!(!::Validators::contains_key(11)); @@ -495,10 +487,10 @@ fn no_candidate_emergency_condition() { fn nominating_and_rewards_should_work() { ExtBuilder::default() .nominate(false) - .validator_pool(true) + .set_status(41, StakerStatus::Validator) .build_and_execute(|| { // initial validators -- everyone is actually even. - assert_eq_uvec!(validator_controllers(), vec![40, 30]); + assert_eq_uvec!(validator_controllers(), vec![40, 20]); // Set payee to controller assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); @@ -513,29 +505,29 @@ fn nominating_and_rewards_should_work() { } // bond two account pairs and state interest in nomination. - // 2 will nominate for 10, 20, 30 assert_ok!(Staking::bond(Origin::signed(1), 2, 1000, RewardDestination::Controller)); assert_ok!(Staking::nominate(Origin::signed(2), vec![11, 21, 31])); - // 4 will nominate for 10, 20, 40 + assert_ok!(Staking::bond(Origin::signed(3), 4, 1000, RewardDestination::Controller)); assert_ok!(Staking::nominate(Origin::signed(4), vec![11, 21, 41])); // the total reward for era 0 let total_payout_0 = current_total_payout_for_duration(reward_time_per_era()); >::reward_by_ids(vec![(41, 1)]); - >::reward_by_ids(vec![(31, 1)]); + >::reward_by_ids(vec![(21, 1)]); mock::start_active_era(1); // 10 and 20 have more votes, they will be chosen. assert_eq_uvec!(validator_controllers(), vec![20, 10]); - // OLD validators must have already received some rewards. + // old validators must have already received some rewards. + let initial_balance_40 = Balances::total_balance(&40); + let mut initial_balance_20 = Balances::total_balance(&20); mock::make_all_reward_payment(0); - assert_eq!(Balances::total_balance(&40), 1 + total_payout_0 / 2); - assert_eq!(Balances::total_balance(&30), 1 + total_payout_0 / 2); - - // ------ check the staked value of all parties. + assert_eq!(Balances::total_balance(&40), initial_balance_40 + total_payout_0 / 2); + assert_eq!(Balances::total_balance(&20), initial_balance_20 + total_payout_0 / 2); + initial_balance_20 = Balances::total_balance(&20); // 30 and 40 are not chosen anymore assert_eq!( @@ -573,8 +565,8 @@ fn nominating_and_rewards_should_work() { mock::start_active_era(2); - // nothing else will happen, era ends and rewards are paid again, - // it is expected that nominators will also be paid. See below + // nothing else will happen, era ends and rewards are paid again, it is expected that + // nominators will also be paid. See below mock::make_all_reward_payment(1); let payout_for_10 = total_payout_1 / 3; @@ -598,10 +590,10 @@ fn nominating_and_rewards_should_work() { initial_balance + 5 * payout_for_10 / 9, 2, ); - // Validator 20: got 1200 / 2200 external stake => 12/22 =? 6/11 => Validator's share = 5/11 + // Validator 20: got `1200/2200` external stake => 12/22 =? 6/11 => Validator's share = 5/11 assert_eq_error_rate!( Balances::total_balance(&20), - initial_balance + 5 * payout_for_20 / 11, + initial_balance_20 + 5 * payout_for_20 / 11, 2, ); }); @@ -885,7 +877,7 @@ fn cannot_transfer_staked_balance_2() { // Tests that a stash account cannot transfer funds // Same test as above but with 20, and more accurate. // 21 has 2000 free balance but 1000 at stake - ExtBuilder::default().nominate(false).fair(true).build_and_execute(|| { + ExtBuilder::default().nominate(false).build_and_execute(|| { // Confirm account 21 is stashed assert_eq!(Staking::bonded(&21), Some(20)); // Confirm account 21 has some free balance @@ -1520,63 +1512,64 @@ fn rebond_is_fifo() { #[test] fn reward_to_stake_works() { - ExtBuilder::default().nominate(false).fair(false).build_and_execute(|| { - // Confirm validator count is 2 - assert_eq!(Staking::validator_count(), 2); - // Confirm account 10 and 20 are validators - assert!(>::contains_key(&11) && >::contains_key(&21)); - - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000); - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 2000); + ExtBuilder::default() + .nominate(false) + .set_status(31, StakerStatus::Idle) + .set_status(41, StakerStatus::Idle) + .set_stake(21, 2000) + .build_and_execute(|| { + assert_eq!(Staking::validator_count(), 2); + // Confirm account 10 and 20 are validators + assert!(>::contains_key(&11) && >::contains_key(&21)); - // Give the man some money. - let _ = Balances::make_free_balance_be(&10, 1000); - let _ = Balances::make_free_balance_be(&20, 1000); + assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000); + assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 2000); - // Bypass logic and change current exposure - ErasStakers::::insert(0, 21, Exposure { total: 69, own: 69, others: vec![] }); + // Give the man some money. + let _ = Balances::make_free_balance_be(&10, 1000); + let _ = Balances::make_free_balance_be(&20, 1000); - // Now lets lower account 20 stake - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 69); - >::insert( - &20, - StakingLedger { - stash: 21, - total: 69, - active: 69, - unlocking: vec![], - claimed_rewards: vec![], - }, - ); + // Bypass logic and change current exposure + ErasStakers::::insert(0, 21, Exposure { total: 69, own: 69, others: vec![] }); + >::insert( + &20, + StakingLedger { + stash: 21, + total: 69, + active: 69, + unlocking: vec![], + claimed_rewards: vec![], + }, + ); - // Compute total payout now for whole duration as other parameter won't change - let total_payout_0 = current_total_payout_for_duration(reward_time_per_era()); - >::reward_by_ids(vec![(11, 1)]); - >::reward_by_ids(vec![(21, 1)]); + // Compute total payout now for whole duration as other parameter won't change + let total_payout_0 = current_total_payout_for_duration(reward_time_per_era()); + >::reward_by_ids(vec![(11, 1)]); + >::reward_by_ids(vec![(21, 1)]); - // New era --> rewards are paid --> stakes are changed - mock::start_active_era(1); - mock::make_all_reward_payment(0); + // New era --> rewards are paid --> stakes are changed + mock::start_active_era(1); + mock::make_all_reward_payment(0); - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000); - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 69); + assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000); + assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 69); - let _11_balance = Balances::free_balance(&11); - assert_eq!(_11_balance, 1000 + total_payout_0 / 2); + let _11_balance = Balances::free_balance(&11); + assert_eq!(_11_balance, 1000 + total_payout_0 / 2); - // Trigger another new era as the info are frozen before the era start. - mock::start_active_era(2); + // Trigger another new era as the info are frozen before the era start. + mock::start_active_era(2); - // -- new infos - assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, - 1000 + total_payout_0 / 2 - ); - assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, - 69 + total_payout_0 / 2 - ); - }); + // -- new infos + assert_eq!( + Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, + 1000 + total_payout_0 / 2 + ); + assert_eq!( + Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, + 69 + total_payout_0 / 2 + ); + }); } #[test] @@ -1585,17 +1578,15 @@ fn on_free_balance_zero_stash_removes_validator() { // Tests that storage items are untouched when controller is empty ExtBuilder::default() .existential_deposit(10) - .min_nominator_bond(10) - .min_validator_bond(10) + .balance_factor(10) .build_and_execute(|| { // Check the balance of the validator account - assert_eq!(Balances::free_balance(10), 256); + assert_eq!(Balances::free_balance(10), 10); // Check the balance of the stash account - assert_eq!(Balances::free_balance(11), 256000); + assert_eq!(Balances::free_balance(11), 10 * 1000); // Check these two accounts are bonded assert_eq!(Staking::bonded(&11), Some(10)); - // Set some storage items which we expect to be cleaned up // Set payee information assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Stash)); @@ -1609,7 +1600,7 @@ fn on_free_balance_zero_stash_removes_validator() { let _ = Balances::slash(&10, Balance::max_value()); // Check the balance of the stash account has not been touched - assert_eq!(Balances::free_balance(11), 256000); + assert_eq!(Balances::free_balance(11), 10 * 1000); // Check these two accounts are still bonded assert_eq!(Staking::bonded(&11), Some(10)); @@ -1642,17 +1633,16 @@ fn on_free_balance_zero_stash_removes_nominator() { // Tests that storage items are untouched when controller is empty ExtBuilder::default() .existential_deposit(10) - .min_nominator_bond(10) - .min_validator_bond(10) + .balance_factor(10) .build_and_execute(|| { // Make 10 a nominator assert_ok!(Staking::nominate(Origin::signed(10), vec![20])); // Check that account 10 is a nominator assert!(>::contains_key(11)); // Check the balance of the nominator account - assert_eq!(Balances::free_balance(10), 256); + assert_eq!(Balances::free_balance(10), 10); // Check the balance of the stash account - assert_eq!(Balances::free_balance(11), 256000); + assert_eq!(Balances::free_balance(11), 10_000); // Set payee information assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Stash)); @@ -1669,7 +1659,7 @@ fn on_free_balance_zero_stash_removes_nominator() { assert_eq!(Balances::total_balance(&10), 0); // Check the balance of the stash account has not been touched - assert_eq!(Balances::free_balance(11), 256000); + assert_eq!(Balances::free_balance(11), 10_000); // Check these two accounts are still bonded assert_eq!(Staking::bonded(&11), Some(10)); @@ -1744,32 +1734,33 @@ fn switching_roles() { } #[test] -fn wrong_vote_is_null() { +fn wrong_vote_is_moot() { ExtBuilder::default() - .nominate(false) - .validator_pool(true) + .add_staker( + 61, + 60, + 500, + StakerStatus::Nominator(vec![ + 11, 21, // good votes + 1, 2, 15, 1000, 25, // crap votes. No effect. + ]), + ) .build_and_execute(|| { - assert_eq_uvec!(validator_controllers(), vec![40, 30]); - - // put some money in account that we'll use. - for i in 1..3 { - let _ = Balances::deposit_creating(&i, 5000); - } - - // add 1 nominators - assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::default())); - assert_ok!(Staking::nominate( - Origin::signed(2), - vec![ - 11, 21, // good votes - 1, 2, 15, 1000, 25 // crap votes. No effect. - ] - )); - - // new block + // the genesis validators already reflect the above vote, nonetheless start a new era. mock::start_active_era(1); + // new validators assert_eq_uvec!(validator_controllers(), vec![20, 10]); + + // our new voter is taken into account + assert!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11) + .others + .iter() + .any(|i| i.who == 61)); + assert!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21) + .others + .iter() + .any(|i| i.who == 61)); }); } @@ -1780,8 +1771,7 @@ fn bond_with_no_staked_value() { ExtBuilder::default() .validator_count(3) .existential_deposit(5) - .min_nominator_bond(5) - .min_validator_bond(5) + .balance_factor(5) .nominate(false) .minimum_validator_count(1) .build_and_execute(|| { @@ -1891,11 +1881,8 @@ fn bond_with_duplicate_vote_should_be_ignored_by_election_provider() { .validator_count(2) .nominate(false) .minimum_validator_count(1) + .set_stake(31, 1000) .build_and_execute(|| { - // disable the nominator - assert_ok!(Staking::chill(Origin::signed(100))); - // make stakes equal. - assert_ok!(Staking::bond_extra(Origin::signed(31), 999)); // ensure all have equal stake. assert_eq!( >::iter() @@ -1933,23 +1920,21 @@ fn bond_with_duplicate_vote_should_be_ignored_by_election_provider() { #[test] fn bond_with_duplicate_vote_should_be_ignored_by_election_provider_elected() { - // same as above but ensures that even when the duple is being elected, everything is sane. + // same as above but ensures that even when the dupe is being elected, everything is sane. ExtBuilder::default() .validator_count(2) .nominate(false) + .set_stake(31, 1000) .minimum_validator_count(1) .build_and_execute(|| { - // disable the nominator - assert_ok!(Staking::chill(Origin::signed(100))); - // 31/30 will have less stake - assert_ok!(Staking::bond_extra(Origin::signed(31), 99)); // ensure all have equal stake. assert_eq!( >::iter() .map(|(v, _)| (v, Staking::ledger(v - 1).unwrap().total)) .collect::>(), - vec![(31, 100), (21, 1000), (11, 1000)], + vec![(31, 1000), (21, 1000), (11, 1000)], ); + // no nominators shall exist. assert!(>::iter().map(|(n, _)| n).collect::>().is_empty()); @@ -1970,8 +1955,8 @@ fn bond_with_duplicate_vote_should_be_ignored_by_election_provider_elected() { assert_eq!( supports, vec![ - (11, Support { total: 1500, voters: vec![(11, 1000), (1, 500)] }), - (21, Support { total: 2500, voters: vec![(21, 1000), (3, 1000), (1, 500)] }) + (21, Support { total: 1800, voters: vec![(21, 1000), (3, 400), (1, 400)] }), + (31, Support { total: 2200, voters: vec![(31, 1000), (3, 600), (1, 600)] }) ], ); }); @@ -1979,19 +1964,14 @@ fn bond_with_duplicate_vote_should_be_ignored_by_election_provider_elected() { #[test] fn new_era_elects_correct_number_of_validators() { - ExtBuilder::default() - .nominate(true) - .validator_pool(true) - .fair(true) - .validator_count(1) - .build_and_execute(|| { - assert_eq!(Staking::validator_count(), 1); - assert_eq!(validator_controllers().len(), 1); + ExtBuilder::default().nominate(true).validator_count(1).build_and_execute(|| { + assert_eq!(Staking::validator_count(), 1); + assert_eq!(validator_controllers().len(), 1); - Session::on_initialize(System::block_number()); + Session::on_initialize(System::block_number()); - assert_eq!(validator_controllers().len(), 1); - }) + assert_eq!(validator_controllers().len(), 1); + }) } #[test] @@ -2488,10 +2468,9 @@ fn garbage_collection_after_slashing() { // ensures that `SlashingSpans` and `SpanSlash` of an account is removed after reaping. ExtBuilder::default() .existential_deposit(2) - .min_nominator_bond(2) - .min_validator_bond(2) + .balance_factor(2) .build_and_execute(|| { - assert_eq!(Balances::free_balance(11), 256_000); + assert_eq!(Balances::free_balance(11), 2000); on_offence_now( &[OffenceDetails { @@ -2501,12 +2480,9 @@ fn garbage_collection_after_slashing() { &[Perbill::from_percent(10)], ); - assert_eq!(Balances::free_balance(11), 256_000 - 25_600); + assert_eq!(Balances::free_balance(11), 2000 - 200); assert!(::SlashingSpans::get(&11).is_some()); - assert_eq!( - ::SpanSlash::get(&(11, 0)).amount_slashed(), - &25_600 - ); + assert_eq!(::SpanSlash::get(&(11, 0)).amount_slashed(), &200); on_offence_now( &[OffenceDetails { @@ -3698,19 +3674,15 @@ fn session_buffering_no_offset() { fn cannot_rebond_to_lower_than_ed() { ExtBuilder::default() .existential_deposit(10) - .min_nominator_bond(10) - .min_validator_bond(10) + .balance_factor(10) .build_and_execute(|| { - // stash must have more balance than bonded for this to work. - assert_eq!(Balances::free_balance(&21), 512_000); - // initial stuff. assert_eq!( Staking::ledger(&20).unwrap(), StakingLedger { stash: 21, - total: 1000, - active: 1000, + total: 10 * 1000, + active: 10 * 1000, unlocking: vec![], claimed_rewards: vec![] } @@ -3718,14 +3690,14 @@ fn cannot_rebond_to_lower_than_ed() { // unbond all of it. must be chilled first. assert_ok!(Staking::chill(Origin::signed(20))); - assert_ok!(Staking::unbond(Origin::signed(20), 1000)); + assert_ok!(Staking::unbond(Origin::signed(20), 10 * 1000)); assert_eq!( Staking::ledger(&20).unwrap(), StakingLedger { stash: 21, - total: 1000, + total: 10 * 1000, active: 0, - unlocking: vec![UnlockChunk { value: 1000, era: 3 }], + unlocking: vec![UnlockChunk { value: 10 * 1000, era: 3 }], claimed_rewards: vec![] } ); @@ -3739,19 +3711,15 @@ fn cannot_rebond_to_lower_than_ed() { fn cannot_bond_extra_to_lower_than_ed() { ExtBuilder::default() .existential_deposit(10) - .min_nominator_bond(10) - .min_validator_bond(10) + .balance_factor(10) .build_and_execute(|| { - // stash must have more balance than bonded for this to work. - assert_eq!(Balances::free_balance(&21), 512_000); - // initial stuff. assert_eq!( Staking::ledger(&20).unwrap(), StakingLedger { stash: 21, - total: 1000, - active: 1000, + total: 10 * 1000, + active: 10 * 1000, unlocking: vec![], claimed_rewards: vec![] } @@ -3759,14 +3727,14 @@ fn cannot_bond_extra_to_lower_than_ed() { // unbond all of it. must be chilled first. assert_ok!(Staking::chill(Origin::signed(20))); - assert_ok!(Staking::unbond(Origin::signed(20), 1000)); + assert_ok!(Staking::unbond(Origin::signed(20), 10 * 1000)); assert_eq!( Staking::ledger(&20).unwrap(), StakingLedger { stash: 21, - total: 1000, + total: 10 * 1000, active: 0, - unlocking: vec![UnlockChunk { value: 1000, era: 3 }], + unlocking: vec![UnlockChunk { value: 10 * 1000, era: 3 }], claimed_rewards: vec![] } ); @@ -3784,27 +3752,26 @@ fn do_not_die_when_active_is_ed() { let ed = 10; ExtBuilder::default() .existential_deposit(ed) - .min_nominator_bond(ed) - .min_validator_bond(ed) + .balance_factor(ed) .build_and_execute(|| { - // initial stuff. + // given assert_eq!( Staking::ledger(&20).unwrap(), StakingLedger { stash: 21, - total: 1000, - active: 1000, + total: 1000 * ed, + active: 1000 * ed, unlocking: vec![], claimed_rewards: vec![] } ); - // unbond all of it except ed. - assert_ok!(Staking::unbond(Origin::signed(20), 1000 - ed)); + // when unbond all of it except ed. + assert_ok!(Staking::unbond(Origin::signed(20), 999 * ed)); start_active_era(3); assert_ok!(Staking::withdraw_unbonded(Origin::signed(20), 100)); - // initial stuff. + // then assert_eq!( Staking::ledger(&20).unwrap(), StakingLedger { @@ -3981,313 +3948,294 @@ mod election_data_provider { assert_eq!(ForceEra::::get(), Forcing::NotForcing); }) } +} - #[test] - #[should_panic] - fn count_check_works() { - ExtBuilder::default().build_and_execute(|| { - // We should never insert into the validators or nominators map directly as this will - // not keep track of the count. This test should panic as we verify the count is accurate - // after every test using the `post_checks` in `mock`. - Validators::::insert(987654321, ValidatorPrefs::default()); - Nominators::::insert( - 987654321, - Nominations { - targets: vec![], - submitted_in: Default::default(), - suppressed: false, - }, +#[test] +#[should_panic] +fn count_check_works() { + ExtBuilder::default().build_and_execute(|| { + // We should never insert into the validators or nominators map directly as this will + // not keep track of the count. This test should panic as we verify the count is accurate + // after every test using the `post_checks` in `mock`. + Validators::::insert(987654321, ValidatorPrefs::default()); + Nominators::::insert( + 987654321, + Nominations { targets: vec![], submitted_in: Default::default(), suppressed: false }, + ); + }) +} + +#[test] +fn min_bond_checks_work() { + ExtBuilder::default() + .existential_deposit(100) + .balance_factor(100) + .min_nominator_bond(1_000) + .min_validator_bond(1_500) + .build_and_execute(|| { + // 500 is not enough for any role + assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::Controller)); + assert_noop!( + Staking::nominate(Origin::signed(4), vec![1]), + Error::::InsufficientBond + ); + assert_noop!( + Staking::validate(Origin::signed(4), ValidatorPrefs::default()), + Error::::InsufficientBond, ); - }) - } - #[test] - fn min_bond_checks_work() { - ExtBuilder::default() - .existential_deposit(100) - .min_nominator_bond(1_000) - .min_validator_bond(1_500) - .build_and_execute(|| { - // 500 is not enough for any role - assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::Controller)); - assert_noop!( - Staking::nominate(Origin::signed(4), vec![1]), - Error::::InsufficientBond - ); - assert_noop!( - Staking::validate(Origin::signed(4), ValidatorPrefs::default()), - Error::::InsufficientBond, - ); - - // 1000 is enough for nominator - assert_ok!(Staking::bond_extra(Origin::signed(3), 500)); - assert_ok!(Staking::nominate(Origin::signed(4), vec![1])); - assert_noop!( - Staking::validate(Origin::signed(4), ValidatorPrefs::default()), - Error::::InsufficientBond, - ); - - // 1500 is enough for validator - assert_ok!(Staking::bond_extra(Origin::signed(3), 500)); - assert_ok!(Staking::nominate(Origin::signed(4), vec![1])); - assert_ok!(Staking::validate(Origin::signed(4), ValidatorPrefs::default())); - - // Can't unbond anything as validator - assert_noop!( - Staking::unbond(Origin::signed(4), 500), - Error::::InsufficientBond - ); - - // Once they are a nominator, they can unbond 500 - assert_ok!(Staking::nominate(Origin::signed(4), vec![1])); - assert_ok!(Staking::unbond(Origin::signed(4), 500)); - assert_noop!( - Staking::unbond(Origin::signed(4), 500), - Error::::InsufficientBond - ); - - // Once they are chilled they can unbond everything - assert_ok!(Staking::chill(Origin::signed(4))); - assert_ok!(Staking::unbond(Origin::signed(4), 1000)); - }) - } + // 1000 is enough for nominator + assert_ok!(Staking::bond_extra(Origin::signed(3), 500)); + assert_ok!(Staking::nominate(Origin::signed(4), vec![1])); + assert_noop!( + Staking::validate(Origin::signed(4), ValidatorPrefs::default()), + Error::::InsufficientBond, + ); - #[test] - fn chill_other_works() { - ExtBuilder::default() - .existential_deposit(100) - .min_nominator_bond(1_000) - .min_validator_bond(1_500) - .build_and_execute(|| { - for i in 0..15 { - let a = 4 * i; - let b = 4 * i + 1; - let c = 4 * i + 2; - let d = 4 * i + 3; - Balances::make_free_balance_be(&a, 100_000); - Balances::make_free_balance_be(&b, 100_000); - Balances::make_free_balance_be(&c, 100_000); - Balances::make_free_balance_be(&d, 100_000); - - // Nominator - assert_ok!(Staking::bond( - Origin::signed(a), - b, - 1000, - RewardDestination::Controller - )); - assert_ok!(Staking::nominate(Origin::signed(b), vec![1])); - - // Validator - assert_ok!(Staking::bond( - Origin::signed(c), - d, - 1500, - RewardDestination::Controller - )); - assert_ok!(Staking::validate(Origin::signed(d), ValidatorPrefs::default())); - } + // 1500 is enough for validator + assert_ok!(Staking::bond_extra(Origin::signed(3), 500)); + assert_ok!(Staking::nominate(Origin::signed(4), vec![1])); + assert_ok!(Staking::validate(Origin::signed(4), ValidatorPrefs::default())); - // To chill other users, we need to: - // * Set a minimum bond amount - // * Set a limit - // * Set a threshold - // - // If any of these are missing, we do not have enough information to allow the - // `chill_other` to succeed from one user to another. - - // Can't chill these users - assert_noop!( - Staking::chill_other(Origin::signed(1337), 1), - Error::::CannotChillOther - ); - assert_noop!( - Staking::chill_other(Origin::signed(1337), 3), - Error::::CannotChillOther - ); - - // Change the minimum bond... but no limits. - assert_ok!(Staking::set_staking_limits( - Origin::root(), - 1_500, - 2_000, - None, - None, - None - )); + // Can't unbond anything as validator + assert_noop!(Staking::unbond(Origin::signed(4), 500), Error::::InsufficientBond); - // Still can't chill these users - assert_noop!( - Staking::chill_other(Origin::signed(1337), 1), - Error::::CannotChillOther - ); - assert_noop!( - Staking::chill_other(Origin::signed(1337), 3), - Error::::CannotChillOther - ); - - // Add limits, but no threshold - assert_ok!(Staking::set_staking_limits( - Origin::root(), - 1_500, - 2_000, - Some(10), - Some(10), - None - )); + // Once they are a nominator, they can unbond 500 + assert_ok!(Staking::nominate(Origin::signed(4), vec![1])); + assert_ok!(Staking::unbond(Origin::signed(4), 500)); + assert_noop!(Staking::unbond(Origin::signed(4), 500), Error::::InsufficientBond); - // Still can't chill these users - assert_noop!( - Staking::chill_other(Origin::signed(1337), 1), - Error::::CannotChillOther - ); - assert_noop!( - Staking::chill_other(Origin::signed(1337), 3), - Error::::CannotChillOther - ); - - // Add threshold, but no limits - assert_ok!(Staking::set_staking_limits( - Origin::root(), - 1_500, - 2_000, - None, - None, - Some(Percent::from_percent(0)) - )); + // Once they are chilled they can unbond everything + assert_ok!(Staking::chill(Origin::signed(4))); + assert_ok!(Staking::unbond(Origin::signed(4), 1000)); + }) +} - // Still can't chill these users - assert_noop!( - Staking::chill_other(Origin::signed(1337), 1), - Error::::CannotChillOther - ); - assert_noop!( - Staking::chill_other(Origin::signed(1337), 3), - Error::::CannotChillOther - ); - - // Add threshold and limits - assert_ok!(Staking::set_staking_limits( - Origin::root(), - 1_500, - 2_000, - Some(10), - Some(10), - Some(Percent::from_percent(75)) +#[test] +fn chill_other_works() { + ExtBuilder::default() + .existential_deposit(100) + .balance_factor(100) + .min_nominator_bond(1_000) + .min_validator_bond(1_500) + .build_and_execute(|| { + let initial_validators = CounterForValidators::::get(); + let initial_nominators = CounterForNominators::::get(); + for i in 0..15 { + let a = 4 * i; + let b = 4 * i + 1; + let c = 4 * i + 2; + let d = 4 * i + 3; + Balances::make_free_balance_be(&a, 100_000); + Balances::make_free_balance_be(&b, 100_000); + Balances::make_free_balance_be(&c, 100_000); + Balances::make_free_balance_be(&d, 100_000); + + // Nominator + assert_ok!(Staking::bond( + Origin::signed(a), + b, + 1000, + RewardDestination::Controller + )); + assert_ok!(Staking::nominate(Origin::signed(b), vec![1])); + + // Validator + assert_ok!(Staking::bond( + Origin::signed(c), + d, + 1500, + RewardDestination::Controller )); + assert_ok!(Staking::validate(Origin::signed(d), ValidatorPrefs::default())); + } - // 16 people total because tests start with 1 active one - assert_eq!(CounterForNominators::::get(), 16); - assert_eq!(CounterForValidators::::get(), 16); + // To chill other users, we need to: + // * Set a minimum bond amount + // * Set a limit + // * Set a threshold + // + // If any of these are missing, we do not have enough information to allow the + // `chill_other` to succeed from one user to another. - // Users can now be chilled down to 7 people, so we try to remove 9 of them (starting with 16) - for i in 6..15 { - let b = 4 * i + 1; - let d = 4 * i + 3; - assert_ok!(Staking::chill_other(Origin::signed(1337), b)); - assert_ok!(Staking::chill_other(Origin::signed(1337), d)); - } + // Can't chill these users + assert_noop!( + Staking::chill_other(Origin::signed(1337), 1), + Error::::CannotChillOther + ); + assert_noop!( + Staking::chill_other(Origin::signed(1337), 3), + Error::::CannotChillOther + ); - // Cant go lower. - assert_noop!( - Staking::chill_other(Origin::signed(1337), 1), - Error::::CannotChillOther - ); - assert_noop!( - Staking::chill_other(Origin::signed(1337), 3), - Error::::CannotChillOther - ); - }) - } + // Change the minimum bond... but no limits. + assert_ok!(Staking::set_staking_limits(Origin::root(), 1_500, 2_000, None, None, None)); - #[test] - fn capped_stakers_works() { - ExtBuilder::default().build_and_execute(|| { - let validator_count = CounterForValidators::::get(); - assert_eq!(validator_count, 3); - let nominator_count = CounterForNominators::::get(); - assert_eq!(nominator_count, 1); + // Still can't chill these users + assert_noop!( + Staking::chill_other(Origin::signed(1337), 1), + Error::::CannotChillOther + ); + assert_noop!( + Staking::chill_other(Origin::signed(1337), 3), + Error::::CannotChillOther + ); - // Change the maximums - let max = 10; + // Add limits, but no threshold assert_ok!(Staking::set_staking_limits( Origin::root(), - 10, - 10, - Some(max), - Some(max), + 1_500, + 2_000, + Some(10), + Some(10), + None + )); + + // Still can't chill these users + assert_noop!( + Staking::chill_other(Origin::signed(1337), 1), + Error::::CannotChillOther + ); + assert_noop!( + Staking::chill_other(Origin::signed(1337), 3), + Error::::CannotChillOther + ); + + // Add threshold, but no limits + assert_ok!(Staking::set_staking_limits( + Origin::root(), + 1_500, + 2_000, + None, + None, Some(Percent::from_percent(0)) )); - // can create `max - validator_count` validators - let mut some_existing_validator = AccountId::default(); - for i in 0..max - validator_count { - let (_, controller) = testing_utils::create_stash_controller::( - i + 10_000_000, - 100, - RewardDestination::Controller, - ) - .unwrap(); - assert_ok!(Staking::validate( - Origin::signed(controller), - ValidatorPrefs::default() - )); - some_existing_validator = controller; + // Still can't chill these users + assert_noop!( + Staking::chill_other(Origin::signed(1337), 1), + Error::::CannotChillOther + ); + assert_noop!( + Staking::chill_other(Origin::signed(1337), 3), + Error::::CannotChillOther + ); + + // Add threshold and limits + assert_ok!(Staking::set_staking_limits( + Origin::root(), + 1_500, + 2_000, + Some(10), + Some(10), + Some(Percent::from_percent(75)) + )); + + // 16 people total because tests start with 2 active one + assert_eq!(CounterForNominators::::get(), 15 + initial_nominators); + assert_eq!(CounterForValidators::::get(), 15 + initial_validators); + + // Users can now be chilled down to 7 people, so we try to remove 9 of them (starting with 16) + for i in 6..15 { + let b = 4 * i + 1; + let d = 4 * i + 3; + assert_ok!(Staking::chill_other(Origin::signed(1337), b)); + assert_ok!(Staking::chill_other(Origin::signed(1337), d)); } - // but no more - let (_, last_validator) = testing_utils::create_stash_controller::( - 1337, + // chill a nominator. Limit is not reached, not chill-able + assert_eq!(CounterForNominators::::get(), 7); + assert_noop!( + Staking::chill_other(Origin::signed(1337), 1), + Error::::CannotChillOther + ); + // chill a validator. Limit is reached, chill-able. + assert_eq!(CounterForValidators::::get(), 9); + assert_ok!(Staking::chill_other(Origin::signed(1337), 3)); + }) +} + +#[test] +fn capped_stakers_works() { + ExtBuilder::default().build_and_execute(|| { + let validator_count = CounterForValidators::::get(); + assert_eq!(validator_count, 3); + let nominator_count = CounterForNominators::::get(); + assert_eq!(nominator_count, 1); + + // Change the maximums + let max = 10; + assert_ok!(Staking::set_staking_limits( + Origin::root(), + 10, + 10, + Some(max), + Some(max), + Some(Percent::from_percent(0)) + )); + + // can create `max - validator_count` validators + let mut some_existing_validator = AccountId::default(); + for i in 0..max - validator_count { + let (_, controller) = testing_utils::create_stash_controller::( + i + 10_000_000, 100, RewardDestination::Controller, ) .unwrap(); + assert_ok!(Staking::validate(Origin::signed(controller), ValidatorPrefs::default())); + some_existing_validator = controller; + } - assert_noop!( - Staking::validate(Origin::signed(last_validator), ValidatorPrefs::default()), - Error::::TooManyValidators, - ); + // but no more + let (_, last_validator) = testing_utils::create_stash_controller::( + 1337, + 100, + RewardDestination::Controller, + ) + .unwrap(); - // same with nominators - let mut some_existing_nominator = AccountId::default(); - for i in 0..max - nominator_count { - let (_, controller) = testing_utils::create_stash_controller::( - i + 20_000_000, - 100, - RewardDestination::Controller, - ) - .unwrap(); - assert_ok!(Staking::nominate(Origin::signed(controller), vec![1])); - some_existing_nominator = controller; - } + assert_noop!( + Staking::validate(Origin::signed(last_validator), ValidatorPrefs::default()), + Error::::TooManyValidators, + ); - // one more is too many - let (_, last_nominator) = testing_utils::create_stash_controller::( - 30_000_000, + // same with nominators + let mut some_existing_nominator = AccountId::default(); + for i in 0..max - nominator_count { + let (_, controller) = testing_utils::create_stash_controller::( + i + 20_000_000, 100, RewardDestination::Controller, ) .unwrap(); - assert_noop!( - Staking::nominate(Origin::signed(last_nominator), vec![1]), - Error::::TooManyNominators - ); + assert_ok!(Staking::nominate(Origin::signed(controller), vec![1])); + some_existing_nominator = controller; + } - // Re-nominate works fine - assert_ok!(Staking::nominate(Origin::signed(some_existing_nominator), vec![1])); - // Re-validate works fine - assert_ok!(Staking::validate( - Origin::signed(some_existing_validator), - ValidatorPrefs::default() - )); + // one more is too many + let (_, last_nominator) = testing_utils::create_stash_controller::( + 30_000_000, + 100, + RewardDestination::Controller, + ) + .unwrap(); + assert_noop!( + Staking::nominate(Origin::signed(last_nominator), vec![1]), + Error::::TooManyNominators + ); - // No problem when we set to `None` again - assert_ok!(Staking::set_staking_limits(Origin::root(), 10, 10, None, None, None)); - assert_ok!(Staking::nominate(Origin::signed(last_nominator), vec![1])); - assert_ok!(Staking::validate( - Origin::signed(last_validator), - ValidatorPrefs::default() - )); - }) - } + // Re-nominate works fine + assert_ok!(Staking::nominate(Origin::signed(some_existing_nominator), vec![1])); + // Re-validate works fine + assert_ok!(Staking::validate( + Origin::signed(some_existing_validator), + ValidatorPrefs::default() + )); + + // No problem when we set to `None` again + assert_ok!(Staking::set_staking_limits(Origin::root(), 10, 10, None, None, None)); + assert_ok!(Staking::nominate(Origin::signed(last_nominator), vec![1])); + assert_ok!(Staking::validate(Origin::signed(last_validator), ValidatorPrefs::default())); + }) } From 03e9d30538b8b485f27d79c1a87857b714bd1f04 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sat, 7 Aug 2021 20:42:57 +0200 Subject: [PATCH 2/4] fix fishy test --- frame/staking/src/tests.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 1de36ad74f199..af9dbd6b79157 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -1936,7 +1936,7 @@ fn bond_with_duplicate_vote_should_be_ignored_by_election_provider_elected() { ); // no nominators shall exist. - assert!(>::iter().map(|(n, _)| n).collect::>().is_empty()); + assert!(>::iter().collect::>().is_empty()); // give the man some money. let initial_balance = 1000; @@ -1945,18 +1945,18 @@ fn bond_with_duplicate_vote_should_be_ignored_by_election_provider_elected() { } assert_ok!(Staking::bond(Origin::signed(1), 2, 1000, RewardDestination::Controller)); - assert_ok!(Staking::nominate(Origin::signed(2), vec![11, 11, 11, 21, 31])); + assert_ok!(Staking::nominate(Origin::signed(2), vec![11, 11, 11, 21])); assert_ok!(Staking::bond(Origin::signed(3), 4, 1000, RewardDestination::Controller)); - assert_ok!(Staking::nominate(Origin::signed(4), vec![21, 31])); + assert_ok!(Staking::nominate(Origin::signed(4), vec![21])); // winners should be 21 and 11. let supports = ::ElectionProvider::elect().unwrap().0; assert_eq!( supports, vec![ - (21, Support { total: 1800, voters: vec![(21, 1000), (3, 400), (1, 400)] }), - (31, Support { total: 2200, voters: vec![(31, 1000), (3, 600), (1, 600)] }) + (11, Support { total: 1500, voters: vec![(11, 1000), (1, 500)] }), + (21, Support { total: 2500, voters: vec![(21, 1000), (3, 1000), (1, 500)] }) ], ); }); From 9acd0df7f390d5624f0ef0bd29864a6ada37b6da Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sat, 7 Aug 2021 22:04:13 +0200 Subject: [PATCH 3/4] fix one more fishy test --- frame/staking/src/tests.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index af9dbd6b79157..605e20e13d614 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -488,11 +488,17 @@ fn nominating_and_rewards_should_work() { ExtBuilder::default() .nominate(false) .set_status(41, StakerStatus::Validator) + .set_status(11, StakerStatus::Idle) + .set_status(31, StakerStatus::Idle) .build_and_execute(|| { - // initial validators -- everyone is actually even. + // initial validators. assert_eq_uvec!(validator_controllers(), vec![40, 20]); - // Set payee to controller + // re-validate with 11 and 31. + assert_ok!(Staking::validate(Origin::signed(10), Default::default())); + assert_ok!(Staking::validate(Origin::signed(30), Default::default())); + + // Set payee to controller. assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); assert_ok!(Staking::set_payee(Origin::signed(20), RewardDestination::Controller)); assert_ok!(Staking::set_payee(Origin::signed(30), RewardDestination::Controller)); From 53edbfa578b8f99b904ee47cfbd39b1c5b8c51d9 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 10 Aug 2021 15:48:48 +0200 Subject: [PATCH 4/4] some review comments --- frame/staking/src/mock.rs | 20 ++--- frame/staking/src/tests.rs | 171 ++++++++++++++++--------------------- 2 files changed, 84 insertions(+), 107 deletions(-) diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 58cf99eb2d774..20f815e8bad65 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -454,19 +454,19 @@ impl ExtBuilder { } // replace any of the status if needed. self.status.into_iter().for_each(|(stash, status)| { - if let Some((_, _, _, ref mut prev_status)) = - stakers.iter_mut().find(|s| s.0 == stash) - { - *prev_status = status - } + let (_, _, _, ref mut prev_status) = stakers + .iter_mut() + .find(|s| s.0 == stash) + .expect("set_status staker should exist; qed"); + *prev_status = status; }); // replaced any of the stakes if needed. self.stakes.into_iter().for_each(|(stash, stake)| { - if let Some((_, _, ref mut prev_stake, _)) = - stakers.iter_mut().find(|s| s.0 == stash) - { - *prev_stake = stake - } + let (_, _, ref mut prev_stake, _) = stakers + .iter_mut() + .find(|s| s.0 == stash) + .expect("set_stake staker should exits; qed."); + *prev_stake = stake; }); // extend stakers if needed. stakers.extend(self.stakers) diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 605e20e13d614..364822ed3e033 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -145,7 +145,7 @@ fn basic_setup_works() { assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21]); assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 11), + Staking::eras_stakers(active_era(), 11), Exposure { total: 1125, own: 1000, @@ -153,7 +153,7 @@ fn basic_setup_works() { }, ); assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 21), + Staking::eras_stakers(active_era(), 21), Exposure { total: 1375, own: 1000, @@ -162,13 +162,13 @@ fn basic_setup_works() { ); // initial total stake = 1125 + 1375 - assert_eq!(Staking::eras_total_stake(Staking::active_era().unwrap().index), 2500); + assert_eq!(Staking::eras_total_stake(active_era()), 2500); // The number of validators required. assert_eq!(Staking::validator_count(), 2); // Initial Era and session - assert_eq!(Staking::active_era().unwrap().index, 0); + assert_eq!(active_era(), 0); // Account 10 has `balance_factor` free balance assert_eq!(Balances::free_balance(10), 1); @@ -236,7 +236,7 @@ fn rewards_should_work() { assert_eq!(Balances::total_balance(&101), init_balance_101); assert_eq_uvec!(Session::validators(), vec![11, 21]); assert_eq!( - Staking::eras_reward_points(Staking::active_era().unwrap().index), + Staking::eras_reward_points(active_era()), EraRewardPoints { total: 50 * 3, individual: vec![(11, 100), (21, 50)].into_iter().collect(), @@ -250,7 +250,7 @@ fn rewards_should_work() { start_session(2); start_session(3); - assert_eq!(Staking::active_era().unwrap().index, 1); + assert_eq!(active_era(), 1); assert_eq!( mock::REWARD_REMAINDER_UNBALANCED.with(|v| *v.borrow()), maximum_payout - total_payout_0, @@ -350,7 +350,7 @@ fn staking_should_work() { // --- Block 4: the validators will now be queued. start_session(4); - assert_eq!(Staking::active_era().unwrap().index, 1); + assert_eq!(active_era(), 1); // --- Block 5: the validators are still in queue. start_session(5); @@ -437,7 +437,7 @@ fn less_than_needed_candidates_works() { // But the exposure is updated in a simple way. No external votes exists. // This is purely self-vote. - assert!(ErasStakers::::iter_prefix_values(Staking::active_era().unwrap().index) + assert!(ErasStakers::::iter_prefix_values(active_era()) .all(|exposure| exposure.others.is_empty())); }); } @@ -535,14 +535,9 @@ fn nominating_and_rewards_should_work() { assert_eq!(Balances::total_balance(&20), initial_balance_20 + total_payout_0 / 2); initial_balance_20 = Balances::total_balance(&20); - // 30 and 40 are not chosen anymore - assert_eq!( - ErasStakers::::iter_prefix_values(Staking::active_era().unwrap().index) - .count(), - 2 - ); + assert_eq!(ErasStakers::::iter_prefix_values(active_era()).count(), 2); assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 11), + Staking::eras_stakers(active_era(), 11), Exposure { total: 1000 + 800, own: 1000, @@ -553,7 +548,7 @@ fn nominating_and_rewards_should_work() { }, ); assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 21), + Staking::eras_stakers(active_era(), 21), Exposure { total: 1000 + 1200, own: 1000, @@ -889,7 +884,7 @@ fn cannot_transfer_staked_balance_2() { // Confirm account 21 has some free balance assert_eq!(Balances::free_balance(21), 2000); // Confirm account 21 (via controller 20) is totally staked - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 1000); + assert_eq!(Staking::eras_stakers(active_era(), 21).total, 1000); // Confirm account 21 can transfer at most 1000 assert_noop!( Balances::transfer(Origin::signed(21), 20, 1001), @@ -908,7 +903,7 @@ fn cannot_reserve_staked_balance() { // Confirm account 11 has some free balance assert_eq!(Balances::free_balance(11), 1000); // Confirm account 11 (via controller 10) is totally staked - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).own, 1000); + assert_eq!(Staking::eras_stakers(active_era(), 11).own, 1000); // Confirm account 11 cannot reserve as a result assert_noop!(Balances::reserve(&11, 1), BalancesError::::LiquidityRestrictions); @@ -1049,7 +1044,7 @@ fn validator_payment_prefs_work() { // Compute total payout now for whole duration as other parameter won't change let total_payout_1 = current_total_payout_for_duration(reward_time_per_era()); - let exposure_1 = Staking::eras_stakers(Staking::active_era().unwrap().index, 11); + let exposure_1 = Staking::eras_stakers(active_era(), 11); >::reward_by_ids(vec![(11, 1)]); mock::start_active_era(2); @@ -1134,7 +1129,7 @@ fn bond_extra_and_withdraw_unbonded_works() { let _ = Balances::make_free_balance_be(&11, 1000000); // Initial config should be correct - assert_eq!(Staking::active_era().unwrap().index, 0); + assert_eq!(active_era(), 0); // check the balance of a validator accounts. assert_eq!(Balances::total_balance(&10), 1); @@ -1154,7 +1149,7 @@ fn bond_extra_and_withdraw_unbonded_works() { }) ); assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 11), + Staking::eras_stakers(active_era(), 11), Exposure { total: 1000, own: 1000, others: vec![] } ); @@ -1173,13 +1168,13 @@ fn bond_extra_and_withdraw_unbonded_works() { ); // Exposure is a snapshot! only updated after the next era update. assert_ne!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 11), + Staking::eras_stakers(active_era(), 11), Exposure { total: 1000 + 100, own: 1000 + 100, others: vec![] } ); // trigger next era. mock::start_active_era(2); - assert_eq!(Staking::active_era().unwrap().index, 2); + assert_eq!(active_era(), 2); // ledger should be the same. assert_eq!( @@ -1194,7 +1189,7 @@ fn bond_extra_and_withdraw_unbonded_works() { ); // Exposure is now updated. assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 11), + Staking::eras_stakers(active_era(), 11), Exposure { total: 1000 + 100, own: 1000 + 100, others: vec![] } ); @@ -1314,7 +1309,7 @@ fn rebond_works() { ); mock::start_active_era(2); - assert_eq!(Staking::active_era().unwrap().index, 2); + assert_eq!(active_era(), 2); // Try to rebond some funds. We get an error since no fund is unbonded. assert_noop!(Staking::rebond(Origin::signed(10), 500), Error::::NoUnlockChunk); @@ -1528,8 +1523,8 @@ fn reward_to_stake_works() { // Confirm account 10 and 20 are validators assert!(>::contains_key(&11) && >::contains_key(&21)); - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000); - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 2000); + assert_eq!(Staking::eras_stakers(active_era(), 11).total, 1000); + assert_eq!(Staking::eras_stakers(active_era(), 21).total, 2000); // Give the man some money. let _ = Balances::make_free_balance_be(&10, 1000); @@ -1557,8 +1552,8 @@ fn reward_to_stake_works() { mock::start_active_era(1); mock::make_all_reward_payment(0); - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000); - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 69); + assert_eq!(Staking::eras_stakers(active_era(), 11).total, 1000); + assert_eq!(Staking::eras_stakers(active_era(), 21).total, 69); let _11_balance = Balances::free_balance(&11); assert_eq!(_11_balance, 1000 + total_payout_0 / 2); @@ -1567,14 +1562,8 @@ fn reward_to_stake_works() { mock::start_active_era(2); // -- new infos - assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, - 1000 + total_payout_0 / 2 - ); - assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, - 69 + total_payout_0 / 2 - ); + assert_eq!(Staking::eras_stakers(active_era(), 11).total, 1000 + total_payout_0 / 2); + assert_eq!(Staking::eras_stakers(active_era(), 21).total, 69 + total_payout_0 / 2); }); } @@ -1759,14 +1748,8 @@ fn wrong_vote_is_moot() { assert_eq_uvec!(validator_controllers(), vec![20, 10]); // our new voter is taken into account - assert!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11) - .others - .iter() - .any(|i| i.who == 61)); - assert!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21) - .others - .iter() - .any(|i| i.who == 61)); + assert!(Staking::eras_stakers(active_era(), 11).others.iter().any(|i| i.who == 61)); + assert!(Staking::eras_stakers(active_era(), 21).others.iter().any(|i| i.who == 61)); }); } @@ -2052,7 +2035,7 @@ fn reward_validator_slashing_validator_does_not_overflow() { // Check slashing on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(100)], @@ -2081,7 +2064,7 @@ fn reward_from_authorship_event_handler_works() { // 21 is rewarded as an uncle producer // 11 is rewarded as a block producer and uncle referencer and uncle producer assert_eq!( - ErasRewardPoints::::get(Staking::active_era().unwrap().index), + ErasRewardPoints::::get(active_era()), EraRewardPoints { individual: vec![(11, 20 + 2 * 2 + 1), (21, 1)].into_iter().collect(), total: 26, @@ -2101,7 +2084,7 @@ fn add_reward_points_fns_works() { >::reward_by_ids(vec![(21, 1), (11, 1), (11, 1)]); assert_eq!( - ErasRewardPoints::::get(Staking::active_era().unwrap().index), + ErasRewardPoints::::get(active_era()), EraRewardPoints { individual: vec![(11, 4), (21, 2)].into_iter().collect(), total: 6 }, ); }) @@ -2156,7 +2139,7 @@ fn offence_forces_new_era() { ExtBuilder::default().build_and_execute(|| { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(5)], @@ -2174,7 +2157,7 @@ fn offence_ensures_new_era_without_clobbering() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(5)], @@ -2192,7 +2175,7 @@ fn offence_deselects_validator_even_when_slash_is_zero() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(0)], @@ -2213,7 +2196,7 @@ fn slashing_performed_according_exposure() { // This test checks that slashing is performed according the exposure (or more precisely, // historical exposure), not the current balance. ExtBuilder::default().build_and_execute(|| { - assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).own, 1000); + assert_eq!(Staking::eras_stakers(active_era(), 11).own, 1000); // Handle an offence with a historical exposure. on_offence_now( @@ -2239,7 +2222,7 @@ fn slash_in_old_span_does_not_deselect() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(0)], @@ -2262,7 +2245,7 @@ fn slash_in_old_span_does_not_deselect() { on_offence_in_era( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(0)], @@ -2276,7 +2259,7 @@ fn slash_in_old_span_does_not_deselect() { on_offence_in_era( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], // NOTE: A 100% slash here would clean up the account, causing de-registration. @@ -2299,14 +2282,11 @@ fn reporters_receive_their_slice() { // The reporters' reward is calculated from the total exposure. let initial_balance = 1125; - assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, - initial_balance - ); + assert_eq!(Staking::eras_stakers(active_era(), 11).total, initial_balance); on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![1, 2], }], &[Perbill::from_percent(50)], @@ -2329,14 +2309,11 @@ fn subsequent_reports_in_same_span_pay_out_less() { // The reporters' reward is calculated from the total exposure. let initial_balance = 1125; - assert_eq!( - Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, - initial_balance - ); + assert_eq!(Staking::eras_stakers(active_era(), 11).total, initial_balance); on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![1], }], &[Perbill::from_percent(20)], @@ -2349,7 +2326,7 @@ fn subsequent_reports_in_same_span_pay_out_less() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![1], }], &[Perbill::from_percent(50)], @@ -2371,7 +2348,7 @@ fn invulnerables_are_not_slashed() { assert_eq!(Balances::free_balance(11), 1000); assert_eq!(Balances::free_balance(21), 2000); - let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 21); + let exposure = Staking::eras_stakers(active_era(), 21); let initial_balance = Staking::slashable_balance_of(&21); let nominator_balances: Vec<_> = @@ -2380,11 +2357,11 @@ fn invulnerables_are_not_slashed() { on_offence_now( &[ OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }, OffenceDetails { - offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)), + offender: (21, Staking::eras_stakers(active_era(), 21)), reporters: vec![], }, ], @@ -2414,7 +2391,7 @@ fn dont_slash_if_fraction_is_zero() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(0)], @@ -2435,7 +2412,7 @@ fn only_slash_for_max_in_era() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(50)], @@ -2447,7 +2424,7 @@ fn only_slash_for_max_in_era() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(25)], @@ -2458,7 +2435,7 @@ fn only_slash_for_max_in_era() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(60)], @@ -2480,7 +2457,7 @@ fn garbage_collection_after_slashing() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(10)], @@ -2492,7 +2469,7 @@ fn garbage_collection_after_slashing() { on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(100)], @@ -2527,7 +2504,7 @@ fn garbage_collection_on_window_pruning() { mock::start_active_era(1); assert_eq!(Balances::free_balance(11), 1000); - let now = Staking::active_era().unwrap().index; + let now = active_era(); let exposure = Staking::eras_stakers(now, 11); assert_eq!(Balances::free_balance(101), 2000); @@ -2569,14 +2546,14 @@ fn slashing_nominators_by_span_max() { assert_eq!(Balances::free_balance(101), 2000); assert_eq!(Staking::slashable_balance_of(&21), 1000); - let exposure_11 = Staking::eras_stakers(Staking::active_era().unwrap().index, 11); - let exposure_21 = Staking::eras_stakers(Staking::active_era().unwrap().index, 21); + let exposure_11 = Staking::eras_stakers(active_era(), 11); + let exposure_21 = Staking::eras_stakers(active_era(), 21); let nominated_value_11 = exposure_11.others.iter().find(|o| o.who == 101).unwrap().value; let nominated_value_21 = exposure_21.others.iter().find(|o| o.who == 101).unwrap().value; on_offence_in_era( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(10)], @@ -2602,7 +2579,7 @@ fn slashing_nominators_by_span_max() { // second slash: higher era, higher value, same span. on_offence_in_era( &[OffenceDetails { - offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)), + offender: (21, Staking::eras_stakers(active_era(), 21)), reporters: vec![], }], &[Perbill::from_percent(30)], @@ -2623,7 +2600,7 @@ fn slashing_nominators_by_span_max() { // in-era value, but lower slash value than slash 2. on_offence_in_era( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(20)], @@ -2657,7 +2634,7 @@ fn slashes_are_summed_across_spans() { on_offence_now( &[OffenceDetails { - offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)), + offender: (21, Staking::eras_stakers(active_era(), 21)), reporters: vec![], }], &[Perbill::from_percent(10)], @@ -2680,7 +2657,7 @@ fn slashes_are_summed_across_spans() { on_offence_now( &[OffenceDetails { - offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)), + offender: (21, Staking::eras_stakers(active_era(), 21)), reporters: vec![], }], &[Perbill::from_percent(10)], @@ -2704,13 +2681,13 @@ fn deferred_slashes_are_deferred() { assert_eq!(Balances::free_balance(11), 1000); - let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11); + let exposure = Staking::eras_stakers(active_era(), 11); assert_eq!(Balances::free_balance(101), 2000); let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value; on_offence_now( &[OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![], }], &[Perbill::from_percent(10)], @@ -2745,7 +2722,7 @@ fn remove_deferred() { assert_eq!(Balances::free_balance(11), 1000); - let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11); + let exposure = Staking::eras_stakers(active_era(), 11); assert_eq!(Balances::free_balance(101), 2000); let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value; @@ -2811,7 +2788,7 @@ fn remove_multi_deferred() { assert_eq!(Balances::free_balance(11), 1000); - let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11); + let exposure = Staking::eras_stakers(active_era(), 11); assert_eq!(Balances::free_balance(101), 2000); on_offence_now( @@ -2821,7 +2798,7 @@ fn remove_multi_deferred() { on_offence_now( &[OffenceDetails { - offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)), + offender: (21, Staking::eras_stakers(active_era(), 21)), reporters: vec![], }], &[Perbill::from_percent(10)], @@ -2880,8 +2857,8 @@ fn slash_kicks_validators_not_nominators_and_disables_nominator_for_kicked_valid assert_eq!(Balances::free_balance(101), 2000); // 11 and 21 both have the support of 100 - let exposure_11 = Staking::eras_stakers(Staking::active_era().unwrap().index, &11); - let exposure_21 = Staking::eras_stakers(Staking::active_era().unwrap().index, &21); + let exposure_11 = Staking::eras_stakers(active_era(), &11); + let exposure_21 = Staking::eras_stakers(active_era(), &21); assert_eq!(exposure_11.total, 1000 + 125); assert_eq!(exposure_21.total, 1000 + 375); @@ -2913,8 +2890,8 @@ fn slash_kicks_validators_not_nominators_and_disables_nominator_for_kicked_valid assert_ok!(Staking::validate(Origin::signed(10), Default::default())); mock::start_active_era(2); - let exposure_11 = Staking::eras_stakers(Staking::active_era().unwrap().index, &11); - let exposure_21 = Staking::eras_stakers(Staking::active_era().unwrap().index, &21); + let exposure_11 = Staking::eras_stakers(active_era(), &11); + let exposure_21 = Staking::eras_stakers(active_era(), &21); // 10 is re-elected, but without the support of 100 assert_eq!(exposure_11.total, 900); @@ -2969,7 +2946,7 @@ fn claim_reward_at_the_last_era_and_no_double_claim_and_invalid_claim() { mock::start_active_era(Staking::history_depth() + 1); - let active_era = Staking::active_era().unwrap().index; + let active_era = active_era(); // This is the latest planned era in staking, not the active era let current_era = Staking::current_era().unwrap(); @@ -3015,7 +2992,7 @@ fn zero_slash_keeps_nominators() { assert_eq!(Balances::free_balance(11), 1000); - let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11); + let exposure = Staking::eras_stakers(active_era(), 11); assert_eq!(Balances::free_balance(101), 2000); on_offence_now( @@ -3048,7 +3025,7 @@ fn six_session_delay() { let val_set = Session::validators(); let init_session = Session::current_index(); - let init_active_era = Staking::active_era().unwrap().index; + let init_active_era = active_era(); // pallet-session is delaying session by one, thus the next session to plan is +2. assert_eq!(>::new_session(init_session + 2), None); @@ -3491,7 +3468,7 @@ fn offences_weight_calculated_correctly() { let offenders: Vec::AccountId, pallet_session::historical::IdentificationTuple>> = (1..10).map(|i| OffenceDetails { - offender: (i, Staking::eras_stakers(Staking::active_era().unwrap().index, i)), + offender: (i, Staking::eras_stakers(active_era(), i)), reporters: vec![], } ).collect(); @@ -3500,7 +3477,7 @@ fn offences_weight_calculated_correctly() { // On Offence with one offenders, Applied let one_offender = [ OffenceDetails { - offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)), + offender: (11, Staking::eras_stakers(active_era(), 11)), reporters: vec![1], }, ];