cosmos-sdk/server
Bryce Neal 97d735f15c
fix: data race issues with api.Server (#11724)
## Description

Closes: #11692

This PR adds a `sync.Mutex` to `api.Server` in order to prevent data race issues. In practice, these data races are unlikely to occur as you'd typically wait for Start to return. However, this does cause an issue with testing using the `-race` flag when using the `testutil/network` package. Currently races occur by default when running tests on projects generated using `ignite`. We should fix this for the sake of passing race detecting tests.

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [X] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [X] added `!` to the type prefix if API or client breaking change
- [X] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [X] provided a link to the relevant issue or specification
- [X] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [X] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [X] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [X] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-04-25 08:52:12 +00:00
..
api fix: data race issues with api.Server (#11724) 2022-04-25 08:52:12 +00:00
cmd fix(server/cmd)!: set environment prefix (#10950) 2022-01-20 14:04:21 +00:00
config refactor!: abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests (#11496) 2022-04-21 15:30:36 -04:00
grpc ci: improve error checking (errcheck linter) (#11195) 2022-04-13 06:46:51 +00:00
mock refactor!: abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests (#11496) 2022-04-21 15:30:36 -04:00
rosetta refactor: Change SignerData.SignerIndex to PubKey (#10692) 2021-12-08 11:43:15 +00:00
types chore: bump TM to v0.35.0 release candidate (#10210) 2021-11-16 11:24:38 -08:00
README.md docs: Improve markdownlint configuration (#11104) 2022-02-10 12:07:01 +00:00
constructors_test.go feat(types): Deprecate the DBBackend variable in favor of new app-db-backend config entry (#11188) 2022-03-18 10:26:20 +01:00
doc.go feat: Allow app developers to override default appConfig template (#9550) 2021-06-23 08:42:39 +00:00
export.go feat(types): Deprecate the DBBackend variable in favor of new app-db-backend config entry (#11188) 2022-03-18 10:26:20 +01:00
export_test.go chore: bump TM to v0.35.0 release candidate (#10210) 2021-11-16 11:24:38 -08:00
logger.go Refactor Logging using Zerolog (#8072) 2020-12-03 23:17:21 +00:00
pruning.go refactor!: abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests (#11496) 2022-04-21 15:30:36 -04:00
pruning_test.go refactor!: abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests (#11496) 2022-04-21 15:30:36 -04:00
rollback.go refactor!: abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests (#11496) 2022-04-21 15:30:36 -04:00
rosetta.go rosetta: upgrade to newest version (#9314) 2021-05-15 00:18:59 +02:00
start.go refactor!: abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests (#11496) 2022-04-21 15:30:36 -04:00
test_helpers.go server: wrap errors (#8879) 2021-03-15 13:50:12 +00:00
tm_cmds.go build(deps): Bump github.com/tendermint/tendermint from 0.35.2 to 0.35.3 (#11599) 2022-04-12 05:19:38 +00:00
util.go ci: improve error checking (errcheck linter) (#11195) 2022-04-13 06:46:51 +00:00
util_test.go ci: improve error checking (errcheck linter) (#11195) 2022-04-13 06:46:51 +00:00

README.md

Server

The server package is responsible for providing the mechanisms necessary to start an ABCI Tendermint application and provides the CLI framework (based on cobra) necessary to fully bootstrap an application. The package exposes two core functions: StartCmd and ExportCmd which creates commands to start the application and export state respectively.

Preliminary

The root command of an application typically is constructed with:

  • command to start an application binary
  • three meta commands: query, tx, and a few auxiliary commands such as genesis. utilities.

It is vital that the root command of an application uses PersistentPreRun() cobra command property for executing the command, so all child commands have access to the server and client contexts. These contexts are set as their default values initially and maybe modified, scoped to the command, in their respective PersistentPreRun() functions. Note that the client.Context is typically pre-populated with "default" values that may be useful for all commands to inherit and override if necessary.

Example:

var (
	initClientCtx  = client.Context{...}

	rootCmd = &cobra.Command{
		Use:   "simd",
		Short: "simulation app",
		PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
			if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
				return err
			}

			return server.InterceptConfigsPreRunHandler(cmd)
		},
	}
    // add root sub-commands ...
)

The SetCmdClientContextHandler call reads persistent flags via ReadPersistentCommandFlags which creates a client.Context and sets that on the root command's Context.

The InterceptConfigsPreRunHandler call creates a viper literal, default server.Context, and a logger and sets that on the root command's Context. The server.Context will be modified and saved to disk via the internal interceptConfigs call, which either reads or creates a Tendermint configuration based on the home path provided. In addition, interceptConfigs also reads and loads the application configuration, app.toml, and binds that to the server.Context viper literal. This is vital so the application can get access to not only the CLI flags, but also to the application configuration values provided by this file.

StartCmd

The StartCmd accepts an AppCreator function which returns an Application. The AppCreator is responsible for constructing the application based on the options provided to it via AppOptions. The AppOptions interface type defines a single method, Get() interface{}, and is implemented as a viper literal that exists in the server.Context. All the possible options an application may use and provide to the construction process are defined by the StartCmd and by the application's config file, app.toml.

The application can either be started in-process or as an external process. The former creates a Tendermint service and the latter creates a Tendermint Node.

Under the hood, StartCmd will call GetServerContextFromCmd, which provides the command access to a server.Context. This context provides access to the viper literal, the Tendermint config and logger. This allows flags to be bound the viper literal and passed to the application construction.

Example:

func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts server.AppOptions) server.Application {
	var cache sdk.MultiStorePersistentCache

	if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) {
		cache = store.NewCommitKVStoreCacheManager()
	}

	skipUpgradeHeights := make(map[int64]bool)
	for _, h := range cast.ToIntSlice(appOpts.Get(server.FlagUnsafeSkipUpgrades)) {
		skipUpgradeHeights[int64(h)] = true
	}

	pruningOpts, err := server.GetPruningOptionsFromFlags(appOpts)
	if err != nil {
		panic(err)
	}

	return simapp.NewSimApp(
		logger, db, traceStore, true, skipUpgradeHeights,
		cast.ToString(appOpts.Get(flags.FlagHome)),
		cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
		baseapp.SetPruning(pruningOpts),
		baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(server.FlagMinGasPrices))),
		baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))),
		baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))),
		baseapp.SetInterBlockCache(cache),
		baseapp.SetTrace(cast.ToBool(appOpts.Get(server.FlagTrace))),
	)
}

Note, some of the options provided are exposed via CLI flags in the start command and some are also allowed to be set in the application's app.toml. It is recommend to use the cast package for type safety guarantees and due to the limitations of CLI flag types.