"substrate/frame/staking/src/mock.rs" did not exist on "60e5011c72589e39373c0088552ea0571f70e9d3"
Newer
Older
// This file is part of Substrate.
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use sp_runtime::{
assert_eq_error_rate, traits::BadOrigin,
};
assert_ok, assert_noop, StorageMap,
traits::{Currency, ReservableCurrency, OnInitialize, OnFinalize},
Benjamin Kampmann
committed
use substrate_test_utils::assert_eq_uvec;
#[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));
// Adds 2 slashing spans
add_slash(&11);
// Cant transfer
assert_noop!(
Balances::transfer(Origin::signed(11), 1, 10),
);
// Force unstake requires root.
assert_noop!(Staking::force_unstake(Origin::signed(11), 11, 2), BadOrigin);
// Force unstake needs correct number of slashing spans (for weight calculation)
assert_noop!(Staking::force_unstake(Origin::root(), 11, 0), Error::<Test>::IncorrectSlashingSpans);
// We now force them to unstake
assert_ok!(Staking::force_unstake(Origin::root(), 11, 2));
// No longer bonded.
assert_eq!(Staking::bonded(&11), None);
// Transfer works.
assert_ok!(Balances::transfer(Origin::signed(11), 1, 10));
});
}
#[test]
fn kill_stash_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));
// Adds 2 slashing spans
add_slash(&11);
// Only can kill a stash account
assert_noop!(Staking::kill_stash(&12, 0), Error::<Test>::NotStash);
// Respects slashing span count
assert_noop!(Staking::kill_stash(&11, 0), Error::<Test>::IncorrectSlashingSpans);
// Correct inputs, everything works
assert_ok!(Staking::kill_stash(&11, 2));
// No longer bonded.
assert_eq!(Staking::bonded(&11), None);
});
}
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);
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]);
Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
Exposure {
total: 1125,
own: 1000,
others: vec![ IndividualExposure { who: 101, value: 125 }]
},
Staking::eras_stakers(Staking::active_era().unwrap().index, 21),
Exposure {
total: 1375,
own: 1000,
others: vec![ IndividualExposure { who: 101, value: 375 }]
},
// initial total stake = 1125 + 1375
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
// Account 10 has `balance_factor` free balance
assert_eq!(Balances::free_balance(10), 1);
assert_eq!(Balances::free_balance(10), 1);
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)));
assert_ok!(Staking::set_controller(Origin::signed(11), 5));
assert_eq!(Staking::bonded(&11), Some(5));
mock::start_active_era(1);
assert_noop!(
Staking::validate(Origin::signed(10), ValidatorPrefs::default()),
);
assert_ok!(Staking::validate(Origin::signed(5), ValidatorPrefs::default()));
})
}
#[test]
fn rewards_should_work() {
ExtBuilder::default().nominate(true).session_per_era(3).build_and_execute(|| {
let init_balance_10 = Balances::total_balance(&10);
let init_balance_11 = Balances::total_balance(&11);
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);
// Set payees
Payee::<Test>::insert(11, RewardDestination::Controller);
Payee::<Test>::insert(21, RewardDestination::Controller);
Payee::<Test>::insert(101, RewardDestination::Controller);
<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.
<Module<Test>>::reward_by_ids(vec![(21, 50)]);
// Compute total payout now for whole duration of the session.
let total_payout_0 = current_total_payout_for_duration(reward_time_per_era());
let maximum_payout = maximum_payout_for_duration(reward_time_per_era());
assert_eq!(Balances::total_balance(&10), init_balance_10);
assert_eq!(Balances::total_balance(&11), init_balance_11);
assert_eq!(Balances::total_balance(&20), init_balance_20);
Loading full blame...