Skip to content

Commit

Permalink
fix: ensure JSON serialization compatibility
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.

Resolves cosmos#463.
  • Loading branch information
tzdybal committed Jul 1, 2022
1 parent 6247abc commit e454904
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion 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 @@ -176,7 +178,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
4 changes: 2 additions & 2 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
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 e454904

Please sign in to comment.