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.

284 lines
7.9 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
2 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  1. package commands
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/spf13/cobra"
  8. dbm "github.com/tendermint/tm-db"
  9. abcitypes "github.com/tendermint/tendermint/abci/types"
  10. tmcfg "github.com/tendermint/tendermint/config"
  11. "github.com/tendermint/tendermint/internal/libs/progressbar"
  12. "github.com/tendermint/tendermint/internal/state"
  13. "github.com/tendermint/tendermint/internal/state/indexer"
  14. "github.com/tendermint/tendermint/internal/state/indexer/sink/kv"
  15. "github.com/tendermint/tendermint/internal/state/indexer/sink/psql"
  16. "github.com/tendermint/tendermint/internal/store"
  17. "github.com/tendermint/tendermint/libs/log"
  18. "github.com/tendermint/tendermint/libs/os"
  19. "github.com/tendermint/tendermint/rpc/coretypes"
  20. "github.com/tendermint/tendermint/types"
  21. )
  22. const (
  23. reindexFailed = "event re-index failed: "
  24. )
  25. // MakeReindexEventCommand constructs a command to re-index events in a block height interval.
  26. func MakeReindexEventCommand(conf *tmcfg.Config, logger log.Logger) *cobra.Command {
  27. var (
  28. startHeight int64
  29. endHeight int64
  30. )
  31. cmd := &cobra.Command{
  32. Use: "reindex-event",
  33. Short: "reindex events to the event store backends",
  34. Long: `
  35. reindex-event is an offline tooling to re-index block and tx events to the eventsinks,
  36. you can run this command when the event store backend dropped/disconnected or you want to
  37. replace the backend. The default start-height is 0, meaning the tooling will start
  38. reindex from the base block height(inclusive); and the default end-height is 0, meaning
  39. the tooling will reindex until the latest block height(inclusive). User can omit
  40. either or both arguments.
  41. `,
  42. Example: `
  43. tendermint reindex-event
  44. tendermint reindex-event --start-height 2
  45. tendermint reindex-event --end-height 10
  46. tendermint reindex-event --start-height 2 --end-height 10
  47. `,
  48. RunE: func(cmd *cobra.Command, args []string) error {
  49. bs, ss, err := loadStateAndBlockStore(conf)
  50. if err != nil {
  51. return fmt.Errorf("%s: %w", reindexFailed, err)
  52. }
  53. cvhArgs := checkValidHeightArgs{
  54. startHeight: startHeight,
  55. endHeight: endHeight,
  56. }
  57. if err := checkValidHeight(bs, cvhArgs); err != nil {
  58. return fmt.Errorf("%s: %w", reindexFailed, err)
  59. }
  60. es, err := loadEventSinks(conf)
  61. if err != nil {
  62. return fmt.Errorf("%s: %w", reindexFailed, err)
  63. }
  64. riArgs := eventReIndexArgs{
  65. startHeight: startHeight,
  66. endHeight: endHeight,
  67. sinks: es,
  68. blockStore: bs,
  69. stateStore: ss,
  70. }
  71. if err := eventReIndex(cmd, riArgs); err != nil {
  72. return fmt.Errorf("%s: %w", reindexFailed, err)
  73. }
  74. logger.Info("event re-index finished")
  75. return nil
  76. },
  77. }
  78. cmd.Flags().Int64Var(&startHeight, "start-height", 0, "the block height would like to start for re-index")
  79. cmd.Flags().Int64Var(&endHeight, "end-height", 0, "the block height would like to finish for re-index")
  80. return cmd
  81. }
  82. func loadEventSinks(cfg *tmcfg.Config) ([]indexer.EventSink, error) {
  83. // Check duplicated sinks.
  84. sinks := map[string]bool{}
  85. for _, s := range cfg.TxIndex.Indexer {
  86. sl := strings.ToLower(s)
  87. if sinks[sl] {
  88. return nil, errors.New("found duplicated sinks, please check the tx-index section in the config.toml")
  89. }
  90. sinks[sl] = true
  91. }
  92. eventSinks := []indexer.EventSink{}
  93. for k := range sinks {
  94. switch k {
  95. case string(indexer.NULL):
  96. return nil, errors.New("found null event sink, please check the tx-index section in the config.toml")
  97. case string(indexer.KV):
  98. store, err := tmcfg.DefaultDBProvider(&tmcfg.DBContext{ID: "tx_index", Config: cfg})
  99. if err != nil {
  100. return nil, err
  101. }
  102. eventSinks = append(eventSinks, kv.NewEventSink(store))
  103. case string(indexer.PSQL):
  104. conn := cfg.TxIndex.PsqlConn
  105. if conn == "" {
  106. return nil, errors.New("the psql connection settings cannot be empty")
  107. }
  108. es, err := psql.NewEventSink(conn, cfg.ChainID())
  109. if err != nil {
  110. return nil, err
  111. }
  112. eventSinks = append(eventSinks, es)
  113. default:
  114. return nil, errors.New("unsupported event sink type")
  115. }
  116. }
  117. if len(eventSinks) == 0 {
  118. return nil, errors.New("no proper event sink can do event re-indexing," +
  119. " please check the tx-index section in the config.toml")
  120. }
  121. if !indexer.IndexingEnabled(eventSinks) {
  122. return nil, fmt.Errorf("no event sink has been enabled")
  123. }
  124. return eventSinks, nil
  125. }
  126. func loadStateAndBlockStore(cfg *tmcfg.Config) (*store.BlockStore, state.Store, error) {
  127. dbType := dbm.BackendType(cfg.DBBackend)
  128. if !os.FileExists(filepath.Join(cfg.DBDir(), "blockstore.db")) {
  129. return nil, nil, fmt.Errorf("no blockstore found in %v", cfg.DBDir())
  130. }
  131. // Get BlockStore
  132. blockStoreDB, err := dbm.NewDB("blockstore", dbType, cfg.DBDir())
  133. if err != nil {
  134. return nil, nil, err
  135. }
  136. blockStore := store.NewBlockStore(blockStoreDB)
  137. if !os.FileExists(filepath.Join(cfg.DBDir(), "state.db")) {
  138. return nil, nil, fmt.Errorf("no blockstore found in %v", cfg.DBDir())
  139. }
  140. // Get StateStore
  141. stateDB, err := dbm.NewDB("state", dbType, cfg.DBDir())
  142. if err != nil {
  143. return nil, nil, err
  144. }
  145. stateStore := state.NewStore(stateDB)
  146. return blockStore, stateStore, nil
  147. }
  148. type eventReIndexArgs struct {
  149. startHeight int64
  150. endHeight int64
  151. sinks []indexer.EventSink
  152. blockStore state.BlockStore
  153. stateStore state.Store
  154. }
  155. func eventReIndex(cmd *cobra.Command, args eventReIndexArgs) error {
  156. var bar progressbar.Bar
  157. bar.NewOption(args.startHeight-1, args.endHeight)
  158. fmt.Println("start re-indexing events:")
  159. defer bar.Finish()
  160. for i := args.startHeight; i <= args.endHeight; i++ {
  161. select {
  162. case <-cmd.Context().Done():
  163. return fmt.Errorf("event re-index terminated at height %d: %w", i, cmd.Context().Err())
  164. default:
  165. b := args.blockStore.LoadBlock(i)
  166. if b == nil {
  167. return fmt.Errorf("not able to load block at height %d from the blockstore", i)
  168. }
  169. r, err := args.stateStore.LoadABCIResponses(i)
  170. if err != nil {
  171. return fmt.Errorf("not able to load ABCI Response at height %d from the statestore", i)
  172. }
  173. e := types.EventDataNewBlockHeader{
  174. Header: b.Header,
  175. NumTxs: int64(len(b.Txs)),
  176. ResultFinalizeBlock: *r.FinalizeBlock,
  177. }
  178. var batch *indexer.Batch
  179. if e.NumTxs > 0 {
  180. batch = indexer.NewBatch(e.NumTxs)
  181. for i := range b.Data.Txs {
  182. tr := abcitypes.TxResult{
  183. Height: b.Height,
  184. Index: uint32(i),
  185. Tx: b.Data.Txs[i],
  186. Result: *(r.FinalizeBlock.TxResults[i]),
  187. }
  188. _ = batch.Add(&tr)
  189. }
  190. }
  191. for _, sink := range args.sinks {
  192. if err := sink.IndexBlockEvents(e); err != nil {
  193. return fmt.Errorf("block event re-index at height %d failed: %w", i, err)
  194. }
  195. if batch != nil {
  196. if err := sink.IndexTxEvents(batch.Ops); err != nil {
  197. return fmt.Errorf("tx event re-index at height %d failed: %w", i, err)
  198. }
  199. }
  200. }
  201. }
  202. bar.Play(i)
  203. }
  204. return nil
  205. }
  206. type checkValidHeightArgs struct {
  207. startHeight int64
  208. endHeight int64
  209. }
  210. func checkValidHeight(bs state.BlockStore, args checkValidHeightArgs) error {
  211. base := bs.Base()
  212. if args.startHeight == 0 {
  213. args.startHeight = base
  214. fmt.Printf("set the start block height to the base height of the blockstore %d \n", base)
  215. }
  216. if args.startHeight < base {
  217. return fmt.Errorf("%s (requested start height: %d, base height: %d)",
  218. coretypes.ErrHeightNotAvailable, args.startHeight, base)
  219. }
  220. height := bs.Height()
  221. if args.startHeight > height {
  222. return fmt.Errorf(
  223. "%s (requested start height: %d, store height: %d)", coretypes.ErrHeightNotAvailable, args.startHeight, height)
  224. }
  225. if args.endHeight == 0 || args.endHeight > height {
  226. args.endHeight = height
  227. fmt.Printf("set the end block height to the latest height of the blockstore %d \n", height)
  228. }
  229. if args.endHeight < base {
  230. return fmt.Errorf(
  231. "%s (requested end height: %d, base height: %d)", coretypes.ErrHeightNotAvailable, args.endHeight, base)
  232. }
  233. if args.endHeight < args.startHeight {
  234. return fmt.Errorf(
  235. "%s (requested the end height: %d is less than the start height: %d)",
  236. coretypes.ErrInvalidRequest, args.startHeight, args.endHeight)
  237. }
  238. return nil
  239. }