frame/beefy: prune entries in set id session mapping (#13411)

Add limit for the number of entries in the `SetIdSession` mapping.
For example, it can be set to the bonding duration (in sessions).

Signed-off-by: acatangiu <adrian@parity.io>
This commit is contained in:
Adrian Catangiu
2023-02-21 12:50:41 +02:00
committed by GitHub
parent 7a10154188
commit 67c250fecb
4 changed files with 59 additions and 1 deletions
+23 -1
View File
@@ -99,8 +99,18 @@ pub mod pallet {
type HandleEquivocation: HandleEquivocation<Self>;
/// The maximum number of authorities that can be added.
#[pallet::constant]
type MaxAuthorities: Get<u32>;
/// The maximum number of entries to keep in the set id to session index mapping.
///
/// Since the `SetIdSession` map is only used for validating equivocations this
/// value should relate to the bonding duration of whatever staking system is
/// being used (if any). If equivocation handling is not enabled then this value
/// can be zero.
#[pallet::constant]
type MaxSetIdSessionEntries: Get<u64>;
/// A hook to act on the new BEEFY validator set.
///
/// For some applications it might be beneficial to make the BEEFY validator set available
@@ -136,6 +146,12 @@ pub mod pallet {
/// A mapping from BEEFY set ID to the index of the *most recent* session for which its
/// members were responsible.
///
/// This is only used for validating equivocation proofs. An equivocation proof must
/// contains a key-ownership proof for a given session, therefore we need a way to tie
/// together sessions and BEEFY set ids, i.e. we need to validate that a validator
/// was the owner of a given key on a given session, and what the active set ID was
/// during that session.
///
/// TWOX-NOTE: `ValidatorSetId` is not under user control.
#[pallet::storage]
#[pallet::getter(fn session_for_set)]
@@ -462,9 +478,15 @@ where
// We want to have at least one BEEFY mandatory block per session.
Self::change_authorities(bounded_next_authorities, bounded_next_queued_authorities);
let validator_set_id = Self::validator_set_id();
// Update the mapping for the new set id that corresponds to the latest session (i.e. now).
let session_index = <pallet_session::Pallet<T>>::current_index();
SetIdSession::<T>::insert(Self::validator_set_id(), &session_index);
SetIdSession::<T>::insert(validator_set_id, &session_index);
// Prune old entry if limit reached.
let max_set_id_session_entries = T::MaxSetIdSessionEntries::get().max(1);
if validator_set_id >= max_set_id_session_entries {
SetIdSession::<T>::remove(validator_set_id - max_set_id_session_entries);
}
}
fn on_disabled(i: u32) {