fixes after my own review

This commit is contained in:
Anton Kaliaev 2019-08-01 17:58:42 +04:00
parent fc4a98e9f8
commit c2e1970a7d
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
4 changed files with 9 additions and 7 deletions

View File

@ -40,6 +40,7 @@ Response:
``` ```
{ {
"jsonrpc": "2.0", "jsonrpc": "2.0",
"id": 0,
"result": { "result": {
"query": "tm.event='ValidatorSetUpdates'", "query": "tm.event='ValidatorSetUpdates'",
"data": { "data": {

View File

@ -25,7 +25,7 @@ func unmarshalResponseBytes(cdc *amino.Codec, responseBytes []byte,
return nil, errors.Wrap(response.Error, "response error") return nil, errors.Wrap(response.Error, "response error")
} }
if err = assertResponseIDEqual(response, expectedID); err != nil { if err = validateAndVerifyID(response, expectedID); err != nil {
return nil, errors.Wrap(err, "error in response ID") return nil, errors.Wrap(err, "error in response ID")
} }
@ -96,7 +96,7 @@ func validateResponseIDs(ids, expectedIDs []types.JSONRPCIntID) error {
// From the JSON-RPC 2.0 spec: // From the JSON-RPC 2.0 spec:
// id: It MUST be the same as the value of the id member in the Request Object. // id: It MUST be the same as the value of the id member in the Request Object.
func assertResponseIDEqual(res *types.RPCResponse, expectedID types.JSONRPCIntID) error { func validateAndVerifyID(res *types.RPCResponse, expectedID types.JSONRPCIntID) error {
// URIClient does not have ID in response // URIClient does not have ID in response
if expectedID == -1 { if expectedID == -1 {
return nil return nil
@ -116,7 +116,7 @@ func validateResponseID(id interface{}) error {
} }
_, ok := id.(types.JSONRPCIntID) _, ok := id.(types.JSONRPCIntID)
if !ok { if !ok {
return errors.Errorf("expected int, but got: %T", id) return errors.Errorf("expected JSONRPCIntID, but got: %T", id)
} }
return nil return nil
} }

View File

@ -486,13 +486,14 @@ func (c *WSClient) readRoutine() {
continue continue
} }
c.mtx.RLock() c.mtx.Lock()
if _, ok := c.sentIDs[response.ID.(types.JSONRPCIntID)]; !ok { if _, ok := c.sentIDs[response.ID.(types.JSONRPCIntID)]; !ok {
c.Logger.Error("unsolicited response ID", "id", response.ID, "expected", c.sentIDs) c.Logger.Error("unsolicited response ID", "id", response.ID, "expected", c.sentIDs)
c.mtx.RUnlock() c.mtx.Unlock()
continue continue
} }
c.mtx.RUnlock() delete(c.sentIDs, response.ID.(types.JSONRPCIntID))
c.mtx.Unlock()
c.Logger.Info("got response", "id", response.ID, "result", fmt.Sprintf("%X", response.Result)) c.Logger.Info("got response", "id", response.ID, "result", fmt.Sprintf("%X", response.Result))
// Combine a non-blocking read on BaseService.Quit with a non-blocking write on ResponsesCh to avoid blocking // Combine a non-blocking read on BaseService.Quit with a non-blocking write on ResponsesCh to avoid blocking

View File

@ -29,7 +29,7 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, cdc *amino.Codec, logger lo
WriteRPCResponseHTTP(w, types.RPCInvalidRequestError(nil, errors.Wrap(err, "error reading request body"))) WriteRPCResponseHTTP(w, types.RPCInvalidRequestError(nil, errors.Wrap(err, "error reading request body")))
return return
} }
defer r.Body.Close() // nolint: errcheck r.Body.Close() // nolint: errcheck
// if its an empty request (like from a browser), // if its an empty request (like from a browser),
// just display a list of functions // just display a list of functions