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.

103 lines
2.2 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 http
  2. import (
  3. "fmt"
  4. "github.com/tendermint/tendermint/lite2/provider"
  5. rpcclient "github.com/tendermint/tendermint/rpc/client"
  6. "github.com/tendermint/tendermint/types"
  7. )
  8. // SignStatusClient combines a SignClient and StatusClient.
  9. type SignStatusClient interface {
  10. rpcclient.SignClient
  11. rpcclient.StatusClient
  12. }
  13. // http provider uses an RPC client (or SignStatusClient more generally) to
  14. // obtain the necessary information.
  15. type http struct {
  16. chainID string
  17. client SignStatusClient
  18. }
  19. // New creates a HTTP provider, which is using the rpcclient.HTTP
  20. // client under the hood.
  21. func New(chainID, remote string) provider.Provider {
  22. return NewWithClient(chainID, rpcclient.NewHTTP(remote, "/websocket"))
  23. }
  24. // NewWithClient allows you to provide custom SignStatusClient.
  25. func NewWithClient(chainID string, client SignStatusClient) provider.Provider {
  26. return &http{
  27. chainID: chainID,
  28. client: client,
  29. }
  30. }
  31. func (p *http) ChainID() string {
  32. return p.chainID
  33. }
  34. func (p *http) SignedHeader(height int64) (*types.SignedHeader, error) {
  35. h, err := validateHeight(height)
  36. if err != nil {
  37. return nil, err
  38. }
  39. commit, err := p.client.Commit(h)
  40. if err != nil {
  41. return nil, err
  42. }
  43. // Verify we're still on the same chain.
  44. if p.chainID != commit.Header.ChainID {
  45. return nil, fmt.Errorf("expected chainID %s, got %s", p.chainID, commit.Header.ChainID)
  46. }
  47. return &commit.SignedHeader, nil
  48. }
  49. func (p *http) ValidatorSet(height int64) (*types.ValidatorSet, error) {
  50. h, err := validateHeight(height)
  51. if err != nil {
  52. return nil, err
  53. }
  54. const maxPerPage = 100
  55. res, err := p.client.Validators(h, 0, maxPerPage)
  56. if err != nil {
  57. return nil, err
  58. }
  59. var (
  60. vals = res.Validators
  61. page = 1
  62. )
  63. // Check if there are more validators.
  64. for len(res.Validators) == maxPerPage {
  65. res, err = p.client.Validators(h, page, maxPerPage)
  66. if err != nil {
  67. return nil, err
  68. }
  69. if len(res.Validators) > 0 {
  70. vals = append(vals, res.Validators...)
  71. }
  72. page++
  73. }
  74. return types.NewValidatorSet(vals), nil
  75. }
  76. func validateHeight(height int64) (*int64, error) {
  77. if height < 0 {
  78. return nil, fmt.Errorf("expected height >= 0, got height %d", height)
  79. }
  80. h := &height
  81. if height == 0 {
  82. h = nil
  83. }
  84. return h, nil
  85. }