Improve offline slashing calculation (#1371)

* srml: apply slashing for all offline reports at once

* srml: add regression test for slashing overflow
This commit is contained in:
André Silva
2019-01-10 15:03:15 +00:00
committed by Robert Habermeier
parent a13fda2e83
commit 3555ef425d
2 changed files with 89 additions and 37 deletions
+39
View File
@@ -557,3 +557,42 @@ fn slash_value_calculation_does_not_overflow() {
Staking::on_offline_validator(10, 100);
});
}
#[test]
fn next_slash_value_calculation_does_not_overflow() {
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
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);
assert_eq!(Balances::total_balance(&10), 1);
assert_eq!(Staking::intentions(), vec![10, 20]);
assert_eq!(Staking::offline_slash_grace(), 0);
// set validator preferences so the validator doesn't back down after
// slashing.
<ValidatorPreferences<Test>>::insert(10, ValidatorPrefs {
unstake_threshold: u32::max_value(),
validator_payment: 0,
});
// we have enough balance to cover the last slash before overflow
Balances::set_free_balance(&10, u64::max_value());
assert_eq!(Balances::total_balance(&10), u64::max_value());
// the balance type is u64, so after slashing 64 times,
// the slash value should have overflowed. add a couple extra for
// good measure with the slash grace.
trait TypeEq {}
impl<A> TypeEq for (A, A) {}
fn assert_type_eq<A: TypeEq>() {}
assert_type_eq::<(u64, <Test as balances::Trait>::Balance)>();
// the total slash value should overflow the balance type
// therefore the total validator balance should be slashed
Staking::on_offline_validator(10, 100);
assert_eq!(Balances::total_balance(&10), 0);
});
}