difftreelog
chore store limits in BTreeMap
in: master
3 files changed
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth26};26};27use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};27use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder, dispatch_to_evm};28use pallet_evm_transaction_payment::CallContext;28use pallet_evm_transaction_payment::CallContext;29use sp_core::H160;29use sp_core::{H160, U256};30use up_data_structs::SponsorshipState;30use up_data_structs::SponsorshipState;31use crate::{31use crate::{32 AllowlistEnabled, Config, Owner, Pallet, SponsorBasket, SponsoringFeeLimit,32 AllowlistEnabled, Config, Owner, Pallet, SponsorBasket, SponsoringFeeLimit,259 ) -> Result<void> {259 ) -> Result<void> {260 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;260 <Pallet<T>>::ensure_owner(contract_address, caller).map_err(dispatch_to_evm::<T>)?;261 <Pallet<T>>::set_sponsoring_fee_limit(contract_address, fee_limit.into());261 <Pallet<T>>::set_sponsoring_fee_limit(contract_address, fee_limit.into())262 .map_err(dispatch_to_evm::<T>)?;262 Ok(())263 Ok(())263 }264 }264265265 fn get_sponsoring_fee_limit(&self, contract_address: address) -> Result<uint256> {266 fn get_sponsoring_fee_limit(&self, contract_address: address) -> Result<uint256> {266 Ok(<SponsoringFeeLimit<T>>::get(contract_address, 0xffffffff))267 Ok(get_sponsoring_fee_limit::<T>(contract_address))267 }268 }268269269 /// Is specified user present in contract allow list270 /// Is specified user present in contract allow list413 }414 }414 }415 }415416416 let sponsored_fee_limit = <SponsoringFeeLimit<T>>::get(contract_address, 0xffffffff);417 let sponsored_fee_limit = get_sponsoring_fee_limit::<T>(contract_address);417418418 if call_context.max_fee > sponsored_fee_limit {419 if call_context.max_fee > sponsored_fee_limit {419 return None;420 return None;425 }426 }426}427}428429fn get_sponsoring_fee_limit<T: Config>(contract_address: address) -> uint256 {430 <SponsoringFeeLimit<T>>::get(contract_address)431 .get(&0xffffffff)432 .cloned()433 .unwrap_or(U256::MAX)434}427435428generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);436generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);429generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);437generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);pallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth22pub use pallet::*;22pub use pallet::*;23pub use eth::*;23pub use eth::*;24use scale_info::TypeInfo;24use scale_info::TypeInfo;25use frame_support::storage::bounded_btree_map::BoundedBTreeMap;25pub mod eth;26pub mod eth;2728/// Maximum number of methods per contract that could have fee limit29pub const MAX_FEE_LIMITED_METHODS: u32 = 5;263027#[frame_support::pallet]31#[frame_support::pallet]28pub mod pallet {32pub mod pallet {48 /// In case of enabled sponsoring, but no sponsoring rate limit set,52 /// In case of enabled sponsoring, but no sponsoring rate limit set,49 /// this value will be used implicitly53 /// this value will be used implicitly50 type DefaultSponsoringRateLimit: Get<Self::BlockNumber>;54 type DefaultSponsoringRateLimit: Get<Self::BlockNumber>;51 /// In case of enabled sponsoring, but no sponsoring fee limit set,52 /// this value will be used implicitly53 type DefaultSponsoringFeeLimit: Get<U256>;54 }55 }555656 #[pallet::error]57 #[pallet::error]61 /// No pending sponsor for contract.62 /// No pending sponsor for contract.62 NoPendingSponsor,63 NoPendingSponsor,6465 /// Number of methods that sponsored limit is defined for exceeds maximum.66 TooManyMethodsHaveSponsoredLimit,63 }67 }646865 #[pallet::pallet]69 #[pallet::pallet]121 /// * **Key2** - sponsored user address.125 /// * **Key2** - sponsored user address.122 /// * **Value** - last sponsored block number.126 /// * **Value** - last sponsored block number.123 #[pallet::storage]127 #[pallet::storage]124 pub(super) type SponsoringFeeLimit<T: Config> = StorageDoubleMap<128 pub(super) type SponsoringFeeLimit<T: Config> = StorageMap<125 Hasher1 = Twox128,129 Hasher = Twox128,126 Key1 = H160,130 Key = H160,127 Hasher2 = Blake2_128Concat,128 Key2 = u32,129 Value = U256,131 Value = BoundedBTreeMap<u32, U256, ConstU32<MAX_FEE_LIMITED_METHODS>>,130 QueryKind = ValueQuery,132 QueryKind = ValueQuery,131 OnEmpty = T::DefaultSponsoringFeeLimit,132 >;133 >;133134134 #[pallet::storage]135 #[pallet::storage]368 }369 }369370370 /// Set maximum for gas limit of transaction371 /// Set maximum for gas limit of transaction371 pub fn set_sponsoring_fee_limit(contract: H160, fee_limit: U256) {372 pub fn set_sponsoring_fee_limit(contract: H160, fee_limit: U256) -> DispatchResult {372 <SponsoringFeeLimit<T>>::insert(contract, 0xffffffff, fee_limit);373 <SponsoringFeeLimit<T>>::try_mutate(contract, |limits_map| {374 limits_map375 .try_insert(0xffffffff, fee_limit)376 .map_err(|_| <Error<T>>::TooManyMethodsHaveSponsoredLimit)377 })?;378 Ok(())373 }379 }374380375 /// Is user added to allowlist, or he is owner of specified contract381 /// Is user added to allowlist, or he is owner of specified contractruntime/common/config/ethereum.rsdiffbeforeafterboth9 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}122119123impl pallet_evm_coder_substrate::Config for Runtime {}120impl pallet_evm_coder_substrate::Config for Runtime {}