You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
4.9 KiB

rpc/lib/client & server: try to conform to JSON-RPC 2.0 spec (#4141) https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
5 years ago
rpc/lib/client & server: try to conform to JSON-RPC 2.0 spec (#4141) https://www.jsonrpc.org/specification What is done in this PR: JSONRPCClient: validate that Response.ID matches Request.ID I wanted to do the same for the WSClient, but since we're sending events as responses, not notifications, checking IDs would require storing them in memory indefinitely (and we won't be able to remove them upon client unsubscribing because ID is different then). Request.ID is now optional. Notification is a Request without an ID. Previously "" or 0 were considered as notifications Remove #event suffix from ID from an event response (partially fixes #2949) ID must be either string, int or null AND must be equal to request's ID. Now, because we've implemented events as responses, WS clients are tripping when they see Response.ID("0#event") != Request.ID("0"). Implementing events as requests would require a lot of time (~ 2 days to completely rewrite WS client and server) generate unique ID for each request switch to integer IDs instead of "json-client-XYZ" id=0 method=/subscribe id=0 result=... id=1 method=/abci_query id=1 result=... > send events (resulting from /subscribe) as requests+notifications (not responses) this will require a lot of work. probably not worth it * rpc: generate an unique ID for each request in conformance with JSON-RPC spec * WSClient: check for unsolicited responses * fix golangci warnings * save commit * fix errors * remove ID from responses from subscribe Refs #2949 * clients are safe for concurrent access * tm-bench: switch to int ID * fixes after my own review * comment out sentIDs in WSClient see commit body for the reason * remove body.Close it will be closed automatically * stop ws connection outside of write/read routines also, use t.Rate in tm-bench indexer when calculating ID fix gocritic issues * update swagger.yaml * Apply suggestions from code review * fix stylecheck and golint linter warnings * update changelog * update changelog2
5 years ago
  1. package rpcserver
  2. import (
  3. "encoding/hex"
  4. "net/http"
  5. "reflect"
  6. "strings"
  7. "github.com/pkg/errors"
  8. amino "github.com/tendermint/go-amino"
  9. "github.com/tendermint/tendermint/libs/log"
  10. types "github.com/tendermint/tendermint/rpc/lib/types"
  11. )
  12. ///////////////////////////////////////////////////////////////////////////////
  13. // HTTP + URI handler
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // convert from a function name to the http handler
  16. func makeHTTPHandler(rpcFunc *RPCFunc, cdc *amino.Codec, logger log.Logger) func(http.ResponseWriter, *http.Request) {
  17. // Always return -1 as there's no ID here.
  18. dummyID := types.JSONRPCIntID(-1) // URIClientRequestID
  19. // Exception for websocket endpoints
  20. if rpcFunc.ws {
  21. return func(w http.ResponseWriter, r *http.Request) {
  22. WriteRPCResponseHTTP(w, types.RPCMethodNotFoundError(dummyID))
  23. }
  24. }
  25. // All other endpoints
  26. return func(w http.ResponseWriter, r *http.Request) {
  27. logger.Debug("HTTP HANDLER", "req", r)
  28. ctx := &types.Context{HTTPReq: r}
  29. args := []reflect.Value{reflect.ValueOf(ctx)}
  30. fnArgs, err := httpParamsToArgs(rpcFunc, cdc, r)
  31. if err != nil {
  32. WriteRPCResponseHTTP(
  33. w,
  34. types.RPCInvalidParamsError(
  35. dummyID,
  36. errors.Wrap(err, "error converting http params to arguments"),
  37. ),
  38. )
  39. return
  40. }
  41. args = append(args, fnArgs...)
  42. returns := rpcFunc.f.Call(args)
  43. logger.Info("HTTPRestRPC", "method", r.URL.Path, "args", args, "returns", returns)
  44. result, err := unreflectResult(returns)
  45. if err != nil {
  46. WriteRPCResponseHTTP(w, types.RPCInternalError(dummyID, err))
  47. return
  48. }
  49. WriteRPCResponseHTTP(w, types.NewRPCSuccessResponse(cdc, dummyID, result))
  50. }
  51. }
  52. // Covert an http query to a list of properly typed values.
  53. // To be properly decoded the arg must be a concrete type from tendermint (if its an interface).
  54. func httpParamsToArgs(rpcFunc *RPCFunc, cdc *amino.Codec, r *http.Request) ([]reflect.Value, error) {
  55. // skip types.Context
  56. const argsOffset = 1
  57. values := make([]reflect.Value, len(rpcFunc.argNames))
  58. for i, name := range rpcFunc.argNames {
  59. argType := rpcFunc.args[i+argsOffset]
  60. values[i] = reflect.Zero(argType) // set default for that type
  61. arg := GetParam(r, name)
  62. // log.Notice("param to arg", "argType", argType, "name", name, "arg", arg)
  63. if arg == "" {
  64. continue
  65. }
  66. v, ok, err := nonJSONStringToArg(cdc, argType, arg)
  67. if err != nil {
  68. return nil, err
  69. }
  70. if ok {
  71. values[i] = v
  72. continue
  73. }
  74. values[i], err = jsonStringToArg(cdc, argType, arg)
  75. if err != nil {
  76. return nil, err
  77. }
  78. }
  79. return values, nil
  80. }
  81. func jsonStringToArg(cdc *amino.Codec, rt reflect.Type, arg string) (reflect.Value, error) {
  82. rv := reflect.New(rt)
  83. err := cdc.UnmarshalJSON([]byte(arg), rv.Interface())
  84. if err != nil {
  85. return rv, err
  86. }
  87. rv = rv.Elem()
  88. return rv, nil
  89. }
  90. func nonJSONStringToArg(cdc *amino.Codec, rt reflect.Type, arg string) (reflect.Value, bool, error) {
  91. if rt.Kind() == reflect.Ptr {
  92. rv1, ok, err := nonJSONStringToArg(cdc, rt.Elem(), arg)
  93. switch {
  94. case err != nil:
  95. return reflect.Value{}, false, err
  96. case ok:
  97. rv := reflect.New(rt.Elem())
  98. rv.Elem().Set(rv1)
  99. return rv, true, nil
  100. default:
  101. return reflect.Value{}, false, nil
  102. }
  103. } else {
  104. return _nonJSONStringToArg(cdc, rt, arg)
  105. }
  106. }
  107. // NOTE: rt.Kind() isn't a pointer.
  108. func _nonJSONStringToArg(cdc *amino.Codec, rt reflect.Type, arg string) (reflect.Value, bool, error) {
  109. isIntString := ReInt.Match([]byte(arg))
  110. isQuotedString := strings.HasPrefix(arg, `"`) && strings.HasSuffix(arg, `"`)
  111. isHexString := strings.HasPrefix(strings.ToLower(arg), "0x")
  112. var expectingString, expectingByteSlice, expectingInt bool
  113. switch rt.Kind() {
  114. case reflect.Int,
  115. reflect.Uint,
  116. reflect.Int8,
  117. reflect.Uint8,
  118. reflect.Int16,
  119. reflect.Uint16,
  120. reflect.Int32,
  121. reflect.Uint32,
  122. reflect.Int64,
  123. reflect.Uint64:
  124. expectingInt = true
  125. case reflect.String:
  126. expectingString = true
  127. case reflect.Slice:
  128. expectingByteSlice = rt.Elem().Kind() == reflect.Uint8
  129. }
  130. if isIntString && expectingInt {
  131. qarg := `"` + arg + `"`
  132. rv, err := jsonStringToArg(cdc, rt, qarg)
  133. if err != nil {
  134. return rv, false, err
  135. }
  136. return rv, true, nil
  137. }
  138. if isHexString {
  139. if !expectingString && !expectingByteSlice {
  140. err := errors.Errorf("got a hex string arg, but expected '%s'",
  141. rt.Kind().String())
  142. return reflect.ValueOf(nil), false, err
  143. }
  144. var value []byte
  145. value, err := hex.DecodeString(arg[2:])
  146. if err != nil {
  147. return reflect.ValueOf(nil), false, err
  148. }
  149. if rt.Kind() == reflect.String {
  150. return reflect.ValueOf(string(value)), true, nil
  151. }
  152. return reflect.ValueOf(value), true, nil
  153. }
  154. if isQuotedString && expectingByteSlice {
  155. v := reflect.New(reflect.TypeOf(""))
  156. err := cdc.UnmarshalJSON([]byte(arg), v.Interface())
  157. if err != nil {
  158. return reflect.ValueOf(nil), false, err
  159. }
  160. v = v.Elem()
  161. return reflect.ValueOf([]byte(v.String())), true, nil
  162. }
  163. return reflect.ValueOf(nil), false, nil
  164. }