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
+19 -7
View File
@@ -32,7 +32,7 @@ use sp_runtime::traits::{IdentityLookup, BlakeTwo256};
use sp_core::H256;
use frame_support::{
impl_outer_origin, impl_outer_event, parameter_types, StorageMap, StorageDoubleMap,
weights::Weight,
weights::{Weight, constants::{WEIGHT_PER_SECOND, RocksDbWeight}},
};
use frame_system as system;
@@ -45,20 +45,23 @@ pub struct OnOffenceHandler;
thread_local! {
pub static ON_OFFENCE_PERBILL: RefCell<Vec<Perbill>> = RefCell::new(Default::default());
pub static CAN_REPORT: RefCell<bool> = RefCell::new(true);
pub static OFFENCE_WEIGHT: RefCell<Weight> = RefCell::new(Default::default());
}
impl<Reporter, Offender> offence::OnOffenceHandler<Reporter, Offender> for OnOffenceHandler {
impl<Reporter, Offender>
offence::OnOffenceHandler<Reporter, Offender, Weight> for OnOffenceHandler
{
fn on_offence(
_offenders: &[OffenceDetails<Reporter, Offender>],
slash_fraction: &[Perbill],
_offence_session: SessionIndex,
) -> Result<(), ()> {
if <Self as offence::OnOffenceHandler<Reporter, Offender>>::can_report() {
) -> Result<Weight, ()> {
if <Self as offence::OnOffenceHandler<Reporter, Offender, Weight>>::can_report() {
ON_OFFENCE_PERBILL.with(|f| {
*f.borrow_mut() = slash_fraction.to_vec();
});
Ok(())
Ok(OFFENCE_WEIGHT.with(|w| *w.borrow()))
} else {
Err(())
}
@@ -79,12 +82,16 @@ pub fn with_on_offence_fractions<R, F: FnOnce(&mut Vec<Perbill>) -> R>(f: F) ->
})
}
pub fn set_offence_weight(new: Weight) {
OFFENCE_WEIGHT.with(|w| *w.borrow_mut() = new);
}
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Runtime;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
@@ -101,7 +108,7 @@ impl frame_system::Trait for Runtime {
type Event = TestEvent;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
type DbWeight = RocksDbWeight;
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ();
type MaximumExtrinsicWeight = MaximumBlockWeight;
@@ -114,10 +121,15 @@ impl frame_system::Trait for Runtime {
type OnKilledAccount = ();
}
parameter_types! {
pub const OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
}
impl Trait for Runtime {
type Event = TestEvent;
type IdentificationTuple = u64;
type OnOffenceHandler = OnOffenceHandler;
type WeightSoftLimit = OffencesWeightSoftLimit;
}
mod offences {