offences: make fn slash_fraction non-static (#11956)

* offences: make fn slash_fraction non-static

* Bastifmt (inline variable)
This commit is contained in:
Andronik
2022-08-02 20:49:03 +02:00
committed by GitHub
parent 63f847c24f
commit 0cda69d34a
8 changed files with 21 additions and 23 deletions
+3 -3
View File
@@ -958,12 +958,12 @@ impl<Offender: Clone> Offence<Offender> for UnresponsivenessOffence<Offender> {
self.session_index
}
fn slash_fraction(offenders: u32, validator_set_count: u32) -> Perbill {
fn slash_fraction(&self, offenders: u32) -> Perbill {
// the formula is min((3 * (k - (n / 10 + 1))) / n, 1) * 0.07
// basically, 10% can be offline with no slash, but after that, it linearly climbs up to 7%
// when 13/30 are offline (around 5% when 1/3 are offline).
if let Some(threshold) = offenders.checked_sub(validator_set_count / 10 + 1) {
let x = Perbill::from_rational(3 * threshold, validator_set_count);
if let Some(threshold) = offenders.checked_sub(self.validator_set_count / 10 + 1) {
let x = Perbill::from_rational(3 * threshold, self.validator_set_count);
x.saturating_mul(Perbill::from_percent(7))
} else {
Perbill::default()
+6 -4
View File
@@ -36,22 +36,24 @@ use sp_runtime::{
#[test]
fn test_unresponsiveness_slash_fraction() {
let dummy_offence =
UnresponsivenessOffence { session_index: 0, validator_set_count: 50, offenders: vec![()] };
// A single case of unresponsiveness is not slashed.
assert_eq!(UnresponsivenessOffence::<()>::slash_fraction(1, 50), Perbill::zero());
assert_eq!(dummy_offence.slash_fraction(1), Perbill::zero());
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(5, 50),
dummy_offence.slash_fraction(5),
Perbill::zero(), // 0%
);
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(7, 50),
dummy_offence.slash_fraction(7),
Perbill::from_parts(4200000), // 0.42%
);
// One third offline should be punished around 5%.
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(17, 50),
dummy_offence.slash_fraction(17),
Perbill::from_parts(46200000), // 4.62%
);
}