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.

717 lines
20 KiB

rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
8 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
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
rpc: add support for batched requests/responses (#3534) Continues from #3280 in building support for batched requests/responses in the JSON RPC (as per issue #3213). * Add JSON RPC batching for client and server As per #3213, this adds support for [JSON RPC batch requests and responses](https://www.jsonrpc.org/specification#batch). * Add additional checks to ensure client responses are the same as results * Fix case where a notification is sent and no response is expected * Add test to check that JSON RPC notifications in a batch are left out in responses * Update CHANGELOG_PENDING.md * Update PR number now that PR has been created * Make errors start with lowercase letter * Refactor batch functionality to be standalone This refactors the batching functionality to rather act in a standalone way. In light of supporting concurrent goroutines making use of the same client, it would make sense to have batching functionality where one could create a batch of requests per goroutine and send that batch without interfering with a batch from another goroutine. * Add examples for simple and batch HTTP client usage * Check errors from writer and remove nolinter directives * Make error strings start with lowercase letter * Refactor examples to make them testable * Use safer deferred shutdown for example Tendermint test node * Recompose rpcClient interface from pre-existing interface components * Rename WaitGroup for brevity * Replace empty ID string with request ID * Remove extraneous test case * Convert first letter of errors.Wrap() messages to lowercase * Remove extraneous function parameter * Make variable declaration terse * Reorder WaitGroup.Done call to help prevent race conditions in the face of failure * Swap mutex to value representation and remove initialization * Restore empty JSONRPC string ID in response to prevent nil * Make JSONRPCBufferedRequest private * Revert PR hard link in CHANGELOG_PENDING * Add client ID for JSONRPCClient This adds code to automatically generate a randomized client ID for the JSONRPCClient, and adds a check of the IDs in the responses (if one was set in the requests). * Extract response ID validation into separate function * Remove extraneous comments * Reorder fields to indicate clearly which are protected by the mutex * Refactor for loop to remove indexing * Restructure and combine loop * Flatten conditional block for better readability * Make multi-variable declaration slightly more readable * Change for loop style * Compress error check statements * Make function description more generic to show that we support different protocols * Preallocate memory for request and result objects
6 years ago
  1. package client_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/rand"
  6. "net/http"
  7. "strings"
  8. "sync"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. abci "github.com/tendermint/tendermint/abci/types"
  13. "github.com/tendermint/tendermint/crypto/ed25519"
  14. "github.com/tendermint/tendermint/crypto/tmhash"
  15. cmn "github.com/tendermint/tendermint/libs/common"
  16. mempl "github.com/tendermint/tendermint/mempool"
  17. "github.com/tendermint/tendermint/privval"
  18. "github.com/tendermint/tendermint/rpc/client"
  19. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  20. rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
  21. rpctest "github.com/tendermint/tendermint/rpc/test"
  22. "github.com/tendermint/tendermint/types"
  23. )
  24. func getHTTPClient() *client.HTTP {
  25. rpcAddr := rpctest.GetConfig().RPC.ListenAddress
  26. return client.NewHTTP(rpcAddr, "/websocket")
  27. }
  28. func getLocalClient() *client.Local {
  29. return client.NewLocal(node)
  30. }
  31. // GetClients returns a slice of clients for table-driven tests
  32. func GetClients() []client.Client {
  33. return []client.Client{
  34. getHTTPClient(),
  35. getLocalClient(),
  36. }
  37. }
  38. func TestNilCustomHTTPClient(t *testing.T) {
  39. require.Panics(t, func() {
  40. client.NewHTTPWithClient("http://example.com", "/websocket", nil)
  41. })
  42. require.Panics(t, func() {
  43. rpcclient.NewJSONRPCClientWithHTTPClient("http://example.com", nil)
  44. })
  45. }
  46. func TestCustomHTTPClient(t *testing.T) {
  47. remote := rpctest.GetConfig().RPC.ListenAddress
  48. c := client.NewHTTPWithClient(remote, "/websocket", http.DefaultClient)
  49. status, err := c.Status()
  50. require.NoError(t, err)
  51. require.NotNil(t, status)
  52. }
  53. func TestCorsEnabled(t *testing.T) {
  54. origin := rpctest.GetConfig().RPC.CORSAllowedOrigins[0]
  55. remote := strings.Replace(rpctest.GetConfig().RPC.ListenAddress, "tcp", "http", -1)
  56. req, err := http.NewRequest("GET", remote, nil)
  57. require.Nil(t, err, "%+v", err)
  58. req.Header.Set("Origin", origin)
  59. c := &http.Client{}
  60. resp, err := c.Do(req)
  61. require.Nil(t, err, "%+v", err)
  62. defer resp.Body.Close()
  63. assert.Equal(t, resp.Header.Get("Access-Control-Allow-Origin"), origin)
  64. }
  65. // Make sure status is correct (we connect properly)
  66. func TestStatus(t *testing.T) {
  67. for i, c := range GetClients() {
  68. moniker := rpctest.GetConfig().Moniker
  69. status, err := c.Status()
  70. require.Nil(t, err, "%d: %+v", i, err)
  71. assert.Equal(t, moniker, status.NodeInfo.Moniker)
  72. }
  73. }
  74. // Make sure info is correct (we connect properly)
  75. func TestInfo(t *testing.T) {
  76. for i, c := range GetClients() {
  77. // status, err := c.Status()
  78. // require.Nil(t, err, "%+v", err)
  79. info, err := c.ABCIInfo()
  80. require.Nil(t, err, "%d: %+v", i, err)
  81. // TODO: this is not correct - fix merkleeyes!
  82. // assert.EqualValues(t, status.SyncInfo.LatestBlockHeight, info.Response.LastBlockHeight)
  83. assert.True(t, strings.Contains(info.Response.Data, "size"))
  84. }
  85. }
  86. func TestNetInfo(t *testing.T) {
  87. for i, c := range GetClients() {
  88. nc, ok := c.(client.NetworkClient)
  89. require.True(t, ok, "%d", i)
  90. netinfo, err := nc.NetInfo()
  91. require.Nil(t, err, "%d: %+v", i, err)
  92. assert.True(t, netinfo.Listening)
  93. assert.Equal(t, 0, len(netinfo.Peers))
  94. }
  95. }
  96. func TestDumpConsensusState(t *testing.T) {
  97. for i, c := range GetClients() {
  98. // FIXME: fix server so it doesn't panic on invalid input
  99. nc, ok := c.(client.NetworkClient)
  100. require.True(t, ok, "%d", i)
  101. cons, err := nc.DumpConsensusState()
  102. require.Nil(t, err, "%d: %+v", i, err)
  103. assert.NotEmpty(t, cons.RoundState)
  104. assert.Empty(t, cons.Peers)
  105. }
  106. }
  107. func TestConsensusState(t *testing.T) {
  108. for i, c := range GetClients() {
  109. // FIXME: fix server so it doesn't panic on invalid input
  110. nc, ok := c.(client.NetworkClient)
  111. require.True(t, ok, "%d", i)
  112. cons, err := nc.ConsensusState()
  113. require.Nil(t, err, "%d: %+v", i, err)
  114. assert.NotEmpty(t, cons.RoundState)
  115. }
  116. }
  117. func TestHealth(t *testing.T) {
  118. for i, c := range GetClients() {
  119. nc, ok := c.(client.NetworkClient)
  120. require.True(t, ok, "%d", i)
  121. _, err := nc.Health()
  122. require.Nil(t, err, "%d: %+v", i, err)
  123. }
  124. }
  125. func TestGenesisAndValidators(t *testing.T) {
  126. for i, c := range GetClients() {
  127. // make sure this is the right genesis file
  128. gen, err := c.Genesis()
  129. require.Nil(t, err, "%d: %+v", i, err)
  130. // get the genesis validator
  131. require.Equal(t, 1, len(gen.Genesis.Validators))
  132. gval := gen.Genesis.Validators[0]
  133. // get the current validators
  134. vals, err := c.Validators(nil)
  135. require.Nil(t, err, "%d: %+v", i, err)
  136. require.Equal(t, 1, len(vals.Validators))
  137. val := vals.Validators[0]
  138. // make sure the current set is also the genesis set
  139. assert.Equal(t, gval.Power, val.VotingPower)
  140. assert.Equal(t, gval.PubKey, val.PubKey)
  141. }
  142. }
  143. func TestABCIQuery(t *testing.T) {
  144. for i, c := range GetClients() {
  145. // write something
  146. k, v, tx := MakeTxKV()
  147. bres, err := c.BroadcastTxCommit(tx)
  148. require.Nil(t, err, "%d: %+v", i, err)
  149. apph := bres.Height + 1 // this is where the tx will be applied to the state
  150. // wait before querying
  151. client.WaitForHeight(c, apph, nil)
  152. res, err := c.ABCIQuery("/key", k)
  153. qres := res.Response
  154. if assert.Nil(t, err) && assert.True(t, qres.IsOK()) {
  155. assert.EqualValues(t, v, qres.Value)
  156. }
  157. }
  158. }
  159. // Make some app checks
  160. func TestAppCalls(t *testing.T) {
  161. assert, require := assert.New(t), require.New(t)
  162. for i, c := range GetClients() {
  163. // get an offset of height to avoid racing and guessing
  164. s, err := c.Status()
  165. require.Nil(err, "%d: %+v", i, err)
  166. // sh is start height or status height
  167. sh := s.SyncInfo.LatestBlockHeight
  168. // look for the future
  169. h := sh + 2
  170. _, err = c.Block(&h)
  171. assert.NotNil(err) // no block yet
  172. // write something
  173. k, v, tx := MakeTxKV()
  174. bres, err := c.BroadcastTxCommit(tx)
  175. require.Nil(err, "%d: %+v", i, err)
  176. require.True(bres.DeliverTx.IsOK())
  177. txh := bres.Height
  178. apph := txh + 1 // this is where the tx will be applied to the state
  179. // wait before querying
  180. if err := client.WaitForHeight(c, apph, nil); err != nil {
  181. t.Error(err)
  182. }
  183. _qres, err := c.ABCIQueryWithOptions("/key", k, client.ABCIQueryOptions{Prove: false})
  184. qres := _qres.Response
  185. if assert.Nil(err) && assert.True(qres.IsOK()) {
  186. assert.Equal(k, qres.Key)
  187. assert.EqualValues(v, qres.Value)
  188. }
  189. // make sure we can lookup the tx with proof
  190. ptx, err := c.Tx(bres.Hash, true)
  191. require.Nil(err, "%d: %+v", i, err)
  192. assert.EqualValues(txh, ptx.Height)
  193. assert.EqualValues(tx, ptx.Tx)
  194. // and we can even check the block is added
  195. block, err := c.Block(&apph)
  196. require.Nil(err, "%d: %+v", i, err)
  197. appHash := block.BlockMeta.Header.AppHash
  198. assert.True(len(appHash) > 0)
  199. assert.EqualValues(apph, block.BlockMeta.Header.Height)
  200. // now check the results
  201. blockResults, err := c.BlockResults(&txh)
  202. require.Nil(err, "%d: %+v", i, err)
  203. assert.Equal(txh, blockResults.Height)
  204. if assert.Equal(1, len(blockResults.Results.DeliverTx)) {
  205. // check success code
  206. assert.EqualValues(0, blockResults.Results.DeliverTx[0].Code)
  207. }
  208. // check blockchain info, now that we know there is info
  209. info, err := c.BlockchainInfo(apph, apph)
  210. require.Nil(err, "%d: %+v", i, err)
  211. assert.True(info.LastHeight >= apph)
  212. if assert.Equal(1, len(info.BlockMetas)) {
  213. lastMeta := info.BlockMetas[0]
  214. assert.EqualValues(apph, lastMeta.Header.Height)
  215. bMeta := block.BlockMeta
  216. assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash)
  217. assert.Equal(bMeta.BlockID, lastMeta.BlockID)
  218. }
  219. // and get the corresponding commit with the same apphash
  220. commit, err := c.Commit(&apph)
  221. require.Nil(err, "%d: %+v", i, err)
  222. cappHash := commit.Header.AppHash
  223. assert.Equal(appHash, cappHash)
  224. assert.NotNil(commit.Commit)
  225. // compare the commits (note Commit(2) has commit from Block(3))
  226. h = apph - 1
  227. commit2, err := c.Commit(&h)
  228. require.Nil(err, "%d: %+v", i, err)
  229. assert.Equal(block.Block.LastCommit, commit2.Commit)
  230. // and we got a proof that works!
  231. _pres, err := c.ABCIQueryWithOptions("/key", k, client.ABCIQueryOptions{Prove: true})
  232. pres := _pres.Response
  233. assert.Nil(err)
  234. assert.True(pres.IsOK())
  235. // XXX Test proof
  236. }
  237. }
  238. func TestBroadcastTxSync(t *testing.T) {
  239. require := require.New(t)
  240. // TODO (melekes): use mempool which is set on RPC rather than getting it from node
  241. mempool := node.Mempool()
  242. initMempoolSize := mempool.Size()
  243. for i, c := range GetClients() {
  244. _, _, tx := MakeTxKV()
  245. bres, err := c.BroadcastTxSync(tx)
  246. require.Nil(err, "%d: %+v", i, err)
  247. require.Equal(bres.Code, abci.CodeTypeOK) // FIXME
  248. require.Equal(initMempoolSize+1, mempool.Size())
  249. txs := mempool.ReapMaxTxs(len(tx))
  250. require.EqualValues(tx, txs[0])
  251. mempool.Flush()
  252. }
  253. }
  254. func TestBroadcastTxCommit(t *testing.T) {
  255. require := require.New(t)
  256. mempool := node.Mempool()
  257. for i, c := range GetClients() {
  258. _, _, tx := MakeTxKV()
  259. bres, err := c.BroadcastTxCommit(tx)
  260. require.Nil(err, "%d: %+v", i, err)
  261. require.True(bres.CheckTx.IsOK())
  262. require.True(bres.DeliverTx.IsOK())
  263. require.Equal(0, mempool.Size())
  264. }
  265. }
  266. func TestUnconfirmedTxs(t *testing.T) {
  267. _, _, tx := MakeTxKV()
  268. mempool := node.Mempool()
  269. _ = mempool.CheckTx(tx, nil, mempl.TxInfo{})
  270. for i, c := range GetClients() {
  271. mc, ok := c.(client.MempoolClient)
  272. require.True(t, ok, "%d", i)
  273. res, err := mc.UnconfirmedTxs(1)
  274. require.Nil(t, err, "%d: %+v", i, err)
  275. assert.Equal(t, 1, res.Count)
  276. assert.Equal(t, 1, res.Total)
  277. assert.Equal(t, mempool.TxsBytes(), res.TotalBytes)
  278. assert.Exactly(t, types.Txs{tx}, types.Txs(res.Txs))
  279. }
  280. mempool.Flush()
  281. }
  282. func TestNumUnconfirmedTxs(t *testing.T) {
  283. _, _, tx := MakeTxKV()
  284. mempool := node.Mempool()
  285. _ = mempool.CheckTx(tx, nil, mempl.TxInfo{})
  286. mempoolSize := mempool.Size()
  287. for i, c := range GetClients() {
  288. mc, ok := c.(client.MempoolClient)
  289. require.True(t, ok, "%d", i)
  290. res, err := mc.NumUnconfirmedTxs()
  291. require.Nil(t, err, "%d: %+v", i, err)
  292. assert.Equal(t, mempoolSize, res.Count)
  293. assert.Equal(t, mempoolSize, res.Total)
  294. assert.Equal(t, mempool.TxsBytes(), res.TotalBytes)
  295. }
  296. mempool.Flush()
  297. }
  298. func TestTx(t *testing.T) {
  299. // first we broadcast a tx
  300. c := getHTTPClient()
  301. _, _, tx := MakeTxKV()
  302. bres, err := c.BroadcastTxCommit(tx)
  303. require.Nil(t, err, "%+v", err)
  304. txHeight := bres.Height
  305. txHash := bres.Hash
  306. anotherTxHash := types.Tx("a different tx").Hash()
  307. cases := []struct {
  308. valid bool
  309. prove bool
  310. hash []byte
  311. }{
  312. // only valid if correct hash provided
  313. {true, false, txHash},
  314. {true, true, txHash},
  315. {false, false, anotherTxHash},
  316. {false, true, anotherTxHash},
  317. {false, false, nil},
  318. {false, true, nil},
  319. }
  320. for i, c := range GetClients() {
  321. for j, tc := range cases {
  322. t.Logf("client %d, case %d", i, j)
  323. // now we query for the tx.
  324. // since there's only one tx, we know index=0.
  325. ptx, err := c.Tx(tc.hash, tc.prove)
  326. if !tc.valid {
  327. require.NotNil(t, err)
  328. } else {
  329. require.Nil(t, err, "%+v", err)
  330. assert.EqualValues(t, txHeight, ptx.Height)
  331. assert.EqualValues(t, tx, ptx.Tx)
  332. assert.Zero(t, ptx.Index)
  333. assert.True(t, ptx.TxResult.IsOK())
  334. assert.EqualValues(t, txHash, ptx.Hash)
  335. // time to verify the proof
  336. proof := ptx.Proof
  337. if tc.prove && assert.EqualValues(t, tx, proof.Data) {
  338. assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
  339. }
  340. }
  341. }
  342. }
  343. }
  344. func TestTxSearch(t *testing.T) {
  345. // first we broadcast a tx
  346. c := getHTTPClient()
  347. _, _, tx := MakeTxKV()
  348. bres, err := c.BroadcastTxCommit(tx)
  349. require.Nil(t, err, "%+v", err)
  350. txHeight := bres.Height
  351. txHash := bres.Hash
  352. anotherTxHash := types.Tx("a different tx").Hash()
  353. for i, c := range GetClients() {
  354. t.Logf("client %d", i)
  355. // now we query for the tx.
  356. // since there's only one tx, we know index=0.
  357. result, err := c.TxSearch(fmt.Sprintf("tx.hash='%v'", txHash), true, 1, 30)
  358. require.Nil(t, err, "%+v", err)
  359. require.Len(t, result.Txs, 1)
  360. ptx := result.Txs[0]
  361. assert.EqualValues(t, txHeight, ptx.Height)
  362. assert.EqualValues(t, tx, ptx.Tx)
  363. assert.Zero(t, ptx.Index)
  364. assert.True(t, ptx.TxResult.IsOK())
  365. assert.EqualValues(t, txHash, ptx.Hash)
  366. // time to verify the proof
  367. proof := ptx.Proof
  368. if assert.EqualValues(t, tx, proof.Data) {
  369. assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
  370. }
  371. // query by height
  372. result, err = c.TxSearch(fmt.Sprintf("tx.height=%d", txHeight), true, 1, 30)
  373. require.Nil(t, err, "%+v", err)
  374. require.Len(t, result.Txs, 1)
  375. // query for non existing tx
  376. result, err = c.TxSearch(fmt.Sprintf("tx.hash='%X'", anotherTxHash), false, 1, 30)
  377. require.Nil(t, err, "%+v", err)
  378. require.Len(t, result.Txs, 0)
  379. // query using a tag (see kvstore application)
  380. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko'", false, 1, 30)
  381. require.Nil(t, err, "%+v", err)
  382. if len(result.Txs) == 0 {
  383. t.Fatal("expected a lot of transactions")
  384. }
  385. // query using a tag (see kvstore application) and height
  386. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko' AND tx.height<10000", true, 1, 30)
  387. require.Nil(t, err, "%+v", err)
  388. if len(result.Txs) == 0 {
  389. t.Fatal("expected a lot of transactions")
  390. }
  391. // query a non existing tx with page 1 and txsPerPage 1
  392. result, err = c.TxSearch("app.creator='Cosmoshi Neetowoko'", true, 1, 1)
  393. require.Nil(t, err, "%+v", err)
  394. require.Len(t, result.Txs, 0)
  395. }
  396. }
  397. func deepcpVote(vote *types.Vote) (res *types.Vote) {
  398. res = &types.Vote{
  399. ValidatorAddress: make([]byte, len(vote.ValidatorAddress)),
  400. ValidatorIndex: vote.ValidatorIndex,
  401. Height: vote.Height,
  402. Round: vote.Round,
  403. Type: vote.Type,
  404. BlockID: types.BlockID{
  405. Hash: make([]byte, len(vote.BlockID.Hash)),
  406. PartsHeader: vote.BlockID.PartsHeader,
  407. },
  408. Signature: make([]byte, len(vote.Signature)),
  409. }
  410. copy(res.ValidatorAddress, vote.ValidatorAddress)
  411. copy(res.BlockID.Hash, vote.BlockID.Hash)
  412. copy(res.Signature, vote.Signature)
  413. return
  414. }
  415. func newEvidence(
  416. t *testing.T,
  417. val *privval.FilePV,
  418. vote *types.Vote,
  419. vote2 *types.Vote,
  420. chainID string,
  421. ) types.DuplicateVoteEvidence {
  422. var err error
  423. vote2_ := deepcpVote(vote2)
  424. vote2_.Signature, err = val.Key.PrivKey.Sign(vote2_.SignBytes(chainID))
  425. require.NoError(t, err)
  426. return types.DuplicateVoteEvidence{
  427. PubKey: val.Key.PubKey,
  428. VoteA: vote,
  429. VoteB: vote2_,
  430. }
  431. }
  432. func makeEvidences(
  433. t *testing.T,
  434. val *privval.FilePV,
  435. chainID string,
  436. ) (ev types.DuplicateVoteEvidence, fakes []types.DuplicateVoteEvidence) {
  437. vote := &types.Vote{
  438. ValidatorAddress: val.Key.Address,
  439. ValidatorIndex: 0,
  440. Height: 1,
  441. Round: 0,
  442. Type: types.PrevoteType,
  443. BlockID: types.BlockID{
  444. Hash: tmhash.Sum([]byte("blockhash")),
  445. PartsHeader: types.PartSetHeader{
  446. Total: 1000,
  447. Hash: tmhash.Sum([]byte("partset")),
  448. },
  449. },
  450. }
  451. var err error
  452. vote.Signature, err = val.Key.PrivKey.Sign(vote.SignBytes(chainID))
  453. require.NoError(t, err)
  454. vote2 := deepcpVote(vote)
  455. vote2.BlockID.Hash = tmhash.Sum([]byte("blockhash2"))
  456. ev = newEvidence(t, val, vote, vote2, chainID)
  457. fakes = make([]types.DuplicateVoteEvidence, 42)
  458. // different address
  459. vote2 = deepcpVote(vote)
  460. for i := 0; i < 10; i++ {
  461. rand.Read(vote2.ValidatorAddress) // nolint: gosec
  462. fakes[i] = newEvidence(t, val, vote, vote2, chainID)
  463. }
  464. // different index
  465. vote2 = deepcpVote(vote)
  466. for i := 10; i < 20; i++ {
  467. vote2.ValidatorIndex = rand.Int()%100 + 1 // nolint: gosec
  468. fakes[i] = newEvidence(t, val, vote, vote2, chainID)
  469. }
  470. // different height
  471. vote2 = deepcpVote(vote)
  472. for i := 20; i < 30; i++ {
  473. vote2.Height = rand.Int63()%1000 + 100 // nolint: gosec
  474. fakes[i] = newEvidence(t, val, vote, vote2, chainID)
  475. }
  476. // different round
  477. vote2 = deepcpVote(vote)
  478. for i := 30; i < 40; i++ {
  479. vote2.Round = rand.Int()%10 + 1 // nolint: gosec
  480. fakes[i] = newEvidence(t, val, vote, vote2, chainID)
  481. }
  482. // different type
  483. vote2 = deepcpVote(vote)
  484. vote2.Type = types.PrecommitType
  485. fakes[40] = newEvidence(t, val, vote, vote2, chainID)
  486. // exactly same vote
  487. vote2 = deepcpVote(vote)
  488. fakes[41] = newEvidence(t, val, vote, vote2, chainID)
  489. return ev, fakes
  490. }
  491. func TestBroadcastEvidenceDuplicateVote(t *testing.T) {
  492. config := rpctest.GetConfig()
  493. chainID := config.ChainID()
  494. pvKeyFile := config.PrivValidatorKeyFile()
  495. pvKeyStateFile := config.PrivValidatorStateFile()
  496. pv := privval.LoadOrGenFilePV(pvKeyFile, pvKeyStateFile)
  497. ev, fakes := makeEvidences(t, pv, chainID)
  498. t.Logf("evidence %v", ev)
  499. for i, c := range GetClients() {
  500. t.Logf("client %d", i)
  501. result, err := c.BroadcastEvidence(&types.DuplicateVoteEvidence{PubKey: ev.PubKey, VoteA: ev.VoteA, VoteB: ev.VoteB})
  502. require.Nil(t, err)
  503. require.Equal(t, ev.Hash(), result.Hash, "Invalid response, result %+v", result)
  504. status, err := c.Status()
  505. require.NoError(t, err)
  506. client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
  507. ed25519pub := ev.PubKey.(ed25519.PubKeyEd25519)
  508. rawpub := ed25519pub[:]
  509. result2, err := c.ABCIQuery("/val", rawpub)
  510. require.Nil(t, err, "Error querying evidence, err %v", err)
  511. qres := result2.Response
  512. require.True(t, qres.IsOK(), "Response not OK")
  513. var v abci.ValidatorUpdate
  514. err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
  515. require.NoError(t, err, "Error reading query result, value %v", qres.Value)
  516. require.EqualValues(t, rawpub, v.PubKey.Data, "Stored PubKey not equal with expected, value %v", string(qres.Value))
  517. require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
  518. for _, fake := range fakes {
  519. _, err := c.BroadcastEvidence(&types.DuplicateVoteEvidence{
  520. PubKey: fake.PubKey,
  521. VoteA: fake.VoteA,
  522. VoteB: fake.VoteB})
  523. require.Error(t, err, "Broadcasting fake evidence succeed: %s", fake.String())
  524. }
  525. }
  526. }
  527. func TestBatchedJSONRPCCalls(t *testing.T) {
  528. c := getHTTPClient()
  529. testBatchedJSONRPCCalls(t, c)
  530. }
  531. func testBatchedJSONRPCCalls(t *testing.T, c *client.HTTP) {
  532. k1, v1, tx1 := MakeTxKV()
  533. k2, v2, tx2 := MakeTxKV()
  534. batch := c.NewBatch()
  535. r1, err := batch.BroadcastTxCommit(tx1)
  536. require.NoError(t, err)
  537. r2, err := batch.BroadcastTxCommit(tx2)
  538. require.NoError(t, err)
  539. require.Equal(t, 2, batch.Count())
  540. bresults, err := batch.Send()
  541. require.NoError(t, err)
  542. require.Len(t, bresults, 2)
  543. require.Equal(t, 0, batch.Count())
  544. bresult1, ok := bresults[0].(*ctypes.ResultBroadcastTxCommit)
  545. require.True(t, ok)
  546. require.Equal(t, *bresult1, *r1)
  547. bresult2, ok := bresults[1].(*ctypes.ResultBroadcastTxCommit)
  548. require.True(t, ok)
  549. require.Equal(t, *bresult2, *r2)
  550. apph := cmn.MaxInt64(bresult1.Height, bresult2.Height) + 1
  551. client.WaitForHeight(c, apph, nil)
  552. q1, err := batch.ABCIQuery("/key", k1)
  553. require.NoError(t, err)
  554. q2, err := batch.ABCIQuery("/key", k2)
  555. require.NoError(t, err)
  556. require.Equal(t, 2, batch.Count())
  557. qresults, err := batch.Send()
  558. require.NoError(t, err)
  559. require.Len(t, qresults, 2)
  560. require.Equal(t, 0, batch.Count())
  561. qresult1, ok := qresults[0].(*ctypes.ResultABCIQuery)
  562. require.True(t, ok)
  563. require.Equal(t, *qresult1, *q1)
  564. qresult2, ok := qresults[1].(*ctypes.ResultABCIQuery)
  565. require.True(t, ok)
  566. require.Equal(t, *qresult2, *q2)
  567. require.Equal(t, qresult1.Response.Key, k1)
  568. require.Equal(t, qresult2.Response.Key, k2)
  569. require.Equal(t, qresult1.Response.Value, v1)
  570. require.Equal(t, qresult2.Response.Value, v2)
  571. }
  572. func TestBatchedJSONRPCCallsCancellation(t *testing.T) {
  573. c := getHTTPClient()
  574. _, _, tx1 := MakeTxKV()
  575. _, _, tx2 := MakeTxKV()
  576. batch := c.NewBatch()
  577. _, err := batch.BroadcastTxCommit(tx1)
  578. require.NoError(t, err)
  579. _, err = batch.BroadcastTxCommit(tx2)
  580. require.NoError(t, err)
  581. // we should have 2 requests waiting
  582. require.Equal(t, 2, batch.Count())
  583. // we want to make sure we cleared 2 pending requests
  584. require.Equal(t, 2, batch.Clear())
  585. // now there should be no batched requests
  586. require.Equal(t, 0, batch.Count())
  587. }
  588. func TestSendingEmptyJSONRPCRequestBatch(t *testing.T) {
  589. c := getHTTPClient()
  590. batch := c.NewBatch()
  591. _, err := batch.Send()
  592. require.Error(t, err, "sending an empty batch of JSON RPC requests should result in an error")
  593. }
  594. func TestClearingEmptyJSONRPCRequestBatch(t *testing.T) {
  595. c := getHTTPClient()
  596. batch := c.NewBatch()
  597. require.Zero(t, batch.Clear(), "clearing an empty batch of JSON RPC requests should result in a 0 result")
  598. }
  599. func TestConcurrentJSONRPCBatching(t *testing.T) {
  600. var wg sync.WaitGroup
  601. c := getHTTPClient()
  602. for i := 0; i < 50; i++ {
  603. wg.Add(1)
  604. go func() {
  605. defer wg.Done()
  606. testBatchedJSONRPCCalls(t, c)
  607. }()
  608. }
  609. wg.Wait()
  610. }