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.

202 lines
6.9 KiB

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