mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 06:08:00 +00:00
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:
@@ -4667,6 +4667,49 @@ fn migrate_era_should_handle_errors_2() {
|
||||
});
|
||||
}
|
||||
|
||||
#[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::Trait>::DbWeight::get().reads_writes(4, 1);
|
||||
assert_eq!(Staking::on_offence(&[], &[Perbill::from_percent(50)], 0), Ok(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::Trait>::DbWeight::get().reads_writes(4, 1)
|
||||
+ <Test as frame_system::Trait>::DbWeight::get().reads_writes(4, 5);
|
||||
|
||||
let offenders: Vec<OffenceDetails<<Test as frame_system::Trait>::AccountId, pallet_session::historical::IdentificationTuple<Test>>>
|
||||
= (1..10).map(|i|
|
||||
OffenceDetails {
|
||||
offender: (i, Staking::eras_stakers(Staking::active_era().unwrap().index, i)),
|
||||
reporters: vec![],
|
||||
}
|
||||
).collect();
|
||||
assert_eq!(Staking::on_offence(&offenders, &[Perbill::from_percent(50)], 0), Ok(n_offence_unapplied_weight));
|
||||
|
||||
// On Offence with one offenders, Applied
|
||||
let one_offender = [
|
||||
OffenceDetails {
|
||||
offender: (11, Staking::eras_stakers(Staking::active_era().unwrap().index, 11)),
|
||||
reporters: vec![1],
|
||||
},
|
||||
];
|
||||
|
||||
let n = 1; // Number of offenders
|
||||
let rw = 3 + 3 * n; // rw reads and writes
|
||||
let one_offence_unapplied_weight = <Test as frame_system::Trait>::DbWeight::get().reads_writes(4, 1)
|
||||
+ <Test as frame_system::Trait>::DbWeight::get().reads_writes(rw, rw)
|
||||
// One `slash_cost`
|
||||
+ <Test as frame_system::Trait>::DbWeight::get().reads_writes(6, 5)
|
||||
// `slash_cost` * nominators (1)
|
||||
+ <Test as frame_system::Trait>::DbWeight::get().reads_writes(6, 5)
|
||||
// `reward_cost` * reporters (1)
|
||||
+ <Test as frame_system::Trait>::DbWeight::get().reads_writes(2, 2);
|
||||
|
||||
assert_eq!(Staking::on_offence(&one_offender, &[Perbill::from_percent(50)], 0), Ok(one_offence_unapplied_weight));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn on_initialize_weight_is_correct() {
|
||||
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
|
||||
|
||||
Reference in New Issue
Block a user