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.

71 lines
1.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 rpcclient
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "github.com/pkg/errors"
  7. amino "github.com/tendermint/go-amino"
  8. types "github.com/tendermint/tendermint/rpc/lib/types"
  9. )
  10. const (
  11. // URIClientRequestID in a request ID used by URIClient
  12. URIClientRequestID = types.JSONRPCIntID(-1)
  13. )
  14. // URIClient is a JSON-RPC client, which sends POST form HTTP requests to the
  15. // remote server.
  16. //
  17. // Request values are amino encoded. Response is expected to be amino encoded.
  18. // New amino codec is used if no other codec was set using SetCodec.
  19. //
  20. // URIClient is safe for concurrent use by multiple goroutines.
  21. type URIClient struct {
  22. address string
  23. client *http.Client
  24. cdc *amino.Codec
  25. }
  26. var _ HTTPClient = (*URIClient)(nil)
  27. // NewURIClient returns a new client.
  28. // The function panics if the provided remote is invalid.
  29. func NewURIClient(remote string) *URIClient {
  30. clientAddress, err := toClientAddress(remote)
  31. if err != nil {
  32. panic(fmt.Sprintf("invalid remote %s: %s", remote, err))
  33. }
  34. return &URIClient{
  35. address: clientAddress,
  36. client: DefaultHTTPClient(remote),
  37. cdc: amino.NewCodec(),
  38. }
  39. }
  40. // Call issues a POST form HTTP request.
  41. func (c *URIClient) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
  42. values, err := argsToURLValues(c.cdc, params)
  43. if err != nil {
  44. return nil, errors.Wrap(err, "failed to encode params")
  45. }
  46. resp, err := c.client.PostForm(c.address+"/"+method, values)
  47. if err != nil {
  48. return nil, errors.Wrap(err, "PostForm failed")
  49. }
  50. defer resp.Body.Close() // nolint: errcheck
  51. responseBytes, err := ioutil.ReadAll(resp.Body)
  52. if err != nil {
  53. return nil, errors.Wrap(err, "failed to read response body")
  54. }
  55. return unmarshalResponseBytes(c.cdc, responseBytes, URIClientRequestID, result)
  56. }
  57. func (c *URIClient) Codec() *amino.Codec { return c.cdc }
  58. func (c *URIClient) SetCodec(cdc *amino.Codec) { c.cdc = cdc }