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.

275 lines
7.5 KiB

8 years ago
8 years ago
8 years ago
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. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/merkleeyes/iavl"
  8. merktest "github.com/tendermint/merkleeyes/testutil"
  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.HasPrefix(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 TestGenesisAndValidators(t *testing.T) {
  70. for i, c := range GetClients() {
  71. // make sure this is the right genesis file
  72. gen, err := c.Genesis()
  73. require.Nil(t, err, "%d: %+v", i, err)
  74. // get the genesis validator
  75. require.Equal(t, 1, len(gen.Genesis.Validators))
  76. gval := gen.Genesis.Validators[0]
  77. // get the current validators
  78. vals, err := c.Validators()
  79. require.Nil(t, err, "%d: %+v", i, err)
  80. require.Equal(t, 1, len(vals.Validators))
  81. val := vals.Validators[0]
  82. // make sure the current set is also the genesis set
  83. assert.Equal(t, gval.Amount, val.VotingPower)
  84. assert.Equal(t, gval.PubKey, val.PubKey)
  85. }
  86. }
  87. // Make some app checks
  88. func TestAppCalls(t *testing.T) {
  89. assert, require := assert.New(t), require.New(t)
  90. for i, c := range GetClients() {
  91. // get an offset of height to avoid racing and guessing
  92. s, err := c.Status()
  93. require.Nil(err, "%d: %+v", i, err)
  94. // sh is start height or status height
  95. sh := s.LatestBlockHeight
  96. // look for the future
  97. _, err = c.Block(sh + 2)
  98. assert.NotNil(err) // no block yet
  99. // write something
  100. k, v, tx := merktest.MakeTxKV()
  101. bres, err := c.BroadcastTxCommit(tx)
  102. require.Nil(err, "%d: %+v", i, err)
  103. require.True(bres.DeliverTx.Code.IsOK())
  104. txh := bres.Height
  105. apph := txh + 1 // this is where the tx will be applied to the state
  106. // wait before querying
  107. client.WaitForHeight(c, apph, nil)
  108. qres, err := c.ABCIQuery("/key", k, false)
  109. if assert.Nil(err) && assert.True(qres.Code.IsOK()) {
  110. // assert.Equal(k, data.GetKey()) // only returned for proofs
  111. assert.EqualValues(v, qres.Value)
  112. }
  113. // make sure we can lookup the tx with proof
  114. // ptx, err := c.Tx(bres.Hash, true)
  115. ptx, err := c.Tx(bres.Hash, true)
  116. require.Nil(err, "%d: %+v", i, err)
  117. assert.Equal(txh, ptx.Height)
  118. assert.EqualValues(tx, ptx.Tx)
  119. // and we can even check the block is added
  120. block, err := c.Block(apph)
  121. require.Nil(err, "%d: %+v", i, err)
  122. appHash := block.BlockMeta.Header.AppHash
  123. assert.True(len(appHash) > 0)
  124. assert.EqualValues(apph, block.BlockMeta.Header.Height)
  125. // check blockchain info, now that we know there is info
  126. // TODO: is this commented somewhere that they are returned
  127. // in order of descending height???
  128. info, err := c.BlockchainInfo(apph, apph)
  129. require.Nil(err, "%d: %+v", i, err)
  130. assert.True(info.LastHeight >= apph)
  131. if assert.Equal(1, len(info.BlockMetas)) {
  132. lastMeta := info.BlockMetas[0]
  133. assert.EqualValues(apph, lastMeta.Header.Height)
  134. bMeta := block.BlockMeta
  135. assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash)
  136. assert.Equal(bMeta.BlockID, lastMeta.BlockID)
  137. }
  138. // and get the corresponding commit with the same apphash
  139. commit, err := c.Commit(apph)
  140. require.Nil(err, "%d: %+v", i, err)
  141. cappHash := commit.Header.AppHash
  142. assert.Equal(appHash, cappHash)
  143. assert.NotNil(commit.Commit)
  144. // compare the commits (note Commit(2) has commit from Block(3))
  145. commit2, err := c.Commit(apph - 1)
  146. require.Nil(err, "%d: %+v", i, err)
  147. assert.Equal(block.Block.LastCommit, commit2.Commit)
  148. // and we got a proof that works!
  149. pres, err := c.ABCIQuery("/key", k, true)
  150. if assert.Nil(err) && assert.True(pres.Code.IsOK()) {
  151. proof, err := iavl.ReadProof(pres.Proof)
  152. if assert.Nil(err) {
  153. key := pres.Key
  154. value := pres.Value
  155. assert.EqualValues(appHash, proof.RootHash)
  156. valid := proof.Verify(key, value, appHash)
  157. assert.True(valid)
  158. }
  159. }
  160. }
  161. }
  162. func TestBroadcastTxSync(t *testing.T) {
  163. require := require.New(t)
  164. mempool := node.MempoolReactor().Mempool
  165. initMempoolSize := mempool.Size()
  166. for i, c := range GetClients() {
  167. _, _, tx := merktest.MakeTxKV()
  168. bres, err := c.BroadcastTxSync(tx)
  169. require.Nil(err, "%d: %+v", i, err)
  170. require.True(bres.Code.IsOK())
  171. require.Equal(initMempoolSize+1, mempool.Size())
  172. txs := mempool.Reap(1)
  173. require.EqualValues(tx, txs[0])
  174. mempool.Flush()
  175. }
  176. }
  177. func TestBroadcastTxCommit(t *testing.T) {
  178. require := require.New(t)
  179. mempool := node.MempoolReactor().Mempool
  180. for i, c := range GetClients() {
  181. _, _, tx := merktest.MakeTxKV()
  182. bres, err := c.BroadcastTxCommit(tx)
  183. require.Nil(err, "%d: %+v", i, err)
  184. require.True(bres.CheckTx.Code.IsOK())
  185. require.True(bres.DeliverTx.Code.IsOK())
  186. require.Equal(0, mempool.Size())
  187. }
  188. }
  189. func TestTx(t *testing.T) {
  190. assert, require := assert.New(t), require.New(t)
  191. // first we broadcast a tx
  192. c := getHTTPClient()
  193. _, _, tx := merktest.MakeTxKV()
  194. bres, err := c.BroadcastTxCommit(tx)
  195. require.Nil(err, "%+v", err)
  196. txHeight := bres.Height
  197. txHash := bres.Hash
  198. anotherTxHash := types.Tx("a different tx").Hash()
  199. cases := []struct {
  200. valid bool
  201. hash []byte
  202. prove bool
  203. }{
  204. // only valid if correct hash provided
  205. {true, txHash, false},
  206. {true, txHash, true},
  207. {false, anotherTxHash, false},
  208. {false, anotherTxHash, true},
  209. {false, nil, false},
  210. {false, nil, true},
  211. }
  212. for i, c := range GetClients() {
  213. for j, tc := range cases {
  214. t.Logf("client %d, case %d", i, j)
  215. // now we query for the tx.
  216. // since there's only one tx, we know index=0.
  217. ptx, err := c.Tx(tc.hash, tc.prove)
  218. if !tc.valid {
  219. require.NotNil(err)
  220. } else {
  221. require.Nil(err, "%+v", err)
  222. assert.Equal(txHeight, ptx.Height)
  223. assert.EqualValues(tx, ptx.Tx)
  224. assert.Equal(0, ptx.Index)
  225. assert.True(ptx.TxResult.Code.IsOK())
  226. // time to verify the proof
  227. proof := ptx.Proof
  228. if tc.prove && assert.EqualValues(tx, proof.Data) {
  229. assert.True(proof.Proof.Verify(proof.Index, proof.Total, txHash, proof.RootHash))
  230. }
  231. }
  232. }
  233. }
  234. }