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.

213 lines
5.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>
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
  1. package core
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "time"
  6. cfg "github.com/tendermint/tendermint/config"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/internal/consensus"
  9. mempl "github.com/tendermint/tendermint/internal/mempool"
  10. "github.com/tendermint/tendermint/internal/p2p"
  11. tmjson "github.com/tendermint/tendermint/libs/json"
  12. "github.com/tendermint/tendermint/libs/log"
  13. "github.com/tendermint/tendermint/proxy"
  14. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  15. sm "github.com/tendermint/tendermint/state"
  16. "github.com/tendermint/tendermint/state/indexer"
  17. "github.com/tendermint/tendermint/types"
  18. )
  19. const (
  20. // see README
  21. defaultPerPage = 30
  22. maxPerPage = 100
  23. // SubscribeTimeout is the maximum time we wait to subscribe for an event.
  24. // must be less than the server's write timeout (see rpcserver.DefaultConfig)
  25. SubscribeTimeout = 5 * time.Second
  26. // genesisChunkSize is the maximum size, in bytes, of each
  27. // chunk in the genesis structure for the chunked API
  28. genesisChunkSize = 16 * 1024 * 1024 // 16
  29. )
  30. //----------------------------------------------
  31. // These interfaces are used by RPC and must be thread safe
  32. type consensusState interface {
  33. GetState() sm.State
  34. GetValidators() (int64, []*types.Validator)
  35. GetLastHeight() int64
  36. GetRoundStateJSON() ([]byte, error)
  37. GetRoundStateSimpleJSON() ([]byte, error)
  38. }
  39. type transport interface {
  40. Listeners() []string
  41. IsListening() bool
  42. NodeInfo() types.NodeInfo
  43. }
  44. type peers interface {
  45. AddPersistentPeers([]string) error
  46. AddUnconditionalPeerIDs([]string) error
  47. AddPrivatePeerIDs([]string) error
  48. DialPeersAsync([]string) error
  49. Peers() p2p.IPeerSet
  50. }
  51. type consensusReactor interface {
  52. WaitSync() bool
  53. GetPeerState(peerID types.NodeID) (*consensus.PeerState, bool)
  54. }
  55. type peerManager interface {
  56. Peers() []types.NodeID
  57. Addresses(types.NodeID) []p2p.NodeAddress
  58. }
  59. //----------------------------------------------
  60. // Environment contains objects and interfaces used by the RPC. It is expected
  61. // to be setup once during startup.
  62. type Environment struct {
  63. // external, thread safe interfaces
  64. ProxyAppQuery proxy.AppConnQuery
  65. ProxyAppMempool proxy.AppConnMempool
  66. // interfaces defined in types and above
  67. StateStore sm.Store
  68. BlockStore sm.BlockStore
  69. EvidencePool sm.EvidencePool
  70. ConsensusState consensusState
  71. ConsensusReactor consensusReactor
  72. P2PPeers peers
  73. // Legacy p2p stack
  74. P2PTransport transport
  75. // interfaces for new p2p interfaces
  76. PeerManager peerManager
  77. // objects
  78. PubKey crypto.PubKey
  79. GenDoc *types.GenesisDoc // cache the genesis structure
  80. EventSinks []indexer.EventSink
  81. EventBus *types.EventBus // thread safe
  82. Mempool mempl.Mempool
  83. BlockSyncReactor consensus.BlockSyncReactor
  84. Logger log.Logger
  85. Config cfg.RPCConfig
  86. // cache of chunked genesis data.
  87. genChunks []string
  88. }
  89. //----------------------------------------------
  90. func validatePage(pagePtr *int, perPage, totalCount int) (int, error) {
  91. // this can only happen if we haven't first run validatePerPage
  92. if perPage < 1 {
  93. panic(fmt.Errorf("%w (%d)", ctypes.ErrZeroOrNegativePerPage, perPage))
  94. }
  95. if pagePtr == nil { // no page parameter
  96. return 1, nil
  97. }
  98. pages := ((totalCount - 1) / perPage) + 1
  99. if pages == 0 {
  100. pages = 1 // one page (even if it's empty)
  101. }
  102. page := *pagePtr
  103. if page <= 0 || page > pages {
  104. return 1, fmt.Errorf("%w expected range: [1, %d], given %d", ctypes.ErrPageOutOfRange, pages, page)
  105. }
  106. return page, nil
  107. }
  108. func (env *Environment) validatePerPage(perPagePtr *int) int {
  109. if perPagePtr == nil { // no per_page parameter
  110. return defaultPerPage
  111. }
  112. perPage := *perPagePtr
  113. if perPage < 1 {
  114. return defaultPerPage
  115. // in unsafe mode there is no max on the page size but in safe mode
  116. // we cap it to maxPerPage
  117. } else if perPage > maxPerPage && !env.Config.Unsafe {
  118. return maxPerPage
  119. }
  120. return perPage
  121. }
  122. // InitGenesisChunks configures the environment and should be called on service
  123. // startup.
  124. func (env *Environment) InitGenesisChunks() error {
  125. if env.genChunks != nil {
  126. return nil
  127. }
  128. if env.GenDoc == nil {
  129. return nil
  130. }
  131. data, err := tmjson.Marshal(env.GenDoc)
  132. if err != nil {
  133. return err
  134. }
  135. for i := 0; i < len(data); i += genesisChunkSize {
  136. end := i + genesisChunkSize
  137. if end > len(data) {
  138. end = len(data)
  139. }
  140. env.genChunks = append(env.genChunks, base64.StdEncoding.EncodeToString(data[i:end]))
  141. }
  142. return nil
  143. }
  144. func validateSkipCount(page, perPage int) int {
  145. skipCount := (page - 1) * perPage
  146. if skipCount < 0 {
  147. return 0
  148. }
  149. return skipCount
  150. }
  151. // latestHeight can be either latest committed or uncommitted (+1) height.
  152. func (env *Environment) getHeight(latestHeight int64, heightPtr *int64) (int64, error) {
  153. if heightPtr != nil {
  154. height := *heightPtr
  155. if height <= 0 {
  156. return 0, fmt.Errorf("%w (requested height: %d)", ctypes.ErrZeroOrNegativeHeight, height)
  157. }
  158. if height > latestHeight {
  159. return 0, fmt.Errorf("%w (requested height: %d, blockchain height: %d)",
  160. ctypes.ErrHeightExceedsChainHead, height, latestHeight)
  161. }
  162. base := env.BlockStore.Base()
  163. if height < base {
  164. return 0, fmt.Errorf("%w (requested height: %d, base height: %d)", ctypes.ErrHeightNotAvailable, height, base)
  165. }
  166. return height, nil
  167. }
  168. return latestHeight, nil
  169. }
  170. func (env *Environment) latestUncommittedHeight() int64 {
  171. nodeIsSyncing := env.ConsensusReactor.WaitSync()
  172. if nodeIsSyncing {
  173. return env.BlockStore.Height()
  174. }
  175. return env.BlockStore.Height() + 1
  176. }