Skip to content

Commit

Permalink
Add Query for Pending ICQs (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs authored Dec 10, 2022
1 parent a37a56d commit 433dabc
Show file tree
Hide file tree
Showing 7 changed files with 841 additions and 20 deletions.
21 changes: 21 additions & 0 deletions proto/stride/interchainquery/v1/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";
package stride.interchainquery.v1;

import "stride/interchainquery/v1/genesis.proto";
import "google/api/annotations.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/Stride-Labs/stride/v4/x/interchainquery/types";

service QueryService {
rpc PendingQueries(QueryPendingQueriesRequest)
returns (QueryPendingQueriesResponse) {
option (google.api.http).get =
"/Stride-Labs/stride/interchainquery/pending_queries";
}
}

message QueryPendingQueriesRequest {}
message QueryPendingQueriesResponse {
repeated Query pending_queries = 1 [ (gogoproto.nullable) = false ];
}
36 changes: 22 additions & 14 deletions x/interchainquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Stride uses interchain queries and interchain accounts to perform multichain liq
3. **[Events](#events)**
4. **[Keeper](#keeper)**
5. **[Msgs](#msgs)**
6. **[Queries](#queries)**

## State

Expand All @@ -33,15 +34,13 @@ The `interchainquery` module keeps `Query` objects and modifies the information
`Query` has information types that pertain to the query itself. `Query` keeps the following:

1. `id` keeps the query identification string.
2. `connection_id` keeps the id of the channel or connection between the controller and host chain.
2. `connection_id` keeps the id of the connection between the controller and host chain.
3. `chain_id` keeps the id of the queried chain.
4. `query_type` keeps the type of interchain query
4. `query_type` keeps the type of interchain query (e.g. bank store query)
5. `request` keeps an bytecode encoded version of the interchain query
6. `period` TODO
7. `last_height` keeps the blockheight of the last block before the query was made
8. `callback_id` keeps the function that will be called by the interchain query
9. `ttl` TODO
10. `height` keeps the height at which the ICQ query should execute on the host zone. This is often `0`, meaning the query should execute at the latest height on the host zone.
6. `callback_id` keeps the function that will be called by the interchain query
7. `ttl` time at which the query expires (in unix nano)
8. `request_sent` keeps a boolean indicating whether the query event has been emitted (and can be identified by a relayer)

`DataPoint` has information types that pertain to the data that is queried. `DataPoint` keeps the following:

Expand All @@ -52,7 +51,7 @@ The `interchainquery` module keeps `Query` objects and modifies the information

## Events

The `interchainquery` module emits an event at the end of every 3 `stride_epoch`s (e.g. 15 minutes on local testnet).
The `interchainquery` module emits an event at the end of every `stride_epoch`s (e.g. 15 minutes on local testnet).

The purpose of this event is to send interchainqueries that query data about staking rewards, which Stride uses to reinvest (aka autocompound) staking rewards.

Expand All @@ -65,7 +64,6 @@ The purpose of this event is to send interchainqueries that query data about sta
sdk.NewAttribute(types.AttributeKeyChainId, queryInfo.ChainId),
sdk.NewAttribute(types.AttributeKeyConnectionId, queryInfo.ConnectionId),
sdk.NewAttribute(types.AttributeKeyType, queryInfo.QueryType),
// TODO: add height to request type
sdk.NewAttribute(types.AttributeKeyHeight, "0"),
sdk.NewAttribute(types.AttributeKeyRequest, hex.EncodeToString(queryInfo.Request)),
)
Expand All @@ -91,12 +89,22 @@ AllQueries(ctx sdk.Context) []types.Query

## Msgs

`interchainquery` has a `Msg` service that passes messages between chains.

```protobuf
service Msg {
// SubmitQueryResponse defines a method for submiting query responses.
rpc SubmitQueryResponse(MsgSubmitQueryResponse) returns (MsgSubmitQueryResponseResponse)
// SubmitQueryResponse is used to return the query response back to Stride
message MsgSubmitQueryResponse {
string chain_id = 1;
string query_id = 2;
bytes result = 3;
tendermint.crypto.ProofOps proof_ops = 4;
int64 height = 5;
string from_address = 6;
}
```

## Queries
```protobuf
// Query PendingQueries lists all queries that have been requested (i.e. emitted)
// but have not had a response submitted yet
message QueryPendingQueriesRequest {}
```

69 changes: 69 additions & 0 deletions x/interchainquery/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cli

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"

"github.com/Stride-Labs/stride/v4/x/interchainquery/types"
)

// GetQueryCmd returns the cli query commands for this module.
func GetQueryCmd() *cobra.Command {
// Group lockup queries under a subcommand
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
GetCmdListPendingQueries(),
)

return cmd
}

// GetCmdQueries provides a list of all pending queries
// (queries that have not have been requested but have not received a response)
func GetCmdListPendingQueries() *cobra.Command {
cmd := &cobra.Command{
Use: "list-pending-queries",
Short: "Query all pending queries",
Example: strings.TrimSpace(
fmt.Sprintf(`$ %s query interchainquery list-pending-queries`,
version.AppName,
),
),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryServiceClient(clientCtx)

req := &types.QueryPendingQueriesRequest{}

res, err := queryClient.PendingQueries(context.Background(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
25 changes: 25 additions & 0 deletions x/interchainquery/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/Stride-Labs/stride/v4/x/interchainquery/types"
)

var _ types.QueryServiceServer = Keeper{}

// Queries all queries that have been requested but have not received a response
func (k Keeper) PendingQueries(c context.Context, req *types.QueryPendingQueriesRequest) (*types.QueryPendingQueriesResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

pendingQueries := []types.Query{}
for _, query := range k.AllQueries(ctx) {
if query.RequestSent {
pendingQueries = append(pendingQueries, query)
}
}

return &types.QueryPendingQueriesResponse{PendingQueries: pendingQueries}, nil
}
14 changes: 8 additions & 6 deletions x/interchainquery/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package interchainquery

import (
"context"
"encoding/json"
"math/rand"

Expand All @@ -20,6 +21,7 @@ import (

"github.com/Stride-Labs/stride/v4/x/interchainquery/keeper"

"github.com/Stride-Labs/stride/v4/x/interchainquery/client/cli"
"github.com/Stride-Labs/stride/v4/x/interchainquery/types"
)

Expand Down Expand Up @@ -78,10 +80,10 @@ func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Rout

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module.
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
// err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
// if err != nil {
// panic(err)
// }
err := types.RegisterQueryServiceHandlerClient(context.Background(), mux, types.NewQueryServiceClient(clientCtx))
if err != nil {
panic(err)
}
}

// GetTxCmd returns the capability module's root tx command.
Expand All @@ -91,7 +93,7 @@ func (a AppModuleBasic) GetTxCmd() *cobra.Command {

// GetQueryCmd returns the capability module's root query command.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return nil
return cli.GetQueryCmd()
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -134,7 +136,7 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
// module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
// types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
types.RegisterQueryServiceServer(cfg.QueryServer(), am.keeper)
}

// RegisterInvariants registers the capability module's invariants.
Expand Down
Loading

0 comments on commit 433dabc

Please sign in to comment.