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.

172 lines
4.4 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>
5 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>
5 years ago
  1. package core
  2. import (
  3. "fmt"
  4. "time"
  5. cfg "github.com/tendermint/tendermint/config"
  6. "github.com/tendermint/tendermint/consensus"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/libs/log"
  9. mempl "github.com/tendermint/tendermint/mempool"
  10. "github.com/tendermint/tendermint/p2p"
  11. "github.com/tendermint/tendermint/proxy"
  12. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  13. sm "github.com/tendermint/tendermint/state"
  14. "github.com/tendermint/tendermint/state/indexer"
  15. "github.com/tendermint/tendermint/state/txindex"
  16. "github.com/tendermint/tendermint/types"
  17. )
  18. const (
  19. // see README
  20. defaultPerPage = 30
  21. maxPerPage = 100
  22. // SubscribeTimeout is the maximum time we wait to subscribe for an event.
  23. // must be less than the server's write timeout (see rpcserver.DefaultConfig)
  24. SubscribeTimeout = 5 * time.Second
  25. )
  26. var (
  27. // set by Node
  28. env *Environment
  29. )
  30. // SetEnvironment sets up the given Environment.
  31. // It will race if multiple Node call SetEnvironment.
  32. func SetEnvironment(e *Environment) {
  33. env = e
  34. }
  35. //----------------------------------------------
  36. // These interfaces are used by RPC and must be thread safe
  37. type Consensus interface {
  38. GetState() sm.State
  39. GetValidators() (int64, []*types.Validator)
  40. GetLastHeight() int64
  41. GetRoundStateJSON() ([]byte, error)
  42. GetRoundStateSimpleJSON() ([]byte, error)
  43. }
  44. type transport interface {
  45. Listeners() []string
  46. IsListening() bool
  47. NodeInfo() p2p.NodeInfo
  48. }
  49. type peers interface {
  50. AddPersistentPeers([]string) error
  51. AddUnconditionalPeerIDs([]string) error
  52. AddPrivatePeerIDs([]string) error
  53. DialPeersAsync([]string) error
  54. Peers() p2p.IPeerSet
  55. }
  56. //----------------------------------------------
  57. // Environment contains objects and interfaces used by the RPC. It is expected
  58. // to be setup once during startup.
  59. type Environment struct {
  60. // external, thread safe interfaces
  61. ProxyAppQuery proxy.AppConnQuery
  62. ProxyAppMempool proxy.AppConnMempool
  63. // interfaces defined in types and above
  64. StateStore sm.Store
  65. BlockStore sm.BlockStore
  66. EvidencePool sm.EvidencePool
  67. ConsensusState Consensus
  68. P2PPeers peers
  69. P2PTransport transport
  70. // objects
  71. PubKey crypto.PubKey
  72. GenDoc *types.GenesisDoc // cache the genesis structure
  73. TxIndexer txindex.TxIndexer
  74. BlockIndexer indexer.BlockIndexer
  75. ConsensusReactor *consensus.Reactor
  76. EventBus *types.EventBus // thread safe
  77. Mempool mempl.Mempool
  78. Logger log.Logger
  79. Config cfg.RPCConfig
  80. }
  81. //----------------------------------------------
  82. func validatePage(pagePtr *int, perPage, totalCount int) (int, error) {
  83. // this can only happen if we haven't first run validatePerPage
  84. if perPage < 1 {
  85. panic(fmt.Errorf("%w (%d)", ctypes.ErrZeroOrNegativePerPage, perPage))
  86. }
  87. if pagePtr == nil { // no page parameter
  88. return 1, nil
  89. }
  90. pages := ((totalCount - 1) / perPage) + 1
  91. if pages == 0 {
  92. pages = 1 // one page (even if it's empty)
  93. }
  94. page := *pagePtr
  95. if page <= 0 || page > pages {
  96. return 1, fmt.Errorf("%w expected range: [1, %d], given %d", ctypes.ErrPageOutOfRange, pages, page)
  97. }
  98. return page, nil
  99. }
  100. func validatePerPage(perPagePtr *int) int {
  101. if perPagePtr == nil { // no per_page parameter
  102. return defaultPerPage
  103. }
  104. perPage := *perPagePtr
  105. if perPage < 1 {
  106. return defaultPerPage
  107. // in unsafe mode there is no max on the page size but in safe mode
  108. // we cap it to maxPerPage
  109. } else if perPage > maxPerPage && !env.Config.Unsafe {
  110. return maxPerPage
  111. }
  112. return perPage
  113. }
  114. func validateSkipCount(page, perPage int) int {
  115. skipCount := (page - 1) * perPage
  116. if skipCount < 0 {
  117. return 0
  118. }
  119. return skipCount
  120. }
  121. // latestHeight can be either latest committed or uncommitted (+1) height.
  122. func getHeight(latestHeight int64, heightPtr *int64) (int64, error) {
  123. if heightPtr != nil {
  124. height := *heightPtr
  125. if height <= 0 {
  126. return 0, fmt.Errorf("%w (requested height: %d)", ctypes.ErrZeroOrNegativeHeight, height)
  127. }
  128. if height > latestHeight {
  129. return 0, fmt.Errorf("%w (requested height: %d, blockchain height: %d)",
  130. ctypes.ErrHeightExceedsChainHead, height, latestHeight)
  131. }
  132. base := env.BlockStore.Base()
  133. if height < base {
  134. return 0, fmt.Errorf("%w (requested height: %d, base height: %d)", ctypes.ErrHeightNotAvailable, height, base)
  135. }
  136. return height, nil
  137. }
  138. return latestHeight, nil
  139. }
  140. func latestUncommittedHeight() int64 {
  141. nodeIsSyncing := env.ConsensusReactor.WaitSync()
  142. if nodeIsSyncing {
  143. return env.BlockStore.Height()
  144. }
  145. return env.BlockStore.Height() + 1
  146. }