Fixes NPoS reward, along with tests, cleanups & docs corrections (#3595)

* Minor cleanups and tests

* Add another test

* fix

* Fix tests

* Fix tests

* Update srml/staking/src/tests.rs

Co-Authored-By: thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Gavin Wood
2019-09-11 15:11:08 +02:00
committed by GitHub
parent ccc73da289
commit d908f32c30
6 changed files with 161 additions and 115 deletions
+62 -56
View File
@@ -132,7 +132,7 @@ fn rewards_should_work() {
// Init some balances
let _ = Balances::make_free_balance_be(&2, 500);
let delay = 1;
let delay = 1000;
let init_balance_2 = Balances::total_balance(&2);
let init_balance_10 = Balances::total_balance(&10);
let init_balance_11 = Balances::total_balance(&11);
@@ -160,7 +160,7 @@ fn rewards_should_work() {
let mut block = 3; // Block 3 => Session 1 => Era 0
System::set_block_number(block);
Timestamp::set_timestamp(block*5); // on time.
Timestamp::set_timestamp(block * 5000); // on time.
Session::on_initialize(System::block_number());
assert_eq!(Staking::current_era(), 0);
assert_eq!(Session::current_index(), 1);
@@ -172,7 +172,7 @@ fn rewards_should_work() {
<Module<Test>>::reward_by_ids(vec![(1001, 10_000)]);
// Compute total payout now for whole duration as other parameter won't change
let total_payout = current_total_payout_for_duration(9 * 5);
let total_payout = current_total_payout_for_duration(9 * 5 * 1000);
assert!(total_payout > 10); // Test is meaningful if reward something
// No reward yet
@@ -182,21 +182,21 @@ fn rewards_should_work() {
block = 6; // Block 6 => Session 2 => Era 0
System::set_block_number(block);
Timestamp::set_timestamp(block*5 + delay); // a little late.
Timestamp::set_timestamp(block * 5000 + delay); // a little late.
Session::on_initialize(System::block_number());
assert_eq!(Staking::current_era(), 0);
assert_eq!(Session::current_index(), 2);
block = 9; // Block 9 => Session 3 => Era 1
System::set_block_number(block);
Timestamp::set_timestamp(block*5); // back to being on time. no delays
Timestamp::set_timestamp(block * 5000); // back to being on time. no delays
Session::on_initialize(System::block_number());
assert_eq!(Staking::current_era(), 1);
assert_eq!(Session::current_index(), 3);
// 11 validator has 2/3 of the total rewards and half half for it and its nominator
assert_eq!(Balances::total_balance(&2), init_balance_2 + total_payout/3);
assert_eq!(Balances::total_balance(&10), init_balance_10 + total_payout/3);
// 11 validator has 2 / 3 of the total rewards and half half for it and its nominator
assert_eq!(Balances::total_balance(&2), init_balance_2 + total_payout / 3);
assert_eq!(Balances::total_balance(&10), init_balance_10 + total_payout / 3);
assert_eq!(Balances::total_balance(&11), init_balance_11);
});
}
@@ -216,7 +216,7 @@ fn multi_era_reward_should_work() {
assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller));
// Compute now as other parameter won't change
let total_payout_0 = current_total_payout_for_duration(3);
let total_payout_0 = current_total_payout_for_duration(3000);
assert!(total_payout_0 > 10); // Test is meaningfull if reward something
dbg!(<Module<Test>>::slot_stake());
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
@@ -231,7 +231,7 @@ fn multi_era_reward_should_work() {
start_session(4);
let total_payout_1 = current_total_payout_for_duration(3);
let total_payout_1 = current_total_payout_for_duration(3000);
assert!(total_payout_1 > 10); // Test is meaningfull if reward something
<Module<Test>>::reward_by_ids(vec![(11, 101)]);
@@ -436,7 +436,7 @@ fn nominating_and_rewards_should_work() {
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(3);
let total_payout_0 = current_total_payout_for_duration(3000);
assert!(total_payout_0 > 100); // Test is meaningfull if reward something
<Module<Test>>::reward_by_ids(vec![(41, 1)]);
<Module<Test>>::reward_by_ids(vec![(31, 1)]);
@@ -449,8 +449,8 @@ fn nominating_and_rewards_should_work() {
assert_eq_uvec!(validator_controllers(), vec![20, 10]);
// OLD validators must have already received some rewards.
assert_eq!(Balances::total_balance(&40), 1 + total_payout_0/2);
assert_eq!(Balances::total_balance(&30), 1 + total_payout_0/2);
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.
@@ -511,7 +511,7 @@ fn nominating_and_rewards_should_work() {
assert_eq!(Staking::stakers(41).total, 0);
// the total reward for era 1
let total_payout_1 = current_total_payout_for_duration(3);
let total_payout_1 = current_total_payout_for_duration(3000);
assert!(total_payout_1 > 100); // Test is meaningfull if reward something
<Module<Test>>::reward_by_ids(vec![(41, 10)]); // must be no-op
<Module<Test>>::reward_by_ids(vec![(31, 10)]); // must be no-op
@@ -523,28 +523,34 @@ fn nominating_and_rewards_should_work() {
// nothing else will happen, era ends and rewards are paid again,
// it is expected that nominators will also be paid. See below
let payout_for_10 = total_payout_1/3;
let payout_for_20 = 2*total_payout_1/3;
let payout_for_10 = total_payout_1 / 3;
let payout_for_20 = 2 * total_payout_1 / 3;
if cfg!(feature = "equalize") {
// Nominator 2: has [400/2000 ~ 1/5 from 10] + [600/2000 ~ 3/10 from 20]'s reward.
assert_eq!(Balances::total_balance(&2), initial_balance + payout_for_10/5 + payout_for_20*3/10 - 1);
// Nominator 4: has [400/2000 ~ 1/5 from 20] + [600/2000 ~ 3/10 from 10]'s reward.
assert_eq!(Balances::total_balance(&4), initial_balance + payout_for_20/5 + payout_for_10*3/10);
// Nominator 2: has [400 / 2000 ~ 1 / 5 from 10] + [600 / 2000 ~ 3 / 10 from 20]'s reward.
assert_eq!(Balances::total_balance(&2), initial_balance + payout_for_10 / 5 + payout_for_20 * 3 / 10);
// Nominator 4: has [400 / 2000 ~ 1 / 5 from 20] + [600 / 2000 ~ 3 / 10 from 10]'s reward.
assert_eq!(Balances::total_balance(&4), initial_balance + payout_for_20 / 5 + payout_for_10 * 3 / 10);
// Validator 10: got 1000 / 2000 external stake.
assert_eq!(Balances::total_balance(&10), initial_balance + payout_for_10/2);
assert_eq!(Balances::total_balance(&10), initial_balance + payout_for_10 / 2);
// Validator 20: got 1000 / 2000 external stake.
assert_eq!(Balances::total_balance(&20), initial_balance + payout_for_20/2);
assert_eq!(Balances::total_balance(&20), initial_balance + payout_for_20 / 2);
} else {
// Nominator 2: has [400/1800 ~ 2/9 from 10] + [600/2200 ~ 3/11 from 20]'s reward. ==> 2/9 + 3/11
assert_eq!(Balances::total_balance(&2), initial_balance + (2*payout_for_10/9 + 3*payout_for_20/11) - 2);
// Nominator 4: has [400/1800 ~ 2/9 from 10] + [600/2200 ~ 3/11 from 20]'s reward. ==> 2/9 + 3/11
assert_eq!(Balances::total_balance(&4), initial_balance + (2*payout_for_10/9 + 3*payout_for_20/11) - 2);
// Nominator 2: has [400 / 1800 ~ 2 / 9 from 10] + [600 / 2200 ~ 3 / 11 from 20]'s reward. ==> 2 / 9 + 3 / 11
assert_eq!(
Balances::total_balance(&2),
initial_balance + (2 * payout_for_10 / 9 + 3 * payout_for_20 / 11) - 2
);
// Nominator 4: has [400 / 1800 ~ 2 / 9 from 10] + [600 / 2200 ~ 3 / 11 from 20]'s reward. ==> 2 / 9 + 3 / 11
assert_eq!(
Balances::total_balance(&4),
initial_balance + (2 * payout_for_10 / 9 + 3 * payout_for_20 / 11) - 2
);
// Validator 10: got 800 / 1800 external stake => 8/18 =? 4/9 => Validator's share = 5/9
assert_eq!(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!(Balances::total_balance(&20), initial_balance + 5*payout_for_20/11);
// Validator 10: got 800 / 1800 external stake => 8 / 18 =? 4 / 9 => Validator's share = 5 / 9
assert_eq!(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!(Balances::total_balance(&20), initial_balance + 5*payout_for_20 / 11 + 1);
}
check_exposure_all();
@@ -576,7 +582,7 @@ fn nominators_also_get_slashed() {
assert_ok!(Staking::bond(Origin::signed(1), 2, nominator_stake, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(2), vec![20, 10]));
let total_payout = current_total_payout_for_duration(3);
let total_payout = current_total_payout_for_duration(3000);
assert!(total_payout > 100); // Test is meaningfull if reward something
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
@@ -824,7 +830,7 @@ fn reward_destination_works() {
}));
// Compute total payout now for whole duration as other parameter won't change
let total_payout_0 = current_total_payout_for_duration(3);
let total_payout_0 = current_total_payout_for_duration(3000);
assert!(total_payout_0 > 100); // Test is meaningfull if reward something
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
@@ -846,7 +852,7 @@ fn reward_destination_works() {
<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(3);
let total_payout_1 = current_total_payout_for_duration(3000);
assert!(total_payout_1 > 100); // Test is meaningfull if reward something
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
@@ -873,7 +879,7 @@ fn reward_destination_works() {
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(3);
let total_payout_2 = current_total_payout_for_duration(3000);
assert!(total_payout_2 > 100); // Test is meaningfull if reward something
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
@@ -926,7 +932,7 @@ fn validator_payment_prefs_work() {
});
// Compute total payout now for whole duration as other parameter won't change
let total_payout_0 = current_total_payout_for_duration(3);
let total_payout_0 = current_total_payout_for_duration(3000);
assert!(total_payout_0 > 100); // Test is meaningfull if reward something
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
@@ -935,11 +941,11 @@ fn validator_payment_prefs_work() {
// whats left to be shared is the sum of 3 rounds minus the validator's cut.
let shared_cut = total_payout_0 - validator_cut;
// Validator's payee is Staked account, 11, reward will be paid here.
assert_eq!(Balances::total_balance(&11), stash_initial_balance + shared_cut/2 + validator_cut);
assert_eq!(Balances::total_balance(&11), stash_initial_balance + shared_cut / 2 + validator_cut);
// Controller account will not get any reward.
assert_eq!(Balances::total_balance(&10), 1);
// Rest of the reward will be shared and paid to the nominator in stake.
assert_eq!(Balances::total_balance(&2), 500 + shared_cut/2);
assert_eq!(Balances::total_balance(&2), 500 + shared_cut / 2);
check_exposure_all();
check_nominator_all();
@@ -1140,7 +1146,7 @@ fn slot_stake_is_least_staked_validator_and_exposure_defines_maximum_punishment(
<Ledger<Test>>::insert(&20, StakingLedger { stash: 22, total: 69, active: 69, unlocking: vec![] });
// Compute total payout now for whole duration as other parameter won't change
let total_payout_0 = current_total_payout_for_duration(3);
let total_payout_0 = current_total_payout_for_duration(3000);
assert!(total_payout_0 > 100); // Test is meaningfull if reward something
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Module<Test>>::reward_by_ids(vec![(21, 1)]);
@@ -1149,14 +1155,14 @@ fn slot_stake_is_least_staked_validator_and_exposure_defines_maximum_punishment(
start_era(1);
// -- new balances + reward
assert_eq!(Staking::stakers(&11).total, 1000 + total_payout_0/2);
assert_eq!(Staking::stakers(&21).total, 69 + total_payout_0/2);
assert_eq!(Staking::stakers(&11).total, 1000 + total_payout_0 / 2);
assert_eq!(Staking::stakers(&21).total, 69 + total_payout_0 / 2);
let _11_balance = Balances::free_balance(&11);
assert_eq!(_11_balance, 1000 + total_payout_0/2);
assert_eq!(_11_balance, 1000 + total_payout_0 / 2);
// -- slot stake should also be updated.
assert_eq!(Staking::slot_stake(), 69 + total_payout_0/2);
assert_eq!(Staking::slot_stake(), 69 + total_payout_0 / 2);
check_exposure_all();
check_nominator_all();
@@ -1621,7 +1627,7 @@ fn bond_with_little_staked_value_bounded_by_slot_stake() {
assert_ok!(Staking::bond(Origin::signed(1), 2, 1, RewardDestination::Controller));
assert_ok!(Staking::validate(Origin::signed(2), ValidatorPrefs::default()));
let total_payout_0 = current_total_payout_for_duration(3);
let total_payout_0 = current_total_payout_for_duration(3000);
assert!(total_payout_0 > 100); // Test is meaningfull if reward something
reward_all_elected();
start_era(1);
@@ -1632,11 +1638,11 @@ fn bond_with_little_staked_value_bounded_by_slot_stake() {
assert_eq!(Staking::slot_stake(), 1);
// Old ones are rewarded.
assert_eq!(Balances::free_balance(&10), init_balance_10 + total_payout_0/3);
assert_eq!(Balances::free_balance(&10), init_balance_10 + total_payout_0 / 3);
// no rewards paid to 2. This was initial election.
assert_eq!(Balances::free_balance(&2), init_balance_2);
let total_payout_1 = current_total_payout_for_duration(3);
let total_payout_1 = current_total_payout_for_duration(3000);
assert!(total_payout_1 > 100); // Test is meaningfull if reward something
reward_all_elected();
start_era(2);
@@ -1644,8 +1650,8 @@ fn bond_with_little_staked_value_bounded_by_slot_stake() {
assert_eq_uvec!(validator_controllers(), vec![20, 10, 2]);
assert_eq!(Staking::slot_stake(), 1);
assert_eq!(Balances::free_balance(&2), init_balance_2 + total_payout_1/3);
assert_eq!(Balances::free_balance(&10), init_balance_10 + total_payout_0/3 + total_payout_1/3);
assert_eq!(Balances::free_balance(&2), init_balance_2 + total_payout_1 / 3);
assert_eq!(Balances::free_balance(&10), init_balance_10 + total_payout_0 / 3 + total_payout_1 / 3);
check_exposure_all();
check_nominator_all();
});
@@ -1750,8 +1756,8 @@ fn phragmen_should_not_overflow_validators() {
bond_validator(2, u64::max_value());
bond_validator(4, u64::max_value());
bond_nominator(6, u64::max_value()/2, vec![3, 5]);
bond_nominator(8, u64::max_value()/2, vec![3, 5]);
bond_nominator(6, u64::max_value() / 2, vec![3, 5]);
bond_nominator(8, u64::max_value() / 2, vec![3, 5]);
start_era(1);
@@ -1773,8 +1779,8 @@ fn phragmen_should_not_overflow_nominators() {
let _ = Staking::chill(Origin::signed(10));
let _ = Staking::chill(Origin::signed(20));
bond_validator(2, u64::max_value()/2);
bond_validator(4, u64::max_value()/2);
bond_validator(2, u64::max_value() / 2);
bond_validator(4, u64::max_value() / 2);
bond_nominator(6, u64::max_value(), vec![3, 5]);
bond_nominator(8, u64::max_value(), vec![3, 5]);
@@ -1943,9 +1949,9 @@ fn reward_from_authorship_event_handler_works() {
assert_eq!(<CurrentElected<Test>>::get(), vec![21, 11]);
// 21 is rewarded as an uncle producer
// 11 is rewarded as a block procuder and uncle referencer and uncle producer
assert_eq!(CurrentEraRewards::get().rewards, vec![1, 20+2*3 + 1]);
assert_eq!(CurrentEraRewards::get().total, 28);
// 11 is rewarded as a block producer and uncle referencer and uncle producer
assert_eq!(CurrentEraPointsEarned::get().individual, vec![1, 20 + 2 * 3 + 1]);
assert_eq!(CurrentEraPointsEarned::get().total, 28);
})
}
@@ -1972,8 +1978,8 @@ fn add_reward_points_fns_works() {
(11, 1),
]);
assert_eq!(CurrentEraRewards::get().rewards, vec![2, 4]);
assert_eq!(CurrentEraRewards::get().total, 6);
assert_eq!(CurrentEraPointsEarned::get().individual, vec![2, 4]);
assert_eq!(CurrentEraPointsEarned::get().total, 6);
})
}