cosmos-sdk/server
Amaury b49f948b36
feat: Add proto annotations for Amino JSON (#13501)
* add legacy_amino_name

* make-proto-gen

* remove useless omitempty

* add annotations

* Add proto annotations

* Add more annotations

* update cosmos-proto

* Add message scalar?

* Add comments

* Fix comment

* lint proto files

* proto-gen

* go mod tidy

* Add multisig encoding

* Add field name

* Format proto

* proto-gen

* Update proto/cosmos/msg/v1/msg.proto

Co-authored-by: Aaron Craelius <aaron@regen.network>

* Add dont_omitempty whenever we have nullable=false

* proto-gen

* Remove problematic annotations

* put legacy_amino in subpackage

* proto-gen

* Fixes

* legacy_amino.v1

* add non-working proto

* Generate in separate package

* Remove `cosmos.msg` prefix

* make proto-gen

* remove v1 too

* make proto-format

* Add field option

* format

* proto-gen

* Use underscores

* update legacy_amino -> amino

* update to `key_field`

* make proto-format

* make proto-gen

Co-authored-by: Aaron Craelius <aaron@regen.network>
2022-11-07 22:51:51 +00:00
..
api chore: use gateway fork (#13754) 2022-11-03 21:14:59 +00:00
cmd feat: bring back filter logging (#13236) 2022-09-12 16:08:13 +02:00
config docs: set api endpoints to localhost by default (#13778) 2022-11-07 11:06:06 +00:00
grpc feat: Add proto annotations for Amino JSON (#13501) 2022-11-07 22:51:51 +00:00
mock chore: move pruning to store (#13609) 2022-10-24 14:02:17 +02:00
types feat: gRPC query for operator and chain configuration (#13485) 2022-10-10 19:12:00 +00:00
README.md feat: migrate to docusaurus (#13471) 2022-10-10 14:01:53 +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!: specify module to export (#13437) 2022-10-04 10:06:30 +00:00
pruning.go chore: move pruning to store (#13609) 2022-10-24 14:02:17 +02:00
pruning_test.go chore: move pruning to store (#13609) 2022-10-24 14:02:17 +02:00
rollback.go fix: rollback command don't actually delete multistore versions (#11361) 2022-08-30 05:50:00 +00:00
start.go fix: misc fixes for cosmos-rosetta (#13583) 2022-11-04 11:48:45 +00:00
swagger.go refactor: helper method for register swagger api (#12955) 2022-08-18 12:19:27 +02:00
test_helpers.go server: wrap errors (#8879) 2021-03-15 13:50:12 +00:00
tm_cmds.go chore: downgrade to tendermint `v0.34.x` (#12958) 2022-08-20 02:33:07 +02:00
util.go fix: all: fix resource leaks found by staticmajor (#13394) 2022-10-06 20:11:40 +00:00
util_test.go perf: all: remove unnecessary allocations from strings.Builder.WriteString(fmt.Sprintf(...)) (#13230) 2022-09-09 21:57:46 +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()
	}

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

	return simapp.NewSimApp(
		logger, db, traceStore, true,
		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.