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
5.9 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. "github.com/tendermint/tendermint/rpc/client/http"
  11. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  12. rpctest "github.com/tendermint/tendermint/rpc/test"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. // GetClient gets a rpc client pointing to the test tendermint rpc
  16. func GetClient() *http.Client {
  17. rpcAddr := rpctest.GetConfig().GetString("rpc_laddr")
  18. return http.New(rpcAddr, "/websocket")
  19. }
  20. // Make sure status is correct (we connect properly)
  21. func TestStatus(t *testing.T) {
  22. c := GetClient()
  23. chainID := rpctest.GetConfig().GetString("chain_id")
  24. status, err := c.Status()
  25. require.Nil(t, err, "%+v", err)
  26. assert.Equal(t, chainID, status.NodeInfo.Network)
  27. }
  28. // Make sure info is correct (we connect properly)
  29. func TestInfo(t *testing.T) {
  30. c := GetClient()
  31. status, err := c.Status()
  32. require.Nil(t, err, "%+v", err)
  33. info, err := c.ABCIInfo()
  34. require.Nil(t, err, "%+v", err)
  35. assert.EqualValues(t, status.LatestBlockHeight, info.Response.LastBlockHeight)
  36. assert.True(t, strings.HasPrefix(info.Response.Data, "size"))
  37. }
  38. func TestNetInfo(t *testing.T) {
  39. c := GetClient()
  40. netinfo, err := c.NetInfo()
  41. require.Nil(t, err, "%+v", err)
  42. assert.True(t, netinfo.Listening)
  43. assert.Equal(t, 0, len(netinfo.Peers))
  44. }
  45. func TestDialSeeds(t *testing.T) {
  46. c := GetClient()
  47. // FIXME: fix server so it doesn't panic on invalid input
  48. _, err := c.DialSeeds([]string{"12.34.56.78:12345"})
  49. require.Nil(t, err, "%+v", err)
  50. }
  51. func TestGenesisAndValidators(t *testing.T) {
  52. c := GetClient()
  53. chainID := rpctest.GetConfig().GetString("chain_id")
  54. // make sure this is the right genesis file
  55. gen, err := c.Genesis()
  56. require.Nil(t, err, "%+v", err)
  57. assert.Equal(t, chainID, gen.Genesis.ChainID)
  58. // get the genesis validator
  59. require.Equal(t, 1, len(gen.Genesis.Validators))
  60. gval := gen.Genesis.Validators[0]
  61. // get the current validators
  62. vals, err := c.Validators()
  63. require.Nil(t, err, "%+v", err)
  64. require.Equal(t, 1, len(vals.Validators))
  65. val := vals.Validators[0]
  66. // make sure the current set is also the genesis set
  67. assert.Equal(t, gval.Amount, val.VotingPower)
  68. assert.Equal(t, gval.PubKey, val.PubKey)
  69. }
  70. // Make some app checks
  71. func TestAppCalls(t *testing.T) {
  72. assert, require := assert.New(t), require.New(t)
  73. c := GetClient()
  74. // get an offset of height to avoid racing and guessing
  75. s, err := c.Status()
  76. require.Nil(err)
  77. // sh is start height or status height
  78. sh := s.LatestBlockHeight
  79. // look for the future
  80. _, err = c.Block(sh + 2)
  81. assert.NotNil(err) // no block yet
  82. // write something
  83. k, v, tx := MakeTxKV()
  84. _, err = c.BroadcastTxCommit(tx)
  85. require.Nil(err, "%+v", err)
  86. // wait before querying
  87. time.Sleep(time.Second * 1)
  88. qres, err := c.ABCIQuery("/key", k, false)
  89. if assert.Nil(err) && assert.True(qres.Response.Code.IsOK()) {
  90. data := qres.Response
  91. // assert.Equal(k, data.GetKey()) // only returned for proofs
  92. assert.Equal(v, data.GetValue())
  93. }
  94. // +/- 1 making my head hurt
  95. h := int(qres.Response.Height) - 1
  96. // and we can even check the block is added
  97. block, err := c.Block(h)
  98. require.Nil(err, "%+v", err)
  99. appHash := block.BlockMeta.Header.AppHash
  100. assert.True(len(appHash) > 0)
  101. assert.EqualValues(h, block.BlockMeta.Header.Height)
  102. // check blockchain info, now that we know there is info
  103. // TODO: is this commented somewhere that they are returned
  104. // in order of descending height???
  105. info, err := c.BlockchainInfo(h-2, h)
  106. require.Nil(err, "%+v", err)
  107. assert.True(info.LastHeight > 2)
  108. if assert.Equal(3, len(info.BlockMetas)) {
  109. lastMeta := info.BlockMetas[0]
  110. assert.EqualValues(h, lastMeta.Header.Height)
  111. bMeta := block.BlockMeta
  112. assert.Equal(bMeta.Header.AppHash, lastMeta.Header.AppHash)
  113. assert.Equal(bMeta.BlockID, lastMeta.BlockID)
  114. }
  115. // and get the corresponding commit with the same apphash
  116. commit, err := c.Commit(h)
  117. require.Nil(err, "%+v", err)
  118. cappHash := commit.Header.AppHash
  119. assert.Equal(appHash, cappHash)
  120. assert.NotNil(commit.Commit)
  121. // compare the commits (note Commit(2) has commit from Block(3))
  122. commit2, err := c.Commit(h - 1)
  123. require.Nil(err, "%+v", err)
  124. assert.Equal(block.Block.LastCommit, commit2.Commit)
  125. // and we got a proof that works!
  126. pres, err := c.ABCIQuery("/key", k, true)
  127. if assert.Nil(err) && assert.True(pres.Response.Code.IsOK()) {
  128. proof, err := merkle.ReadProof(pres.Response.GetProof())
  129. if assert.Nil(err) {
  130. key := pres.Response.GetKey()
  131. value := pres.Response.GetValue()
  132. assert.Equal(appHash, proof.RootHash)
  133. valid := proof.Verify(key, value, appHash)
  134. assert.True(valid)
  135. }
  136. }
  137. }
  138. func TestSubscriptions(t *testing.T) {
  139. require := require.New(t)
  140. c := GetClient()
  141. err := c.StartWebsocket()
  142. require.Nil(err)
  143. defer c.StopWebsocket()
  144. // subscribe to a transaction event
  145. _, _, tx := MakeTxKV()
  146. // this causes a panic in tendermint core!!!
  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. }