-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opcode to require a minimal gas value while ensuring a useful eth_estimateGas #2075
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Actually the binary search strategy is already in use, I was testing on ganache only. So the benefits would be optimization |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
eip: requireGas
title: Opcode to require a minimal gas value while ensuring a useful eth_estimateGas
author: Ronan Sandford (@wighawag)
type: Standards Track
category: Core
status: Draft
created: 2019-05-24
Simple Summary
Add an opcode to require a specific amount of gas (similar to
require(gasleft()>X)
in solidity) that alloweth_estimateGas
to return a value that will represent enough gas to make the transaction succeed, even though the actual gas used will be less.Abstract
Currently when using
require(gasleft()>X)
, a call toeth_estimateGas
will return a value that while representing the correct amount of gas used by the transaction when sent with more than enough gas, might be returning an amount that in itself is not sufficient to make the tx to succeed. This is because when checking forgasleft()
the actual gas used afterward might be less than the requirement.This proposal add a new opcode that perform the same check as
require(gasleft()>X)
but ensure thateth_estimateGas
return the minimum amount of gas required for the call to succeed (the use case foreth_estimateGas
)Specification
Let specify
minimalGas
as a new variable that the EVM need to keep track for the purpose ofeth_estimateGas
. At the start of a tx, it is set to zero.At the end of an
eth_estimateGas
call, the gas spent is compared tominimalGas
. The bigger value of the two is returned as "estimate". It does not have any other role in the context of a transaction call, its only purpose is to fix the current behavior ofeth_estimateGas
that do not consider code likerequire(gasleft()>X)
when asked to return the gas required to perform a tx.Adds a new opcode
REQUIRE_GAS
at<TBD>
, which uses 1 stack argument : a 32 bytes value that represent the gas required for the contract code to proceed. It will check if the gas left at that point is greater or equal to that value.If not it will revert the call.
If there is enough gas, the gas amount specified will be added to the gas spent up to that point. If that amount is bigger than
minimalGas
it replaces it. In other words:where X is the value passed to
REQUIRE_GAS
As mentioned the result of an
eth_estimateGas
is nowThe operation costs
G_base
+G_verylow
to execute.Rationale
eth_estimateGas
is currently returning the gas used, and not the gas required for the operation to succeed. While in most case, these two values are equals, there are cases where this is not the case.In those cases, applications can't relies on
eth_estimateGas
to give them a useful value. They can't even know what increase in gas they need for the transaction to succeed unless they perform a binary search by executingeth_estimateGas
multiple time.These cases happen as soon as a smart contract is using code like
where X is greater than the gas actually used after that check.
This is a common pattern for meta-transaction where it is not possible for the smart contract to know whether that value X is an over estimation of the gas actually required.
With the introduction of EIP-1706 such problem will also arise for contract that simply use
SSTORE
, even existing contract, unless a similar mechanism (usingminimalGas
tracking) is used to ensureeth_estimateGas
continue to work as expected (see comment here)This mechanism will also be useful for EIP-1930 that also implicitly check for
gasleft()
Alternative Solutions
Make
eth_estimateGas
to execute the call multiple time until it finds the minimum gas required for the call to succeed. This requirelog2(N)
calls to the smart contract code where N is the difference between the initial gas given (usually the block gas limit) and the gas value returned by that first call.eth_estimateGas
is already an expensive call to make for user interface and such solution would require up to 20 calls to find the minimal safe value.Backwards Compatibility
Backwards compatible for smart contract as it introduce a new opcode.
eth_estimateGas
will continue to work as intended : allow wallet to compute the amount of gas to send for the transaction to succeed.Test Cases
TBD
Implementation
TBD
Copyright
Copyright and related rights waived via CC0.