Browse Source

light: wait for tendermint node to start before running example test (#6744)

pull/6771/head
Callum Waters 3 years ago
committed by GitHub
parent
commit
9a3861fb82
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 89 deletions
  1. +18
    -83
      light/example_test.go
  2. +32
    -6
      light/light_test.go

+ 18
- 83
light/example_test.go View File

@ -2,7 +2,6 @@ package light_test
import (
"context"
"fmt"
"io/ioutil"
stdlog "log"
"os"
@ -19,23 +18,22 @@ import (
rpctest "github.com/tendermint/tendermint/rpc/test"
)
// Automatically getting new headers and verifying them.
func ExampleClient_Update() {
// Manually getting light blocks and verifying them.
func ExampleClient() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conf := rpctest.CreateConfig("ExampleClient_Update")
conf := rpctest.CreateConfig("ExampleClient_VerifyLightBlockAtHeight")
logger := log.TestingLogger()
// 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)
@ -49,6 +47,9 @@ func ExampleClient_Update() {
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)
@ -59,8 +60,7 @@ func ExampleClient_Update() {
stdlog.Fatal(err)
}
c, err := light.NewClient(
ctx,
c, err := light.NewClient(ctx,
chainID,
light.TrustOptions{
Period: 504 * time.Hour, // 21 days
@ -70,7 +70,7 @@ func ExampleClient_Update() {
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)
@ -81,91 +81,26 @@ func ExampleClient_Update() {
}
}()
// wait for a few more blocks to be produced
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() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conf := rpctest.CreateConfig("ExampleClient_VerifyLightBlockAtHeight")
// 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()),
)
// veify the block at height 3
_, err = c.VerifyLightBlockAtHeight(context.Background(), 3, time.Now())
if err != nil {
stdlog.Fatal(err)
}
defer func() {
if err := c.Cleanup(); err != nil {
stdlog.Fatal(err)
}
}()
_, err = c.VerifyLightBlockAtHeight(context.Background(), 3, time.Now())
// retrieve light block at height 3
_, err = c.TrustedLightBlock(3)
if err != nil {
stdlog.Fatal(err)
}
h, err := c.TrustedLightBlock(3)
// update to the latest height
lb, err := c.Update(ctx, time.Now())
if err != nil {
stdlog.Fatal(err)
}
fmt.Println("got header", h.Height)
logger.Info("verified light block", "light-block", lb)
}

+ 32
- 6
light/light_test.go View File

@ -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
}
}
}

Loading…
Cancel
Save