Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

x/auth: Finish CLI Refactor #6765

Merged
merged 6 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,24 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont
clientCtx = clientCtx.WithTrustNode(trustNode)
}

if flagSet.Changed(flags.FlagKeyringBackend) {
if clientCtx.Keyring == nil || flagSet.Changed(flags.FlagKeyringBackend) {
keyringBackend, _ := flagSet.GetString(flags.FlagKeyringBackend)

kr, err := newKeyringFromFlags(clientCtx, keyringBackend)
if err != nil {
return clientCtx, err
}
if keyringBackend != "" {
kr, err := newKeyringFromFlags(clientCtx, keyringBackend)
if err != nil {
return clientCtx, err
}

clientCtx = clientCtx.WithKeyring(kr)
clientCtx = clientCtx.WithKeyring(kr)
}
}

if flagSet.Changed(flags.FlagNode) {
if clientCtx.Client == nil || flagSet.Changed(flags.FlagNode) {
rpcURI, _ := flagSet.GetString(flags.FlagNode)
clientCtx = clientCtx.WithNodeURI(rpcURI)
if rpcURI != "" {
clientCtx = clientCtx.WithNodeURI(rpcURI)
}
}

return clientCtx, nil
Expand Down
20 changes: 6 additions & 14 deletions client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ import (

"github.com/gorilla/mux"
"github.com/spf13/cobra"
"github.com/spf13/viper"

tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
)

// TODO these next two functions feel kinda hacky based on their placement

//ValidatorCommand returns the validator set for a given height
func ValidatorCommand(cdc *codec.Codec) *cobra.Command {
func ValidatorCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "tendermint-validator-set [height]",
Short: "Get the full tendermint validator set at given height",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

var height *int64

// optional height
Expand All @@ -43,9 +43,10 @@ func ValidatorCommand(cdc *codec.Codec) *cobra.Command {
}
}

clientCtx := client.NewContext().WithCodec(cdc)
page, _ := cmd.Flags().GetInt(flags.FlagPage)
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)

result, err := GetValidators(clientCtx, height, viper.GetInt(flags.FlagPage), viper.GetInt(flags.FlagLimit))
result, err := GetValidators(clientCtx, height, page, limit)
if err != nil {
return err
}
Expand All @@ -55,19 +56,10 @@ func ValidatorCommand(cdc *codec.Codec) *cobra.Command {
}

cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode))

cmd.Flags().Bool(flags.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
viper.BindPFlag(flags.FlagTrustNode, cmd.Flags().Lookup(flags.FlagTrustNode))

cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
viper.BindPFlag(flags.FlagKeyringBackend, cmd.Flags().Lookup(flags.FlagKeyringBackend))

cmd.Flags().Int(flags.FlagPage, 0, "Query a specific page of paginated results")
viper.BindPFlag(flags.FlagPage, cmd.Flags().Lookup(flags.FlagPage))

cmd.Flags().Int(flags.FlagLimit, 100, "Query number of results returned per page")
viper.BindPFlag(flags.FlagLimit, cmd.Flags().Lookup(flags.FlagLimit))

return cmd
}
Expand Down
8 changes: 4 additions & 4 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ func queryCommand() *cobra.Command {
}

cmd.AddCommand(
authcmd.GetAccountCmd(encodingConfig.Amino),
rpc.ValidatorCommand(encodingConfig.Amino),
authcmd.GetAccountCmd(),
rpc.ValidatorCommand(),
rpc.BlockCommand(),
authcmd.QueryTxsByEventsCmd(encodingConfig.Amino),
authcmd.QueryTxCmd(encodingConfig.Amino),
authcmd.QueryTxsByEventsCmd(),
authcmd.QueryTxCmd(),
)

