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.

184 lines
5.4 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. )
  12. func getHTTPClient() *client.HTTP {
  13. rpcAddr := rpctest.GetConfig().RPCListenAddress
  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.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. // 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()
  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.Amount, 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. _, err = c.Block(sh + 2)
  97. assert.NotNil(err) // no block yet
  98. // write something
  99. k, v, tx := merktest.MakeTxKV()
  100. bres, err := c.BroadcastTxCommit(tx)
  101. require.Nil(err, "%d: %+v", i, err)
  102. require.True(bres.DeliverTx.Code.IsOK())
  103. txh := bres.Height
  104. apph := txh + 1 // this is where the tx will be applied to the state
  105. // wait before querying
  106. client.WaitForHeight(c, apph, nil)
  107. qres, err := c.ABCIQuery("/key", k, false)
  108. if assert.Nil(err) && assert.True(qres.Code.IsOK()) {
  109. // assert.Equal(k, data.GetKey()) // only returned for proofs
  110. assert.EqualValues(v, qres.Value)
  111. }
  112. // make sure we can lookup the tx with proof
  113. // ptx, err := c.Tx(bres.Hash, true)
  114. ptx, err := c.Tx(bres.Hash, true)
  115. require.Nil(err, "%d: %+v", i, err)
  116. assert.Equal(txh, ptx.Height)
  117. assert.EqualValues(tx, ptx.Tx)
  118. // and we can even check the block is added
  119. block, err := c.Block(apph)
  120. require.Nil(err, "%d: %+v", i, err)
  121. appHash := block.BlockMeta.Header.AppHash
  122. assert.True(len(appHash) > 0)
  123. assert.EqualValues(apph, block.BlockMeta.Header.Height)
  124. // check blockchain info, now that we know there is info
  125. // TODO: is this commented somewhere that they are returned
  126. // in order of descending height???
  127. info, err := c.BlockchainInfo(apph, apph)
  128. require.Nil(err, "%d: %+v", i, err)
  129. assert.True(info.LastHeight >= apph)
  130. if assert.Equal(1, len(info.BlockMetas)) {
  131. lastMeta := info.BlockMetas[0]
  132. assert.EqualValues(apph, lastMeta.Header.Height)
  133. bMeta := block.BlockMeta
  134. assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash)
  135. assert.Equal(bMeta.BlockID, lastMeta.BlockID)
  136. }
  137. // and get the corresponding commit with the same apphash
  138. commit, err := c.Commit(apph)
  139. require.Nil(err, "%d: %+v", i, err)
  140. cappHash := commit.Header.AppHash
  141. assert.Equal(appHash, cappHash)
  142. assert.NotNil(commit.Commit)
  143. // compare the commits (note Commit(2) has commit from Block(3))
  144. commit2, err := c.Commit(apph - 1)
  145. require.Nil(err, "%d: %+v", i, err)
  146. assert.Equal(block.Block.LastCommit, commit2.Commit)
  147. // and we got a proof that works!
  148. pres, err := c.ABCIQuery("/key", k, true)
  149. if assert.Nil(err) && assert.True(pres.Code.IsOK()) {
  150. proof, err := iavl.ReadProof(pres.Proof)
  151. if assert.Nil(err) {
  152. key := pres.Key
  153. value := pres.Value
  154. assert.EqualValues(appHash, proof.RootHash)
  155. valid := proof.Verify(key, value, appHash)
  156. assert.True(valid)
  157. }
  158. }
  159. }
  160. }