From 02b183a1e8fee6176c36049806a15f3c6020990d Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Wed, 28 Jul 2021 09:00:49 +0000 Subject: [PATCH] feat: only enable sponsoring if rate limit is on --- --- a/pallets/evm-contract-helpers/src/eth.rs +++ b/pallets/evm-contract-helpers/src/eth.rs @@ -52,7 +52,7 @@ fn allowed(&self, contract: address, user: address) -> Result { self.0.consume_sload()?; - Ok(>::allowed(contract, user)) + Ok(>::allowed(contract, user, true)) } fn allowlist_enabled(&self, contract: address) -> Result { @@ -106,7 +106,7 @@ value: sp_core::U256, ) -> Option { // TODO: Extract to another OnMethodCall handler - if !>::allowed(*target, *source) { + if !>::allowed(*target, *source, true) { return Some(PrecompileOutput { exit_status: ExitReason::Revert(ExitRevert::Reverted), cost: 0, @@ -144,13 +144,14 @@ pub struct HelpersContractSponsoring(PhantomData<*const T>); impl SponsorshipHandler)> for HelpersContractSponsoring { fn get_sponsor(who: &H160, call: &(H160, Vec)) -> Option { - if >::get(&call.0) { + if >::get(&call.0) && >::allowed(call.0, *who, false) { let block_number = >::block_number() as T::BlockNumber; - let limit = >::get(&call.0); if let Some(last_tx_block) = >::get(&call.0, who) { - >::insert(&call.0, who, block_number); - let limit_time = last_tx_block + limit; + let rate_limit = >::get(&call.0); + let limit_time = last_tx_block + rate_limit; + if block_number > limit_time { + >::insert(&call.0, who, block_number); return Some(call.0); } } else { --- a/pallets/evm-contract-helpers/src/lib.rs +++ b/pallets/evm-contract-helpers/src/lib.rs @@ -76,9 +76,10 @@ >::insert(contract, rate_limit); } - pub fn allowed(contract: H160, user: H160) -> bool { + /// Default is returned if allowlist is disabled + pub fn allowed(contract: H160, user: H160, default: bool) -> bool { if !>::get(contract) { - return true; + return default; } >::get(&contract, &user) || >::get(&contract) == user } -- gitstuff