-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(packet-server): add queryServer to packet-server
- Loading branch information
1 parent
37d3d39
commit 75f7379
Showing
7 changed files
with
1,027 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.