diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c8ccf61811..3641552f84a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -120,6 +120,9 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### API Breaking Changes +* (client) [#20976](https://github.com/cosmos/cosmos-sdk/pull/20976) Simplified command initialization by removing unnecessary parameters such as `txConfig` and `addressCodec`. + * Remove parameter `txConfig` from `genutilcli.Commands`,`genutilcli.CommandsWithCustomMigrationMap`,`genutilcli.GenTxCmd`. + * Remove parameter `addressCodec` from `genutilcli.GenTxCmd`,`genutilcli.AddGenesisAccountCmd`,`stakingcli.BuildCreateValidatorMsg`. * (x/genutil) [#20740](https://github.com/cosmos/cosmos-sdk/pull/20740) Update `genutilcli.Commands` and `genutilcli.CommandsWithCustomMigrationMap` to take the genesis module and abstract the module manager. * (server) [#20422](https://github.com/cosmos/cosmos-sdk/pull/20422) Deprecated `ServerContext`. To get `cmtcfg.Config` from cmd, use `client.GetCometConfigFromCmd(cmd)` instead of `server.GetServerContextFromCmd(cmd).Config` * (types)[#20369](https://github.com/cosmos/cosmos-sdk/pull/20369) The signature of `HasAminoCodec` has changed to accept a `core/legacy.Amino` interface instead of `codec.LegacyAmino`. diff --git a/simapp/simd/cmd/commands.go b/simapp/simd/cmd/commands.go index 60c5808f25b..67175d3ee31 100644 --- a/simapp/simd/cmd/commands.go +++ b/simapp/simd/cmd/commands.go @@ -31,7 +31,6 @@ import ( func initRootCmd( rootCmd *cobra.Command, - txConfig client.TxConfig, moduleManager *module.Manager, ) { cfg := sdk.GetConfig() @@ -51,7 +50,7 @@ func initRootCmd( // add keybase, auxiliary RPC, query, genesis, and tx child commands rootCmd.AddCommand( server.StatusCommand(), - genesisCommand(txConfig, moduleManager, appExport), + genesisCommand(moduleManager, appExport), queryCommand(), txCommand(), keys.Commands(), @@ -60,8 +59,8 @@ func initRootCmd( } // genesisCommand builds genesis-related `simd genesis` command. Users may provide application specific commands as a parameter -func genesisCommand(txConfig client.TxConfig, moduleManager *module.Manager, appExport servertypes.AppExporter, cmds ...*cobra.Command) *cobra.Command { - cmd := genutilcli.Commands(txConfig, moduleManager.Modules[genutiltypes.ModuleName].(genutil.AppModule), moduleManager, appExport) +func genesisCommand(moduleManager *module.Manager, appExport servertypes.AppExporter, cmds ...*cobra.Command) *cobra.Command { + cmd := genutilcli.Commands(moduleManager.Modules[genutiltypes.ModuleName].(genutil.AppModule), moduleManager, appExport) for _, subCmd := range cmds { cmd.AddCommand(subCmd) } diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 007bdd5b938..5734452f0bd 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -109,7 +109,7 @@ func NewRootCmd() *cobra.Command { }, } - initRootCmd(rootCmd, encodingConfig.TxConfig, tempApp.ModuleManager) + initRootCmd(rootCmd, tempApp.ModuleManager) // autocli opts customClientTemplate, customClientConfig := initClientConfig() diff --git a/simapp/simd/cmd/root_di.go b/simapp/simd/cmd/root_di.go index 4ed5437d163..08fc997af43 100644 --- a/simapp/simd/cmd/root_di.go +++ b/simapp/simd/cmd/root_di.go @@ -81,7 +81,7 @@ func NewRootCmd() *cobra.Command { }, } - initRootCmd(rootCmd, clientCtx.TxConfig, moduleManager) + initRootCmd(rootCmd, moduleManager) if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil { panic(err) diff --git a/simapp/v2/simdv2/cmd/commands.go b/simapp/v2/simdv2/cmd/commands.go index 8f2edc75b8e..d638b2e9fe1 100644 --- a/simapp/v2/simdv2/cmd/commands.go +++ b/simapp/v2/simdv2/cmd/commands.go @@ -102,7 +102,7 @@ func initRootCmd[T transaction.Tx]( // add keybase, auxiliary RPC, query, genesis, and tx child commands rootCmd.AddCommand( - genesisCommand[T](txConfig, moduleManager, appExport[T]), + genesisCommand[T](moduleManager, appExport[T]), queryCommand(), txCommand(), keys.Commands(), @@ -123,7 +123,6 @@ func initRootCmd[T transaction.Tx]( // genesisCommand builds genesis-related `simd genesis` command. Users may provide application specific commands as a parameter func genesisCommand[T transaction.Tx]( - txConfig client.TxConfig, moduleManager *runtimev2.MM[T], appExport func(logger log.Logger, height int64, @@ -143,7 +142,7 @@ func genesisCommand[T transaction.Tx]( return appExport(logger, height, forZeroHeight, jailAllowedAddrs, viperAppOpts, modulesToExport) } - cmd := genutilcli.Commands(txConfig, moduleManager.Modules()[genutiltypes.ModuleName].(genutil.AppModule), moduleManager, compatAppExporter) + cmd := genutilcli.Commands(moduleManager.Modules()[genutiltypes.ModuleName].(genutil.AppModule), moduleManager, compatAppExporter) for _, subCmd := range cmds { cmd.AddCommand(subCmd) } diff --git a/x/genutil/client/cli/commands.go b/x/genutil/client/cli/commands.go index 6e0c50ee608..4793db294eb 100644 --- a/x/genutil/client/cli/commands.go +++ b/x/genutil/client/cli/commands.go @@ -21,13 +21,13 @@ type genesisMM interface { } // Commands adds core sdk's sub-commands into genesis command. -func Commands(txConfig client.TxConfig, genutilModule genutil.AppModule, genMM genesisMM, appExport servertypes.AppExporter) *cobra.Command { - return CommandsWithCustomMigrationMap(txConfig, genutilModule, genMM, appExport, MigrationMap) +func Commands(genutilModule genutil.AppModule, genMM genesisMM, appExport servertypes.AppExporter) *cobra.Command { + return CommandsWithCustomMigrationMap(genutilModule, genMM, appExport, MigrationMap) } // CommandsWithCustomMigrationMap adds core sdk's sub-commands into genesis command with custom migration map. // This custom migration map can be used by the application to add its own migration map. -func CommandsWithCustomMigrationMap(txConfig client.TxConfig, genutilModule genutil.AppModule, genMM genesisMM, appExport servertypes.AppExporter, migrationMap genutiltypes.MigrationMap) *cobra.Command { +func CommandsWithCustomMigrationMap(genutilModule genutil.AppModule, genMM genesisMM, appExport servertypes.AppExporter, migrationMap genutiltypes.MigrationMap) *cobra.Command { cmd := &cobra.Command{ Use: "genesis", Short: "Application's genesis-related subcommands", @@ -36,11 +36,11 @@ func CommandsWithCustomMigrationMap(txConfig client.TxConfig, genutilModule genu RunE: client.ValidateCmd, } cmd.AddCommand( - GenTxCmd(genMM, txConfig, banktypes.GenesisBalancesIterator{}, txConfig.SigningContext().ValidatorAddressCodec()), + GenTxCmd(genMM, banktypes.GenesisBalancesIterator{}), MigrateGenesisCmd(migrationMap), CollectGenTxsCmd(genutilModule.GenTxValidator()), ValidateGenesisCmd(genMM), - AddGenesisAccountCmd(txConfig.SigningContext().AddressCodec()), + AddGenesisAccountCmd(), ExportCmd(appExport), ) diff --git a/x/genutil/client/cli/genaccount.go b/x/genutil/client/cli/genaccount.go index 8debe3f97a6..bafd4f2f942 100644 --- a/x/genutil/client/cli/genaccount.go +++ b/x/genutil/client/cli/genaccount.go @@ -6,8 +6,6 @@ import ( "github.com/spf13/cobra" - "cosmossdk.io/core/address" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -25,7 +23,7 @@ const ( // AddGenesisAccountCmd returns add-genesis-account cobra Command. // This command is provided as a default, applications are expected to provide their own command if custom genesis accounts are needed. -func AddGenesisAccountCmd(addressCodec address.Codec) *cobra.Command { +func AddGenesisAccountCmd() *cobra.Command { cmd := &cobra.Command{ Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]", Short: "Add a genesis account to genesis.json", @@ -39,6 +37,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa clientCtx := client.GetClientContextFromCmd(cmd) config := client.GetConfigFromCmd(cmd) + addressCodec := clientCtx.TxConfig.SigningContext().AddressCodec() var kr keyring.Keyring addr, err := addressCodec.StringToBytes(args[0]) if err != nil { diff --git a/x/genutil/client/cli/genaccount_test.go b/x/genutil/client/cli/genaccount_test.go index d2c6c08b674..9acf60e29ad 100644 --- a/x/genutil/client/cli/genaccount_test.go +++ b/x/genutil/client/cli/genaccount_test.go @@ -12,7 +12,6 @@ import ( "cosmossdk.io/x/auth" "github.com/cosmos/cosmos-sdk/client" - addresscodec "github.com/cosmos/cosmos-sdk/codec/address" codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -71,15 +70,18 @@ func TestAddGenesisAccountCmd(t *testing.T) { t.Run(tc.name, func(t *testing.T) { home := t.TempDir() logger := log.NewNopLogger() - viper := viper.New() + v := viper.New() - appCodec := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, auth.AppModule{}).Codec + encodingConfig := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, auth.AppModule{}) + appCodec := encodingConfig.Codec + txConfig := encodingConfig.TxConfig err = genutiltest.ExecInitCmd(testMbm, home, appCodec) require.NoError(t, err) - err := writeAndTrackDefaultConfig(viper, home) + err := writeAndTrackDefaultConfig(v, home) require.NoError(t, err) - clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home).WithAddressCodec(ac) + clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home). + WithAddressCodec(ac).WithTxConfig(txConfig) if tc.withKeyring { path := hd.CreateHDPath(118, 0, 0).String() @@ -92,10 +94,10 @@ func TestAddGenesisAccountCmd(t *testing.T) { ctx := context.Background() ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) - ctx = context.WithValue(ctx, corectx.ViperContextKey, viper) + ctx = context.WithValue(ctx, corectx.ViperContextKey, v) ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger) - cmd := genutilcli.AddGenesisAccountCmd(addresscodec.NewBech32Codec("cosmos")) + cmd := genutilcli.AddGenesisAccountCmd() cmd.SetArgs([]string{ tc.addr, tc.denom, diff --git a/x/genutil/client/cli/gentx.go b/x/genutil/client/cli/gentx.go index 695eadcb240..8e130286332 100644 --- a/x/genutil/client/cli/gentx.go +++ b/x/genutil/client/cli/gentx.go @@ -11,7 +11,6 @@ import ( "github.com/spf13/cobra" - "cosmossdk.io/core/address" "cosmossdk.io/errors" authclient "cosmossdk.io/x/auth/client" "cosmossdk.io/x/staking/client/cli" @@ -28,7 +27,7 @@ import ( ) // GenTxCmd builds the application's gentx command. -func GenTxCmd(genMM genesisMM, txEncCfg client.TxEncodingConfig, genBalIterator types.GenesisBalancesIterator, valAdddressCodec address.Codec) *cobra.Command { +func GenTxCmd(genMM genesisMM, genBalIterator types.GenesisBalancesIterator) *cobra.Command { ipDefault, _ := server.ExternalIP() fsCreateValidator, defaultsDesc := cli.CreateValidatorMsgFlagSet(ipDefault) @@ -139,11 +138,7 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o return err } - pub, err := key.GetAddress() - if err != nil { - return err - } - clientCtx = clientCtx.WithInput(inBuf).WithFromAddress(pub) + clientCtx = clientCtx.WithInput(inBuf).WithFromAddress(addr) // The following line comes from a discrepancy between the `gentx` // and `create-validator` commands: @@ -159,7 +154,7 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o createValCfg.Amount = amount // create a 'create-validator' message - txBldr, msg, err := cli.BuildCreateValidatorMsg(clientCtx, createValCfg, txFactory, true, valAdddressCodec) + txBldr, msg, err := cli.BuildCreateValidatorMsg(clientCtx, createValCfg, txFactory, true) if err != nil { return errors.Wrap(err, "failed to build create-validator message") } diff --git a/x/genutil/client/cli/gentx_test.go b/x/genutil/client/cli/gentx_test.go index fac78ec6772..1991822b0a9 100644 --- a/x/genutil/client/cli/gentx_test.go +++ b/x/genutil/client/cli/gentx_test.go @@ -17,7 +17,6 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/codec/address" codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" "github.com/cosmos/cosmos-sdk/crypto/keyring" svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" @@ -124,7 +123,7 @@ func (s *CLITestSuite) TestGenTxCmd() { clientCtx := s.clientCtx ctx := svrcmd.CreateExecuteContext(context.Background()) - cmd := cli.GenTxCmd(module.NewManager(), clientCtx.TxConfig, banktypes.GenesisBalancesIterator{}, address.NewBech32Codec("cosmosvaloper")) + cmd := cli.GenTxCmd(module.NewManager(), banktypes.GenesisBalancesIterator{}) cmd.SetContext(ctx) cmd.SetArgs(tc.args) diff --git a/x/genutil/client/cli/init.go b/x/genutil/client/cli/init.go index 6fbf9f5a6f4..557050b10ae 100644 --- a/x/genutil/client/cli/init.go +++ b/x/genutil/client/cli/init.go @@ -89,6 +89,9 @@ func InitCmd(mm genesisMM) *cobra.Command { default: chainID = fmt.Sprintf("test-chain-%v", unsafe.Str(6)) } + if config.RootDir == "" { + config.RootDir = clientCtx.HomeDir + } // Get bip39 mnemonic var mnemonic string diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index b5d3b204298..495917167bb 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -365,7 +365,7 @@ func PrepareConfigForTxCreateValidator(flagSet *flag.FlagSet, moniker, nodeID, c } // BuildCreateValidatorMsg makes a new MsgCreateValidator. -func BuildCreateValidatorMsg(clientCtx client.Context, config TxCreateValidatorConfig, txBldr tx.Factory, generateOnly bool, valCodec address.Codec) (tx.Factory, sdk.Msg, error) { +func BuildCreateValidatorMsg(clientCtx client.Context, config TxCreateValidatorConfig, txBldr tx.Factory, generateOnly bool) (tx.Factory, sdk.Msg, error) { amounstStr := config.Amount amount, err := sdk.ParseCoinNormalized(amounstStr) if err != nil { @@ -398,7 +398,8 @@ func BuildCreateValidatorMsg(clientCtx client.Context, config TxCreateValidatorC return txBldr, nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "minimum self delegation must be a positive integer") } - valStr, err := valCodec.BytesToString(sdk.ValAddress(valAddr)) + valCodec := clientCtx.TxConfig.SigningContext().ValidatorAddressCodec() + valStr, err := valCodec.BytesToString(valAddr) if err != nil { return txBldr, nil, err }