Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Convert x/auth query CLI commands to use gRPC query client #6643

Closed
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
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func queryCommand() *cobra.Command {
}

cmd.AddCommand(
authcmd.GetAccountCmd(encodingConfig.Amino),
authcmd.GetAccountCmd(initClientCtx),
rpc.ValidatorCommand(encodingConfig.Amino),
rpc.BlockCommand(),
authcmd.QueryTxsByEventsCmd(encodingConfig.Amino),
Expand Down
39 changes: 21 additions & 18 deletions x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"fmt"
"strings"

Expand All @@ -24,7 +25,7 @@ const (
)

// GetQueryCmd returns the transaction commands for this module
func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
func GetQueryCmd(clientCtx client.Context) *cobra.Command {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an FYI, this will all be undone once #6573 is merged. I'm not sure about the order of preference here, but just keep that in mind.

Essentially, command constructors will take no arguments anymore (at least not client.Context). They will instead use a clientCtx := client.GetClientContextFromCmd(cmd) call.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we can wait until #6573 is merged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexanderbez Just to get this right: assuming we remove the client.Context arg here and use clientCtx := client.GetClientContextFromCmd(cmd), we can still merge this PR before #6573, correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If tests pass, yes 👍

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

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

return cmd
}

// QueryParamsCmd returns the command handler for evidence parameter querying.
func QueryParamsCmd(cdc *codec.Codec) *cobra.Command {
func QueryParamsCmd(clientCtx client.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "Query the current auth parameters",
Expand All @@ -52,20 +53,16 @@ func QueryParamsCmd(cdc *codec.Codec) *cobra.Command {
$ <appcli> query auth params
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.NewContext().WithCodec(cdc)
queryClient := types.NewQueryClient(clientCtx.Init())

params := types.NewQueryParametersRequest()

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, _, err := clientCtx.QueryWithData(route, nil)
res, err := queryClient.Parameters(context.Background(), params)
if err != nil {
return err
}

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

return clientCtx.PrintOutput(params)
return clientCtx.PrintOutput(res.Params)
},
}

Expand All @@ -74,26 +71,32 @@ $ <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(clientCtx client.Context) *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)
queryClient := types.NewQueryClient(clientCtx.Init())

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

acc, err := accGetter.GetAccount(clientCtx, key)
params := types.NewQueryAccountRequest(key)

res, err := queryClient.Account(context.Background(), params)
if err != nil {
return err
}

return clientCtx.PrintOutput(acc)
account, ok := res.Account.GetCachedValue().(types.AccountI)
if !ok {
return fmt.Errorf("error when unpacking account")
}

return clientCtx.PrintOutput(account)
},
}

Expand Down
6 changes: 4 additions & 2 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (AppModuleBasic) GetTxCmd(clientCtx client.Context) *cobra.Command {

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

// RegisterInterfaceTypes registers interfaces and implementations of the auth module.
Expand Down Expand Up @@ -118,7 +118,9 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
return keeper.NewQuerier(am.accountKeeper)
}

func (am AppModule) RegisterQueryService(grpc.Server) {}
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.accountKeeper)
}

// InitGenesis performs genesis initialization for the auth module. It returns
// no validator updates.
Expand Down