Skip to content

Commit 75f7379

Browse files
committed
chore(packet-server): add queryServer to packet-server
1 parent 37d3d39 commit 75f7379

File tree

7 files changed

+1027
-0
lines changed

7 files changed

+1027
-0
lines changed

modules/core/module.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/cosmos/ibc-go/v9/modules/core/client/cli"
2828
"github.com/cosmos/ibc-go/v9/modules/core/exported"
2929
"github.com/cosmos/ibc-go/v9/modules/core/keeper"
30+
packetserverkeeper "github.com/cosmos/ibc-go/v9/modules/core/packet-server/keeper"
3031
packetservertypes "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
3132
"github.com/cosmos/ibc-go/v9/modules/core/simulation"
3233
"github.com/cosmos/ibc-go/v9/modules/core/types"
@@ -92,6 +93,10 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r
9293
if err != nil {
9394
panic(err)
9495
}
96+
err = packetservertypes.RegisterQueryHandlerClient(context.Background(), mux, packetservertypes.NewQueryClient(clientCtx))
97+
if err != nil {
98+
panic(err)
99+
}
95100
}
96101

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

142148
clientMigrator := clientkeeper.NewMigrator(am.keeper.ClientKeeper)
143149
if err := cfg.RegisterMigration(exported.ModuleName, 2, clientMigrator.Migrate2to3); err != nil {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package keeper
2+
3+
import (
4+
"context"
5+
6+
"google.golang.org/grpc/codes"
7+
"google.golang.org/grpc/status"
8+
9+
sdk "github.com/cosmos/cosmos-sdk/types"
10+
11+
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
12+
"github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
13+
)
14+
15+
var _ types.QueryServer = (*queryServer)(nil)
16+
17+
// queryServer implements the packet-server types.QueryServer interface.
18+
type queryServer struct {
19+
*Keeper
20+
}
21+
22+
// NewQueryServer returns a new types.QueryServer implementation.
23+
func NewQueryServer(k *Keeper) types.QueryServer {
24+
return &queryServer{
25+
Keeper: k,
26+
}
27+
}
28+
29+
// Client implements the Query/Client gRPC method
30+
func (q *queryServer) Client(ctx context.Context, req *types.QueryClientRequest) (*types.QueryClientResponse, error) {
31+
if req == nil {
32+
return nil, status.Error(codes.InvalidArgument, "empty request")
33+
}
34+
35+
if err := host.ClientIdentifierValidator(req.ClientId); err != nil {
36+
return nil, status.Error(codes.InvalidArgument, err.Error())
37+
}
38+
39+
res := types.QueryClientResponse{}
40+
41+
sdkCtx := sdk.UnwrapSDKContext(ctx)
42+
creator, _ := q.ClientKeeper.GetCreator(sdkCtx, req.ClientId)
43+
res.Creator = creator
44+
45+
counterparty, _ := q.GetCounterparty(sdkCtx, req.ClientId)
46+
res.Counterparty = counterparty
47+
48+
return &res, nil
49+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package keeper_test
2+
3+
import (
4+
"fmt"
5+
6+
"google.golang.org/grpc/codes"
7+
"google.golang.org/grpc/status"
8+
9+
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
10+
"github.com/cosmos/ibc-go/v9/modules/core/packet-server/keeper"
11+
"github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
12+
ibctesting "github.com/cosmos/ibc-go/v9/testing"
13+
)
14+
15+
func (suite *KeeperTestSuite) TestQueryClient() {
16+
var (
17+
req *types.QueryClientRequest
18+
expCreator string
19+
expCounterparty types.Counterparty
20+
)
21+
22+
testCases := []struct {
23+
msg string
24+
malleate func()
25+
expError error
26+
}{
27+
{
28+
"success",
29+
func() {
30+
ctx := suite.chainA.GetContext()
31+
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCreator(ctx, ibctesting.FirstClientID, expCreator)
32+
suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCounterparty(ctx, ibctesting.FirstClientID, expCounterparty)
33+
34+
req = &types.QueryClientRequest{
35+
ClientId: ibctesting.FirstClientID,
36+
}
37+
},
38+
nil,
39+
},
40+
{
41+
"success: no creator",
42+
func() {
43+
expCreator = ""
44+
45+
suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCounterparty(suite.chainA.GetContext(), ibctesting.FirstClientID, expCounterparty)
46+
47+
req = &types.QueryClientRequest{
48+
ClientId: ibctesting.FirstClientID,
49+
}
50+
},
51+
nil,
52+
},
53+
{
54+
"success: no counterparty",
55+
func() {
56+
expCounterparty = types.Counterparty{}
57+
58+
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCreator(suite.chainA.GetContext(), ibctesting.FirstClientID, expCreator)
59+
60+
req = &types.QueryClientRequest{
61+
ClientId: ibctesting.FirstClientID,
62+
}
63+
},
64+
nil,
65+
},
66+
{
67+
"req is nil",
68+
func() {
69+
req = nil
70+
},
71+
status.Error(codes.InvalidArgument, "empty request"),
72+
},
73+
{
74+
"invalid clientID",
75+
func() {
76+
req = &types.QueryClientRequest{}
77+
},
78+
status.Error(codes.InvalidArgument, "identifier cannot be blank: invalid identifier"),
79+
},
80+
}
81+
82+
for _, tc := range testCases {
83+
tc := tc
84+
85+
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
86+
suite.SetupTest() // reset
87+
88+
expCreator = ibctesting.TestAccAddress
89+
merklePathPrefix := commitmenttypes.NewMerklePath([]byte("prefix"))
90+
expCounterparty = types.Counterparty{ClientId: ibctesting.SecondClientID, MerklePathPrefix: merklePathPrefix}
91+
92+
tc.malleate()
93+
94+
queryServer := keeper.NewQueryServer(suite.chainA.GetSimApp().IBCKeeper.PacketServerKeeper)
95+
res, err := queryServer.Client(suite.chainA.GetContext(), req)
96+
97+
expPass := tc.expError == nil
98+
if expPass {
99+
suite.Require().NoError(err)
100+
suite.Require().NotNil(res)
101+
suite.Require().Equal(expCreator, res.Creator)
102+
suite.Require().Equal(expCounterparty, res.Counterparty)
103+
} else {
104+
suite.Require().ErrorIs(err, tc.expError)
105+
suite.Require().Nil(res)
106+
}
107+
})
108+
}
109+
}

modules/core/packet-server/types/expected_keepers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ type ClientKeeper interface {
5252
// GetClientTimestampAtHeight returns the timestamp for a given height on the client
5353
// given its client ID and height
5454
GetClientTimestampAtHeight(ctx sdk.Context, clientID string, height exported.Height) (uint64, error)
55+
56+
// GetCreator returns the creator of the client denoted by the clientID.
57+
GetCreator(ctx sdk.Context, clientID string) (string, bool)
5558
}

0 commit comments

Comments
 (0)