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.

138 lines
3.5 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. )
  18. // NOTE: these are ports of the tests from example_test.go but
  19. // rewritten as more conventional tests.
  20. // Automatically getting new headers and verifying them.
  21. func TestClientIntegration_Update(t *testing.T) {
  22. t.Parallel()
  23. ctx, cancel := context.WithCancel(context.Background())
  24. defer cancel()
  25. conf := rpctest.CreateConfig(t.Name())
  26. // Start a test application
  27. app := kvstore.NewApplication()
  28. _, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
  29. require.NoError(t, err)
  30. defer func() { require.NoError(t, closer(ctx)) }()
  31. // give Tendermint time to generate some blocks
  32. time.Sleep(5 * time.Second)
  33. dbDir, err := ioutil.TempDir("", "light-client-test-update-example")
  34. require.NoError(t, err)
  35. defer os.RemoveAll(dbDir)
  36. chainID := conf.ChainID()
  37. primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
  38. require.NoError(t, err)
  39. block, err := primary.LightBlock(ctx, 2)
  40. require.NoError(t, err)
  41. db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
  42. require.NoError(t, err)
  43. c, err := light.NewClient(
  44. ctx,
  45. chainID,
  46. light.TrustOptions{
  47. Period: 504 * time.Hour, // 21 days
  48. Height: 2,
  49. Hash: block.Hash(),
  50. },
  51. primary,
  52. []provider.Provider{primary}, // NOTE: primary should not be used here
  53. dbs.New(db),
  54. light.Logger(log.TestingLogger()),
  55. )
  56. require.NoError(t, err)
  57. defer func() { require.NoError(t, c.Cleanup()) }()
  58. time.Sleep(2 * time.Second)
  59. h, err := c.Update(ctx, time.Now())
  60. require.NoError(t, err)
  61. require.NotNil(t, h)
  62. require.True(t, h.Height > 2)
  63. }
  64. // Manually getting light blocks and verifying them.
  65. func TestClientIntegration_VerifyLightBlockAtHeight(t *testing.T) {
  66. t.Parallel()
  67. ctx, cancel := context.WithCancel(context.Background())
  68. defer cancel()
  69. conf := rpctest.CreateConfig(t.Name())
  70. // Start a test application
  71. app := kvstore.NewApplication()
  72. _, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
  73. require.NoError(t, err)
  74. defer func() { require.NoError(t, closer(ctx)) }()
  75. // give Tendermint time to generate some blocks
  76. time.Sleep(5 * time.Second)
  77. dbDir, err := ioutil.TempDir("", "light-client-test-verify-example")
  78. require.NoError(t, err)
  79. defer os.RemoveAll(dbDir)
  80. chainID := conf.ChainID()
  81. primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
  82. require.NoError(t, err)
  83. block, err := primary.LightBlock(ctx, 2)
  84. require.NoError(t, err)
  85. db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
  86. require.NoError(t, err)
  87. c, err := light.NewClient(ctx,
  88. chainID,
  89. light.TrustOptions{
  90. Period: 504 * time.Hour, // 21 days
  91. Height: 2,
  92. Hash: block.Hash(),
  93. },
  94. primary,
  95. []provider.Provider{primary}, // NOTE: primary should not be used here
  96. dbs.New(db),
  97. light.Logger(log.TestingLogger()),
  98. )
  99. require.NoError(t, err)
  100. defer func() { require.NoError(t, c.Cleanup()) }()
  101. _, err = c.VerifyLightBlockAtHeight(ctx, 3, time.Now())
  102. require.NoError(t, err)
  103. h, err := c.TrustedLightBlock(3)
  104. require.NoError(t, err)
  105. require.EqualValues(t, 3, h.Height)
  106. }