2018-04-25 07:18:06 -07:00
package cli
2018-02-28 17:57:38 -08:00
import (
feat: Add cli for listing all module accounts (#9812)
## Description
This PR adds a CLI for querying all module accounts, `module-accounts`, in the auth module.
Using this command would display all the account information, including human readable name of the module, module account address, account number, permissions, etc.
This command would be especially useful for developers using cosmos-sdk as there are lots of instances where a developer would have to look into the module account, but only to do so by getting the module account address after getting the module name, hashing it and bech32fying it in the status quo. By using this command, users would be able to get a quick look on the list of all module accounts' information.
---
### Author Checklist
*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*
I have...
- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed
### Reviewers Checklist
*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*
I have...
- [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [x] reviewed tests and test coverage
- [x] manually tested (if applicable)
2021-08-02 11:47:43 -07:00
"context"
2019-06-15 05:34:11 -07:00
"fmt"
"strings"
2018-02-28 17:57:38 -08:00
"github.com/spf13/cobra"
2020-03-06 07:16:30 -08:00
tmtypes "github.com/tendermint/tendermint/types"
2018-02-28 17:57:38 -08:00
2019-06-15 05:34:11 -07:00
"github.com/cosmos/cosmos-sdk/client"
2019-06-05 16:26:17 -07:00
"github.com/cosmos/cosmos-sdk/client/flags"
2018-02-28 17:57:38 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2021-07-27 06:32:56 -07:00
"github.com/cosmos/cosmos-sdk/types/errors"
2021-07-06 03:04:54 -07:00
"github.com/cosmos/cosmos-sdk/types/query"
2020-01-06 08:49:31 -08:00
"github.com/cosmos/cosmos-sdk/version"
2021-03-01 07:57:28 -08:00
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
2019-06-05 16:26:17 -07:00
"github.com/cosmos/cosmos-sdk/x/auth/types"
2019-06-15 05:34:11 -07:00
)
const (
2020-01-06 08:49:31 -08:00
flagEvents = "events"
2021-07-27 06:32:56 -07:00
flagType = "type"
typeHash = "hash"
typeAccSeq = "acc_seq"
typeSig = "signature"
2020-01-06 08:49:31 -08:00
eventFormat = "{eventType}.{eventAttribute}={value}"
2018-02-28 17:57:38 -08:00
)
2019-10-10 08:53:30 -07:00
// GetQueryCmd returns the transaction commands for this module
2020-07-17 13:20:45 -07:00
func GetQueryCmd ( ) * cobra . Command {
2019-06-05 16:26:17 -07:00
cmd := & cobra . Command {
Use : types . ModuleName ,
Short : "Querying commands for the auth module" ,
DisableFlagParsing : true ,
SuggestionsMinimumDistance : 2 ,
2019-06-15 05:34:11 -07:00
RunE : client . ValidateCmd ,
2019-06-05 16:26:17 -07:00
}
2019-06-15 05:34:11 -07:00
2020-02-26 07:33:56 -08:00
cmd . AddCommand (
2020-07-17 13:20:45 -07:00
GetAccountCmd ( ) ,
2021-02-22 06:21:46 -08:00
GetAccountsCmd ( ) ,
2020-07-17 13:20:45 -07:00
QueryParamsCmd ( ) ,
feat: Add cli for listing all module accounts (#9812)
## Description
This PR adds a CLI for querying all module accounts, `module-accounts`, in the auth module.
Using this command would display all the account information, including human readable name of the module, module account address, account number, permissions, etc.
This command would be especially useful for developers using cosmos-sdk as there are lots of instances where a developer would have to look into the module account, but only to do so by getting the module account address after getting the module name, hashing it and bech32fying it in the status quo. By using this command, users would be able to get a quick look on the list of all module accounts' information.
---
### Author Checklist
*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*
I have...
- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed
### Reviewers Checklist
*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*
I have...
- [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [x] reviewed tests and test coverage
- [x] manually tested (if applicable)
2021-08-02 11:47:43 -07:00
QueryModuleAccountsCmd ( ) ,
2020-02-26 07:33:56 -08:00
)
2019-06-15 05:34:11 -07:00
2019-06-05 16:26:17 -07:00
return cmd
}
2020-02-26 07:33:56 -08:00
// QueryParamsCmd returns the command handler for evidence parameter querying.
2020-07-17 13:20:45 -07:00
func QueryParamsCmd ( ) * cobra . Command {
2020-03-06 07:16:30 -08:00
cmd := & cobra . Command {
2020-02-26 07:33:56 -08:00
Use : "params" ,
Short : "Query the current auth parameters" ,
Args : cobra . NoArgs ,
Long : strings . TrimSpace ( ` Query the current auth parameters :
2020-10-12 08:31:25 -07:00
$ < appd > query auth params
2020-02-26 07:33:56 -08:00
` ) ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-12-14 14:09:51 -08:00
clientCtx , err := client . GetClientQueryContext ( cmd )
2020-07-17 13:20:45 -07:00
if err != nil {
return err
}
2020-02-26 07:33:56 -08:00
2020-07-21 10:48:37 -07:00
queryClient := types . NewQueryClient ( clientCtx )
2021-02-03 01:47:25 -08:00
res , err := queryClient . Params ( cmd . Context ( ) , & types . QueryParamsRequest { } )
2020-02-26 07:33:56 -08:00
if err != nil {
return err
}
2020-12-08 12:17:40 -08:00
return clientCtx . PrintProto ( & res . Params )
2020-02-26 07:33:56 -08:00
} ,
}
2020-03-06 07:16:30 -08:00
2020-07-11 01:13:46 -07:00
flags . AddQueryFlagsToCmd ( cmd )
return cmd
2020-02-26 07:33:56 -08:00
}
2018-08-06 11:11:30 -07:00
// GetAccountCmd returns a query account that will display the state of the
// account at a given address.
2020-07-17 13:20:45 -07:00
func GetAccountCmd ( ) * cobra . Command {
2018-11-19 09:02:34 -08:00
cmd := & cobra . Command {
2018-04-23 08:47:39 -07:00
Use : "account [address]" ,
2020-01-30 13:31:16 -08:00
Short : "Query for account by address" ,
2018-04-23 08:47:39 -07:00
Args : cobra . ExactArgs ( 1 ) ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-12-14 14:09:51 -08:00
clientCtx , err := client . GetClientQueryContext ( cmd )
2020-07-17 13:20:45 -07:00
if err != nil {
return err
}
2019-01-22 09:28:39 -08:00
key , err := sdk . AccAddressFromBech32 ( args [ 0 ] )
2018-04-23 08:47:39 -07:00
if err != nil {
return err
}
2018-03-30 05:57:53 -07:00
2020-07-21 10:48:37 -07:00
queryClient := types . NewQueryClient ( clientCtx )
2021-02-03 01:47:25 -08:00
res , err := queryClient . Account ( cmd . Context ( ) , & types . QueryAccountRequest { Address : key . String ( ) } )
2020-07-21 10:48:37 -07:00
if err != nil {
return err
}
2020-12-08 12:17:40 -08:00
return clientCtx . PrintProto ( res . Account )
2018-04-23 08:47:39 -07:00
} ,
2018-02-28 17:57:38 -08:00
}
2019-06-15 05:34:11 -07:00
2020-07-11 01:13:46 -07:00
flags . AddQueryFlagsToCmd ( cmd )
return cmd
2018-02-28 17:57:38 -08:00
}
2019-06-15 05:34:11 -07:00
2021-02-22 06:21:46 -08:00
// GetAccountsCmd returns a query command that will display a list of accounts
func GetAccountsCmd ( ) * cobra . Command {
cmd := & cobra . Command {
Use : "accounts" ,
Short : "Query all the accounts" ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
clientCtx , err := client . GetClientQueryContext ( cmd )
if err != nil {
return err
}
pageReq , err := client . ReadPageRequest ( cmd . Flags ( ) )
if err != nil {
return err
}
queryClient := types . NewQueryClient ( clientCtx )
res , err := queryClient . Accounts ( cmd . Context ( ) , & types . QueryAccountsRequest { Pagination : pageReq } )
if err != nil {
return err
}
return clientCtx . PrintProto ( res )
} ,
}
flags . AddQueryFlagsToCmd ( cmd )
flags . AddPaginationFlagsToCmd ( cmd , "all-accounts" )
return cmd
}
feat: Add cli for listing all module accounts (#9812)
## Description
This PR adds a CLI for querying all module accounts, `module-accounts`, in the auth module.
Using this command would display all the account information, including human readable name of the module, module account address, account number, permissions, etc.
This command would be especially useful for developers using cosmos-sdk as there are lots of instances where a developer would have to look into the module account, but only to do so by getting the module account address after getting the module name, hashing it and bech32fying it in the status quo. By using this command, users would be able to get a quick look on the list of all module accounts' information.
---
### Author Checklist
*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*
I have...
- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed
### Reviewers Checklist
*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*
I have...
- [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [x] reviewed tests and test coverage
- [x] manually tested (if applicable)
2021-08-02 11:47:43 -07:00
// QueryAllModuleAccountsCmd returns a list of all the existing module accounts with their account information and permissions
func QueryModuleAccountsCmd ( ) * cobra . Command {
cmd := & cobra . Command {
Use : "module-accounts" ,
Short : "Query all module accounts" ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
clientCtx , err := client . GetClientQueryContext ( cmd )
if err != nil {
return err
}
queryClient := types . NewQueryClient ( clientCtx )
res , err := queryClient . ModuleAccounts ( context . Background ( ) , & types . QueryModuleAccountsRequest { } )
if err != nil {
return err
}
return clientCtx . PrintProto ( res )
} ,
}
flags . AddQueryFlagsToCmd ( cmd )
return cmd
}
2019-06-28 06:32:47 -07:00
// QueryTxsByEventsCmd returns a command to search through transactions by events.
2020-07-17 13:20:45 -07:00
func QueryTxsByEventsCmd ( ) * cobra . Command {
2019-06-15 05:34:11 -07:00
cmd := & cobra . Command {
Use : "txs" ,
2020-01-06 08:49:31 -08:00
Short : "Query for paginated transactions that match a set of events" ,
Long : strings . TrimSpace (
fmt . Sprintf ( `
Search for transactions that match the exact given events where results are paginated .
Each event takes the form of ' % s ' . Please refer
to each module ' s documentation for the full set of events to query for . Each module
documents its respective events under ' xx_events . md ' .
2019-06-15 05:34:11 -07:00
Example :
2020-01-06 08:49:31 -08:00
$ % s query txs -- % s ' message . sender = cosmos1 ... & message . action = withdraw_delegator_reward ' -- page 1 -- limit 30
2020-07-07 08:40:46 -07:00
` , eventFormat , version . AppName , flagEvents ) ,
2020-01-06 08:49:31 -08:00
) ,
2019-06-15 05:34:11 -07:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-12-14 14:09:51 -08:00
clientCtx , err := client . GetClientQueryContext ( cmd )
2020-07-21 10:48:37 -07:00
if err != nil {
return err
}
2020-07-06 07:23:32 -07:00
eventsRaw , _ := cmd . Flags ( ) . GetString ( flagEvents )
eventsStr := strings . Trim ( eventsRaw , "'" )
2019-06-15 05:34:11 -07:00
2020-01-06 08:49:31 -08:00
var events [ ] string
if strings . Contains ( eventsStr , "&" ) {
events = strings . Split ( eventsStr , "&" )
2019-06-15 05:34:11 -07:00
} else {
2020-01-06 08:49:31 -08:00
events = append ( events , eventsStr )
2019-06-15 05:34:11 -07:00
}
2020-01-06 08:49:31 -08:00
var tmEvents [ ] string
for _ , event := range events {
if ! strings . Contains ( event , "=" ) {
return fmt . Errorf ( "invalid event; event %s should be of the format: %s" , event , eventFormat )
} else if strings . Count ( event , "=" ) > 1 {
return fmt . Errorf ( "invalid event; event %s should be of the format: %s" , event , eventFormat )
2019-06-15 05:34:11 -07:00
}
2020-01-06 08:49:31 -08:00
tokens := strings . Split ( event , "=" )
if tokens [ 0 ] == tmtypes . TxHeightKey {
event = fmt . Sprintf ( "%s=%s" , tokens [ 0 ] , tokens [ 1 ] )
2019-06-15 05:34:11 -07:00
} else {
2020-01-06 08:49:31 -08:00
event = fmt . Sprintf ( "%s='%s'" , tokens [ 0 ] , tokens [ 1 ] )
2019-06-15 05:34:11 -07:00
}
2020-01-06 08:49:31 -08:00
tmEvents = append ( tmEvents , event )
2019-06-15 05:34:11 -07:00
}
2020-07-06 07:23:32 -07:00
page , _ := cmd . Flags ( ) . GetInt ( flags . FlagPage )
limit , _ := cmd . Flags ( ) . GetInt ( flags . FlagLimit )
2019-06-15 05:34:11 -07:00
2021-03-01 07:57:28 -08:00
txs , err := authtx . QueryTxsByEvents ( clientCtx , tmEvents , page , limit , "" )
2019-06-15 05:34:11 -07:00
if err != nil {
return err
}
2020-12-08 12:17:40 -08:00
return clientCtx . PrintProto ( txs )
2019-06-15 05:34:11 -07:00
} ,
}
2021-06-21 03:40:56 -07:00
flags . AddQueryFlagsToCmd ( cmd )
2021-07-06 03:04:54 -07:00
cmd . Flags ( ) . Int ( flags . FlagPage , query . DefaultPage , "Query a specific page of paginated results" )
cmd . Flags ( ) . Int ( flags . FlagLimit , query . DefaultLimit , "Query number of transactions results per page returned" )
2020-01-06 08:49:31 -08:00
cmd . Flags ( ) . String ( flagEvents , "" , fmt . Sprintf ( "list of transaction events in the form of %s" , eventFormat ) )
cmd . MarkFlagRequired ( flagEvents )
2019-06-15 05:34:11 -07:00
return cmd
}
// QueryTxCmd implements the default command for a tx query.
2020-07-17 13:20:45 -07:00
func QueryTxCmd ( ) * cobra . Command {
2019-06-15 05:34:11 -07:00
cmd := & cobra . Command {
2021-07-27 06:32:56 -07:00
Use : "tx --type=[hash|acc_seq|signature] [hash|acc_seq|signature]" ,
2021-08-17 01:51:48 -07:00
Short : "Query for a transaction by hash, \"<addr>/<seq>\" combination or comma-separated signatures in a committed block" ,
2021-07-27 06:32:56 -07:00
Long : strings . TrimSpace ( fmt . Sprintf ( `
Example :
$ % s query tx < hash >
2021-08-17 01:51:48 -07:00
$ % s query tx -- % s = % s < addr > / < sequence >
$ % s query tx -- % s = % s < sig1_base64 > , < sig2_base64 ... >
2021-07-27 06:32:56 -07:00
` ,
version . AppName ,
version . AppName , flagType , typeAccSeq ,
version . AppName , flagType , typeSig ) ) ,
Args : cobra . ExactArgs ( 1 ) ,
2019-06-15 05:34:11 -07:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-12-14 14:09:51 -08:00
clientCtx , err := client . GetClientQueryContext ( cmd )
2020-07-21 10:48:37 -07:00
if err != nil {
return err
}
2019-06-15 05:34:11 -07:00
2021-07-27 06:32:56 -07:00
typ , _ := cmd . Flags ( ) . GetString ( flagType )
switch typ {
case typeHash :
{
if args [ 0 ] == "" {
return fmt . Errorf ( "argument should be a tx hash" )
}
// If hash is given, then query the tx by hash.
output , err := authtx . QueryTx ( clientCtx , args [ 0 ] )
if err != nil {
return err
}
2019-06-15 05:34:11 -07:00
2021-07-27 06:32:56 -07:00
if output . Empty ( ) {
return fmt . Errorf ( "no transaction found with hash %s" , args [ 0 ] )
}
return clientCtx . PrintProto ( output )
}
case typeSig :
{
sigParts , err := parseSigArgs ( args )
if err != nil {
return err
}
tmEvents := make ( [ ] string , len ( sigParts ) )
for i , sig := range sigParts {
tmEvents [ i ] = fmt . Sprintf ( "%s.%s='%s'" , sdk . EventTypeTx , sdk . AttributeKeySignature , sig )
}
txs , err := authtx . QueryTxsByEvents ( clientCtx , tmEvents , query . DefaultPage , query . DefaultLimit , "" )
if err != nil {
return err
}
if len ( txs . Txs ) == 0 {
return fmt . Errorf ( "found no txs matching given signatures" )
}
if len ( txs . Txs ) > 1 {
// This case means there's a bug somewhere else in the code. Should not happen.
return errors . ErrLogic . Wrapf ( "found %d txs matching given signatures" , len ( txs . Txs ) )
}
return clientCtx . PrintProto ( txs . Txs [ 0 ] )
}
case typeAccSeq :
{
if args [ 0 ] == "" {
return fmt . Errorf ( "`acc_seq` type takes an argument '<addr>/<seq>'" )
}
tmEvents := [ ] string {
fmt . Sprintf ( "%s.%s='%s'" , sdk . EventTypeTx , sdk . AttributeKeyAccountSequence , args [ 0 ] ) ,
}
txs , err := authtx . QueryTxsByEvents ( clientCtx , tmEvents , query . DefaultPage , query . DefaultLimit , "" )
if err != nil {
return err
}
if len ( txs . Txs ) == 0 {
return fmt . Errorf ( "found no txs matching given address and sequence combination" )
}
if len ( txs . Txs ) > 1 {
// This case means there's a bug somewhere else in the code. Should not happen.
return fmt . Errorf ( "found %d txs matching given address and sequence combination" , len ( txs . Txs ) )
}
return clientCtx . PrintProto ( txs . Txs [ 0 ] )
}
default :
return fmt . Errorf ( "unknown --%s value %s" , flagType , typ )
}
2019-06-15 05:34:11 -07:00
} ,
}
2020-09-13 06:52:09 -07:00
flags . AddQueryFlagsToCmd ( cmd )
2021-07-27 06:32:56 -07:00
cmd . Flags ( ) . String ( flagType , typeHash , fmt . Sprintf ( "The type to be used when querying tx, can be one of \"%s\", \"%s\", \"%s\"" , typeHash , typeAccSeq , typeSig ) )
2019-06-15 05:34:11 -07:00
return cmd
}
2021-07-27 06:32:56 -07:00
// parseSigArgs parses comma-separated signatures from the CLI arguments.
func parseSigArgs ( args [ ] string ) ( [ ] string , error ) {
if len ( args ) != 1 || args [ 0 ] == "" {
return nil , fmt . Errorf ( "argument should be comma-separated signatures" )
}
return strings . Split ( args [ 0 ] , "," ) , nil
}