You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.0 KiB

lite2: light client with weak subjectivity (#3989) Refs #1771 ADR: https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-044-lite-client-with-weak-subjectivity.md ## Commits: * add Verifier and VerifyCommitTrusting * add two more checks make trustLevel an option * float32 for trustLevel * check newHeader time * started writing lite Client * unify Verify methods * ensure h2.Header.bfttime < h1.Header.bfttime + tp * move trust checks into Verify function * add more comments * more docs * started writing tests * unbonding period failures * tests are green * export ErrNewHeaderTooFarIntoFuture * make golangci happy * test for non-adjusted headers * more precision * providers and stores * VerifyHeader and VerifyHeaderAtHeight funcs * fix compile errors * remove lastVerifiedHeight, persist new trusted header * sequential verification * remove TrustedStore option * started writing tests for light client * cover basic cases for linear verification * bisection tests PASS * rename BisectingVerification to SkippingVerification * refactor the code * add TrustedHeader method * consolidate sequential verification tests * consolidate skipping verification tests * rename trustedVals to trustedNextVals * start writing docs * ValidateTrustLevel func and ErrOldHeaderExpired error * AutoClient and example tests * fix errors * update doc * remove ErrNewHeaderTooFarIntoFuture This check is unnecessary given existing a) ErrOldHeaderExpired b) h2.Time > now checks. * return an error if we're at more recent height * add comments * add LastSignedHeaderHeight method to Store I think it's fine if Store tracks last height * copy over proxy from old lite package * make TrustedHeader return latest if height=0 * modify LastSignedHeaderHeight to return an error if no headers exist * copy over proxy impl * refactor proxy and start http lite client * Tx and BlockchainInfo methods * Block method * commit method * code compiles again * lite client compiles * extract updateLiteClientIfNeededTo func * move final parts * add placeholder for tests * force usage of lite http client in proxy * comment out query tests for now * explicitly mention tp: trusting period * verify nextVals in VerifyHeader * refactor bisection * move the NextValidatorsHash check into updateTrustedHeaderAndVals + update the comment * add ConsensusParams method to RPC client * add ConsensusParams to rpc/mock/client * change trustLevel type to a new cmn.Fraction type + update SkippingVerification comment * stress out trustLevel is only used for non-adjusted headers * fixes after Fede's review Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * compare newHeader with a header from an alternative provider * save pivot header Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349122824 * check header can still be trusted in TrustedHeader Refs https://github.com/tendermint/tendermint/pull/3989#discussion_r349101424 * lite: update Validators and Block endpoints - Block no longer contains BlockMeta - Validators now accept two additional params: page and perPage * make linter happy
5 years ago
lite: follow up from #3989 (#4209) * 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>
5 years ago
  1. package lite
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. stdlog "log"
  6. "os"
  7. "testing"
  8. "time"
  9. "github.com/pkg/errors"
  10. dbm "github.com/tendermint/tm-db"
  11. "github.com/tendermint/tendermint/abci/example/kvstore"
  12. "github.com/tendermint/tendermint/libs/log"
  13. httpp "github.com/tendermint/tendermint/lite2/provider/http"
  14. dbs "github.com/tendermint/tendermint/lite2/store/db"
  15. rpctest "github.com/tendermint/tendermint/rpc/test"
  16. )
  17. func TestExample_Client(t *testing.T) {
  18. // give Tendermint time to generate some blocks
  19. time.Sleep(5 * time.Second)
  20. dbDir, err := ioutil.TempDir("", "lite-client-example")
  21. if err != nil {
  22. stdlog.Fatal(err)
  23. }
  24. defer os.RemoveAll(dbDir)
  25. var (
  26. config = rpctest.GetConfig()
  27. chainID = config.ChainID()
  28. )
  29. provider, err := httpp.New(chainID, config.RPC.ListenAddress)
  30. if err != nil {
  31. stdlog.Fatal(err)
  32. }
  33. header, err := provider.SignedHeader(2)
  34. if err != nil {
  35. stdlog.Fatal(err)
  36. }
  37. db, err := dbm.NewGoLevelDB("lite-client-db", dbDir)
  38. if err != nil {
  39. stdlog.Fatal(err)
  40. }
  41. c, err := NewClient(
  42. chainID,
  43. TrustOptions{
  44. Period: 504 * time.Hour, // 21 days
  45. Height: 2,
  46. Hash: header.Hash(),
  47. },
  48. provider,
  49. dbs.New(db, chainID),
  50. )
  51. if err != nil {
  52. stdlog.Fatal(err)
  53. }
  54. c.SetLogger(log.TestingLogger())
  55. _, err = c.VerifyHeaderAtHeight(3, time.Now())
  56. if err != nil {
  57. stdlog.Fatal(err)
  58. }
  59. h, err := c.TrustedHeader(3, time.Now())
  60. if err != nil {
  61. stdlog.Fatal(err)
  62. }
  63. fmt.Println("got header", h.Height)
  64. // Output: got header 3
  65. }
  66. func TestExample_AutoClient(t *testing.T) {
  67. // give Tendermint time to generate some blocks
  68. time.Sleep(5 * time.Second)
  69. dbDir, err := ioutil.TempDir("", "lite-client-example")
  70. if err != nil {
  71. stdlog.Fatal(err)
  72. }
  73. defer os.RemoveAll(dbDir)
  74. var (
  75. config = rpctest.GetConfig()
  76. chainID = config.ChainID()
  77. )
  78. provider, err := httpp.New(chainID, config.RPC.ListenAddress)
  79. if err != nil {
  80. stdlog.Fatal(err)
  81. }
  82. header, err := provider.SignedHeader(2)
  83. if err != nil {
  84. stdlog.Fatal(err)
  85. }
  86. db, err := dbm.NewGoLevelDB("lite-client-db", dbDir)
  87. if err != nil {
  88. stdlog.Fatal(err)
  89. }
  90. base, err := NewClient(
  91. chainID,
  92. TrustOptions{
  93. Period: 504 * time.Hour, // 21 days
  94. Height: 2,
  95. Hash: header.Hash(),
  96. },
  97. provider,
  98. dbs.New(db, chainID),
  99. )
  100. if err != nil {
  101. stdlog.Fatal(err)
  102. }
  103. base.SetLogger(log.TestingLogger())
  104. c := NewAutoClient(base, 1*time.Second)
  105. defer c.Stop()
  106. select {
  107. case h := <-c.TrustedHeaders():
  108. fmt.Println("got header", h.Height)
  109. // Output: got header 3
  110. case err := <-c.Errs():
  111. switch errors.Cause(err).(type) {
  112. case ErrOldHeaderExpired:
  113. // reobtain trust height and hash
  114. stdlog.Fatal(err)
  115. default:
  116. // try with another full node
  117. stdlog.Fatal(err)
  118. }
  119. }
  120. }
  121. func TestMain(m *testing.M) {
  122. // start a tendermint node (and kvstore) in the background to test against
  123. app := kvstore.NewApplication()
  124. node := rpctest.StartTendermint(app)
  125. code := m.Run()
  126. // and shut down proper at the end
  127. rpctest.StopTendermint(node)
  128. os.Exit(code)
  129. }