Skip to content

Commit

Permalink
feat: add provider info query (#1164)
Browse files Browse the repository at this point in the history
* feat: add provider info query

* feat: refactor query providerInfo

* feat: query provider info

* feat: query all provider info

* feat: add provider info query cli

* feat: add consumer info to provider info query

* feat: add consumer chain-id to provider info query

* fix: return err when client, channel not found

* fix: lint

* chore: fix lint

* chore: lint fix

* chore: add nonlint

* chore: add nonlint to the right place

* chore: add nolint nolintlint

* chore: nolint all

* chore: nolint:golint

* chore: fix package import order

* chore: update queryProviderChainInfo to queryProviderInfo

* chore: update proto

* update query.go

* docs: update changelog
  • Loading branch information
yaruwangway authored Aug 9, 2023
1 parent 006680a commit aaa545d
Show file tree
Hide file tree
Showing 9 changed files with 940 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Add an entry to the unreleased section whenever merging a PR to main that is not
* (fix!) revert consumer packet data changes from #1037 [#1150](https://github.com/cosmos/interchain-security/pull/1150)
* (fix!) proper deletion of pending packets [#1146](https://github.com/cosmos/interchain-security/pull/1146)
* (feat!) optimize pending packets storage on consumer, with migration! [#1037](https://github.com/cosmos/interchain-security/pull/1037)
* (feat) introduce the gRPC query `/interchain_security/ccv/consumer/provider-info` and CLI command `interchain-security-cd q ccvconsumer provider-info`to retrieve provider info from the consumer chain.

## v3.1.0

Expand Down
18 changes: 18 additions & 0 deletions proto/interchain_security/ccv/consumer/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ service Query {
rpc QueryParams(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/interchain_security/ccv/consumer/params";
}

rpc QueryProviderInfo(QueryProviderInfoRequest) returns (QueryProviderInfoResponse) {
option (google.api.http).get = "/interchain_security/ccv/consumer/provider-info";
}
}

// NextFeeDistributionEstimate holds information about next fee distribution
Expand Down Expand Up @@ -52,3 +56,17 @@ message QueryParamsResponse {
// params holds all the parameters of this module.
Params params = 1 [ (gogoproto.nullable) = false ];
}

message QueryProviderInfoRequest {}

message QueryProviderInfoResponse {
ChainInfo consumer = 1 [ (gogoproto.nullable) = false ];
ChainInfo provider = 2 [ (gogoproto.nullable) = false ];
}

message ChainInfo {
string chainID = 1;
string clientID = 2;
string connectionID = 3;
string channelID = 4;
}
10 changes: 10 additions & 0 deletions testutil/keeper/mocks.go

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

32 changes: 31 additions & 1 deletion x/ccv/consumer/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func NewQueryCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdNextFeeDistribution())
cmd.AddCommand(
CmdNextFeeDistribution(),
CmdProviderInfo(),
)

return cmd
}
Expand Down Expand Up @@ -50,3 +53,30 @@ func CmdNextFeeDistribution() *cobra.Command {

return cmd
}

func CmdProviderInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "provider-info",
Short: "Query provider info",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryProviderInfoRequest{}
res, err := queryClient.QueryProviderInfo(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
17 changes: 14 additions & 3 deletions x/ccv/consumer/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
)

var _ types.QueryServer = Keeper{}
var _ types.QueryServer = Keeper{} //nolint:golint

func (k Keeper) QueryNextFeeDistribution(c context.Context,
func (k Keeper) QueryNextFeeDistribution(c context.Context, //nolint:golint
req *types.QueryNextFeeDistributionEstimateRequest,
) (*types.QueryNextFeeDistributionEstimateResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
Expand All @@ -27,7 +27,7 @@ func (k Keeper) QueryNextFeeDistribution(c context.Context,
return &types.QueryNextFeeDistributionEstimateResponse{Data: &nextDist}, nil
}

func (k Keeper) QueryParams(c context.Context,
func (k Keeper) QueryParams(c context.Context, //nolint:golint
req *types.QueryParamsRequest,
) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
Expand All @@ -40,3 +40,14 @@ func (k Keeper) QueryParams(c context.Context,

return &types.QueryParamsResponse{Params: p}, nil
}

func (k Keeper) QueryProviderInfo(c context.Context, //nolint:golint
req *types.QueryProviderInfoRequest,
) (*types.QueryProviderInfoResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

return k.GetProviderInfo(ctx)
}
54 changes: 54 additions & 0 deletions x/ccv/consumer/keeper/provider_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package keeper

import (
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" //nolint:golint

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types"
)

func (k Keeper) GetProviderInfo(ctx sdk.Context) (*types.QueryProviderInfoResponse, error) { //nolint:golint
consumerChannelID, found := k.GetProviderChannel(ctx)
if !found {
return nil, ccvtypes.ErrChannelNotFound
}
consumerChannel, found := k.channelKeeper.GetChannel(ctx, ccvtypes.ConsumerPortID, consumerChannelID)
if !found {
return nil, ccvtypes.ErrChannelNotFound
}

// from channel get connection
consumerConnectionID, consumerConnection, err := k.channelKeeper.GetChannelConnection(ctx, ccvtypes.ConsumerPortID, consumerChannelID)
if err != nil {
return nil, err
}

providerChannelID := consumerChannel.GetCounterparty().GetChannelID()
providerConnection := consumerConnection.GetCounterparty()

consumerClientState, found := k.clientKeeper.GetClientState(ctx, consumerConnection.GetClientID())
if !found {
return nil, ccvtypes.ErrClientNotFound
}
providerChainID := consumerClientState.(*ibctm.ClientState).ChainId

resp := types.QueryProviderInfoResponse{
Consumer: types.ChainInfo{
ChainID: ctx.ChainID(),
ClientID: consumerConnection.GetClientID(),
ConnectionID: consumerConnectionID,
ChannelID: consumerChannelID,
},

Provider: types.ChainInfo{
ChainID: providerChainID,
ClientID: providerConnection.GetClientID(),
ConnectionID: providerConnection.GetConnectionID(),
ChannelID: providerChannelID,
},
}

return &resp, nil
}
Loading

0 comments on commit aaa545d

Please sign in to comment.