2424
2525mod mock;
2626mod tests;
27+ mod migration;
2728
2829use sp_std:: vec:: Vec ;
2930use 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 } ;
3334use 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.
4344type 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-
5246pub 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
8070decl_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! {
10692decl_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
116101decl_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
212163impl < 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
295219struct TriageOutcome < T : Config > {
0 commit comments