|
@ -2,7 +2,6 @@ package light_test |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"context" |
|
|
"context" |
|
|
"fmt" |
|
|
|
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
stdlog "log" |
|
|
stdlog "log" |
|
|
"os" |
|
|
"os" |
|
@ -19,23 +18,22 @@ import ( |
|
|
rpctest "github.com/tendermint/tendermint/rpc/test" |
|
|
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()) |
|
|
ctx, cancel := context.WithCancel(context.Background()) |
|
|
defer cancel() |
|
|
defer cancel() |
|
|
conf := rpctest.CreateConfig("ExampleClient_Update") |
|
|
|
|
|
|
|
|
conf := rpctest.CreateConfig("ExampleClient_VerifyLightBlockAtHeight") |
|
|
|
|
|
logger := log.TestingLogger() |
|
|
|
|
|
|
|
|
// Start a test application
|
|
|
// Start a test application
|
|
|
app := kvstore.NewApplication() |
|
|
app := kvstore.NewApplication() |
|
|
|
|
|
|
|
|
_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout) |
|
|
_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
stdlog.Fatal(err) |
|
|
stdlog.Fatal(err) |
|
|
} |
|
|
} |
|
|
defer func() { _ = closer(ctx) }() |
|
|
defer func() { _ = closer(ctx) }() |
|
|
|
|
|
|
|
|
// give Tendermint time to generate some blocks
|
|
|
|
|
|
time.Sleep(5 * time.Second) |
|
|
|
|
|
|
|
|
|
|
|
dbDir, err := ioutil.TempDir("", "light-client-example") |
|
|
dbDir, err := ioutil.TempDir("", "light-client-example") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
stdlog.Fatal(err) |
|
|
stdlog.Fatal(err) |
|
@ -49,6 +47,9 @@ func ExampleClient_Update() { |
|
|
stdlog.Fatal(err) |
|
|
stdlog.Fatal(err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// give Tendermint time to generate some blocks
|
|
|
|
|
|
time.Sleep(5 * time.Second) |
|
|
|
|
|
|
|
|
block, err := primary.LightBlock(ctx, 2) |
|
|
block, err := primary.LightBlock(ctx, 2) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
stdlog.Fatal(err) |
|
|
stdlog.Fatal(err) |
|
@ -59,8 +60,7 @@ func ExampleClient_Update() { |
|
|
stdlog.Fatal(err) |
|
|
stdlog.Fatal(err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
c, err := light.NewClient( |
|
|
|
|
|
ctx, |
|
|
|
|
|
|
|
|
c, err := light.NewClient(ctx, |
|
|
chainID, |
|
|
chainID, |
|
|
light.TrustOptions{ |
|
|
light.TrustOptions{ |
|
|
Period: 504 * time.Hour, // 21 days
|
|
|
Period: 504 * time.Hour, // 21 days
|
|
@ -70,7 +70,7 @@ func ExampleClient_Update() { |
|
|
primary, |
|
|
primary, |
|
|
[]provider.Provider{primary}, // NOTE: primary should not be used here
|
|
|
[]provider.Provider{primary}, // NOTE: primary should not be used here
|
|
|
dbs.New(db), |
|
|
dbs.New(db), |
|
|
light.Logger(log.TestingLogger()), |
|
|
|
|
|
|
|
|
light.Logger(logger), |
|
|
) |
|
|
) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
stdlog.Fatal(err) |
|
|
stdlog.Fatal(err) |
|
@ -81,91 +81,26 @@ func ExampleClient_Update() { |
|
|
} |
|
|
} |
|
|
}() |
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
// wait for a few more blocks to be produced
|
|
|
time.Sleep(2 * time.Second) |
|
|
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 { |
|
|
if err != nil { |
|
|
stdlog.Fatal(err) |
|
|
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 { |
|
|
if err != nil { |
|
|
stdlog.Fatal(err) |
|
|
stdlog.Fatal(err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
h, err := c.TrustedLightBlock(3) |
|
|
|
|
|
|
|
|
// update to the latest height
|
|
|
|
|
|
lb, err := c.Update(ctx, time.Now()) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
stdlog.Fatal(err) |
|
|
stdlog.Fatal(err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fmt.Println("got header", h.Height) |
|
|
|
|
|
|
|
|
logger.Info("verified light block", "light-block", lb) |
|
|
} |
|
|
} |