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.

76 lines
1.6 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 lite
  2. import (
  3. "time"
  4. "github.com/tendermint/tendermint/types"
  5. )
  6. // AutoClient can auto update itself by fetching headers every N seconds.
  7. type AutoClient struct {
  8. base *Client
  9. updatePeriod time.Duration
  10. quit chan struct{}
  11. trustedHeaders chan *types.SignedHeader
  12. err chan error
  13. }
  14. // NewAutoClient creates a new client and starts a polling goroutine.
  15. func NewAutoClient(base *Client, updatePeriod time.Duration) *AutoClient {
  16. c := &AutoClient{
  17. base: base,
  18. updatePeriod: updatePeriod,
  19. quit: make(chan struct{}),
  20. trustedHeaders: make(chan *types.SignedHeader),
  21. err: make(chan error),
  22. }
  23. go c.autoUpdate()
  24. return c
  25. }
  26. // TrustedHeaders returns a channel onto which new trusted headers are posted.
  27. func (c *AutoClient) TrustedHeaders() <-chan *types.SignedHeader {
  28. return c.trustedHeaders
  29. }
  30. // Err returns a channel onto which errors are posted.
  31. func (c *AutoClient) Err() <-chan error {
  32. return c.err
  33. }
  34. // Stop stops the client.
  35. func (c *AutoClient) Stop() {
  36. close(c.quit)
  37. }
  38. func (c *AutoClient) autoUpdate() {
  39. lastTrustedHeight, err := c.base.LastTrustedHeight()
  40. if err != nil {
  41. c.err <- err
  42. return
  43. }
  44. ticker := time.NewTicker(c.updatePeriod)
  45. defer ticker.Stop()
  46. for {
  47. select {
  48. case <-ticker.C:
  49. err := c.base.VerifyHeaderAtHeight(lastTrustedHeight+1, time.Now())
  50. if err != nil {
  51. c.err <- err
  52. continue
  53. }
  54. h, err := c.base.TrustedHeader(lastTrustedHeight+1, time.Now())
  55. if err != nil {
  56. c.err <- err
  57. continue
  58. }
  59. c.trustedHeaders <- h
  60. lastTrustedHeight = h.Height
  61. case <-c.quit:
  62. return
  63. }
  64. }
  65. }