pub type EvmReady<Db, Insp = NoOpInspector> = Trevm<Db, Insp, Ready>;Expand description
A Trevm that is ready to execute a transaction.
The transaction may be executed with EvmReady::run or cleared
with EvmReady::clear_tx
Aliased Type§
pub struct EvmReady<Db, Insp = NoOpInspector> { /* private fields */ }Implementations§
Source§impl<Db, Insp> EvmReady<Db, Insp>
impl<Db, Insp> EvmReady<Db, Insp>
Sourcepub fn clear_tx(self) -> EvmNeedsTx<Db, Insp>
pub fn clear_tx(self) -> EvmNeedsTx<Db, Insp>
Clear the current transaction environment.
Sourcepub fn run(self) -> Result<EvmTransacted<Db, Insp>, EvmErrored<Db, Insp>>
pub fn run(self) -> Result<EvmTransacted<Db, Insp>, EvmErrored<Db, Insp>>
Execute the loaded transaction. This is a wrapper around
InspectEvm::inspect_tx and produces either EvmTransacted or
EvmErrored.
Sourcepub fn call(
self,
) -> Result<(ExecutionResult, EvmNeedsTx<Db, Insp>), EvmErrored<Db, Insp>>
Available on crate feature call only.
pub fn call( self, ) -> Result<(ExecutionResult, EvmNeedsTx<Db, Insp>), EvmErrored<Db, Insp>>
call only.Simulate the transaction, and return the ExecutionResult. The
following modifications are made to the environment while simulating.
- EIP-3607 is disabled.
- Base fee checks are disabled.
- Nonce checks are disabled.
Sourcepub fn calculate_initial_gas(&self) -> u64
pub fn calculate_initial_gas(&self) -> u64
Calculate the minimum gas required to start EVM execution.
This uses calculate_initial_tx_gas_for_tx to calculate the initial
gas. Its output is dependent on
- the EVM spec
- the input data
- whether the transaction is a contract creation or a call
- the EIP-2930 access list
- the number of EIP-7702 authorizations
Sourcepub fn estimate_gas(
self,
) -> Result<(EstimationResult, Self), EvmErrored<Db, Insp>>
Available on crate feature estimate_gas only.
pub fn estimate_gas( self, ) -> Result<(EstimationResult, Self), EvmErrored<Db, Insp>>
estimate_gas only.Implements gas estimation. This will output an estimate of the minimum amount of gas that the transaction will consume, calculated via iterated simulation.
In the worst case this will perform a binary search, resulting in
O(log(n)) simulations.
§Returns
An EstimationResult and the EVM with the transaction populated.
Like with the remainder of the API, an EVM revert or an EVM halt is
NOT an error. An Err is returned only if the EVM encounters a
condition of use violation or a DB access fails.
§Estimation Algorithm
This function is largely based on the reth RPC estimation algorithm, which can be found here. The algorithm is as follows:
- Disable eip-3607, allowing estimation from contract accounts.
- Disable base fee checks.
- Check if the transaction is a simple transfer
- Is there input data empty? If yes, proceed to regular estimation
- Is the callee a contract? If yes, proceed to regular estimation
- Otherwise, shortcut return success with
MIN_TRANSACTION_GAS.
- Simulate the transaction with the maximum possible gas limit.
- If the simulation fails, shortcut return the failure.
- If succesful, store the gas used as the search minimum.
- Simulate the transaction with an “optimistic” gas limit.
- If the simulation fails, shortcut return the failure.
- If succesful, begin the binary search around that range.
- Binary search loop:
- If the search range is small enough, break the loop and return the current estimate.
- Calculate a new gas limit based on the midpoint of the search range.
- Simulate the transaction with the new gas limit.
- Adjust the search range based on the simulation result:
- If the result is a success, pull the search max down to the limit.
- If the result is a revert, push the search min up to the limit.
- If the result is a halt, check if the halt is potentially a
gas-dynamic halt.
- If it is, treat it as a revert.
- If it is not, shortcut return the halt.
- Loop.