mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-10 22:10:11 +00:00
* rename adjusted to adjacent Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r352140829 * rename ErrTooMuchChange to ErrNotEnoughVotingPowerSigned Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r352142785 * verify commit is properly signed * remove no longer trusted headers * restore trustedHeader and trustedNextVals * check trustedHeader using options Refs https://github.com/tendermint/tendermint/pull/4209#issuecomment-562462165 * use correct var when checking if headers are adjacent in bisection func + replace TODO with a comment https://github.com/tendermint/tendermint/pull/3989#discussion_r352125455 * return header in VerifyHeaderAtHeight because that way we avoid DB call + add godoc comments + check if there are no headers yet in AutoClient https://github.com/tendermint/tendermint/pull/3989#pullrequestreview-315454506 * TestVerifyAdjacentHeaders: add 2 more test-cases + add TestVerifyReturnsErrorIfTrustLevelIsInvalid * lite: avoid overflow when parsing key in db store! * lite: rename AutoClient#Err to Errs * lite: add a test for AutoClient * lite: fix keyPattern and call itr.Next in db store * lite: add two tests for db store * lite: add TestClientRemovesNoLongerTrustedHeaders * lite: test Client#Cleanup * lite: test restoring trustedHeader https://github.com/tendermint/tendermint/pull/4209#issuecomment-562462165 * lite: comment out unused code in test_helpers * fix TestVerifyReturnsErrorIfTrustLevelIsInvalid after merge * change defaultRemoveNoLongerTrustedHeadersPeriod and add docs * write more doc * lite: uncomment testable examples * use stdlog.Fatal to stop AutoClient tests * make lll linter happy * separate errors for 2 cases - the validator set of a skipped header cannot be trusted, i.e. <1/3rd of h1 validator set has signed (new error, something like ErrNewValSetCantBeTrusted) - the validator set is trusted but < 2/3rds has signed (ErrNewHeaderCantBeTrusted) https://github.com/tendermint/tendermint/pull/4209#discussion_r360331253 * remove all headers (even the last one) that are outside of the trusting period. By doing this, we avoid checking the trustedHeader's hash in checkTrustedHeaderUsingOptions (case #1). https://github.com/tendermint/tendermint/pull/4209#discussion_r360332460 * explain restoreTrustedHeaderAndNextVals better https://github.com/tendermint/tendermint/pull/4209#discussion_r360602328 * add ConfirmationFunction option for optionally prompting for user input Y/n before removing headers Refs https://github.com/tendermint/tendermint/pull/4209#discussion_r360602945 * make cleaning optional https://github.com/tendermint/tendermint/pull/4209#discussion_r364838189 * return error when user refused to remove headers * check for double votes in VerifyCommitTrusting * leave only ErrNewValSetCantBeTrusted error to differenciate between h2Vals.VerifyCommit and h1NextVals.VerifyCommitTrusting * fix example tests * remove unnecessary if condition https://github.com/tendermint/tendermint/pull/4209#discussion_r365171847 It will be handled by the above switch. * verifyCommitBasic does not depend on vals Co-authored-by: Marko <marbar3778@yahoo.com>
156 lines
3.0 KiB
Go
156 lines
3.0 KiB
Go
package lite
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
stdlog "log"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
dbm "github.com/tendermint/tm-db"
|
|
|
|
"github.com/tendermint/tendermint/abci/example/kvstore"
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
httpp "github.com/tendermint/tendermint/lite2/provider/http"
|
|
dbs "github.com/tendermint/tendermint/lite2/store/db"
|
|
rpctest "github.com/tendermint/tendermint/rpc/test"
|
|
)
|
|
|
|
func TestExample_Client(t *testing.T) {
|
|
// give Tendermint time to generate some blocks
|
|
time.Sleep(5 * time.Second)
|
|
|
|
dbDir, err := ioutil.TempDir("", "lite-client-example")
|
|
if err != nil {
|
|
stdlog.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dbDir)
|
|
|
|
var (
|
|
config = rpctest.GetConfig()
|
|
chainID = config.ChainID()
|
|
)
|
|
|
|
provider, err := httpp.New(chainID, config.RPC.ListenAddress)
|
|
if err != nil {
|
|
stdlog.Fatal(err)
|
|
}
|
|
|
|
header, err := provider.SignedHeader(2)
|
|
if err != nil {
|
|
stdlog.Fatal(err)
|
|
}
|
|
|
|
db, err := dbm.NewGoLevelDB("lite-client-db", dbDir)
|
|
if err != nil {
|
|
stdlog.Fatal(err)
|
|
}
|
|
|
|
c, err := NewClient(
|
|
chainID,
|
|
TrustOptions{
|
|
Period: 504 * time.Hour, // 21 days
|
|
Height: 2,
|
|
Hash: header.Hash(),
|
|
},
|
|
provider,
|
|
dbs.New(db, chainID),
|
|
)
|
|
if err != nil {
|
|
stdlog.Fatal(err)
|
|
}
|
|
c.SetLogger(log.TestingLogger())
|
|
|
|
_, 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 TestExample_AutoClient(t *testing.T) {
|
|
// give Tendermint time to generate some blocks
|
|
time.Sleep(5 * time.Second)
|
|
|
|
dbDir, err := ioutil.TempDir("", "lite-client-example")
|
|
if err != nil {
|
|
stdlog.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dbDir)
|
|
|
|
var (
|
|
config = rpctest.GetConfig()
|
|
chainID = config.ChainID()
|
|
)
|
|
|
|
provider, err := httpp.New(chainID, config.RPC.ListenAddress)
|
|
if err != nil {
|
|
stdlog.Fatal(err)
|
|
}
|
|
|
|
header, err := provider.SignedHeader(2)
|
|
if err != nil {
|
|
stdlog.Fatal(err)
|
|
}
|
|
|
|
db, err := dbm.NewGoLevelDB("lite-client-db", dbDir)
|
|
if err != nil {
|
|
stdlog.Fatal(err)
|
|
}
|
|
|
|
base, err := NewClient(
|
|
chainID,
|
|
TrustOptions{
|
|
Period: 504 * time.Hour, // 21 days
|
|
Height: 2,
|
|
Hash: header.Hash(),
|
|
},
|
|
provider,
|
|
dbs.New(db, chainID),
|
|
)
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
// start a tendermint node (and kvstore) in the background to test against
|
|
app := kvstore.NewApplication()
|
|
node := rpctest.StartTendermint(app)
|
|
|
|
code := m.Run()
|
|
|
|
// and shut down proper at the end
|
|
rpctest.StopTendermint(node)
|
|
os.Exit(code)
|
|
}
|