From 6a930409d280d372e28ad1f34fb8d9bf910b7e5d Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 8 Oct 2024 11:07:35 +0200 Subject: [PATCH 1/3] chore: rm bad cap function --- crates/rpc/rpc-eth-api/src/helpers/call.rs | 7 ++++--- crates/rpc/rpc-eth-types/src/revm_utils.rs | 16 +++------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/crates/rpc/rpc-eth-api/src/helpers/call.rs b/crates/rpc/rpc-eth-api/src/helpers/call.rs index 819aff546d42..18d561c27418 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/call.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/call.rs @@ -28,8 +28,8 @@ use reth_rpc_eth_types::{ cache::db::{StateCacheDbRefMutWrapper, StateProviderTraitObjWrapper}, error::ensure_success, revm_utils::{ - apply_block_overrides, apply_state_overrides, caller_gas_allowance, - cap_tx_gas_limit_with_caller_allowance, get_precompiles, CallFees, + apply_block_overrides, apply_state_overrides, caller_gas_allowance, get_precompiles, + CallFees, }, simulate::{self, EthSimulateError}, EthApiError, RevertError, RpcInvalidTransactionError, StateCacheDb, @@ -379,8 +379,9 @@ pub trait EthCall: Call + LoadPendingBlock { let mut db = CacheDB::new(StateProviderDatabase::new(state)); if request.gas.is_none() && env.tx.gas_price > U256::ZERO { + let cap = caller_gas_allowance(&mut db, &env.tx)?; // no gas limit was provided in the request, so we need to cap the request's gas limit - cap_tx_gas_limit_with_caller_allowance(&mut db, &mut env.tx)?; + env.tx.gas_limit = cap.min(env.block.gas_limit).saturating_to(); } let from = request.from.unwrap_or_default(); diff --git a/crates/rpc/rpc-eth-types/src/revm_utils.rs b/crates/rpc/rpc-eth-types/src/revm_utils.rs index f6fbdc2f7ac8..1911ddaf80e0 100644 --- a/crates/rpc/rpc-eth-types/src/revm_utils.rs +++ b/crates/rpc/rpc-eth-types/src/revm_utils.rs @@ -23,25 +23,15 @@ pub fn get_precompiles(spec_id: SpecId) -> impl IntoIterator { Precompiles::new(spec).addresses().copied().map(Address::from) } -/// Caps the configured [`TxEnv`] `gas_limit` with the allowance of the caller. -pub fn cap_tx_gas_limit_with_caller_allowance(db: &mut DB, env: &mut TxEnv) -> EthResult<()> -where - DB: Database, - EthApiError: From<::Error>, -{ - if let Ok(gas_limit) = caller_gas_allowance(db, env)?.try_into() { - env.gas_limit = gas_limit; - } - - Ok(()) -} - /// Calculates the caller gas allowance. /// /// `allowance = (account.balance - tx.value) / tx.gas_price` /// /// Returns an error if the caller has insufficient funds. /// Caution: This assumes non-zero `env.gas_price`. Otherwise, zero allowance will be returned. +/// +/// Note: this takes [Database] because we the loaded sender can be reused for the following +/// operation like eth_call. pub fn caller_gas_allowance(db: &mut DB, env: &TxEnv) -> EthResult where DB: Database, From cd156c8d130f64c51344dbfbb7dee33afa1b0078 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 8 Oct 2024 11:33:53 +0200 Subject: [PATCH 2/3] fix docs again --- crates/rpc/rpc-eth-types/src/revm_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rpc/rpc-eth-types/src/revm_utils.rs b/crates/rpc/rpc-eth-types/src/revm_utils.rs index 1911ddaf80e0..6c85cade59de 100644 --- a/crates/rpc/rpc-eth-types/src/revm_utils.rs +++ b/crates/rpc/rpc-eth-types/src/revm_utils.rs @@ -31,7 +31,7 @@ pub fn get_precompiles(spec_id: SpecId) -> impl IntoIterator { /// Caution: This assumes non-zero `env.gas_price`. Otherwise, zero allowance will be returned. /// /// Note: this takes [Database] because we the loaded sender can be reused for the following -/// operation like eth_call. +/// operation like `eth_call`. pub fn caller_gas_allowance(db: &mut DB, env: &TxEnv) -> EthResult where DB: Database, From 015551ca6b5ba33487e64d050906aa35ade41b37 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 8 Oct 2024 15:02:55 +0200 Subject: [PATCH 3/3] typo --- crates/rpc/rpc-eth-types/src/revm_utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/rpc/rpc-eth-types/src/revm_utils.rs b/crates/rpc/rpc-eth-types/src/revm_utils.rs index 6c85cade59de..25c54fd46777 100644 --- a/crates/rpc/rpc-eth-types/src/revm_utils.rs +++ b/crates/rpc/rpc-eth-types/src/revm_utils.rs @@ -30,8 +30,8 @@ pub fn get_precompiles(spec_id: SpecId) -> impl IntoIterator { /// Returns an error if the caller has insufficient funds. /// Caution: This assumes non-zero `env.gas_price`. Otherwise, zero allowance will be returned. /// -/// Note: this takes [Database] because we the loaded sender can be reused for the following -/// operation like `eth_call`. +/// Note: this takes the mut [Database] trait because the loaded sender can be reused for the +/// following operation like `eth_call`. pub fn caller_gas_allowance(db: &mut DB, env: &TxEnv) -> EthResult where DB: Database,