1515// See the License for the specific language governing permissions and
1616// limitations under the License.
1717
18- //! # Offences Module
18+ //! # Offences Pallet
1919//!
2020//! Tracks reported offences
2121
@@ -26,16 +26,16 @@ mod mock;
2626mod tests;
2727mod migration;
2828
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 ;
3331use sp_runtime:: { traits:: Hash , Perbill } ;
3432use sp_staking:: {
35- SessionIndex ,
36- offence :: { Offence , ReportOffence , Kind , OnOffenceHandler , OffenceDetails , OffenceError } ,
33+ offence :: { Kind , Offence , OffenceDetails , OffenceError , OnOffenceHandler , ReportOffence } ,
34+ SessionIndex
3735} ;
38- use codec:: { Encode , Decode } ;
36+ use codec:: { Decode , Encode } ;
37+
38+ pub use pallet:: * ;
3939
4040/// A binary blob which represents a SCALE codec-encoded `O::TimeSlot`.
4141type OpaqueTimeSlot = Vec < u8 > ;
@@ -57,59 +57,87 @@ impl WeightInfo for () {
5757 fn on_initialize ( _d : u32 , ) -> Weight { 1_000_000_000 }
5858}
5959
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 > ;
8979 }
90- }
9180
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) ]
93121 pub enum Event {
94122 /// There is an offence reported of the given `kind` happened at the `session_index` and
95123 /// (kind-specific) time slot. This event is not deposited for duplicate slashes.
96124 /// \[kind, timeslot\].
97125 Offence ( Kind , OpaqueTimeSlot ) ,
98126 }
99- ) ;
100-
101- decl_module ! {
102- pub struct Module <T : Config > for enum Call where origin: T :: Origin {
103- fn deposit_event( ) = default ;
104127
128+ #[ pallet:: hooks]
129+ impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
105130 fn on_runtime_upgrade ( ) -> Weight {
106131 migration:: remove_deferred_storage :: < T > ( )
107132 }
108133 }
134+
135+ #[ pallet:: call]
136+ impl < T : Config > Pallet < T > { }
109137}
110138
111139impl < 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 >
113141where
114142 T :: IdentificationTuple : Clone ,
115143{
@@ -120,11 +148,9 @@ where
120148
121149 // Go through all offenders in the offence report and find all offenders that were spotted
122150 // 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) {
128154 Some ( triage) => triage,
129155 // The report contained only duplicates, so there is no need to slash again.
130156 None => return Err ( OffenceError :: DuplicateReport ) ,
@@ -136,7 +162,8 @@ where
136162 let new_fraction = O :: slash_fraction ( offenders_count, validator_set_count) ;
137163
138164 let slash_perbill: Vec < _ > = ( 0 ..concurrent_offenders. len ( ) )
139- . map ( |_| new_fraction. clone ( ) ) . collect ( ) ;
165+ . map ( |_| new_fraction. clone ( ) )
166+ . collect ( ) ;
140167
141168 T :: OnOffenceHandler :: on_offence (
142169 & concurrent_offenders,
@@ -160,7 +187,7 @@ where
160187 }
161188}
162189
163- impl < T : Config > Module < T > {
190+ impl < T : Config > Pallet < T > {
164191 /// Compute the ID for the given report properties.
165192 ///
166193 /// The report id depends on the offence kind, time slot and the id of offender.
@@ -200,7 +227,8 @@ impl<T: Config> Module<T> {
200227
201228 if any_new {
202229 // 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
204232 . iter ( )
205233 . filter_map ( |report_id| <Reports < T > >:: get ( report_id) )
206234 . collect :: < Vec < _ > > ( ) ;
@@ -238,7 +266,7 @@ impl<T: Config, O: Offence<T::IdentificationTuple>> ReportIndexStorage<T, O> {
238266 fn load ( time_slot : & O :: TimeSlot ) -> Self {
239267 let opaque_time_slot = time_slot. encode ( ) ;
240268
241- let same_kind_reports = < ReportsByKindIndex >:: get ( & O :: ID ) ;
269+ let same_kind_reports = ReportsByKindIndex :: < T > :: get ( & O :: ID ) ;
242270 let same_kind_reports =
243271 Vec :: < ( O :: TimeSlot , ReportIdOf < T > ) > :: decode ( & mut & same_kind_reports[ ..] )
244272 . unwrap_or_default ( ) ;
@@ -272,7 +300,7 @@ impl<T: Config, O: Offence<T::IdentificationTuple>> ReportIndexStorage<T, O> {
272300
273301 /// Dump the indexes to the storage.
274302 fn save ( self ) {
275- < ReportsByKindIndex >:: insert ( & O :: ID , self . same_kind_reports . encode ( ) ) ;
303+ ReportsByKindIndex :: < T > :: insert ( & O :: ID , self . same_kind_reports . encode ( ) ) ;
276304 <ConcurrentReportsIndex < T > >:: insert (
277305 & O :: ID ,
278306 & self . opaque_time_slot ,
0 commit comments