Skip to content

Commit

Permalink
chore: rm bad cap function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Oct 8, 2024
1 parent 5e288b2 commit 6a93040
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
7 changes: 4 additions & 3 deletions crates/rpc/rpc-eth-api/src/helpers/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 3 additions & 13 deletions crates/rpc/rpc-eth-types/src/revm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,15 @@ pub fn get_precompiles(spec_id: SpecId) -> impl IntoIterator<Item = Address> {
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>(db: &mut DB, env: &mut TxEnv) -> EthResult<()>
where
DB: Database,
EthApiError: From<<DB as Database>::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>(db: &mut DB, env: &TxEnv) -> EthResult<U256>
where
DB: Database,
Expand Down

0 comments on commit 6a93040

Please sign in to comment.