@@ -27,38 +27,14 @@ use frame_support::{
2727 inherent:: { InherentData , ProvideInherent , InherentIdentifier } ,
2828} ;
2929use codec:: { Encode , Decode } ;
30- use frame_system:: ensure_none;
3130use sp_runtime:: traits:: { Header as HeaderT , One , Zero } ;
3231use sp_authorship:: { INHERENT_IDENTIFIER , UnclesInherentData , InherentError } ;
3332
3433const MAX_UNCLES : usize = 10 ;
3534
36- pub trait Config : frame_system:: Config {
37- /// Find the author of a block.
38- type FindAuthor : FindAuthor < Self :: AccountId > ;
39- /// The number of blocks back we should accept uncles.
40- /// This means that we will deal with uncle-parents that are
41- /// `UncleGenerations + 1` before `now`.
42- type UncleGenerations : Get < Self :: BlockNumber > ;
43- /// A filter for uncles within a block. This is for implementing
44- /// further constraints on what uncles can be included, other than their ancestry.
45- ///
46- /// For PoW, as long as the seals are checked, there is no need to use anything
47- /// but the `VerifySeal` implementation as the filter. This is because the cost of making many equivocating
48- /// uncles is high.
49- ///
50- /// For PoS, there is no such limitation, so a further constraint must be imposed
51- /// beyond a seal check in order to prevent an arbitrary number of
52- /// equivocating uncles from being included.
53- ///
54- /// The `OnePerAuthorPerHeight` filter is good for many slot-based PoS
55- /// engines.
56- type FilterUncle : FilterUncle < Self :: Header , Self :: AccountId > ;
57- /// An event handler for authored blocks.
58- type EventHandler : EventHandler < Self :: AccountId , Self :: BlockNumber > ;
59- }
35+ pub use pallet:: * ;
6036
61- /// An event handler for the authorship module . There is a dummy implementation
37+ /// An event handler for the authorship pallet . There is a dummy implementation
6238/// for `()`, which does nothing.
6339#[ impl_trait_for_tuples:: impl_for_tuples( 30 ) ]
6440pub trait EventHandler < Author , BlockNumber > {
@@ -149,41 +125,44 @@ enum UncleEntryItem<BlockNumber, Hash, Author> {
149125 InclusionHeight ( BlockNumber ) ,
150126 Uncle ( Hash , Option < Author > ) ,
151127}
128+ #[ frame_support:: pallet]
129+ pub mod pallet {
130+ use frame_support:: pallet_prelude:: * ;
131+ use frame_system:: pallet_prelude:: * ;
132+ use super :: * ;
152133
153- decl_storage ! {
154- trait Store for Module <T : Config > as Authorship {
155- /// Uncles
156- Uncles : Vec <UncleEntryItem <T :: BlockNumber , T :: Hash , T :: AccountId >>;
157- /// Author of current block.
158- Author : Option <T :: AccountId >;
159- /// Whether uncles were already set in this block.
160- DidSetUncles : bool ;
134+ #[ pallet:: config]
135+ pub trait Config : frame_system:: Config {
136+ /// Find the author of a block.
137+ type FindAuthor : FindAuthor < Self :: AccountId > ;
138+ /// The number of blocks back we should accept uncles.
139+ /// This means that we will deal with uncle-parents that are
140+ /// `UncleGenerations + 1` before `now`.
141+ type UncleGenerations : Get < Self :: BlockNumber > ;
142+ /// A filter for uncles within a block. This is for implementing
143+ /// further constraints on what uncles can be included, other than their ancestry.
144+ ///
145+ /// For PoW, as long as the seals are checked, there is no need to use anything
146+ /// but the `VerifySeal` implementation as the filter. This is because the cost of making many equivocating
147+ /// uncles is high.
148+ ///
149+ /// For PoS, there is no such limitation, so a further constraint must be imposed
150+ /// beyond a seal check in order to prevent an arbitrary number of
151+ /// equivocating uncles from being included.
152+ ///
153+ /// The `OnePerAuthorPerHeight` filter is good for many slot-based PoS
154+ /// engines.
155+ type FilterUncle : FilterUncle < Self :: Header , Self :: AccountId > ;
156+ /// An event handler for authored blocks.
157+ type EventHandler : EventHandler < Self :: AccountId , Self :: BlockNumber > ;
161158 }
162- }
163159
164- decl_error ! {
165- /// Error for the authorship module.
166- pub enum Error for Module <T : Config > {
167- /// The uncle parent not in the chain.
168- InvalidUncleParent ,
169- /// Uncles already set in the block.
170- UnclesAlreadySet ,
171- /// Too many uncles.
172- TooManyUncles ,
173- /// The uncle is genesis.
174- GenesisUncle ,
175- /// The uncle is too high in chain.
176- TooHighUncle ,
177- /// The uncle is already included.
178- UncleAlreadyIncluded ,
179- /// The uncle isn't recent enough to be included.
180- OldUncle ,
181- }
182- }
160+ #[ pallet:: pallet]
161+ #[ pallet:: generate_store( pub ( super ) trait Store ) ]
162+ pub struct Pallet < T > ( _ ) ;
183163
184- decl_module ! {
185- pub struct Module <T : Config > for enum Call where origin: T :: Origin {
186- type Error = Error <T >;
164+ #[ pallet:: hooks]
165+ impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
187166
188167 fn on_initialize ( now : T :: BlockNumber ) -> Weight {
189168 let uncle_generations = T :: UncleGenerations :: get ( ) ;
@@ -193,60 +172,98 @@ decl_module! {
193172 Self :: prune_old_uncles ( minimum_height)
194173 }
195174
196- <Self as Store > :: DidSetUncles :: put( false ) ;
175+ <DidSetUncles < T > > :: put ( false ) ;
197176
198177 T :: EventHandler :: note_author ( Self :: author ( ) ) ;
199178
200179 0
201180 }
202181
203- fn on_finalize( ) {
182+ fn on_finalize ( _ : T :: BlockNumber ) {
204183 // ensure we never go to trie with these values.
205- <Self as Store > :: Author :: kill( ) ;
206- <Self as Store > :: DidSetUncles :: kill( ) ;
184+ <Author < T > > :: kill ( ) ;
185+ <DidSetUncles < T > > :: kill ( ) ;
207186 }
187+ }
208188
189+ #[ pallet:: storage]
190+ /// Uncles
191+ pub ( super ) type Uncles < T : Config > = StorageValue <
192+ _ ,
193+ Vec < UncleEntryItem < T :: BlockNumber , T :: Hash , T :: AccountId > > ,
194+ ValueQuery ,
195+ > ;
196+
197+ #[ pallet:: storage]
198+ /// Author of current block.
199+ pub ( super ) type Author < T : Config > = StorageValue < _ , T :: AccountId , OptionQuery > ;
200+
201+ #[ pallet:: storage]
202+ /// Whether uncles were already set in this block.
203+ pub ( super ) type DidSetUncles < T : Config > = StorageValue < _ , bool , ValueQuery > ;
204+
205+
206+ #[ pallet:: error]
207+ pub enum Error < T > {
208+ /// The uncle parent not in the chain.
209+ InvalidUncleParent ,
210+ /// Uncles already set in the block.
211+ UnclesAlreadySet ,
212+ /// Too many uncles.
213+ TooManyUncles ,
214+ /// The uncle is genesis.
215+ GenesisUncle ,
216+ /// The uncle is too high in chain.
217+ TooHighUncle ,
218+ /// The uncle is already included.
219+ UncleAlreadyIncluded ,
220+ /// The uncle isn't recent enough to be included.
221+ OldUncle ,
222+ }
223+
224+ #[ pallet:: call]
225+ impl < T : Config > Pallet < T > {
209226 /// Provide a set of uncles.
210- #[ weight = ( 0 , DispatchClass :: Mandatory ) ]
211- fn set_uncles( origin, new_uncles: Vec <T :: Header >) -> dispatch :: DispatchResult {
227+ #[ pallet :: weight( ( 0 , DispatchClass :: Mandatory ) ) ]
228+ fn set_uncles ( origin : OriginFor < T > , new_uncles : Vec < T :: Header > ) -> DispatchResult {
212229 ensure_none ( origin) ?;
213230 ensure ! ( new_uncles. len( ) <= MAX_UNCLES , Error :: <T >:: TooManyUncles ) ;
214231
215- if <Self as Store > :: DidSetUncles :: get( ) {
232+ if <DidSetUncles < T > > :: get ( ) {
216233 Err ( Error :: < T > :: UnclesAlreadySet ) ?
217234 }
218- <Self as Store > :: DidSetUncles :: put( true ) ;
235+ <DidSetUncles < T > > :: put ( true ) ;
219236
220237 Self :: verify_and_import_uncles ( new_uncles)
221238 }
222239 }
223240}
224241
225- impl < T : Config > Module < T > {
242+ impl < T : Config > Pallet < T > {
226243 /// Fetch the author of the block.
227244 ///
228245 /// This is safe to invoke in `on_initialize` implementations, as well
229246 /// as afterwards.
230247 pub fn author ( ) -> T :: AccountId {
231248 // Check the memoized storage value.
232- if let Some ( author) = <Self as Store > :: Author :: get ( ) {
249+ if let Some ( author) = <Author < T > > :: get ( ) {
233250 return author;
234251 }
235252
236- let digest = <frame_system:: Module < T > >:: digest ( ) ;
253+ let digest = <frame_system:: Pallet < T > >:: digest ( ) ;
237254 let pre_runtime_digests = digest. logs . iter ( ) . filter_map ( |d| d. as_pre_runtime ( ) ) ;
238255 if let Some ( author) = T :: FindAuthor :: find_author ( pre_runtime_digests) {
239- <Self as Store > :: Author :: put ( & author) ;
256+ <Author < T > > :: put ( & author) ;
240257 author
241258 } else {
242259 Default :: default ( )
243260 }
244261 }
245262
246263 fn verify_and_import_uncles ( new_uncles : Vec < T :: Header > ) -> dispatch:: DispatchResult {
247- let now = <frame_system:: Module < T > >:: block_number ( ) ;
264+ let now = <frame_system:: Pallet < T > >:: block_number ( ) ;
248265
249- let mut uncles = <Self as Store > :: Uncles :: get ( ) ;
266+ let mut uncles = <Uncles < T > > :: get ( ) ;
250267 uncles. push ( UncleEntryItem :: InclusionHeight ( now) ) ;
251268
252269 let mut acc: <T :: FilterUncle as FilterUncle < _ , _ > >:: Accumulator = Default :: default ( ) ;
@@ -267,7 +284,7 @@ impl<T: Config> Module<T> {
267284 uncles. push ( UncleEntryItem :: Uncle ( hash, author) ) ;
268285 }
269286
270- <Self as Store > :: Uncles :: put ( & uncles) ;
287+ <Uncles < T > > :: put ( & uncles) ;
271288 Ok ( ( ) )
272289 }
273290
@@ -301,7 +318,7 @@ impl<T: Config> Module<T> {
301318
302319 {
303320 let parent_number = uncle. number ( ) . clone ( ) - One :: one ( ) ;
304- let parent_hash = <frame_system:: Module < T > >:: block_hash ( & parent_number) ;
321+ let parent_hash = <frame_system:: Pallet < T > >:: block_hash ( & parent_number) ;
305322 if & parent_hash != uncle. parent_hash ( ) {
306323 return Err ( Error :: < T > :: InvalidUncleParent . into ( ) ) ;
307324 }
@@ -312,7 +329,7 @@ impl<T: Config> Module<T> {
312329 }
313330
314331 let duplicate = existing_uncles. into_iter ( ) . find ( |h| * * h == hash) . is_some ( ) ;
315- let in_chain = <frame_system:: Module < T > >:: block_hash ( uncle. number ( ) ) == hash;
332+ let in_chain = <frame_system:: Pallet < T > >:: block_hash ( uncle. number ( ) ) == hash;
316333
317334 if duplicate || in_chain {
318335 return Err ( Error :: < T > :: UncleAlreadyIncluded . into ( ) )
@@ -323,19 +340,19 @@ impl<T: Config> Module<T> {
323340 }
324341
325342 fn prune_old_uncles ( minimum_height : T :: BlockNumber ) {
326- let mut uncles = <Self as Store > :: Uncles :: get ( ) ;
343+ let mut uncles = <Uncles < T > > :: get ( ) ;
327344 let prune_entries = uncles. iter ( ) . take_while ( |item| match item {
328345 UncleEntryItem :: Uncle ( _, _) => true ,
329346 UncleEntryItem :: InclusionHeight ( height) => height < & minimum_height,
330347 } ) ;
331348 let prune_index = prune_entries. count ( ) ;
332349
333350 let _ = uncles. drain ( ..prune_index) ;
334- <Self as Store > :: Uncles :: put ( uncles) ;
351+ <Uncles < T > > :: put ( uncles) ;
335352 }
336353}
337354
338- impl < T : Config > ProvideInherent for Module < T > {
355+ impl < T : Config > ProvideInherent for Pallet < T > {
339356 type Call = Call < T > ;
340357 type Error = InherentError ;
341358 const INHERENT_IDENTIFIER : InherentIdentifier = INHERENT_IDENTIFIER ;
@@ -345,7 +362,7 @@ impl<T: Config> ProvideInherent for Module<T> {
345362 let mut set_uncles = Vec :: new ( ) ;
346363
347364 if !uncles. is_empty ( ) {
348- let prev_uncles = <Self as Store > :: Uncles :: get ( ) ;
365+ let prev_uncles = <Uncles < T > > :: get ( ) ;
349366 let mut existing_hashes: Vec < _ > = prev_uncles. into_iter ( ) . filter_map ( |entry|
350367 match entry {
351368 UncleEntryItem :: InclusionHeight ( _) => None ,
@@ -390,6 +407,10 @@ impl<T: Config> ProvideInherent for Module<T> {
390407 } ,
391408 }
392409 }
410+
411+ fn is_inherent ( call : & Self :: Call ) -> bool {
412+ matches ! ( call, Call :: set_uncles( _) )
413+ }
393414}
394415
395416#[ cfg( test) ]
@@ -411,8 +432,8 @@ mod tests {
411432 NodeBlock = Block ,
412433 UncheckedExtrinsic = UncheckedExtrinsic ,
413434 {
414- System : frame_system:: { Module , Call , Config , Storage , Event <T >} ,
415- Authorship : pallet_authorship:: { Module , Call , Storage , Inherent } ,
435+ System : frame_system:: { Pallet , Call , Config , Storage , Event <T >} ,
436+ Authorship : pallet_authorship:: { Pallet , Call , Storage , Inherent } ,
416437 }
417438 ) ;
418439
@@ -445,13 +466,14 @@ mod tests {
445466 type OnKilledAccount = ( ) ;
446467 type SystemWeightInfo = ( ) ;
447468 type SS58Prefix = ( ) ;
469+ type OnSetCode = ( ) ;
448470 }
449471
450472 parameter_types ! {
451473 pub const UncleGenerations : u64 = 5 ;
452474 }
453475
454- impl Config for Test {
476+ impl pallet :: Config for Test {
455477 type FindAuthor = AuthorGiven ;
456478 type UncleGenerations = UncleGenerations ;
457479 type FilterUncle = SealVerify < VerifyBlock > ;
0 commit comments