Skip to content
tests.rs 143 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::BadOrigin,
};
use sp_staking::offence::OffenceDetails;
use frame_support::{
	assert_ok, assert_noop, StorageMap,
	traits::{Currency, ReservableCurrency, OnInitialize},
Gavin Wood's avatar
Gavin Wood committed
use pallet_balances::Error as BalancesError;
#[test]
fn force_unstake_works() {
	ExtBuilder::default().build_and_execute(|| {
		// 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_and_execute(|| {
		// 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),
			Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![], claimed_rewards: vec![] })
		// Account 20 controls the stash from account 21, which is 200 * balance_factor units
		assert_eq!(
			Staking::ledger(&20),
			Some(StakingLedger { stash: 21, total: 1000, active: 1000, unlocking: vec![], claimed_rewards: vec![] })
		// Account 1 does not control any stash
		assert_eq!(Staking::ledger(&1), None);

		// ValidatorPrefs are default
		assert_eq_uvec!(<Validators<Test>>::iter().collect::<Vec<_>>(), vec![
			(31, ValidatorPrefs::default()),
			(21, ValidatorPrefs::default()),
			(11, ValidatorPrefs::default())
		assert_eq!(
			Staking::ledger(100),
			Some(StakingLedger { stash: 101, total: 500, active: 500, unlocking: vec![], claimed_rewards: vec![] })
		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 total stake = 1125 + 1375
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);
#[test]
fn change_controller_works() {
	ExtBuilder::default().build_and_execute(|| {
		// 10 and 11 are bonded as stash controller.
		assert_eq!(Staking::bonded(&11), Some(10));

		// 10 can control 11 who is initially a validator.
		assert_ok!(Staking::chill(Origin::signed(10)));

		// change controller
		assert_ok!(Staking::set_controller(Origin::signed(11), 5));
		assert_eq!(Staking::bonded(&11), Some(5));
		mock::start_era(1);
		// 10 is no longer in control.
		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
	// * `RewardRemainder::on_unbalanced` is called
	// * Check that nominators are also rewarded
	ExtBuilder::default().nominate(true).build_and_execute(|| {
		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);
		assert_eq!(mock::REWARD_REMAINDER_UNBALANCED.with(|v| *v.borrow()), 7050);
Loading full blame...