Skip to content

Commit

Permalink
[EVM] Meaningful error message for eth_call balance override overflow (
Browse files Browse the repository at this point in the history
  • Loading branch information
jewei1997 authored Jul 26, 2024
1 parent 0812c51 commit 7574464
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions evmrpc/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,12 @@ func formatParam(p interface{}) string {
kvs = append(kvs, fmt.Sprintf("\"%s\":%s", k, formatParam(v)))
}
return fmt.Sprintf("{%s}", strings.Join(kvs, ","))
case map[string]map[string]interface{}:
kvs := []string{}
for k, v := range v {
kvs = append(kvs, fmt.Sprintf("\"%s\":%s", k, formatParam(v)))
}
return fmt.Sprintf("{%s}", strings.Join(kvs, ","))
default:
panic("did not match on type")
}
Expand Down
10 changes: 10 additions & 0 deletions evmrpc/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"strings"
"time"

"github.com/sei-protocol/sei-chain/utils/helpers"
Expand Down Expand Up @@ -90,6 +91,15 @@ func (s *SimulationAPI) EstimateGas(ctx context.Context, args ethapi.Transaction
func (s *SimulationAPI) Call(ctx context.Context, args ethapi.TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash, overrides *ethapi.StateOverride, blockOverrides *ethapi.BlockOverrides) (result hexutil.Bytes, returnErr error) {
startTime := time.Now()
defer recordMetrics("eth_call", s.connectionType, startTime, returnErr == nil)
defer func() {
if r := recover(); r != nil {
if strings.Contains(fmt.Sprintf("%s", r), "Int overflow") {
returnErr = errors.New("error: balance override overflow")
} else {
returnErr = fmt.Errorf("something went wrong: %v", r)
}
}
}()
if blockNrOrHash == nil {
latest := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
blockNrOrHash = &latest
Expand Down
25 changes: 25 additions & 0 deletions evmrpc/simulate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ func TestCall(t *testing.T) {
Ctx = Ctx.WithBlockHeight(8)
}

func TestEthCallHighAmount(t *testing.T) {
Ctx = Ctx.WithBlockHeight(1)
_, from := testkeeper.MockAddressPair()
_, to := testkeeper.MockAddressPair()
txArgs := map[string]interface{}{
"from": from.Hex(),
"to": to.Hex(),
"value": "0x0",
"nonce": "0x2",
"chainId": fmt.Sprintf("%#x", EVMKeeper.ChainID(Ctx)),
}

overrides := map[string]map[string]interface{}{
from.Hex(): {"balance": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
}
resObj := sendRequestGood(t, "call", txArgs, "latest", overrides)
fmt.Println("resObj = ", resObj)
errMap := resObj["error"].(map[string]interface{})
result := errMap["message"]
fmt.Println("res = ", result)
require.Equal(t, result, "error: balance override overflow")

Ctx = Ctx.WithBlockHeight(8)
}

func TestNewRevertError(t *testing.T) {
err := evmrpc.NewRevertError(&core.ExecutionResult{})
require.NotNil(t, err)
Expand Down

0 comments on commit 7574464

Please sign in to comment.