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.

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