diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-01-21 06:39:16 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-01-21 06:39:16 +0800 |
commit | 682875adff760a29a2bb0024190883e4b4dd5d72 (patch) | |
tree | 36507842d38b82731d222878cda7e977b2b82a3e /accounts/abi/bind/backends | |
parent | 0126d014351bc4f58e2d7e6564a054fb80f87153 (diff) | |
download | go-tangerine-682875adff760a29a2bb0024190883e4b4dd5d72.tar go-tangerine-682875adff760a29a2bb0024190883e4b4dd5d72.tar.gz go-tangerine-682875adff760a29a2bb0024190883e4b4dd5d72.tar.bz2 go-tangerine-682875adff760a29a2bb0024190883e4b4dd5d72.tar.lz go-tangerine-682875adff760a29a2bb0024190883e4b4dd5d72.tar.xz go-tangerine-682875adff760a29a2bb0024190883e4b4dd5d72.tar.zst go-tangerine-682875adff760a29a2bb0024190883e4b4dd5d72.zip |
accounts/abi/bind, internal/ethapi: binary search gas estimation (#3587)
Gas estimation currently mostly works, but can underestimate for more funky
refunds. This is because various ops (e.g. CALL) need more gas to run than they
actually consume (e.g. 2300 stipend that is refunded if not used). With more
intricate contract interplays, it becomes almost impossible to return a proper
value to the user.
This commit swaps out the simplistic gas estimation to a binary search approach,
honing in on the correct gas use. This does mean that gas estimation needs to
rerun the transaction log(max-price) times to measure whether it fails or not,
but it's a price paid by the transaction issuer, and it should be worth it to
support proper estimates.
Diffstat (limited to 'accounts/abi/bind/backends')
-rw-r--r-- | accounts/abi/bind/backends/simulated.go | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index c26662ea1..1c34ba0e7 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -201,10 +201,32 @@ func (b *SimulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (*big.Int, error) { b.mu.Lock() defer b.mu.Unlock() - defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot()) - _, gas, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState) - return gas, err + // Binary search the gas requirement, as it may be higher than the amount used + var lo, hi uint64 + if call.Gas != nil { + hi = call.Gas.Uint64() + } else { + hi = b.pendingBlock.GasLimit().Uint64() + } + for lo+1 < hi { + // Take a guess at the gas, and check transaction validity + mid := (hi + lo) / 2 + call.Gas = new(big.Int).SetUint64(mid) + + snapshot := b.pendingState.Snapshot() + _, gas, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState) + b.pendingState.RevertToSnapshot(snapshot) + + // If the transaction became invalid or used all the gas (failed), raise the gas limit + if err != nil || gas.Cmp(call.Gas) == 0 { + lo = mid + continue + } + // Otherwise assume the transaction succeeded, lower the gas limit + hi = mid + } + return new(big.Int).SetUint64(hi), nil } // callContract implemens common code between normal and pending contract calls. |