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.

443 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. 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. 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. require.Nil(t, err, "%+v", err)
  37. defer resp.Body.Close()
  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. res, err := mc.UnconfirmedTxs(1)
  248. require.Nil(t, err, "%d: %+v", i, err)
  249. assert.Equal(t, 1, res.Count)
  250. assert.Equal(t, 1, res.Total)
  251. assert.Equal(t, mempool.TxsBytes(), res.TotalBytes)
  252. assert.Exactly(t, types.Txs{tx}, types.Txs(res.Txs))
  253. }
  254. mempool.Flush()
  255. }
  256. func TestNumUnconfirmedTxs(t *testing.T) {
  257. _, _, tx := MakeTxKV()
  258. mempool := node.MempoolReactor().Mempool
  259. _ = mempool.CheckTx(tx, nil)
  260. mempoolSize := mempool.Size()
  261. for i, c := range GetClients() {
  262. mc, ok := c.(client.MempoolClient)
  263. require.True(t, ok, "%d", i)
  264. res, err := mc.NumUnconfirmedTxs()
  265. require.Nil(t, err, "%d: %+v", i, err)
  266. assert.Equal(t, mempoolSize, res.Count)
  267. assert.Equal(t, mempoolSize, res.Total)
  268. assert.Equal(t, mempool.TxsBytes(), res.TotalBytes)
  269. }
  270. mempool.Flush()
  271. }
  272. func TestTx(t *testing.T) {
  273. // first we broadcast a tx
  274. c := getHTTPClient()
  275. _, _, tx := MakeTxKV()
  276. bres, err := c.BroadcastTxCommit(tx)
  277. require.Nil(t, err, "%+v", err)
  278. txHeight := bres.Height
  279. txHash := bres.Hash
  280. anotherTxHash := types.Tx("a different tx").Hash()
  281. cases := []struct {
  282. valid bool
  283. hash []byte
  284. prove bool
  285. }{
  286. // only valid if correct hash provided
  287. {true, txHash, false},
  288. {true, txHash, true},
  289. {false, anotherTxHash, false},
  290. {false, anotherTxHash, true},
  291. {false, nil, false},
  292. {false, nil, true},
  293. }
  294. for i, c := range GetClients() {
  295. for j, tc := range cases {
  296. t.Logf("client %d, case %d", i, j)
  297. // now we query for the tx.
  298. // since there's only one tx, we know index=0.
  299. ptx, err := c.Tx(tc.hash, tc.prove)
  300. if !tc.valid {
  301. require.NotNil(t, err)
  302. } else {
  303. require.Nil(t, err, "%+v", err)
  304. assert.EqualValues(t, txHeight, ptx.Height)
  305. assert.EqualValues(t, tx, ptx.Tx)
  306. assert.Zero(t, ptx.Index)
  307. assert.True(t, ptx.TxResult.IsOK())
  308. assert.EqualValues(t, txHash, ptx.Hash)
  309. // time to verify the proof
  310. proof := ptx.Proof
  311. if tc.prove && assert.EqualValues(t, tx, proof.Data) {
  312. assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
  313. }
  314. }
  315. }
  316. }
  317. }
  318. func TestTxSearch(t *testing.T) {
  319. // first we broadcast a tx
  320. c := getHTTPClient()
  321. _, _, tx := MakeTxKV()
  322. bres, err := c.BroadcastTxCommit(tx)
  323. require.Nil(t, err, "%+v", err)
  324. txHeight := bres.Height
  325. txHash := bres.Hash
  326. anotherTxHash := types.Tx("a different tx").Hash()
  327. for i, c := range GetClients() {
  328. t.Logf("client %d", i)
  329. // now we query for the tx.
  330. // since there's only one tx, we know index=0.
  331. result, err := c.TxSearch(fmt.Sprintf("tx.hash='%v'", txHash), true, 1, 30)
  332. require.Nil(t, err, "%+v", err)
  333. require.Len(t, result.Txs, 1)
  334. ptx := result.Txs[0]
  335. assert.EqualValues(t, txHeight, ptx.Height)
  336. assert.EqualValues(t, tx, ptx.Tx)
  337. assert.Zero(t, ptx.Index)
  338. assert.True(t, ptx.TxResult.IsOK())
  339. assert.EqualValues(t, txHash, ptx.Hash)
  340. // time to verify the proof
  341. proof := ptx.Proof
  342. if assert.EqualValues(t, tx, proof.Data) {
  343. assert.NoError(t, proof.Proof.Verify(proof.RootHash, txHash))
  344. }
  345. // query by height
  346. result, err = c.TxSearch(fmt.Sprintf("tx.height=%d", txHeight), true, 1, 30)
  347. require.Nil(t, err, "%+v", err)
  348. require.Len(t, result.Txs, 1)
  349. // query for non existing tx
  350. result, err = c.TxSearch(fmt.Sprintf("tx.hash='%X'", anotherTxHash), false, 1, 30)
  351. require.Nil(t, err, "%+v", err)
  352. require.Len(t, result.Txs, 0)
  353. // query using a tag (see kvstore application)
  354. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko'", false, 1, 30)
  355. require.Nil(t, err, "%+v", err)
  356. if len(result.Txs) == 0 {
  357. t.Fatal("expected a lot of transactions")
  358. }
  359. // query using a tag (see kvstore application) and height
  360. result, err = c.TxSearch("app.creator='Cosmoshi Netowoko' AND tx.height<10000", true, 1, 30)
  361. require.Nil(t, err, "%+v", err)
  362. if len(result.Txs) == 0 {
  363. t.Fatal("expected a lot of transactions")
  364. }
  365. // query a non existing tx with page 1 and txsPerPage 1
  366. result, err = c.TxSearch("app.creator='Cosmoshi Neetowoko'", true, 1, 1)
  367. require.Nil(t, err, "%+v", err)
  368. require.Len(t, result.Txs, 0)
  369. }
  370. }