refactor(cli): use clientCtx adress codecs in cli (#18070)

This commit is contained in:
Julien Robert 2023-10-11 15:04:40 +02:00 committed by GitHub
parent 6009891ed6
commit f28a93a063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 60 additions and 79 deletions

View File

@ -9,8 +9,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/client/tx"
@ -24,7 +22,7 @@ const (
)
// GetTxCmd returns vesting module's transaction commands.
func GetTxCmd(ac address.Codec) *cobra.Command {
func GetTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Vesting transaction subcommands",
@ -34,9 +32,9 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
}
txCmd.AddCommand(
NewMsgCreateVestingAccountCmd(ac),
NewMsgCreatePermanentLockedAccountCmd(ac),
NewMsgCreatePeriodicVestingAccountCmd(ac),
NewMsgCreateVestingAccountCmd(),
NewMsgCreatePermanentLockedAccountCmd(),
NewMsgCreatePeriodicVestingAccountCmd(),
)
return txCmd
@ -44,7 +42,7 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
// NewMsgCreateVestingAccountCmd returns a CLI command handler for creating a
// MsgCreateVestingAccount transaction.
func NewMsgCreateVestingAccountCmd(ac address.Codec) *cobra.Command {
func NewMsgCreateVestingAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-vesting-account [to_address] [amount] [end_time]",
Short: "Create a new vesting account funded with an allocation of tokens.",
@ -59,7 +57,7 @@ timestamp.`,
if err != nil {
return err
}
toAddr, err := ac.StringToBytes(args[0])
toAddr, err := clientCtx.AddressCodec.StringToBytes(args[0])
if err != nil {
return err
}
@ -93,7 +91,7 @@ timestamp.`,
// NewMsgCreatePermanentLockedAccountCmd returns a CLI command handler for creating a
// MsgCreatePermanentLockedAccount transaction.
func NewMsgCreatePermanentLockedAccountCmd(ac address.Codec) *cobra.Command {
func NewMsgCreatePermanentLockedAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-permanent-locked-account [to_address] [amount]",
Short: "Create a new permanently locked account funded with an allocation of tokens.",
@ -106,7 +104,7 @@ tokens.`,
if err != nil {
return err
}
toAddr, err := ac.StringToBytes(args[0])
toAddr, err := clientCtx.AddressCodec.StringToBytes(args[0])
if err != nil {
return err
}
@ -142,7 +140,7 @@ type InputPeriod struct {
// NewMsgCreatePeriodicVestingAccountCmd returns a CLI command handler for creating a
// MsgCreatePeriodicVestingAccountCmd transaction.
func NewMsgCreatePeriodicVestingAccountCmd(ac address.Codec) *cobra.Command {
func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-periodic-vesting-account [to_address] [periods_json_file]",
Short: "Create a new vesting account funded with an allocation of tokens.",
@ -170,7 +168,7 @@ func NewMsgCreatePeriodicVestingAccountCmd(ac address.Codec) *cobra.Command {
return err
}
toAddr, err := ac.StringToBytes(args[0])
toAddr, err := clientCtx.AddressCodec.StringToBytes(args[0])
if err != nil {
return err
}

View File

@ -54,7 +54,7 @@ func (s *CLITestSuite) SetupSuite() {
func (s *CLITestSuite) TestNewMsgCreateVestingAccountCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewMsgCreateVestingAccountCmd(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewMsgCreateVestingAccountCmd()
cmd.SetOutput(io.Discard)
extraArgs := []string{
@ -143,7 +143,7 @@ func (s *CLITestSuite) TestNewMsgCreateVestingAccountCmd() {
func (s *CLITestSuite) TestNewMsgCreatePermanentLockedAccountCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewMsgCreatePermanentLockedAccountCmd(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewMsgCreatePermanentLockedAccountCmd()
cmd.SetOutput(io.Discard)
extraArgs := []string{
@ -222,7 +222,7 @@ func (s *CLITestSuite) TestNewMsgCreatePermanentLockedAccountCmd() {
func (s *CLITestSuite) TestNewMsgCreatePeriodicVestingAccountCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewMsgCreatePeriodicVestingAccountCmd(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewMsgCreatePeriodicVestingAccountCmd()
cmd.SetOutput(io.Discard)
extraArgs := []string{

View File

@ -9,7 +9,6 @@ import (
"google.golang.org/grpc"
modulev1 "cosmossdk.io/api/cosmos/vesting/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
@ -33,9 +32,7 @@ var (
// AppModuleBasic defines the basic application module used by the sub-vesting
// module. The module itself contain no special logic or state other than message
// handling.
type AppModuleBasic struct {
ac address.Codec
}
type AppModuleBasic struct{}
// Name returns the module's name.
func (AppModuleBasic) Name() string {
@ -69,7 +66,7 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *gwruntime.S
// GetTxCmd returns the root tx command for the auth module.
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd(ab.ac)
return cli.GetTxCmd()
}
// AppModule extends the AppModuleBasic implementation by implementing the
@ -83,7 +80,7 @@ type AppModule struct {
func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{ac: ak.AddressCodec()},
AppModuleBasic: AppModuleBasic{},
accountKeeper: ak,
bankKeeper: bk,
}

View File

@ -8,8 +8,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/client/tx"
@ -35,7 +33,7 @@ const (
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd(ac address.Codec) *cobra.Command {
func GetTxCmd() *cobra.Command {
AuthorizationTxCmd := &cobra.Command{
Use: authz.ModuleName,
Short: "Authorization transactions subcommands",
@ -46,7 +44,7 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
}
AuthorizationTxCmd.AddCommand(
NewCmdGrantAuthorization(ac),
NewCmdGrantAuthorization(),
NewCmdExecAuthorization(),
)
@ -54,20 +52,17 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
}
// NewCmdGrantAuthorization returns a CLI command handler for creating a MsgGrant transaction.
//
// cannot give autocli support, can be CLI breaking
func NewCmdGrantAuthorization(ac address.Codec) *cobra.Command {
// Migrating this command to AutoCLI is possible but would be CLI breaking.
func NewCmdGrantAuthorization() *cobra.Command {
cmd := &cobra.Command{
Use: "grant <grantee> <authorization_type=\"send\"|\"generic\"|\"delegate\"|\"unbond\"|\"redelegate\"> --from <granter>",
Use: "grant [grantee] <authorization_type=\"send\"|\"generic\"|\"delegate\"|\"unbond\"|\"redelegate\"> --from [granter]",
Short: "Grant authorization to an address",
Long: strings.TrimSpace(
fmt.Sprintf(`create a new grant authorization to an address to execute a transaction on your behalf:
Examples:
$ %s tx %s grant cosmos1skjw.. send --spend-limit=1000stake --from=cosmos1skl..
$ %s tx %s grant cosmos1skjw.. generic --msg-type=/cosmos.gov.v1.MsgVote --from=cosmos1sk..
`, version.AppName, authz.ModuleName, version.AppName, authz.ModuleName),
),
$ %[1]s tx authz grant cosmos1skjw.. send --spend-limit=1000stake --from=cosmos1skl..
$ %[1]s tx authz grant cosmos1skjw.. generic --msg-type=/cosmos.gov.v1.MsgVote --from=cosmos1sk..
`, version.AppName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
@ -79,7 +74,7 @@ Examples:
return errors.New("grantee and granter should be different")
}
grantee, err := ac.StringToBytes(args[0])
grantee, err := clientCtx.AddressCodec.StringToBytes(args[0])
if err != nil {
return err
}
@ -115,7 +110,7 @@ Examples:
}
}
allowed, err := bech32toAccAddresses(allowList, ac)
allowed, err := bech32toAccAddresses(clientCtx, allowList)
if err != nil {
return err
}
@ -168,12 +163,12 @@ Examples:
delegateLimit = &spendLimit
}
allowed, err := bech32toValAddresses(allowValidators)
allowed, err := bech32toValAddresses(clientCtx, allowValidators)
if err != nil {
return err
}
denied, err := bech32toValAddresses(denyValidators)
denied, err := bech32toValAddresses(clientCtx, denyValidators)
if err != nil {
return err
}
@ -271,10 +266,10 @@ Example:
}
// bech32toValAddresses returns []ValAddress from a list of Bech32 string addresses.
func bech32toValAddresses(validators []string) ([]sdk.ValAddress, error) {
func bech32toValAddresses(clientCtx client.Context, validators []string) ([]sdk.ValAddress, error) {
vals := make([]sdk.ValAddress, len(validators))
for i, validator := range validators {
addr, err := sdk.ValAddressFromBech32(validator)
addr, err := clientCtx.ValidatorAddressCodec.StringToBytes(validator)
if err != nil {
return nil, err
}
@ -284,10 +279,10 @@ func bech32toValAddresses(validators []string) ([]sdk.ValAddress, error) {
}
// bech32toAccAddresses returns []AccAddress from a list of Bech32 string addresses.
func bech32toAccAddresses(accAddrs []string, ac address.Codec) ([]sdk.AccAddress, error) {
func bech32toAccAddresses(clientCtx client.Context, accAddrs []string) ([]sdk.AccAddress, error) {
addrs := make([]sdk.AccAddress, len(accAddrs))
for i, addr := range accAddrs {
accAddr, err := ac.StringToBytes(addr)
accAddr, err := clientCtx.AddressCodec.StringToBytes(addr)
if err != nil {
return nil, err
}

View File

@ -2,13 +2,12 @@ package authz
import (
"github.com/cosmos/cosmos-sdk/client"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/x/authz/client/cli"
)
func CreateGrant(clientCtx client.Context, args []string) (testutil.BufferWriter, error) {
cmd := cli.NewCmdGrantAuthorization(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdGrantAuthorization()
return clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
}

View File

@ -95,7 +95,7 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux
// GetTxCmd returns the transaction commands for the authz module
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd(ab.ac)
return cli.GetTxCmd()
}
// AppModule implements the sdk.AppModule interface

View File

@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"
"cosmossdk.io/core/address"
"cosmossdk.io/x/feegrant"
"github.com/cosmos/cosmos-sdk/client"
@ -27,7 +26,7 @@ const (
)
// GetTxCmd returns the transaction commands for feegrant module
func GetTxCmd(ac address.Codec) *cobra.Command {
func GetTxCmd() *cobra.Command {
feegrantTxCmd := &cobra.Command{
Use: feegrant.ModuleName,
Short: "Feegrant transactions sub-commands",
@ -38,7 +37,7 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
}
feegrantTxCmd.AddCommand(
NewCmdFeeGrant(ac),
NewCmdFeeGrant(),
)
return feegrantTxCmd
@ -46,7 +45,7 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
// NewCmdFeeGrant returns a CLI command handler to create a MsgGrantAllowance transaction.
// This command is more powerful than AutoCLI generated command as it allows a better input validation.
func NewCmdFeeGrant(ac address.Codec) *cobra.Command {
func NewCmdFeeGrant() *cobra.Command {
cmd := &cobra.Command{
Use: "grant [granter_key_or_address] [grantee]",
Aliases: []string{"grant-allowance"},
@ -75,13 +74,13 @@ Examples:
return err
}
_, err = ac.StringToBytes(args[1])
_, err = clientCtx.AddressCodec.StringToBytes(args[1])
if err != nil {
return err
}
granter := clientCtx.GetFromAddress()
granterStr, err := ac.BytesToString(granter)
granterStr, err := clientCtx.AddressCodec.BytesToString(granter)
if err != nil {
return err
}

View File

@ -136,7 +136,7 @@ func (s *CLITestSuite) createGrant(granter, grantee sdk.Address) {
commonFlags...,
)
cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, args)
s.Require().NoError(err)
@ -423,7 +423,7 @@ func (s *CLITestSuite) TestNewCmdFeeGrant() {
tc := tc
s.Run(tc.name, func() {
cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
@ -466,7 +466,7 @@ func (s *CLITestSuite) TestTxWithFeeGrant() {
commonFlags...,
)
cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()
var res sdk.TxResponse
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
@ -619,7 +619,7 @@ func (s *CLITestSuite) TestFilteredFeeAllowance() {
tc := tc
s.Run(tc.name, func() {
cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErrMsg != "" {
s.Require().Error(err)
@ -669,7 +669,7 @@ func (s *CLITestSuite) TestFilteredFeeAllowance() {
commonFlags...,
)
cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &sdk.TxResponse{}), out.String())

View File

@ -9,7 +9,6 @@ import (
"github.com/spf13/cobra"
modulev1 "cosmossdk.io/api/cosmos/feegrant/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
@ -44,7 +43,6 @@ var (
// AppModuleBasic defines the basic application module used by the feegrant module.
type AppModuleBasic struct {
cdc codec.Codec
ac address.Codec
}
// Name returns the feegrant module's name.
@ -99,7 +97,7 @@ func (ab AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context,
// GetTxCmd returns the root tx command for the feegrant module.
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd(ab.ac)
return cli.GetTxCmd()
}
// ----------------------------------------------------------------------------
@ -119,7 +117,7 @@ type AppModule struct {
// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, ak feegrant.AccountKeeper, bk feegrant.BankKeeper, keeper keeper.Keeper, registry cdctypes.InterfaceRegistry) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{cdc: cdc, ac: ak.AddressCodec()},
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
accountKeeper: ak,
bankKeeper: bk,
@ -205,6 +203,6 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return simulation.WeightedOperations(
am.registry, simState.AppParams, simState.Cdc, simState.TxConfig,
am.accountKeeper, am.bankKeeper, am.keeper, am.ac,
am.accountKeeper, am.bankKeeper, am.keeper, am.accountKeeper.AddressCodec(),
)
}

View File

@ -7,12 +7,10 @@ import (
"github.com/stretchr/testify/require"
"cosmossdk.io/x/upgrade/types"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
)
func TestParsePlan(t *testing.T) {
fs := NewCmdSubmitUpgradeProposal(addresscodec.NewBech32Codec("cosmos")).Flags()
fs := NewCmdSubmitUpgradeProposal().Flags()
proposal := types.MsgSoftwareUpgrade{
Plan: types.Plan{

View File

@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"
addresscodec "cosmossdk.io/core/address"
"cosmossdk.io/x/upgrade/plan"
"cosmossdk.io/x/upgrade/types"
@ -29,22 +28,22 @@ const (
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd(ac addresscodec.Codec) *cobra.Command {
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Upgrade transaction subcommands",
}
cmd.AddCommand(
NewCmdSubmitUpgradeProposal(ac),
NewCmdSubmitCancelUpgradeProposal(ac),
NewCmdSubmitUpgradeProposal(),
NewCmdSubmitCancelUpgradeProposal(),
)
return cmd
}
// NewCmdSubmitUpgradeProposal implements a command handler for submitting a software upgrade proposal transaction.
func NewCmdSubmitUpgradeProposal(ac addresscodec.Codec) *cobra.Command {
func NewCmdSubmitUpgradeProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "software-upgrade [name] (--upgrade-height [height]) (--upgrade-info [info]) [flags]",
Args: cobra.ExactArgs(1),
@ -97,11 +96,11 @@ func NewCmdSubmitUpgradeProposal(ac addresscodec.Codec) *cobra.Command {
authority, _ := cmd.Flags().GetString(FlagAuthority)
if authority != "" {
if _, err = ac.StringToBytes(authority); err != nil {
if _, err = clientCtx.AddressCodec.StringToBytes(authority); err != nil {
return fmt.Errorf("invalid authority address: %w", err)
}
} else {
if authority, err = ac.BytesToString(address.Module("gov")); err != nil {
if authority, err = clientCtx.AddressCodec.BytesToString(address.Module("gov")); err != nil {
return fmt.Errorf("failed to convert authority address to string: %w", err)
}
}
@ -138,7 +137,7 @@ func NewCmdSubmitUpgradeProposal(ac addresscodec.Codec) *cobra.Command {
}
// NewCmdSubmitCancelUpgradeProposal implements a command handler for submitting a software upgrade cancel proposal transaction.
func NewCmdSubmitCancelUpgradeProposal(ac addresscodec.Codec) *cobra.Command {
func NewCmdSubmitCancelUpgradeProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "cancel-software-upgrade [flags]",
Args: cobra.ExactArgs(0),
@ -157,11 +156,11 @@ func NewCmdSubmitCancelUpgradeProposal(ac addresscodec.Codec) *cobra.Command {
authority, _ := cmd.Flags().GetString(FlagAuthority)
if authority != "" {
if _, err = ac.StringToBytes(authority); err != nil {
if _, err = clientCtx.AddressCodec.StringToBytes(authority); err != nil {
return fmt.Errorf("invalid authority address: %w", err)
}
} else {
if authority, err = ac.BytesToString(address.Module("gov")); err != nil {
if authority, err = clientCtx.AddressCodec.BytesToString(address.Module("gov")); err != nil {
return fmt.Errorf("failed to convert authority address to string: %w", err)
}
}

View File

@ -47,9 +47,7 @@ var (
)
// AppModuleBasic implements the sdk.AppModuleBasic interface
type AppModuleBasic struct {
ac address.Codec
}
type AppModuleBasic struct{}
// Name returns the ModuleName
func (AppModuleBasic) Name() string {
@ -70,7 +68,7 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *g
// GetTxCmd returns the CLI transaction commands for this module
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd(ab.ac)
return cli.GetTxCmd()
}
// RegisterInterfaces registers interfaces and implementations of the upgrade module.
@ -87,7 +85,7 @@ type AppModule struct {
// NewAppModule creates a new AppModule object
func NewAppModule(keeper *keeper.Keeper, ac address.Codec) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{ac: ac},
AppModuleBasic: AppModuleBasic{},
keeper: keeper,
}
}