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.

179 lines
5.3 KiB

  1. package client_test
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. merkle "github.com/tendermint/go-merkle"
  9. merktest "github.com/tendermint/merkleeyes/testutil"
  10. "github.com/tendermint/tendermint/rpc/client"
  11. rpctest "github.com/tendermint/tendermint/rpc/test"
  12. )
  13. func getHTTPClient() *client.HTTP {
  14. rpcAddr := rpctest.GetConfig().GetString("rpc_laddr")
  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. chainID := rpctest.GetConfig().GetString("chain_id")
  31. status, err := c.Status()
  32. require.Nil(t, err, "%d: %+v", i, err)
  33. assert.Equal(t, chainID, status.NodeInfo.Network)
  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. chainID := rpctest.GetConfig().GetString("chain_id")
  72. // make sure this is the right genesis file
  73. gen, err := c.Genesis()
  74. require.Nil(t, err, "%d: %+v", i, err)
  75. assert.Equal(t, chainID, gen.Genesis.ChainID)
  76. // get the genesis validator
  77. require.Equal(t, 1, len(gen.Genesis.Validators))
  78. gval := gen.Genesis.Validators[0]
  79. // get the current validators
  80. vals, err := c.Validators()
  81. require.Nil(t, err, "%d: %+v", i, err)
  82. require.Equal(t, 1, len(vals.Validators))
  83. val := vals.Validators[0]
  84. // make sure the current set is also the genesis set
  85. assert.Equal(t, gval.Amount, val.VotingPower)
  86. assert.Equal(t, gval.PubKey, val.PubKey)
  87. }
  88. }
  89. // Make some app checks
  90. func TestAppCalls(t *testing.T) {
  91. assert, require := assert.New(t), require.New(t)
  92. for i, c := range GetClients() {
  93. // get an offset of height to avoid racing and guessing
  94. s, err := c.Status()
  95. require.Nil(err, "%d: %+v", i, err)
  96. // sh is start height or status height
  97. sh := s.LatestBlockHeight
  98. // look for the future
  99. _, err = c.Block(sh + 2)
  100. assert.NotNil(err) // no block yet
  101. // write something
  102. k, v, tx := merktest.MakeTxKV()
  103. _, err = c.BroadcastTxCommit(tx)
  104. require.Nil(err, "%d: %+v", i, err)
  105. // wait before querying
  106. time.Sleep(time.Second * 1)
  107. qres, err := c.ABCIQuery("/key", k, false)
  108. if assert.Nil(err) && assert.True(qres.Response.Code.IsOK()) {
  109. data := qres.Response
  110. // assert.Equal(k, data.GetKey()) // only returned for proofs
  111. assert.Equal(v, data.GetValue())
  112. }
  113. // +/- 1 making my head hurt
  114. h := int(qres.Response.Height) - 1
  115. // and we can even check the block is added
  116. block, err := c.Block(h)
  117. require.Nil(err, "%d: %+v", i, err)
  118. appHash := block.BlockMeta.Header.AppHash
  119. assert.True(len(appHash) > 0)
  120. assert.EqualValues(h, block.BlockMeta.Header.Height)
  121. // check blockchain info, now that we know there is info
  122. // TODO: is this commented somewhere that they are returned
  123. // in order of descending height???
  124. info, err := c.BlockchainInfo(h-2, h)
  125. require.Nil(err, "%d: %+v", i, err)
  126. assert.True(info.LastHeight > 2)
  127. if assert.Equal(3, len(info.BlockMetas)) {
  128. lastMeta := info.BlockMetas[0]
  129. assert.EqualValues(h, lastMeta.Header.Height)
  130. bMeta := block.BlockMeta
  131. assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash)
  132. assert.Equal(bMeta.BlockID, lastMeta.BlockID)
  133. }
  134. // and get the corresponding commit with the same apphash
  135. commit, err := c.Commit(h)
  136. require.Nil(err, "%d: %+v", i, err)
  137. cappHash := commit.Header.AppHash
  138. assert.Equal(appHash, cappHash)
  139. assert.NotNil(commit.Commit)
  140. // compare the commits (note Commit(2) has commit from Block(3))
  141. commit2, err := c.Commit(h - 1)
  142. require.Nil(err, "%d: %+v", i, err)
  143. assert.Equal(block.Block.LastCommit, commit2.Commit)
  144. // and we got a proof that works!
  145. pres, err := c.ABCIQuery("/key", k, true)
  146. if assert.Nil(err) && assert.True(pres.Response.Code.IsOK()) {
  147. proof, err := merkle.ReadProof(pres.Response.GetProof())
  148. if assert.Nil(err) {
  149. key := pres.Response.GetKey()
  150. value := pres.Response.GetValue()
  151. assert.Equal(appHash, proof.RootHash)
  152. valid := proof.Verify(key, value, appHash)
  153. assert.True(valid)
  154. }
  155. }
  156. }
  157. }