diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 70c6e56c6f..a49dc59691 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -443,13 +443,12 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs } if failed { if result != nil { - // If the revert reason is provided, return it to user. - if result.Err == vm.ErrExecutionReverted { - return 0, fmt.Errorf("transaction reverted (0x%x)", result.RevertReason) - } - // If it's an invalid transaction, return the concrete vm error if result.Err != vm.ErrOutOfGas { - return 0, fmt.Errorf("always failing transaction (%v)", result.Err) + errMsg := fmt.Sprintf("always failing transaction (%v)", result.Err) + if len(result.RevertReason) > 0 { + errMsg += fmt.Sprintf(" (0x%x)", result.RevertReason) + } + return 0, errors.New(errMsg) } } // Otherwise, the specified gas cap is too low diff --git a/accounts/abi/bind/backends/simulated_test.go b/accounts/abi/bind/backends/simulated_test.go index fc30b09b32..d5f03d2eaf 100644 --- a/accounts/abi/bind/backends/simulated_test.go +++ b/accounts/abi/bind/backends/simulated_test.go @@ -358,14 +358,14 @@ func TestSimulatedBackend_TransactionByHash(t *testing.T) { func TestSimulatedBackend_EstimateGas(t *testing.T) { /* - pragma solidity ^0.6.4; - contract GasEstimation { - function PureRevert() public { revert(); } - function Revert() public { revert("revert reason");} - function OOG() public { for (uint i = 0; ; i++) {}} - function Assert() public { assert(false);} - function Valid() public {} - }*/ + pragma solidity ^0.6.4; + contract GasEstimation { + function PureRevert() public { revert(); } + function Revert() public { revert("revert reason");} + function OOG() public { for (uint i = 0; ; i++) {}} + function Assert() public { assert(false);} + function Valid() public {} + }*/ const contractAbi = "[{\"inputs\":[],\"name\":\"Assert\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"OOG\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"PureRevert\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"Revert\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"Valid\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" const contractBin = "0x60806040523480156100115760006000fd5b50610017565b61016e806100266000396000f3fe60806040523480156100115760006000fd5b506004361061005c5760003560e01c806350f6fe3414610062578063aa8b1d301461006c578063b9b046f914610076578063d8b9839114610080578063e09fface1461008a5761005c565b60006000fd5b61006a610094565b005b6100746100ad565b005b61007e6100b5565b005b6100886100c2565b005b610092610135565b005b6000600090505b5b808060010191505061009b565b505b565b60006000fd5b565b600015156100bf57fe5b5b565b6040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252600d8152602001807f72657665727420726561736f6e0000000000000000000000000000000000000081526020015060200191505060405180910390fd5b565b5b56fea2646970667358221220345bbcbb1a5ecf22b53a78eaebf95f8ee0eceff6d10d4b9643495084d2ec934a64736f6c63430006040033" @@ -380,12 +380,12 @@ func TestSimulatedBackend_EstimateGas(t *testing.T) { contractAddr, _, _, _ := bind.DeployContract(opts, parsed, common.FromHex(contractBin), sim) sim.Commit() - var cases = []struct{ + var cases = []struct { name string message ethereum.CallMsg expect uint64 expectError error - } { + }{ {"plain transfer(valid)", ethereum.CallMsg{ From: addr, To: &addr, @@ -402,7 +402,7 @@ func TestSimulatedBackend_EstimateGas(t *testing.T) { GasPrice: big.NewInt(0), Value: big.NewInt(1), Data: nil, - }, 0, errors.New("transaction reverted (0x)")}, + }, 0, errors.New("always failing transaction (execution reverted)")}, {"Revert", ethereum.CallMsg{ From: addr, @@ -411,7 +411,7 @@ func TestSimulatedBackend_EstimateGas(t *testing.T) { GasPrice: big.NewInt(0), Value: nil, Data: common.Hex2Bytes("d8b98391"), - }, 0, errors.New("transaction reverted (0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d72657665727420726561736f6e00000000000000000000000000000000000000)")}, + }, 0, errors.New("always failing transaction (execution reverted) (0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d72657665727420726561736f6e00000000000000000000000000000000000000)")}, {"PureRevert", ethereum.CallMsg{ From: addr, @@ -420,7 +420,7 @@ func TestSimulatedBackend_EstimateGas(t *testing.T) { GasPrice: big.NewInt(0), Value: nil, Data: common.Hex2Bytes("aa8b1d30"), - }, 0, errors.New("transaction reverted (0x)")}, + }, 0, errors.New("always failing transaction (execution reverted)")}, {"OOG", ethereum.CallMsg{ From: addr, diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index e5a61aee12..7de0a8e1bc 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -947,13 +947,12 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash } if failed { if result != nil { - // If the revert reason is provided, return it to user. - if result.Err == vm.ErrExecutionReverted { - return 0, fmt.Errorf("transaction reverted (0x%x)", result.RevertReason) - } - // If it's an invalid transaction, return the concrete vm error if result.Err != vm.ErrOutOfGas { - return 0, fmt.Errorf("always failing transaction (%v)", result.Err) + errMsg := fmt.Sprintf("always failing transaction (%v)", result.Err) + if len(result.RevertReason) > 0 { + errMsg += fmt.Sprintf(" (0x%x)", result.RevertReason) + } + return 0, errors.New(errMsg) } } // Otherwise, the specified gas cap is too low