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.

355 lines
9.6 KiB

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
  1. package client_test
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. abci "github.com/tendermint/abci/types"
  9. "github.com/tendermint/tendermint/rpc/client"
  10. rpctest "github.com/tendermint/tendermint/rpc/test"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func getHTTPClient() *client.HTTP {
  14. rpcAddr := rpctest.GetConfig().RPC.ListenAddress
  15. return client.NewHTTP(rpcAddr, "/websocket")
  16. }
  17. func getLocalClient() *client.Local {
  18. return client.NewLocal(node)
  19. }
  20. // GetClients returns a slice of clients for table-driven tests
  21. func GetClients() []client.Client {
  22. return []client.Client{
  23. getHTTPClient(),
  24. getLocalClient(),
  25. }
  26. }
  27. // Make sure status is correct (we connect properly)
  28. func TestStatus(t *testing.T) {
  29. for i, c := range GetClients() {
  30. moniker := rpctest.GetConfig().Moniker
  31. status, err := c.Status()
  32. require.Nil(t, err, "%d: %+v", i, err)
  33. assert.Equal(t, moniker, status.NodeInfo.Moniker)
  34. }
  35. }
  36. // Make sure info is correct (we connect properly)
  37. func TestInfo(t *testing.T) {
  38. for i, c := range GetClients() {
  39. // status, err := c.Status()
  40. // require.Nil(t, err, "%+v", err)
  41. info, err := c.ABCIInfo()
  42. require.Nil(t, err, "%d: %+v", i, err)
  43. // TODO: this is not correct - fix merkleeyes!
  44. // assert.EqualValues(t, status.LatestBlockHeight, info.Response.LastBlockHeight)
  45. assert.True(t, strings.Contains(info.Response.Data, "size"))
  46. }
  47. }
  48. func TestNetInfo(t *testing.T) {
  49. for i, c := range GetClients() {
  50. nc, ok := c.(client.NetworkClient)
  51. require.True(t, ok, "%d", i)
  52. netinfo, err := nc.NetInfo()
  53. require.Nil(t, err, "%d: %+v", i, err)
  54. assert.True(t, netinfo.Listening)
  55. assert.Equal(t, 0, len(netinfo.Peers))
  56. }
  57. }
  58. func TestDumpConsensusState(t *testing.T) {
  59. for i, c := range GetClients() {
  60. // FIXME: fix server so it doesn't panic on invalid input
  61. nc, ok := c.(client.NetworkClient)
  62. require.True(t, ok, "%d", i)
  63. cons, err := nc.DumpConsensusState()
  64. require.Nil(t, err, "%d: %+v", i, err)
  65. assert.NotEmpty(t, cons.RoundState)
  66. assert.Empty(t, cons.PeerRoundStates)
  67. }
  68. }
  69. func TestHealth(t *testing.T) {
  70. for i, c := range GetClients() {
  71. nc, ok := c.(client.NetworkClient)
  72. require.True(t, ok, "%d", i)
  73. _, err := nc.Health()
  74. require.Nil(t, err, "%d: %+v", i, err)
  75. }
  76. }
  77. func TestGenesisAndValidators(t *testing.T) {
  78. for i, c := range GetClients() {
  79. // make sure this is the right genesis file
  80. gen, err := c.Genesis()
  81. require.Nil(t, err, "%d: %+v", i, err)
  82. // get the genesis validator
  83. require.Equal(t, 1, len(gen.Genesis.Validators))
  84. gval := gen.Genesis.Validators[0]
  85. // get the current validators
  86. vals, err := c.Validators(nil)
  87. require.Nil(t, err, "%d: %+v", i, err)
  88. require.Equal(t, 1, len(vals.Validators))
  89. val := vals.Validators[0]
  90. // make sure the current set is also the genesis set
  91. assert.Equal(t, gval.Power, val.VotingPower)
  92. assert.Equal(t, gval.PubKey, val.PubKey)
  93. }
  94. }
  95. func TestABCIQuery(t *testing.T) {
  96. for i, c := range GetClients() {
  97. // write something
  98. k, v, tx := MakeTxKV()
  99. bres, err := c.BroadcastTxCommit(tx)
  100. require.Nil(t, err, "%d: %+v", i, err)
  101. apph := bres.Height + 1 // this is where the tx will be applied to the state
  102. // wait before querying
  103. client.WaitForHeight(c, apph, nil)
  104. res, err := c.ABCIQuery("/key", k)
  105. qres := res.Response
  106. if assert.Nil(t, err) && assert.True(t, qres.IsOK()) {
  107. assert.EqualValues(t, v, qres.Value)
  108. }
  109. }
  110. }
  111. // Make some app checks
  112. func TestAppCalls(t *testing.T) {
  113. assert, require := assert.New(t), require.New(t)
  114. for i, c := range GetClients() {
  115. // get an offset of height to avoid racing and guessing
  116. s, err := c.Status()
  117. require.Nil(err, "%d: %+v", i, err)
  118. // sh is start height or status height
  119. sh := s.LatestBlockHeight
  120. // look for the future
  121. h := sh + 2
  122. _, err = c.Block(&h)
  123. assert.NotNil(err) // no block yet
  124. // write something
  125. k, v, tx := MakeTxKV()
  126. bres, err := c.BroadcastTxCommit(tx)
  127. require.Nil(err, "%d: %+v", i, err)
  128. require.True(bres.DeliverTx.IsOK())
  129. txh := bres.Height
  130. apph := txh + 1 // this is where the tx will be applied to the state
  131. // wait before querying
  132. if err := client.WaitForHeight(c, apph, nil); err != nil {
  133. t.Error(err)
  134. }
  135. _qres, err := c.ABCIQueryWithOptions("/key", k, client.ABCIQueryOptions{Trusted: true})
  136. qres := _qres.Response
  137. if assert.Nil(err) && assert.True(qres.IsOK()) {
  138. // assert.Equal(k, data.GetKey()) // only returned for proofs
  139. assert.EqualValues(v, qres.Value)
  140. }
  141. // make sure we can lookup the tx with proof
  142. ptx, err := c.Tx(bres.Hash, true)
  143. require.Nil(err, "%d: %+v", i, err)
  144. assert.EqualValues(txh, ptx.Height)
  145. assert.EqualValues(tx, ptx.Tx)
  146. // and we can even check the block is added
  147. block, err := c.Block(&apph)
  148. require.Nil(err, "%d: %+v", i, err)
  149. appHash := block.BlockMeta.Header.AppHash
  150. assert.True(len(appHash) > 0)
  151. assert.EqualValues(apph, block.BlockMeta.Header.Height)
  152. // now check the results
  153. blockResults, err := c.BlockResults(&txh)
  154. require.Nil(err, "%d: %+v", i, err)
  155. assert.Equal(txh, blockResults.Height)
  156. if assert.Equal(1, len(blockResults.Results.DeliverTx)) {
  157. // check success code
  158. assert.EqualValues(0, blockResults.Results.DeliverTx[0].Code)
  159. }
  160. // check blockchain info, now that we know there is info
  161. info, err := c.BlockchainInfo(apph, apph)
  162. require.Nil(err, "%d: %+v", i, err)
  163. assert.True(info.LastHeight >= apph)
  164. if assert.Equal(1, len(info.BlockMetas)) {
  165. lastMeta := info.BlockMetas[0]
  166. assert.EqualValues(apph, lastMeta.Header.Height)
  167. bMeta := block.BlockMeta
  168. assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash)
  169. assert.Equal(bMeta.BlockID, lastMeta.BlockID)
  170. }
  171. // and get the corresponding commit with the same apphash
  172. commit, err := c.Commit(&apph)
  173. require.Nil(err, "%d: %+v", i, err)
  174. cappHash := commit.Header.AppHash
  175. assert.Equal(appHash, cappHash)
  176. assert.NotNil(commit.Commit)
  177. // compare the commits (note Commit(2) has commit from Block(3))
  178. h = apph - 1
  179. commit2, err := c.Commit(&h)
  180. require.Nil(err, "%d: %+v", i, err)
  181. assert.Equal(block.Block.LastCommit, commit2.Commit)
  182. // and we got a proof that works!
  183. _pres, err := c.ABCIQueryWithOptions("/key", k, client.ABCIQueryOptions{Trusted: false})
  184. pres := _pres.Response
  185. assert.Nil(err)
  186. assert.True(pres.IsOK())
  187. }
  188. }
  189. func TestBroadcastTxSync(t *testing.T) {
  190. require := require.New(t)
  191. mempool := node.MempoolReactor().Mempool
  192. initMempoolSize := mempool.Size()
  193. for i, c := range GetClients() {
  194. _, _, tx := MakeTxKV()
  195. bres, err := c.BroadcastTxSync(tx)
  196. require.Nil(err, "%d: %+v", i, err)
  197. require.Equal(bres.Code, abci.CodeTypeOK) // FIXME
  198. require.Equal(initMempoolSize+1, mempool.Size())
  199. txs := mempool.Reap(1)
  200. require.EqualValues(tx, txs[0])
  201. mempool.Flush()
  202. }
  203. }
  204. func TestBroadcastTxCommit(t *testing.T) {
  205. require := require.New(t)
  206. mempool := node.MempoolReactor().Mempool
  207. for i, c := range GetClients() {
  208. _, _, tx := MakeTxKV()
  209. bres, err := c.BroadcastTxCommit(tx)
  210. require.Nil(err, "%d: %+v", i, err)
  211. require.True(bres.CheckTx.IsOK())
  212. require.True(bres.DeliverTx.IsOK())
  213. require.Equal(0, mempool.Size())
  214. }
  215. }
  216. func TestTx(t *testing.T) {
  217. // first we broadcast a tx
  218. c := getHTTPClient()
  219. _, _, tx := MakeTxKV()
  220. bres, err := c.BroadcastTxCommit(tx)
  221. require.Nil(t, err, "%+v", err)
  222. txHeight := bres.Height
  223. txHash := bres.Hash
  224. anotherTxHash := types.Tx("a different tx").Hash()
  225. cases := []struct {
  226. valid bool
  227. hash []byte
  228. prove bool
  229. }{
  230. // only valid if correct hash provided
  231. {true, txHash, false},
  232. {true, txHash, true},
  233. {false, anotherTxHash, false},
  234. {false, anotherTxHash, true},
  235. {false, nil, false},
  236. {false, nil, true},
  237. }
  238. for i, c := range GetClients() {
  239. for j, tc := range cases {
  240. t.Logf("client %d, case %d", i, j)
  241. // now we query for the tx.
  242. // since there's only one tx, we know index=0.
  243. ptx, err := c.Tx(tc.hash, tc.prove)
  244. if !tc.valid {
  245. require.NotNil(t, err)
  246. } else {
  247. require.Nil(t, err, "%+v", err)
  248. assert.EqualValues(t, txHeight, ptx.Height)
  249. assert.EqualValues(t, tx, ptx.Tx)
  250. assert.Zero(t, ptx.Index)
  251. assert.True(t, ptx.TxResult.IsOK())
  252. assert.EqualValues(t, txHash, ptx.Hash)
  253. // time to verify the proof
  254. proof := ptx.Proof
  255. if tc.prove && assert.EqualValues(t, tx, proof.Data) {
  256. assert.True(t, proof.Proof.Verify(proof.Index, proof.Total, txHash, proof.RootHash))
  257. }
  258. }
  259. }
  260. }
  261. }
  262. func TestTxSearch(t *testing.T) {
  263. // first we broadcast a tx
  264. c := getHTTPClient()
  265. _, _, tx := MakeTxKV()
  266. bres, err := c.BroadcastTxCommit(tx)
  267. require.Nil(t, err, "%+v", err)
  268. txHeight := bres.Height
  269. txHash := bres.Hash
  270. anotherTxHash := types.Tx("a different tx").Hash()
  271. for i, c := range GetClients() {
  272. t.Logf("client %d", i)
  273. // now we query for the tx.
  274. // since there's only one tx, we know index=0.
  275. results, err := c.TxSearch(fmt.Sprintf("tx.hash='%v'", txHash), true)
  276. require.Nil(t, err, "%+v", err)
  277. require.Len(t, results, 1)
  278. ptx := results[0]
  279. assert.EqualValues(t, txHeight, ptx.Height)
  280. assert.EqualValues(t, tx, ptx.Tx)
  281. assert.Zero(t, ptx.Index)
  282. assert.True(t, ptx.TxResult.IsOK())
  283. assert.EqualValues(t, txHash, ptx.Hash)
  284. // time to verify the proof
  285. proof := ptx.Proof
  286. if assert.EqualValues(t, tx, proof.Data) {
  287. assert.True(t, proof.Proof.Verify(proof.Index, proof.Total, txHash, proof.RootHash))
  288. }
  289. // we query for non existing tx
  290. results, err = c.TxSearch(fmt.Sprintf("tx.hash='%X'", anotherTxHash), false)
  291. require.Nil(t, err, "%+v", err)
  292. require.Len(t, results, 0)
  293. // we query using a tag (see kvstore application)
  294. results, err = c.TxSearch("app.creator='jae'", false)
  295. require.Nil(t, err, "%+v", err)
  296. if len(results) == 0 {
  297. t.Fatal("expected a lot of transactions")
  298. }
  299. }
  300. }