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.

109 lines
2.9 KiB

  1. package e2e_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/rand"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. // Tests that any initial state given in genesis has made it into the app.
  14. func TestApp_InitialState(t *testing.T) {
  15. testNode(t, func(t *testing.T, node e2e.Node) {
  16. if node.Stateless() {
  17. return
  18. }
  19. if len(node.Testnet.InitialState) == 0 {
  20. return
  21. }
  22. client, err := node.Client()
  23. require.NoError(t, err)
  24. for k, v := range node.Testnet.InitialState {
  25. resp, err := client.ABCIQuery(ctx, "", []byte(k))
  26. require.NoError(t, err)
  27. assert.Equal(t, k, string(resp.Response.Key))
  28. assert.Equal(t, v, string(resp.Response.Value))
  29. }
  30. })
  31. }
  32. // Tests that the app hash (as reported by the app) matches the last
  33. // block and the node sync status.
  34. func TestApp_Hash(t *testing.T) {
  35. testNode(t, func(t *testing.T, node e2e.Node) {
  36. if node.Mode == e2e.ModeSeed {
  37. return
  38. }
  39. client, err := node.Client()
  40. require.NoError(t, err)
  41. info, err := client.ABCIInfo(ctx)
  42. require.NoError(t, err)
  43. require.NotEmpty(t, info.Response.LastBlockAppHash, "expected app to return app hash")
  44. block, err := client.Block(ctx, nil)
  45. require.NoError(t, err)
  46. require.EqualValues(t, info.Response.LastBlockAppHash, block.Block.AppHash,
  47. "app hash does not match last block's app hash")
  48. status, err := client.Status(ctx)
  49. require.NoError(t, err)
  50. require.EqualValues(t, info.Response.LastBlockAppHash, status.SyncInfo.LatestAppHash,
  51. "app hash does not match node status")
  52. })
  53. }
  54. // Tests that we can set a value and retrieve it.
  55. func TestApp_Tx(t *testing.T) {
  56. testNode(t, func(t *testing.T, node e2e.Node) {
  57. if node.Mode == e2e.ModeSeed {
  58. return
  59. }
  60. client, err := node.Client()
  61. require.NoError(t, err)
  62. // Generate a random value, to prevent duplicate tx errors when
  63. // manually running the test multiple times for a testnet.
  64. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  65. bz := make([]byte, 32)
  66. _, err = r.Read(bz)
  67. require.NoError(t, err)
  68. key := fmt.Sprintf("testapp-tx-%v", node.Name)
  69. value := fmt.Sprintf("%x", bz)
  70. tx := types.Tx(fmt.Sprintf("%v=%v", key, value))
  71. _, err = client.BroadcastTxSync(ctx, tx)
  72. require.NoError(t, err)
  73. hash := tx.Hash()
  74. waitTime := 20 * time.Second
  75. require.Eventuallyf(t, func() bool {
  76. txResp, err := client.Tx(ctx, hash, false)
  77. return err == nil && bytes.Equal(txResp.Tx, tx)
  78. }, waitTime, time.Second,
  79. "submitted tx %X wasn't committed after %v", hash, waitTime,
  80. )
  81. // NOTE: we don't test abci query of the light client
  82. if node.Mode == e2e.ModeLight {
  83. return
  84. }
  85. abciResp, err := client.ABCIQuery(ctx, "", []byte(key))
  86. require.NoError(t, err)
  87. assert.Equal(t, key, string(abciResp.Response.Key))
  88. assert.Equal(t, value, string(abciResp.Response.Value))
  89. })
  90. }