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.

211 lines
6.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package http_test
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. merkle "github.com/tendermint/go-merkle"
  10. merktest "github.com/tendermint/merkleeyes/testutil"
  11. "github.com/tendermint/tendermint/rpc/client/http"
  12. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  13. rpctest "github.com/tendermint/tendermint/rpc/test"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. // GetClient gets a rpc client pointing to the test tendermint rpc
  17. func GetClient() *http.Client {
  18. rpcAddr := rpctest.GetConfig().GetString("rpc_laddr")
  19. return http.New(rpcAddr, "/websocket")
  20. }
  21. // Make sure status is correct (we connect properly)
  22. func TestStatus(t *testing.T) {
  23. c := GetClient()
  24. chainID := rpctest.GetConfig().GetString("chain_id")
  25. status, err := c.Status()
  26. require.Nil(t, err, "%+v", err)
  27. assert.Equal(t, chainID, status.NodeInfo.Network)
  28. }
  29. // Make sure info is correct (we connect properly)
  30. func TestInfo(t *testing.T) {
  31. c := GetClient()
  32. status, err := c.Status()
  33. require.Nil(t, err, "%+v", err)
  34. info, err := c.ABCIInfo()
  35. require.Nil(t, err, "%+v", err)
  36. assert.EqualValues(t, status.LatestBlockHeight, info.Response.LastBlockHeight)
  37. assert.True(t, strings.HasPrefix(info.Response.Data, "size"))
  38. }
  39. func TestNetInfo(t *testing.T) {
  40. c := GetClient()
  41. netinfo, err := c.NetInfo()
  42. require.Nil(t, err, "%+v", err)
  43. assert.True(t, netinfo.Listening)
  44. assert.Equal(t, 0, len(netinfo.Peers))
  45. }
  46. func TestDialSeeds(t *testing.T) {
  47. c := GetClient()
  48. // FIXME: fix server so it doesn't panic on invalid input
  49. _, err := c.DialSeeds([]string{"12.34.56.78:12345"})
  50. require.Nil(t, err, "%+v", err)
  51. }
  52. func TestGenesisAndValidators(t *testing.T) {
  53. c := GetClient()
  54. chainID := rpctest.GetConfig().GetString("chain_id")
  55. // make sure this is the right genesis file
  56. gen, err := c.Genesis()
  57. require.Nil(t, err, "%+v", err)
  58. assert.Equal(t, chainID, gen.Genesis.ChainID)
  59. // get the genesis validator
  60. require.Equal(t, 1, len(gen.Genesis.Validators))
  61. gval := gen.Genesis.Validators[0]
  62. // get the current validators
  63. vals, err := c.Validators()
  64. require.Nil(t, err, "%+v", err)
  65. require.Equal(t, 1, len(vals.Validators))
  66. val := vals.Validators[0]
  67. // make sure the current set is also the genesis set
  68. assert.Equal(t, gval.Amount, val.VotingPower)
  69. assert.Equal(t, gval.PubKey, val.PubKey)
  70. }
  71. // Make some app checks
  72. func TestAppCalls(t *testing.T) {
  73. assert, require := assert.New(t), require.New(t)
  74. c := GetClient()
  75. // get an offset of height to avoid racing and guessing
  76. s, err := c.Status()
  77. require.Nil(err)
  78. // sh is start height or status height
  79. sh := s.LatestBlockHeight
  80. // look for the future
  81. _, err = c.Block(sh + 2)
  82. assert.NotNil(err) // no block yet
  83. // write something
  84. k, v, tx := merktest.MakeTxKV()
  85. _, err = c.BroadcastTxCommit(tx)
  86. require.Nil(err, "%+v", err)
  87. // wait before querying
  88. time.Sleep(time.Second * 1)
  89. qres, err := c.ABCIQuery("/key", k, false)
  90. if assert.Nil(err) && assert.True(qres.Response.Code.IsOK()) {
  91. data := qres.Response
  92. // assert.Equal(k, data.GetKey()) // only returned for proofs
  93. assert.Equal(v, data.GetValue())
  94. }
  95. // +/- 1 making my head hurt
  96. h := int(qres.Response.Height) - 1
  97. // and we can even check the block is added
  98. block, err := c.Block(h)
  99. require.Nil(err, "%+v", err)
  100. appHash := block.BlockMeta.Header.AppHash
  101. assert.True(len(appHash) > 0)
  102. assert.EqualValues(h, block.BlockMeta.Header.Height)
  103. // check blockchain info, now that we know there is info
  104. // TODO: is this commented somewhere that they are returned
  105. // in order of descending height???
  106. info, err := c.BlockchainInfo(h-2, h)
  107. require.Nil(err, "%+v", err)
  108. assert.True(info.LastHeight > 2)
  109. if assert.Equal(3, len(info.BlockMetas)) {
  110. lastMeta := info.BlockMetas[0]
  111. assert.EqualValues(h, lastMeta.Header.Height)
  112. bMeta := block.BlockMeta
  113. assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash)
  114. assert.Equal(bMeta.BlockID, lastMeta.BlockID)
  115. }
  116. // and get the corresponding commit with the same apphash
  117. commit, err := c.Commit(h)
  118. require.Nil(err, "%+v", err)
  119. cappHash := commit.Header.AppHash
  120. assert.Equal(appHash, cappHash)
  121. assert.NotNil(commit.Commit)
  122. // compare the commits (note Commit(2) has commit from Block(3))
  123. commit2, err := c.Commit(h - 1)
  124. require.Nil(err, "%+v", err)
  125. assert.Equal(block.Block.LastCommit, commit2.Commit)
  126. // and we got a proof that works!
  127. pres, err := c.ABCIQuery("/key", k, true)
  128. if assert.Nil(err) && assert.True(pres.Response.Code.IsOK()) {
  129. proof, err := merkle.ReadProof(pres.Response.GetProof())
  130. if assert.Nil(err) {
  131. key := pres.Response.GetKey()
  132. value := pres.Response.GetValue()
  133. assert.Equal(appHash, proof.RootHash)
  134. valid := proof.Verify(key, value, appHash)
  135. assert.True(valid)
  136. }
  137. }
  138. }
  139. func TestSubscriptions(t *testing.T) {
  140. require := require.New(t)
  141. c := GetClient()
  142. err := c.StartWebsocket()
  143. require.Nil(err)
  144. defer c.StopWebsocket()
  145. // subscribe to a transaction event
  146. _, _, tx := merktest.MakeTxKV()
  147. eventType := types.EventStringTx(types.Tx(tx))
  148. c.Subscribe(eventType)
  149. // set up a listener
  150. r, e := c.GetEventChannels()
  151. go func() {
  152. // send a tx and wait for it to propogate
  153. _, err = c.BroadcastTxCommit(tx)
  154. require.Nil(err, string(tx))
  155. }()
  156. checkData := func(data []byte, kind byte) {
  157. x := []interface{}{}
  158. err := json.Unmarshal(data, &x)
  159. require.Nil(err)
  160. // gotta love wire's json format
  161. require.EqualValues(kind, x[0])
  162. }
  163. res := <-r
  164. checkData(res, ctypes.ResultTypeSubscribe)
  165. // read one event, must be success
  166. select {
  167. case res := <-r:
  168. checkData(res, ctypes.ResultTypeEvent)
  169. // this is good.. let's get the data... ugh...
  170. // result := new(ctypes.TMResult)
  171. // wire.ReadJSON(result, res, &err)
  172. // require.Nil(err, "%+v", err)
  173. // event, ok := (*result).(*ctypes.ResultEvent)
  174. // require.True(ok)
  175. // assert.Equal("foo", event.Name)
  176. // data, ok := event.Data.(types.EventDataTx)
  177. // require.True(ok)
  178. // assert.EqualValues(0, data.Code)
  179. // assert.EqualValues(tx, data.Tx)
  180. case err := <-e:
  181. // this is a failure
  182. require.Nil(err)
  183. }
  184. }