Migrate pallet-staking to pallet attribute macro (#9083)

* Migrate staking pallet to pallet attribute macro.

* HistoryDepth default value.

* Make all calls public.

* Update frame/staking/src/lib.rs

* remove externalities again

* Update lib.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Shaun Wang
2021-06-12 12:22:40 +12:00
committed by GitHub
parent 68833498c6
commit 0869fdf9aa
10 changed files with 731 additions and 552 deletions
+36 -36
View File
@@ -17,7 +17,7 @@
//! Tests for the module.
use super::*;
use super::{*, Event};
use mock::*;
use sp_runtime::{
assert_eq_error_rate,
@@ -25,7 +25,7 @@ use sp_runtime::{
};
use sp_staking::offence::OffenceDetails;
use frame_support::{
assert_ok, assert_noop, StorageMap,
assert_ok, assert_noop,
traits::{Currency, ReservableCurrency, OnInitialize},
weights::{extract_actual_weight, GetDispatchInfo},
};
@@ -187,10 +187,10 @@ fn rewards_should_work() {
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)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 50)]);
<Pallet<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)]);
<Pallet<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());
@@ -227,7 +227,7 @@ fn rewards_should_work() {
);
assert_eq!(
*mock::staking_events().last().unwrap(),
RawEvent::EraPayout(0, total_payout_0, maximum_payout - total_payout_0)
Event::EraPayout(0, total_payout_0, maximum_payout - total_payout_0)
);
mock::make_all_reward_payment(0);
@@ -253,7 +253,7 @@ fn rewards_should_work() {
assert_eq_error_rate!(Balances::total_balance(&101), init_balance_101, 2);
assert_eq_uvec!(Session::validators(), vec![11, 21]);
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
// Compute total payout now for whole duration as other parameter won't change
let total_payout_1 = current_total_payout_for_duration(reward_time_per_era());
@@ -265,7 +265,7 @@ fn rewards_should_work() {
);
assert_eq!(
*mock::staking_events().last().unwrap(),
RawEvent::EraPayout(1, total_payout_1, maximum_payout - total_payout_1)
Event::EraPayout(1, total_payout_1, maximum_payout - total_payout_1)
);
mock::make_all_reward_payment(1);
@@ -482,8 +482,8 @@ fn nominating_and_rewards_should_work() {
// the total reward for era 0
let total_payout_0 = current_total_payout_for_duration(reward_time_per_era());
<Module<Test>>::reward_by_ids(vec![(41, 1)]);
<Module<Test>>::reward_by_ids(vec![(31, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(41, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(31, 1)]);
mock::start_active_era(1);
@@ -524,8 +524,8 @@ fn nominating_and_rewards_should_work() {
// the total reward for era 1
let total_payout_1 = current_total_payout_for_duration(reward_time_per_era());
<Module<Test>>::reward_by_ids(vec![(21, 2)]);
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(21, 2)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
mock::start_active_era(2);
@@ -779,7 +779,7 @@ fn forcing_new_era_works() {
assert_eq!(active_era(), 1);
// no era change.
ForceEra::put(Forcing::ForceNone);
ForceEra::<Test>::put(Forcing::ForceNone);
start_session(4);
assert_eq!(active_era(), 1);
@@ -795,7 +795,7 @@ fn forcing_new_era_works() {
// back to normal.
// this immediately starts a new session.
ForceEra::put(Forcing::NotForcing);
ForceEra::<Test>::put(Forcing::NotForcing);
start_session(8);
assert_eq!(active_era(), 1);
@@ -803,7 +803,7 @@ fn forcing_new_era_works() {
start_session(9);
assert_eq!(active_era(), 2);
// forceful change
ForceEra::put(Forcing::ForceAlways);
ForceEra::<Test>::put(Forcing::ForceAlways);
start_session(10);
assert_eq!(active_era(), 2);
@@ -815,10 +815,10 @@ fn forcing_new_era_works() {
assert_eq!(active_era(), 4);
// just one forceful change
ForceEra::put(Forcing::ForceNew);
ForceEra::<Test>::put(Forcing::ForceNew);
start_session(13);
assert_eq!(active_era(), 5);
assert_eq!(ForceEra::get(), Forcing::NotForcing);
assert_eq!(ForceEra::<Test>::get(), Forcing::NotForcing);
start_session(14);
assert_eq!(active_era(), 6);
@@ -917,7 +917,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(reward_time_per_era());
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
mock::start_active_era(1);
mock::make_all_reward_payment(0);
@@ -940,7 +940,7 @@ fn reward_destination_works() {
// Compute total payout now for whole duration as other parameter won't change
let total_payout_1 = current_total_payout_for_duration(reward_time_per_era());
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
mock::start_active_era(2);
mock::make_all_reward_payment(1);
@@ -968,7 +968,7 @@ fn reward_destination_works() {
// Compute total payout now for whole duration as other parameter won't change
let total_payout_2 = current_total_payout_for_duration(reward_time_per_era());
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
mock::start_active_era(3);
mock::make_all_reward_payment(2);
@@ -1015,7 +1015,7 @@ fn validator_payment_prefs_work() {
// Compute total payout now for whole duration as other parameter won't change
let total_payout_1 = current_total_payout_for_duration(reward_time_per_era());
let exposure_1 = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
mock::start_active_era(2);
mock::make_all_reward_payment(1);
@@ -1508,8 +1508,8 @@ fn reward_to_stake_works() {
// Compute total payout now for whole duration as other parameter won't change
let total_payout_0 = current_total_payout_for_duration(reward_time_per_era());
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Module<Test>>::reward_by_ids(vec![(21, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(21, 1)]);
// New era --> rewards are paid --> stakes are changed
mock::start_active_era(1);
@@ -2009,10 +2009,10 @@ fn reward_from_authorship_event_handler_works() {
assert_eq!(<pallet_authorship::Pallet<Test>>::author(), 11);
<Module<Test>>::note_author(11);
<Module<Test>>::note_uncle(21, 1);
<Pallet<Test>>::note_author(11);
<Pallet<Test>>::note_uncle(21, 1);
// Rewarding the same two times works.
<Module<Test>>::note_uncle(11, 1);
<Pallet<Test>>::note_uncle(11, 1);
// Not mandatory but must be coherent with rewards
assert_eq_uvec!(Session::validators(), vec![11, 21]);
@@ -2035,13 +2035,13 @@ fn add_reward_points_fns_works() {
// Not mandatory but must be coherent with rewards
assert_eq_uvec!(Session::validators(), vec![21, 11]);
<Module<Test>>::reward_by_ids(vec![
<Pallet<Test>>::reward_by_ids(vec![
(21, 1),
(11, 1),
(11, 1),
]);
<Module<Test>>::reward_by_ids(vec![
<Pallet<Test>>::reward_by_ids(vec![
(21, 1),
(11, 1),
(11, 1),
@@ -2084,7 +2084,7 @@ fn era_is_always_same_length() {
assert_eq!(Staking::eras_start_session_index(current_era()).unwrap(), session_per_era * 2u32);
let session = Session::current_index();
ForceEra::put(Forcing::ForceNew);
ForceEra::<Test>::put(Forcing::ForceNew);
advance_session();
advance_session();
assert_eq!(current_era(), 3);
@@ -2992,13 +2992,13 @@ fn claim_reward_at_the_last_era_and_no_double_claim_and_invalid_claim() {
Payee::<Test>::insert(11, RewardDestination::Controller);
Payee::<Test>::insert(101, RewardDestination::Controller);
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
// Compute total payout now for whole duration as other parameter won't change
let total_payout_0 = current_total_payout_for_duration(reward_time_per_era());
mock::start_active_era(1);
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
// Change total issuance in order to modify total payout
let _ = Balances::deposit_creating(&999, 1_000_000_000);
// Compute total payout now for whole duration as other parameter won't change
@@ -3007,7 +3007,7 @@ fn claim_reward_at_the_last_era_and_no_double_claim_and_invalid_claim() {
mock::start_active_era(2);
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
// Change total issuance in order to modify total payout
let _ = Balances::deposit_creating(&999, 1_000_000_000);
// Compute total payout now for whole duration as other parameter won't change
@@ -3168,7 +3168,7 @@ fn test_max_nominator_rewarded_per_validator_and_cant_steal_someone_else_reward(
}
mock::start_active_era(1);
<Module<Test>>::reward_by_ids(vec![(11, 1)]);
<Pallet<Test>>::reward_by_ids(vec![(11, 1)]);
// compute and ensure the reward amount is greater than zero.
let _ = current_total_payout_for_duration(reward_time_per_era());
@@ -3832,7 +3832,7 @@ fn do_not_die_when_active_is_ed() {
fn on_finalize_weight_is_nonzero() {
ExtBuilder::default().build_and_execute(|| {
let on_finalize_weight = <Test as frame_system::Config>::DbWeight::get().reads(1);
assert!(Staking::on_initialize(1) >= on_finalize_weight);
assert!(<Staking as Hooks<u64>>::on_initialize(1) >= on_finalize_weight);
})
}
@@ -3954,7 +3954,7 @@ mod election_data_provider {
assert_eq!(staking_events().len(), 1);
assert_eq!(
*staking_events().last().unwrap(),
RawEvent::StakingElection
Event::StakingElection
);
for b in 21..45 {
@@ -3968,7 +3968,7 @@ mod election_data_provider {
assert_eq!(staking_events().len(), 3);
assert_eq!(
*staking_events().last().unwrap(),
RawEvent::StakingElection
Event::StakingElection
);
})
}