Skip to content

Commit

Permalink
Remove useless whitespace in Websocket output (tendermint#9720)
Browse files Browse the repository at this point in the history
* First try at tendermint#9696

* Brief explanation

* Removed all prettified JSON RPC responses

* Fixes for failing tests.

Adapted the assertions in
- TestWriteRPCResponseHTTP
- TestWriteRPCResponseHTTPError
to work with non-pretty JSON-RPC output

* Added changelog pending entry

* Update CHANGELOG_PENDING.md

Co-authored-by: Thane Thomson <[email protected]>
  • Loading branch information
adizere and thanethomson authored Nov 20, 2022
1 parent f9bfdf4 commit 20ffa4f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

- Data Storage
- [state] \#6541 Move pruneBlocks from consensus/state to state/execution. (@JayT106)

- Tooling
- [tools/tm-signer-harness] \#6498 Set OS home dir to instead of the hardcoded PATH. (@JayT106)
- [metrics] \#9682 move state-syncing and block-syncing metrics to their respective packages (@cmwaters)
Expand Down
4 changes: 2 additions & 2 deletions rpc/jsonrpc/server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func WriteRPCResponseHTTPError(
panic("tried to write http error response without RPC error")
}

jsonBytes, err := json.MarshalIndent(res, "", " ")
jsonBytes, err := json.Marshal(res)
if err != nil {
return fmt.Errorf("json marshal: %w", err)
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func writeRPCResponseHTTP(w http.ResponseWriter, headers []httpHeader, res ...ty
v = res
}

jsonBytes, err := json.MarshalIndent(v, "", " ")
jsonBytes, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("json marshal: %w", err)
}
Expand Down
35 changes: 3 additions & 32 deletions rpc/jsonrpc/server/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,7 @@ func TestWriteRPCResponseHTTP(t *testing.T) {
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Equal(t, "public, max-age=86400", resp.Header.Get("Cache-control"))
assert.Equal(t, `{
"jsonrpc": "2.0",
"id": -1,
"result": {
"value": "hello"
}
}`, string(body))
assert.Equal(t, `{"jsonrpc":"2.0","id":-1,"result":{"value":"hello"}}`, string(body))

// multiple arguments
w = httptest.NewRecorder()
Expand All @@ -142,22 +136,7 @@ func TestWriteRPCResponseHTTP(t *testing.T) {

assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Equal(t, `[
{
"jsonrpc": "2.0",
"id": -1,
"result": {
"value": "hello"
}
},
{
"jsonrpc": "2.0",
"id": -1,
"result": {
"value": "world"
}
}
]`, string(body))
assert.Equal(t, `[{"jsonrpc":"2.0","id":-1,"result":{"value":"hello"}},{"jsonrpc":"2.0","id":-1,"result":{"value":"world"}}]`, string(body))
}

func TestWriteRPCResponseHTTPError(t *testing.T) {
Expand All @@ -173,13 +152,5 @@ func TestWriteRPCResponseHTTPError(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Equal(t, `{
"jsonrpc": "2.0",
"id": -1,
"error": {
"code": -32603,
"message": "Internal error",
"data": "foo"
}
}`, string(body))
assert.Equal(t, `{"jsonrpc":"2.0","id":-1,"error":{"code":-32603,"message":"Internal error","data":"foo"}}`, string(body))
}
5 changes: 4 additions & 1 deletion rpc/jsonrpc/server/ws_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,10 @@ func (wsc *wsConnection) writeRoutine() {
return
}
case msg := <-wsc.writeChan:
jsonBytes, err := json.MarshalIndent(msg, "", " ")
// Use json.MarshalIndent instead of Marshal for pretty output.
// Pretty output not necessary, since most consumers of WS events are
// automated processes, not humans.
jsonBytes, err := json.Marshal(msg)
if err != nil {
wsc.Logger.Error("Failed to marshal RPCResponse to JSON", "err", err)
continue
Expand Down

0 comments on commit 20ffa4f

Please sign in to comment.