Skip to main content

reth_rpc_eth_api/helpers/
estimate.rs

1//! Estimate gas needed implementation
2
3use super::{Call, LoadPendingBlock};
4use crate::{AsEthApiError, FromEthApiError, IntoEthApiError};
5use alloy_evm::overrides::{apply_block_overrides, apply_state_overrides};
6use alloy_network::TransactionBuilder;
7use alloy_primitives::{TxKind, U256};
8use alloy_rpc_types_eth::{state::EvmOverrides, BlockId};
9use futures::Future;
10use reth_chainspec::MIN_TRANSACTION_GAS;
11use reth_errors::ProviderError;
12use reth_evm::{
13    env::BlockEnvironment, ConfigureEvm, Database, Evm, EvmEnvFor, EvmFor, TransactionEnvMut,
14    TxEnvFor,
15};
16use reth_revm::{
17    database::{EvmStateProvider, StateProviderDatabase},
18    db::{bal::EvmDatabaseError, State},
19};
20use reth_rpc_convert::{RpcConvert, RpcTxReq};
21use reth_rpc_eth_types::{
22    error::{
23        api::{FromEvmHalt, FromRevert},
24        FromEvmError,
25    },
26    EthApiError, RpcInvalidTransactionError,
27};
28use reth_rpc_server_types::constants::gas_oracle::{CALL_STIPEND_GAS, ESTIMATE_GAS_ERROR_RATIO};
29use revm::{
30    context::Block,
31    context_interface::{result::ExecutionResult, Cfg, Transaction},
32    primitives::KECCAK_EMPTY,
33};
34use tracing::trace;
35
36/// Gas execution estimates
37pub trait EstimateCall: Call {
38    /// Estimates the gas usage of the `request` with the state.
39    ///
40    /// This will execute the [`RpcTxReq`] and find the best gas limit via binary search.
41    ///
42    /// ## EVM settings
43    ///
44    /// This modifies certain EVM settings to mirror geth's `SkipAccountChecks` when transacting requests, see also: <https://github.com/ethereum/go-ethereum/blob/380688c636a654becc8f114438c2a5d93d2db032/core/state_transition.go#L145-L148>:
45    ///
46    ///  - `disable_eip3607` is set to `true`
47    ///  - `disable_base_fee` is set to `true`
48    ///  - `disable_fee_charge` is set to `true`
49    ///  - `nonce` is set to `None`
50    fn estimate_gas_with<S>(
51        &self,
52        mut evm_env: EvmEnvFor<Self::Evm>,
53        mut request: RpcTxReq<<Self::RpcConvert as RpcConvert>::Network>,
54        state: S,
55        overrides: EvmOverrides,
56    ) -> Result<U256, Self::Error>
57    where
58        S: EvmStateProvider,
59    {
60        // Disabled because eth_estimateGas is sometimes used with eoa senders
61        // See <https://github.com/paradigmxyz/reth/issues/1959>
62        evm_env.cfg_env.disable_eip3607 = true;
63
64        // The basefee should be ignored for eth_estimateGas and similar
65        // See:
66        // <https://github.com/ethereum/go-ethereum/blob/ee8e83fa5f6cb261dad2ed0a7bbcde4930c41e6c/internal/ethapi/api.go#L985>
67        evm_env.cfg_env.disable_base_fee = true;
68
69        // Disable additional fee charges (e.g. L2 operator fees) for gas estimation,
70        // consistent with `prepare_call_env` for `eth_call`.
71        evm_env.cfg_env.disable_fee_charge = true;
72
73        // set nonce to None so that the correct nonce is chosen by the EVM
74        request.as_mut().take_nonce();
75
76        // Keep a copy of gas related request values
77        let tx_request_gas_limit = request.as_ref().gas_limit();
78        let tx_request_gas_price = request.as_ref().gas_price();
79
80        // Configure the evm env
81        let mut db = State::builder().with_database(StateProviderDatabase::new(state)).build();
82
83        // Apply any block overrides before deriving block-derived limits and the tx env so
84        // overrides for `gasLimit`, `baseFee` and `blobBaseFee` are visible to estimation.
85        // Mirrors geth's behavior, see:
86        // <https://github.com/ethereum/go-ethereum/pull/30695>
87        if let Some(block_overrides) = overrides.block {
88            apply_block_overrides(*block_overrides, &mut db, evm_env.block_env.inner_mut());
89        }
90
91        // Apply any state overrides if specified.
92        if let Some(state_override) = overrides.state {
93            apply_state_overrides(state_override, &mut db).map_err(Self::Error::from_eth_err)?;
94        }
95
96        // the gas limit of the corresponding block
97        let max_gas_limit = evm_env
98            .cfg_env
99            .tx_gas_limit_cap
100            // If EIP-8037 is enabled, the transaction gas limit cap is not applicable
101            .filter(|_| !evm_env.cfg_env.is_amsterdam_eip8037_enabled())
102            .map_or_else(
103                || evm_env.block_env.gas_limit(),
104                |cap| cap.min(evm_env.block_env.gas_limit()),
105            );
106
107        // Determine the highest possible gas limit, considering both the request's specified limit
108        // and the block's limit.
109        let mut highest_gas_limit = tx_request_gas_limit
110            .map(|mut tx_gas_limit| {
111                if max_gas_limit < tx_gas_limit {
112                    // requested gas limit is higher than the allowed gas limit, capping
113                    tx_gas_limit = max_gas_limit;
114                }
115                tx_gas_limit
116            })
117            .unwrap_or(max_gas_limit);
118
119        let mut tx_env = self.create_txn_env(&evm_env, request, &mut db)?;
120
121        // Check if this is a basic transfer (no input data to account with no code)
122        let is_basic_transfer = if tx_env.input().is_empty() &&
123            let TxKind::Call(to) = tx_env.kind()
124        {
125            match db.database.basic_account(&to) {
126                Ok(Some(account)) => {
127                    account.bytecode_hash.is_none() || account.bytecode_hash == Some(KECCAK_EMPTY)
128                }
129                _ => true,
130            }
131        } else {
132            false
133        };
134
135        // Check funds of the sender (only useful to check if transaction gas price is more than 0).
136        //
137        // The caller allowance is check by doing `(account.balance - tx.value) / tx.gas_price`
138        if tx_env.gas_price() > 0 {
139            // cap the highest gas limit by max gas caller can afford with given gas price
140            highest_gas_limit =
141                highest_gas_limit.min(self.caller_gas_allowance(&mut db, &evm_env, &tx_env)?);
142        }
143
144        // If the provided gas limit is less than computed cap, use that
145        tx_env.set_gas_limit(tx_env.gas_limit().min(highest_gas_limit));
146
147        // Create EVM instance once and reuse it throughout the entire estimation process
148        let mut evm = self.evm_config().evm_with_env(&mut db, evm_env);
149
150        // For basic transfers, try using minimum gas before running full binary search
151        if is_basic_transfer {
152            // If the tx is a simple transfer (call to an account with no code) we can
153            // shortcircuit. But simply returning
154            // `MIN_TRANSACTION_GAS` is dangerous because there might be additional
155            // field combos that bump the price up, so we try executing the function
156            // with the minimum gas limit to make sure.
157            let mut min_tx_env = tx_env.clone();
158            min_tx_env.set_gas_limit(MIN_TRANSACTION_GAS);
159
160            // Reuse the same EVM instance
161            if let Ok(res) = evm.transact(min_tx_env).map_err(Self::Error::from_evm_err) &&
162                res.result.is_success()
163            {
164                return Ok(U256::from(MIN_TRANSACTION_GAS))
165            }
166        }
167
168        trace!(target: "rpc::eth::estimate", ?tx_env, gas_limit = tx_env.gas_limit(), is_basic_transfer, "Starting gas estimation");
169
170        // Execute the transaction with the highest possible gas limit.
171        let mut res = match evm.transact(tx_env.clone()).map_err(Self::Error::from_evm_err) {
172            // Handle the exceptional case where the transaction initialization uses too much
173            // gas. If the gas price or gas limit was specified in the request,
174            // retry the transaction with the block's gas limit to determine if
175            // the failure was due to insufficient gas.
176            Err(err)
177                if err.is_gas_too_high() &&
178                    (tx_request_gas_limit.is_some() || tx_request_gas_price.is_some()) =>
179            {
180                return Self::map_out_of_gas_err(&mut evm, tx_env, max_gas_limit);
181            }
182            Err(err) if err.is_gas_too_low() => {
183                // This failed because the configured gas cost of the tx was lower than what
184                // actually consumed by the tx This can happen if the
185                // request provided fee values manually and the resulting gas cost exceeds the
186                // sender's allowance, so we return the appropriate error here
187                return Err(RpcInvalidTransactionError::GasRequiredExceedsAllowance {
188                    gas_limit: tx_env.gas_limit(),
189                }
190                .into_eth_err());
191            }
192            // Propagate other results (successful or other errors).
193            ethres => ethres?,
194        };
195
196        let gas_refund = match res.result {
197            ExecutionResult::Success { gas, .. } => gas.final_refunded(),
198            ExecutionResult::Halt { reason, .. } => {
199                // here we don't check for invalid opcode because already executed with highest gas
200                // limit
201                return Err(Self::Error::from_evm_halt(reason, tx_env.gas_limit()))
202            }
203            ExecutionResult::Revert { output, .. } => {
204                // if price or limit was included in the request then we can execute the request
205                // again with the block's gas limit to check if revert is gas related or not
206                return if tx_request_gas_limit.is_some() || tx_request_gas_price.is_some() {
207                    Self::map_out_of_gas_err(&mut evm, tx_env, max_gas_limit)
208                } else {
209                    // the transaction did revert
210                    Err(Self::Error::from_revert(output))
211                };
212            }
213        };
214
215        // At this point we know the call succeeded but want to find the _best_ (lowest) gas the
216        // transaction succeeds with. We find this by doing a binary search over the possible range.
217
218        // we know the tx succeeded with the configured gas limit, so we can use that as the
219        // highest, in case we applied a gas cap due to caller allowance above
220        highest_gas_limit = tx_env.gas_limit();
221
222        // NOTE: this is the gas the transaction used, which is less than the
223        // transaction requires to succeed.
224        let mut gas_used = res.result.tx_gas_used();
225        // the lowest value is capped by the gas used by the unconstrained transaction
226        let mut lowest_gas_limit = gas_used.saturating_sub(1);
227
228        // As stated in Geth, there is a good chance that the transaction will pass if we set the
229        // gas limit to the execution gas used plus the gas refund, so we check this first
230        // <https://github.com/ethereum/go-ethereum/blob/a5a4fa7032bb248f5a7c40f4e8df2b131c4186a4/eth/gasestimator/gasestimator.go#L135
231        //
232        // Calculate the optimistic gas limit by adding gas used and gas refund,
233        // then applying a 64/63 multiplier to account for gas forwarding rules.
234        let optimistic_gas_limit = (gas_used + gas_refund + CALL_STIPEND_GAS) * 64 / 63;
235        if optimistic_gas_limit < highest_gas_limit {
236            // Set the transaction's gas limit to the calculated optimistic gas limit.
237            let mut optimistic_tx_env = tx_env.clone();
238            optimistic_tx_env.set_gas_limit(optimistic_gas_limit);
239
240            // Re-execute the transaction with the new gas limit and update the result and
241            // environment.
242            res = evm.transact(optimistic_tx_env).map_err(Self::Error::from_evm_err)?;
243
244            // Update the gas used based on the new result.
245            gas_used = res.result.tx_gas_used();
246            // Update the gas limit estimates (highest and lowest) based on the execution result.
247            update_estimated_gas_range(
248                res.result,
249                optimistic_gas_limit,
250                &mut highest_gas_limit,
251                &mut lowest_gas_limit,
252            )?;
253        };
254
255        // Pick a point that's close to the estimated gas
256        let mut mid_gas_limit = std::cmp::min(
257            gas_used * 3,
258            ((highest_gas_limit as u128 + lowest_gas_limit as u128) / 2) as u64,
259        );
260
261        trace!(target: "rpc::eth::estimate", ?highest_gas_limit, ?lowest_gas_limit, ?mid_gas_limit, "Starting binary search for gas");
262
263        // Binary search narrows the range to find the minimum gas limit needed for the transaction
264        // to succeed.
265        while lowest_gas_limit + 1 < highest_gas_limit {
266            // An estimation error is allowed once the current gas limit range used in the binary
267            // search is small enough (less than 1.5% of the highest gas limit)
268            // <https://github.com/ethereum/go-ethereum/blob/a5a4fa7032bb248f5a7c40f4e8df2b131c4186a4/eth/gasestimator/gasestimator.go#L152
269            let ratio = (highest_gas_limit - lowest_gas_limit) as f64 / (highest_gas_limit as f64);
270            if ratio < ESTIMATE_GAS_ERROR_RATIO {
271                break
272            };
273
274            let mut mid_tx_env = tx_env.clone();
275            mid_tx_env.set_gas_limit(mid_gas_limit);
276
277            // Execute transaction and handle potential gas errors, adjusting limits accordingly.
278            match evm.transact(mid_tx_env).map_err(Self::Error::from_evm_err) {
279                Err(err) if err.is_gas_too_high() => {
280                    // Decrease the highest gas limit if gas is too high
281                    highest_gas_limit = mid_gas_limit;
282                }
283                Err(err) if err.is_gas_too_low() => {
284                    // Increase the lowest gas limit if gas is too low
285                    lowest_gas_limit = mid_gas_limit;
286                }
287                // Handle other cases, including successful transactions.
288                ethres => {
289                    // Unpack the result and environment if the transaction was successful.
290                    res = ethres?;
291                    // Update the estimated gas range based on the transaction result.
292                    update_estimated_gas_range(
293                        res.result,
294                        mid_gas_limit,
295                        &mut highest_gas_limit,
296                        &mut lowest_gas_limit,
297                    )?;
298                }
299            }
300
301            // New midpoint
302            mid_gas_limit = ((highest_gas_limit as u128 + lowest_gas_limit as u128) / 2) as u64;
303        }
304
305        Ok(U256::from(highest_gas_limit))
306    }
307
308    /// Estimate gas needed for execution of the `request` at the [`BlockId`].
309    fn estimate_gas_at(
310        &self,
311        request: RpcTxReq<<Self::RpcConvert as RpcConvert>::Network>,
312        at: BlockId,
313        overrides: EvmOverrides,
314    ) -> impl Future<Output = Result<U256, Self::Error>> + Send
315    where
316        Self: LoadPendingBlock,
317    {
318        async move {
319            let (evm_env, at) = self.evm_env_at(at).await?;
320
321            self.spawn_blocking_io_fut(async move |this| {
322                let state = this.state_at_block_id(at).await?;
323                EstimateCall::estimate_gas_with(&this, evm_env, request, state, overrides)
324            })
325            .await
326        }
327    }
328
329    /// Executes the requests again after an out of gas error to check if the error is gas related
330    /// or not
331    #[inline]
332    fn map_out_of_gas_err<DB>(
333        evm: &mut EvmFor<Self::Evm, DB>,
334        mut tx_env: TxEnvFor<Self::Evm>,
335        max_gas_limit: u64,
336    ) -> Result<U256, Self::Error>
337    where
338        DB: Database<Error = EvmDatabaseError<ProviderError>>,
339        EthApiError: From<DB::Error>,
340    {
341        let req_gas_limit = tx_env.gas_limit();
342        tx_env.set_gas_limit(max_gas_limit);
343
344        let retry_res = evm.transact(tx_env).map_err(Self::Error::from_evm_err)?;
345
346        match retry_res.result {
347            ExecutionResult::Success { .. } => {
348                // Transaction succeeded by manually increasing the gas limit,
349                // which means the caller lacks funds to pay for the tx
350                Err(RpcInvalidTransactionError::BasicOutOfGas(req_gas_limit).into_eth_err())
351            }
352            ExecutionResult::Revert { output, .. } => {
353                // reverted again after bumping the limit
354                Err(Self::Error::from_revert(output))
355            }
356            ExecutionResult::Halt { reason, .. } => {
357                Err(Self::Error::from_evm_halt(reason, req_gas_limit))
358            }
359        }
360    }
361}
362
363/// Updates the highest and lowest gas limits for binary search based on the execution result.
364///
365/// This function refines the gas limit estimates used in a binary search to find the optimal
366/// gas limit for a transaction. It adjusts the highest or lowest gas limits depending on
367/// whether the execution succeeded, reverted, or halted due to specific reasons.
368#[inline]
369pub fn update_estimated_gas_range<Halt>(
370    result: ExecutionResult<Halt>,
371    tx_gas_limit: u64,
372    highest_gas_limit: &mut u64,
373    lowest_gas_limit: &mut u64,
374) -> Result<(), EthApiError> {
375    match result {
376        ExecutionResult::Success { .. } => {
377            // Cap the highest gas limit with the succeeding gas limit.
378            *highest_gas_limit = tx_gas_limit;
379        }
380        ExecutionResult::Revert { .. } | ExecutionResult::Halt { .. } => {
381            // We know that transaction succeeded with a higher gas limit before, so any failure
382            // means that we need to increase it.
383            //
384            // We are ignoring all halts here, and not just OOG errors because there are cases when
385            // non-OOG halt might flag insufficient gas limit as well.
386            //
387            // Common usage of invalid opcode in OpenZeppelin:
388            // <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/94697be8a3f0dfcd95dfb13ffbd39b5973f5c65d/contracts/metatx/ERC2771Forwarder.sol#L360-L367>
389            *lowest_gas_limit = tx_gas_limit;
390        }
391    };
392
393    Ok(())
394}