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.

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