Skip to content
tests.rs 95.5 KiB
Newer Older
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
Gav Wood's avatar
Gav Wood committed
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
Gav Wood's avatar
Gav Wood committed
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate.  If not, see <http://www.gnu.org/licenses/>.
Gav Wood's avatar
Gav Wood committed

//! Tests for the module.

use super::*;
use sp_runtime::{assert_eq_error_rate, traits::{OnInitialize, BadOrigin}};
use sp_staking::offence::OffenceDetails;
use frame_support::{
	assert_ok, assert_noop,
	traits::{Currency, ReservableCurrency},
Gavin Wood's avatar
Gavin Wood committed
	StorageMap,
Gavin Wood's avatar
Gavin Wood committed
use pallet_balances::Error as BalancesError;
Gavin Wood's avatar
Gavin Wood committed
use crate::Store;
#[test]
fn force_unstake_works() {
	// Verifies initial conditions of mock
	ExtBuilder::default().build().execute_with(|| {
		// Account 11 is stashed and locked, and account 10 is the controller
		assert_eq!(Staking::bonded(&11), Some(10));
		// Cant transfer
		assert_noop!(
			Balances::transfer(Origin::signed(11), 1, 10),
Gavin Wood's avatar
Gavin Wood committed
			BalancesError::<Test, _>::LiquidityRestrictions
		);
		// Force unstake requires root.
		assert_noop!(Staking::force_unstake(Origin::signed(11), 11), BadOrigin);
		// We now force them to unstake
		assert_ok!(Staking::force_unstake(Origin::ROOT, 11));
		// No longer bonded.
		assert_eq!(Staking::bonded(&11), None);
		// Transfer works.
		assert_ok!(Balances::transfer(Origin::signed(11), 1, 10));
	});
}

Gav Wood's avatar
Gav Wood committed
#[test]
fn basic_setup_works() {
	// Verifies initial conditions of mock
	ExtBuilder::default().build().execute_with(|| {
		// Account 11 is stashed and locked, and account 10 is the controller
		assert_eq!(Staking::bonded(&11), Some(10));
		// Account 21 is stashed and locked, and account 20 is the controller
		assert_eq!(Staking::bonded(&21), Some(20));
		// Account 1 is not a stashed
		assert_eq!(Staking::bonded(&1), None);

		// Account 10 controls the stash from account 11, which is 100 * balance_factor units
		assert_eq!(
			Staking::ledger(&10),
Gavin Wood's avatar
Gavin Wood committed
			Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![], last_reward: None })
		// Account 20 controls the stash from account 21, which is 200 * balance_factor units
		assert_eq!(
			Staking::ledger(&20),
Gavin Wood's avatar
Gavin Wood committed
			Some(StakingLedger { stash: 21, total: 1000, active: 1000, unlocking: vec![], last_reward: None })
		// Account 1 does not control any stash
		assert_eq!(Staking::ledger(&1), None);

		// ValidatorPrefs are default
		assert_eq!(<Validators<Test>>::enumerate().collect::<Vec<_>>(), vec![
			(31, ValidatorPrefs::default()),
			(21, ValidatorPrefs::default()),
			(11, ValidatorPrefs::default())
		assert_eq!(
			Staking::ledger(100),
Gavin Wood's avatar
Gavin Wood committed
			Some(StakingLedger { stash: 101, total: 500, active: 500, unlocking: vec![], last_reward: None })
		assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21]);
thiolliere's avatar
thiolliere committed
		assert_eq!(
Gavin Wood's avatar
Gavin Wood committed
			Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
			Exposure {
				total: 1125,
				own: 1000,
				others: vec![ IndividualExposure { who: 101, value: 125 }]
			},
thiolliere's avatar
thiolliere committed
		);
		assert_eq!(
Gavin Wood's avatar
Gavin Wood committed
			Staking::eras_stakers(Staking::active_era().unwrap().index, 21),
			Exposure {
				total: 1375,
				own: 1000,
				others: vec![ IndividualExposure { who: 101, value: 375 }]
			},
thiolliere's avatar
thiolliere committed
		);
		// initial slot_stake
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::eras_total_stake(Staking::active_era().unwrap().index), 2500);

		// The number of validators required.
		assert_eq!(Staking::validator_count(), 2);

		// Initial Era and session
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 0);
		// Account 10 has `balance_factor` free balance
		assert_eq!(Balances::free_balance(10), 1);
		assert_eq!(Balances::free_balance(10), 1);
		// New era is not being forced
		assert_eq!(Staking::force_era(), Forcing::NotForcing);

		// All exposures must be correct.
Gavin Wood's avatar
Gavin Wood committed
		check_exposure_all(Staking::active_era().unwrap().index);
		check_nominator_all(Staking::active_era().unwrap().index);
#[test]
fn change_controller_works() {
	ExtBuilder::default().build().execute_with(|| {
		assert_eq!(Staking::bonded(&11), Some(10));

Gavin Wood's avatar
Gavin Wood committed
		assert!(Session::validators().contains(&11));
		// 10 can control 11 who is initially a validator.
		assert_ok!(Staking::chill(Origin::signed(10)));
Gavin Wood's avatar
Gavin Wood committed
		assert!(Session::validators().contains(&11));

		assert_ok!(Staking::set_controller(Origin::signed(11), 5));

		start_era(1);

		assert_noop!(
			Staking::validate(Origin::signed(10), ValidatorPrefs::default()),
			Error::<Test>::NotController,
		);
		assert_ok!(Staking::validate(Origin::signed(5), ValidatorPrefs::default()));
	})
}

#[test]
fn rewards_should_work() {
	// should check that:
	// * rewards get recorded per session
	// * rewards get paid per Era
	// * Check that nominators are also rewarded
Gavin Wood's avatar
Gavin Wood committed
	ExtBuilder::default().nominate(true).build().execute_with(|| {
		let init_balance_10 = Balances::total_balance(&10);
		let init_balance_11 = Balances::total_balance(&11);
Gavin Wood's avatar
Gavin Wood committed
		let init_balance_20 = Balances::total_balance(&20);
		let init_balance_21 = Balances::total_balance(&21);
		let init_balance_100 = Balances::total_balance(&100);
		let init_balance_101 = Balances::total_balance(&101);
Gavin Wood's avatar
Gavin Wood committed
		// Check state
		Payee::<Test>::insert(11, RewardDestination::Controller);
		Payee::<Test>::insert(21, RewardDestination::Controller);
		Payee::<Test>::insert(101, RewardDestination::Controller);
thiolliere's avatar
thiolliere committed
		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		<Module<Test>>::reward_by_ids(vec![(11, 50)]);
		// This is the second validator of the current elected set.
thiolliere's avatar
thiolliere committed
		<Module<Test>>::reward_by_ids(vec![(21, 50)]);

		// Compute total payout now for whole duration as other parameter won't change
Gavin Wood's avatar
Gavin Wood committed
		let total_payout_0 = current_total_payout_for_duration(3 * 1000);
		assert!(total_payout_0 > 10); // Test is meaningful if reward something
Gavin Wood's avatar
Gavin Wood committed
		start_session(1);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Balances::total_balance(&10), init_balance_10);
		assert_eq!(Balances::total_balance(&11), init_balance_11);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Balances::total_balance(&20), init_balance_20);
		assert_eq!(Balances::total_balance(&21), init_balance_21);
		assert_eq!(Balances::total_balance(&100), init_balance_100);
		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), EraRewardPoints {
			total: 50*3,
			individual: vec![(11, 100), (21, 50)].into_iter().collect(),
		});
		let part_for_10 = Perbill::from_rational_approximation::<u32>(1000, 1125);
		let part_for_20 = Perbill::from_rational_approximation::<u32>(1000, 1375);
		let part_for_100_from_10 = Perbill::from_rational_approximation::<u32>(125, 1125);
		let part_for_100_from_20 = Perbill::from_rational_approximation::<u32>(375, 1375);
		start_session(2);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 1);
		mock::make_all_reward_payment(0);

		assert_eq_error_rate!(Balances::total_balance(&10), init_balance_10 + part_for_10 * total_payout_0*2/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&11), init_balance_11, 2);
		assert_eq_error_rate!(Balances::total_balance(&20), init_balance_20 + part_for_20 * total_payout_0*1/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&21), init_balance_21, 2);
		assert_eq_error_rate!(
			Balances::total_balance(&100),
			init_balance_100
				+ part_for_100_from_10 * total_payout_0 * 2/3
				+ part_for_100_from_20 * total_payout_0 * 1/3,
			2
		);
		assert_eq_error_rate!(Balances::total_balance(&101), init_balance_101, 2);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq_uvec!(Session::validators(), vec![11, 21]);
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);
Gavin Wood's avatar
Gavin Wood committed
		// Compute total payout now for whole duration as other parameter won't change
		let total_payout_1 = current_total_payout_for_duration(3 * 1000);
		assert!(total_payout_1 > 10); // Test is meaningful if reward something
