From 95158636cd9ce3a2eeb12f5ba5eda4b1e3b3afad Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Mon, 31 Jan 2022 06:16:10 -0800 Subject: [PATCH] rpc: clean up unmarshaling of batch-valued responses (#7728) Update the interface of the batch decoder to match the type signature of the single-response case. The caller provides the outputs, so there is no need to return them as well. No functional changes. --- rpc/jsonrpc/client/decode.go | 36 +++++++------------------- rpc/jsonrpc/client/http_json_client.go | 5 +++- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/rpc/jsonrpc/client/decode.go b/rpc/jsonrpc/client/decode.go index 2f5ec5fcc..1254949eb 100644 --- a/rpc/jsonrpc/client/decode.go +++ b/rpc/jsonrpc/client/decode.go @@ -31,29 +31,12 @@ func unmarshalResponseBytes(responseBytes []byte, expectedID rpctypes.JSONRPCInt return nil } -func unmarshalResponseBytesArray( - responseBytes []byte, - expectedIDs []rpctypes.JSONRPCIntID, - results []interface{}, -) ([]interface{}, error) { - - var ( - responses []rpctypes.RPCResponse - ) - +func unmarshalResponseBytesArray(responseBytes []byte, expectedIDs []rpctypes.JSONRPCIntID, results []interface{}) error { + var responses []rpctypes.RPCResponse if err := json.Unmarshal(responseBytes, &responses); err != nil { - return nil, fmt.Errorf("error unmarshaling: %w", err) - } - - // No response error checking here as there may be a mixture of successful - // and unsuccessful responses. - - if len(results) != len(responses) { - return nil, fmt.Errorf( - "expected %d result objects into which to inject responses, but got %d", - len(responses), - len(results), - ) + return fmt.Errorf("unmarshaling responses: %w", err) + } else if len(responses) != len(results) { + return fmt.Errorf("got %d results, wanted %d", len(responses), len(results)) } // Intersect IDs from responses with expectedIDs. @@ -62,20 +45,19 @@ func unmarshalResponseBytesArray( for i, resp := range responses { ids[i], ok = resp.ID.(rpctypes.JSONRPCIntID) if !ok { - return nil, fmt.Errorf("expected JSONRPCIntID, got %T", resp.ID) + return fmt.Errorf("expected JSONRPCIntID, got %T", resp.ID) } } if err := validateResponseIDs(ids, expectedIDs); err != nil { - return nil, fmt.Errorf("wrong IDs: %w", err) + return fmt.Errorf("wrong IDs: %w", err) } for i := 0; i < len(responses); i++ { if err := json.Unmarshal(responses[i].Result, results[i]); err != nil { - return nil, fmt.Errorf("error unmarshaling #%d result: %w", i, err) + return fmt.Errorf("error unmarshaling #%d result: %w", i, err) } } - - return results, nil + return nil } func validateResponseIDs(ids, expectedIDs []rpctypes.JSONRPCIntID) error { diff --git a/rpc/jsonrpc/client/http_json_client.go b/rpc/jsonrpc/client/http_json_client.go index 07f739cf0..8da108890 100644 --- a/rpc/jsonrpc/client/http_json_client.go +++ b/rpc/jsonrpc/client/http_json_client.go @@ -271,7 +271,10 @@ func (c *Client) sendBatch(ctx context.Context, requests []*jsonRPCBufferedReque ids[i] = req.request.ID.(rpctypes.JSONRPCIntID) } - return unmarshalResponseBytesArray(responseBytes, ids, results) + if err := unmarshalResponseBytesArray(responseBytes, ids, results); err != nil { + return nil, err + } + return results, nil } func (c *Client) nextRequestID() rpctypes.JSONRPCIntID {