Skip to content

Commit

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

* Revert "chore: change id to use uint64 in `AccountAddressByID` (cosmos#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>
  • Loading branch information
2 people authored and Wryhder committed Oct 26, 2022
1 parent 18c0d0f commit 5c4feab
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 110 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/genutil)[#12956](https://github.com/cosmos/cosmos-sdk/pull/12956) `genutil.AppModuleBasic` has a new attribute: genesis transaction validation function. The existing validation logic is implemented in `genutiltypes.DefaultMessageValidator`. Use `genutil.NewAppModuleBasic` to create a new genutil Module Basic.
* (codec) [#12964](https://github.com/cosmos/cosmos-sdk/pull/12964) `ProtoCodec.MarshalInterface` now returns an error when serializing unregistered types and a subsequent `ProtoCodec.UnmarshalInterface` would fail.
* (x/staking) [#12973](https://github.com/cosmos/cosmos-sdk/pull/12973) Removed `stakingkeeper.RandomValidator`. Use `testutil.RandSliceElem(r, sk.GetAllValidators(ctx))` instead.
* (x/gov) [13160](https://github.com/cosmos/cosmos-sdk/pull/13160) Remove custom marshaling of proposl and voteoption.
* (x/auth) [13411](https://github.com/cosmos/cosmos-sdk/pull/13411) Change account id to use uint64 in `AccountAddressByID` grpc query.
* (types) [13430](https://github.com/cosmos/cosmos-sdk/pull/13430) Remove unused code `ResponseCheckTx` and `ResponseDeliverTx`
* (x/gov) [#13160](https://github.com/cosmos/cosmos-sdk/pull/13160) Remove custom marshaling of proposl and voteoption.
* (types) [#13430](https://github.com/cosmos/cosmos-sdk/pull/13430) Remove unused code `ResponseCheckTx` and `ResponseDeliverTx`
* (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.

### CLI Breaking Changes

Expand Down
33 changes: 22 additions & 11 deletions api/cosmos/auth/v1beta1/query.pulsar.go

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

4 changes: 4 additions & 0 deletions api/cosmos/auth/v1beta1/query_grpc.pb.go

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

4 changes: 2 additions & 2 deletions api/cosmos/distribution/v1beta1/query_grpc.pb.go

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

16 changes: 8 additions & 8 deletions api/cosmos/group/v1/types.pulsar.go

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

2 changes: 1 addition & 1 deletion client/grpc/tmservice/query.pb.go

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

17 changes: 15 additions & 2 deletions proto/cosmos/auth/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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 (cosmos.query.v1.module_query_safe) = true;
option (google.api.http).get = "/cosmos/auth/v1beta1/address_by_id/{id}";
Expand Down Expand Up @@ -180,23 +182,34 @@ message AddressStringToBytesResponse {
}

// QueryAccountAddressByIDRequest is the request type for AccountAddressByID rpc method
//
// Since: cosmos-sdk 0.46.2
message QueryAccountAddressByIDRequest {
uint64 id = 1;
// 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"];
}

// QueryAccountInfoRequest is the Query/AccountInfo request type.
//
// Since: cosmos-sdk 0.47
message QueryAccountInfoRequest {

// address is the account address string.
string address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

// QueryAccountInfoResponse is the Query/AccountInfo response type.
//
// Since: cosmos-sdk 0.47
message QueryAccountInfoResponse {

// info is the account info which is represented by BaseAccount.
Expand Down
2 changes: 1 addition & 1 deletion store/types/commit_info.pb.go

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

13 changes: 7 additions & 6 deletions x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
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.ParseUint(args[0], 10, 64)
accNum, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return fmt.Errorf("id %s not a valid uint, please input a valid account-id", args[0])
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
8 changes: 6 additions & 2 deletions x/auth/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ func (ak AccountKeeper) AccountAddressByID(c context.Context, req *types.QueryAc
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

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

ctx := sdk.UnwrapSDKContext(c)
address := ak.GetAccountAddressByID(ctx, req.Id)
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
10 changes: 9 additions & 1 deletion x/auth/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ func (suite *KeeperTestSuite) TestGRPCQueryAccountAddressByID() {
expPass bool
posttests func(res *types.QueryAccountAddressByIDResponse)
}{
{
"invalid request",
func() {
req = &types.QueryAccountAddressByIDRequest{Id: -1}
},
false,
func(res *types.QueryAccountAddressByIDResponse) {},
},
{
"account address not found",
func() {
Expand All @@ -179,7 +187,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryAccountAddressByID() {
func() {
account := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr)
suite.accountKeeper.SetAccount(suite.ctx, account)
req = &types.QueryAccountAddressByIDRequest{Id: account.GetAccountNumber()}
req = &types.QueryAccountAddressByIDRequest{Id: int64(account.GetAccountNumber())}
},
true,
func(res *types.QueryAccountAddressByIDResponse) {
Expand Down
Loading

0 comments on commit 5c4feab

Please sign in to comment.