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

Commit

Permalink
feat: add CrossChainQuery type
Browse files Browse the repository at this point in the history
  • Loading branch information
Woojinger committed Sep 12, 2022
1 parent 811e86a commit 93b5aa8
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 157 deletions.
1 change: 1 addition & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,7 @@ GenesisState defines the ICS31 ibc-query genesis state
| ----- | ---- | ----- | ----------- |
| `queries` | [CrossChainQuery](#ibc.applications.ibc_query.v1.CrossChainQuery) | repeated | |
| `results` | [CrossChainQueryResult](#ibc.applications.ibc_query.v1.CrossChainQueryResult) | repeated | |
| `port_id` | [string](#string) | | |



Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ require (
github.com/hdevalence/ed25519consensus v0.0.0-20210204194344-59a8610d2b87 // indirect
github.com/improbable-eng/grpc-web v0.14.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d // indirect
github.com/klauspost/compress v1.13.6 // indirect
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6l
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
github.com/aws/aws-sdk-go v1.25.48/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.40.45 h1:QN1nsY27ssD/JmW4s83qmSb+uL6DG4GmCDzjmJB4xUI=
github.com/aws/aws-sdk-go v1.40.45/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/aws/aws-sdk-go-v2 v1.9.1/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4=
Expand Down Expand Up @@ -578,9 +577,7 @@ github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jhump/protoreflect v1.9.0 h1:npqHz788dryJiR/l6K/RUQAyh2SwV91+d1dnh4RjO9w=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U=
github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ=
Expand Down
15 changes: 7 additions & 8 deletions modules/apps/31-ibc-query/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const (

func NewMsgCrossChainQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cross-chain-query [src-port] [src-channel] [query-path] [query-height]",
Short: "Request ibc query on a given channel.",
Long: strings.TrimSpace(`Register a payee address on a given channel.`),
Args: cobra.ExactArgs(3),
Use: "cross-chain-query [src-port] [src-channel] [query-path] [query-height]",
Short: "Request ibc query on a given channel.",
Long: strings.TrimSpace(`Register a payee address on a given channel.`),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -41,8 +41,7 @@ func NewMsgCrossChainQueryCmd() *cobra.Command {
srcPort := args[0]
srcChannel := args[1]
path := args[2]
queryHeight, _ := strconv.ParseUint(args[2],10, 64)

queryHeight, _ := strconv.ParseUint(args[2], 10, 64)

timeoutHeightStr, err := cmd.Flags().GetString(flagPacketTimeoutHeight)
if err != nil {
Expand All @@ -57,8 +56,8 @@ func NewMsgCrossChainQueryCmd() *cobra.Command {
if err != nil {
return err
}
msg := types.NewMsgSubmitCrossChainQuery(queryId, path, timeoutHeight, timeoutTimestamp, queryHeight, creator, srcPort, srcChannel)

msg := types.NewMsgSubmitCrossChainQuery(queryId, path, &timeoutHeight, timeoutTimestamp, queryHeight, creator, srcPort, srcChannel)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
Expand Down
12 changes: 6 additions & 6 deletions modules/apps/31-ibc-query/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
k.SetPort(ctx, state.PortId)
for _, query := range state.Queries {
k.SetSubmitCrossChainQuery(ctx, *query)
k.SetCrossChainQuery(ctx, *query)
}
for _, result := range state.Results {
k.SetSubmitCrossChainQueryResult(ctx, *result)
k.SetCrossChainQueryResult(ctx, *result)
}

if !k.IsBound(ctx, state.PortId) {
if !k.IsBound(ctx, state.PortId) {
err := k.BindPort(ctx, state.PortId)
if err != nil {
if err != nil {
panic(fmt.Sprintf("could not claim port capability: %v", err))
}
}
Expand All @@ -28,7 +28,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
// ExportGenesis returns the application exported genesis
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return &types.GenesisState{
Queries: k.GetAllSubmitCrossChainQueries(ctx),
Results: k.GetAllSubmitCrossChainQueryResults(ctx),
Queries: k.GetAllCrossChainQueries(ctx),
Results: k.GetAllCrossChainQueryResults(ctx),
}
}
39 changes: 19 additions & 20 deletions modules/apps/31-ibc-query/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+host.ModuleName+"-"+types.ModuleName)
}


// IsBound checks if the IBC query module is already bound to the desired port
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
Expand Down Expand Up @@ -103,27 +102,27 @@ func (k Keeper) SetNextQuerySequence(ctx sdk.Context, sequence uint64) {
}

// SetSubmitCrossChainQuery stores the MsgSubmitCrossChainQuery in state keyed by the query id
func (k Keeper) SetSubmitCrossChainQuery(ctx sdk.Context, query types.MsgSubmitCrossChainQuery) {
func (k Keeper) SetCrossChainQuery(ctx sdk.Context, query types.CrossChainQuery) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.QueryKey)
bz := k.MustMarshalQuery(&query)
store.Set(host.QueryKey(query.Id), bz)
}

// GetSubmitCrossChainQuery retrieve the MsgSubmitCrossChainQuery stored in state given the query id
func (k Keeper) GetSubmitCrossChainQuery(ctx sdk.Context, queryId string) (types.MsgSubmitCrossChainQuery, bool) {
func (k Keeper) GetCrossChainQuery(ctx sdk.Context, queryId string) (types.CrossChainQuery, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.QueryKey)
key := host.QueryKey(queryId)
bz := store.Get(key)
if bz == nil {
return types.MsgSubmitCrossChainQuery{}, false
return types.CrossChainQuery{}, false
}

return k.MustUnmarshalQuery(bz), true
}

// GetAllSubmitCrossChainQueries returns a list of all MsgSubmitCrossChainQueries that are stored in state
func (k Keeper) GetAllSubmitCrossChainQueries(ctx sdk.Context) []*types.MsgSubmitCrossChainQuery {
var crossChainQueries []*types.MsgSubmitCrossChainQuery
func (k Keeper) GetAllCrossChainQueries(ctx sdk.Context) []*types.CrossChainQuery {
var crossChainQueries []*types.CrossChainQuery
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.QueryKey)

Expand All @@ -138,7 +137,7 @@ func (k Keeper) GetAllSubmitCrossChainQueries(ctx sdk.Context) []*types.MsgSubmi
}

// DeleteCrossChainQuery deletes MsgSubmitCrossChainQuery associated with the query id
func (k Keeper) DeleteSubmitCrossChainQuery(ctx sdk.Context, queryId string) {
func (k Keeper) DeleteCrossChainQuery(ctx sdk.Context, queryId string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.QueryKey)
store.Delete(host.QueryKey(queryId))
}
Expand All @@ -149,27 +148,27 @@ func (k Keeper) DeleteSubmitCrossChainQuery(ctx sdk.Context, queryId string) {
// 2. save query in private store

// SetCrossChainQueryResult stores the CrossChainQueryResult in state keyed by the query id
func (k Keeper) SetSubmitCrossChainQueryResult(ctx sdk.Context, result types.MsgSubmitCrossChainQueryResult) {
func (k Keeper) SetCrossChainQueryResult(ctx sdk.Context, result types.CrossChainQueryResult) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.QueryResultKey)
bz := k.MustMarshalQueryResult(&result)
store.Set(host.QueryResultKey(result.Id), bz)
}

// GetCrossChainQueryResult retrieve the CrossChainQueryResult stored in state given the query id
func (k Keeper) GetCrossChainQueryResult(ctx sdk.Context, queryId string) (types.MsgSubmitCrossChainQueryResult, bool) {
func (k Keeper) GetCrossChainQueryResult(ctx sdk.Context, queryId string) (types.CrossChainQueryResult, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.QueryResultKey)
key := host.QueryResultKey(queryId)
bz := store.Get(key)
if bz == nil {
return types.MsgSubmitCrossChainQueryResult{}, false
return types.CrossChainQueryResult{}, false
}

return k.MustUnmarshalQueryResult(bz), true
}

// GetAllCrossChainQueryResults returns a list of all CrossChainQueryResults that are stored in state
func (k Keeper) GetAllSubmitCrossChainQueryResults(ctx sdk.Context) []*types.MsgSubmitCrossChainQueryResult {
var crossChainQueryResults []*types.MsgSubmitCrossChainQueryResult
func (k Keeper) GetAllCrossChainQueryResults(ctx sdk.Context) []*types.CrossChainQueryResult {
var crossChainQueryResults []*types.CrossChainQueryResult
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.QueryResultKey)

Expand All @@ -184,35 +183,35 @@ func (k Keeper) GetAllSubmitCrossChainQueryResults(ctx sdk.Context) []*types.Msg
}

// DeleteCrossChainQueryResult deletes CrossChainQueryResult associated with the query id
func (k Keeper) DeleteSubmitCrossChainQueryResult(ctx sdk.Context, queryId string) {
func (k Keeper) DeleteCrossChainQueryResult(ctx sdk.Context, queryId string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.QueryResultKey)
store.Delete(host.QueryResultKey(queryId))
}

// MustMarshalQuery attempts to encode a CrossChainQuery object and returns the
// raw encoded bytes. It panics on error.
func (k Keeper) MustMarshalQuery(query *types.MsgSubmitCrossChainQuery) []byte {
func (k Keeper) MustMarshalQuery(query *types.CrossChainQuery) []byte {
return k.cdc.MustMarshal(query)
}

// MustUnmarshalQuery attempts to decode and return a CrossChainQuery object from
// raw encoded bytes. It panics on error.
func (k Keeper) MustUnmarshalQuery(bz []byte) types.MsgSubmitCrossChainQuery {
var query types.MsgSubmitCrossChainQuery
func (k Keeper) MustUnmarshalQuery(bz []byte) types.CrossChainQuery {
var query types.CrossChainQuery
k.cdc.MustUnmarshal(bz, &query)
return query
}

// MustMarshalQuery attempts to encode a CrossChainQuery object and returns the
// raw encoded bytes. It panics on error.
func (k Keeper) MustMarshalQueryResult(result *types.MsgSubmitCrossChainQueryResult) []byte {
func (k Keeper) MustMarshalQueryResult(result *types.CrossChainQueryResult) []byte {
return k.cdc.MustMarshal(result)
}

// MustUnmarshalQuery attempts to decode and return a CrossChainQuery object from
// raw encoded bytes. It panics on error.
func (k Keeper) MustUnmarshalQueryResult(bz []byte) types.MsgSubmitCrossChainQueryResult {
var result types.MsgSubmitCrossChainQueryResult
func (k Keeper) MustUnmarshalQueryResult(bz []byte) types.CrossChainQueryResult {
var result types.CrossChainQueryResult
k.cdc.MustUnmarshal(bz, &result)
return result
}
Expand All @@ -221,4 +220,4 @@ func (k Keeper) MustUnmarshalQueryResult(bz []byte) types.MsgSubmitCrossChainQue
// passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
}
36 changes: 18 additions & 18 deletions modules/apps/31-ibc-query/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ func (k Keeper) SubmitCrossChainQuery(goCtx context.Context, msg *types.MsgSubmi

// call keeper function
// keeper func save query in private store
query := types.MsgSubmitCrossChainQuery{
Id: msg.Id,
Path: msg.Path,
LocalTimeoutHeight: msg.LocalTimeoutHeight,
LocalTimeoutStamp: msg.LocalTimeoutStamp,
QueryHeight: msg.QueryHeight,
Sender: msg.Sender,
query := types.CrossChainQuery{
Id: msg.Id,
Path: msg.Path,
LocalTimeoutHeight: msg.LocalTimeoutHeight,
LocalTimeoutTimestamp: msg.LocalTimeoutStamp,
QueryHeight: msg.QueryHeight,
ClientId: msg.Sender,
}

k.SetSubmitCrossChainQuery(ctx, query)
k.SetCrossChainQuery(ctx, query)

// TODO
// TODO
// var data []byte
// err := query.Unmarshal(data)
// if err != nil {
// return nil, err
// }
if err := k.SendQuery(ctx, msg.SourcePort, msg.SourceChannel, query.GetSignBytes(),
msg.LocalTimeoutHeight, msg.LocalTimeoutStamp); err != nil {
return nil, err
}

//if err := k.SendQuery(ctx, msg.SourcePort, msg.SourceChannel, query.GetSignBytes(),
// msg.LocalTimeoutHeight, msg.LocalTimeoutStamp); err != nil {
// return nil, err
//}

// Log the query request
k.Logger(ctx).Info("query sent", "query_id", msg.GetQueryId())
Expand All @@ -73,21 +73,21 @@ func (k Keeper) SubmitCrossChainQueryResult(goCtx context.Context, msg *types.Ms
ctx := sdk.UnwrapSDKContext(goCtx)

// check CrossChainQuery exist
if _, found := k.GetSubmitCrossChainQuery(ctx, msg.Id); !found {
if _, found := k.GetCrossChainQuery(ctx, msg.Id); !found {
return nil, types.ErrCrossChainQueryNotFound
}

// remove query from privateStore
k.DeleteSubmitCrossChainQuery(ctx, msg.Id)
k.DeleteCrossChainQuery(ctx, msg.Id)

queryResult := types.MsgSubmitCrossChainQueryResult{
queryResult := types.CrossChainQueryResult{
Id: msg.Id,
Result: msg.Result,
Data: msg.Data,
}

// store result in privateStore
k.SetSubmitCrossChainQueryResult(ctx, queryResult)
k.SetCrossChainQueryResult(ctx, queryResult)

return &types.MsgSubmitCrossChainQueryResultResponse{}, nil
}
Loading

0 comments on commit 93b5aa8

Please sign in to comment.