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.

148 lines
3.9 KiB

  1. package e2e_test
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "math/rand"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "github.com/tendermint/tendermint/rpc/client/http"
  12. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. // Tests that any initial state given in genesis has made it into the app.
  16. func TestApp_InitialState(t *testing.T) {
  17. testNode(t, func(t *testing.T, node e2e.Node) {
  18. if len(node.Testnet.InitialState) == 0 {
  19. return
  20. }
  21. client, err := node.Client()
  22. require.NoError(t, err)
  23. for k, v := range node.Testnet.InitialState {
  24. resp, err := client.ABCIQuery(ctx, "", []byte(k))
  25. require.NoError(t, err)
  26. assert.Equal(t, k, string(resp.Response.Key))
  27. assert.Equal(t, v, string(resp.Response.Value))
  28. }
  29. })
  30. }
  31. // Tests that the app hash (as reported by the app) matches the last
  32. // block and the node sync status.
  33. func TestApp_Hash(t *testing.T) {
  34. testNode(t, func(t *testing.T, node e2e.Node) {
  35. client, err := node.Client()
  36. require.NoError(t, err)
  37. info, err := client.ABCIInfo(ctx)
  38. require.NoError(t, err)
  39. require.NotEmpty(t, info.Response.LastBlockAppHash, "expected app to return app hash")
  40. block, err := client.Block(ctx, nil)
  41. require.NoError(t, err)
  42. require.EqualValues(t, info.Response.LastBlockAppHash, block.Block.AppHash.Bytes(),
  43. "app hash does not match last block's app hash")
  44. status, err := client.Status(ctx)
  45. require.NoError(t, err)
  46. require.EqualValues(t, info.Response.LastBlockAppHash, status.SyncInfo.LatestAppHash,
  47. "app hash does not match node status")
  48. })
  49. }
  50. // Tests that we can set a value and retrieve it.
  51. func TestApp_Tx(t *testing.T) {
  52. type broadcastFunc func(context.Context, types.Tx) error
  53. testCases := []struct {
  54. Name string
  55. WaitTime time.Duration
  56. BroadcastTx func(client *http.HTTP) broadcastFunc
  57. ShouldSkip bool
  58. }{
  59. {
  60. Name: "Sync",
  61. WaitTime: 30 * time.Second,
  62. BroadcastTx: func(client *http.HTTP) broadcastFunc {
  63. return func(ctx context.Context, tx types.Tx) error {
  64. _, err := client.BroadcastTxSync(ctx, tx)
  65. return err
  66. }
  67. },
  68. },
  69. {
  70. Name: "Commit",
  71. WaitTime: time.Minute,
  72. BroadcastTx: func(client *http.HTTP) broadcastFunc {
  73. return func(ctx context.Context, tx types.Tx) error {
  74. _, err := client.BroadcastTxCommit(ctx, tx)
  75. return err
  76. }
  77. },
  78. },
  79. {
  80. Name: "Async",
  81. WaitTime: time.Minute,
  82. ShouldSkip: true,
  83. BroadcastTx: func(client *http.HTTP) broadcastFunc {
  84. return func(ctx context.Context, tx types.Tx) error {
  85. _, err := client.BroadcastTxAsync(ctx, tx)
  86. return err
  87. }
  88. },
  89. },
  90. }
  91. for idx, test := range testCases {
  92. if test.ShouldSkip {
  93. continue
  94. }
  95. t.Run(test.Name, func(t *testing.T) {
  96. // testNode calls t.Parallel as well, so we should
  97. // have a copy of the
  98. test := testCases[idx]
  99. testNode(t, func(t *testing.T, node e2e.Node) {
  100. client, err := node.Client()
  101. require.NoError(t, err)
  102. // Generate a random value, to prevent duplicate tx errors when
  103. // manually running the test multiple times for a testnet.
  104. bz := make([]byte, 32)
  105. _, err = rand.Read(bz)
  106. require.NoError(t, err)
  107. key := fmt.Sprintf("testapp-tx-%v", node.Name)
  108. value := fmt.Sprintf("%x", bz)
  109. tx := types.Tx(fmt.Sprintf("%v=%v", key, value))
  110. require.NoError(t, test.BroadcastTx(client)(ctx, tx))
  111. hash := tx.Hash()
  112. require.Eventuallyf(t, func() bool {
  113. txResp, err := client.Tx(ctx, hash, false)
  114. return err == nil && bytes.Equal(txResp.Tx, tx)
  115. },
  116. test.WaitTime, // timeout
  117. time.Second, // interval
  118. "submitted tx %X wasn't committed after %v",
  119. hash, test.WaitTime,
  120. )
  121. abciResp, err := client.ABCIQuery(ctx, "", []byte(key))
  122. require.NoError(t, err)
  123. assert.Equal(t, key, string(abciResp.Response.Key))
  124. assert.Equal(t, value, string(abciResp.Response.Value))
  125. })
  126. })
  127. }
  128. }