Skip to content

Commit

Permalink
Merge pull request cosmos#464 from celestiaorg/tzdybal/json/tmjson
Browse files Browse the repository at this point in the history
Because Tendermint encodes ints as strings, and expects strings while
decoding, Optimint has to encode int values as strings as well.

This PR ensures that Tendermint JSON library is used for encoding of results both in plain HTTP requests and JSON RPC requests.

Resolves cosmos#463.
  • Loading branch information
tzdybal authored Jul 4, 2022
2 parents 6247abc + 27d3eae commit bfa0fa4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
17 changes: 15 additions & 2 deletions rpc/json/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"reflect"
"strconv"

tmjson "github.com/tendermint/tendermint/libs/json"

"github.com/celestiaorg/optimint/log"
"github.com/gorilla/rpc/v2"
"github.com/gorilla/rpc/v2/json2"
Expand Down Expand Up @@ -101,7 +103,13 @@ func (h *handler) serveJSONRPCforWS(w http.ResponseWriter, r *http.Request, wsCo

// Encode the response.
if errResult == nil {
codecReq.WriteResponse(w, rets[0].Interface())
var raw json.RawMessage
raw, err = tmjson.Marshal(rets[0].Interface())
if err != nil {
codecReq.WriteError(w, http.StatusInternalServerError, err)
return
}
codecReq.WriteResponse(w, raw)
} else {
codecReq.WriteError(w, statusCode, errResult)
}
Expand Down Expand Up @@ -176,7 +184,12 @@ func (h *handler) encodeAndWriteResponse(w http.ResponseWriter, result interface
if errResult != nil {
resp.Error = &json2.Error{Code: json2.ErrorCode(statusCode), Data: errResult.Error()}
} else {
resp.Result = result
bytes, err := tmjson.Marshal(result)
if err != nil {
resp.Error = &json2.Error{Code: json2.ErrorCode(json2.E_INTERNAL), Data: err.Error()}
} else {
resp.Result = bytes
}
}

encoder := json.NewEncoder(w)
Expand Down
6 changes: 3 additions & 3 deletions rpc/json/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ func TestREST(t *testing.T) {

{"invalid/malformed request", "/block?so{}wrong!", http.StatusOK, int(json2.E_INVALID_REQ), ``},
{"invalid/missing param", "/block", http.StatusOK, int(json2.E_INVALID_REQ), `missing param 'height'`},
{"valid/no params", "/abci_info", http.StatusOK, -1, `"last_block_height":345`},
{"valid/no params", "/abci_info", http.StatusOK, -1, `"last_block_height":"345"`},
// to keep test simple, allow returning application error in following case
{"valid/int param", "/block?height=321", http.StatusOK, int(json2.E_INTERNAL), `"key not found"`},
{"invalid/int param", "/block?height=foo", http.StatusOK, int(json2.E_PARSE), "failed to parse param 'height'"},
{"valid/bool int string params",
"/tx_search?" + txSearchParams.Encode(),
http.StatusOK, -1, `"total_count":0`},
http.StatusOK, -1, `"total_count":"0"`},
{"invalid/bool int string params",
"/tx_search?" + strings.Replace(txSearchParams.Encode(), "true", "blue", 1),
http.StatusOK, int(json2.E_PARSE), "failed to parse param 'prove'"},
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestStringyRequest(t *testing.T) {
// `starport chain faucet ...` generates broken JSON (ints are "quoted" as strings)
brokenJSON := `{"jsonrpc":"2.0","id":0,"method":"tx_search","params":{"order_by":"","page":"1","per_page":"1000","prove":true,"query":"message.sender='cosmos1njr26e02fjcq3schxstv458a3w5szp678h23dh' AND transfer.recipient='cosmos1e0ajth0s847kqcu2ssnhut32fsrptf94fqnfzx'"}}`

respJson := `{"jsonrpc":"2.0","result":{"txs":[],"total_count":0},"id":0}` + "\n"
respJson := `{"jsonrpc":"2.0","result":{"txs":[],"total_count":"0"},"id":0}` + "\n"

req := httptest.NewRequest(http.MethodGet, "/", strings.NewReader(brokenJSON))
resp := httptest.NewRecorder()
Expand Down
2 changes: 1 addition & 1 deletion rpc/json/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func unmarshalStrInt64(b []byte, s *StrInt64) error {

type response struct {
Version string `json:"jsonrpc"`
Result interface{} `json:"result,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *json2.Error `json:"error,omitempty"`
Id json.RawMessage `json:"id"`
}

0 comments on commit bfa0fa4

Please sign in to comment.