Skip to content

Commit

Permalink
add cli to get denom hash from trace path and base denom (#679)
Browse files Browse the repository at this point in the history
* get denom hash from denom trace

* revert .gitignore

* final revert

* ignore history files

* Update modules/apps/transfer/client/cli/query.go

Co-authored-by: Carlos Rodriguez <[email protected]>

* Update modules/apps/transfer/keeper/grpc_query.go

Co-authored-by: Carlos Rodriguez <[email protected]>

* Update modules/apps/transfer/keeper/keeper.go

Co-authored-by: Carlos Rodriguez <[email protected]>

* Update modules/apps/transfer/keeper/grpc_query.go

Co-authored-by: colin axnér <[email protected]>

* code review feedback integration

* moved new feature  to unreleased section

* fix duplicate block in success test case

* fix CHANGELOG format

* fix invalid trace path argument error handling

* refactor positive test case code

Co-authored-by: Carlos Rodriguez <[email protected]>
Co-authored-by: colin axnér <[email protected]>
(cherry picked from commit cb9bf5d)

# Conflicts:
#	CHANGELOG.md
#	modules/apps/transfer/types/query.pb.go
  • Loading branch information
nir1218 authored and mergify-bot committed Feb 23, 2022
1 parent b9e1306 commit d44983c
Show file tree
Hide file tree
Showing 11 changed files with 789 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ dependency-graph.png
*.aux
*.out
*.synctex.gz

*.history

15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* (channel) [\#692](https://github.com/cosmos/ibc-go/pull/692) Minimize channel logging by only emitting the packet sequence, source port/channel, destination port/channel upon packet receives, acknowledgements and timeouts.
<<<<<<< HEAD
=======
* [\#383](https://github.com/cosmos/ibc-go/pull/383) Adds helper functions for merging and splitting middleware versions from the underlying app version.
* (modules/core/05-port) [\#288](https://github.com/cosmos/ibc-go/issues/288) Making the 05-port keeper function IsBound public. The IsBound function checks if the provided portID is already binded to a module.
* (channel) [\#644](https://github.com/cosmos/ibc-go/pull/644) Adds `GetChannelConnection` to the ChannelKeeper. This function returns the connectionID and connection state associated with a channel.
* (channel) [\647](https://github.com/cosmos/ibc-go/pull/647) Reorganizes channel handshake handling to set channel state after IBC application callbacks.

### Features

* [\#432](https://github.com/cosmos/ibc-go/pull/432) Introduce `MockIBCApp` struct to the mock module. Allows the mock module to be reused to perform custom logic on each IBC App interface function. This might be useful when testing out IBC applications written as middleware.
* [\#380](https://github.com/cosmos/ibc-go/pull/380) Adding the Interchain Accounts module v1
* [\#679](https://github.com/cosmos/ibc-go/pull/679) New CLI command `query ibc-transfer denom-hash <denom trace>` to get the denom hash for a denom trace; this might be useful for debug

### Bug Fixes
>>>>>>> cb9bf5d (add cli to get denom hash from trace path and base denom (#679))
## [v2.0.2](https://github.com/cosmos/ibc-go/releases/tag/v2.0.2) - 2021-12-15

Expand Down
66 changes: 66 additions & 0 deletions docs/client/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,54 @@ info:
description: A REST interface for state queries
version: 1.0.0
paths:
'/ibc/apps/transfer/v1/denom_hashes/{trace}':
get:
summary: DenomHash queries a denomination hash information.
operationId: DenomHash
responses:
'200':
description: A successful response.
schema:
type: object
properties:
hash:
type: string
description: hash (in hex format) of the denomination trace information.
description: >-
QueryDenomHashResponse is the response type for the
Query/DenomHash RPC
method.
default:
description: An unexpected error response
schema:
type: object
properties:
error:
type: string
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
type_url:
type: string
value:
type: string
format: byte
parameters:
- name: trace
description: 'The denomination trace ([port_id]/[channel_id])+/[denom]'
in: path
required: true
type: string
tags:
- Query
/ibc/apps/transfer/v1/denom_traces:
get:
summary: DenomTraces queries all denomination traces.
Expand Down Expand Up @@ -7326,6 +7374,15 @@ paths:
required: false
type: boolean
format: boolean
- name: packet_commitment_sequences
description: list of packet sequences.
in: query
required: false
type: array
items:
type: string
format: uint64
collectionFormat: multi
tags:
- Query
'/ibc/core/channel/v1/channels/{channel_id}/ports/{port_id}/packet_acks/{sequence}':
Expand Down Expand Up @@ -9839,6 +9896,15 @@ definitions:
SendEnabled
parameter for the denomination to false.
ibc.applications.transfer.v1.QueryDenomHashResponse:
type: object
properties:
hash:
type: string
description: hash (in hex format) of the denomination trace information.
description: |-
QueryDenomHashResponse is the response type for the Query/DenomHash RPC
method.
ibc.applications.transfer.v1.QueryDenomTraceResponse:
type: object
properties:
Expand Down
35 changes: 35 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- [GenesisState](#ibc.applications.transfer.v1.GenesisState)

- [ibc/applications/transfer/v1/query.proto](#ibc/applications/transfer/v1/query.proto)
- [QueryDenomHashRequest](#ibc.applications.transfer.v1.QueryDenomHashRequest)
- [QueryDenomHashResponse](#ibc.applications.transfer.v1.QueryDenomHashResponse)
- [QueryDenomTraceRequest](#ibc.applications.transfer.v1.QueryDenomTraceRequest)
- [QueryDenomTraceResponse](#ibc.applications.transfer.v1.QueryDenomTraceResponse)
- [QueryDenomTracesRequest](#ibc.applications.transfer.v1.QueryDenomTracesRequest)
Expand Down Expand Up @@ -346,6 +348,38 @@ GenesisState defines the ibc-transfer genesis state



<a name="ibc.applications.transfer.v1.QueryDenomHashRequest"></a>

### QueryDenomHashRequest
QueryDenomHashRequest is the request type for the Query/DenomHash RPC
method


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `trace` | [string](#string) | | The denomination trace ([port_id]/[channel_id])+/[denom] |






<a name="ibc.applications.transfer.v1.QueryDenomHashResponse"></a>

### QueryDenomHashResponse
QueryDenomHashResponse is the response type for the Query/DenomHash RPC
method.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `hash` | [string](#string) | | hash (in hex format) of the denomination trace information. |






<a name="ibc.applications.transfer.v1.QueryDenomTraceRequest"></a>

### QueryDenomTraceRequest
Expand Down Expand Up @@ -452,6 +486,7 @@ Query provides defines the gRPC querier service.
| `DenomTrace` | [QueryDenomTraceRequest](#ibc.applications.transfer.v1.QueryDenomTraceRequest) | [QueryDenomTraceResponse](#ibc.applications.transfer.v1.QueryDenomTraceResponse) | DenomTrace queries a denomination trace information. | GET|/ibc/apps/transfer/v1/denom_traces/{hash}|
| `DenomTraces` | [QueryDenomTracesRequest](#ibc.applications.transfer.v1.QueryDenomTracesRequest) | [QueryDenomTracesResponse](#ibc.applications.transfer.v1.QueryDenomTracesResponse) | DenomTraces queries all denomination traces. | GET|/ibc/apps/transfer/v1/denom_traces|
| `Params` | [QueryParamsRequest](#ibc.applications.transfer.v1.QueryParamsRequest) | [QueryParamsResponse](#ibc.applications.transfer.v1.QueryParamsResponse) | Params queries all parameters of the ibc-transfer module. | GET|/ibc/apps/transfer/v1/params|
| `DenomHash` | [QueryDenomHashRequest](#ibc.applications.transfer.v1.QueryDenomHashRequest) | [QueryDenomHashResponse](#ibc.applications.transfer.v1.QueryDenomHashResponse) | DenomHash queries a denomination hash information. | GET|/ibc/apps/transfer/v1/denom_hashes/{trace}|

<!-- end services -->

Expand Down
1 change: 1 addition & 0 deletions modules/apps/transfer/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func GetQueryCmd() *cobra.Command {
GetCmdQueryDenomTraces(),
GetCmdParams(),
GetCmdQueryEscrowAddress(),
GetCmdQueryDenomHash(),
)

return queryCmd
Expand Down
32 changes: 32 additions & 0 deletions modules/apps/transfer/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,35 @@ func GetCmdQueryEscrowAddress() *cobra.Command {

return cmd
}

// GetCmdQueryDenomHash defines the command to query a denomination hash from a given trace.
func GetCmdQueryDenomHash() *cobra.Command {
cmd := &cobra.Command{
Use: "denom-hash [trace]",
Short: "Query the denom hash info from a given denom trace",
Long: "Query the denom hash info from a given denom trace",
Example: fmt.Sprintf("%s query ibc-transfer denom-hash [denom_trace]", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryDenomHashRequest{
Trace: args[0],
}

res, err := queryClient.DenomHash(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
27 changes: 27 additions & 0 deletions modules/apps/transfer/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,30 @@ func (q Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.Q
Params: &params,
}, nil
}

// DenomHash implements the Query/DenomHash gRPC method
func (q Keeper) DenomHash(c context.Context, req *types.QueryDenomHashRequest) (*types.QueryDenomHashResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

// Convert given request trace path to DenomTrace struct to confirm the path in a valid denom trace format
denomTrace := types.ParseDenomTrace(req.Trace)
if err := denomTrace.Validate(); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

ctx := sdk.UnwrapSDKContext(c)
denomHash := denomTrace.Hash()
found := q.HasDenomTrace(ctx, denomHash)
if !found {
return nil, status.Error(
codes.NotFound,
sdkerrors.Wrap(types.ErrTraceNotFound, req.Trace).Error(),
)
}

return &types.QueryDenomHashResponse{
Hash: denomHash.String(),
}, nil
}
67 changes: 67 additions & 0 deletions modules/apps/transfer/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,70 @@ func (suite *KeeperTestSuite) TestQueryParams() {
res, _ := suite.queryClient.Params(ctx, &types.QueryParamsRequest{})
suite.Require().Equal(&expParams, res.Params)
}

func (suite *KeeperTestSuite) TestQueryDenomHash() {

reqTrace := types.DenomTrace{
Path: "transfer/channelToA/transfer/channelToB",
BaseDenom: "uatom",
}

var (
req *types.QueryDenomHashRequest
expHash = reqTrace.Hash().String()
)

testCases := []struct {
msg string
malleate func()
expPass bool
}{
{
"invalid trace",
func() {
req = &types.QueryDenomHashRequest{
Trace: "transfer/channelToA/transfer/",
}
},
false,
},
{
"not found denom trace",
func() {
req = &types.QueryDenomHashRequest{
Trace: "transfer/channelToC/uatom",
}
},
false,
},
{
"success",
func() {},
true,
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

req = &types.QueryDenomHashRequest{
Trace: reqTrace.GetFullDenomPath(),
}
suite.chainA.GetSimApp().TransferKeeper.SetDenomTrace(suite.chainA.GetContext(), reqTrace)

tc.malleate()
ctx := sdk.WrapSDKContext(suite.chainA.GetContext())

res, err := suite.queryClient.DenomHash(ctx, req)

if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expHash, res.Hash)
} else {
suite.Require().Error(err)
}
})
}
}
Loading

0 comments on commit d44983c

Please sign in to comment.