Skip to content

Commit

Permalink
add check to validate eth_call params (0xPolygonHermez#1885)
Browse files Browse the repository at this point in the history
  • Loading branch information
tclemos authored Mar 22, 2023
1 parent 3060d8e commit d4cfd78
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func (e *EthEndpoints) BlockNumber() (interface{}, types.Error) {
// useful to execute view/pure methods and retrieve values.
func (e *EthEndpoints) Call(arg *types.TxArgs, number *types.BlockNumber) (interface{}, types.Error) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
if arg == nil {
return rpcErrorResponse(types.InvalidParamsErrorCode, "missing value for required argument 0", nil)
} else if number == nil {
return rpcErrorResponse(types.InvalidParamsErrorCode, "missing value for required argument 1", nil)
}

// If the caller didn't supply the gas limit in the message, then we set it to maximum possible => block gas limit
if arg.Gas == nil || *arg.Gas == types.ArgUint64(0) {
header, err := e.getBlockHeader(ctx, *number, dbTx)
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/jsonrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,44 @@ func Test_RevertOnSCCallTransaction(t *testing.T) {
assert.Equal(t, "Today is not juernes", unpackedData)
}
}

func TestCallMissingParameters(t *testing.T) {
if testing.Short() {
t.Skip()
}
setup()
defer teardown()

type testCase struct {
name string
params []interface{}
expectedError types.ErrorObject
}

testCases := []testCase{
{
name: "params is empty",
params: []interface{}{},
expectedError: types.ErrorObject{Code: types.InvalidParamsErrorCode, Message: "missing value for required argument 0"},
},
{
name: "params has only first parameter",
params: []interface{}{map[string]interface{}{"value": "0x1"}},
expectedError: types.ErrorObject{Code: types.InvalidParamsErrorCode, Message: "missing value for required argument 1"},
},
}

for _, network := range networks {
log.Infof("Network %s", network.Name)
for _, testCase := range testCases {
t.Run(network.Name+testCase.name, func(t *testing.T) {
response, err := client.JSONRPCCall(network.URL, "eth_call", testCase.params...)
require.NoError(t, err)
require.NotNil(t, response.Error)
require.Nil(t, response.Result)
require.Equal(t, testCase.expectedError.Code, response.Error.Code)
require.Equal(t, testCase.expectedError.Message, response.Error.Message)
})
}
}
}

0 comments on commit d4cfd78

Please sign in to comment.