lite2: move AutoClient into Client (#4326)

* lite2: move AutoClient into Client

Most of the users will want auto update feature, so it makes sense to
move it into the Client itself, rather than having a separate
abstraction (it makes the code cleaner, but introduces an extra thing
the user will need to learn).

Also, add `FirstTrustedHeight` func to Client to get first trusted height.

* fix db store tests

* separate examples for auto and manual clients

* AutoUpdate tries to update to latest state

NOT 1 header at a time

* fix errors

* lite2: make Logger an option

remove SetLogger func

* fix lite cmd

* lite2: make concurrency assumptions explicit

* fixes after my own review

* no need for nextHeightFn

sequence func will download intermediate headers

* correct comment
This commit is contained in:
Anton Kaliaev
2020-01-22 15:19:03 +04:00
committed by GitHub
parent 5f2f97548f
commit f95409e070
13 changed files with 303 additions and 222 deletions

View File

@@ -8,8 +8,6 @@ import (
"testing"
"time"
"github.com/pkg/errors"
dbm "github.com/tendermint/tm-db"
"github.com/tendermint/tendermint/abci/example/kvstore"
@@ -19,7 +17,8 @@ import (
rpctest "github.com/tendermint/tendermint/rpc/test"
)
func TestExample_Client(t *testing.T) {
// Automatically getting new headers and verifying them.
func TestExample_Client_AutoUpdate(t *testing.T) {
// give Tendermint time to generate some blocks
time.Sleep(5 * time.Second)
@@ -58,16 +57,15 @@ func TestExample_Client(t *testing.T) {
},
provider,
dbs.New(db, chainID),
UpdatePeriod(1*time.Second),
Logger(log.TestingLogger()),
)
if err != nil {
stdlog.Fatal(err)
}
c.SetLogger(log.TestingLogger())
defer c.Stop()
_, err = c.VerifyHeaderAtHeight(3, time.Now())
if err != nil {
stdlog.Fatal(err)
}
time.Sleep(2 * time.Second)
h, err := c.TrustedHeader(3, time.Now())
if err != nil {
@@ -78,7 +76,8 @@ func TestExample_Client(t *testing.T) {
// Output: got header 3
}
func TestExample_AutoClient(t *testing.T) {
// Manually getting headers and verifying them.
func TestExample_Client_ManualUpdate(t *testing.T) {
// give Tendermint time to generate some blocks
time.Sleep(5 * time.Second)
@@ -108,7 +107,7 @@ func TestExample_AutoClient(t *testing.T) {
stdlog.Fatal(err)
}
base, err := NewClient(
c, err := NewClient(
chainID,
TrustOptions{
Period: 504 * time.Hour, // 21 days
@@ -117,29 +116,26 @@ func TestExample_AutoClient(t *testing.T) {
},
provider,
dbs.New(db, chainID),
UpdatePeriod(0),
Logger(log.TestingLogger()),
)
if err != nil {
stdlog.Fatal(err)
}
base.SetLogger(log.TestingLogger())
c := NewAutoClient(base, 1*time.Second)
defer c.Stop()
select {
case h := <-c.TrustedHeaders():
fmt.Println("got header", h.Height)
// Output: got header 3
case err := <-c.Errs():
switch errors.Cause(err).(type) {
case ErrOldHeaderExpired:
// reobtain trust height and hash
stdlog.Fatal(err)
default:
// try with another full node
stdlog.Fatal(err)
}
_, err = c.VerifyHeaderAtHeight(3, time.Now())
if err != nil {
stdlog.Fatal(err)
}
h, err := c.TrustedHeader(3, time.Now())
if err != nil {
stdlog.Fatal(err)
}
fmt.Println("got header", h.Height)
// Output: got header 3
}
func TestMain(m *testing.M) {