Implement gas estimation
See original GitHub issueWhat is wrong?
We need to implement gas estimation for the JSON-RPC interface.
An ideal gas estimation API should take into account the following.
- total gas used isn’t always right due to gas refunds.
- transactions can alter execution flow based on the txn gas.
How can it be fixed
This should be implemented using a swappable backend API. Accurate gas estimations are likely to be computationally expensive where-as naive estimations based on gas used are likely to work for most use cases and should be computationally cheap.
The API can be something as simple as a function which takes VM
instance and a Transaction
instance and returns the gas estimation. It might be appropriate to include this as an API on the Chain
and VM
as Chain.estimate_transaction_gas()
and VM.estimate_transaction_gas()
Strategies
We should first implement a PeekGasUsedStrategy
which uses the peek gas usage
- run the computation with the gas limit set at the full gas limit for the next pending block.
- return the
computation.gas_meter.start_gas - computation.gas_metea.gas_remaining
for the outer computation.
Then we should implement what I believe is the current strategy used by go-ethereum which starts with running the transaction using the gas limit for the next pending block and then performs a binary search, re-running the transaction at each level of the search until it finds the threshold for which the transaction succeeds/fails.
Note that the binary search can be performed at a higher granularity than 1 gas. From @carver comment, a 1024 granularity should produce approximately a 2x speedup.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (7 by maintainers)
I have no idea what I was looking at. Disregard.
Thanks, I updated the spec to reflect the gas limit and the granularity.