clear storage based on provided history depth

Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
Uncle Stinky
2025-06-18 13:47:13 +03:00
parent b5845924dc
commit 417de5a7b2
4 changed files with 51 additions and 4 deletions

View File

@@ -299,6 +299,9 @@ pub mod pallet {
#[pallet::constant]
type UnsignedPriority: Get<TransactionPriority>;
#[pallet::constant]
type HistoryDepth: Get<SessionIndex>;
type WeightInfo: WeightInfo;
}
@@ -339,11 +342,11 @@ pub mod pallet {
pub(super) type ReceivedClaps<T: Config> = StorageNMap<
_,
(
NMapKey<Twox64Concat, u32>,
NMapKey<Twox64Concat, SessionIndex>,
NMapKey<Twox64Concat, H256>,
NMapKey<Twox64Concat, H256>,
),
BoundedBTreeSet<u32, T::MaxAuthorities>,
BoundedBTreeSet<AuthIndex, T::MaxAuthorities>,
ValueQuery
>;
@@ -352,7 +355,7 @@ pub mod pallet {
pub(super) type ApplausesForTransaction<T: Config> = StorageNMap<
_,
(
NMapKey<Twox64Concat, u32>,
NMapKey<Twox64Concat, SessionIndex>,
NMapKey<Twox64Concat, H256>,
NMapKey<Twox64Concat, H256>,
),
@@ -928,10 +931,23 @@ impl<T: Config> Pallet<T> {
assert!(Authorities::<T>::get(&session_index).is_empty(), "Authorities are already initilized!");
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities)
.expect("more than the maximum number of authorities");
if let Some(target_session_index) = session_index.checked_sub(T::HistoryDepth::get()) {
Self::clear_history(&target_session_index);
}
Authorities::<T>::set(&session_index, bounded_authorities);
ClapsInSession::<T>::set(&session_index, Default::default());
}
fn clear_history(target_session_index: &SessionIndex) {
ClapsInSession::<T>::remove(target_session_index);
let mut cursor = ReceivedClaps::<T>::clear_prefix((target_session_index,), u32::MAX, None);
debug_assert!(cursor.maybe_cursor.is_none());
cursor = ApplausesForTransaction::<T>::clear_prefix((target_session_index,), u32::MAX, None);
debug_assert!(cursor.maybe_cursor.is_none());
}
fn calculate_median_claps(session_index: &SessionIndex) -> u32 {
let mut claps_in_session = ClapsInSession::<T>::get(session_index)
.values()