26
26
//! These roots and proofs of inclusion can be generated at any time during the current session.
27
27
//! Afterwards, the proofs can be fed to a consensus module when reporting misbehavior.
28
28
29
- use super :: { Pallet as SessionModule , SessionIndex } ;
29
+ pub mod offchain;
30
+ pub mod onchain;
31
+ mod shared;
32
+
30
33
use codec:: { Decode , Encode } ;
31
- use frame_support:: {
32
- decl_module, decl_storage, print,
33
- traits:: { ValidatorSet , ValidatorSetWithIdentification } ,
34
- Parameter ,
35
- } ;
36
34
use sp_runtime:: {
37
35
traits:: { Convert , OpaqueKeys } ,
38
36
KeyTypeId ,
39
37
} ;
40
38
use sp_session:: { MembershipProof , ValidatorCount } ;
39
+ use sp_staking:: SessionIndex ;
41
40
use sp_std:: prelude:: * ;
42
41
use sp_trie:: {
43
42
trie_types:: { TrieDB , TrieDBMut } ,
44
43
MemoryDB , Recorder , Trie , TrieMut , EMPTY_PREFIX ,
45
44
} ;
46
45
47
- pub mod offchain;
48
- pub mod onchain;
49
- mod shared;
46
+ use frame_support:: {
47
+ print,
48
+ traits:: { KeyOwnerProofSystem , StorageVersion , ValidatorSet , ValidatorSetWithIdentification } ,
49
+ Parameter ,
50
+ } ;
50
51
51
- /// Config necessary for the historical module.
52
- pub trait Config : super :: Config {
53
- /// Full identification of the validator.
54
- type FullIdentification : Parameter ;
55
-
56
- /// A conversion from validator ID to full identification.
57
- ///
58
- /// This should contain any references to economic actors associated with the
59
- /// validator, since they may be outdated by the time this is queried from a
60
- /// historical trie.
61
- ///
62
- /// It must return the identification for the current session index.
63
- type FullIdentificationOf : Convert < Self :: ValidatorId , Option < Self :: FullIdentification > > ;
64
- }
52
+ use crate :: { self as pallet_session, Pallet as Session } ;
53
+
54
+ pub use pallet:: * ;
65
55
66
- decl_storage ! {
67
- trait Store for Module <T : Config > as Session {
68
- /// Mapping from historical session indices to session-data root hash and validator count.
69
- HistoricalSessions get( fn historical_root) :
70
- map hasher( twox_64_concat) SessionIndex => Option <( T :: Hash , ValidatorCount ) >;
71
- /// The range of historical sessions we store. [first, last)
72
- StoredRange : Option <( SessionIndex , SessionIndex ) >;
73
- /// Deprecated.
74
- CachedObsolete :
75
- map hasher( twox_64_concat) SessionIndex
76
- => Option <Vec <( T :: ValidatorId , T :: FullIdentification ) >>;
56
+ #[ frame_support:: pallet]
57
+ pub mod pallet {
58
+ use super :: * ;
59
+ use frame_support:: pallet_prelude:: * ;
60
+
61
+ /// The current storage version.
62
+ const STORAGE_VERSION : StorageVersion = StorageVersion :: new ( 1 ) ;
63
+
64
+ #[ pallet:: pallet]
65
+ #[ pallet:: generate_store( pub ( super ) trait Store ) ]
66
+ #[ pallet:: storage_version( STORAGE_VERSION ) ]
67
+ pub struct Pallet < T > ( _ ) ;
68
+
69
+ /// Config necessary for the historical pallet.
70
+ #[ pallet:: config]
71
+ pub trait Config : pallet_session:: Config + frame_system:: Config {
72
+ /// Full identification of the validator.
73
+ type FullIdentification : Parameter ;
74
+
75
+ /// A conversion from validator ID to full identification.
76
+ ///
77
+ /// This should contain any references to economic actors associated with the
78
+ /// validator, since they may be outdated by the time this is queried from a
79
+ /// historical trie.
80
+ ///
81
+ /// It must return the identification for the current session index.
82
+ type FullIdentificationOf : Convert < Self :: ValidatorId , Option < Self :: FullIdentification > > ;
77
83
}
78
- }
79
84
80
- decl_module ! {
81
- pub struct Module <T : Config > for enum Call where origin: T :: Origin { }
85
+ /// Mapping from historical session indices to session-data root hash and validator count.
86
+ #[ pallet:: storage]
87
+ #[ pallet:: getter( fn historical_root) ]
88
+ pub type HistoricalSessions < T : Config > =
89
+ StorageMap < _ , Twox64Concat , SessionIndex , ( T :: Hash , ValidatorCount ) , OptionQuery > ;
90
+
91
+ /// The range of historical sessions we store. [first, last)
92
+ #[ pallet:: storage]
93
+ pub type StoredRange < T > = StorageValue < _ , ( SessionIndex , SessionIndex ) , OptionQuery > ;
82
94
}
83
95
84
- impl < T : Config > Module < T > {
96
+ impl < T : Config > Pallet < T > {
85
97
/// Prune historical stored session roots up to (but not including)
86
98
/// `up_to`.
87
99
pub fn prune_up_to ( up_to : SessionIndex ) {
@@ -109,7 +121,7 @@ impl<T: Config> Module<T> {
109
121
}
110
122
}
111
123
112
- impl < T : Config > ValidatorSet < T :: AccountId > for Module < T > {
124
+ impl < T : Config > ValidatorSet < T :: AccountId > for Pallet < T > {
113
125
type ValidatorId = T :: ValidatorId ;
114
126
type ValidatorIdOf = T :: ValidatorIdOf ;
115
127
@@ -122,15 +134,15 @@ impl<T: Config> ValidatorSet<T::AccountId> for Module<T> {
122
134
}
123
135
}
124
136
125
- impl < T : Config > ValidatorSetWithIdentification < T :: AccountId > for Module < T > {
137
+ impl < T : Config > ValidatorSetWithIdentification < T :: AccountId > for Pallet < T > {
126
138
type Identification = T :: FullIdentification ;
127
139
type IdentificationOf = T :: FullIdentificationOf ;
128
140
}
129
141
130
142
/// Specialization of the crate-level `SessionManager` which returns the set of full identification
131
143
/// when creating a new session.
132
144
pub trait SessionManager < ValidatorId , FullIdentification > :
133
- crate :: SessionManager < ValidatorId >
145
+ pallet_session :: SessionManager < ValidatorId >
134
146
{
135
147
/// If there was a validator set change, its returns the set of new validators along with their
136
148
/// full identifications.
@@ -150,7 +162,7 @@ pub struct NoteHistoricalRoot<T, I>(sp_std::marker::PhantomData<(T, I)>);
150
162
151
163
impl < T : Config , I : SessionManager < T :: ValidatorId , T :: FullIdentification > > NoteHistoricalRoot < T , I > {
152
164
fn do_new_session ( new_index : SessionIndex , is_genesis : bool ) -> Option < Vec < T :: ValidatorId > > {
153
- StoredRange :: mutate ( |range| {
165
+ < StoredRange < T > > :: mutate ( |range| {
154
166
range. get_or_insert_with ( || ( new_index, new_index) ) . 1 = new_index + 1 ;
155
167
} ) ;
156
168
@@ -183,7 +195,7 @@ impl<T: Config, I: SessionManager<T::ValidatorId, T::FullIdentification>> NoteHi
183
195
}
184
196
}
185
197
186
- impl < T : Config , I > crate :: SessionManager < T :: ValidatorId > for NoteHistoricalRoot < T , I >
198
+ impl < T : Config , I > pallet_session :: SessionManager < T :: ValidatorId > for NoteHistoricalRoot < T , I >
187
199
where
188
200
I : SessionManager < T :: ValidatorId , T :: FullIdentification > ,
189
201
{
@@ -207,7 +219,7 @@ where
207
219
208
220
/// A tuple of the validator's ID and their full identification.
209
221
pub type IdentificationTuple < T > =
210
- ( <T as crate :: Config >:: ValidatorId , <T as Config >:: FullIdentification ) ;
222
+ ( <T as pallet_session :: Config >:: ValidatorId , <T as Config >:: FullIdentification ) ;
211
223
212
224
/// A trie instance for checking and generating proofs.
213
225
pub struct ProvingTrie < T : Config > {
@@ -227,7 +239,7 @@ impl<T: Config> ProvingTrie<T> {
227
239
let mut trie = TrieDBMut :: new ( & mut db, & mut root) ;
228
240
for ( i, ( validator, full_id) ) in validators. into_iter ( ) . enumerate ( ) {
229
241
let i = i as u32 ;
230
- let keys = match <SessionModule < T > >:: load_keys ( & validator) {
242
+ let keys = match <Session < T > >:: load_keys ( & validator) {
231
243
None => continue ,
232
244
Some ( k) => k,
233
245
} ;
@@ -304,15 +316,13 @@ impl<T: Config> ProvingTrie<T> {
304
316
}
305
317
}
306
318
307
- impl < T : Config , D : AsRef < [ u8 ] > > frame_support:: traits:: KeyOwnerProofSystem < ( KeyTypeId , D ) >
308
- for Module < T >
309
- {
319
+ impl < T : Config , D : AsRef < [ u8 ] > > KeyOwnerProofSystem < ( KeyTypeId , D ) > for Pallet < T > {
310
320
type Proof = MembershipProof ;
311
321
type IdentificationTuple = IdentificationTuple < T > ;
312
322
313
323
fn prove ( key : ( KeyTypeId , D ) ) -> Option < Self :: Proof > {
314
- let session = <SessionModule < T > >:: current_index ( ) ;
315
- let validators = <SessionModule < T > >:: validators ( )
324
+ let session = <Session < T > >:: current_index ( ) ;
325
+ let validators = <Session < T > >:: validators ( )
316
326
. into_iter ( )
317
327
. filter_map ( |validator| {
318
328
T :: FullIdentificationOf :: convert ( validator. clone ( ) )
@@ -335,10 +345,10 @@ impl<T: Config, D: AsRef<[u8]>> frame_support::traits::KeyOwnerProofSystem<(KeyT
335
345
fn check_proof ( key : ( KeyTypeId , D ) , proof : Self :: Proof ) -> Option < IdentificationTuple < T > > {
336
346
let ( id, data) = key;
337
347
338
- if proof. session == <SessionModule < T > >:: current_index ( ) {
339
- <SessionModule < T > >:: key_owner ( id, data. as_ref ( ) ) . and_then ( |owner| {
348
+ if proof. session == <Session < T > >:: current_index ( ) {
349
+ <Session < T > >:: key_owner ( id, data. as_ref ( ) ) . and_then ( |owner| {
340
350
T :: FullIdentificationOf :: convert ( owner. clone ( ) ) . and_then ( move |id| {
341
- let count = <SessionModule < T > >:: validators ( ) . len ( ) as ValidatorCount ;
351
+ let count = <Session < T > >:: validators ( ) . len ( ) as ValidatorCount ;
342
352
343
353
if count != proof. validator_count {
344
354
return None
@@ -374,7 +384,7 @@ pub(crate) mod tests {
374
384
BasicExternalities ,
375
385
} ;
376
386
377
- type Historical = Module < Test > ;
387
+ type Historical = Pallet < Test > ;
378
388
379
389
pub ( crate ) fn new_test_ext ( ) -> sp_io:: TestExternalities {
380
390
let mut t = frame_system:: GenesisConfig :: default ( ) . build_storage :: < Test > ( ) . unwrap ( ) ;
@@ -386,7 +396,9 @@ pub(crate) mod tests {
386
396
frame_system:: Pallet :: < Test > :: inc_providers ( k) ;
387
397
}
388
398
} ) ;
389
- crate :: GenesisConfig :: < Test > { keys } . assimilate_storage ( & mut t) . unwrap ( ) ;
399
+ pallet_session:: GenesisConfig :: < Test > { keys }
400
+ . assimilate_storage ( & mut t)
401
+ . unwrap ( ) ;
390
402
sp_io:: TestExternalities :: new ( t)
391
403
}
392
404
@@ -436,27 +448,27 @@ pub(crate) mod tests {
436
448
Session :: on_initialize ( i) ;
437
449
}
438
450
439
- assert_eq ! ( StoredRange :: get( ) , Some ( ( 0 , 100 ) ) ) ;
451
+ assert_eq ! ( < StoredRange < Test >> :: get( ) , Some ( ( 0 , 100 ) ) ) ;
440
452
441
453
for i in 0 ..100 {
442
454
assert ! ( Historical :: historical_root( i) . is_some( ) )
443
455
}
444
456
445
457
Historical :: prune_up_to ( 10 ) ;
446
- assert_eq ! ( StoredRange :: get( ) , Some ( ( 10 , 100 ) ) ) ;
458
+ assert_eq ! ( < StoredRange < Test >> :: get( ) , Some ( ( 10 , 100 ) ) ) ;
447
459
448
460
Historical :: prune_up_to ( 9 ) ;
449
- assert_eq ! ( StoredRange :: get( ) , Some ( ( 10 , 100 ) ) ) ;
461
+ assert_eq ! ( < StoredRange < Test >> :: get( ) , Some ( ( 10 , 100 ) ) ) ;
450
462
451
463
for i in 10 ..100 {
452
464
assert ! ( Historical :: historical_root( i) . is_some( ) )
453
465
}
454
466
455
467
Historical :: prune_up_to ( 99 ) ;
456
- assert_eq ! ( StoredRange :: get( ) , Some ( ( 99 , 100 ) ) ) ;
468
+ assert_eq ! ( < StoredRange < Test >> :: get( ) , Some ( ( 99 , 100 ) ) ) ;
457
469
458
470
Historical :: prune_up_to ( 100 ) ;
459
- assert_eq ! ( StoredRange :: get( ) , None ) ;
471
+ assert_eq ! ( < StoredRange < Test >> :: get( ) , None ) ;
460
472
461
473
for i in 99 ..199u64 {
462
474
set_next_validators ( vec ! [ i] ) ;
@@ -466,14 +478,14 @@ pub(crate) mod tests {
466
478
Session :: on_initialize ( i) ;
467
479
}
468
480
469
- assert_eq ! ( StoredRange :: get( ) , Some ( ( 100 , 200 ) ) ) ;
481
+ assert_eq ! ( < StoredRange < Test >> :: get( ) , Some ( ( 100 , 200 ) ) ) ;
470
482
471
483
for i in 100 ..200 {
472
484
assert ! ( Historical :: historical_root( i) . is_some( ) )
473
485
}
474
486
475
487
Historical :: prune_up_to ( 9999 ) ;
476
- assert_eq ! ( StoredRange :: get( ) , None ) ;
488
+ assert_eq ! ( < StoredRange < Test >> :: get( ) , None ) ;
477
489
478
490
for i in 100 ..200 {
479
491
assert ! ( Historical :: historical_root( i) . is_none( ) )
0 commit comments