Merge PR #6765: x/auth: Finish CLI Refactor

This commit is contained in:
Alexander Bezobchuk 2020-07-17 16:20:45 -04:00 committed by GitHub
parent f33749263f
commit e59781eca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 46 deletions

View File

@ -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

View File

@ -9,13 +9,11 @@ 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"
)
@ -23,12 +21,14 @@ import (
// 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
@ -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
}
@ -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
}

View File

@ -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)

View File

@ -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 {

View File

@ -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"
@ -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",
@ -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",
@ -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)
@ -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)
}
@ -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
}
@ -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",
@ -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, "'")
@ -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
}
@ -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 {

View File

@ -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.