Offences Weight for OnInitialize (#5961)

* Weight accounting for on_offence.

* Try to compute weight.

* Guesstimate upper bounds on db read/writes for slashing

* greater than or equal to

* add new trait

* Update mock.rs

* Add basic weight test

* one more test

* Update frame/staking/src/lib.rs

Co-authored-by: thiolliere <gui.thiolliere@gmail.com>

* Update frame/staking/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Add test for offences queue

Co-authored-by: Tomasz Drwięga <tomasz@parity.io>
Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Shawn Tabrizi
2020-05-21 14:00:24 +02:00
committed by GitHub
parent 0ddd5cc278
commit ac049a97be
9 changed files with 219 additions and 38 deletions
+46 -1
View File
@@ -22,7 +22,7 @@
use super::*;
use crate::mock::{
Offences, System, Offence, TestEvent, KIND, new_test_ext, with_on_offence_fractions,
offence_reports, set_can_report,
offence_reports, set_can_report, set_offence_weight,
};
use sp_runtime::Perbill;
use frame_support::traits::OnInitialize;
@@ -265,3 +265,48 @@ fn should_queue_and_resubmit_rejected_offence() {
assert_eq!(Offences::deferred_offences().len(), 0);
})
}
#[test]
fn weight_soft_limit_is_used() {
new_test_ext().execute_with(|| {
set_can_report(false);
// Only 2 can fit in one block
set_offence_weight(<mock::Runtime as Trait>::WeightSoftLimit::get() / 2);
// Queue 3 offences
// #1
let offence = Offence {
validator_set_count: 5,
time_slot: 42,
offenders: vec![5],
};
Offences::report_offence(vec![], offence).unwrap();
// #2
let offence = Offence {
validator_set_count: 5,
time_slot: 62,
offenders: vec![5],
};
Offences::report_offence(vec![], offence).unwrap();
// #3
let offence = Offence {
validator_set_count: 5,
time_slot: 72,
offenders: vec![5],
};
Offences::report_offence(vec![], offence).unwrap();
// 3 are queued
assert_eq!(Offences::deferred_offences().len(), 3);
// Allow reporting
set_can_report(true);
Offences::on_initialize(3);
// Two are completed, one is left in the queue
assert_eq!(Offences::deferred_offences().len(), 1);
Offences::on_initialize(4);
// All are done now
assert_eq!(Offences::deferred_offences().len(), 0);
})
}