Newer
Older
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// 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,
// 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/>.
use super::*;
use runtime_io::with_externalities;
use srml_support::{assert_ok, assert_noop, EnumerableStorageMap};
use mock::{Balances, Session, Staking, System, Timestamp, Test, ExtBuilder, Origin};
use srml_support::traits::Currency;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
fn basic_setup_works() {
// Verifies initial conditions of mock
// TODO: Verify this check is comprehensive
// - Session Per Era, Session Reward
with_externalities(&mut ExtBuilder::default().build(),
|| {
assert_eq!(Staking::bonded(&11), Some(10)); // Account 11 is stashed and locked, and account 10 is the controller
assert_eq!(Staking::bonded(&21), Some(20)); // Account 21 is stashed and locked, and account 20 is the controller
assert_eq!(Staking::bonded(&1), None); // Account 1 is not a stashed
// 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![] }));
// Account 20 controls the stash from account 21, which is 200 * balance_factor units
assert_eq!(Staking::ledger(&20), Some(StakingLedger { stash: 21, total: 2000, active: 2000, unlocking: vec![] }));
// Account 1 does not control any stash
assert_eq!(Staking::ledger(&1), None);
// ValidatorPrefs are default, thus unstake_threshold is 3, other values are default for their type
assert_eq!(<Validators<Test>>::enumerate().collect::<Vec<_>>(), vec![
(20, ValidatorPrefs { unstake_threshold: 3, validator_payment: 0 }),
(10, ValidatorPrefs { unstake_threshold: 3, validator_payment: 0 })
]);
// Account 10 is exposed by 100 * balance_factor from their own stash in account 11
assert_eq!(Staking::stakers(10), Exposure { total: 1000, own: 1000, others: vec![] });
assert_eq!(Staking::stakers(20), Exposure { total: 2000, own: 2000, others: vec![] });
});
}
#[test]
fn no_offline_should_work() {
// Test the staking module works when no validators are offline
with_externalities(&mut ExtBuilder::default().build(),
|| {
// Slashing begins for validators immediately if found offline
// Account 10 has not been reported offline
// Account 10 has `balance_factor` free balance
assert_eq!(Balances::free_balance(&10), 1);
// Nothing happens to Account 10, as expected
assert_eq!(Balances::free_balance(&10), 1);
assert!(Staking::forcing_new_era().is_none());
});
}
#[test]
fn invulnerability_should_work() {
// Test that users can be invulnerable from slashing and being kicked
with_externalities(&mut ExtBuilder::default().build(),
|| {
// Make account 10 invulnerable
assert_ok!(Staking::set_invulnerables(vec![10]));
Balances::set_free_balance(&10, 70);
assert_eq!(Staking::offline_slash_grace(), 0);
assert_eq!(Staking::slash_count(&10), 0);
// Account 10 has the 70 funds we gave it above
assert_eq!(Balances::free_balance(&10), 70);
// Account 10 should be a validator
assert!(<Validators<Test>>::exists(&10));
// Set account 10 as an offline validator with a large number of reports
// Should exit early if invulnerable
Staking::on_offline_validator(10, 100);
// Show that account 10 has not been touched
assert_eq!(Balances::free_balance(&10), 70);
assert!(<Validators<Test>>::exists(&10));
// New era not being forced
assert!(Staking::forcing_new_era().is_none());
});
}
#[test]
fn offline_should_slash_and_kick() {
// Test that an offline validator gets slashed and kicked
with_externalities(&mut ExtBuilder::default().build(), || {
// Give account 10 some balance
Balances::set_free_balance(&10, 1000);
// Confirm account 10 is a validator
assert!(<Validators<Test>>::exists(&10));
// Validators get slashed immediately
// Unstake threshold is 3
assert_eq!(Staking::validators(&10).unstake_threshold, 3);
// Account 10 has not been slashed before
// Account 10 has the funds we just gave it
assert_eq!(Balances::free_balance(&10), 1000);
// Report account 10 as offline, one greater than unstake threshold
Staking::on_offline_validator(10, 4);
// Confirm user has been reported
assert_eq!(Staking::slash_count(&10), 4);
// Confirm `slot_stake` is greater than exponential punishment, else math below will be different
assert!(Staking::slot_stake() > 2_u64.pow(3) * 20);
// Confirm balance has been reduced by 2^unstake_threshold * current_offline_slash()
assert_eq!(Balances::free_balance(&10), 1000 - 2_u64.pow(3) * 20);
// Confirm account 10 has been removed as a validator
assert!(!<Validators<Test>>::exists(&10));
// A new era is forced due to slashing
assert!(Staking::forcing_new_era().is_some());
fn offline_grace_should_delay_slashing() {
// Tests that with grace, slashing is delayed
with_externalities(&mut ExtBuilder::default().build(), || {
// Initialize account 10 with balance
Balances::set_free_balance(&10, 70);
assert_eq!(Balances::free_balance(&10), 70);
// Set offline slash grace
let offline_slash_grace = 1;
assert_ok!(Staking::set_offline_slash_grace(offline_slash_grace));
assert_eq!(Staking::offline_slash_grace(), 1);
// Check unstaked_threshold is 3 (default)
let default_unstake_threshold = 3;
assert_eq!(Staking::validators(&10), ValidatorPrefs { unstake_threshold: default_unstake_threshold, validator_payment: 0 });
// Report account 10 up to the threshold
Staking::on_offline_validator(10, default_unstake_threshold as usize + offline_slash_grace as usize);
// Confirm slash count
assert_eq!(Staking::slash_count(&10), 4);
// Nothing should happen
assert_eq!(Balances::free_balance(&10), 70);
// Report account 10 one more time
Staking::on_offline_validator(10, 1);
assert_eq!(Staking::slash_count(&10), 5);
// User gets slashed
assert_eq!(Balances::free_balance(&10), 0);
// New era is forced
assert!(Staking::forcing_new_era().is_some());
});
}
#[test]
fn rewards_should_work() {
// should check that:
// 1) rewards get recorded per session
// 2) rewards get paid per Era
// 3) (bonus) Check that nominators are also rewarded
with_externalities(&mut ExtBuilder::default().build(),
|| {
let delay = 2;
// this test is only in the scope of one era. Since this variable changes
// at the last block/new era, we'll save it.
let session_reward = 10;
// Set payee to controller
assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller));
// Initial config should be correct
assert_eq!(Staking::era_length(), 9);
assert_eq!(Staking::sessions_per_era(), 3);
assert_eq!(Staking::last_era_length_change(), 0);
assert_eq!(Staking::current_era(), 0);
assert_eq!(Session::current_index(), 0);
Loading full blame...