Newer
Older
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
page
),
true
);
}
}
assert_eq!(Staking::claimed_rewards(14, &11), vec![0, 1]);
let last_era = 99;
let history_depth = HistoryDepth::get();
let last_reward_era = last_era - 1;
let first_claimable_reward_era = last_era - history_depth;
for i in 16..=last_era {
Staking::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());
mock::start_active_era(i);
}
// verify we clean up history as we go
for era in 0..15 {
assert_eq!(Staking::claimed_rewards(era, &11), Vec::<sp_staking::Page>::new());
}
// verify only page 0 is marked as claimed
assert_ok!(Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
first_claimable_reward_era,
0
));
assert_eq!(Staking::claimed_rewards(first_claimable_reward_era, &11), vec![0]);
// verify page 0 and 1 are marked as claimed
assert_ok!(Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
first_claimable_reward_era,
1
));
assert_eq!(Staking::claimed_rewards(first_claimable_reward_era, &11), vec![0, 1]);
// verify only page 0 is marked as claimed
assert_ok!(Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
last_reward_era,
0
));
assert_eq!(Staking::claimed_rewards(last_reward_era, &11), vec![0]);
// verify page 0 and 1 are marked as claimed
assert_ok!(Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
last_reward_era,
1
));
assert_eq!(Staking::claimed_rewards(last_reward_era, &11), vec![0, 1]);
// Out of order claims works.
assert_ok!(Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, 69, 0));
assert_eq!(Staking::claimed_rewards(69, &11), vec![0]);
assert_ok!(Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, 23, 1));
assert_eq!(Staking::claimed_rewards(23, &11), vec![1]);
assert_ok!(Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, 42, 0));
assert_eq!(Staking::claimed_rewards(42, &11), vec![0]);
});
}
#[test]
fn test_multi_page_payout_stakers_backward_compatible() {
// Test that payout_stakers work in general and that it pays the correct amount of reward.
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
let balance = 1000;
// Track the exposure of the validator and all nominators.
let mut total_exposure = balance;
// Create a validator:
bond_validator(11, balance); // Default(64)
assert_eq!(Validators::<Test>::count(), 1);
let err_weight = <Test as Config>::WeightInfo::payout_stakers_alive_staked(0);
// Create nominators, targeting stash of validators
for i in 0..100 {
let bond_amount = balance + i as Balance;
bond_nominator(1000 + i, bond_amount, vec![11]);
// with multi page reward payout, payout exposure is same as total exposure.
total_exposure += bond_amount;
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
mock::start_active_era(1);
Staking::reward_by_ids(vec![(11, 1)]);
// Since `MaxExposurePageSize = 64`, there are two pages of validator exposure.
assert_eq!(EraInfo::<Test>::get_page_count(1, &11), 2);
// compute and ensure the reward amount is greater than zero.
let payout = current_total_payout_for_duration(reward_time_per_era());
mock::start_active_era(2);
// verify the exposures are calculated correctly.
let actual_exposure_0 = EraInfo::<Test>::get_paged_exposure(1, &11, 0).unwrap();
assert_eq!(actual_exposure_0.total(), total_exposure);
assert_eq!(actual_exposure_0.own(), 1000);
assert_eq!(actual_exposure_0.others().len(), 64);
let actual_exposure_1 = EraInfo::<Test>::get_paged_exposure(1, &11, 1).unwrap();
assert_eq!(actual_exposure_1.total(), total_exposure);
// own stake is only included once in the first page
assert_eq!(actual_exposure_1.own(), 0);
assert_eq!(actual_exposure_1.others().len(), 100 - 64);
let pre_payout_total_issuance = Balances::total_issuance();
RewardOnUnbalanceWasCalled::set(false);
let controller_balance_before_p0_payout = Balances::free_balance(&11);
// Payout rewards for first exposure page
assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, 1));
// page 0 is claimed
assert_noop!(
Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, 1, 0),
Error::<Test>::AlreadyClaimed.with_weight(err_weight)
);
let controller_balance_after_p0_payout = Balances::free_balance(&11);
// verify rewards have been paid out but still some left
assert!(Balances::total_issuance() > pre_payout_total_issuance);
assert!(Balances::total_issuance() < pre_payout_total_issuance + payout);
// verify the validator has been rewarded
assert!(controller_balance_after_p0_payout > controller_balance_before_p0_payout);
// This should payout the second and last page of nominators
assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, 1));
// cannot claim any more pages
assert_noop!(
Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, 1),
Error::<Test>::AlreadyClaimed.with_weight(err_weight)
);
// verify the validator was not rewarded the second time
assert_eq!(Balances::free_balance(&11), controller_balance_after_p0_payout);
// verify all rewards have been paid out
assert_eq_error_rate!(Balances::total_issuance(), pre_payout_total_issuance + payout, 2);
assert!(RewardOnUnbalanceWasCalled::get());
// verify all nominators of validator 11 are paid out, including the validator
// Validator payout goes to controller.
assert!(Balances::free_balance(&11) > balance);
for i in 0..100 {
assert!(Balances::free_balance(&(1000 + i)) > balance + i as Balance);
}
// verify we no longer track rewards in `legacy_claimed_rewards` vec
let ledger = Staking::ledger(11.into());
Staking::ledger(11.into()).unwrap(),
StakingLedgerInspect {
stash: 11,
total: 1000,
active: 1000,
unlocking: Default::default(),
legacy_claimed_rewards: bounded_vec![]
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
// verify rewards are tracked to prevent double claims
for page in 0..EraInfo::<Test>::get_page_count(1, &11) {
assert_eq!(
EraInfo::<Test>::is_rewards_claimed_with_legacy_fallback(
1,
ledger.as_ref().unwrap(),
&11,
page
),
true
);
}
for i in 3..16 {
Staking::reward_by_ids(vec![(11, 1)]);
// compute and ensure the reward amount is greater than zero.
let payout = current_total_payout_for_duration(reward_time_per_era());
let pre_payout_total_issuance = Balances::total_issuance();
mock::start_active_era(i);
RewardOnUnbalanceWasCalled::set(false);
mock::make_all_reward_payment(i - 1);
assert_eq_error_rate!(
Balances::total_issuance(),
pre_payout_total_issuance + payout,
2
);
assert!(RewardOnUnbalanceWasCalled::get());
// verify we track rewards for each era and page
for page in 0..EraInfo::<Test>::get_page_count(i - 1, &11) {
assert_eq!(
EraInfo::<Test>::is_rewards_claimed_with_legacy_fallback(
i - 1,
Staking::ledger(11.into()).as_ref().unwrap(),
&11,
page
),
true
);
}
}
assert_eq!(Staking::claimed_rewards(14, &11), vec![0, 1]);
let last_era = 99;
let history_depth = HistoryDepth::get();
let last_reward_era = last_era - 1;
let first_claimable_reward_era = last_era - history_depth;
// compute and ensure the reward amount is greater than zero.
let _ = current_total_payout_for_duration(reward_time_per_era());
mock::start_active_era(i);
// verify we clean up history as we go
for era in 0..15 {
assert_eq!(Staking::claimed_rewards(era, &11), Vec::<sp_staking::Page>::new());
}
// verify only page 0 is marked as claimed
assert_ok!(Staking::payout_stakers(
RuntimeOrigin::signed(1337),
11,
first_claimable_reward_era
assert_eq!(Staking::claimed_rewards(first_claimable_reward_era, &11), vec![0]);
// verify page 0 and 1 are marked as claimed
assert_ok!(Staking::payout_stakers(
RuntimeOrigin::signed(1337),
11,
first_claimable_reward_era,
assert_eq!(Staking::claimed_rewards(first_claimable_reward_era, &11), vec![0, 1]);
// change order and verify only page 1 is marked as claimed
assert_ok!(Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
last_reward_era,
1
));
assert_eq!(Staking::claimed_rewards(last_reward_era, &11), vec![1]);
// verify page 0 is claimed even when explicit page is not passed
assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, last_reward_era,));
assert_eq!(Staking::claimed_rewards(last_reward_era, &11), vec![1, 0]);
// cannot claim any more pages
assert_noop!(
Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, last_reward_era),
Error::<Test>::AlreadyClaimed.with_weight(err_weight)
// Create 4 nominator pages
for i in 100..200 {
let bond_amount = balance + i as Balance;
bond_nominator(1000 + i, bond_amount, vec![11]);
}
let test_era = last_era + 1;
mock::start_active_era(test_era);
Staking::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());
mock::start_active_era(test_era + 1);
assert_ok!(Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, test_era, 2));
assert_eq!(Staking::claimed_rewards(test_era, &11), vec![2]);
assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, test_era));
assert_eq!(Staking::claimed_rewards(test_era, &11), vec![2, 0]);
// cannot claim page 2 again
assert_noop!(
Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, test_era, 2),
Error::<Test>::AlreadyClaimed.with_weight(err_weight)
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, test_era));
assert_eq!(Staking::claimed_rewards(test_era, &11), vec![2, 0, 1]);
assert_ok!(Staking::payout_stakers(RuntimeOrigin::signed(1337), 11, test_era));
assert_eq!(Staking::claimed_rewards(test_era, &11), vec![2, 0, 1, 3]);
});
}
#[test]
fn test_page_count_and_size() {
// Test that payout_stakers work in general and that it pays the correct amount of reward.
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
let balance = 1000;
// Track the exposure of the validator and all nominators.
// Create a validator:
bond_validator(11, balance); // Default(64)
assert_eq!(Validators::<Test>::count(), 1);
// Create nominators, targeting stash of validators
for i in 0..100 {
let bond_amount = balance + i as Balance;
bond_nominator(1000 + i, bond_amount, vec![11]);
}
mock::start_active_era(1);
// Since max exposure page size is 64, 2 pages of nominators are created.
assert_eq!(EraInfo::<Test>::get_page_count(1, &11), 2);
// first page has 64 nominators
assert_eq!(EraInfo::<Test>::get_paged_exposure(1, &11, 0).unwrap().others().len(), 64);
// second page has 36 nominators
assert_eq!(EraInfo::<Test>::get_paged_exposure(1, &11, 1).unwrap().others().len(), 36);
// now lets decrease page size
MaxExposurePageSize::set(32);
mock::start_active_era(2);
// now we expect 4 pages.
assert_eq!(EraInfo::<Test>::get_page_count(2, &11), 4);
// first 3 pages have 32 nominators each
assert_eq!(EraInfo::<Test>::get_paged_exposure(2, &11, 0).unwrap().others().len(), 32);
assert_eq!(EraInfo::<Test>::get_paged_exposure(2, &11, 1).unwrap().others().len(), 32);
assert_eq!(EraInfo::<Test>::get_paged_exposure(2, &11, 2).unwrap().others().len(), 32);
assert_eq!(EraInfo::<Test>::get_paged_exposure(2, &11, 3).unwrap().others().len(), 4);
// now lets decrease page size even more
MaxExposurePageSize::set(5);
mock::start_active_era(3);
// now we expect the max 20 pages (100/5).
assert_eq!(EraInfo::<Test>::get_page_count(3, &11), 20);
});
}
#[test]
fn payout_stakers_handles_basic_errors() {
// Here we will test payouts handle all errors.
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
// Consumed weight for all payout_stakers dispatches that fail
let err_weight = <Test as Config>::WeightInfo::payout_stakers_alive_staked(0);
// Same setup as the test above
let balance = 1000;
bond_validator(11, balance); // Default(64)
// Create nominators, targeting stash
for i in 0..100 {
bond_nominator(1000 + i, balance + i as Balance, vec![11]);
mock::start_active_era(1);
// compute and ensure the reward amount is greater than zero.
let _ = current_total_payout_for_duration(reward_time_per_era());
mock::start_active_era(2);
assert_noop!(
Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, 2, 0),
Error::<Test>::InvalidEraToReward.with_weight(err_weight)
);
assert_noop!(
Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 10, 1, 0),
Error::<Test>::NotStash.with_weight(err_weight)
);
let last_era = 99;
for i in 3..=last_era {
// compute and ensure the reward amount is greater than zero.
let _ = current_total_payout_for_duration(reward_time_per_era());
mock::start_active_era(i);
let history_depth = HistoryDepth::get();
let expected_last_reward_era = last_era - 1;
let expected_start_reward_era = last_era - history_depth;
// We are at era last_era=99. Given history_depth=80, we should be able
// to payout era starting from expected_start_reward_era=19 through
// expected_last_reward_era=98 (80 total eras), but not 18 or 99.
assert_noop!(
Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
expected_start_reward_era - 1,
0
),
Error::<Test>::InvalidEraToReward.with_weight(err_weight)
);
assert_noop!(
Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
expected_last_reward_era + 1,
0
),
Error::<Test>::InvalidEraToReward.with_weight(err_weight)
);
assert_ok!(Staking::payout_stakers_by_page(
expected_start_reward_era,
0
assert_ok!(Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
expected_last_reward_era,
0
));
// can call page 1
assert_ok!(Staking::payout_stakers_by_page(
expected_last_reward_era,
1
assert_noop!(
Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
expected_start_reward_era,
0
),
Error::<Test>::AlreadyClaimed.with_weight(err_weight)
);
assert_noop!(
Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
expected_last_reward_era,
0
),
Error::<Test>::AlreadyClaimed.with_weight(err_weight)
);
assert_noop!(
Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
expected_last_reward_era,
1
),
Error::<Test>::AlreadyClaimed.with_weight(err_weight)
);
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
// invalid page
assert_noop!(
Staking::payout_stakers_by_page(
RuntimeOrigin::signed(1337),
11,
expected_last_reward_era,
2
),
Error::<Test>::InvalidPage.with_weight(err_weight)
);
});
}
#[test]
fn test_commission_paid_across_pages() {
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
let balance = 1;
let commission = 50;
// Create a validator:
bond_validator(11, balance);
assert_ok!(Staking::validate(
RuntimeOrigin::signed(11),
ValidatorPrefs { commission: Perbill::from_percent(commission), blocked: false }
));
assert_eq!(Validators::<Test>::count(), 1);
// Create nominators, targeting stash of validators
for i in 0..200 {
let bond_amount = balance + i as Balance;
bond_nominator(1000 + i, bond_amount, vec![11]);
}
mock::start_active_era(1);
Staking::reward_by_ids(vec![(11, 1)]);
// Since `MaxExposurePageSize = 64`, there are four pages of validator
// exposure.
assert_eq!(EraInfo::<Test>::get_page_count(1, &11), 4);
// compute and ensure the reward amount is greater than zero.
let payout = current_total_payout_for_duration(reward_time_per_era());
mock::start_active_era(2);
let initial_balance = Balances::free_balance(&11);
// Payout rewards for first exposure page
assert_ok!(Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, 1, 0));
let controller_balance_after_p0_payout = Balances::free_balance(&11);
// some commission is paid
assert!(initial_balance < controller_balance_after_p0_payout);
// payout all pages
for i in 1..4 {
let before_balance = Balances::free_balance(&11);
assert_ok!(Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, 1, i));
let after_balance = Balances::free_balance(&11);
// some commission is paid for every page
assert!(before_balance < after_balance);
}
assert_eq_error_rate!(Balances::free_balance(&11), initial_balance + payout / 2, 1,);
});
}
#[test]
fn payout_stakers_handles_weight_refund() {
// Note: this test relies on the assumption that `payout_stakers_alive_staked` is solely used by
// `payout_stakers` to calculate the weight of each payout op.
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
let max_nom_rewarded = MaxExposurePageSize::get();
// Make sure the configured value is meaningful for our use.
assert!(max_nom_rewarded >= 4);
let half_max_nom_rewarded = max_nom_rewarded / 2;
// Sanity check our max and half max nominator quantities.
assert!(half_max_nom_rewarded > 0);
assert!(max_nom_rewarded > half_max_nom_rewarded);
let max_nom_rewarded_weight =
<Test as Config>::WeightInfo::payout_stakers_alive_staked(max_nom_rewarded);
let half_max_nom_rewarded_weight =
<Test as Config>::WeightInfo::payout_stakers_alive_staked(half_max_nom_rewarded);
let zero_nom_payouts_weight = <Test as Config>::WeightInfo::payout_stakers_alive_staked(0);
assert!(zero_nom_payouts_weight.any_gt(Weight::zero()));
assert!(half_max_nom_rewarded_weight.any_gt(zero_nom_payouts_weight));
assert!(max_nom_rewarded_weight.any_gt(half_max_nom_rewarded_weight));
let balance = 1000;
bond_validator(11, balance);
start_active_era(1);
// Reward just the validator.
Staking::reward_by_ids(vec![(11, 1)]);
// Add some `half_max_nom_rewarded` nominators who will start backing the validator in the
// next era.
for i in 0..half_max_nom_rewarded {
bond_nominator((1000 + i).into(), balance + i as Balance, vec![11]);
}
start_active_era(2);
// Collect payouts when there are no nominators
let call = TestCall::Staking(StakingCall::payout_stakers_by_page {
validator_stash: 11,
era: 1,
page: 0,
});
let info = call.get_dispatch_info();
let result = call.dispatch(RuntimeOrigin::signed(20));
assert_ok!(result);
assert_eq!(extract_actual_weight(&result, &info), zero_nom_payouts_weight);
// The validator is not rewarded in this era; so there will be zero payouts to claim for
// this era.
start_active_era(3);
// Collect payouts for an era where the validator did not receive any points.
let call = TestCall::Staking(StakingCall::payout_stakers_by_page {
validator_stash: 11,
era: 2,
page: 0,
});
let info = call.get_dispatch_info();
let result = call.dispatch(RuntimeOrigin::signed(20));
assert_ok!(result);
assert_eq!(extract_actual_weight(&result, &info), zero_nom_payouts_weight);
// Reward the validator and its nominators.
Staking::reward_by_ids(vec![(11, 1)]);
start_active_era(4);
// Collect payouts when the validator has `half_max_nom_rewarded` nominators.
let call = TestCall::Staking(StakingCall::payout_stakers_by_page {
validator_stash: 11,
era: 3,
page: 0,
});
let info = call.get_dispatch_info();
let result = call.dispatch(RuntimeOrigin::signed(20));
assert_ok!(result);
assert_eq!(extract_actual_weight(&result, &info), half_max_nom_rewarded_weight);
// Add enough nominators so that we are at the limit. They will be active nominators
// in the next era.
for i in half_max_nom_rewarded..max_nom_rewarded {
bond_nominator((1000 + i).into(), balance + i as Balance, vec![11]);
}
start_active_era(5);
// We now have `max_nom_rewarded` nominators actively nominating our validator.
// Reward the validator so we can collect for everyone in the next era.
Staking::reward_by_ids(vec![(11, 1)]);
start_active_era(6);
// Collect payouts when the validator had `half_max_nom_rewarded` nominators.
let call = TestCall::Staking(StakingCall::payout_stakers_by_page {
validator_stash: 11,
era: 5,
page: 0,
});
let info = call.get_dispatch_info();
let result = call.dispatch(RuntimeOrigin::signed(20));
assert_ok!(result);
assert_eq!(extract_actual_weight(&result, &info), max_nom_rewarded_weight);
// Try and collect payouts for an era that has already been collected.
let call = TestCall::Staking(StakingCall::payout_stakers_by_page {
validator_stash: 11,
era: 5,
page: 0,
});
let info = call.get_dispatch_info();
let result = call.dispatch(RuntimeOrigin::signed(20));
assert!(result.is_err());
// When there is an error the consumed weight == weight when there are 0 nominator payouts.
assert_eq!(extract_actual_weight(&result, &info), zero_nom_payouts_weight);
fn bond_during_era_does_not_populate_legacy_claimed_rewards() {
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
bond_validator(9, 1000);
Staking::ledger(9.into()).unwrap(),
StakingLedgerInspect {
stash: 9,
total: 1000,
active: 1000,
unlocking: Default::default(),
legacy_claimed_rewards: bounded_vec![],
mock::start_active_era(5);
bond_validator(11, 1000);
Staking::ledger(11.into()).unwrap(),
StakingLedgerInspect {
stash: 11,
total: 1000,
active: 1000,
unlocking: Default::default(),
legacy_claimed_rewards: bounded_vec![],
// make sure only era up to history depth is stored
let current_era = 99;
mock::start_active_era(current_era);
bond_validator(13, 1000);
Staking::ledger(13.into()).unwrap(),
StakingLedgerInspect {
stash: 13,
total: 1000,
active: 1000,
unlocking: Default::default(),
legacy_claimed_rewards: Default::default(),
#[test]
fn offences_weight_calculated_correctly() {
ExtBuilder::default().nominate(true).build_and_execute(|| {
// On offence with zero offenders: 4 Reads, 1 Write
let zero_offence_weight =
<Test as frame_system::Config>::DbWeight::get().reads_writes(4, 1);
assert_eq!(
Staking::on_offence(&[], &[Perbill::from_percent(50)], 0, DisableStrategy::WhenSlashed),
zero_offence_weight
);
// On Offence with N offenders, Unapplied: 4 Reads, 1 Write + 4 Reads, 5 Writes
let n_offence_unapplied_weight = <Test as frame_system::Config>::DbWeight::get()
.reads_writes(4, 1) +
<Test as frame_system::Config>::DbWeight::get().reads_writes(4, 5);
let offenders: Vec<
OffenceDetails<
<Test as frame_system::Config>::AccountId,
pallet_session::historical::IdentificationTuple<Test>,
>,
> = (1..10)
.map(|i| OffenceDetails {
offender: (i, Staking::eras_stakers(active_era(), &i)),
reporters: vec![],
})
.collect();
assert_eq!(
Staking::on_offence(
&offenders,
&[Perbill::from_percent(50)],
0,
DisableStrategy::WhenSlashed
),
n_offence_unapplied_weight
);
// On Offence with one offenders, Applied
let one_offender = [OffenceDetails {
offender: (11, Staking::eras_stakers(active_era(), &11)),
let n = 1; // Number of offenders
let rw = 3 + 3 * n; // rw reads and writes
let one_offence_unapplied_weight =
<Test as frame_system::Config>::DbWeight::get().reads_writes(4, 1)
+
<Test as frame_system::Config>::DbWeight::get().reads_writes(rw, rw)
+ <Test as frame_system::Config>::DbWeight::get().reads_writes(6, 5)
// `slash_cost` * nominators (1)
+ <Test as frame_system::Config>::DbWeight::get().reads_writes(6, 5)
// `reward_cost` * reporters (1)
+ <Test as frame_system::Config>::DbWeight::get().reads_writes(2, 2)
;
assert_eq!(
Staking::on_offence(
&one_offender,
&[Perbill::from_percent(50)],
0,
DisableStrategy::WhenSlashed{}
),
one_offence_unapplied_weight
);
#[test]
fn payout_to_any_account_works() {
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
let balance = 1000;
// Create a validator:
bond_validator(11, balance); // Default(64)
// Create a stash/controller pair
bond_nominator(1234, 100, vec![11]);
// Update payout location
assert_ok!(Staking::set_payee(RuntimeOrigin::signed(1234), RewardDestination::Account(42)));
// Reward Destination account doesn't exist
assert_eq!(Balances::free_balance(42), 0);
mock::start_active_era(1);
Staking::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());
mock::start_active_era(2);
assert_ok!(Staking::payout_stakers_by_page(RuntimeOrigin::signed(1337), 11, 1, 0));
// Payment is successful
assert!(Balances::free_balance(42) > 0);
})
}
#[test]
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
fn session_buffering_with_offset() {
// similar to live-chains, have some offset for the first session
ExtBuilder::default()
.offset(2)
.period(5)
.session_per_era(5)
.build_and_execute(|| {
assert_eq!(current_era(), 0);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 0);
start_session(1);
assert_eq!(current_era(), 0);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 1);
assert_eq!(System::block_number(), 2);
start_session(2);
assert_eq!(current_era(), 0);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 2);
assert_eq!(System::block_number(), 7);
start_session(3);
assert_eq!(current_era(), 0);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 3);
assert_eq!(System::block_number(), 12);
// active era is lagging behind by one session, because of how session module works.
start_session(4);
assert_eq!(current_era(), 1);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 4);
assert_eq!(System::block_number(), 17);
start_session(5);
assert_eq!(current_era(), 1);
assert_eq!(active_era(), 1);
assert_eq!(Session::current_index(), 5);
assert_eq!(System::block_number(), 22);
// go all the way to active 2.
start_active_era(2);
assert_eq!(current_era(), 2);
assert_eq!(active_era(), 2);
assert_eq!(Session::current_index(), 10);
});
}
#[test]
fn session_buffering_no_offset() {
// no offset, first session starts immediately
ExtBuilder::default()
.offset(0)
.period(5)
.session_per_era(5)
.build_and_execute(|| {
assert_eq!(current_era(), 0);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 0);
start_session(1);
assert_eq!(current_era(), 0);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 1);
assert_eq!(System::block_number(), 5);
start_session(2);
assert_eq!(current_era(), 0);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 2);
assert_eq!(System::block_number(), 10);
start_session(3);
assert_eq!(current_era(), 0);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 3);
assert_eq!(System::block_number(), 15);
// active era is lagging behind by one session, because of how session module works.
start_session(4);
assert_eq!(current_era(), 1);
assert_eq!(active_era(), 0);
assert_eq!(Session::current_index(), 4);
assert_eq!(System::block_number(), 20);
start_session(5);
assert_eq!(current_era(), 1);
assert_eq!(active_era(), 1);
assert_eq!(Session::current_index(), 5);
assert_eq!(System::block_number(), 25);
// go all the way to active 2.
start_active_era(2);
assert_eq!(current_era(), 2);
assert_eq!(active_era(), 2);
assert_eq!(Session::current_index(), 10);
});
}
#[test]
fn cannot_rebond_to_lower_than_ed() {
ExtBuilder::default()
.existential_deposit(11)
.balance_factor(11)
.build_and_execute(|| {
// initial stuff.
assert_eq!(
Staking::ledger(21.into()).unwrap(),
StakingLedgerInspect {
stash: 21,
total: 11 * 1000,
active: 11 * 1000,
unlocking: Default::default(),
legacy_claimed_rewards: bounded_vec![],
}
);
// unbond all of it. must be chilled first.
assert_ok!(Staking::chill(RuntimeOrigin::signed(21)));
assert_ok!(Staking::unbond(RuntimeOrigin::signed(21), 11 * 1000));
assert_eq!(
Staking::ledger(21.into()).unwrap(),
StakingLedgerInspect {
stash: 21,
total: 11 * 1000,
active: 0,
unlocking: bounded_vec![UnlockChunk { value: 11 * 1000, era: 3 }],
legacy_claimed_rewards: bounded_vec![],
}
);
// now bond a wee bit more
Staking::rebond(RuntimeOrigin::signed(21), 5),
Error::<Test>::InsufficientBond
);
})
}
#[test]
fn cannot_bond_extra_to_lower_than_ed() {
ExtBuilder::default()
.existential_deposit(11)
.balance_factor(11)
.build_and_execute(|| {
// initial stuff.
assert_eq!(
Staking::ledger(21.into()).unwrap(),
StakingLedgerInspect {
stash: 21,
total: 11 * 1000,
active: 11 * 1000,
unlocking: Default::default(),
legacy_claimed_rewards: bounded_vec![],
}
);
// unbond all of it. must be chilled first.
assert_ok!(Staking::chill(RuntimeOrigin::signed(21)));
assert_ok!(Staking::unbond(RuntimeOrigin::signed(21), 11 * 1000));
assert_eq!(
Staking::ledger(21.into()).unwrap(),
StakingLedgerInspect {
stash: 21,
total: 11 * 1000,
active: 0,
unlocking: bounded_vec![UnlockChunk { value: 11 * 1000, era: 3 }],
legacy_claimed_rewards: bounded_vec![],
}
);
// now bond a wee bit more
assert_noop!(
Staking::bond_extra(RuntimeOrigin::signed(21), 5),
Error::<Test>::InsufficientBond,
);
})
}
#[test]
fn do_not_die_when_active_is_ed() {
let ed = 10;
ExtBuilder::default()
.existential_deposit(ed)
.build_and_execute(|| {
Staking::ledger(21.into()).unwrap(),
StakingLedgerInspect {
total: 1000 * ed,
active: 1000 * ed,
unlocking: Default::default(),
legacy_claimed_rewards: bounded_vec![],