Gavin Wood's avatar
Gavin Wood committed
		start_era(2);
		mock::make_all_reward_payment(1);

		assert_eq_error_rate!(Balances::total_balance(&10), init_balance_10 + part_for_10 * (total_payout_0 * 2/3 + total_payout_1), 2);
		assert_eq_error_rate!(Balances::total_balance(&11), init_balance_11, 2);
		assert_eq_error_rate!(Balances::total_balance(&20), init_balance_20 + part_for_20 * total_payout_0 * 1/3, 2);
		assert_eq_error_rate!(Balances::total_balance(&21), init_balance_21, 2);
		assert_eq_error_rate!(
			Balances::total_balance(&100),
			init_balance_100
				+ part_for_100_from_10 * (total_payout_0 * 2/3 + total_payout_1)
				+ part_for_100_from_20 * total_payout_0 * 1/3,
			2
		);
		assert_eq_error_rate!(Balances::total_balance(&101), init_balance_101, 2);
Gav Wood's avatar
Gav Wood committed

#[test]
fn staking_should_work() {
	// should test:
	// * new validators can be added to the default set
	// * new ones will be chosen per era
	// * either one can unlock the stash and back-down from being a validator via `chill`ing.
	ExtBuilder::default()
		.nominate(false)
		.fair(false) // to give 20 more staked value
		.build()
		.execute_with(|| {
Gavin Wood's avatar
Gavin Wood committed
			// --- Block 1:
			start_session(1);

			Timestamp::set_timestamp(1); // Initialize time.
			// 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); }
Gavin Wood's avatar
Gavin Wood committed
			// --- 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));
Gavin Wood's avatar
Gavin Wood committed
			let current_era_at_bond = Staking::current_era();
			assert_ok!(Staking::validate(Origin::signed(4), ValidatorPrefs::default()));
			// No effects will be seen so far.
			assert_eq_uvec!(validator_controllers(), vec![20, 10]);
Gavin Wood's avatar
Gavin Wood committed
			// --- 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]);
Gavin Wood's avatar
Gavin Wood committed
			// --- Block 4: the validators will now be queued.
Gavin Wood's avatar
Gavin Wood committed
			assert_eq!(Staking::active_era().unwrap().index, 1);

			// --- Block 5: the validators are still in queue.
			start_session(5);

			// --- Block 6: the validators will now be changed.
			start_session(6);
			assert_eq_uvec!(validator_controllers(), vec![20, 4]);
Gavin Wood's avatar
Gavin Wood committed
			// --- Block 6: Unstake 4 as a validator, freeing up the balance stashed in 3
			// 4 will chill
			Staking::chill(Origin::signed(4)).unwrap();
Gavin Wood's avatar
Gavin Wood committed
			// --- Block 7: nothing. 4 is still there.
			start_session(7);
			assert_eq_uvec!(validator_controllers(), vec![20, 4]);
Gavin Wood's avatar
Gavin Wood committed
			// --- Block 8:
			start_session(8);

			// --- 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),
Gavin Wood's avatar
Gavin Wood committed
				Some(StakingLedger {
					stash: 3,
					total: 1500,
					active: 1500,
					unlocking: vec![],
					last_reward: current_era_at_bond,
				})
			);
			// e.g. it cannot spend more than 500 that it has free from the total 2000
			assert_noop!(
				Balances::reserve(&3, 501),
Gavin Wood's avatar
Gavin Wood committed
				BalancesError::<Test, _>::LiquidityRestrictions
			assert_ok!(Balances::reserve(&3, 409));
		});
#[test]
fn less_than_needed_candidates_works() {
	ExtBuilder::default()
		.minimum_validator_count(1)
		.validator_count(4)
		.nominate(false)
		.num_validators(3)
		.build()
		.execute_with(|| {
			assert_eq!(Staking::validator_count(), 4);
			assert_eq!(Staking::minimum_validator_count(), 1);
			assert_eq_uvec!(validator_controllers(), vec![30, 20, 10]);

			start_era(1);

			// Previous set is selected. NO election algorithm is even executed.
			assert_eq_uvec!(validator_controllers(), vec![30, 20, 10]);

			// But the exposure is updated in a simple way. No external votes exists.
			// This is purely self-vote.
Gavin Wood's avatar
Gavin Wood committed
			assert!(
				ErasStakers::<Test>::iter_prefix(Staking::active_era().unwrap().index)
					.all(|exposure| exposure.others.is_empty())
			);
			check_exposure_all(Staking::active_era().unwrap().index);
			check_nominator_all(Staking::active_era().unwrap().index);
}

#[test]
fn no_candidate_emergency_condition() {
	ExtBuilder::default()
		.minimum_validator_count(1)
		.validator_count(15)
		.num_validators(4)
		.validator_pool(true)
		.nominate(false)
		.build()
		.execute_with(|| {
			// initial validators
			assert_eq_uvec!(validator_controllers(), vec![10, 20, 30, 40]);
			let prefs = ValidatorPrefs { commission: Perbill::one() };
			<Staking as crate::Store>::Validators::insert(11, prefs.clone());
			// set the minimum validator count.
			<Staking as crate::Store>::MinimumValidatorCount::put(10);
Gavin Wood's avatar
Gavin Wood committed
			// try to chill
			let _ = Staking::chill(Origin::signed(10));
			start_era(1);
			// Previous ones are elected. chill is invalidates. TODO: #2494
			assert_eq_uvec!(validator_controllers(), vec![10, 20, 30, 40]);
			// Though the validator preferences has been removed.
			assert!(Staking::validators(11) != prefs);
Gav Wood's avatar
Gav Wood committed
#[test]
fn nominating_and_rewards_should_work() {
	// PHRAGMEN OUTPUT: running this test with the reference impl gives:
	//
	// Sequential Phragmén gives
	// 10  is elected with stake  2200.0 and score  0.0003333333333333333
	// 20  is elected with stake  1800.0 and score  0.0005555555555555556

	// 10  has load  0.0003333333333333333 and supported
	// 10  with stake  1000.0
	// 20  has load  0.0005555555555555556 and supported
	// 20  with stake  1000.0
	// 30  has load  0 and supported
	// 30  with stake  0
	// 40  has load  0 and supported
	// 40  with stake  0
	// 2  has load  0.0005555555555555556 and supported
	// 10  with stake  600.0 20  with stake  400.0 30  with stake  0.0
	// 4  has load  0.0005555555555555556 and supported
	// 10  with stake  600.0 20  with stake  400.0 40  with stake  0.0

	// Sequential Phragmén with post processing gives
	// 10  is elected with stake  2000.0 and score  0.0003333333333333333
	// 20  is elected with stake  2000.0 and score  0.0005555555555555556

	// 10  has load  0.0003333333333333333 and supported
	// 10  with stake  1000.0
	// 20  has load  0.0005555555555555556 and supported
	// 20  with stake  1000.0
	// 30  has load  0 and supported
	// 30  with stake  0
	// 40  has load  0 and supported
	// 40  with stake  0
	// 2  has load  0.0005555555555555556 and supported
	// 10  with stake  400.0 20  with stake  600.0 30  with stake  0
	// 4  has load  0.0005555555555555556 and supported
	// 10  with stake  600.0 20  with stake  400.0 40  with stake  0.0
	ExtBuilder::default()
		.nominate(false)
		.validator_pool(true)
		.build()
		.execute_with(|| {
			// initial validators -- everyone is actually even.
			assert_eq_uvec!(validator_controllers(), vec![40, 30]);

			// 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));
			assert_ok!(Staking::set_payee(Origin::signed(40), RewardDestination::Controller));

			// give the man some money
			let initial_balance = 1000;
			for i in [1, 2, 3, 4, 5, 10, 11, 20, 21].iter() {
				let _ = Balances::make_free_balance_be(i, initial_balance);
			}

			// 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(3000);
			assert!(total_payout_0 > 100); // Test is meaningful if reward something
			<Module<Test>>::reward_by_ids(vec![(41, 1)]);
			<Module<Test>>::reward_by_ids(vec![(31, 1)]);

			start_era(1);

			// 10 and 20 have more votes, they will be chosen by phragmen.
			assert_eq_uvec!(validator_controllers(), vec![20, 10]);

			// OLD validators must have already received some rewards.
Gavin Wood's avatar
Gavin Wood committed
			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.

Gavin Wood's avatar
Gavin Wood committed
			// 30 and 40 are not chosen anymore
			assert_eq!(ErasStakers::<Test>::iter_prefix(Staking::active_era().unwrap().index).count(), 2);
thiolliere's avatar
thiolliere committed
			assert_eq!(
Gavin Wood's avatar
Gavin Wood committed
				Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
				Exposure {
					total: 1000 + 800,
					own: 1000,
					others: vec![
						IndividualExposure { who: 3, value: 400 },
						IndividualExposure { who: 1, value: 400 },
					]
				},
thiolliere's avatar
thiolliere committed
			);
			assert_eq!(
Gavin Wood's avatar
Gavin Wood committed
				Staking::eras_stakers(Staking::active_era().unwrap().index, 21),
				Exposure {
					total: 1000 + 1200,
					own: 1000,
					others: vec![
						IndividualExposure { who: 3, value: 600 },
						IndividualExposure { who: 1, value: 600 },
					]
				},
thiolliere's avatar
thiolliere committed
			);

			// the total reward for era 1
			let total_payout_1 = current_total_payout_for_duration(3000);
			assert!(total_payout_1 > 100); // Test is meaningful if reward something
			<Module<Test>>::reward_by_ids(vec![(21, 2)]);
			<Module<Test>>::reward_by_ids(vec![(11, 1)]);

			start_era(2);

			// nothing else will happen, era ends and rewards are paid again,
			// it is expected that nominators will also be paid. See below

Gavin Wood's avatar
Gavin Wood committed
			mock::make_all_reward_payment(1);
			let payout_for_10 = total_payout_1 / 3;
			let payout_for_20 = 2 * total_payout_1 / 3;
thiolliere's avatar
thiolliere committed
			// Nominator 2: has [400/1800 ~ 2/9 from 10] + [600/2200 ~ 3/11 from 20]'s reward. ==> 2/9 + 3/11
			assert_eq_error_rate!(
				Balances::total_balance(&2),
				initial_balance + (2 * payout_for_10 / 9 + 3 * payout_for_20 / 11),
				1,
			);
			// Nominator 4: has [400/1800 ~ 2/9 from 10] + [600/2200 ~ 3/11 from 20]'s reward. ==> 2/9 + 3/11
			assert_eq_error_rate!(
				Balances::total_balance(&4),
				initial_balance + (2 * payout_for_10 / 9 + 3 * payout_for_20 / 11),
				1,
			);

			// Validator 10: got 800 / 1800 external stake => 8/18 =? 4/9 => Validator's share = 5/9
			assert_eq_error_rate!(
				Balances::total_balance(&10),
				initial_balance + 5 * payout_for_10 / 9,
				1,
			);
			// 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,
				1,
			);
Gavin Wood's avatar
Gavin Wood committed
			check_exposure_all(Staking::active_era().unwrap().index);
			check_nominator_all(Staking::active_era().unwrap().index);
Gav Wood's avatar
Gav Wood committed
#[test]
fn nominators_also_get_slashed() {
	// A nominator should be slashed if the validator they nominated is slashed
	// Here is the breakdown of roles:
	// 10 - is the controller of 11
	// 11 - is the stash.
	// 2 - is the nominator of 20, 10
	ExtBuilder::default().nominate(false).build().execute_with(|| {
Gav Wood's avatar
Gav Wood committed
		assert_eq!(Staking::validator_count(), 2);

		// Set payee to controller
		assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller));
Gav Wood's avatar
Gav Wood committed

		// give the man some money.
		let initial_balance = 1000;
		for i in [1, 2, 3, 10].iter() {
			let _ = Balances::make_free_balance_be(i, initial_balance);
Gav Wood's avatar
Gav Wood committed

		// 2 will nominate for 10, 20
		let nominator_stake = 500;
		assert_ok!(Staking::bond(Origin::signed(1), 2, nominator_stake, RewardDestination::default()));
		assert_ok!(Staking::nominate(Origin::signed(2), vec![20, 10]));
Gav Wood's avatar
Gav Wood committed

		let total_payout = current_total_payout_for_duration(3000);
		assert!(total_payout > 100); // Test is meaningful if reward something
thiolliere's avatar
thiolliere committed
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);
		// new era, pay rewards,
		start_era(1);
		// Nominator stash didn't collect any.
		assert_eq!(Balances::total_balance(&2), initial_balance);

		// 10 goes offline
		on_offence_now(
			&[OffenceDetails {
				offender: (
					11,
Gavin Wood's avatar
Gavin Wood committed
					Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
				),
				reporters: vec![],
			}],
			&[Perbill::from_percent(5)],
		);
Gavin Wood's avatar
Gavin Wood committed
		let expo = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
		let slash_value = 50;
		let total_slash = expo.total.min(slash_value);
		let validator_slash = expo.own.min(total_slash);
		let nominator_slash = nominator_stake.min(total_slash - validator_slash);

		// initial + first era reward + slash
		assert_eq!(Balances::total_balance(&11), initial_balance - validator_slash);
		assert_eq!(Balances::total_balance(&2), initial_balance - nominator_slash);
Gavin Wood's avatar
Gavin Wood committed
		check_exposure_all(Staking::active_era().unwrap().index);
		check_nominator_all(Staking::active_era().unwrap().index);
		// Because slashing happened.
		assert!(is_disabled(10));
Gav Wood's avatar
Gav Wood committed
	});
}

#[test]
fn double_staking_should_fail() {
	// should test (in the same order):
	// * an account already bonded as stash cannot be be stashed again.
	// * an account already bonded as stash cannot nominate.
	// * an account already bonded as controller can nominate.
	ExtBuilder::default().build().execute_with(|| {
		let arbitrary_value = 5;
		// 2 = controller, 1 stashed => ok
		assert_ok!(
			Staking::bond(Origin::signed(1), 2, arbitrary_value,
			RewardDestination::default())
		);
		// 4 = not used so far, 1 stashed => not allowed.
		assert_noop!(
			Staking::bond(Origin::signed(1), 4, arbitrary_value,
			RewardDestination::default()), Error::<Test>::AlreadyBonded,
		);
		// 1 = stashed => attempting to nominate should fail.
		assert_noop!(Staking::nominate(Origin::signed(1), vec![1]), Error::<Test>::NotController);
		// 2 = controller  => nominating should work.
		assert_ok!(Staking::nominate(Origin::signed(2), vec![1]));
	});
}

#[test]
fn double_controlling_should_fail() {
	// should test (in the same order):
	// * an account already bonded as controller CANNOT be reused as the controller of another account.
	ExtBuilder::default().build().execute_with(|| {
		let arbitrary_value = 5;
		// 2 = controller, 1 stashed => ok
		assert_ok!(Staking::bond(
			Origin::signed(1),
			2,
			arbitrary_value,
			RewardDestination::default(),
		));
		// 2 = controller, 3 stashed (Note that 2 is reused.) => no-op
		assert_noop!(
			Staking::bond(Origin::signed(3), 2, arbitrary_value, RewardDestination::default()),
			Error::<Test>::AlreadyPaired,
Gav Wood's avatar
Gav Wood committed
#[test]
fn session_and_eras_work() {
	ExtBuilder::default().build().execute_with(|| {
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 0);
Gav Wood's avatar
Gav Wood committed

		// Block 1: No change.
Gavin Wood's avatar
Gavin Wood committed
		start_session(1);
		assert_eq!(Session::current_index(), 1);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 0);
Gavin Wood's avatar
Gavin Wood committed
		// Block 2: No change.
		start_session(2);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Session::current_index(), 2);
		assert_eq!(Staking::active_era().unwrap().index, 0);
Gavin Wood's avatar
Gavin Wood committed
		// Block 3: Era increment.
		start_session(3);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Session::current_index(), 3);
		assert_eq!(Staking::active_era().unwrap().index, 1);

		// Block 4: No change.
		start_session(4);
		assert_eq!(Session::current_index(), 4);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 1);
Gavin Wood's avatar
Gavin Wood committed
		// Block 5: No change.
		start_session(5);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Session::current_index(), 5);
		assert_eq!(Staking::active_era().unwrap().index, 1);
Gavin Wood's avatar
Gavin Wood committed
		// Block 6: Era increment.
		start_session(6);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Session::current_index(), 6);
		assert_eq!(Staking::active_era().unwrap().index, 2);
Gavin Wood's avatar
Gavin Wood committed
		// Block 7: No change.
		start_session(7);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Session::current_index(), 7);
		assert_eq!(Staking::active_era().unwrap().index, 2);
Gavin Wood's avatar
Gavin Wood committed
		// Block 8: No change.
		start_session(8);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Session::current_index(), 8);
		assert_eq!(Staking::active_era().unwrap().index, 2);

		// Block 9: Era increment.
		start_session(9);
		assert_eq!(Session::current_index(), 9);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 3);
#[test]
fn forcing_new_era_works() {
	ExtBuilder::default().build().execute_with(|| {
		// normal flow of session.
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 0);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 0);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 0);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 0);
		start_session(3);
		assert_eq!(Staking::active_era().unwrap().index, 1);

		// no era change.
		ForceEra::put(Forcing::ForceNone);
		start_session(4);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 1);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 1);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 1);
		start_session(7);
		assert_eq!(Staking::active_era().unwrap().index, 1);
		// back to normal.
		// this immediately starts a new session.
		ForceEra::put(Forcing::NotForcing);
		start_session(8);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 1); // There is one session delay
		start_session(9);
		assert_eq!(Staking::active_era().unwrap().index, 2);
		ForceEra::put(Forcing::ForceAlways);
		start_session(10);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 2); // There is one session delay
		start_session(11);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 3);
		start_session(12);
		assert_eq!(Staking::active_era().unwrap().index, 4);

		// just one forceful change
		ForceEra::put(Forcing::ForceNew);
		start_session(13);
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::active_era().unwrap().index, 5);
		assert_eq!(ForceEra::get(), Forcing::NotForcing);
		start_session(14);
		assert_eq!(Staking::active_era().unwrap().index, 6);
		start_session(15);
		assert_eq!(Staking::active_era().unwrap().index, 6);
