Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(packet-server): add queryServer to packet-server #7283

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/core/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/cosmos/ibc-go/v9/modules/core/client/cli"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
"github.com/cosmos/ibc-go/v9/modules/core/keeper"
packetserverkeeper "github.com/cosmos/ibc-go/v9/modules/core/packet-server/keeper"
packetservertypes "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
"github.com/cosmos/ibc-go/v9/modules/core/simulation"
"github.com/cosmos/ibc-go/v9/modules/core/types"
Expand Down Expand Up @@ -92,6 +93,10 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r
if err != nil {
panic(err)
}
err = packetservertypes.RegisterQueryHandlerClient(context.Background(), mux, packetservertypes.NewQueryClient(clientCtx))
if err != nil {
panic(err)
}
}

// GetTxCmd returns the root tx command for the ibc module.
Expand Down Expand Up @@ -138,6 +143,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
clienttypes.RegisterQueryServer(cfg.QueryServer(), clientkeeper.NewQueryServer(am.keeper.ClientKeeper))
connectiontypes.RegisterQueryServer(cfg.QueryServer(), connectionkeeper.NewQueryServer(am.keeper.ConnectionKeeper))
channeltypes.RegisterQueryServer(cfg.QueryServer(), channelkeeper.NewQueryServer(am.keeper.ChannelKeeper))
packetservertypes.RegisterQueryServer(cfg.QueryServer(), packetserverkeeper.NewQueryServer(am.keeper.PacketServerKeeper))

clientMigrator := clientkeeper.NewMigrator(am.keeper.ClientKeeper)
if err := cfg.RegisterMigration(exported.ModuleName, 2, clientMigrator.Migrate2to3); err != nil {
Expand Down
49 changes: 49 additions & 0 deletions modules/core/packet-server/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package keeper

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

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

host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
"github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
)

var _ types.QueryServer = (*queryServer)(nil)

// queryServer implements the packet-server types.QueryServer interface.
type queryServer struct {
*Keeper
}

// NewQueryServer returns a new types.QueryServer implementation.
func NewQueryServer(k *Keeper) types.QueryServer {
return &queryServer{
Keeper: k,
}
}

// Client implements the Query/Client gRPC method
func (q *queryServer) Client(ctx context.Context, req *types.QueryClientRequest) (*types.QueryClientResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if err := host.ClientIdentifierValidator(req.ClientId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

res := types.QueryClientResponse{}

sdkCtx := sdk.UnwrapSDKContext(ctx)
creator, _ := q.ClientKeeper.GetCreator(sdkCtx, req.ClientId)
res.Creator = creator

counterparty, _ := q.GetCounterparty(sdkCtx, req.ClientId)
res.Counterparty = counterparty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it safe to discard these found vars? Should we error out in this case instead of returning an empty response?

Copy link
Contributor Author

@DimitrisJim DimitrisJim Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, update, I remembered these can exist independetly until provide counterparty is called. I.e only creator and then only counterparty. I guess if both are not found I can error.


return &res, nil
}
109 changes: 109 additions & 0 deletions modules/core/packet-server/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package keeper_test

import (
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
"github.com/cosmos/ibc-go/v9/modules/core/packet-server/keeper"
"github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
)

func (suite *KeeperTestSuite) TestQueryClient() {
var (
req *types.QueryClientRequest
expCreator string
expCounterparty types.Counterparty
)

testCases := []struct {
msg string
malleate func()
expError error
}{
{
"success",
func() {
ctx := suite.chainA.GetContext()
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCreator(ctx, ibctesting.FirstClientID, expCreator)
suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCounterparty(ctx, ibctesting.FirstClientID, expCounterparty)

req = &types.QueryClientRequest{
ClientId: ibctesting.FirstClientID,
}
},
nil,
},
{
"success: no creator",
func() {
expCreator = ""

suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCounterparty(suite.chainA.GetContext(), ibctesting.FirstClientID, expCounterparty)

req = &types.QueryClientRequest{
ClientId: ibctesting.FirstClientID,
}
},
nil,
},
{
"success: no counterparty",
func() {
expCounterparty = types.Counterparty{}

suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCreator(suite.chainA.GetContext(), ibctesting.FirstClientID, expCreator)

req = &types.QueryClientRequest{
ClientId: ibctesting.FirstClientID,
}
},
nil,
},
{
"req is nil",
func() {
req = nil
},
status.Error(codes.InvalidArgument, "empty request"),
},
{
"invalid clientID",
func() {
req = &types.QueryClientRequest{}
},
status.Error(codes.InvalidArgument, "identifier cannot be blank: invalid identifier"),
},
}

for _, tc := range testCases {
tc := tc

suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

expCreator = ibctesting.TestAccAddress
merklePathPrefix := commitmenttypes.NewMerklePath([]byte("prefix"))
expCounterparty = types.Counterparty{ClientId: ibctesting.SecondClientID, MerklePathPrefix: merklePathPrefix}

tc.malleate()

queryServer := keeper.NewQueryServer(suite.chainA.GetSimApp().IBCKeeper.PacketServerKeeper)
res, err := queryServer.Client(suite.chainA.GetContext(), req)

expPass := tc.expError == nil
if expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expCreator, res.Creator)
suite.Require().Equal(expCounterparty, res.Counterparty)
} else {
suite.Require().ErrorIs(err, tc.expError)
suite.Require().Nil(res)
}
})
}
}
3 changes: 3 additions & 0 deletions modules/core/packet-server/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ type ClientKeeper interface {
// GetClientTimestampAtHeight returns the timestamp for a given height on the client
// given its client ID and height
GetClientTimestampAtHeight(ctx sdk.Context, clientID string, height exported.Height) (uint64, error)

// GetCreator returns the creator of the client denoted by the clientID.
GetCreator(ctx sdk.Context, clientID string) (string, bool)
}
Loading
Loading