clear storage based on provided history depth
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
		
							parent
							
								
									b5845924dc
								
							
						
					
					
						commit
						417de5a7b2
					
				| @ -1,6 +1,6 @@ | |||||||
| [package] | [package] | ||||||
| name = "ghost-slow-clap" | name = "ghost-slow-clap" | ||||||
| version = "0.3.24" | version = "0.3.25" | ||||||
| description = "Applause protocol for the EVM bridge" | description = "Applause protocol for the EVM bridge" | ||||||
| license.workspace = true | license.workspace = true | ||||||
| authors.workspace = true | authors.workspace = true | ||||||
|  | |||||||
| @ -299,6 +299,9 @@ pub mod pallet { | |||||||
|         #[pallet::constant] |         #[pallet::constant] | ||||||
|         type UnsignedPriority: Get<TransactionPriority>; |         type UnsignedPriority: Get<TransactionPriority>; | ||||||
| 
 | 
 | ||||||
|  |         #[pallet::constant] | ||||||
|  |         type HistoryDepth: Get<SessionIndex>; | ||||||
|  | 
 | ||||||
|         type WeightInfo: WeightInfo; |         type WeightInfo: WeightInfo; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -339,11 +342,11 @@ pub mod pallet { | |||||||
|     pub(super) type ReceivedClaps<T: Config> = StorageNMap< |     pub(super) type ReceivedClaps<T: Config> = StorageNMap< | ||||||
|         _, |         _, | ||||||
|         ( |         ( | ||||||
|             NMapKey<Twox64Concat, u32>, |             NMapKey<Twox64Concat, SessionIndex>, | ||||||
|             NMapKey<Twox64Concat, H256>, |             NMapKey<Twox64Concat, H256>, | ||||||
|             NMapKey<Twox64Concat, H256>, |             NMapKey<Twox64Concat, H256>, | ||||||
|         ), |         ), | ||||||
|         BoundedBTreeSet<u32, T::MaxAuthorities>, |         BoundedBTreeSet<AuthIndex, T::MaxAuthorities>, | ||||||
|         ValueQuery |         ValueQuery | ||||||
|     >; |     >; | ||||||
|     
 |     
 | ||||||
| @ -352,7 +355,7 @@ pub mod pallet { | |||||||
|     pub(super) type ApplausesForTransaction<T: Config> = StorageNMap< |     pub(super) type ApplausesForTransaction<T: Config> = StorageNMap< | ||||||
|         _, |         _, | ||||||
|         ( |         ( | ||||||
|             NMapKey<Twox64Concat, u32>, |             NMapKey<Twox64Concat, SessionIndex>, | ||||||
|             NMapKey<Twox64Concat, H256>, |             NMapKey<Twox64Concat, H256>, | ||||||
|             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!"); |         assert!(Authorities::<T>::get(&session_index).is_empty(), "Authorities are already initilized!"); | ||||||
|         let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities) |         let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities) | ||||||
|             .expect("more than the maximum number of 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); |         Authorities::<T>::set(&session_index, bounded_authorities); | ||||||
|         ClapsInSession::<T>::set(&session_index, Default::default()); |         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 { |     fn calculate_median_claps(session_index: &SessionIndex) -> u32 { | ||||||
|         let mut claps_in_session = ClapsInSession::<T>::get(session_index) |         let mut claps_in_session = ClapsInSession::<T>::get(session_index) | ||||||
|             .values() |             .values() | ||||||
|  | |||||||
| @ -187,6 +187,7 @@ pallet_staking_reward_curve::build! { | |||||||
| parameter_types! { | parameter_types! { | ||||||
|     pub static ExistentialDeposit: u64 = 2; |     pub static ExistentialDeposit: u64 = 2; | ||||||
|     pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; |     pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; | ||||||
|  |     pub const HistoryDepth: u32 = 10; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] | #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] | ||||||
| @ -212,6 +213,7 @@ impl Config for Runtime { | |||||||
|     type ApplauseThreshold = ConstU32<50>; |     type ApplauseThreshold = ConstU32<50>; | ||||||
|     type OffenceThreshold = ConstU32<75>; |     type OffenceThreshold = ConstU32<75>; | ||||||
|     type UnsignedPriority = ConstU64<{ 1 << 20 }>; |     type UnsignedPriority = ConstU64<{ 1 << 20 }>; | ||||||
|  |     type HistoryDepth = HistoryDepth; | ||||||
| 
 | 
 | ||||||
|     type WeightInfo = (); |     type WeightInfo = (); | ||||||
| } | } | ||||||
|  | |||||||
| @ -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::<Runtime>::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::<Runtime>::get(&storage_key), true); | ||||||
|  | 
 | ||||||
|  |         for _ in 0..history_depth { | ||||||
|  |             advance_session(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         assert_claps_info_correct(&storage_key, &session_index, 0); | ||||||
|  |         assert_eq!(pallet::ApplausesForTransaction::<Runtime>::get(&storage_key), false); | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[test] | #[test] | ||||||
| fn should_collect_commission_accordingly() { | fn should_collect_commission_accordingly() { | ||||||
|     let (network_id, _, _) = generate_unique_hash(None, None, None, None); |     let (network_id, _, _) = generate_unique_hash(None, None, None, None); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user