diff options
author | rjl493456442 <garyrong0905@gmail.com> | 2017-08-24 11:18:21 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-10-02 20:26:40 +0800 |
commit | 94903d572b4ee72c7ea62ce9856f51def85fdd2a (patch) | |
tree | 1e9232ff6440cfc68e27c9531e3b5a3819689993 /accounts | |
parent | f86c4177d506b635f9ebdaea8ab5aef27f53d20a (diff) | |
download | go-tangerine-94903d572b4ee72c7ea62ce9856f51def85fdd2a.tar go-tangerine-94903d572b4ee72c7ea62ce9856f51def85fdd2a.tar.gz go-tangerine-94903d572b4ee72c7ea62ce9856f51def85fdd2a.tar.bz2 go-tangerine-94903d572b4ee72c7ea62ce9856f51def85fdd2a.tar.lz go-tangerine-94903d572b4ee72c7ea62ce9856f51def85fdd2a.tar.xz go-tangerine-94903d572b4ee72c7ea62ce9856f51def85fdd2a.tar.zst go-tangerine-94903d572b4ee72c7ea62ce9856f51def85fdd2a.zip |
internal, accounts, eth: utilize vm failed flag to help gas estimation
Diffstat (limited to 'accounts')
-rw-r--r-- | accounts/abi/bind/backends/simulated.go | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index d6d16eb3f..0621e81c2 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -168,7 +168,7 @@ func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallM if err != nil { return nil, err } - rval, _, err := b.callContract(ctx, call, b.blockchain.CurrentBlock(), state) + rval, _, _, err := b.callContract(ctx, call, b.blockchain.CurrentBlock(), state) return rval, err } @@ -178,7 +178,7 @@ func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereu defer b.mu.Unlock() defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot()) - rval, _, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState) + rval, _, _, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState) return rval, err } @@ -204,8 +204,11 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs defer b.mu.Unlock() // Binary search the gas requirement, as it may be higher than the amount used - var lo, hi uint64 - if call.Gas != nil { + var ( + lo uint64 = params.TxGas - 1 + hi uint64 + ) + if call.Gas != nil && call.Gas.Uint64() >= params.TxGas { hi = call.Gas.Uint64() } else { hi = b.pendingBlock.GasLimit().Uint64() @@ -216,11 +219,11 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs call.Gas = new(big.Int).SetUint64(mid) snapshot := b.pendingState.Snapshot() - _, gas, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState) + _, _, failed, 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 { + // If the transaction became invalid or execution failed, raise the gas limit + if err != nil || failed { lo = mid continue } @@ -232,7 +235,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs // callContract implemens common code between normal and pending contract calls. // state is modified during execution, make sure to copy it if necessary. -func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, *big.Int, error) { +func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, *big.Int, bool, error) { // Ensure message is initialized properly. if call.GasPrice == nil { call.GasPrice = big.NewInt(1) @@ -254,9 +257,8 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM // about the transaction and calling mechanisms. vmenv := vm.NewEVM(evmContext, statedb, b.config, vm.Config{}) gaspool := new(core.GasPool).AddGas(math.MaxBig256) - // TODO utilize returned failed flag to help gas estimation. - ret, gasUsed, _, _, err := core.NewStateTransition(vmenv, msg, gaspool).TransitionDb() - return ret, gasUsed, err + ret, gasUsed, _, failed, err := core.NewStateTransition(vmenv, msg, gaspool).TransitionDb() + return ret, gasUsed, failed, err } // SendTransaction updates the pending block to include the given transaction. |