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.

248 lines
6.8 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
  1. package rpcserver
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "reflect"
  9. "sort"
  10. "github.com/pkg/errors"
  11. amino "github.com/tendermint/go-amino"
  12. "github.com/tendermint/tendermint/libs/log"
  13. types "github.com/tendermint/tendermint/rpc/lib/types"
  14. )
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // HTTP + JSON handler
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // jsonrpc calls grab the given method's function info and runs reflect.Call
  19. func makeJSONRPCHandler(funcMap map[string]*RPCFunc, cdc *amino.Codec, logger log.Logger) http.HandlerFunc {
  20. return func(w http.ResponseWriter, r *http.Request) {
  21. b, err := ioutil.ReadAll(r.Body)
  22. if err != nil {
  23. WriteRPCResponseHTTP(
  24. w,
  25. types.RPCInvalidRequestError(
  26. nil,
  27. errors.Wrap(err, "error reading request body"),
  28. ),
  29. )
  30. return
  31. }
  32. // if its an empty request (like from a browser), just display a list of
  33. // functions
  34. if len(b) == 0 {
  35. writeListOfEndpoints(w, r, funcMap)
  36. return
  37. }
  38. // first try to unmarshal the incoming request as an array of RPC requests
  39. var (
  40. requests []types.RPCRequest
  41. responses []types.RPCResponse
  42. )
  43. if err := json.Unmarshal(b, &requests); err != nil {
  44. // next, try to unmarshal as a single request
  45. var request types.RPCRequest
  46. if err := json.Unmarshal(b, &request); err != nil {
  47. WriteRPCResponseHTTP(
  48. w,
  49. types.RPCParseError(
  50. errors.Wrap(err, "error unmarshalling request"),
  51. ),
  52. )
  53. return
  54. }
  55. requests = []types.RPCRequest{request}
  56. }
  57. for _, request := range requests {
  58. request := request
  59. // A Notification is a Request object without an "id" member.
  60. // The Server MUST NOT reply to a Notification, including those that are within a batch request.
  61. if request.ID == nil {
  62. logger.Debug(
  63. "HTTPJSONRPC received a notification, skipping... (please send a non-empty ID if you want to call a method)",
  64. "req", request,
  65. )
  66. continue
  67. }
  68. if len(r.URL.Path) > 1 {
  69. responses = append(
  70. responses,
  71. types.RPCInvalidRequestError(request.ID, errors.Errorf("path %s is invalid", r.URL.Path)),
  72. )
  73. continue
  74. }
  75. rpcFunc, ok := funcMap[request.Method]
  76. if !ok || rpcFunc.ws {
  77. responses = append(responses, types.RPCMethodNotFoundError(request.ID))
  78. continue
  79. }
  80. ctx := &types.Context{JSONReq: &request, HTTPReq: r}
  81. args := []reflect.Value{reflect.ValueOf(ctx)}
  82. if len(request.Params) > 0 {
  83. fnArgs, err := jsonParamsToArgs(rpcFunc, cdc, request.Params)
  84. if err != nil {
  85. responses = append(
  86. responses,
  87. types.RPCInvalidParamsError(request.ID, errors.Wrap(err, "error converting json params to arguments")),
  88. )
  89. continue
  90. }
  91. args = append(args, fnArgs...)
  92. }
  93. returns := rpcFunc.f.Call(args)
  94. logger.Info("HTTPJSONRPC", "method", request.Method, "args", args, "returns", returns)
  95. result, err := unreflectResult(returns)
  96. if err != nil {
  97. responses = append(responses, types.RPCInternalError(request.ID, err))
  98. continue
  99. }
  100. responses = append(responses, types.NewRPCSuccessResponse(cdc, request.ID, result))
  101. }
  102. if len(responses) > 0 {
  103. WriteRPCResponseArrayHTTP(w, responses)
  104. }
  105. }
  106. }
  107. func handleInvalidJSONRPCPaths(next http.HandlerFunc) http.HandlerFunc {
  108. return func(w http.ResponseWriter, r *http.Request) {
  109. // Since the pattern "/" matches all paths not matched by other registered patterns,
  110. // we check whether the path is indeed "/", otherwise return a 404 error
  111. if r.URL.Path != "/" {
  112. http.NotFound(w, r)
  113. return
  114. }
  115. next(w, r)
  116. }
  117. }
  118. func mapParamsToArgs(
  119. rpcFunc *RPCFunc,
  120. cdc *amino.Codec,
  121. params map[string]json.RawMessage,
  122. argsOffset int,
  123. ) ([]reflect.Value, error) {
  124. values := make([]reflect.Value, len(rpcFunc.argNames))
  125. for i, argName := range rpcFunc.argNames {
  126. argType := rpcFunc.args[i+argsOffset]
  127. if p, ok := params[argName]; ok && p != nil && len(p) > 0 {
  128. val := reflect.New(argType)
  129. err := cdc.UnmarshalJSON(p, val.Interface())
  130. if err != nil {
  131. return nil, err
  132. }
  133. values[i] = val.Elem()
  134. } else { // use default for that type
  135. values[i] = reflect.Zero(argType)
  136. }
  137. }
  138. return values, nil
  139. }
  140. func arrayParamsToArgs(
  141. rpcFunc *RPCFunc,
  142. cdc *amino.Codec,
  143. params []json.RawMessage,
  144. argsOffset int,
  145. ) ([]reflect.Value, error) {
  146. if len(rpcFunc.argNames) != len(params) {
  147. return nil, errors.Errorf("expected %v parameters (%v), got %v (%v)",
  148. len(rpcFunc.argNames), rpcFunc.argNames, len(params), params)
  149. }
  150. values := make([]reflect.Value, len(params))
  151. for i, p := range params {
  152. argType := rpcFunc.args[i+argsOffset]
  153. val := reflect.New(argType)
  154. err := cdc.UnmarshalJSON(p, val.Interface())
  155. if err != nil {
  156. return nil, err
  157. }
  158. values[i] = val.Elem()
  159. }
  160. return values, nil
  161. }
  162. // raw is unparsed json (from json.RawMessage) encoding either a map or an
  163. // array.
  164. //
  165. // Example:
  166. // rpcFunc.args = [rpctypes.Context string]
  167. // rpcFunc.argNames = ["arg"]
  168. func jsonParamsToArgs(rpcFunc *RPCFunc, cdc *amino.Codec, raw []byte) ([]reflect.Value, error) {
  169. const argsOffset = 1
  170. // TODO: Make more efficient, perhaps by checking the first character for '{' or '['?
  171. // First, try to get the map.
  172. var m map[string]json.RawMessage
  173. err := json.Unmarshal(raw, &m)
  174. if err == nil {
  175. return mapParamsToArgs(rpcFunc, cdc, m, argsOffset)
  176. }
  177. // Otherwise, try an array.
  178. var a []json.RawMessage
  179. err = json.Unmarshal(raw, &a)
  180. if err == nil {
  181. return arrayParamsToArgs(rpcFunc, cdc, a, argsOffset)
  182. }
  183. // Otherwise, bad format, we cannot parse
  184. return nil, errors.Errorf("unknown type for JSON params: %v. Expected map or array", err)
  185. }
  186. // writes a list of available rpc endpoints as an html page
  187. func writeListOfEndpoints(w http.ResponseWriter, r *http.Request, funcMap map[string]*RPCFunc) {
  188. noArgNames := []string{}
  189. argNames := []string{}
  190. for name, funcData := range funcMap {
  191. if len(funcData.args) == 0 {
  192. noArgNames = append(noArgNames, name)
  193. } else {
  194. argNames = append(argNames, name)
  195. }
  196. }
  197. sort.Strings(noArgNames)
  198. sort.Strings(argNames)
  199. buf := new(bytes.Buffer)
  200. buf.WriteString("<html><body>")
  201. buf.WriteString("<br>Available endpoints:<br>")
  202. for _, name := range noArgNames {
  203. link := fmt.Sprintf("//%s/%s", r.Host, name)
  204. buf.WriteString(fmt.Sprintf("<a href=\"%s\">%s</a></br>", link, link))
  205. }
  206. buf.WriteString("<br>Endpoints that require arguments:<br>")
  207. for _, name := range argNames {
  208. link := fmt.Sprintf("//%s/%s?", r.Host, name)
  209. funcData := funcMap[name]
  210. for i, argName := range funcData.argNames {
  211. link += argName + "=_"
  212. if i < len(funcData.argNames)-1 {
  213. link += "&"
  214. }
  215. }
  216. buf.WriteString(fmt.Sprintf("<a href=\"%s\">%s</a></br>", link, link))
  217. }
  218. buf.WriteString("</body></html>")
  219. w.Header().Set("Content-Type", "text/html")
  220. w.WriteHeader(200)
  221. w.Write(buf.Bytes()) // nolint: errcheck
  222. }