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.

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