Fix null result field bug for JSON-RPC raw response (#362)

This commit is contained in:
Ivan Daniluk 2017-09-26 12:19:17 +02:00 committed by GitHub
parent edcb2d7933
commit 14a04c1b20
3 changed files with 24 additions and 0 deletions

View File

@ -376,3 +376,18 @@ func (s *BackendTestSuite) TestRPCEthAccountsWithUpstream() {
}`)
require.Equal(expectedResponse, resp)
}
// regression test: eth_getTransactionReceipt with invalid transaction hash should return null
func (s *BackendTestSuite) TestRegressionGetTransactionReceipt() {
require := s.Require()
s.StartTestBackend(params.RopstenNetworkID)
defer s.StopTestBackend()
rpcClient := s.backend.NodeManager().RPCClient()
// note: transaction hash is assumed to be invalid
got := rpcClient.CallRaw(`{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xbbebf28d0a3a3cbb38e6053a5b21f08f82c62b0c145a17b1c4313cac3f68ae7c"],"id":7}`)
expected := `{"jsonrpc":"2.0","id":7,"result":null}`
require.Equal(expected, got)
}

View File

@ -157,6 +157,13 @@ func newSuccessResponse(result json.RawMessage, id json.RawMessage) string {
id = defaultMsgID
}
// original response with `null` is lost due to geth RPC client
// conversion (unmarshalling). We'll have nil result object in this case,
// so handle it specially. `result` is required field in response.
if result == nil {
result = json.RawMessage("null")
}
msg := &jsonrpcMessage{
ID: id,
Version: jsonrpcVersion,

View File

@ -18,6 +18,8 @@ func TestNewSuccessResponse(t *testing.T) {
{"string", json.RawMessage(`"3434=done"`), nil, `{"jsonrpc":"2.0","id":0,"result":"3434=done"}`},
{"struct_nil_id", json.RawMessage(`{"field": "value"}`), nil, `{"jsonrpc":"2.0","id":0,"result":{"field":"value"}}`},
{"struct_non_nil_id", json.RawMessage(`{"field": "value"}`), json.RawMessage(`42`), `{"jsonrpc":"2.0","id":42,"result":{"field":"value"}}`},
{"null", json.RawMessage(`null`), json.RawMessage(`7`), `{"jsonrpc":"2.0","id":7,"result":null}`},
{"null_nil", nil, json.RawMessage(`7`), `{"jsonrpc":"2.0","id":7,"result":null}`},
}
for _, test := range cases {