diff --git a/modules/apps/31-ibc-query/client/cli/tx.go b/modules/apps/31-ibc-query/client/cli/tx.go index 7f1e458cd3a..ca8f2ed3c96 100644 --- a/modules/apps/31-ibc-query/client/cli/tx.go +++ b/modules/apps/31-ibc-query/client/cli/tx.go @@ -1 +1,67 @@ package cli + +import ( + "strings" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/spf13/cobra" + + "github.com/cosmos/ibc-go/v4/modules/apps/31-ibc-query/client/utils" + "github.com/cosmos/ibc-go/v4/modules/apps/31-ibc-query/types" + clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types" +) + +const ( + flagPacketTimeoutHeight = "packet-timeout-height" + flagPacketTimeoutTimestamp = "packet-timeout-timestamp" + flagAbsoluteTimeouts = "absolute-timeouts" +) + +func NewMsgCrossChainQueryCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "cross-chain-query [client-id] [query-path]", + Short: "Request ibc query on a given channel.", + Long: strings.TrimSpace(`Register a payee address on a given channel.`), + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + creator := clientCtx.GetFromAddress().String() + queryId := utils.GetQueryIdentifier() + + clientId := args[0] + path := args[1] + + //TODO + // Get chain height from queried chain + temporaryQueryHeight := uint64(123) + + timeoutHeightStr, err := cmd.Flags().GetString(flagPacketTimeoutHeight) + if err != nil { + return err + } + timeoutHeight, err := clienttypes.ParseHeight(timeoutHeightStr) + if err != nil { + return err + } + + timeoutTimestamp, err := cmd.Flags().GetUint64(flagPacketTimeoutTimestamp) + if err != nil { + return err + } + + msg := types.NewMsgSubmitCrossChainQuery(queryId, path, timeoutHeight.RevisionHeight, timeoutTimestamp, temporaryQueryHeight, clientId, creator) + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/modules/apps/31-ibc-query/client/utils/utils.go b/modules/apps/31-ibc-query/client/utils/utils.go new file mode 100644 index 00000000000..61551996f41 --- /dev/null +++ b/modules/apps/31-ibc-query/client/utils/utils.go @@ -0,0 +1,12 @@ +package utils + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/ibc-go/v4/modules/apps/31-ibc-query/keeper" +) + + +func GetQueryIdentifier() string { + return keeper.Keeper.GenerateQueryIdentifier(keeper.Keeper{}, sdk.Context{}) +} + diff --git a/modules/apps/31-ibc-query/keeper/keeper.go b/modules/apps/31-ibc-query/keeper/keeper.go index 0f2a3f692d7..34a9fb88b34 100644 --- a/modules/apps/31-ibc-query/keeper/keeper.go +++ b/modules/apps/31-ibc-query/keeper/keeper.go @@ -34,6 +34,42 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", "x/"+host.ModuleName+"-"+types.ModuleName) } +func (k Keeper) GenerateQueryIdentifier(ctx sdk.Context) string { + nextQuerySeq := k.GetNextQuerySequence(ctx) + queryID := types.FormatQueryIdentifier(nextQuerySeq) + + nextQuerySeq++ + k.SetNextQuerySequence(ctx, nextQuerySeq) + return queryID +} + + +func (k Keeper) GetNextQuerySequence(ctx sdk.Context) uint64 { + store := ctx.KVStore(k.storeKey) + bz := store.Get([]byte(types.KeyNextQuerySequence)) + if bz == nil { + panic("next connection sequence is nil") + } + + return sdk.BigEndianToUint64(bz) +} + +func (k Keeper) SetNextQuerySequence(ctx sdk.Context, sequence uint64) { + store := ctx.KVStore(k.storeKey) + bz := sdk.Uint64ToBigEndian(sequence) + store.Set([]byte(types.KeyNextQuerySequence), bz) +} + +func (k Keeper) SetQuery(ctx sdk.Context, query types.MsgSubmitCrossChainQuery) string { + store := ctx.KVStore(k.storeKey) + appendedValue := k.cdc.MustMarshal(&query) + + store.Set([]byte(host.QueryPath(query.Id)), appendedValue) + + return query.Id +} + + // TODO // func handleIbcQuery // 1. set unique query Id