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.

152 lines
5.8 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
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
8 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
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
8 years ago
  1. package client
  2. /*
  3. The client package provides a general purpose interface (Client) for connecting
  4. to a tendermint node, as well as higher-level functionality.
  5. The main implementation for production code is client.HTTP, which
  6. connects via http to the jsonrpc interface of the tendermint node.
  7. For connecting to a node running in the same process (eg. when
  8. compiling the abci app in the same process), you can use the client.Local
  9. implementation.
  10. For mocking out server responses during testing to see behavior for
  11. arbitrary return values, use the mock package.
  12. In addition to the Client interface, which should be used externally
  13. for maximum flexibility and testability, and two implementations,
  14. this package also provides helper functions that work on any Client
  15. implementation.
  16. */
  17. import (
  18. "context"
  19. "github.com/tendermint/tendermint/libs/bytes"
  20. "github.com/tendermint/tendermint/libs/service"
  21. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  22. "github.com/tendermint/tendermint/types"
  23. )
  24. // Client wraps most important rpc calls a client would make if you want to
  25. // listen for events, test if it also implements events.EventSwitch.
  26. type Client interface {
  27. service.Service
  28. ABCIClient
  29. EventsClient
  30. HistoryClient
  31. NetworkClient
  32. SignClient
  33. StatusClient
  34. EvidenceClient
  35. MempoolClient
  36. }
  37. // ABCIClient groups together the functionality that principally affects the
  38. // ABCI app.
  39. //
  40. // In many cases this will be all we want, so we can accept an interface which
  41. // is easier to mock.
  42. type ABCIClient interface {
  43. // Reading from abci app
  44. ABCIInfo(context.Context) (*ctypes.ResultABCIInfo, error)
  45. ABCIQuery(ctx context.Context, path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error)
  46. ABCIQueryWithOptions(ctx context.Context, path string, data bytes.HexBytes,
  47. opts ABCIQueryOptions) (*ctypes.ResultABCIQuery, error)
  48. // Writing to abci app
  49. BroadcastTxCommit(context.Context, types.Tx) (*ctypes.ResultBroadcastTxCommit, error)
  50. BroadcastTxAsync(context.Context, types.Tx) (*ctypes.ResultBroadcastTx, error)
  51. BroadcastTxSync(context.Context, types.Tx) (*ctypes.ResultBroadcastTx, error)
  52. }
  53. // SignClient groups together the functionality needed to get valid signatures
  54. // and prove anything about the chain.
  55. type SignClient interface {
  56. Block(ctx context.Context, height *int64) (*ctypes.ResultBlock, error)
  57. BlockByHash(ctx context.Context, hash []byte) (*ctypes.ResultBlock, error)
  58. BlockResults(ctx context.Context, height *int64) (*ctypes.ResultBlockResults, error)
  59. Commit(ctx context.Context, height *int64) (*ctypes.ResultCommit, error)
  60. Validators(ctx context.Context, height *int64, page, perPage *int) (*ctypes.ResultValidators, error)
  61. Tx(ctx context.Context, hash []byte, prove bool) (*ctypes.ResultTx, error)
  62. // TxSearch defines a method to search for a paginated set of transactions by
  63. // DeliverTx event search criteria.
  64. TxSearch(
  65. ctx context.Context,
  66. query string,
  67. prove bool,
  68. page, perPage *int,
  69. orderBy string,
  70. ) (*ctypes.ResultTxSearch, error)
  71. // BlockSearch defines a method to search for a paginated set of blocks by
  72. // BeginBlock and EndBlock event search criteria.
  73. BlockSearch(
  74. ctx context.Context,
  75. query string,
  76. page, perPage *int,
  77. orderBy string,
  78. ) (*ctypes.ResultBlockSearch, error)
  79. }
  80. // HistoryClient provides access to data from genesis to now in large chunks.
  81. type HistoryClient interface {
  82. Genesis(context.Context) (*ctypes.ResultGenesis, error)
  83. GenesisChunked(context.Context, uint) (*ctypes.ResultGenesisChunk, error)
  84. BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error)
  85. }
  86. // StatusClient provides access to general chain info.
  87. type StatusClient interface {
  88. Status(context.Context) (*ctypes.ResultStatus, error)
  89. }
  90. // NetworkClient is general info about the network state. May not be needed
  91. // usually.
  92. type NetworkClient interface {
  93. NetInfo(context.Context) (*ctypes.ResultNetInfo, error)
  94. DumpConsensusState(context.Context) (*ctypes.ResultDumpConsensusState, error)
  95. ConsensusState(context.Context) (*ctypes.ResultConsensusState, error)
  96. ConsensusParams(ctx context.Context, height *int64) (*ctypes.ResultConsensusParams, error)
  97. Health(context.Context) (*ctypes.ResultHealth, error)
  98. }
  99. // EventsClient is reactive, you can subscribe to any message, given the proper
  100. // string. see tendermint/types/events.go
  101. type EventsClient interface {
  102. // Subscribe subscribes given subscriber to query. Returns a channel with
  103. // cap=1 onto which events are published. An error is returned if it fails to
  104. // subscribe. outCapacity can be used optionally to set capacity for the
  105. // channel. Channel is never closed to prevent accidental reads.
  106. //
  107. // ctx cannot be used to unsubscribe. To unsubscribe, use either Unsubscribe
  108. // or UnsubscribeAll.
  109. Subscribe(ctx context.Context, subscriber, query string, outCapacity ...int) (out <-chan ctypes.ResultEvent, err error)
  110. // Unsubscribe unsubscribes given subscriber from query.
  111. Unsubscribe(ctx context.Context, subscriber, query string) error
  112. // UnsubscribeAll unsubscribes given subscriber from all the queries.
  113. UnsubscribeAll(ctx context.Context, subscriber string) error
  114. }
  115. // MempoolClient shows us data about current mempool state.
  116. type MempoolClient interface {
  117. UnconfirmedTxs(ctx context.Context, limit *int) (*ctypes.ResultUnconfirmedTxs, error)
  118. NumUnconfirmedTxs(context.Context) (*ctypes.ResultUnconfirmedTxs, error)
  119. CheckTx(context.Context, types.Tx) (*ctypes.ResultCheckTx, error)
  120. }
  121. // EvidenceClient is used for submitting an evidence of the malicious
  122. // behavior.
  123. type EvidenceClient interface {
  124. BroadcastEvidence(context.Context, types.Evidence) (*ctypes.ResultBroadcastEvidence, error)
  125. }
  126. // RemoteClient is a Client, which can also return the remote network address.
  127. type RemoteClient interface {
  128. Client
  129. // Remote returns the remote network address in a string form.
  130. Remote() string
  131. }