Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
sujine2 authored and boosik committed Sep 5, 2022
1 parent 952d8a3 commit 37977d9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 19 deletions.
29 changes: 21 additions & 8 deletions modules/apps/31-ibc-query/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -185,3 +195,6 @@ func (k Keeper) MustUnmarshalQueryResult(bz []byte) types.CrossChainQueryResult
k.cdc.MustUnmarshal(bz, &result)
return result
}



17 changes: 6 additions & 11 deletions modules/apps/31-ibc-query/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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,
)
}

Expand All @@ -48,22 +47,18 @@ 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())

// emit event
EmitQueryEvent(ctx, msg)

return &types.MsgSubmitCrossChainQueryResponse{Query: query, Index: capKey.Index}, nil
return &types.MsgSubmitCrossChainQueryResponse{Query: query}, nil
}

// SubmitCrossChainQueryResult Handling SubmitCrossChainQueryResult transaction
Expand Down
53 changes: 53 additions & 0 deletions modules/apps/31-ibc-query/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions modules/core/24-host/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 37977d9

Please sign in to comment.