From 82bedeffe28d2a582c0d67a723aa65bc693a6493 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 18 Nov 2021 13:45:18 +0000 Subject: [PATCH] fix: evm sponsoring allowlist handling --- --- a/pallets/evm-contract-helpers/src/eth.rs +++ b/pallets/evm-contract-helpers/src/eth.rs @@ -59,7 +59,8 @@ fn allowed(&self, contract_address: address, user: address) -> Result { self.0.consume_sload()?; - Ok(>::allowed(contract_address, user, true)) + Ok(>::allowed(contract_address, user) + || !>::get(contract_address)) } fn allowlist_enabled(&self, contract_address: address) -> Result { @@ -113,7 +114,7 @@ value: sp_core::U256, ) -> Option { // TODO: Extract to another OnMethodCall handler - if !>::allowed(*target, *source, true) { + if >::get(target) && !>::allowed(*target, *source) { return Some(PrecompileOutput { exit_status: ExitReason::Revert(ExitRevert::Reverted), cost: 0, @@ -151,22 +152,26 @@ pub struct HelpersContractSponsoring(PhantomData<*const T>); impl SponsorshipHandler)> for HelpersContractSponsoring { fn get_sponsor(who: &H160, call: &(H160, Vec)) -> Option { - if >::get(&call.0) && >::allowed(call.0, *who, false) { - let block_number = >::block_number() as T::BlockNumber; - if let Some(last_tx_block) = >::get(&call.0, who) { - let rate_limit = >::get(&call.0); - let limit_time = last_tx_block + rate_limit; + if !>::get(&call.0) { + return None; + } + if !>::allowed(call.0, *who) { + return None; + } + let block_number = >::block_number() as T::BlockNumber; - if block_number > limit_time { - >::insert(&call.0, who, block_number); - return Some(call.0); - } - } else { - >::insert(&call.0, who, block_number); - return Some(call.0); + if let Some(last_tx_block) = >::get(&call.0, who) { + let limit = >::get(&call.0); + + let timeout = last_tx_block + limit.into(); + if block_number < timeout { + return None; } } - None + + >::insert(&call.0, who, block_number); + + Some(call.0) } } --- a/pallets/evm-contract-helpers/src/lib.rs +++ b/pallets/evm-contract-helpers/src/lib.rs @@ -76,11 +76,7 @@ >::insert(contract, rate_limit); } - /// Default is returned if allowlist is disabled - pub fn allowed(contract: H160, user: H160, default: bool) -> bool { - if !>::get(contract) { - return default; - } + pub fn allowed(contract: H160, user: H160) -> bool { >::get(&contract, &user) || >::get(&contract) == user } -- gitstuff