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.

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