Skip to content

Commit

Permalink
refactor: Improve and clarify API around AccountAddressById (backport
Browse files Browse the repository at this point in the history
#13460) (#13461)

* refactor: Improve and clarify API around `AccountAddressById`  (#13460)

* Revert "chore: change id to use uint64 in `AccountAddressByID` (#13411)"

This reverts commit 3d0e214.

* Add since

* Update CLI command

* make proto-gen

* id->accNum

* No API breaking changes

* improve err msg

* Update CHANGELOG.md

Co-authored-by: Julien Robert <julien@rbrt.fr>

* Update CHANGELOG.md

Co-authored-by: Julien Robert <julien@rbrt.fr>

Co-authored-by: Julien Robert <julien@rbrt.fr>
(cherry picked from commit 90bd316)

# Conflicts:
#	CHANGELOG.md
#	api/cosmos/auth/v1beta1/query.pulsar.go
#	api/cosmos/auth/v1beta1/query_grpc.pb.go
#	api/cosmos/distribution/v1beta1/query_grpc.pb.go
#	api/cosmos/group/v1/types.pulsar.go
#	proto/cosmos/auth/v1beta1/query.proto
#	x/auth/client/cli/query.go
#	x/auth/keeper/grpc_query.go
#	x/auth/keeper/grpc_query_test.go
#	x/auth/types/query.pb.go
#	x/distribution/types/query.pb.go

* fix conflicts

* changelog

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
  • Loading branch information
mergify[bot] and amaury1093 committed Oct 6, 2022
1 parent 26786a0 commit 982b891
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Improvements

* (auth) [#13460](https://github.com/cosmos/cosmos-sdk/pull/13460) The `q auth address-by-id` CLI command has been renamed to `q auth address-by-acc-num` to be more explicit. However, the old `address-by-id` version is still kept as an alias, for backwards compatibility.

## [v0.46.2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.2) - 2022-10-03

### API Breaking Changes
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -10,6 +10,7 @@ require (
github.com/bgentry/speakeasy v0.1.0
github.com/btcsuite/btcd v0.22.1
github.com/celestiaorg/smt v0.3.0
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/cockroachdb/apd/v2 v2.0.2
github.com/coinbase/rosetta-sdk-go v0.7.9
github.com/confio/ics23/go v0.7.0
Expand Down Expand Up @@ -75,7 +76,6 @@ require (
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/ledger-go v0.9.2 // indirect
github.com/creachadair/taskgroup v0.3.2 // indirect
Expand Down
13 changes: 11 additions & 2 deletions proto/cosmos/auth/v1beta1/query.proto
Expand Up @@ -24,7 +24,9 @@ service Query {
option (google.api.http).get = "/cosmos/auth/v1beta1/accounts/{address}";
}

// AccountAddressByID returns account address based on account id
// AccountAddressByID returns account address based on account number.
//
// Since: cosmos-sdk 0.46.2
rpc AccountAddressByID(QueryAccountAddressByIDRequest) returns (QueryAccountAddressByIDResponse) {
option (google.api.http).get = "/cosmos/auth/v1beta1/address_by_id/{id}";
}
Expand Down Expand Up @@ -159,11 +161,18 @@ message AddressStringToBytesResponse {
}

// QueryAccountAddressByIDRequest is the request type for AccountAddressByID rpc method
message QueryAccountAddressByIDRequest{
//
// Since: cosmos-sdk 0.46.2
message QueryAccountAddressByIDRequest {
// id is the account number of the address to be queried. This field
// should have been an uint64 (like all account numbers), and will be
// updated to uint64 in a future version of the auth query.
int64 id = 1;
}

// QueryAccountAddressByIDResponse is the response type for AccountAddressByID rpc method
//
// Since: cosmos-sdk 0.46.2
message QueryAccountAddressByIDResponse {
string account_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}
11 changes: 6 additions & 5 deletions x/auth/client/cli/query.go
Expand Up @@ -129,23 +129,24 @@ func GetAccountCmd() *cobra.Command {
// GetAccountAddressByIDCmd returns a query account that will display the account address of a given account id.
func GetAccountAddressByIDCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "address-by-id [id]",
Short: "Query for account address by account id",
Use: "address-by-acc-num [acc-num]",
Aliases: []string{"address-by-id"},
Short: "Query for an address by account number",
Args: cobra.ExactArgs(1),
Example: fmt.Sprintf("%s q auth address-by-id 1", version.AppName),
Example: fmt.Sprintf("%s q auth address-by-acc-num 1", version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

id, err := strconv.ParseInt(args[0], 10, 64)
accNum, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.AccountAddressByID(cmd.Context(), &types.QueryAccountAddressByIDRequest{Id: id})
res, err := queryClient.AccountAddressByID(cmd.Context(), &types.QueryAccountAddressByIDRequest{Id: accNum})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/keeper/grpc_query.go
Expand Up @@ -25,13 +25,13 @@ func (ak AccountKeeper) AccountAddressByID(c context.Context, req *types.QueryAc
}

if req.Id < 0 {
return nil, status.Error(codes.InvalidArgument, "Invalid account id")
return nil, status.Error(codes.InvalidArgument, "invalid account number")
}

ctx := sdk.UnwrapSDKContext(c)
address := ak.GetAccountAddressByID(ctx, uint64(req.GetId()))
if len(address) == 0 {
return nil, status.Errorf(codes.NotFound, "account address not found with id %d", req.Id)
return nil, status.Errorf(codes.NotFound, "account address not found with account number %d", req.Id)
}

return &types.QueryAccountAddressByIDResponse{AccountAddress: address}, nil
Expand Down
15 changes: 13 additions & 2 deletions x/auth/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 982b891

Please sign in to comment.