Skip to content

Commit

Permalink
chore(packet-server): add queryServer to packet-server
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim committed Sep 11, 2024
1 parent 37d3d39 commit 75f7379
Show file tree
Hide file tree
Showing 7 changed files with 1,027 additions and 0 deletions.
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

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

0 comments on commit 75f7379

Please sign in to comment.