From 417de5a7b245dae57362cab85021f30fb3e9a468 Mon Sep 17 00:00:00 2001 From: Uncle Stinky Date: Wed, 18 Jun 2025 13:47:13 +0300 Subject: [PATCH] clear storage based on provided history depth Signed-off-by: Uncle Stinky --- pallets/slow-clap/Cargo.toml | 2 +- pallets/slow-clap/src/lib.rs | 22 +++++++++++++++++++--- pallets/slow-clap/src/mock.rs | 2 ++ pallets/slow-clap/src/tests.rs | 29 +++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index e87e73f..88e0b1b 100644 --- a/pallets/slow-clap/Cargo.toml +++ b/pallets/slow-clap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-slow-clap" -version = "0.3.24" +version = "0.3.25" description = "Applause protocol for the EVM bridge" license.workspace = true authors.workspace = true diff --git a/pallets/slow-clap/src/lib.rs b/pallets/slow-clap/src/lib.rs index bdadbfd..30fb2c8 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -299,6 +299,9 @@ pub mod pallet { #[pallet::constant] type UnsignedPriority: Get; + #[pallet::constant] + type HistoryDepth: Get; + type WeightInfo: WeightInfo; } @@ -339,11 +342,11 @@ pub mod pallet { pub(super) type ReceivedClaps = StorageNMap< _, ( - NMapKey, + NMapKey, NMapKey, NMapKey, ), - BoundedBTreeSet, + BoundedBTreeSet, ValueQuery >; @@ -352,7 +355,7 @@ pub mod pallet { pub(super) type ApplausesForTransaction = StorageNMap< _, ( - NMapKey, + NMapKey, NMapKey, NMapKey, ), @@ -928,10 +931,23 @@ impl Pallet { assert!(Authorities::::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::::set(&session_index, bounded_authorities); ClapsInSession::::set(&session_index, Default::default()); } + fn clear_history(target_session_index: &SessionIndex) { + ClapsInSession::::remove(target_session_index); + let mut cursor = ReceivedClaps::::clear_prefix((target_session_index,), u32::MAX, None); + debug_assert!(cursor.maybe_cursor.is_none()); + cursor = ApplausesForTransaction::::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::::get(session_index) .values() diff --git a/pallets/slow-clap/src/mock.rs b/pallets/slow-clap/src/mock.rs index eb221c7..d1045f2 100644 --- a/pallets/slow-clap/src/mock.rs +++ b/pallets/slow-clap/src/mock.rs @@ -187,6 +187,7 @@ pallet_staking_reward_curve::build! { parameter_types! { pub static ExistentialDeposit: u64 = 2; pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; + pub const HistoryDepth: u32 = 10; } #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] @@ -212,6 +213,7 @@ impl Config for Runtime { type ApplauseThreshold = ConstU32<50>; type OffenceThreshold = ConstU32<75>; type UnsignedPriority = ConstU64<{ 1 << 20 }>; + type HistoryDepth = HistoryDepth; type WeightInfo = (); } diff --git a/pallets/slow-clap/src/tests.rs b/pallets/slow-clap/src/tests.rs index 8e1cb6f..031a485 100644 --- a/pallets/slow-clap/src/tests.rs +++ b/pallets/slow-clap/src/tests.rs @@ -247,6 +247,35 @@ fn should_make_http_call_and_parse_logs() { }); } +#[test] +fn should_clear_sesions_based_on_history_depth() { + let (network_id, transaction_hash, unique_transaction_hash) = + generate_unique_hash(None, None, None, None); + + new_test_ext().execute_with(|| { + let session_index = advance_session_and_get_index(); + let history_depth = HistoryDepth::get(); + let storage_key = (session_index, transaction_hash, unique_transaction_hash); + + assert_claps_info_correct(&storage_key, &session_index, 0); + assert_eq!(pallet::ApplausesForTransaction::::get(&storage_key), false); + + assert_ok!(do_clap_from(session_index, network_id, 0, false)); + assert_ok!(do_clap_from(session_index, network_id, 1, false)); + assert_ok!(do_clap_from(session_index, network_id, 2, false)); + + assert_claps_info_correct(&storage_key, &session_index, 3); + assert_eq!(pallet::ApplausesForTransaction::::get(&storage_key), true); + + for _ in 0..history_depth { + advance_session(); + } + + assert_claps_info_correct(&storage_key, &session_index, 0); + assert_eq!(pallet::ApplausesForTransaction::::get(&storage_key), false); + }); +} + #[test] fn should_collect_commission_accordingly() { let (network_id, _, _) = generate_unique_hash(None, None, None, None);