mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-06 21:36:26 +00:00
light: wait for tendermint node to start before running example test (#6744)
This commit is contained in:
@@ -2,7 +2,6 @@ package light_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
stdlog "log"
|
||||
"os"
|
||||
@@ -19,87 +18,12 @@ import (
|
||||
rpctest "github.com/tendermint/tendermint/rpc/test"
|
||||
)
|
||||
|
||||
// Automatically getting new headers and verifying them.
|
||||
func ExampleClient_Update() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
conf := rpctest.CreateConfig("ExampleClient_Update")
|
||||
|
||||
// Start a test application
|
||||
app := kvstore.NewApplication()
|
||||
_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
defer func() { _ = closer(ctx) }()
|
||||
|
||||
// give Tendermint time to generate some blocks
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
dbDir, err := ioutil.TempDir("", "light-client-example")
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(dbDir)
|
||||
|
||||
chainID := conf.ChainID()
|
||||
|
||||
primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
|
||||
block, err := primary.LightBlock(ctx, 2)
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
|
||||
db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
|
||||
c, err := light.NewClient(
|
||||
ctx,
|
||||
chainID,
|
||||
light.TrustOptions{
|
||||
Period: 504 * time.Hour, // 21 days
|
||||
Height: 2,
|
||||
Hash: block.Hash(),
|
||||
},
|
||||
primary,
|
||||
[]provider.Provider{primary}, // NOTE: primary should not be used here
|
||||
dbs.New(db),
|
||||
light.Logger(log.TestingLogger()),
|
||||
)
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := c.Cleanup(); err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
h, err := c.Update(ctx, time.Now())
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
|
||||
if h != nil && h.Height > 2 {
|
||||
fmt.Println("successful update")
|
||||
} else {
|
||||
fmt.Println("update failed")
|
||||
}
|
||||
}
|
||||
|
||||
// Manually getting light blocks and verifying them.
|
||||
func ExampleClient_VerifyLightBlockAtHeight() {
|
||||
func ExampleClient() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
conf := rpctest.CreateConfig("ExampleClient_VerifyLightBlockAtHeight")
|
||||
logger := log.TestingLogger()
|
||||
|
||||
// Start a test application
|
||||
app := kvstore.NewApplication()
|
||||
@@ -110,9 +34,6 @@ func ExampleClient_VerifyLightBlockAtHeight() {
|
||||
}
|
||||
defer func() { _ = closer(ctx) }()
|
||||
|
||||
// give Tendermint time to generate some blocks
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
dbDir, err := ioutil.TempDir("", "light-client-example")
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
@@ -126,6 +47,9 @@ func ExampleClient_VerifyLightBlockAtHeight() {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
|
||||
// give Tendermint time to generate some blocks
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
block, err := primary.LightBlock(ctx, 2)
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
@@ -146,7 +70,7 @@ func ExampleClient_VerifyLightBlockAtHeight() {
|
||||
primary,
|
||||
[]provider.Provider{primary}, // NOTE: primary should not be used here
|
||||
dbs.New(db),
|
||||
light.Logger(log.TestingLogger()),
|
||||
light.Logger(logger),
|
||||
)
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
@@ -157,15 +81,26 @@ func ExampleClient_VerifyLightBlockAtHeight() {
|
||||
}
|
||||
}()
|
||||
|
||||
// wait for a few more blocks to be produced
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// veify the block at height 3
|
||||
_, err = c.VerifyLightBlockAtHeight(context.Background(), 3, time.Now())
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
|
||||
h, err := c.TrustedLightBlock(3)
|
||||
// retrieve light block at height 3
|
||||
_, err = c.TrustedLightBlock(3)
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("got header", h.Height)
|
||||
// update to the latest height
|
||||
lb, err := c.Update(ctx, time.Now())
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
|
||||
logger.Info("verified light block", "light-block", lb)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
httpp "github.com/tendermint/tendermint/light/provider/http"
|
||||
dbs "github.com/tendermint/tendermint/light/store/db"
|
||||
rpctest "github.com/tendermint/tendermint/rpc/test"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
// NOTE: these are ports of the tests from example_test.go but
|
||||
@@ -48,7 +49,8 @@ func TestClientIntegration_Update(t *testing.T) {
|
||||
primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
|
||||
require.NoError(t, err)
|
||||
|
||||
block, err := primary.LightBlock(ctx, 2)
|
||||
// give Tendermint time to generate some blocks
|
||||
block, err := waitForBlock(ctx, primary, 2)
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
|
||||
@@ -71,7 +73,9 @@ func TestClientIntegration_Update(t *testing.T) {
|
||||
|
||||
defer func() { require.NoError(t, c.Cleanup()) }()
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
// ensure Tendermint is at height 3 or higher
|
||||
_, err = waitForBlock(ctx, primary, 3)
|
||||
require.NoError(t, err)
|
||||
|
||||
h, err := c.Update(ctx, time.Now())
|
||||
require.NoError(t, err)
|
||||
@@ -94,9 +98,6 @@ func TestClientIntegration_VerifyLightBlockAtHeight(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer func() { require.NoError(t, closer(ctx)) }()
|
||||
|
||||
// give Tendermint time to generate some blocks
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
dbDir, err := ioutil.TempDir("", "light-client-test-verify-example")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(dbDir)
|
||||
@@ -106,7 +107,8 @@ func TestClientIntegration_VerifyLightBlockAtHeight(t *testing.T) {
|
||||
primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
|
||||
require.NoError(t, err)
|
||||
|
||||
block, err := primary.LightBlock(ctx, 2)
|
||||
// give Tendermint time to generate some blocks
|
||||
block, err := waitForBlock(ctx, primary, 2)
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
|
||||
@@ -128,6 +130,10 @@ func TestClientIntegration_VerifyLightBlockAtHeight(t *testing.T) {
|
||||
|
||||
defer func() { require.NoError(t, c.Cleanup()) }()
|
||||
|
||||
// ensure Tendermint is at height 3 or higher
|
||||
_, err = waitForBlock(ctx, primary, 3)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = c.VerifyLightBlockAtHeight(ctx, 3, time.Now())
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -136,3 +142,23 @@ func TestClientIntegration_VerifyLightBlockAtHeight(t *testing.T) {
|
||||
|
||||
require.EqualValues(t, 3, h.Height)
|
||||
}
|
||||
|
||||
func waitForBlock(ctx context.Context, p provider.Provider, height int64) (*types.LightBlock, error) {
|
||||
for {
|
||||
block, err := p.LightBlock(ctx, height)
|
||||
switch err {
|
||||
case nil:
|
||||
return block, nil
|
||||
// node isn't running yet, wait 1 second and repeat
|
||||
case provider.ErrNoResponse, provider.ErrHeightTooHigh:
|
||||
timer := time.NewTimer(1 * time.Second)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user