Newer
Older
#[test]
fn slashing_performed_according_exposure() {
// This test checks that slashing is performed according the exposure (or more precisely,
// historical exposure), not the current balance.
ExtBuilder::default().build().execute_with(|| {
assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).own, 1000);
// Handle an offence with a historical exposure.
&[OffenceDetails {
offender: (
11,
Exposure {
total: 500,
own: 500,
others: vec![],
},
),
reporters: vec![],
}],
&[Perbill::from_percent(50)],
);
// The stash account should be slashed for 250 (50% of 500).
assert_eq!(Balances::free_balance(11), 1000 - 250);
#[test]
fn slash_in_old_span_does_not_deselect() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
assert!(<Validators<Test>>::contains_key(11));
on_offence_now(
&[OffenceDetails {
offender: (
11,
Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
),
reporters: vec![],
}],
&[Perbill::from_percent(0)],
);
assert_eq!(Staking::force_era(), Forcing::ForceNew);
assert!(!<Validators<Test>>::contains_key(11));
start_era(2);
Staking::validate(Origin::signed(10), Default::default()).unwrap();
assert_eq!(Staking::force_era(), Forcing::NotForcing);
assert!(<Validators<Test>>::contains_key(11));
start_era(3);
// this staker is in a new slashing span now, having re-registered after
// their prior slash.
on_offence_in_era(
&[OffenceDetails {
offender: (
11,
Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
),
reporters: vec![],
}],
&[Perbill::from_percent(0)],
1,
);
// not for zero-slash.
assert_eq!(Staking::force_era(), Forcing::NotForcing);
assert!(<Validators<Test>>::contains_key(11));
on_offence_in_era(
&[OffenceDetails {
offender: (
11,
Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
),
reporters: vec![],
}],
// NOTE: A 100% slash here would clean up the account, causing de-registration.
&[Perbill::from_percent(95)],
1,
);
// or non-zero.
assert_eq!(Staking::force_era(), Forcing::NotForcing);
assert!(<Validators<Test>>::contains_key(11));
assert_ledger_consistent(11);
});
}
#[test]
fn reporters_receive_their_slice() {
// This test verifies that the reporters of the offence receive their slice from the slashed
// amount.
ExtBuilder::default().build().execute_with(|| {
// The reporters' reward is calculated from the total exposure.
assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, initial_balance);
&[OffenceDetails {
offender: (
11,
Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
),
reporters: vec![1, 2],
}],
&[Perbill::from_percent(50)],
);
// F1 * (reward_proportion * slash - 0)
// 50% * (10% * initial_balance / 2)
let reward = (initial_balance / 20) / 2;
let reward_each = reward / 2; // split into two pieces.
assert_eq!(Balances::free_balance(1), 10 + reward_each);
assert_eq!(Balances::free_balance(2), 20 + reward_each);
assert_ledger_consistent(11);
fn subsequent_reports_in_same_span_pay_out_less() {
// This test verifies that the reporters of the offence receive their slice from the slashed
// amount.
ExtBuilder::default().build().execute_with(|| {
// The reporters' reward is calculated from the total exposure.
let initial_balance = 1125;
assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).total, initial_balance);
on_offence_now(
&[OffenceDetails {
offender: (
11,
Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
),
reporters: vec![1],
}],
&[Perbill::from_percent(20)],
);
// F1 * (reward_proportion * slash - 0)
// 50% * (10% * initial_balance * 20%)
let reward = (initial_balance / 5) / 20;
assert_eq!(Balances::free_balance(1), 10 + reward);
on_offence_now(
&[OffenceDetails {
offender: (
11,
Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
),
reporters: vec![1],
}],
&[Perbill::from_percent(50)],
);
let prior_payout = reward;
// F1 * (reward_proportion * slash - prior_payout)
// 50% * (10% * (initial_balance / 2) - prior_payout)
let reward = ((initial_balance / 20) - prior_payout) / 2;
assert_eq!(Balances::free_balance(1), 10 + prior_payout + reward);
assert_ledger_consistent(11);
});
}
#[test]
fn invulnerables_are_not_slashed() {
// For invulnerable validators no slashing is performed.
ExtBuilder::default().invulnerables(vec![11]).build().execute_with(|| {
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(21), 2000);
let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 21);
let initial_balance = Staking::slashable_balance_of(&21);
let nominator_balances: Vec<_> = exposure.others
.iter().map(|o| Balances::free_balance(&o.who)).collect();
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
OffenceDetails {
offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)),
reporters: vec![],
},
],
&[Perbill::from_percent(50), Perbill::from_percent(20)],
);
// The validator 11 hasn't been slashed, but 21 has been.
assert_eq!(Balances::free_balance(11), 1000);
// 2000 - (0.2 * initial_balance)
assert_eq!(Balances::free_balance(21), 2000 - (2 * initial_balance / 10));
// ensure that nominators were slashed as well.
for (initial_balance, other) in nominator_balances.into_iter().zip(exposure.others) {
assert_eq!(
Balances::free_balance(&other.who),
initial_balance - (2 * other.value / 10),
);
}
assert_ledger_consistent(11);
assert_ledger_consistent(21);
}
#[test]
fn dont_slash_if_fraction_is_zero() {
// Don't slash if the fraction is zero.
ExtBuilder::default().build().execute_with(|| {
assert_eq!(Balances::free_balance(11), 1000);
&[OffenceDetails {
offender: (
11,
Staking::eras_stakers(Staking::active_era().unwrap().index, 11),
),
reporters: vec![],
}],
&[Perbill::from_percent(0)],
);
// The validator hasn't been slashed. The new era is not forced.
assert_eq!(Balances::free_balance(11), 1000);
assert_ledger_consistent(11);
});
}
#[test]
fn only_slash_for_max_in_era() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(Balances::free_balance(11), 1000);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
],
&[Perbill::from_percent(50)],
);
// The validator has been slashed and has been force-chilled.
assert_eq!(Balances::free_balance(11), 500);
assert_eq!(Staking::force_era(), Forcing::ForceNew);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
],
&[Perbill::from_percent(25)],
);
// The validator has not been slashed additionally.
assert_eq!(Balances::free_balance(11), 500);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
],
&[Perbill::from_percent(60)],
);
// The validator got slashed 10% more.
assert_eq!(Balances::free_balance(11), 400);
assert_ledger_consistent(11);
})
}
#[test]
fn garbage_collection_after_slashing() {
ExtBuilder::default().existential_deposit(2).build().execute_with(|| {
assert_eq!(Balances::free_balance(11), 256_000);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
assert_eq!(Balances::free_balance(11), 256_000 - 25_600);
assert!(<Staking as crate::Store>::SlashingSpans::get(&11).is_some());
assert_eq!(<Staking as crate::Store>::SpanSlash::get(&(11, 0)).amount_slashed(), &25_600);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
],
&[Perbill::from_percent(100)],
);
// validator and nominator slash in era are garbage-collected by era change,
// so we don't test those here.
assert_eq!(Balances::free_balance(11), 0);
assert_eq!(Balances::total_balance(&11), 0);
assert_ok!(Staking::reap_stash(Origin::NONE, 11));
assert!(<Staking as crate::Store>::SlashingSpans::get(&11).is_none());
assert_eq!(<Staking as crate::Store>::SpanSlash::get(&(11, 0)).amount_slashed(), &0);
})
}
#[test]
fn garbage_collection_on_window_pruning() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(11), 1000);
let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
assert_eq!(Balances::free_balance(101), 2000);
let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
assert_eq!(Balances::free_balance(11), 900);
assert_eq!(Balances::free_balance(101), 2000 - (nominated_value / 10));
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
assert!(<Staking as crate::Store>::ValidatorSlashInEra::get(&now, &11).is_some());
assert!(<Staking as crate::Store>::NominatorSlashInEra::get(&now, &101).is_some());
// + 1 because we have to exit the bonding window.
for era in (0..(BondingDuration::get() + 1)).map(|offset| offset + now + 1) {
assert!(<Staking as crate::Store>::ValidatorSlashInEra::get(&now, &11).is_some());
assert!(<Staking as crate::Store>::NominatorSlashInEra::get(&now, &101).is_some());
start_era(era);
}
assert!(<Staking as crate::Store>::ValidatorSlashInEra::get(&now, &11).is_none());
assert!(<Staking as crate::Store>::NominatorSlashInEra::get(&now, &101).is_none());
})
}
#[test]
fn slashing_nominators_by_span_max() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
start_era(2);
start_era(3);
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(21), 2000);
assert_eq!(Balances::free_balance(101), 2000);
assert_eq!(Staking::slashable_balance_of(&21), 1000);
let exposure_11 = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
let exposure_21 = Staking::eras_stakers(Staking::active_era().unwrap().index, 21);
assert_eq!(Balances::free_balance(101), 2000);
let nominated_value_11 = exposure_11.others.iter().find(|o| o.who == 101).unwrap().value;
let nominated_value_21 = exposure_21.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_in_era(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
2,
);
assert_eq!(Balances::free_balance(11), 900);
let slash_1_amount = Perbill::from_percent(10) * nominated_value_11;
assert_eq!(Balances::free_balance(101), 2000 - slash_1_amount);
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
let expected_spans = vec![
slashing::SlashingSpan { index: 1, start: 4, length: None },
slashing::SlashingSpan { index: 0, start: 0, length: Some(4) },
];
let get_span = |account| <Staking as crate::Store>::SlashingSpans::get(&account).unwrap();
assert_eq!(
get_span(11).iter().collect::<Vec<_>>(),
expected_spans,
);
assert_eq!(
get_span(101).iter().collect::<Vec<_>>(),
expected_spans,
);
// second slash: higher era, higher value, same span.
on_offence_in_era(
&[
OffenceDetails {
offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)),
reporters: vec![],
},
],
&[Perbill::from_percent(30)],
3,
);
// 11 was not further slashed, but 21 and 101 were.
assert_eq!(Balances::free_balance(11), 900);
assert_eq!(Balances::free_balance(21), 1700);
let slash_2_amount = Perbill::from_percent(30) * nominated_value_21;
assert!(slash_2_amount > slash_1_amount);
// only the maximum slash in a single span is taken.
assert_eq!(Balances::free_balance(101), 2000 - slash_2_amount);
// third slash: in same era and on same validator as first, higher
// in-era value, but lower slash value than slash 2.
on_offence_in_era(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
],
&[Perbill::from_percent(20)],
2,
);
// 11 was further slashed, but 21 and 101 were not.
assert_eq!(Balances::free_balance(11), 800);
assert_eq!(Balances::free_balance(21), 1700);
let slash_3_amount = Perbill::from_percent(20) * nominated_value_21;
assert!(slash_3_amount < slash_2_amount);
assert!(slash_3_amount > slash_1_amount);
// only the maximum slash in a single span is taken.
assert_eq!(Balances::free_balance(101), 2000 - slash_2_amount);
});
}
#[test]
fn slashes_are_summed_across_spans() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
start_era(2);
start_era(3);
assert_eq!(Balances::free_balance(21), 2000);
assert_eq!(Staking::slashable_balance_of(&21), 1000);
let get_span = |account| <Staking as crate::Store>::SlashingSpans::get(&account).unwrap();
on_offence_now(
&[
OffenceDetails {
offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
let expected_spans = vec![
slashing::SlashingSpan { index: 1, start: 4, length: None },
slashing::SlashingSpan { index: 0, start: 0, length: Some(4) },
];
assert_eq!(get_span(21).iter().collect::<Vec<_>>(), expected_spans);
assert_eq!(Balances::free_balance(21), 1900);
// 21 has been force-chilled. re-signal intent to validate.
Staking::validate(Origin::signed(20), Default::default()).unwrap();
start_era(4);
assert_eq!(Staking::slashable_balance_of(&21), 900);
on_offence_now(
&[
OffenceDetails {
offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
let expected_spans = vec![
slashing::SlashingSpan { index: 2, start: 5, length: None },
slashing::SlashingSpan { index: 1, start: 4, length: Some(1) },
slashing::SlashingSpan { index: 0, start: 0, length: Some(4) },
];
assert_eq!(get_span(21).iter().collect::<Vec<_>>(), expected_spans);
assert_eq!(Balances::free_balance(21), 1810);
});
}
#[test]
fn deferred_slashes_are_deferred() {
ExtBuilder::default().slash_defer_duration(2).build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(11), 1000);
let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
assert_eq!(Balances::free_balance(101), 2000);
let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(101), 2000);
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(101), 2000);
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(101), 2000);
// at the start of era 4, slashes from era 1 are processed,
// after being deferred for at least 2 full eras.
start_era(4);
assert_eq!(Balances::free_balance(11), 900);
assert_eq!(Balances::free_balance(101), 2000 - (nominated_value / 10));
})
}
#[test]
fn remove_deferred() {
ExtBuilder::default().slash_defer_duration(2).build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(11), 1000);
let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
assert_eq!(Balances::free_balance(101), 2000);
let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_now(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(101), 2000);
start_era(2);
on_offence_in_era(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(15)],
1,
);
// fails if empty
assert_noop!(
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![]),
Error::<Test>::EmptyTargets
);
assert_ok!(Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![0]));
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(101), 2000);
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(101), 2000);
// at the start of era 4, slashes from era 1 are processed,
// after being deferred for at least 2 full eras.
start_era(4);
// the first slash for 10% was cancelled, so no effect.
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(101), 2000);
start_era(5);
let slash_10 = Perbill::from_percent(10);
let slash_15 = Perbill::from_percent(15);
let initial_slash = slash_10 * nominated_value;
let total_slash = slash_15 * nominated_value;
let actual_slash = total_slash - initial_slash;
// 5% slash (15 - 10) processed now.
assert_eq!(Balances::free_balance(11), 950);
assert_eq!(Balances::free_balance(101), 2000 - actual_slash);
})
}
#[test]
fn remove_multi_deferred() {
ExtBuilder::default().slash_defer_duration(2).build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(11), 1000);
let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
assert_eq!(Balances::free_balance(101), 2000);
on_offence_now(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
on_offence_now(
&[
OffenceDetails {
offender: (21, Staking::eras_stakers(Staking::active_era().unwrap().index, 21)),
reporters: vec![],
}
],
&[Perbill::from_percent(10)],
);
on_offence_now(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(25)],
);
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
on_offence_now(
&[
OffenceDetails {
offender: (42, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(25)],
);
on_offence_now(
&[
OffenceDetails {
offender: (69, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(25)],
);
assert_eq!(<Staking as Store>::UnappliedSlashes::get(&1).len(), 5);
// fails if list is not sorted
assert_noop!(
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![2, 0, 4]),
Error::<Test>::NotSortedAndUnique
);
// fails if list is not unique
assert_noop!(
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![0, 2, 2]),
Error::<Test>::NotSortedAndUnique
);
// fails if bad index
assert_noop!(
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![1, 2, 3, 4, 5]),
Error::<Test>::InvalidSlashIndex
);
assert_ok!(Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![0, 2, 4]));
let slashes = <Staking as Store>::UnappliedSlashes::get(&1);
assert_eq!(slashes.len(), 2);
println!("Slashes: {:?}", slashes);
assert_eq!(slashes[0].validator, 21);
assert_eq!(slashes[1].validator, 42);
#[test]
fn slash_kicks_validators_not_nominators() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(11), 1000);
let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
assert_eq!(Balances::free_balance(101), 2000);
let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_now(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
assert_eq!(Balances::free_balance(11), 900);
assert_eq!(Balances::free_balance(101), 2000 - (nominated_value / 10));
// This is the best way to check that the validator was chilled; `get` will
// return default value.
for (stash, _) in <Staking as Store>::Validators::iter() {
assert!(stash != 11);
}
let nominations = <Staking as Store>::Nominators::get(&101).unwrap();
// and make sure that the vote will be ignored even if the validator
// re-registers.
let last_slash = <Staking as Store>::SlashingSpans::get(&11).unwrap().last_nonzero_slash();
assert!(nominations.submitted_in < last_slash);
});
}
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
#[test]
fn claim_reward_at_the_last_era_and_no_double_claim_and_invalid_claim() {
// should check that:
// * rewards get paid until history_depth for both validators and nominators
// * an invalid era to claim doesn't update last_reward
// * double claim of one era fails
ExtBuilder::default().nominate(true).build().execute_with(|| {
let init_balance_10 = Balances::total_balance(&10);
let init_balance_100 = Balances::total_balance(&100);
let part_for_10 = Perbill::from_rational_approximation::<u32>(1000, 1125);
let part_for_100 = Perbill::from_rational_approximation::<u32>(125, 1125);
// Check state
Payee::<Test>::insert(11, RewardDestination::Controller);
Payee::<Test>::insert(101, RewardDestination::Controller);
<Module<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(3000);
assert!(total_payout_0 > 10); // Test is meaningful if reward something
start_era(1);
<Module<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
let total_payout_1 = current_total_payout_for_duration(3000);
assert!(total_payout_1 > 10); // Test is meaningful if reward something
assert!(total_payout_1 != total_payout_0);
start_era(2);
<Module<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
let total_payout_2 = current_total_payout_for_duration(3000);
assert!(total_payout_2 > 10); // Test is meaningful if reward something
assert!(total_payout_2 != total_payout_0);
assert!(total_payout_2 != total_payout_1);
start_era(Staking::history_depth() + 1);
let active_era = Staking::active_era().unwrap().index;
// This is the latest planned era in staking, not the active era
let current_era = Staking::current_era().unwrap();
// Last kept is 1:
assert!(current_era - Staking::history_depth() == 1);
assert_noop!(
Staking::payout_validator(Origin::signed(10), 0),
// Fail: Era out of history
Error::<Test>::InvalidEraToReward
);
assert_ok!(Staking::payout_validator(Origin::signed(10), 1));
assert_ok!(Staking::payout_validator(Origin::signed(10), 2));
assert_noop!(
Staking::payout_validator(Origin::signed(10), 2),
// Fail: Double claim
Error::<Test>::InvalidEraToReward
);
assert_noop!(
Staking::payout_validator(Origin::signed(10), active_era),
// Fail: Era not finished yet
Error::<Test>::InvalidEraToReward
);
assert_noop!(
Staking::payout_nominator(Origin::signed(100), 0, vec![(11, 0)]),
// Fail: Era out of history
Error::<Test>::InvalidEraToReward
);
assert_ok!(Staking::payout_nominator(Origin::signed(100), 1, vec![(11, 0)]));
assert_ok!(Staking::payout_nominator(Origin::signed(100), 2, vec![(11, 0)]));
assert_noop!(
Staking::payout_nominator(Origin::signed(100), 2, vec![(11, 0)]),
// Fail: Double claim
Error::<Test>::InvalidEraToReward
);
assert_noop!(
Staking::payout_nominator(Origin::signed(100), active_era, vec![(11, 0)]),
// Fail: Era not finished yet
Error::<Test>::InvalidEraToReward
);
// Era 0 can't be rewarded anymore and current era can't be rewarded yet
// only era 1 and 2 can be rewarded.
assert_eq!(
Balances::total_balance(&10),
init_balance_10 + part_for_10 * (total_payout_1 + total_payout_2),
);
assert_eq!(
Balances::total_balance(&100),
init_balance_100 + part_for_100 * (total_payout_1 + total_payout_2),
);
});
}
#[test]
fn zero_slash_keeps_nominators() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(11), 1000);
let exposure = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
assert_eq!(Balances::free_balance(101), 2000);
on_offence_now(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(0)],
);
assert_eq!(Balances::free_balance(11), 1000);
assert_eq!(Balances::free_balance(101), 2000);
// This is the best way to check that the validator was chilled; `get` will
// return default value.
for (stash, _) in <Staking as Store>::Validators::iter() {
assert!(stash != 11);
}
let nominations = <Staking as Store>::Nominators::get(&101).unwrap();
// and make sure that the vote will not be ignored, because the slash was
// zero.
let last_slash = <Staking as Store>::SlashingSpans::get(&11).unwrap().last_nonzero_slash();
assert!(nominations.submitted_in >= last_slash);
});
}
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
#[test]
fn six_session_delay() {
ExtBuilder::default().build().execute_with(|| {
use pallet_session::SessionManager;
let val_set = Session::validators();
let init_session = Session::current_index();
let init_active_era = Staking::active_era().unwrap().index;
// pallet-session is delaying session by one, thus the next session to plan is +2.
assert_eq!(<Staking as SessionManager<_>>::new_session(init_session + 2), None);
assert_eq!(<Staking as SessionManager<_>>::new_session(init_session + 3), Some(val_set.clone()));
assert_eq!(<Staking as SessionManager<_>>::new_session(init_session + 4), None);
assert_eq!(<Staking as SessionManager<_>>::new_session(init_session + 5), None);
assert_eq!(<Staking as SessionManager<_>>::new_session(init_session + 6), Some(val_set.clone()));
<Staking as SessionManager<_>>::end_session(init_session);
<Staking as SessionManager<_>>::start_session(init_session + 1);
assert_eq!(Staking::active_era().unwrap().index, init_active_era);
<Staking as SessionManager<_>>::end_session(init_session + 1);
<Staking as SessionManager<_>>::start_session(init_session + 2);
assert_eq!(Staking::active_era().unwrap().index, init_active_era);
// Reward current era
Staking::reward_by_ids(vec![(11, 1)]);
// New active era is triggered here.
<Staking as SessionManager<_>>::end_session(init_session + 2);
<Staking as SessionManager<_>>::start_session(init_session + 3);
assert_eq!(Staking::active_era().unwrap().index, init_active_era + 1);
<Staking as SessionManager<_>>::end_session(init_session + 3);
<Staking as SessionManager<_>>::start_session(init_session + 4);
assert_eq!(Staking::active_era().unwrap().index, init_active_era + 1);
<Staking as SessionManager<_>>::end_session(init_session + 4);
<Staking as SessionManager<_>>::start_session(init_session + 5);
assert_eq!(Staking::active_era().unwrap().index, init_active_era + 1);
// Reward current era
Staking::reward_by_ids(vec![(21, 2)]);
// New active era is triggered here.
<Staking as SessionManager<_>>::end_session(init_session + 5);
<Staking as SessionManager<_>>::start_session(init_session + 6);
assert_eq!(Staking::active_era().unwrap().index, init_active_era + 2);
// That reward are correct
assert_eq!(Staking::eras_reward_points(init_active_era).total, 1);
assert_eq!(Staking::eras_reward_points(init_active_era + 1).total, 2);
});
}
#[test]
fn test_max_nominator_rewarded_per_validator_and_cant_steal_someone_else_reward() {
// Test:
// * If nominator nomination is below the $MaxNominatorRewardedPerValidator other nominator
// then the nominator can't claim its reward
// * A nominator can't claim another nominator reward
ExtBuilder::default().build().execute_with(|| {
for i in 0..=<Test as Trait>::MaxNominatorRewardedPerValidator::get() {
let stash = 10_000 + i as u64;
let controller = 20_000 + i as u64;
let balance = 10_000 + i as u64;
Balances::make_free_balance_be(&stash, balance);
assert_ok!(
Staking::bond(
Origin::signed(stash),
controller,
balance,
RewardDestination::Stash
)
);
assert_ok!(Staking::nominate(Origin::signed(controller), vec![11]));
}
mock::start_era(1);
<Module<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(3 * 1000);