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.

188 lines
5.7 KiB

  1. package client_test
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. merkle "github.com/tendermint/go-merkle"
  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().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. bres, err := c.BroadcastTxCommit(tx)
  104. require.Nil(err, "%d: %+v", i, err)
  105. require.True(bres.DeliverTx.GetCode().IsOK())
  106. txh := bres.Height
  107. apph := txh + 1 // this is where the tx will be applied to the state
  108. // wait before querying
  109. client.WaitForHeight(c, apph, nil)
  110. qres, err := c.ABCIQuery("/key", k, false)
  111. if assert.Nil(err) && assert.True(qres.Response.Code.IsOK()) {
  112. data := qres.Response
  113. // assert.Equal(k, data.GetKey()) // only returned for proofs
  114. assert.Equal(v, data.GetValue())
  115. }
  116. // make sure we can lookup the tx with proof
  117. // ptx, err := c.Tx(bres.Hash, true)
  118. ptx, err := c.Tx(bres.Hash, true)
  119. require.Nil(err, "%d: %+v", i, err)
  120. assert.Equal(txh, ptx.Height)
  121. assert.Equal(types.Tx(tx), ptx.Tx)
  122. // and we can even check the block is added
  123. block, err := c.Block(apph)
  124. require.Nil(err, "%d: %+v", i, err)
  125. appHash := block.BlockMeta.Header.AppHash
  126. assert.True(len(appHash) > 0)
  127. assert.EqualValues(apph, block.BlockMeta.Header.Height)
  128. // check blockchain info, now that we know there is info
  129. // TODO: is this commented somewhere that they are returned
  130. // in order of descending height???
  131. info, err := c.BlockchainInfo(apph, apph)
  132. require.Nil(err, "%d: %+v", i, err)
  133. assert.True(info.LastHeight >= apph)
  134. if assert.Equal(1, len(info.BlockMetas)) {
  135. lastMeta := info.BlockMetas[0]
  136. assert.EqualValues(apph, lastMeta.Header.Height)
  137. bMeta := block.BlockMeta
  138. assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash)
  139. assert.Equal(bMeta.BlockID, lastMeta.BlockID)
  140. }
  141. // and get the corresponding commit with the same apphash
  142. commit, err := c.Commit(apph)
  143. require.Nil(err, "%d: %+v", i, err)
  144. cappHash := commit.Header.AppHash
  145. assert.Equal(appHash, cappHash)
  146. assert.NotNil(commit.Commit)
  147. // compare the commits (note Commit(2) has commit from Block(3))
  148. commit2, err := c.Commit(apph - 1)
  149. require.Nil(err, "%d: %+v", i, err)
  150. assert.Equal(block.Block.LastCommit, commit2.Commit)
  151. // and we got a proof that works!
  152. pres, err := c.ABCIQuery("/key", k, true)
  153. if assert.Nil(err) && assert.True(pres.Response.Code.IsOK()) {
  154. proof, err := merkle.ReadProof(pres.Response.GetProof())
  155. if assert.Nil(err) {
  156. key := pres.Response.GetKey()
  157. value := pres.Response.GetValue()
  158. assert.Equal(appHash, proof.RootHash)
  159. valid := proof.Verify(key, value, appHash)
  160. assert.True(valid)
  161. }
  162. }
  163. }
  164. }