simapp.ModuleBasics.AddQueryCommands(cmd, initClientCtx)
Expand Down
3 changes: 2 additions & 1 deletion simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ func InitTestnet(
if err != nil {
return fmt.Errorf("error creating tx: %w", err)
}
txBldr.WithChainID(chainID).WithMemo(memo).WithKeybase(kb)

txBldr = txBldr.WithChainID(chainID).WithMemo(memo).WithKeybase(kb)

signedTx, err := txBldr.SignStdTx(nodeDirName, tx, false)
if err != nil {
Expand Down
41 changes: 25 additions & 16 deletions x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/version"
Expand All @@ -24,7 +23,7 @@ const (
)

// GetQueryCmd returns the transaction commands for this module
func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the auth module",
Expand All @@ -34,15 +33,15 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
}

cmd.AddCommand(
GetAccountCmd(cdc),
QueryParamsCmd(cdc),
GetAccountCmd(),
QueryParamsCmd(),
)

return cmd
}

// QueryParamsCmd returns the command handler for evidence parameter querying.
func QueryParamsCmd(cdc *codec.Codec) *cobra.Command {
func QueryParamsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "Query the current auth parameters",
Expand All @@ -52,7 +51,11 @@ func QueryParamsCmd(cdc *codec.Codec) *cobra.Command {
$ <appcli> query auth params
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, _, err := clientCtx.QueryWithData(route, nil)
Expand All @@ -61,7 +64,7 @@ $ <appcli> query auth params
}

var params types.Params
if err := cdc.UnmarshalJSON(res, &params); err != nil {
if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &params); err != nil {
return fmt.Errorf("failed to unmarshal params: %w", err)
}

Expand All @@ -76,21 +79,26 @@ $ <appcli> query auth params

// GetAccountCmd returns a query account that will display the state of the
// account at a given address.
func GetAccountCmd(cdc *codec.Codec) *cobra.Command {
func GetAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "account [address]",
Short: "Query for account by address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
accGetter := types.NewAccountRetriever(authclient.Codec)
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadQueryCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}

accRetriever := types.NewAccountRetriever(clientCtx.JSONMarshaler)

key, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

acc, err := accGetter.GetAccount(clientCtx, key)
acc, err := accRetriever.GetAccount(clientCtx, key)
if err != nil {
return err
}
Expand All @@ -105,7 +113,7 @@ func GetAccountCmd(cdc *codec.Codec) *cobra.Command {
}

// QueryTxsByEventsCmd returns a command to search through transactions by events.
func QueryTxsByEventsCmd(cdc *codec.Codec) *cobra.Command {
func QueryTxsByEventsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "txs",
Short: "Query for paginated transactions that match a set of events",
Expand All @@ -121,6 +129,8 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator
`, eventFormat, version.AppName, flagEvents),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

eventsRaw, _ := cmd.Flags().GetString(flagEvents)
eventsStr := strings.Trim(eventsRaw, "'")

Expand Down Expand Up @@ -153,13 +163,12 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator
page, _ := cmd.Flags().GetInt(flags.FlagPage)
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)

clientCtx := client.NewContext().WithCodec(cdc)
txs, err := authclient.QueryTxsByEvents(clientCtx, tmEvents, page, limit, "")
if err != nil {
return err
}

output, err := cdc.MarshalJSON(txs)
output, err := clientCtx.JSONMarshaler.MarshalJSON(txs)
if err != nil {
return err
}
Expand All @@ -181,13 +190,13 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator
}

// QueryTxCmd implements the default command for a tx query.
func QueryTxCmd(cdc *codec.Codec) *cobra.Command {
func QueryTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "tx [hash]",
Short: "Query for a transaction by hash in a committed block",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
clientCtx := client.GetClientContextFromCmd(cmd)

output, err := authclient.QueryTx(clientCtx, args[0])
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Rout
}

// GetTxCmd returns the root tx command for the auth module.
func (AppModuleBasic) GetTxCmd(clientCtx client.Context) *cobra.Command {
func (AppModuleBasic) GetTxCmd(_ client.Context) *cobra.Command {
return cli.GetTxCmd()
}

// GetQueryCmd returns the root query command for the auth module.
func (AppModuleBasic) GetQueryCmd(clientCtx client.Context) *cobra.Command {
return cli.GetQueryCmd(clientCtx.Codec)
func (AppModuleBasic) GetQueryCmd(_ client.Context) *cobra.Command {
return cli.GetQueryCmd()
}

// RegisterInterfaceTypes registers interfaces and implementations of the auth module.
Expand Down