Skip to content

Commit ce25db7

Browse files
internal/ethapi: unified estimateGasErrors, simplified logic
1 parent 841607d commit ce25db7

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

accounts/abi/bind/backends/simulated_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,31 @@ func TestSimulatedBackend_PendingAndCallContract(t *testing.T) {
950950
}
951951
}
952952

953+
// This test is based on the following contract:
954+
/*
955+
contract Reverter {
956+
function revertString() public pure{
957+
require(false, "some error");
958+
}
959+
function revertNoString() public pure {
960+
require(false, "");
961+
}
962+
function revertASM() public pure {
963+
assembly {
964+
revert(0x0, 0x0)
965+
}
966+
}
967+
function noRevert() public pure {
968+
assembly {
969+
// Assembles something that looks like require(false, "some error") but is not reverted
970+
mstore(0x0, 0x08c379a000000000000000000000000000000000000000000000000000000000)
971+
mstore(0x4, 0x0000000000000000000000000000000000000000000000000000000000000020)
972+
mstore(0x24, 0x000000000000000000000000000000000000000000000000000000000000000a)
973+
mstore(0x44, 0x736f6d65206572726f7200000000000000000000000000000000000000000000)
974+
return(0x0, 0x64)
975+
}
976+
}
977+
}*/
953978
func TestSimulatedBackend_CallContractRevert(t *testing.T) {
954979
testAddr := crypto.PubkeyToAddress(testKey.PublicKey)
955980
sim := simTestBackend(testAddr)

console/bridge.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -425,19 +425,19 @@ func (b *bridge) Send(call jsre.Call) (goja.Value, error) {
425425
}
426426
resultVal, err := parse(goja.Null(), call.VM.ToValue(string(result)))
427427
if err != nil {
428-
setError(resp, -32603, err.Error())
428+
setError(resp, -32603, err.Error(), nil)
429429
} else {
430430
resp.Set("result", resultVal)
431431
}
432432
}
433433
case rpc.Error:
434-
errMap := map[string]interface{}{"code": err.ErrorCode(), "message": err.Error()}
435434
if dataErr, ok := err.(rpc.DataError); ok {
436-
errMap["data"] = dataErr.ErrorData()
435+
setError(resp, err.ErrorCode(), err.Error(), dataErr.ErrorData())
436+
} else {
437+
setError(resp, err.ErrorCode(), err.Error(), nil)
437438
}
438-
resp.Set("error", errMap)
439439
default:
440-
setError(resp, -32603, err.Error())
440+
setError(resp, -32603, err.Error(), nil)
441441
}
442442
resps = append(resps, resp)
443443
}
@@ -457,8 +457,14 @@ func (b *bridge) Send(call jsre.Call) (goja.Value, error) {
457457
return result, nil
458458
}
459459

460-
func setError(resp *goja.Object, code int, msg string) {
461-
resp.Set("error", map[string]interface{}{"code": code, "message": msg})
460+
func setError(resp *goja.Object, code int, msg string, data interface{}) {
461+
err := make(map[string]interface{})
462+
err["code"] = code
463+
err["message"] = msg
464+
if data != nil {
465+
err["data"] = data
466+
}
467+
resp.Set("error", err)
462468
}
463469

464470
// isNumber returns true if input value is a JS number.

graphql/graphql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ func (b *Block) Call(ctx context.Context, args struct {
811811
if result.Failed() {
812812
status = 0
813813
}
814-
//TODO(rjl493456442, MariusVanDerWijden) return revert reason here once the spec supports an error reason.
814+
815815
return &CallResult{
816816
data: result.ReturnData,
817817
gasUsed: hexutil.Uint64(result.UsedGas),
@@ -881,7 +881,7 @@ func (p *Pending) Call(ctx context.Context, args struct {
881881
if result.Failed() {
882882
status = 0
883883
}
884-
//TODO(rjl493456442, MariusVanDerWijden) return revert reason here once the spec supports an error reason.
884+
885885
return &CallResult{
886886
data: result.ReturnData,
887887
gasUsed: hexutil.Uint64(result.UsedGas),

internal/ethapi/api.go

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -864,17 +864,11 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo
864864
return result, err
865865
}
866866

867-
var _ rpc.DataError = (*revertError)(nil)
868-
869867
type revertError struct {
870-
err string // The error string
868+
error
871869
errData interface{} // additional data
872870
}
873871

874-
func (e revertError) Error() string {
875-
return e.err
876-
}
877-
878872
func (e revertError) ErrorCode() int {
879873
// revert errors are execution errors.
880874
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
@@ -905,37 +899,14 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNrOr
905899
reason, err := abi.UnpackRevert(result.Revert())
906900
if err == nil {
907901
return nil, &revertError{
908-
err: "execution reverted",
902+
error: errors.New("execution reverted"),
909903
errData: reason,
910904
}
911905
}
912906
}
913907
return result.Return(), result.Err
914908
}
915909

916-
var _ rpc.DataError = (*estimateGasError)(nil)
917-
918-
type estimateGasError struct {
919-
error string // Concrete error type if it's failed to estimate gas usage
920-
vmerr error // Additional field, it's non-nil if the given transaction is invalid
921-
revert string // Additional field, it's non-empty if the transaction is reverted and reason is provided
922-
}
923-
924-
func (e estimateGasError) Error() string {
925-
errMsg := e.error
926-
if e.vmerr != nil {
927-
errMsg += fmt.Sprintf(" (%v)", e.vmerr)
928-
}
929-
if e.revert != "" {
930-
errMsg += fmt.Sprintf(" (%s)", e.revert)
931-
}
932-
return errMsg
933-
}
934-
935-
func (e estimateGasError) ErrorData() interface{} {
936-
return e.revert
937-
}
938-
939910
func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, gasCap *big.Int) (hexutil.Uint64, error) {
940911
// Binary search the gas requirement, as it may be higher than the amount used
941912
var (
@@ -1037,14 +1008,13 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash
10371008
revert = ret
10381009
}
10391010
}
1040-
return 0, estimateGasError{
1041-
error: "always failing transaction",
1042-
vmerr: result.Err,
1043-
revert: revert,
1011+
return 0, revertError{
1012+
error: errors.New("always failing transaction"),
1013+
errData: revert,
10441014
}
10451015
}
10461016
// Otherwise, the specified gas cap is too low
1047-
return 0, estimateGasError{error: fmt.Sprintf("gas required exceeds allowance (%d)", cap)}
1017+
return 0, fmt.Errorf("gas required exceeds allowance (%d)", cap)
10481018
}
10491019
}
10501020
return hexutil.Uint64(hi), nil

0 commit comments

Comments
 (0)