From 86bb60ca6f09848f1de9ef949794e3f2a5df051a Mon Sep 17 00:00:00 2001 From: Vadim Smirnov Date: Sun, 11 Feb 2024 18:51:43 +0100 Subject: [PATCH 1/4] feat(pallet-gear-staking-rewards): Use bounded btree set --- pallets/staking-rewards/src/lib.rs | 18 +++++++++--------- runtime/vara/src/lib.rs | 1 + 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pallets/staking-rewards/src/lib.rs b/pallets/staking-rewards/src/lib.rs index f6b0d9e16de..4d397f8cecb 100644 --- a/pallets/staking-rewards/src/lib.rs +++ b/pallets/staking-rewards/src/lib.rs @@ -64,7 +64,7 @@ use parity_scale_codec::{Decode, Encode}; pub use scale_info::TypeInfo; use sp_runtime::{ traits::{AccountIdConversion, Saturating, StaticLookup}, - PerThing, Perquintill, + BoundedBTreeSet, PerThing, Perquintill, }; use sp_std::{collections::btree_set::BTreeSet, vec::Vec}; @@ -105,7 +105,6 @@ pub mod pallet { #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] - #[pallet::without_storage_info] pub struct Pallet(PhantomData); #[pallet::config] @@ -145,6 +144,9 @@ pub mod pallet { #[pallet::constant] type Falloff: Get; + /// This is enforced in the code; the filtered accounts size can not exceed this limit. + type MaxFilteredAccounts: Get; + /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; } @@ -171,7 +173,8 @@ pub mod pallet { /// During the 1st year the non-stakeable amount is accounted for as a fixed fraction of TTS. #[pallet::storage] #[pallet::getter(fn filtered_accounts)] - pub type FilteredAccounts = StorageValue<_, BTreeSet, ValueQuery>; + pub type FilteredAccounts = + StorageValue<_, BoundedBTreeSet, ValueQuery>; #[pallet::genesis_config] #[derive(frame_support::DefaultNoBound)] @@ -199,12 +202,9 @@ pub mod pallet { TargetInflation::::put(self.target_inflation); IdealStakingRatio::::put(self.ideal_stake); NonStakeableShare::::put(self.non_stakeable); - FilteredAccounts::::put( - self.filtered_accounts - .iter() - .cloned() - .collect::>(), - ); + let bounded_accounts = + BoundedBTreeSet::::try_from(self.filtered_accounts.iter().cloned().collect::>()).expect("Filtered accounts vec too big"); + FilteredAccounts::::put(bounded_accounts); } } diff --git a/runtime/vara/src/lib.rs b/runtime/vara/src/lib.rs index 6c4fbfbec55..5981f5407c1 100644 --- a/runtime/vara/src/lib.rs +++ b/runtime/vara/src/lib.rs @@ -463,6 +463,7 @@ impl pallet_gear_staking_rewards::Config for Runtime { type MinInflation = MinInflation; type MaxROI = MaxROI; type Falloff = Falloff; + type MaxFilteredAccounts = ConstU32<500>; type WeightInfo = pallet_gear_staking_rewards::weights::SubstrateWeight; } From 06273d95b1d69aa5423c02f536cf125c1982e128 Mon Sep 17 00:00:00 2001 From: Vadim Smirnov Date: Sun, 11 Feb 2024 19:04:48 +0100 Subject: [PATCH 2/4] rm without_storage_info macro in pallet-gear-payment --- pallets/payment/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/payment/src/lib.rs b/pallets/payment/src/lib.rs index f189d49fa0c..af778854a14 100644 --- a/pallets/payment/src/lib.rs +++ b/pallets/payment/src/lib.rs @@ -357,6 +357,5 @@ pub mod pallet { } #[pallet::pallet] - #[pallet::without_storage_info] pub struct Pallet(_); } From 54ca7333cefb64e16de130e0213abfc12fc1d28b Mon Sep 17 00:00:00 2001 From: Vadim Smirnov Date: Sat, 2 Mar 2024 15:50:25 +0100 Subject: [PATCH 3/4] rm without_storage_info in gas pallet --- pallets/gas/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/gas/src/lib.rs b/pallets/gas/src/lib.rs index b2efbfabeb8..47cfe488820 100644 --- a/pallets/gas/src/lib.rs +++ b/pallets/gas/src/lib.rs @@ -172,7 +172,6 @@ pub mod pallet { #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] - #[pallet::without_storage_info] pub struct Pallet(_); // Gas pallet error. From 501248c5912e3285a2684a26b1d34693bded584a Mon Sep 17 00:00:00 2001 From: Vadim Smirnov Date: Sat, 2 Mar 2024 15:50:45 +0100 Subject: [PATCH 4/4] Use BoundedBTreeSet for QuickCache and limit 100 --- pallets/gear-builtin/src/lib.rs | 14 +++++++++++--- runtime/vara/src/lib.rs | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pallets/gear-builtin/src/lib.rs b/pallets/gear-builtin/src/lib.rs index 1bcf0bf28bf..8dcb6ea7b55 100644 --- a/pallets/gear-builtin/src/lib.rs +++ b/pallets/gear-builtin/src/lib.rs @@ -64,6 +64,7 @@ use gear_core::{ use impl_trait_for_tuples::impl_for_tuples; use pallet_gear::{BuiltinCache, BuiltinDispatcher, BuiltinDispatcherFactory, HandleFn}; use parity_scale_codec::{Decode, Encode}; +use sp_runtime::BoundedBTreeSet; use sp_std::prelude::*; pub use pallet::*; @@ -153,7 +154,6 @@ pub mod pallet { // This pallet doesn't define a storage version because it doesn't use any storage #[pallet::pallet] - #[pallet::without_storage_info] pub struct Pallet(PhantomData); #[pallet::config] @@ -161,13 +161,16 @@ pub mod pallet { /// The builtin actor type. type Builtins: BuiltinCollection; + /// This is enforced in the code; the quick cache size can not exceed this limit. + type MaxQuickCache: Get; + /// Weight cost incurred by builtin actors calls. type WeightInfo: WeightInfo; } #[pallet::storage] #[pallet::getter(fn quick_cache)] - pub type QuickCache = StorageValue<_, BTreeSet>; + pub type QuickCache = StorageValue<_, BoundedBTreeSet>; #[pallet::hooks] impl Hooks> for Pallet {} @@ -202,7 +205,12 @@ impl BuiltinCache for Pallet { // Populate the cache at the first call let registry = BuiltinRegistry::::new(); QuickCache::::mutate(|keys| { - *keys = Some(registry.registry.keys().cloned().collect()) + let set: BoundedBTreeSet = + BoundedBTreeSet::::try_from( + registry.registry.keys().cloned().collect::>(), + ) + .expect("Filtered accounts vec too big"); + *keys = Some(set) }); } Self::quick_cache() diff --git a/runtime/vara/src/lib.rs b/runtime/vara/src/lib.rs index f3cc487e4a2..a22964aafdd 100644 --- a/runtime/vara/src/lib.rs +++ b/runtime/vara/src/lib.rs @@ -1064,6 +1064,7 @@ parameter_types! { impl pallet_gear_builtin::Config for Runtime { type Builtins = BuiltinActors; + type MaxQuickCache = ConstU32<100>; type WeightInfo = pallet_gear_builtin::weights::SubstrateWeight; }