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.

103 lines
2.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
  1. package rpcserver
  2. import (
  3. "net/http"
  4. "reflect"
  5. "strings"
  6. "github.com/pkg/errors"
  7. amino "github.com/tendermint/go-amino"
  8. "github.com/tendermint/tendermint/libs/log"
  9. )
  10. // RegisterRPCFuncs adds a route for each function in the funcMap, as well as
  11. // general jsonrpc and websocket handlers for all functions. "result" is the
  12. // interface on which the result objects are registered, and is popualted with
  13. // every RPCResponse
  14. func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc, cdc *amino.Codec, logger log.Logger) {
  15. // HTTP endpoints
  16. for funcName, rpcFunc := range funcMap {
  17. mux.HandleFunc("/"+funcName, makeHTTPHandler(rpcFunc, cdc, logger))
  18. }
  19. // JSONRPC endpoints
  20. mux.HandleFunc("/", handleInvalidJSONRPCPaths(makeJSONRPCHandler(funcMap, cdc, logger)))
  21. }
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // Function introspection
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // RPCFunc contains the introspected type information for a function
  26. type RPCFunc struct {
  27. f reflect.Value // underlying rpc function
  28. args []reflect.Type // type of each function arg
  29. returns []reflect.Type // type of each return arg
  30. argNames []string // name of each argument
  31. ws bool // websocket only
  32. }
  33. // NewRPCFunc wraps a function for introspection.
  34. // f is the function, args are comma separated argument names
  35. func NewRPCFunc(f interface{}, args string) *RPCFunc {
  36. return newRPCFunc(f, args, false)
  37. }
  38. // NewWSRPCFunc wraps a function for introspection and use in the websockets.
  39. func NewWSRPCFunc(f interface{}, args string) *RPCFunc {
  40. return newRPCFunc(f, args, true)
  41. }
  42. func newRPCFunc(f interface{}, args string, ws bool) *RPCFunc {
  43. var argNames []string
  44. if args != "" {
  45. argNames = strings.Split(args, ",")
  46. }
  47. return &RPCFunc{
  48. f: reflect.ValueOf(f),
  49. args: funcArgTypes(f),
  50. returns: funcReturnTypes(f),
  51. argNames: argNames,
  52. ws: ws,
  53. }
  54. }
  55. // return a function's argument types
  56. func funcArgTypes(f interface{}) []reflect.Type {
  57. t := reflect.TypeOf(f)
  58. n := t.NumIn()
  59. typez := make([]reflect.Type, n)
  60. for i := 0; i < n; i++ {
  61. typez[i] = t.In(i)
  62. }
  63. return typez
  64. }
  65. // return a function's return types
  66. func funcReturnTypes(f interface{}) []reflect.Type {
  67. t := reflect.TypeOf(f)
  68. n := t.NumOut()
  69. typez := make([]reflect.Type, n)
  70. for i := 0; i < n; i++ {
  71. typez[i] = t.Out(i)
  72. }
  73. return typez
  74. }
  75. //-------------------------------------------------------------
  76. // NOTE: assume returns is result struct and error. If error is not nil, return it
  77. func unreflectResult(returns []reflect.Value) (interface{}, error) {
  78. errV := returns[1]
  79. if errV.Interface() != nil {
  80. return nil, errors.Errorf("%v", errV.Interface())
  81. }
  82. rv := returns[0]
  83. // the result is a registered interface,
  84. // we need a pointer to it so we can marshal with type byte
  85. rvp := reflect.New(rv.Type())
  86. rvp.Elem().Set(rv)
  87. return rvp.Interface(), nil
  88. }