mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-15 11:46:11 +00:00
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
133 lines
2.8 KiB
Go
133 lines
2.8 KiB
Go
package db
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"github.com/tendermint/go-amino"
|
|
dbm "github.com/tendermint/tm-db"
|
|
|
|
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
|
|
"github.com/tendermint/tendermint/lite2/store"
|
|
"github.com/tendermint/tendermint/types"
|
|
)
|
|
|
|
type dbs struct {
|
|
db dbm.DB
|
|
prefix string
|
|
|
|
cdc *amino.Codec
|
|
}
|
|
|
|
// New returns a Store that wraps any DB (with an optional prefix in case you
|
|
// want to use one DB with many light clients).
|
|
func New(db dbm.DB, prefix string) store.Store {
|
|
cdc := amino.NewCodec()
|
|
cryptoAmino.RegisterAmino(cdc)
|
|
return &dbs{db: db, prefix: prefix, cdc: cdc}
|
|
}
|
|
|
|
func (s *dbs) SaveSignedHeader(sh *types.SignedHeader) error {
|
|
if sh.Height <= 0 {
|
|
panic("negative or zero height")
|
|
}
|
|
|
|
bz, err := s.cdc.MarshalBinaryLengthPrefixed(sh)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.db.Set(s.shKey(sh.Height), bz)
|
|
return nil
|
|
}
|
|
|
|
func (s *dbs) SaveValidatorSet(valSet *types.ValidatorSet, height int64) error {
|
|
if height <= 0 {
|
|
panic("negative or zero height")
|
|
}
|
|
|
|
bz, err := s.cdc.MarshalBinaryLengthPrefixed(valSet)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.db.Set(s.vsKey(height), bz)
|
|
return nil
|
|
}
|
|
|
|
func (s *dbs) SignedHeader(height int64) (*types.SignedHeader, error) {
|
|
bz := s.db.Get(s.shKey(height))
|
|
if bz == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
var signedHeader *types.SignedHeader
|
|
err := s.cdc.UnmarshalBinaryLengthPrefixed(bz, &signedHeader)
|
|
return signedHeader, err
|
|
}
|
|
|
|
func (s *dbs) ValidatorSet(height int64) (*types.ValidatorSet, error) {
|
|
bz := s.db.Get(s.vsKey(height))
|
|
if bz == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
var valSet *types.ValidatorSet
|
|
err := s.cdc.UnmarshalBinaryLengthPrefixed(bz, &valSet)
|
|
return valSet, err
|
|
}
|
|
|
|
func (s *dbs) LastSignedHeaderHeight() (int64, error) {
|
|
itr := s.db.ReverseIterator(
|
|
s.shKey(1),
|
|
append(s.shKey(1<<63-1), byte(0x00)),
|
|
)
|
|
defer itr.Close()
|
|
|
|
for itr.Valid() {
|
|
key := itr.Key()
|
|
_, height, ok := parseShKey(key)
|
|
if ok {
|
|
return height, nil
|
|
}
|
|
}
|
|
|
|
return -1, errors.New("no headers found")
|
|
}
|
|
|
|
func (s *dbs) shKey(height int64) []byte {
|
|
return []byte(fmt.Sprintf("sh/%s/%010d", s.prefix, height))
|
|
}
|
|
|
|
func (s *dbs) vsKey(height int64) []byte {
|
|
return []byte(fmt.Sprintf("vs/%s/%010d", s.prefix, height))
|
|
}
|
|
|
|
var keyPattern = regexp.MustCompile(`^(sh|vs)/([^/]*)/([0-9]+)/$`)
|
|
|
|
func parseKey(key []byte) (part string, prefix string, height int64, ok bool) {
|
|
submatch := keyPattern.FindSubmatch(key)
|
|
if submatch == nil {
|
|
return "", "", 0, false
|
|
}
|
|
part = string(submatch[1])
|
|
prefix = string(submatch[2])
|
|
heightStr := string(submatch[3])
|
|
heightInt, err := strconv.Atoi(heightStr)
|
|
if err != nil {
|
|
return "", "", 0, false
|
|
}
|
|
height = int64(heightInt)
|
|
ok = true // good!
|
|
return
|
|
}
|
|
|
|
func parseShKey(key []byte) (prefix string, height int64, ok bool) {
|
|
var part string
|
|
part, prefix, height, ok = parseKey(key)
|
|
if part != "sh" {
|
|
return "", 0, false
|
|
}
|
|
return
|
|
}
|