@@ -121,6 +121,15 @@ pub mod pallet {
121
121
/// Max Authorities in use
122
122
#[ pallet:: constant]
123
123
type MaxAuthorities : Get < u32 > ;
124
+
125
+ /// The maximum number of entries to keep in the set id to session index mapping.
126
+ ///
127
+ /// Since the `SetIdSession` map is only used for validating equivocations this
128
+ /// value should relate to the bonding duration of whatever staking system is
129
+ /// being used (if any). If equivocation handling is not enabled then this value
130
+ /// can be zero.
131
+ #[ pallet:: constant]
132
+ type MaxSetIdSessionEntries : Get < u64 > ;
124
133
}
125
134
126
135
#[ pallet:: hooks]
@@ -323,6 +332,12 @@ pub mod pallet {
323
332
/// A mapping from grandpa set ID to the index of the *most recent* session for which its
324
333
/// members were responsible.
325
334
///
335
+ /// This is only used for validating equivocation proofs. An equivocation proof must
336
+ /// contains a key-ownership proof for a given session, therefore we need a way to tie
337
+ /// together sessions and GRANDPA set ids, i.e. we need to validate that a validator
338
+ /// was the owner of a given key on a given session, and what the active set ID was
339
+ /// during that session.
340
+ ///
326
341
/// TWOX-NOTE: `SetId` is not under user control.
327
342
#[ pallet:: storage]
328
343
#[ pallet:: getter( fn session_for_set) ]
@@ -643,10 +658,17 @@ where
643
658
} ;
644
659
645
660
if res. is_ok ( ) {
646
- CurrentSetId :: < T > :: mutate ( |s| {
661
+ let current_set_id = CurrentSetId :: < T > :: mutate ( |s| {
647
662
* s += 1 ;
648
663
* s
649
- } )
664
+ } ) ;
665
+
666
+ let max_set_id_session_entries = T :: MaxSetIdSessionEntries :: get ( ) . max ( 1 ) ;
667
+ if current_set_id >= max_set_id_session_entries {
668
+ SetIdSession :: < T > :: remove ( current_set_id - max_set_id_session_entries) ;
669
+ }
670
+
671
+ current_set_id
650
672
} else {
651
673
// either the session module signalled that the validators have changed
652
674
// or the set was stalled. but since we didn't successfully schedule
@@ -659,8 +681,8 @@ where
659
681
Self :: current_set_id ( )
660
682
} ;
661
683
662
- // if we didn't issue a change, we update the mapping to note that the current
663
- // set corresponds to the latest equivalent session (i.e. now).
684
+ // update the mapping to note that the current set corresponds to the
685
+ // latest equivalent session (i.e. now).
664
686
let session_index = <pallet_session:: Pallet < T > >:: current_index ( ) ;
665
687
SetIdSession :: < T > :: insert ( current_set_id, & session_index) ;
666
688
}
0 commit comments