rpc: simplify and consolidate response construction (#7725)

Responses are constructed from requests using MakeResponse, MakeError, and
MakeErrorf. This ensures the response is always paired with the correct ID,
makes cases where there is no ID more explicit at the usage site, and
consolidates the handling of error introspection across transports.

The logic for unpacking errors and assigning JSON-RPC response types was
previously duplicated in three places. Consolidate it in the types package for
the RPC subsystem.

* update test cases
This commit is contained in:
M. J. Fromberger
2022-01-28 13:33:02 -08:00
committed by GitHub
parent 20886fdb39
commit fbe86ea645
10 changed files with 144 additions and 160 deletions

View File

@@ -80,12 +80,12 @@ func TestProvider(t *testing.T) {
lb, err = p.LightBlock(ctx, 9001)
require.Error(t, err)
require.Nil(t, lb)
assert.Equal(t, provider.ErrHeightTooHigh, err)
assert.ErrorIs(t, err, provider.ErrHeightTooHigh)
lb, err = p.LightBlock(ctx, 1)
require.Error(t, err)
require.Nil(t, lb)
assert.Equal(t, provider.ErrLightBlockNotFound, err)
assert.ErrorIs(t, err, provider.ErrLightBlockNotFound)
// if the provider is unable to provide four more blocks then we should return
// an unreliable peer error

View File

@@ -655,11 +655,7 @@ func (c *Client) SubscribeWS(ctx context.Context, query string) (*coretypes.Resu
case resultEvent := <-out:
// We should have a switch here that performs a validation
// depending on the event's type.
callInfo.WSConn.TryWriteRPCResponse(bctx,
rpctypes.NewRPCSuccessResponse(
rpctypes.JSONRPCStringID(fmt.Sprintf("%v#event", callInfo.RPCRequest.ID)),
resultEvent,
))
callInfo.WSConn.TryWriteRPCResponse(bctx, callInfo.RPCRequest.MakeResponse(resultEvent))
case <-bctx.Done():
return
}