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