Gav Wood's avatar
Gav Wood committed
#[test]
fn cannot_transfer_staked_balance() {
	// Tests that a stash account cannot transfer funds
	ExtBuilder::default().nominate(false).build().execute_with(|| {
		// Confirm account 11 is stashed
		assert_eq!(Staking::bonded(&11), Some(10));
		// Confirm account 11 has some free balance
		assert_eq!(Balances::free_balance(11), 1000);
		// Confirm account 11 (via controller 10) is totally staked
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, 1000);
		// Confirm account 11 cannot transfer as a result
		assert_noop!(
			Balances::transfer(Origin::signed(11), 20, 1),
Gavin Wood's avatar
Gavin Wood committed
			BalancesError::<Test, _>::LiquidityRestrictions

		// Give account 11 extra free balance
		let _ = Balances::make_free_balance_be(&11, 10000);
		// Confirm that account 11 can now transfer some balance
		assert_ok!(Balances::transfer(Origin::signed(11), 20, 1));
#[test]
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().execute_with(|| {
		// Confirm account 21 is stashed
		assert_eq!(Staking::bonded(&21), Some(20));
		// Confirm account 21 has some free balance
		assert_eq!(Balances::free_balance(21), 2000);
		// Confirm account 21 (via controller 20) is totally staked
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 21).total, 1000);
		// Confirm account 21 can transfer at most 1000
		assert_noop!(
			Balances::transfer(Origin::signed(21), 20, 1001),
Gavin Wood's avatar
Gavin Wood committed
			BalancesError::<Test, _>::LiquidityRestrictions
		assert_ok!(Balances::transfer(Origin::signed(21), 20, 1000));
	});
}

Gav Wood's avatar
Gav Wood committed
#[test]
fn cannot_reserve_staked_balance() {
	// Checks that a bonded account cannot reserve balance from free balance
	ExtBuilder::default().build().execute_with(|| {
		// Confirm account 11 is stashed
		assert_eq!(Staking::bonded(&11), Some(10));
		// Confirm account 11 has some free balance
		assert_eq!(Balances::free_balance(11), 1000);
		// Confirm account 11 (via controller 10) is totally staked
Gavin Wood's avatar
Gavin Wood committed
		assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).own, 1000);
		// Confirm account 11 cannot transfer as a result
		assert_noop!(
			Balances::reserve(&11, 1),
Gavin Wood's avatar
Gavin Wood committed
			BalancesError::<Test, _>::LiquidityRestrictions

		// Give account 11 extra free balance
		let _ = Balances::make_free_balance_be(&11, 10000);
		// Confirm account 11 can now reserve balance
		assert_ok!(Balances::reserve(&11, 1));
#[test]
fn reward_destination_works() {
	// Rewards go to the correct destination as determined in Payee
	ExtBuilder::default().nominate(false).build().execute_with(|| {
		// Check that account 11 is a validator
Gavin Wood's avatar
Gavin Wood committed
		assert!(Session::validators().contains(&11));
		// Check the balance of the validator account
		assert_eq!(Balances::free_balance(10), 1);
		// Check the balance of the stash account
		assert_eq!(Balances::free_balance(11), 1000);
		// Check how much is at stake
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000,
			active: 1000,
			unlocking: vec![],
Gavin Wood's avatar
Gavin Wood committed
			last_reward: None,
		// Compute total payout now for whole duration as other parameter won't change
		let total_payout_0 = current_total_payout_for_duration(3000);
		assert!(total_payout_0 > 100); // Test is meaningful if reward something
thiolliere's avatar
thiolliere committed
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);
		start_era(1);
Gavin Wood's avatar
Gavin Wood committed
		mock::make_all_reward_payment(0);

		// Check that RewardDestination is Staked (default)
		assert_eq!(Staking::payee(&11), RewardDestination::Staked);
		// Check that reward went to the stash account of validator
		assert_eq!(Balances::free_balance(11), 1000 + total_payout_0);
		// Check that amount at stake increased accordingly
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000 + total_payout_0,
			active: 1000 + total_payout_0,
			unlocking: vec![],
Gavin Wood's avatar
Gavin Wood committed
			last_reward: Some(0),

		//Change RewardDestination to Stash
		<Payee<Test>>::insert(&11, RewardDestination::Stash);
		// Compute total payout now for whole duration as other parameter won't change
		let total_payout_1 = current_total_payout_for_duration(3000);
		assert!(total_payout_1 > 100); // Test is meaningful if reward something
thiolliere's avatar
thiolliere committed
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);
		start_era(2);
Gavin Wood's avatar
Gavin Wood committed
		mock::make_all_reward_payment(1);

		// Check that RewardDestination is Stash
		assert_eq!(Staking::payee(&11), RewardDestination::Stash);
		// Check that reward went to the stash account
		assert_eq!(Balances::free_balance(11), 1000 + total_payout_0 + total_payout_1);
		// Record this value
		let recorded_stash_balance = 1000 + total_payout_0 + total_payout_1;
		// Check that amount at stake is NOT increased
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000 + total_payout_0,
			active: 1000 + total_payout_0,
			unlocking: vec![],
Gavin Wood's avatar
Gavin Wood committed
			last_reward: Some(1),

		// Change RewardDestination to Controller
		<Payee<Test>>::insert(&11, RewardDestination::Controller);
		// Check controller balance
		assert_eq!(Balances::free_balance(10), 1);
		// Compute total payout now for whole duration as other parameter won't change
		let total_payout_2 = current_total_payout_for_duration(3000);
		assert!(total_payout_2 > 100); // Test is meaningful if reward something
thiolliere's avatar
thiolliere committed
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);
		start_era(3);
Gavin Wood's avatar
Gavin Wood committed
		mock::make_all_reward_payment(2);
		// Check that RewardDestination is Controller
		assert_eq!(Staking::payee(&11), RewardDestination::Controller);
		// Check that reward went to the controller account
		assert_eq!(Balances::free_balance(10), 1 + total_payout_2);
		// Check that amount at stake is NOT increased
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000 + total_payout_0,
			active: 1000 + total_payout_0,
			unlocking: vec![],
Gavin Wood's avatar
Gavin Wood committed
			last_reward: Some(2),
		// Check that amount in staked account is NOT increased.
		assert_eq!(Balances::free_balance(11), recorded_stash_balance);
fn validator_payment_prefs_work() {
	// Test that validator preferences are correctly honored
	// Note: unstake threshold is being directly tested in slashing tests.
	// This test will focus on validator payment.
	ExtBuilder::default().build().execute_with(|| {
Gavin Wood's avatar
Gavin Wood committed
		let commission = Perbill::from_percent(40);
		<Validators<Test>>::insert(&11, ValidatorPrefs {
Gavin Wood's avatar
Gavin Wood committed
			commission: commission.clone(),
Gavin Wood's avatar
Gavin Wood committed
		// Reward controller so staked ratio doesn't change.
		<Payee<Test>>::insert(&11, RewardDestination::Controller);
		<Payee<Test>>::insert(&101, RewardDestination::Controller);

		start_era(1);
		mock::make_all_reward_payment(0);

		let balance_era_1_10 = Balances::total_balance(&10);
		let balance_era_1_100 = Balances::total_balance(&100);

		// Compute total payout now for whole duration as other parameter won't change
Gavin Wood's avatar
Gavin Wood committed
		let total_payout_1 = current_total_payout_for_duration(3000);
		assert!(total_payout_1 > 100); // Test is meaningful if reward something
		let exposure_1 = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
thiolliere's avatar
thiolliere committed
		<Module<Test>>::reward_by_ids(vec![(11, 1)]);
Gavin Wood's avatar
Gavin Wood committed
		start_era(2);
		mock::make_all_reward_payment(1);
Gavin Wood's avatar
Gavin Wood committed
		let taken_cut = commission * total_payout_1;
		let shared_cut = total_payout_1 - taken_cut;
		let reward_of_10 = shared_cut * exposure_1.own / exposure_1.total + taken_cut;
		let reward_of_100 = shared_cut * exposure_1.others[0].value / exposure_1.total;
		assert_eq_error_rate!(Balances::total_balance(&10), balance_era_1_10 + reward_of_10, 2);
		assert_eq_error_rate!(Balances::total_balance(&100), balance_era_1_100 + reward_of_100, 2);
Gavin Wood's avatar
Gavin Wood committed
		check_exposure_all(Staking::active_era().unwrap().index);
		check_nominator_all(Staking::active_era().unwrap().index);
#[test]
fn bond_extra_works() {
	// Tests that extra `free_balance` in the stash can be added to stake
	// NOTE: this tests only verifies `StakingLedger` for correct updates
	// See `bond_extra_and_withdraw_unbonded_works` for more details and updates on `Exposure`.
	ExtBuilder::default().build().execute_with(|| {
		// Check that account 10 is a validator
		assert!(<Validators<Test>>::contains_key(11));
		// Check that account 10 is bonded to account 11
		assert_eq!(Staking::bonded(&11), Some(10));
		// Check how much is at stake
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000,
			active: 1000,
			unlocking: vec![],
Gavin Wood's avatar
Gavin Wood committed
			last_reward: None,

		// Give account 11 some large free balance greater than total
		let _ = Balances::make_free_balance_be(&11, 1000000);

		// Call the bond_extra function from controller, add only 100
		assert_ok!(Staking::bond_extra(Origin::signed(11), 100));
		// There should be 100 more `total` and `active` in the ledger
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000 + 100,
			active: 1000 + 100,
			unlocking: vec![],
Gavin Wood's avatar
Gavin Wood committed
			last_reward: None,

		// Call the bond_extra function with a large number, should handle it
		assert_ok!(Staking::bond_extra(Origin::signed(11), u64::max_value()));
		// The full amount of the funds should now be in the total and active
		assert_eq!(Staking::ledger(&10), Some(StakingLedger {
			stash: 11,
			total: 1000000,
			active: 1000000,