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.

164 lines
4.2 KiB

  1. package light_test
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. dbm "github.com/tendermint/tm-db"
  10. "github.com/tendermint/tendermint/abci/example/kvstore"
  11. "github.com/tendermint/tendermint/libs/log"
  12. "github.com/tendermint/tendermint/light"
  13. "github.com/tendermint/tendermint/light/provider"
  14. httpp "github.com/tendermint/tendermint/light/provider/http"
  15. dbs "github.com/tendermint/tendermint/light/store/db"
  16. rpctest "github.com/tendermint/tendermint/rpc/test"
  17. "github.com/tendermint/tendermint/types"
  18. )
  19. // NOTE: these are ports of the tests from example_test.go but
  20. // rewritten as more conventional tests.
  21. // Automatically getting new headers and verifying them.
  22. func TestClientIntegration_Update(t *testing.T) {
  23. t.Parallel()
  24. ctx, cancel := context.WithCancel(context.Background())
  25. defer cancel()
  26. conf := rpctest.CreateConfig(t.Name())
  27. // Start a test application
  28. app := kvstore.NewApplication()
  29. _, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
  30. require.NoError(t, err)
  31. defer func() { require.NoError(t, closer(ctx)) }()
  32. // give Tendermint time to generate some blocks
  33. time.Sleep(5 * time.Second)
  34. dbDir, err := ioutil.TempDir("", "light-client-test-update-example")
  35. require.NoError(t, err)
  36. defer os.RemoveAll(dbDir)
  37. chainID := conf.ChainID()
  38. primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
  39. require.NoError(t, err)
  40. // give Tendermint time to generate some blocks
  41. block, err := waitForBlock(ctx, primary, 2)
  42. require.NoError(t, err)
  43. db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
  44. require.NoError(t, err)
  45. c, err := light.NewClient(
  46. ctx,
  47. chainID,
  48. light.TrustOptions{
  49. Period: 504 * time.Hour, // 21 days
  50. Height: 2,
  51. Hash: block.Hash(),
  52. },
  53. primary,
  54. []provider.Provider{primary}, // NOTE: primary should not be used here
  55. dbs.New(db),
  56. light.Logger(log.TestingLogger()),
  57. )
  58. require.NoError(t, err)
  59. defer func() { require.NoError(t, c.Cleanup()) }()
  60. // ensure Tendermint is at height 3 or higher
  61. _, err = waitForBlock(ctx, primary, 3)
  62. require.NoError(t, err)
  63. h, err := c.Update(ctx, time.Now())
  64. require.NoError(t, err)
  65. require.NotNil(t, h)
  66. require.True(t, h.Height > 2)
  67. }
  68. // Manually getting light blocks and verifying them.
  69. func TestClientIntegration_VerifyLightBlockAtHeight(t *testing.T) {
  70. t.Parallel()
  71. ctx, cancel := context.WithCancel(context.Background())
  72. defer cancel()
  73. conf := rpctest.CreateConfig(t.Name())
  74. // Start a test application
  75. app := kvstore.NewApplication()
  76. _, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
  77. require.NoError(t, err)
  78. defer func() { require.NoError(t, closer(ctx)) }()
  79. dbDir, err := ioutil.TempDir("", "light-client-test-verify-example")
  80. require.NoError(t, err)
  81. defer os.RemoveAll(dbDir)
  82. chainID := conf.ChainID()
  83. primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
  84. require.NoError(t, err)
  85. // give Tendermint time to generate some blocks
  86. block, err := waitForBlock(ctx, primary, 2)
  87. require.NoError(t, err)
  88. db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
  89. require.NoError(t, err)
  90. c, err := light.NewClient(ctx,
  91. chainID,
  92. light.TrustOptions{
  93. Period: 504 * time.Hour, // 21 days
  94. Height: 2,
  95. Hash: block.Hash(),
  96. },
  97. primary,
  98. []provider.Provider{primary}, // NOTE: primary should not be used here
  99. dbs.New(db),
  100. light.Logger(log.TestingLogger()),
  101. )
  102. require.NoError(t, err)
  103. defer func() { require.NoError(t, c.Cleanup()) }()
  104. // ensure Tendermint is at height 3 or higher
  105. _, err = waitForBlock(ctx, primary, 3)
  106. require.NoError(t, err)
  107. _, err = c.VerifyLightBlockAtHeight(ctx, 3, time.Now())
  108. require.NoError(t, err)
  109. h, err := c.TrustedLightBlock(3)
  110. require.NoError(t, err)
  111. require.EqualValues(t, 3, h.Height)
  112. }
  113. func waitForBlock(ctx context.Context, p provider.Provider, height int64) (*types.LightBlock, error) {
  114. for {
  115. block, err := p.LightBlock(ctx, height)
  116. switch err {
  117. case nil:
  118. return block, nil
  119. // node isn't running yet, wait 1 second and repeat
  120. case provider.ErrNoResponse, provider.ErrHeightTooHigh:
  121. timer := time.NewTimer(1 * time.Second)
  122. select {
  123. case <-ctx.Done():
  124. return nil, ctx.Err()
  125. case <-timer.C:
  126. }
  127. default:
  128. return nil, err
  129. }
  130. }
  131. }