Feature: Gas Limit on Estimation.sol #1719
Open
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.
Change log
Estimation.sol
so that it first calculates a gasLimit and then tries to simulate the calls with the set limit. Upon failure, it makes the gasLimit 3 times bigger and tries againProblem
Estimation.sol
correctly estimates the gas cost of a transaction but when firing a request toEstimation.sol
, the request has an inifinitegasLimit
set. This of course is normal - the goal forEstimation.sol
to return the correctgasLimit
of the transaction and it wouldn't make sense for it to revert because of insufficient gas.This is working brilliantly in 99% of cases unless one - where an UI artificially requests more gas than actually needed.
Let's take the sample contract below as an example:
We see that
guardedCall
has aminGasRequired
parameter. So a dapp can take advantage of this and send this call to the wallet:This means the dapp is requesting a minimum of 60k gas for the execution of its contract call. When we're doing an estimation with
Estimation.sol
, we've set the gas parameter to infinite so the above passes without problems. However, after estimation has concluded,Estimation.sol
will return the calculated gasLimit for running the above code. Guess what? It's below 60k as the execution cost of the above method doesn't actually require 60k regardless that the dapp requests it.Another interesting part of this problem is the fact that
estimateGas
won't actually return below 60k becauseestimateGas
works on a binary search principle. This means thatestimateGas
does estimates by starting at a higher setgasLimit
and reduces it in half after each successful estimation until it hits a failure. So the moment it goes below 60k,estimateGas
will revert and it will return the last successful estimation before going below 60k, making it a winning bet.Solution
We're changing how
Estimation.sol
works to incorporate the above problem and make it work, to a reasonable extent, with "dapp-requested" gasLimit. Here's how:The above algorithm allows us to make successful estimations on "dapp-requested" gas limits that are up to 3 times larger than what would originally cost. Any more than 3 times at this point in time we consider unreasonable and will not allow the execution of, returning an estimation error to the user.
Here are real world examples of such transactions:
We see clearly that 325k is more than enough to execute the first transaction that had an unreasonably large amount of 850k set. However, the dapp has included a minimum gas limit in its contract logic, forcing the 325k transaction to fail
There's a reason for such minimum gas limits. In the above particular case, here it is:
For the curious: here's the full contract, search for callWithMinGas