Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 600c1f2

Browse files
Lohannkianenigma
authored andcommitted
Remove Offence delay (#8414)
* Removed can_report api from OnOffenceHandler * Removed DeferredOffences and create a storage migration * Removed missing comments * Mock set_deferred_offences and deferred_offences methods * OnOffenceHandler::on_offence always succeed * Fix benchmark tests * Fix runtime-benchmark cfg methods * Removed 'applied' attribute from Offence event * refactor deprecated deferred offences getter * Validate if offences are submited after on_runtime_upgrade * update changelog * Remove empty lines * Fix remove_deferred_storage weights * Remove Offence::on_runtime_upgrade benchmark * Revert CHANGELOG.md update * Deprecate DeferredOffenceOf type * Update copyright Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Add migration logs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Fix migration log * Remove unused import * Add migration tests * rustfmt * use generate_storage_alias! macro * Refactor should_resubmit_deferred_offences test * Replace spaces by tabs * Refactor should_resubmit_deferred_offences test * Removed WeightSoftLimit * Removed WeightSoftLimit from tests and mocks * Remove unused imports * Apply suggestions from code review Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
1 parent 1e9cc0c commit 600c1f2

13 files changed

Lines changed: 132 additions & 314 deletions

File tree

bin/node/runtime/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -878,16 +878,10 @@ impl pallet_im_online::Config for Runtime {
878878
type WeightInfo = pallet_im_online::weights::SubstrateWeight<Runtime>;
879879
}
880880

881-
parameter_types! {
882-
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) *
883-
RuntimeBlockWeights::get().max_block;
884-
}
885-
886881
impl pallet_offences::Config for Runtime {
887882
type Event = Event;
888883
type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
889884
type OnOffenceHandler = Staking;
890-
type WeightSoftLimit = OffencesWeightSoftLimit;
891885
}
892886

893887
impl pallet_authority_discovery::Config for Runtime {}

frame/babe/src/mock.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use frame_system::InitKind;
2929
use frame_support::{
3030
parameter_types,
3131
traits::{KeyOwnerProofSystem, OnInitialize},
32-
weights::Weight,
3332
};
3433
use sp_io;
3534
use sp_core::{H256, U256, crypto::{IsWrappedBy, KeyTypeId, Pair}};
@@ -213,16 +212,10 @@ impl pallet_staking::Config for Test {
213212
type WeightInfo = ();
214213
}
215214

216-
parameter_types! {
217-
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60)
218-
* BlockWeights::get().max_block;
219-
}
220-
221215
impl pallet_offences::Config for Test {
222216
type Event = Event;
223217
type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
224218
type OnOffenceHandler = Staking;
225-
type WeightSoftLimit = OffencesWeightSoftLimit;
226219
}
227220

