@@ -121,6 +121,15 @@ pub mod pallet {
121121 /// Max Authorities in use
122122 #[ pallet:: constant]
123123 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 > ;
124133 }
125134
126135 #[ pallet:: hooks]
@@ -323,6 +332,12 @@ pub mod pallet {
323332 /// A mapping from grandpa set ID to the index of the *most recent* session for which its
324333 /// members were responsible.
325334 ///
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+ ///
326341 /// TWOX-NOTE: `SetId` is not under user control.
327342 #[ pallet:: storage]
328343 #[ pallet:: getter( fn session_for_set) ]
@@ -643,10 +658,17 @@ where
643658 } ;
644659
645660 if res. is_ok ( ) {
646- CurrentSetId :: < T > :: mutate ( |s| {
661+ let current_set_id = CurrentSetId :: < T > :: mutate ( |s| {
647662 * s += 1 ;
648663 * 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
650672 } else {
651673 // either the session module signalled that the validators have changed
652674 // or the set was stalled. but since we didn't successfully schedule
@@ -659,8 +681,8 @@ where
659681 Self :: current_set_id ( )
660682 } ;
661683
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).
664686 let session_index = <pallet_session:: Pallet < T > >:: current_index ( ) ;
665687 SetIdSession :: < T > :: insert ( current_set_id, & session_index) ;
666688 }
0 commit comments