From 8963d828b276350fb0200673eee63037587ff14f Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Sun, 20 Nov 2022 15:39:14 +0100 Subject: [PATCH] Remove useless whitespace in Websocket output (#9720) * First try at #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 (cherry picked from commit 20ffa4fd3227a436a9f4438921ea2b7ce2e6cb82) # Conflicts: # CHANGELOG_PENDING.md # rpc/jsonrpc/server/http_server_test.go --- CHANGELOG_PENDING.md | 42 ++++++++++++++++++++++++++ rpc/jsonrpc/server/http_server.go | 4 +-- rpc/jsonrpc/server/http_server_test.go | 32 +++++--------------- rpc/jsonrpc/server/ws_handler.go | 5 ++- 4 files changed, 55 insertions(+), 28 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index afb3464a3..7fc69b305 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -1,5 +1,47 @@ # Unreleased Changes +<<<<<<< HEAD +======= +## v0.38.0 + +### BREAKING CHANGES + +- CLI/RPC/Config + +- Apps + +- P2P Protocol + +- Go API + - [p2p] \#9625 Remove unused p2p/trust package (@cmwaters) + +- Blockchain Protocol + +- 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) + labels have moved from block_syncing -> blocksync_syncing and state_syncing -> statesync_syncing + +### FEATURES + +- [config] \#9680 Introduce `BootstrapPeers` to the config to allow nodes to list peers to be added to + the addressbook upon start up (@cmwaters) + +### IMPROVEMENTS + +- [pubsub] \#7319 Performance improvements for the event query API (@creachadair) +- [p2p/pex] \#6509 Improve addrBook.hash performance (@cuonglm) +- [crypto/merkle] \#6443 & \#6513 Improve HashAlternatives performance (@cuonglm, @marbar3778) +- [rpc] \#9650 Enable caching of RPC responses (@JayT106) + +### BUG FIXES + +- [docker] \#9462 ensure Docker image uses consistent version of Go + +>>>>>>> 20ffa4fd3 (Remove useless whitespace in Websocket output (#9720)) ## v0.37.0 Special thanks to external contributors on this release: diff --git a/rpc/jsonrpc/server/http_server.go b/rpc/jsonrpc/server/http_server.go index 6eaa0ab93..c8dd2bba5 100644 --- a/rpc/jsonrpc/server/http_server.go +++ b/rpc/jsonrpc/server/http_server.go @@ -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) } @@ -124,7 +124,7 @@ func WriteRPCResponseHTTP(w http.ResponseWriter, res ...types.RPCResponse) error v = res } - jsonBytes, err := json.MarshalIndent(v, "", " ") + jsonBytes, err := json.Marshal(v) if err != nil { return fmt.Errorf("json marshal: %w", err) } diff --git a/rpc/jsonrpc/server/http_server_test.go b/rpc/jsonrpc/server/http_server_test.go index 6e2024b8d..55681fa18 100644 --- a/rpc/jsonrpc/server/http_server_test.go +++ b/rpc/jsonrpc/server/http_server_test.go @@ -120,6 +120,7 @@ func TestWriteRPCResponseHTTP(t *testing.T) { require.NoError(t, err) assert.Equal(t, 200, resp.StatusCode) assert.Equal(t, "application/json", resp.Header.Get("Content-Type")) +<<<<<<< HEAD assert.Equal(t, `{ "jsonrpc": "2.0", "id": -1, @@ -127,6 +128,10 @@ func TestWriteRPCResponseHTTP(t *testing.T) { "value": "hello" } }`, string(body)) +======= + 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)) +>>>>>>> 20ffa4fd3 (Remove useless whitespace in Websocket output (#9720)) // multiple arguments w = httptest.NewRecorder() @@ -141,22 +146,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) { @@ -172,13 +162,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)) } diff --git a/rpc/jsonrpc/server/ws_handler.go b/rpc/jsonrpc/server/ws_handler.go index f8a89714e..b2f1382c5 100644 --- a/rpc/jsonrpc/server/ws_handler.go +++ b/rpc/jsonrpc/server/ws_handler.go @@ -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