228221
parameter_types! {

frame/grandpa/src/mock.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use codec::Encode;
2525
use frame_support::{
2626
parameter_types,
2727
traits::{KeyOwnerProofSystem, OnFinalize, OnInitialize},
28-
weights::Weight,
2928
};
3029
use pallet_staking::EraIndex;
3130
use sp_core::{crypto::KeyTypeId, H256};
@@ -219,15 +218,10 @@ impl pallet_staking::Config for Test {
219218
type WeightInfo = ();
220219
}
221220

222-
parameter_types! {
223-
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * BlockWeights::get().max_block;
224-
}
225-
226221
impl pallet_offences::Config for Test {
227222
type Event = Event;
228223
type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
229224
type OnOffenceHandler = Staking;
230-
type WeightSoftLimit = OffencesWeightSoftLimit;
231225
}
232226

233227
parameter_types! {

frame/offences/benchmarking/src/lib.rs

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ use sp_std::vec;
2626

2727
use frame_system::{RawOrigin, Pallet as System, Config as SystemConfig};
2828
use frame_benchmarking::{benchmarks, account, impl_benchmark_test_suite};
29-
use frame_support::traits::{Currency, OnInitialize, ValidatorSet, ValidatorSetWithIdentification};
29+
use frame_support::traits::{Currency, ValidatorSet, ValidatorSetWithIdentification};
3030

3131
use sp_runtime::{
3232
Perbill,
3333
traits::{Convert, StaticLookup, Saturating, UniqueSaturatedInto},
3434
};
35-
use sp_staking::offence::{ReportOffence, Offence, OffenceDetails};
35+
use sp_staking::offence::{ReportOffence, Offence};
3636

3737
use pallet_balances::Config as BalancesConfig;
3838
use pallet_babe::BabeEquivocationOffence;
@@ -51,7 +51,6 @@ const SEED: u32 = 0;
5151
const MAX_REPORTERS: u32 = 100;
5252
const MAX_OFFENDERS: u32 = 100;
5353
const MAX_NOMINATORS: u32 = 100;
54-
const MAX_DEFERRED_OFFENCES: u32 = 100;
5554

5655
pub struct Pallet<T: Config>(Offences<T>);
5756

@@ -271,8 +270,6 @@ benchmarks! {
271270
);
272271
}
273272
verify {
274-
// make sure the report was not deferred
275-
assert!(Offences::<T>::deferred_offences().is_empty());
276273
let bond_amount: u32 = UniqueSaturatedInto::<u32>::unique_saturated_into(bond_amount::<T>());
277274
let slash_amount = slash_fraction * bond_amount;
278275
let reward_amount = slash_amount * (1 + n) / 2;
@@ -306,7 +303,6 @@ benchmarks! {
306303
pallet_offences::Event::Offence(
307304
UnresponsivenessOffence::<T>::ID,
308305
0_u32.to_le_bytes().to_vec(),
309-
true
310306
)
311307
).into()))
312308
);
@@ -336,8 +332,6 @@ benchmarks! {
336332
let _ = Offences::<T>::report_offence(reporters, offence);
337333
}
338334
verify {
339-
// make sure the report was not deferred
340-
assert!(Offences::<T>::deferred_offences().is_empty());
341335
// make sure that all slashes have been applied
342336
assert_eq!(
343337
System::<T>::event_count(), 0
@@ -372,8 +366,6 @@ benchmarks! {
372366
let _ = Offences::<T>::report_offence(reporters, offence);
373367
}
374368
verify {
375-
// make sure the report was not deferred
376-
assert!(Offences::<T>::deferred_offences().is_empty());
377369
// make sure that all slashes have been applied
378370
assert_eq!(
379371
System::<T>::event_count(), 0
@@ -383,42 +375,6 @@ benchmarks! {
383375
+ n // nominators slashed
384376
);
385377
}
386-
387-
on_initialize {
388-
let d in 1 .. MAX_DEFERRED_OFFENCES;
389-
let o = 10;
390-
let n = 100;
391-
392-
let mut deferred_offences = vec![];
393-
let offenders = make_offenders::<T>(o, n)?.0;
394-
let offence_details = offenders.into_iter()
395-
.map(|offender| OffenceDetails {
396-
offender: T::convert(offender),
397-
reporters: vec![],
398-
})
399-
.collect::<Vec<_>>();
400-
401-
for i in 0 .. d {
402-
let fractions = offence_details.iter()
403-
.map(|_| Perbill::from_percent(100 * (i + 1) / MAX_DEFERRED_OFFENCES))
404-
.collect::<Vec<_>>();
405-
deferred_offences.push((offence_details.clone(), fractions.clone(), 0u32));
406-
}
407-
408-
Offences::<T>::set_deferred_offences(deferred_offences);
409-
assert!(!Offences::<T>::deferred_offences().is_empty());
410-
}: {
411-
Offences::<T>::on_initialize(0u32.into());
412-
}
413-
verify {
414-
// make sure that all deferred offences were reported with Ok status.
415-
assert!(Offences::<T>::deferred_offences().is_empty());
416-
assert_eq!(
417-
System::<T>::event_count(), d * (0
418-
+ o // offenders slashed
419-
+ o * n // nominators slashed
420-
));
421-
}
422378
}
423379

424380
impl_benchmark_test_suite!(

frame/offences/benchmarking/src/mock.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use super::*;
2323
use frame_support::{
2424
parameter_types,
25-
weights::{Weight, constants::WEIGHT_PER_SECOND},
25+
weights::constants::WEIGHT_PER_SECOND,
2626
};
2727
use frame_system as system;
2828
use sp_runtime::{
@@ -188,15 +188,10 @@ impl pallet_im_online::Config for Test {
188188
type WeightInfo = ();
189189
}
190190

191-
parameter_types! {
192-
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * BlockWeights::get().max_block;
193-
}
194-
195191
impl pallet_offences::Config for Test {
196192
type Event = Event;
197193
type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
198194
type OnOffenceHandler = Staking;
199-
type WeightSoftLimit = OffencesWeightSoftLimit;
200195
}
201196

202197
impl<T> frame_system::offchain::SendTransactionTypes<T> for Test where Call: From<T> {

frame/offences/src/lib.rs

Lines changed: 10 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424

2525
mod mock;
2626
mod tests;
27+
mod migration;
2728

2829
use sp_std::vec::Vec;
2930
use frame_support::{
30-
decl_module, decl_event, decl_storage, Parameter, traits::Get, weights::Weight,
31+
decl_module, decl_event, decl_storage, Parameter, weights::Weight,
3132
};
32-
use sp_runtime::{traits::{Hash, Zero}, Perbill};
33+
use sp_runtime::{traits::Hash, Perbill};
3334
use sp_staking::{
3435
SessionIndex,
3536
offence::{Offence, ReportOffence, Kind, OnOffenceHandler, OffenceDetails, OffenceError},
@@ -42,13 +43,6 @@ type OpaqueTimeSlot = Vec<u8>;
4243
/// A type alias for a report identifier.
4344
type ReportIdOf<T> = <T as frame_system::Config>::Hash;
4445

45-
/// Type of data stored as a deferred offence
46-
pub type DeferredOffenceOf<T> = (
47-
Vec<OffenceDetails<<T as frame_system::Config>::AccountId, <T as Config>::IdentificationTuple>>,
48-
Vec<Perbill>,
49-
SessionIndex,
50-
);
51-
5246
pub trait WeightInfo {
5347
fn report_offence_im_online(r: u32, o: u32, n: u32, ) -> Weight;
5448
fn report_offence_grandpa(r: u32, n: u32, ) -> Weight;
@@ -71,10 +65,6 @@ pub trait Config: frame_system::Config {
7165
type IdentificationTuple: Parameter + Ord;
7266
/// A handler called for every offence report.
7367
type OnOffenceHandler: OnOffenceHandler<Self::AccountId, Self::IdentificationTuple, Weight>;
74-
/// The a soft limit on maximum weight that may be consumed while dispatching deferred offences in
75-
/// `on_initialize`.
76-
/// Note it's going to be exceeded before we stop adding to it, so it has to be set conservatively.
77-
type WeightSoftLimit: Get<Weight>;
7868
}
7969

8070
decl_storage! {
@@ -84,10 +74,6 @@ decl_storage! {
8474
map hasher(twox_64_concat) ReportIdOf<T>
8575
=> Option<OffenceDetails<T::AccountId, T::IdentificationTuple>>;
8676

87-
/// Deferred reports that have been rejected by the offence handler and need to be submitted
88-
/// at a later time.
89-
DeferredOffences get(fn deferred_offences): Vec<DeferredOffenceOf<T>>;
90-
9177
/// A vector of reports of the same kind that happened at the same time slot.
9278
ConcurrentReportsIndex:
9379
double_map hasher(twox_64_concat) Kind, hasher(twox_64_concat) OpaqueTimeSlot
@@ -106,53 +92,18 @@ decl_storage! {
10692
decl_event!(
10793
pub enum Event {
10894
/// There is an offence reported of the given `kind` happened at the `session_index` and
109-
/// (kind-specific) time slot. This event is not deposited for duplicate slashes. last
110-
/// element indicates of the offence was applied (true) or queued (false)
111-
/// \[kind, timeslot, applied\].
112-
Offence(Kind, OpaqueTimeSlot, bool),
95+
/// (kind-specific) time slot. This event is not deposited for duplicate slashes.
96+
/// \[kind, timeslot\].
97+
Offence(Kind, OpaqueTimeSlot),
11398
}
11499
);
115100

116101
decl_module! {
117102
pub struct Module<T: Config> for enum Call where origin: T::Origin {
118103
fn deposit_event() = default;
119104

120-
fn on_initialize(now: T::BlockNumber) -> Weight {
121-
// only decode storage if we can actually submit anything again.
122-
if !T::OnOffenceHandler::can_report() {
123-
return 0;
124-
}
125-
126-
let limit = T::WeightSoftLimit::get();
127-
let mut consumed = Weight::zero();
128-
129-
<DeferredOffences<T>>::mutate(|deferred| {
130-
deferred.retain(|(offences, perbill, session)| {
131-
if consumed >= limit {
132-
true
133-
} else {
134-
// keep those that fail to be reported again. An error log is emitted here; this
135-
// should not happen if staking's `can_report` is implemented properly.
136-
match T::OnOffenceHandler::on_offence(&offences, &perbill, *session) {
137-
Ok(weight) => {
138-
consumed += weight;
139-
false
140-
},
141-
Err(_) => {
142-
log::error!(
143-
target: "runtime::offences",
144-
"re-submitting a deferred slash returned Err at {:?}. \
145-
This should not happen with pallet-staking",
146-
now,
147-
);
148-
true
149-
},
150-
}
151-
}
152-
})
153-
});
154-
155-
consumed
105+
fn on_runtime_upgrade() -> Weight {
106+
migration::remove_deferred_storage::<T>()
156107
}
157108
}
158109
}
@@ -187,14 +138,14 @@ where
187138
let slash_perbill: Vec<_> = (0..concurrent_offenders.len())
188139
.map(|_| new_fraction.clone()).collect();
189140

190-
let applied = Self::report_or_store_offence(
141+
T::OnOffenceHandler::on_offence(
191142
&concurrent_offenders,
192143
&slash_perbill,
193144
offence.session_index(),
194145
);
195146

196147
// Deposit the event.
197-
Self::deposit_event(Event::Offence(O::ID, time_slot.encode(), applied));
148+
Self::deposit_event(Event::Offence(O::ID, time_slot.encode()));
198149

199150
Ok(())
200151
}
@@ -210,28 +161,6 @@ where
210161
}
211162

212163
impl<T: Config> Module<T> {
213-
/// Tries (without checking) to report an offence. Stores them in [`DeferredOffences`] in case
214-
/// it fails. Returns false in case it has to store the offence.
215-
fn report_or_store_offence(
216-
concurrent_offenders: &[OffenceDetails<T::AccountId, T::IdentificationTuple>],
217-
slash_perbill: &[Perbill],
218-
session_index: SessionIndex,
219-
) -> bool {
220-
match T::OnOffenceHandler::on_offence(
221-
&concurrent_offenders,
222-
&slash_perbill,
223-
session_index,
224-
) {
225-
Ok(_) => true,
226-
Err(_) => {
227-
<DeferredOffences<T>>::mutate(|d|
228-
d.push((concurrent_offenders.to_vec(), slash_perbill.to_vec(), session_index))
229-
);
230-
false
231-
}
232-
}
233-
}
234-
235164
/// Compute the ID for the given report properties.
236165
///
237166
/// The report id depends on the offence kind, time slot and the id of offender.
@@ -285,11 +214,6 @@ impl<T: Config> Module<T> {
285214
None
286215
}
287216
}
288-
289-
#[cfg(feature = "runtime-benchmarks")]
290-
pub fn set_deferred_offences(offences: Vec<DeferredOffenceOf<T>>) {
291-
<DeferredOffences<T>>::put(offences);
292-
}
293217
}
294218

295219
struct TriageOutcome<T: Config> {

0 commit comments

Comments
 (0)