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.

132 lines
2.8 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
  1. package db
  2. import (
  3. "errors"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "github.com/tendermint/go-amino"
  8. dbm "github.com/tendermint/tm-db"
  9. cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
  10. "github.com/tendermint/tendermint/lite2/store"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. type dbs struct {
  14. db dbm.DB
  15. prefix string
  16. cdc *amino.Codec
  17. }
  18. // New returns a Store that wraps any DB (with an optional prefix in case you
  19. // want to use one DB with many light clients).
  20. func New(db dbm.DB, prefix string) store.Store {
  21. cdc := amino.NewCodec()
  22. cryptoAmino.RegisterAmino(cdc)
  23. return &dbs{db: db, prefix: prefix, cdc: cdc}
  24. }
  25. func (s *dbs) SaveSignedHeader(sh *types.SignedHeader) error {
  26. if sh.Height <= 0 {
  27. panic("negative or zero height")
  28. }
  29. bz, err := s.cdc.MarshalBinaryLengthPrefixed(sh)
  30. if err != nil {
  31. return err
  32. }
  33. s.db.Set(s.shKey(sh.Height), bz)
  34. return nil
  35. }
  36. func (s *dbs) SaveValidatorSet(valSet *types.ValidatorSet, height int64) error {
  37. if height <= 0 {
  38. panic("negative or zero height")
  39. }
  40. bz, err := s.cdc.MarshalBinaryLengthPrefixed(valSet)
  41. if err != nil {
  42. return err
  43. }
  44. s.db.Set(s.vsKey(height), bz)
  45. return nil
  46. }
  47. func (s *dbs) SignedHeader(height int64) (*types.SignedHeader, error) {
  48. bz := s.db.Get(s.shKey(height))
  49. if bz == nil {
  50. return nil, nil
  51. }
  52. var signedHeader *types.SignedHeader
  53. err := s.cdc.UnmarshalBinaryLengthPrefixed(bz, &signedHeader)
  54. return signedHeader, err
  55. }
  56. func (s *dbs) ValidatorSet(height int64) (*types.ValidatorSet, error) {
  57. bz := s.db.Get(s.vsKey(height))
  58. if bz == nil {
  59. return nil, nil
  60. }
  61. var valSet *types.ValidatorSet
  62. err := s.cdc.UnmarshalBinaryLengthPrefixed(bz, &valSet)
  63. return valSet, err
  64. }
  65. func (s *dbs) LastSignedHeaderHeight() (int64, error) {
  66. itr := s.db.ReverseIterator(
  67. s.shKey(1),
  68. append(s.shKey(1<<63-1), byte(0x00)),
  69. )
  70. defer itr.Close()
  71. for itr.Valid() {
  72. key := itr.Key()
  73. _, height, ok := parseShKey(key)
  74. if ok {
  75. return height, nil
  76. }
  77. }
  78. return -1, errors.New("no headers found")
  79. }
  80. func (s *dbs) shKey(height int64) []byte {
  81. return []byte(fmt.Sprintf("sh/%s/%010d", s.prefix, height))
  82. }
  83. func (s *dbs) vsKey(height int64) []byte {
  84. return []byte(fmt.Sprintf("vs/%s/%010d", s.prefix, height))
  85. }
  86. var keyPattern = regexp.MustCompile(`^(sh|vs)/([^/]*)/([0-9]+)/$`)
  87. func parseKey(key []byte) (part string, prefix string, height int64, ok bool) {
  88. submatch := keyPattern.FindSubmatch(key)
  89. if submatch == nil {
  90. return "", "", 0, false
  91. }
  92. part = string(submatch[1])
  93. prefix = string(submatch[2])
  94. heightStr := string(submatch[3])
  95. heightInt, err := strconv.Atoi(heightStr)
  96. if err != nil {
  97. return "", "", 0, false
  98. }
  99. height = int64(heightInt)
  100. ok = true // good!
  101. return
  102. }
  103. func parseShKey(key []byte) (prefix string, height int64, ok bool) {
  104. var part string
  105. part, prefix, height, ok = parseKey(key)
  106. if part != "sh" {
  107. return "", 0, false
  108. }
  109. return
  110. }