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.

257 lines
7.7 KiB

limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
7 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
7 years ago
  1. package proxy
  2. import (
  3. "context"
  4. "fmt"
  5. cmn "github.com/tendermint/tendermint/libs/common"
  6. "github.com/tendermint/tendermint/crypto/merkle"
  7. "github.com/tendermint/tendermint/lite"
  8. rpcclient "github.com/tendermint/tendermint/rpc/client"
  9. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  10. rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
  11. )
  12. var _ rpcclient.Client = Wrapper{}
  13. // Wrapper wraps a rpcclient with a Verifier and double-checks any input that is
  14. // provable before passing it along. Allows you to make any rpcclient fully secure.
  15. type Wrapper struct {
  16. rpcclient.Client
  17. cert *lite.DynamicVerifier
  18. prt *merkle.ProofRuntime
  19. }
  20. // SecureClient uses a given Verifier to wrap an connection to an untrusted
  21. // host and return a cryptographically secure rpc client.
  22. //
  23. // If it is wrapping an HTTP rpcclient, it will also wrap the websocket interface
  24. func SecureClient(c rpcclient.Client, cert *lite.DynamicVerifier) Wrapper {
  25. prt := defaultProofRuntime()
  26. wrap := Wrapper{c, cert, prt}
  27. // TODO: no longer possible as no more such interface exposed....
  28. // if we wrap http client, then we can swap out the event switch to filter
  29. // if hc, ok := c.(*rpcclient.HTTP); ok {
  30. // evt := hc.WSEvents.EventSwitch
  31. // hc.WSEvents.EventSwitch = WrappedSwitch{evt, wrap}
  32. // }
  33. return wrap
  34. }
  35. // ABCIQueryWithOptions exposes all options for the ABCI query and verifies the returned proof
  36. func (w Wrapper) ABCIQueryWithOptions(path string, data cmn.HexBytes,
  37. opts rpcclient.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
  38. res, err := GetWithProofOptions(w.prt, path, data, opts, w.Client, w.cert)
  39. return res, err
  40. }
  41. // ABCIQuery uses default options for the ABCI query and verifies the returned proof
  42. func (w Wrapper) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.ResultABCIQuery, error) {
  43. return w.ABCIQueryWithOptions(path, data, rpcclient.DefaultABCIQueryOptions)
  44. }
  45. // Tx queries for a given tx and verifies the proof if it was requested
  46. func (w Wrapper) Tx(hash []byte, prove bool) (*ctypes.ResultTx, error) {
  47. res, err := w.Client.Tx(hash, prove)
  48. if !prove || err != nil {
  49. return res, err
  50. }
  51. h := res.Height
  52. sh, err := GetCertifiedCommit(h, w.Client, w.cert)
  53. if err != nil {
  54. return res, err
  55. }
  56. err = res.Proof.Validate(sh.DataHash)
  57. return res, err
  58. }
  59. // BlockchainInfo requests a list of headers and verifies them all...
  60. // Rather expensive.
  61. //
  62. // TODO: optimize this if used for anything needing performance
  63. func (w Wrapper) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
  64. r, err := w.Client.BlockchainInfo(minHeight, maxHeight)
  65. if err != nil {
  66. return nil, err
  67. }
  68. // go and verify every blockmeta in the result....
  69. for _, meta := range r.BlockMetas {
  70. // get a checkpoint to verify from
  71. res, err := w.Commit(&meta.Header.Height)
  72. if err != nil {
  73. return nil, err
  74. }
  75. sh := res.SignedHeader
  76. err = ValidateBlockMeta(meta, sh)
  77. if err != nil {
  78. return nil, err
  79. }
  80. }
  81. return r, nil
  82. }
  83. // Block returns an entire block and verifies all signatures
  84. func (w Wrapper) Block(height *int64) (*ctypes.ResultBlock, error) {
  85. resBlock, err := w.Client.Block(height)
  86. if err != nil {
  87. return nil, err
  88. }
  89. // get a checkpoint to verify from
  90. resCommit, err := w.Commit(height)
  91. if err != nil {
  92. return nil, err
  93. }
  94. sh := resCommit.SignedHeader
  95. err = ValidateBlock(resBlock.Block, sh)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return resBlock, nil
  100. }
  101. // Commit downloads the Commit and certifies it with the lite.
  102. //
  103. // This is the foundation for all other verification in this module
  104. func (w Wrapper) Commit(height *int64) (*ctypes.ResultCommit, error) {
  105. if height == nil {
  106. resStatus, err := w.Client.Status()
  107. if err != nil {
  108. return nil, err
  109. }
  110. // NOTE: If resStatus.CatchingUp, there is a race
  111. // condition where the validator set for the next height
  112. // isn't available until some time after the blockstore
  113. // has height h on the remote node. This isn't an issue
  114. // once the node has caught up, and a syncing node likely
  115. // won't have this issue esp with the implementation we
  116. // have here, but we may have to address this at some
  117. // point.
  118. height = new(int64)
  119. *height = resStatus.SyncInfo.LatestBlockHeight
  120. }
  121. rpcclient.WaitForHeight(w.Client, *height, nil)
  122. res, err := w.Client.Commit(height)
  123. // if we got it, then verify it
  124. if err == nil {
  125. sh := res.SignedHeader
  126. err = w.cert.Verify(sh)
  127. }
  128. return res, err
  129. }
  130. func (w Wrapper) RegisterOpDecoder(typ string, dec merkle.OpDecoder) {
  131. w.prt.RegisterOpDecoder(typ, dec)
  132. }
  133. // SubscribeWS subscribes for events using the given query and remote address as
  134. // a subscriber, but does not verify responses (UNSAFE)!
  135. func (w Wrapper) SubscribeWS(ctx *rpctypes.Context, query string) (*ctypes.ResultSubscribe, error) {
  136. out, err := w.Client.Subscribe(context.Background(), ctx.RemoteAddr(), query)
  137. if err != nil {
  138. return nil, err
  139. }
  140. go func() {
  141. for {
  142. select {
  143. case resultEvent := <-out:
  144. // XXX(melekes) We should have a switch here that performs a validation
  145. // depending on the event's type.
  146. ctx.WSConn.TryWriteRPCResponse(
  147. rpctypes.NewRPCSuccessResponse(
  148. ctx.WSConn.Codec(),
  149. rpctypes.JSONRPCStringID(fmt.Sprintf("%v#event", ctx.JSONReq.ID)),
  150. resultEvent,
  151. ))
  152. case <-w.Client.Quit():
  153. return
  154. }
  155. }
  156. }()
  157. return &ctypes.ResultSubscribe{}, nil
  158. }
  159. // UnsubscribeWS calls original client's Unsubscribe using remote address as a
  160. // subscriber.
  161. func (w Wrapper) UnsubscribeWS(ctx *rpctypes.Context, query string) (*ctypes.ResultUnsubscribe, error) {
  162. err := w.Client.Unsubscribe(context.Background(), ctx.RemoteAddr(), query)
  163. if err != nil {
  164. return nil, err
  165. }
  166. return &ctypes.ResultUnsubscribe{}, nil
  167. }
  168. // UnsubscribeAllWS calls original client's UnsubscribeAll using remote address
  169. // as a subscriber.
  170. func (w Wrapper) UnsubscribeAllWS(ctx *rpctypes.Context) (*ctypes.ResultUnsubscribe, error) {
  171. err := w.Client.UnsubscribeAll(context.Background(), ctx.RemoteAddr())
  172. if err != nil {
  173. return nil, err
  174. }
  175. return &ctypes.ResultUnsubscribe{}, nil
  176. }
  177. // // WrappedSwitch creates a websocket connection that auto-verifies any info
  178. // // coming through before passing it along.
  179. // //
  180. // // Since the verification takes 1-2 rpc calls, this is obviously only for
  181. // // relatively low-throughput situations that can tolerate a bit extra latency
  182. // type WrappedSwitch struct {
  183. // types.EventSwitch
  184. // client rpcclient.Client
  185. // }
  186. // // FireEvent verifies any block or header returned from the eventswitch
  187. // func (s WrappedSwitch) FireEvent(event string, data events.EventData) {
  188. // tm, ok := data.(types.TMEventData)
  189. // if !ok {
  190. // fmt.Printf("bad type %#v\n", data)
  191. // return
  192. // }
  193. // // check to validate it if possible, and drop if not valid
  194. // switch t := tm.(type) {
  195. // case types.EventDataNewBlockHeader:
  196. // err := verifyHeader(s.client, t.Header)
  197. // if err != nil {
  198. // fmt.Printf("Invalid header: %#v\n", err)
  199. // return
  200. // }
  201. // case types.EventDataNewBlock:
  202. // err := verifyBlock(s.client, t.Block)
  203. // if err != nil {
  204. // fmt.Printf("Invalid block: %#v\n", err)
  205. // return
  206. // }
  207. // // TODO: can we verify tx as well? anything else
  208. // }
  209. // // looks good, we fire it
  210. // s.EventSwitch.FireEvent(event, data)
  211. // }
  212. // func verifyHeader(c rpcclient.Client, head *types.Header) error {
  213. // // get a checkpoint to verify from
  214. // commit, err := c.Commit(&head.Height)
  215. // if err != nil {
  216. // return err
  217. // }
  218. // check := certclient.CommitFromResult(commit)
  219. // return ValidateHeader(head, check)
  220. // }
  221. //
  222. // func verifyBlock(c rpcclient.Client, block *types.Block) error {
  223. // // get a checkpoint to verify from
  224. // commit, err := c.Commit(&block.Height)
  225. // if err != nil {
  226. // return err
  227. // }
  228. // check := certclient.CommitFromResult(commit)
  229. // return ValidateBlock(block, check)
  230. // }