cosmos-sdk/server
Robert Zaremba 3a9e696bbf
fix: Signature only flag bug on tx sign command 7632 (#8106)
* fix: Signature only flag bug on tx sign command 7632

* Update client/context.go

Co-authored-by: Cory <cjlevinson@gmail.com>

* Update client/context.go

Co-authored-by: Cory <cjlevinson@gmail.com>

* use named return value and closure (#8111)

This is to correctly handle deferred Close()
calls on writable files.

* set the right 'append' logic for signing transactions

* cleanup

* update tx.Sign interface by adding overwrite option

* Update Changelog

* sign command cleanup

* implementation and changelog update

* fix SignTx and tx.Sign calls

* fix: sign didn't write to a file

* update flags description

* Add tx.Sign tests

* fix grpc/server_test.go

* Update client/tx/tx.go

Co-authored-by: Cory <cjlevinson@gmail.com>

* changelog update

* Add test to verify matching signatures

* cli_test: add integration tests for sign CMD

* add output-file flag test

* add flagAmino test

* Update x/auth/client/cli/tx_sign.go

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* Update x/auth/client/cli/tx_sign.go

* update amino serialization test

* TestSign: adding unit test for signing with different modes

* Add test with Multi Signers into Robert's TxSign PR (#8142)

* Add test with Multi Signers

* remove true false

* Use SIGN_MODE_DIRECT

* Fix litn

* Use correct pubkeys

* Correct accNum and seq

* Use amino

* cleanups

* client.Sign: raise error when signing tx with multiple signers in Direct

+ added more unit tests

* add more tests

* Update client/tx/tx_test.go

Co-authored-by: Cory <cjlevinson@gmail.com>

* fix TestGetBroadcastCommand_WithoutOfflineFlag

* Any.UnsafeSetCachedValue

* fix note packed messages in tx builder

* reorder unit tests

* Changelog update

* cleaning / linting

* cli_tes: copy validator object instead of modifying it's shared codec

* x/auth cli_test: remove custom codec creation in tests

* Update CHANGELOG.md

* updates to CHANGELOG.md

* remove unused method

* add new instance of transaction builder for TestSign

Co-authored-by: Cory <cjlevinson@gmail.com>
Co-authored-by: SaReN <sahithnarahari@gmail.com>
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Amaury <amaury.martiny@protonmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-12-14 21:44:15 +00:00
..
api Rename GRPCRouter (#8079) 2020-12-04 15:06:50 +00:00
cmd server: init commit (#8144) 2020-12-10 19:12:42 +00:00
config Register swagger API (#7246) 2020-09-19 00:34:56 +00:00
grpc fix: Signature only flag bug on tx sign command 7632 (#8106) 2020-12-14 21:44:15 +00:00
mock Merge PR #7265: Tendermint Block Pruning 2020-09-14 10:12:49 -04:00
types gRPC-gateway routes as alternative to legacy tendermint REST endpoints (#7965) 2020-11-25 15:58:11 +00:00
README.md simapp: rename MakeEncodingConfig to MakeTestEncodingConfig (#7681) 2020-10-27 11:33:48 +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 fix: Rework InterceptConfigsPreRunHandler to make sure configuration … (#7501) 2020-10-16 13:56:10 +00:00
export.go Fixed help message for export's 'jail-whitelist' command to be clearer (#7885) 2020-11-13 12:53:18 +00:00
export_test.go Update x/banking and x/crisis InitChain re slow Gaia startup (#7764) 2020-11-02 19:10:14 +00:00
init.go custom testnet cmd signing algorithms (#7012) 2020-08-12 08:34:10 +00:00
init_test.go replace testutil.NewTestCaseDir() with Go1.15's T.TempDir() (#7014) 2020-09-18 12:08:24 +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
start.go make it exit gracefully. (#8101) 2020-12-10 11:37:57 +00:00
test_helpers.go Server/simd: Viper Removal (#6599) 2020-07-05 16:56:17 +00:00
tm_cmds.go Replace tmcrypto.PubKey by our own cryptotypes.PubKey (#7419) 2020-11-09 16:01:43 +00:00
util.go Refactor Logging using Zerolog (#8072) 2020-12-03 23:17:21 +00:00
util_test.go Rename GRPCRouter (#8079) 2020-12-04 15:06:50 +00:00

README.md

Server

The server package is responsible for providing the mechanisms necessary to start an ABCI Tendermint application and providing the CLI framework necessary to fully bootstrap an application. The package exposes two core commands, StartCmd and ExportCmd.

Preliminary

The root command of an application typically is constructed with three core sub-commands, query commands, tx commands, and auxiliary commands such as genesis utilities, and starting an application binary.

It is vital that the root command of an application set the appropriate PersistentPreRun(E) function 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(E) functions. Note, 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 (
	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)
		},
	}

	encodingConfig = simapp.MakeTestEncodingConfig()
	initClientCtx  = client.Context{}.
			WithJSONMarshaler(encodingConfig.Marshaler).
			WithTxConfig(encodingConfig.TxConfig).
			WithCodec(encodingConfig.Amino).
			WithInput(os.Stdin).
			WithAccountRetriever(types.NewAccountRetriever(encodingConfig.Marshaler)).
			WithBroadcastMode(flags.BroadcastBlock).
			WithHomeDir(simapp.DefaultNodeHome)
)

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.