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.

186 lines
5.5 KiB

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. )
  12. func getHTTPClient() *client.HTTP {
  13. rpcAddr := rpctest.GetConfig().GetString("rpc_laddr")
  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. chainID := rpctest.GetConfig().GetString("chain_id")
  30. status, err := c.Status()
  31. require.Nil(t, err, "%d: %+v", i, err)
  32. assert.Equal(t, chainID, status.NodeInfo.Network)
  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.HasPrefix(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. chainID := rpctest.GetConfig().GetString("chain_id")
  71. // make sure this is the right genesis file
  72. gen, err := c.Genesis()
  73. require.Nil(t, err, "%d: %+v", i, err)
  74. assert.Equal(t, chainID, gen.Genesis.ChainID)
  75. // get the genesis validator
  76. require.Equal(t, 1, len(gen.Genesis.Validators))
  77. gval := gen.Genesis.Validators[0]
  78. // get the current validators
  79. vals, err := c.Validators()
  80. require.Nil(t, err, "%d: %+v", i, err)
  81. require.Equal(t, 1, len(vals.Validators))
  82. val := vals.Validators[0]
  83. // make sure the current set is also the genesis set
  84. assert.Equal(t, gval.Amount, val.VotingPower)
  85. assert.Equal(t, gval.PubKey, val.PubKey)
  86. }
  87. }
  88. // Make some app checks
  89. func TestAppCalls(t *testing.T) {
  90. assert, require := assert.New(t), require.New(t)
  91. for i, c := range GetClients() {
  92. // get an offset of height to avoid racing and guessing
  93. s, err := c.Status()
  94. require.Nil(err, "%d: %+v", i, err)
  95. // sh is start height or status height
  96. sh := s.LatestBlockHeight
  97. // look for the future
  98. _, err = c.Block(sh + 2)
  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. commit2, err := c.Commit(apph - 1)
  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. }