cosmos-sdk/server
mergify[bot] a3f8a83ec4
chore(types): add MustAccAddressFromBech32 util func (backport #12201) (#12205)
2022-06-09 15:01:04 -04:00
..
api fix: data race issues with api.Server (backport #11724) (#11748) 2022-04-25 10:41:34 -04:00
cmd server: init commit (#8144) 2020-12-10 19:12:42 +00:00
config refactor: prune everything (backport #11177) (#11258) 2022-02-23 23:22:10 +01:00
grpc fix!: remove grpc query routing through tendermint (backport #10045) (#10269) 2021-10-04 19:24:16 +02:00
mock feat!: Add hooks to allow app modules to add things to state-sync (backport #10961) (#11267) 2022-03-02 12:18:23 +01:00
rosetta chore(types): add MustAccAddressFromBech32 util func (backport #12201) (#12205) 2022-06-09 15:01:04 -04:00
types fix!: remove grpc query routing through tendermint (backport #10045) (#10269) 2021-10-04 19:24:16 +02:00
README.md chore: add markdownlint to lint commands (#9353) 2021-05-27 15:31:04 +00:00
constructors_test.go replace testutil.NewTestCaseDir() with Go1.15's T.TempDir() (#7014) 2020-09-18 12:08:24 +01:00
doc.go feat: Allow app developers to override default appConfig template (#9550) 2021-06-23 08:42:39 +00:00
export.go Set proper default command output (#8628) 2021-04-17 00:21:32 +00:00
export_test.go refactor(client): add client/Context.Codec and deprecate JSONCodec (#9498) 2021-06-11 11:49:39 +00:00
init.go chore: move server.GenerateCoinKey and server.GenerateSaveCoinKey to testutil (#10956) 2022-01-18 15:37:05 +01:00
logger.go Refactor Logging using Zerolog (#8072) 2020-12-03 23:17:21 +00:00
pruning.go Add gRPC server & reflection (#6463) 2020-07-27 17:57:15 +00:00
pruning_test.go Server/simd: Viper Removal (#6599) 2020-07-05 16:56:17 +00:00
rollback.go Implement rollback command (#11179) (#11314) 2022-03-07 11:46:00 +01:00
rosetta.go chore: Cleanup Changelog, add Rosetta to Release Notes as beta (#9691) 2021-07-14 21:37:13 +02:00
start.go check error returned from NewNode (#11624) 2022-04-13 10:17:38 +02:00
test_helpers.go server: wrap errors (#8879) 2021-03-15 13:50:12 +00:00
tm_cmds.go chore: update TM dep (#11562) 2022-04-08 09:15:43 -04:00
util.go chore: update TM dep (#11562) 2022-04-08 09:15:43 -04:00
util_test.go fix: Don't error on startup if min-gas-price is empty (#9621) 2021-07-02 17:35:31 +02: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.