From 37977d9c62e83e11e1db9e10cc4a1f95d4ad991a Mon Sep 17 00:00:00 2001 From: sujine2 Date: Sun, 28 Aug 2022 16:48:13 +0900 Subject: [PATCH] add test --- modules/apps/31-ibc-query/keeper/keeper.go | 29 +++++++--- .../apps/31-ibc-query/keeper/msg_server.go | 17 +++--- .../31-ibc-query/keeper/msg_server_test.go | 53 +++++++++++++++++++ modules/core/24-host/keys.go | 6 +++ 4 files changed, 86 insertions(+), 19 deletions(-) diff --git a/modules/apps/31-ibc-query/keeper/keeper.go b/modules/apps/31-ibc-query/keeper/keeper.go index 451a6d536de..86eea4e215a 100644 --- a/modules/apps/31-ibc-query/keeper/keeper.go +++ b/modules/apps/31-ibc-query/keeper/keeper.go @@ -18,10 +18,11 @@ type Keeper struct { } // NewKeeper creates a new 31-ibc-query Keeper instance -func NewKeeper(cdc codec.BinaryCodec, key sdk.StoreKey) Keeper { +func NewKeeper(cdc codec.BinaryCodec, key sdk.StoreKey,) Keeper { return Keeper{ cdc: cdc, storeKey: key, + } } @@ -56,20 +57,29 @@ func (k Keeper) SetNextQuerySequence(ctx sdk.Context, sequence uint64) { store.Set([]byte(types.KeyNextQuerySequence), bz) } -func (k Keeper) SetQuery(ctx sdk.Context, query types.MsgSubmitCrossChainQuery) string { +func (k Keeper) SetSubmitCrossChainQuery(ctx sdk.Context, query types.MsgSubmitCrossChainQuery) string { store := ctx.KVStore(k.storeKey) - appendedValue := k.cdc.MustMarshal(&query) + bz := k.cdc.MustMarshal(&query) - store.Set([]byte(host.QueryPath(query.Id)), appendedValue) + store.Set(host.QueryKey(query.Id), bz) return query.Id } +func (k Keeper) GetSubmitCrossChainQuery(ctx sdk.Context, queryId string) (types.MsgSubmitCrossChainQuery, bool) { + store := ctx.KVStore(k.storeKey) + bz := store.Get(host.QueryKey(queryId)) + if bz == nil { + return types.MsgSubmitCrossChainQuery{}, false + } + + var query types.MsgSubmitCrossChainQuery + k.cdc.MustUnmarshal(bz, &query) + + return query, true +} + -// TODO -// func handleIbcQuery -// 1. set unique query Id -// 2. set query in private store // TODO // func handleIbcQueryResult @@ -185,3 +195,6 @@ func (k Keeper) MustUnmarshalQueryResult(bz []byte) types.CrossChainQueryResult k.cdc.MustUnmarshal(bz, &result) return result } + + + diff --git a/modules/apps/31-ibc-query/keeper/msg_server.go b/modules/apps/31-ibc-query/keeper/msg_server.go index df13dfba498..9d02ff1cd8d 100644 --- a/modules/apps/31-ibc-query/keeper/msg_server.go +++ b/modules/apps/31-ibc-query/keeper/msg_server.go @@ -7,7 +7,6 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/ibc-go/v4/modules/apps/31-ibc-query/types" clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types" - host "github.com/cosmos/ibc-go/v4/modules/core/24-host" ) var _ types.MsgServer = Keeper{} @@ -24,14 +23,14 @@ func (k Keeper) SubmitCrossChainQuery(goCtx context.Context, msg *types.MsgSubmi if !(msg.LocalTimeoutHeight == 0 || msg.LocalTimeoutHeight > currentHeight.RevisionHeight){ return nil, sdkerrors.Wrapf( types.ErrInvalidTimeoutHeight, - "localTimeoutHeight is not 0 and current height >= localTimeoutHeight(%s >= %s)", currentHeight.RevisionHeight, msg.LocalTimeoutHeight, + "localTimeoutHeight is not 0 and current height >= localTimeoutHeight(%d >= %d)", currentHeight.RevisionHeight, msg.LocalTimeoutHeight, ) } // Sanity-check that localTimeoutTimestamp is 0 or greater than the current timestamp, otherwise the query will always time out. if !(msg.LocalTimeoutStamp == 0 || msg.LocalTimeoutStamp > currentTimestamp){ return nil, sdkerrors.Wrapf( types.ErrQuerytTimeout, - "localTimeoutTimestamp is not 0 and current timestamp >= localTimeoutTimestamp(%s >= %s)", currentTimestamp, msg.LocalTimeoutStamp, + "localTimeoutTimestamp is not 0 and current timestamp >= localTimeoutTimestamp(%d >= %d)", currentTimestamp, msg.LocalTimeoutStamp, ) } @@ -48,14 +47,10 @@ func (k Keeper) SubmitCrossChainQuery(goCtx context.Context, msg *types.MsgSubmi Sender: msg.Sender, } - k.SetQuery(ctx,query) + k.SetSubmitCrossChainQuery(ctx,query) - - capKey, err := k.scopedKeeper.NewCapability(ctx, host.QueryPath(msg.Id)) - if err != nil { - return nil, sdkerrors.Wrapf(err, "could not create query capability for query ID %s ", msg.Id) - } - + //TODO + //Capablity // Log the query request k.Logger(ctx).Info("query sent","query_id", msg.GetQueryId()) @@ -63,7 +58,7 @@ func (k Keeper) SubmitCrossChainQuery(goCtx context.Context, msg *types.MsgSubmi // emit event EmitQueryEvent(ctx, msg) - return &types.MsgSubmitCrossChainQueryResponse{Query: query, Index: capKey.Index}, nil + return &types.MsgSubmitCrossChainQueryResponse{Query: query}, nil } // SubmitCrossChainQueryResult Handling SubmitCrossChainQueryResult transaction diff --git a/modules/apps/31-ibc-query/keeper/msg_server_test.go b/modules/apps/31-ibc-query/keeper/msg_server_test.go index a384814fd57..a7deec3a48b 100644 --- a/modules/apps/31-ibc-query/keeper/msg_server_test.go +++ b/modules/apps/31-ibc-query/keeper/msg_server_test.go @@ -3,8 +3,61 @@ package keeper_test import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/v4/modules/apps/31-ibc-query/types" + clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types" ) + +var ( + timeoutHeight = clienttypes.NewHeight(0, 100) + timeoutTimestamp = uint64(0) +) + +func (suite *KeeperTestSuite) TestSubmitCrossChainQuery() { + var ( + msg *types.MsgSubmitCrossChainQuery + ) + + testCases := []struct { + name string + expPass bool + malleate func() + }{ + { + "success", + true, + func() { + msg = types.NewMsgSubmitCrossChainQuery("query-1", "test/query_path", timeoutHeight.RevisionHeight, timeoutTimestamp, 12, "client-1", "cosmos1234565") + }, + }, + } + + for _, tc := range testCases { + suite.SetupTest() + + tc.malleate() + res, err := suite.chainA.GetSimApp().IBCQueryKeeper.SubmitCrossChainQuery(sdk.WrapSDKContext(suite.chainA.GetContext()), msg) + + if tc.expPass { + suite.Require().NoError(err) + suite.Require().NotNil(res) + queryResult, found := suite.chainA.GetSimApp().IBCQueryKeeper.GetSubmitCrossChainQuery(suite.chainA.GetContext(), "query-1") + + suite.Require().True(found) + suite.Require().Equal("query-1", queryResult.Id) + suite.Require().Equal("test/query_path", queryResult.Path) + suite.Require().Equal(timeoutHeight.RevisionHeight, queryResult.LocalTimeoutHeight) + suite.Require().Equal(timeoutTimestamp, queryResult.LocalTimeoutStamp) + suite.Require().Equal(uint64(0xc), queryResult.QueryHeight) + suite.Require().Equal("client-1", queryResult.ClientId) + suite.Require().Equal("cosmos1234565", queryResult.Sender) + } else { + suite.Require().Error(err) + } + } +} + + + func (suite *KeeperTestSuite) TestSubmitCrossChainQueryResult() { var ( msg *types.MsgSubmitCrossChainQueryResult diff --git a/modules/core/24-host/keys.go b/modules/core/24-host/keys.go index 1e618a38bef..03bf53710a5 100644 --- a/modules/core/24-host/keys.go +++ b/modules/core/24-host/keys.go @@ -219,10 +219,16 @@ func PacketReceiptKey(portID, channelID string, sequence uint64) []byte { return []byte(PacketReceiptPath(portID, channelID, sequence)) } +//ICS31 + func QueryPath(queryID string) string { return fmt.Sprintf("%s/%s", KeyQueryPrefix, queryID) } +func QueryKey(queryID string) []byte { + return []byte(QueryPath(queryID)) +} + func channelPath(portID, channelID string) string { return fmt.Sprintf("%s/%s/%s/%s", KeyPortPrefix, portID, KeyChannelPrefix, channelID) }