Skip to content

Commit

Permalink
refactor(genutil/cli): remove txConfig from genesis command (#20976)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jul 18, 2024
1 parent f9f0ca4 commit 2370a50
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 36 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
7 changes: 3 additions & 4 deletions simapp/simd/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (

func initRootCmd(
rootCmd *cobra.Command,
txConfig client.TxConfig,
moduleManager *module.Manager,
) {
cfg := sdk.GetConfig()
Expand All @@ -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(),
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func NewRootCmd() *cobra.Command {
},
}

initRootCmd(rootCmd, encodingConfig.TxConfig, tempApp.ModuleManager)
initRootCmd(rootCmd, tempApp.ModuleManager)

// autocli opts
customClientTemplate, customClientConfig := initClientConfig()
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
Expand All @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions x/genutil/client/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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),
)

Expand Down
5 changes: 2 additions & 3 deletions x/genutil/client/cli/genaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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 {
Expand Down
16 changes: 9 additions & 7 deletions x/genutil/client/cli/genaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down
11 changes: 3 additions & 8 deletions x/genutil/client/cli/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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")
}
Expand Down
3 changes: 1 addition & 2 deletions x/genutil/client/cli/gentx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions x/genutil/client/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 2370a50

Please sign in to comment.