mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 01:11:10 +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:
@@ -28,9 +28,10 @@ mod tests;
|
||||
use sp_std::vec::Vec;
|
||||
use frame_support::{
|
||||
decl_module, decl_event, decl_storage, Parameter, debug,
|
||||
traits::Get,
|
||||
weights::Weight,
|
||||
};
|
||||
use sp_runtime::{traits::Hash, Perbill};
|
||||
use sp_runtime::{traits::{Hash, Zero}, Perbill};
|
||||
use sp_staking::{
|
||||
SessionIndex,
|
||||
offence::{Offence, ReportOffence, Kind, OnOffenceHandler, OffenceDetails, OffenceError},
|
||||
@@ -58,7 +59,11 @@ pub trait Trait: frame_system::Trait {
|
||||
/// Full identification of the validator.
|
||||
type IdentificationTuple: Parameter + Ord;
|
||||
/// A handler called for every offence report.
|
||||
type OnOffenceHandler: OnOffenceHandler<Self::AccountId, Self::IdentificationTuple>;
|
||||
type OnOffenceHandler: OnOffenceHandler<Self::AccountId, Self::IdentificationTuple, Weight>;
|
||||
/// The a soft limit on maximum weight that may be consumed while dispatching deferred offences in
|
||||
/// `on_initialize`.
|
||||
/// Note it's going to be exceeded before we stop adding to it, so it has to be set conservatively.
|
||||
type WeightSoftLimit: Get<Weight>;
|
||||
}
|
||||
|
||||
decl_storage! {
|
||||
@@ -102,23 +107,39 @@ decl_module! {
|
||||
|
||||
fn on_initialize(now: T::BlockNumber) -> Weight {
|
||||
// only decode storage if we can actually submit anything again.
|
||||
if T::OnOffenceHandler::can_report() {
|
||||
<DeferredOffences<T>>::mutate(|deferred| {
|
||||
// keep those that fail to be reported again. An error log is emitted here; this
|
||||
// should not happen if staking's `can_report` is implemented properly.
|
||||
deferred.retain(|(o, p, s)| {
|
||||
T::OnOffenceHandler::on_offence(&o, &p, *s).map_err(|_| {
|
||||
debug::native::error!(
|
||||
target: "pallet-offences",
|
||||
"re-submitting a deferred slash returned Err at {}. This should not happen with pallet-staking",
|
||||
now,
|
||||
);
|
||||
}).is_err()
|
||||
})
|
||||
})
|
||||
if !T::OnOffenceHandler::can_report() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
0
|
||||
let limit = T::WeightSoftLimit::get();
|
||||
let mut consumed = Weight::zero();
|
||||
|
||||
<DeferredOffences<T>>::mutate(|deferred| {
|
||||
deferred.retain(|(offences, perbill, session)| {
|
||||
if consumed >= limit {
|
||||
true
|
||||
} else {
|
||||
// keep those that fail to be reported again. An error log is emitted here; this
|
||||
// should not happen if staking's `can_report` is implemented properly.
|
||||
match T::OnOffenceHandler::on_offence(&offences, &perbill, *session) {
|
||||
Ok(weight) => {
|
||||
consumed += weight;
|
||||
false
|
||||
},
|
||||
Err(_) => {
|
||||
debug::native::error!(
|
||||
target: "pallet-offences",
|
||||
"re-submitting a deferred slash returned Err at {}. This should not happen with pallet-staking",
|
||||
now,
|
||||
);
|
||||
true
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
consumed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user