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.

102 lines
2.5 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 proxy
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "github.com/pkg/errors"
  7. amino "github.com/tendermint/go-amino"
  8. "github.com/tendermint/tendermint/libs/log"
  9. tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
  10. lrpc "github.com/tendermint/tendermint/lite2/rpc"
  11. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  12. rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
  13. )
  14. // A Proxy defines parameters for running an HTTP server proxy.
  15. type Proxy struct {
  16. Addr string // TCP address to listen on, ":http" if empty
  17. Config *rpcserver.Config
  18. Codec *amino.Codec
  19. Client *lrpc.Client
  20. Logger log.Logger
  21. }
  22. // ListenAndServe configures the rpcserver.WebsocketManager, sets up the RPC
  23. // routes to proxy via Client, and starts up an HTTP server on the TCP network
  24. // address p.Addr.
  25. // See http#Server#ListenAndServe.
  26. func (p *Proxy) ListenAndServe() error {
  27. listener, mux, err := p.listen()
  28. if err != nil {
  29. return err
  30. }
  31. return rpcserver.StartHTTPServer(
  32. listener,
  33. mux,
  34. p.Logger,
  35. p.Config,
  36. )
  37. }
  38. // ListenAndServeTLS acts identically to ListenAndServe, except that it expects
  39. // HTTPS connections.
  40. // See http#Server#ListenAndServeTLS.
  41. func (p *Proxy) ListenAndServeTLS(certFile, keyFile string) error {
  42. listener, mux, err := p.listen()
  43. if err != nil {
  44. return err
  45. }
  46. return rpcserver.StartHTTPAndTLSServer(
  47. listener,
  48. mux,
  49. certFile,
  50. keyFile,
  51. p.Logger,
  52. p.Config,
  53. )
  54. }
  55. func (p *Proxy) listen() (net.Listener, *http.ServeMux, error) {
  56. ctypes.RegisterAmino(p.Codec)
  57. mux := http.NewServeMux()
  58. // 1) Register regular routes.
  59. r := RPCRoutes(p.Client)
  60. rpcserver.RegisterRPCFuncs(mux, r, p.Codec, p.Logger)
  61. // 2) Allow websocket connections.
  62. wmLogger := p.Logger.With("protocol", "websocket")
  63. wm := rpcserver.NewWebsocketManager(r, p.Codec,
  64. rpcserver.OnDisconnect(func(remoteAddr string) {
  65. err := p.Client.UnsubscribeAll(context.Background(), remoteAddr)
  66. if err != nil && err != tmpubsub.ErrSubscriptionNotFound {
  67. wmLogger.Error("Failed to unsubscribe addr from events", "addr", remoteAddr, "err", err)
  68. }
  69. }),
  70. rpcserver.ReadLimit(p.Config.MaxBodyBytes),
  71. )
  72. wm.SetLogger(wmLogger)
  73. mux.HandleFunc("/websocket", wm.WebsocketHandler)
  74. // 3) Start a client.
  75. if !p.Client.IsRunning() {
  76. if err := p.Client.Start(); err != nil {
  77. return nil, mux, errors.Wrap(err, "Client#Start")
  78. }
  79. }
  80. // 4) Start listening for new connections.
  81. listener, err := rpcserver.Listen(p.Addr, p.Config)
  82. if err != nil {
  83. return nil, mux, err
  84. }
  85. return listener, mux, nil
  86. }