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.

187 lines
5.9 KiB

  1. package statesync
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. dbm "github.com/tendermint/tm-db"
  7. "github.com/tendermint/tendermint/libs/log"
  8. tmsync "github.com/tendermint/tendermint/libs/sync"
  9. "github.com/tendermint/tendermint/light"
  10. lightprovider "github.com/tendermint/tendermint/light/provider"
  11. lighthttp "github.com/tendermint/tendermint/light/provider/http"
  12. lightrpc "github.com/tendermint/tendermint/light/rpc"
  13. lightdb "github.com/tendermint/tendermint/light/store/db"
  14. tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
  15. rpchttp "github.com/tendermint/tendermint/rpc/client/http"
  16. sm "github.com/tendermint/tendermint/state"
  17. "github.com/tendermint/tendermint/types"
  18. )
  19. //go:generate mockery -case underscore -name StateProvider
  20. // StateProvider is a provider of trusted state data for bootstrapping a node. This refers
  21. // to the state.State object, not the state machine.
  22. type StateProvider interface {
  23. // AppHash returns the app hash after the given height has been committed.
  24. AppHash(height uint64) ([]byte, error)
  25. // Commit returns the commit at the given height.
  26. Commit(height uint64) (*types.Commit, error)
  27. // State returns a state object at the given height.
  28. State(height uint64) (sm.State, error)
  29. }
  30. // lightClientStateProvider is a state provider using the light client.
  31. type lightClientStateProvider struct {
  32. tmsync.Mutex // light.Client is not concurrency-safe
  33. lc *light.Client
  34. version tmstate.Version
  35. initialHeight int64
  36. providers map[lightprovider.Provider]string
  37. }
  38. // NewLightClientStateProvider creates a new StateProvider using a light client and RPC clients.
  39. func NewLightClientStateProvider(
  40. chainID string,
  41. version tmstate.Version,
  42. initialHeight int64,
  43. servers []string,
  44. trustOptions light.TrustOptions,
  45. logger log.Logger,
  46. ) (StateProvider, error) {
  47. if len(servers) < 2 {
  48. return nil, fmt.Errorf("at least 2 RPC servers are required, got %v", len(servers))
  49. }
  50. providers := make([]lightprovider.Provider, 0, len(servers))
  51. providerRemotes := make(map[lightprovider.Provider]string)
  52. for _, server := range servers {
  53. client, err := rpcClient(server)
  54. if err != nil {
  55. return nil, fmt.Errorf("failed to set up RPC client: %w", err)
  56. }
  57. provider := lighthttp.NewWithClient(chainID, client)
  58. providers = append(providers, provider)
  59. // We store the RPC addresses keyed by provider, so we can find the address of the primary
  60. // provider used by the light client and use it to fetch consensus parameters.
  61. providerRemotes[provider] = server
  62. }
  63. lc, err := light.NewClient(chainID, trustOptions, providers[0], providers[1:],
  64. lightdb.New(dbm.NewMemDB(), ""), light.Logger(logger), light.MaxRetryAttempts(5))
  65. if err != nil {
  66. return nil, err
  67. }
  68. return &lightClientStateProvider{
  69. lc: lc,
  70. version: version,
  71. initialHeight: initialHeight,
  72. providers: providerRemotes,
  73. }, nil
  74. }
  75. // AppHash implements StateProvider.
  76. func (s *lightClientStateProvider) AppHash(height uint64) ([]byte, error) {
  77. s.Lock()
  78. defer s.Unlock()
  79. // We have to fetch the next height, which contains the app hash for the previous height.
  80. header, err := s.lc.VerifyHeaderAtHeight(int64(height+1), time.Now())
  81. if err != nil {
  82. return nil, err
  83. }
  84. return header.AppHash, nil
  85. }
  86. // Commit implements StateProvider.
  87. func (s *lightClientStateProvider) Commit(height uint64) (*types.Commit, error) {
  88. s.Lock()
  89. defer s.Unlock()
  90. header, err := s.lc.VerifyHeaderAtHeight(int64(height), time.Now())
  91. if err != nil {
  92. return nil, err
  93. }
  94. return header.Commit, nil
  95. }
  96. // State implements StateProvider.
  97. func (s *lightClientStateProvider) State(height uint64) (sm.State, error) {
  98. s.Lock()
  99. defer s.Unlock()
  100. state := sm.State{
  101. ChainID: s.lc.ChainID(),
  102. Version: s.version,
  103. InitialHeight: s.initialHeight,
  104. }
  105. if state.InitialHeight == 0 {
  106. state.InitialHeight = 1
  107. }
  108. // We need to verify up until h+2, to get the validator set. This also prefetches the headers
  109. // for h and h+1 in the typical case where the trusted header is after the snapshot height.
  110. _, err := s.lc.VerifyHeaderAtHeight(int64(height+2), time.Now())
  111. if err != nil {
  112. return sm.State{}, err
  113. }
  114. header, err := s.lc.VerifyHeaderAtHeight(int64(height), time.Now())
  115. if err != nil {
  116. return sm.State{}, err
  117. }
  118. nextHeader, err := s.lc.VerifyHeaderAtHeight(int64(height+1), time.Now())
  119. if err != nil {
  120. return sm.State{}, err
  121. }
  122. state.LastBlockHeight = header.Height
  123. state.LastBlockTime = header.Time
  124. state.LastBlockID = header.Commit.BlockID
  125. state.AppHash = nextHeader.AppHash
  126. state.LastResultsHash = nextHeader.LastResultsHash
  127. state.LastValidators, _, err = s.lc.TrustedValidatorSet(int64(height))
  128. if err != nil {
  129. return sm.State{}, err
  130. }
  131. state.Validators, _, err = s.lc.TrustedValidatorSet(int64(height + 1))
  132. if err != nil {
  133. return sm.State{}, err
  134. }
  135. state.NextValidators, _, err = s.lc.TrustedValidatorSet(int64(height + 2))
  136. if err != nil {
  137. return sm.State{}, err
  138. }
  139. state.LastHeightValidatorsChanged = int64(height)
  140. // We'll also need to fetch consensus params via RPC, using light client verification.
  141. primaryURL, ok := s.providers[s.lc.Primary()]
  142. if !ok || primaryURL == "" {
  143. return sm.State{}, fmt.Errorf("could not find address for primary light client provider")
  144. }
  145. primaryRPC, err := rpcClient(primaryURL)
  146. if err != nil {
  147. return sm.State{}, fmt.Errorf("unable to create RPC client: %w", err)
  148. }
  149. rpcclient := lightrpc.NewClient(primaryRPC, s.lc)
  150. result, err := rpcclient.ConsensusParams(&nextHeader.Height)
  151. if err != nil {
  152. return sm.State{}, fmt.Errorf("unable to fetch consensus parameters for height %v: %w",
  153. nextHeader.Height, err)
  154. }
  155. state.ConsensusParams = result.ConsensusParams
  156. return state, nil
  157. }
  158. // rpcClient sets up a new RPC client
  159. func rpcClient(server string) (*rpchttp.HTTP, error) {
  160. if !strings.Contains(server, "://") {
  161. server = "http://" + server
  162. }
  163. c, err := rpchttp.New(server, "/websocket")
  164. if err != nil {
  165. return nil, err
  166. }
  167. return c, nil
  168. }