git.delta.rocks / unique-network / refs/commits / d78c2fedc380

difftreelog

chore store limits in BTreeMap

Grigoriy Simonov2022-09-13parent: #7e9e885.patch.diff
in: master

3 files changed

modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/eth.rs
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -26,7 +26,7 @@
 };
 use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};
 use pallet_evm_transaction_payment::CallContext;
-use sp_core::H160;
+use sp_core::{H160, U256};
 use up_data_structs::SponsorshipState;
 use crate::{
 	AllowlistEnabled, Config, Owner, Pallet, SponsorBasket, SponsoringFeeLimit,
@@ -258,12 +258,13 @@
 		fee_limit: uint256,
 	) -> Result<void> {
 		<Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;
-		<Pallet<T>>::set_sponsoring_fee_limit(contract_address, fee_limit.into());
+		<Pallet<T>>::set_sponsoring_fee_limit(contract_address, fee_limit.into())
+			.map_err(dispatch_to_evm::<T>)?;
 		Ok(())
 	}
 
 	fn get_sponsoring_fee_limit(&self, contract_address: address) -> Result<uint256> {
-		Ok(<SponsoringFeeLimit<T>>::get(contract_address, 0xffffffff))
+		Ok(get_sponsoring_fee_limit::<T>(contract_address))
 	}
 
 	/// Is specified user present in contract allow list
@@ -413,7 +414,7 @@
 			}
 		}
 
-		let sponsored_fee_limit = <SponsoringFeeLimit<T>>::get(contract_address, 0xffffffff);
+		let sponsored_fee_limit = get_sponsoring_fee_limit::<T>(contract_address);
 
 		if call_context.max_fee > sponsored_fee_limit {
 			return None;
@@ -425,5 +426,12 @@
 	}
 }
 
+fn get_sponsoring_fee_limit<T: Config>(contract_address: address) -> uint256 {
+	<SponsoringFeeLimit<T>>::get(contract_address)
+		.get(&0xffffffff)
+		.cloned()
+		.unwrap_or(U256::MAX)
+}
+
 generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);
 generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);
modifiedpallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/lib.rs
+++ b/pallets/evm-contract-helpers/src/lib.rs
@@ -22,8 +22,12 @@
 pub use pallet::*;
 pub use eth::*;
 use scale_info::TypeInfo;
+use frame_support::storage::bounded_btree_map::BoundedBTreeMap;
 pub mod eth;
 
+/// Maximum number of methods per contract that could have fee limit
+pub const MAX_FEE_LIMITED_METHODS: u32 = 5;
+
 #[frame_support::pallet]
 pub mod pallet {
 	pub use super::*;
@@ -48,9 +52,6 @@
 		/// In case of enabled sponsoring, but no sponsoring rate limit set,
 		/// this value will be used implicitly
 		type DefaultSponsoringRateLimit: Get<Self::BlockNumber>;
-		/// In case of enabled sponsoring, but no sponsoring fee limit set,
-		/// this value will be used implicitly
-		type DefaultSponsoringFeeLimit: Get<U256>;
 	}
 
 	#[pallet::error]
@@ -60,6 +61,9 @@
 
 		/// No pending sponsor for contract.
 		NoPendingSponsor,
+
+		/// Number of methods that sponsored limit is defined for exceeds maximum.
+		TooManyMethodsHaveSponsoredLimit,
 	}
 
 	#[pallet::pallet]
@@ -121,14 +125,11 @@
 	/// * **Key2** - sponsored user address.
 	/// * **Value** - last sponsored block number.
 	#[pallet::storage]
-	pub(super) type SponsoringFeeLimit<T: Config> = StorageDoubleMap<
-		Hasher1 = Twox128,
-		Key1 = H160,
-		Hasher2 = Blake2_128Concat,
-		Key2 = u32,
-		Value = U256,
+	pub(super) type SponsoringFeeLimit<T: Config> = StorageMap<
+		Hasher = Twox128,
+		Key = H160,
+		Value = BoundedBTreeMap<u32, U256, ConstU32<MAX_FEE_LIMITED_METHODS>>,
 		QueryKind = ValueQuery,
-		OnEmpty = T::DefaultSponsoringFeeLimit,
 	>;
 
 	#[pallet::storage]
@@ -368,8 +369,13 @@
 		}
 
 		/// Set maximum for gas limit of transaction
-		pub fn set_sponsoring_fee_limit(contract: H160, fee_limit: U256) {
-			<SponsoringFeeLimit<T>>::insert(contract, 0xffffffff, fee_limit);
+		pub fn set_sponsoring_fee_limit(contract: H160, fee_limit: U256) -> DispatchResult {
+			<SponsoringFeeLimit<T>>::try_mutate(contract, |limits_map| {
+				limits_map
+					.try_insert(0xffffffff, fee_limit)
+					.map_err(|_| <Error<T>>::TooManyMethodsHaveSponsoredLimit)
+			})?;
+			Ok(())
 		}
 
 		/// Is user added to allowlist, or he is owner of specified contract
modifiedruntime/common/config/ethereum.rsdiffbeforeafterboth
9 runtime_common::{9 runtime_common::{
10 dispatch::CollectionDispatchT,10 dispatch::CollectionDispatchT, ethereum::sponsoring::EvmSponsorshipHandler,
11 ethereum::sponsoring::EvmSponsorshipHandler,
12 config::sponsoring::{DefaultSponsoringFeeLimit, DefaultSponsoringRateLimit},11 config::sponsoring::DefaultSponsoringRateLimit, DealWithFees,
13 DealWithFees,
14 },12 },
15 Runtime, Aura, Balances, Event, ChainId,13 Runtime, Aura, Balances, Event, ChainId,
117 type Event = Event;115 type Event = Event;
118 type ContractAddress = HelpersContractAddress;116 type ContractAddress = HelpersContractAddress;
119 type DefaultSponsoringRateLimit = DefaultSponsoringRateLimit;117 type DefaultSponsoringRateLimit = DefaultSponsoringRateLimit;
120 type DefaultSponsoringFeeLimit = DefaultSponsoringFeeLimit;
121}118}
122119
123impl pallet_evm_coder_substrate::Config for Runtime {}120impl pallet_evm_coder_substrate::Config for Runtime {}