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.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" //TODO use tendermint/iavl ?
  8. "github.com/tendermint/tendermint/rpc/client"
  9. rpctest "github.com/tendermint/tendermint/rpc/test"
  10. "github.com/tendermint/tendermint/types"
  11. )
  12. func getHTTPClient() *client.HTTP {
  13. rpcAddr := rpctest.GetConfig().RPC.ListenAddress
  14. return client.NewHTTP(rpcAddr, "/websocket")
  15. }
  16. func getLocalClient() client.Local {
  17. return client.NewLocal(node)
  18. }
  19. // GetClients returns a slice of clients for table-driven tests
  20. func GetClients() []client.Client {
  21. return []client.Client{
  22. getHTTPClient(),
  23. getLocalClient(),
  24. }
  25. }
  26. // Make sure status is correct (we connect properly)
  27. func TestStatus(t *testing.T) {
  28. for i, c := range GetClients() {
  29. moniker := rpctest.GetConfig().Moniker
  30. status, err := c.Status()
  31. require.Nil(t, err, "%d: %+v", i, err)
  32. assert.Equal(t, moniker, status.NodeInfo.Moniker)
  33. }
  34. }
  35. // Make sure info is correct (we connect properly)
  36. func TestInfo(t *testing.T) {
  37. for i, c := range GetClients() {
  38. // status, err := c.Status()
  39. // require.Nil(t, err, "%+v", err)
  40. info, err := c.ABCIInfo()
  41. require.Nil(t, err, "%d: %+v", i, err)
  42. // TODO: this is not correct - fix merkleeyes!
  43. // assert.EqualValues(t, status.LatestBlockHeight, info.Response.LastBlockHeight)
  44. assert.True(t, strings.Contains(info.Response.Data, "size"))
  45. }
  46. }
  47. func TestNetInfo(t *testing.T) {
  48. for i, c := range GetClients() {
  49. nc, ok := c.(client.NetworkClient)
  50. require.True(t, ok, "%d", i)
  51. netinfo, err := nc.NetInfo()
  52. require.Nil(t, err, "%d: %+v", i, err)
  53. assert.True(t, netinfo.Listening)
  54. assert.Equal(t, 0, len(netinfo.Peers))
  55. }
  56. }
  57. func TestDumpConsensusState(t *testing.T) {
  58. for i, c := range GetClients() {
  59. // FIXME: fix server so it doesn't panic on invalid input
  60. nc, ok := c.(client.NetworkClient)
  61. require.True(t, ok, "%d", i)
  62. cons, err := nc.DumpConsensusState()
  63. require.Nil(t, err, "%d: %+v", i, err)
  64. assert.NotEmpty(t, cons.RoundState)
  65. assert.Empty(t, cons.PeerRoundStates)
  66. }
  67. }
  68. func TestGenesisAndValidators(t *testing.T) {
  69. for i, c := range GetClients() {
  70. // make sure this is the right genesis file
  71. gen, err := c.Genesis()
  72. require.Nil(t, err, "%d: %+v", i, err)
  73. // get the genesis validator
  74. require.Equal(t, 1, len(gen.Genesis.Validators))
  75. gval := gen.Genesis.Validators[0]
  76. // get the current validators
  77. vals, err := c.Validators(nil)
  78. require.Nil(t, err, "%d: %+v", i, err)
  79. require.Equal(t, 1, len(vals.Validators))
  80. val := vals.Validators[0]
  81. // make sure the current set is also the genesis set
  82. assert.Equal(t, gval.Power, val.VotingPower)
  83. assert.Equal(t, gval.PubKey, val.PubKey)
  84. }
  85. }
  86. // Make some app checks
  87. func TestAppCalls(t *testing.T) {
  88. assert, require := assert.New(t), require.New(t)
  89. for i, c := range GetClients() {
  90. // get an offset of height to avoid racing and guessing
  91. s, err := c.Status()
  92. require.Nil(err, "%d: %+v", i, err)
  93. // sh is start height or status height
  94. sh := s.LatestBlockHeight
  95. // look for the future
  96. h := sh + 2
  97. _, err = c.Block(&h)
  98. assert.NotNil(err) // no block yet
  99. // write something
  100. k, v, tx := 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. h = apph - 1
  146. commit2, err := c.Commit(&h)
  147. require.Nil(err, "%d: %+v", i, err)
  148. assert.Equal(block.Block.LastCommit, commit2.Commit)
  149. // and we got a proof that works!
  150. pres, err := c.ABCIQuery("/key", k, true)
  151. if assert.Nil(err) && assert.True(pres.Code.IsOK()) {
  152. proof, err := iavl.ReadProof(pres.Proof)
  153. if assert.Nil(err) {
  154. key := pres.Key
  155. value := pres.Value
  156. assert.EqualValues(appHash, proof.RootHash)
  157. valid := proof.Verify(key, value, appHash)
  158. assert.True(valid)
  159. }
  160. }
  161. }
  162. }
  163. func TestBroadcastTxSync(t *testing.T) {
  164. require := require.New(t)
  165. mempool := node.MempoolReactor().Mempool
  166. initMempoolSize := mempool.Size()
  167. for i, c := range GetClients() {
  168. _, _, tx := MakeTxKV()
  169. bres, err := c.BroadcastTxSync(tx)
  170. require.Nil(err, "%d: %+v", i, err)
  171. require.True(bres.Code.IsOK())
  172. require.Equal(initMempoolSize+1, mempool.Size())
  173. txs := mempool.Reap(1)
  174. require.EqualValues(tx, txs[0])
  175. mempool.Flush()
  176. }
  177. }
  178. func TestBroadcastTxCommit(t *testing.T) {
  179. require := require.New(t)
  180. mempool := node.MempoolReactor().Mempool
  181. for i, c := range GetClients() {
  182. _, _, tx := MakeTxKV()
  183. bres, err := c.BroadcastTxCommit(tx)
  184. require.Nil(err, "%d: %+v", i, err)
  185. require.True(bres.CheckTx.Code.IsOK())
  186. require.True(bres.DeliverTx.Code.IsOK())
  187. require.Equal(0, mempool.Size())
  188. }
  189. }
  190. func TestTx(t *testing.T) {
  191. assert, require := assert.New(t), require.New(t)
  192. // first we broadcast a tx
  193. c := getHTTPClient()
  194. _, _, tx := MakeTxKV()
  195. bres, err := c.BroadcastTxCommit(tx)
  196. require.Nil(err, "%+v", err)
  197. txHeight := bres.Height
  198. txHash := bres.Hash
  199. anotherTxHash := types.Tx("a different tx").Hash()
  200. cases := []struct {
  201. valid bool
  202. hash []byte
  203. prove bool
  204. }{
  205. // only valid if correct hash provided
  206. {true, txHash, false},
  207. {true, txHash, true},
  208. {false, anotherTxHash, false},
  209. {false, anotherTxHash, true},
  210. {false, nil, false},
  211. {false, nil, true},
  212. }
  213. for i, c := range GetClients() {
  214. for j, tc := range cases {
  215. t.Logf("client %d, case %d", i, j)
  216. // now we query for the tx.
  217. // since there's only one tx, we know index=0.
  218. ptx, err := c.Tx(tc.hash, tc.prove)
  219. if !tc.valid {
  220. require.NotNil(err)
  221. } else {
  222. require.Nil(err, "%+v", err)
  223. assert.Equal(txHeight, ptx.Height)
  224. assert.EqualValues(tx, ptx.Tx)
  225. assert.Equal(0, ptx.Index)
  226. assert.True(ptx.TxResult.Code.IsOK())
  227. // time to verify the proof
  228. proof := ptx.Proof
  229. if tc.prove && assert.EqualValues(tx, proof.Data) {
  230. assert.True(proof.Proof.Verify(proof.Index, proof.Total, txHash, proof.RootHash))
  231. }
  232. }
  233. }
  234. }
  235. }