Browse Source

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.
pull/7738/head
M. J. Fromberger 2 years ago
committed by GitHub
parent
commit
95158636cd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 28 deletions
  1. +9
    -27
      rpc/jsonrpc/client/decode.go
  2. +4
    -1
      rpc/jsonrpc/client/http_json_client.go

+ 9
- 27
rpc/jsonrpc/client/decode.go View File

@ -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 {


+ 4
- 1
rpc/jsonrpc/client/http_json_client.go View File

@ -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 {


Loading…
Cancel
Save