15
15
// See the License for the specific language governing permissions and
16
16
// limitations under the License.
17
17
18
- //! # Offences Module
18
+ //! # Offences Pallet
19
19
//!
20
20
//! Tracks reported offences
21
21
@@ -26,16 +26,16 @@ mod mock;
26
26
mod tests;
27
27
mod migration;
28
28
29
- use sp_std:: vec:: Vec ;
30
- use frame_support:: {
31
- decl_module, decl_event, decl_storage, Parameter , weights:: Weight ,
32
- } ;
29
+ use sp_std:: prelude:: * ;
30
+ use frame_support:: weights:: Weight ;
33
31
use sp_runtime:: { traits:: Hash , Perbill } ;
34
32
use sp_staking:: {
35
- SessionIndex ,
36
- offence :: { Offence , ReportOffence , Kind , OnOffenceHandler , OffenceDetails , OffenceError } ,
33
+ offence :: { Kind , Offence , OffenceDetails , OffenceError , OnOffenceHandler , ReportOffence } ,
34
+ SessionIndex
37
35
} ;
38
- use codec:: { Encode , Decode } ;
36
+ use codec:: { Decode , Encode } ;
37
+
38
+ pub use pallet:: * ;
39
39
40
40
/// A binary blob which represents a SCALE codec-encoded `O::TimeSlot`.
41
41
type OpaqueTimeSlot = Vec < u8 > ;
@@ -57,59 +57,87 @@ impl WeightInfo for () {
57
57
fn on_initialize ( _d : u32 , ) -> Weight { 1_000_000_000 }
58
58
}
59
59
60
- /// Offences trait
61
- pub trait Config : frame_system:: Config {
62
- /// The overarching event type.
63
- type Event : From < Event > + Into < <Self as frame_system:: Config >:: Event > ;
64
- /// Full identification of the validator.
65
- type IdentificationTuple : Parameter + Ord ;
66
- /// A handler called for every offence report.
67
- type OnOffenceHandler : OnOffenceHandler < Self :: AccountId , Self :: IdentificationTuple , Weight > ;
68
- }
69
-
70
- decl_storage ! {
71
- trait Store for Module <T : Config > as Offences {
72
- /// The primary structure that holds all offence records keyed by report identifiers.
73
- Reports get( fn reports) :
74
- map hasher( twox_64_concat) ReportIdOf <T >
75
- => Option <OffenceDetails <T :: AccountId , T :: IdentificationTuple >>;
76
-
77
- /// A vector of reports of the same kind that happened at the same time slot.
78
- ConcurrentReportsIndex :
79
- double_map hasher( twox_64_concat) Kind , hasher( twox_64_concat) OpaqueTimeSlot
80
- => Vec <ReportIdOf <T >>;
81
-
82
- /// Enumerates all reports of a kind along with the time they happened.
83
- ///
84
- /// All reports are sorted by the time of offence.
85
- ///
86
- /// Note that the actual type of this mapping is `Vec<u8>`, this is because values of
87
- /// different types are not supported at the moment so we are doing the manual serialization.
88
- ReportsByKindIndex : map hasher( twox_64_concat) Kind => Vec <u8 >; // (O::TimeSlot, ReportIdOf<T>)
60
+ #[ frame_support:: pallet]
61
+ pub mod pallet {
62
+ use super :: * ;
63
+ use frame_support:: pallet_prelude:: * ;
64
+ use frame_system:: pallet_prelude:: * ;
65
+
66
+ #[ pallet:: pallet]
67
+ #[ pallet:: generate_store( pub ( super ) trait Store ) ]
68
+ pub struct Pallet < T > ( _ ) ;
69
+
70
+ /// The pallet's config trait.
71
+ #[ pallet:: config]
72
+ pub trait Config : frame_system:: Config {
73
+ /// The overarching event type.
74
+ type Event : From < Event > + IsType < <Self as frame_system:: Config >:: Event > ;
75
+ /// Full identification of the validator.
76
+ type IdentificationTuple : Parameter + Ord ;
77
+ /// A handler called for every offence report.
78
+ type OnOffenceHandler : OnOffenceHandler < Self :: AccountId , Self :: IdentificationTuple , Weight > ;
89
79
}
90
- }
91
80
92
- decl_event ! (
81
+ /// The primary structure that holds all offence records keyed by report identifiers.
82
+ #[ pallet:: storage]
83
+ #[ pallet:: getter( fn reports) ]
84
+ pub type Reports < T : Config > = StorageMap <
85
+ _ ,
86
+ Twox64Concat ,
87
+ ReportIdOf < T > ,
88
+ OffenceDetails < T :: AccountId , T :: IdentificationTuple > ,
89
+ > ;
90
+
91
+ /// A vector of reports of the same kind that happened at the same time slot.
92
+ #[ pallet:: storage]
93
+ pub type ConcurrentReportsIndex < T : Config > = StorageDoubleMap <
94
+ _ ,
95
+ Twox64Concat ,
96
+ Kind ,
97
+ Twox64Concat ,
98
+ OpaqueTimeSlot ,
99
+ Vec < ReportIdOf < T > > ,
100
+ ValueQuery ,
101
+ > ;
102
+
103
+ /// Enumerates all reports of a kind along with the time they happened.
104
+ ///
105
+ /// All reports are sorted by the time of offence.
106
+ ///
107
+ /// Note that the actual type of this mapping is `Vec<u8>`, this is because values of
108
+ /// different types are not supported at the moment so we are doing the manual serialization.
109
+ #[ pallet:: storage]
110
+ pub type ReportsByKindIndex < T > = StorageMap <
111
+ _ ,
112
+ Twox64Concat ,
113
+ Kind ,
114
+ Vec < u8 > , // (O::TimeSlot, ReportIdOf<T>)
115
+ ValueQuery ,
116
+ > ;
117
+
118
+ /// Events type.
119
+ #[ pallet:: event]
120
+ #[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
93
121
pub enum Event {
94
122
/// There is an offence reported of the given `kind` happened at the `session_index` and
95
123
/// (kind-specific) time slot. This event is not deposited for duplicate slashes.
96
124
/// \[kind, timeslot\].
97
125
Offence ( Kind , OpaqueTimeSlot ) ,
98
126
}
99
- ) ;
100
-
101
- decl_module ! {
102
- pub struct Module <T : Config > for enum Call where origin: T :: Origin {
103
- fn deposit_event( ) = default ;
104
127
128
+ #[ pallet:: hooks]
129
+ impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
105
130
fn on_runtime_upgrade ( ) -> Weight {
106
131
migration:: remove_deferred_storage :: < T > ( )
107
132
}
108
133
}
134
+
135
+ #[ pallet:: call]
136
+ impl < T : Config > Pallet < T > { }
109
137
}
110
138
111
139
impl < T : Config , O : Offence < T :: IdentificationTuple > >
112
- ReportOffence < T :: AccountId , T :: IdentificationTuple , O > for Module < T >
140
+ ReportOffence < T :: AccountId , T :: IdentificationTuple , O > for Pallet < T >
113
141
where
114
142
T :: IdentificationTuple : Clone ,
115
143
{
@@ -120,11 +148,9 @@ where
120
148
121
149
// Go through all offenders in the offence report and find all offenders that were spotted
122
150
// in unique reports.
123
- let TriageOutcome { concurrent_offenders } = match Self :: triage_offence_report :: < O > (
124
- reporters,
125
- & time_slot,
126
- offenders,
127
- ) {
151
+ let TriageOutcome {
152
+ concurrent_offenders,
153
+ } = match Self :: triage_offence_report :: < O > ( reporters, & time_slot, offenders) {
128
154
Some ( triage) => triage,
129
155
// The report contained only duplicates, so there is no need to slash again.
130
156
None => return Err ( OffenceError :: DuplicateReport ) ,
@@ -136,7 +162,8 @@ where
136
162
let new_fraction = O :: slash_fraction ( offenders_count, validator_set_count) ;
137
163
138
164
let slash_perbill: Vec < _ > = ( 0 ..concurrent_offenders. len ( ) )
139
- . map ( |_| new_fraction. clone ( ) ) . collect ( ) ;
165
+ . map ( |_| new_fraction. clone ( ) )
166
+ . collect ( ) ;
140
167
141
168
T :: OnOffenceHandler :: on_offence (
142
169
& concurrent_offenders,
@@ -160,7 +187,7 @@ where
160
187
}
161
188
}
162
189
163
- impl < T : Config > Module < T > {
190
+ impl < T : Config > Pallet < T > {
164
191
/// Compute the ID for the given report properties.
165
192
///
166
193
/// The report id depends on the offence kind, time slot and the id of offender.
@@ -200,7 +227,8 @@ impl<T: Config> Module<T> {
200
227
201
228
if any_new {
202
229
// Load report details for the all reports happened at the same time.
203
- let concurrent_offenders = storage. concurrent_reports
230
+ let concurrent_offenders = storage
231
+ . concurrent_reports
204
232
. iter ( )
205
233
. filter_map ( |report_id| <Reports < T > >:: get ( report_id) )
206
234
. collect :: < Vec < _ > > ( ) ;
@@ -238,7 +266,7 @@ impl<T: Config, O: Offence<T::IdentificationTuple>> ReportIndexStorage<T, O> {
238
266
fn load ( time_slot : & O :: TimeSlot ) -> Self {
239
267
let opaque_time_slot = time_slot. encode ( ) ;
240
268
241
- let same_kind_reports = < ReportsByKindIndex >:: get ( & O :: ID ) ;
269
+ let same_kind_reports = ReportsByKindIndex :: < T > :: get ( & O :: ID ) ;
242
270
let same_kind_reports =
243
271
Vec :: < ( O :: TimeSlot , ReportIdOf < T > ) > :: decode ( & mut & same_kind_reports[ ..] )
244
272
. unwrap_or_default ( ) ;
@@ -272,7 +300,7 @@ impl<T: Config, O: Offence<T::IdentificationTuple>> ReportIndexStorage<T, O> {
272
300
273
301
/// Dump the indexes to the storage.
274
302
fn save ( self ) {
275
- < ReportsByKindIndex >:: insert ( & O :: ID , self . same_kind_reports . encode ( ) ) ;
303
+ ReportsByKindIndex :: < T > :: insert ( & O :: ID , self . same_kind_reports . encode ( ) ) ;
276
304
<ConcurrentReportsIndex < T > >:: insert (
277
305
& O :: ID ,
278
306
& self . opaque_time_slot ,
0 commit comments