Skip to content

Commit

Permalink
feat(logic): introduce parameter "limit" for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Mar 11, 2024
1 parent 9a34f7d commit cc86031
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 39 deletions.
8 changes: 8 additions & 0 deletions proto/logic/v1beta2/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ message QueryServiceAskRequest {
string program = 1 [(gogoproto.moretags) = "yaml:\"program\",omitempty"];
// query is the query string to be executed.
string query = 2 [(gogoproto.moretags) = "yaml:\"query\",omitempty"];
// limit specifies the maximum number of solutions to be returned. This field is governed by
// max_result_count, which defines the upper limit of results that may be requested per query.
// If this field is not explicitly set, a default value of 1 is applied.
string limit = 3 [
(gogoproto.moretags) = "yaml:\"limit\",omitempty",
(gogoproto.customtype) = "cosmossdk.io/math.Uint",
(gogoproto.nullable) = true
];
}

// QueryServiceAskResponse is response type for the QueryService/Ask RPC method.
Expand Down
7 changes: 6 additions & 1 deletion x/logic/keeper/grpc_query_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import (
goctx "context"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"

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

"github.com/okp4/okp4d/v7/x/logic/meter"
"github.com/okp4/okp4d/v7/x/logic/types"
"github.com/okp4/okp4d/v7/x/logic/util"
)

var defaultLimits = sdkmath.OneUint()

func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (response *types.QueryServiceAskResponse, err error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

Expand Down Expand Up @@ -43,7 +47,8 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo
return k.execute(
sdkCtx,
req.Program,
req.Query)
req.Query,
util.DerefOrDefault(req.Limit, defaultLimits))
}

// withGasMeter returns a new context with a gas meter that has the given limit.
Expand Down
4 changes: 2 additions & 2 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (k Keeper) enhanceContext(ctx context.Context) context.Context {
return sdkCtx
}

func (k Keeper) execute(ctx context.Context, program, query string) (*types.QueryServiceAskResponse, error) {
func (k Keeper) execute(ctx context.Context, program, query string, limit sdkmath.Uint) (*types.QueryServiceAskResponse, error) {
ctx = k.enhanceContext(ctx)
sdkCtx := sdk.UnwrapSDKContext(ctx)
limits := k.limits(sdkCtx)
Expand All @@ -53,7 +53,7 @@ func (k Keeper) execute(ctx context.Context, program, query string) (*types.Quer
return nil, errorsmod.Wrapf(types.InvalidArgument, "error compiling query: %v", err.Error())
}

answer, err := k.queryInterpreter(ctx, i, query, *limits.MaxResultCount)
answer, err := k.queryInterpreter(ctx, i, query, sdkmath.MinUint(limit, *limits.MaxResultCount))
if err != nil {
return nil, errorsmod.Wrapf(types.InvalidArgument, "error executing query: %v", err.Error())
}
Expand Down
126 changes: 93 additions & 33 deletions x/logic/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/logic/wasm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (querier LogicQuerier) Ask(ctx sdk.Context, query AskQuery) ([]byte, error)
grpcResp, err := querier.k.Ask(ctx, &types.QueryServiceAskRequest{
Program: query.Program,
Query: query.Query,
Limit: query.Limit,
})
if err != nil {
return nil, err
Expand Down
11 changes: 8 additions & 3 deletions x/logic/wasm/types.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package wasm

import "github.com/okp4/okp4d/v7/x/logic/types"
import (
sdkmath "cosmossdk.io/math"

"github.com/okp4/okp4d/v7/x/logic/types"
)

// AskQuery implements the wasm custom Ask query JSON schema, it basically redefined the Ask gRPC request parameters
// to keep control in case of eventual breaking change in the logic module definition, and to decouple the
// serialization logic.
type AskQuery struct {
Program string `json:"program"`
Query string `json:"query"`
Program string `json:"program"`
Query string `json:"query"`
Limit *sdkmath.Uint `json:"limit"`
}

// AskResponse implements the Ask query response JSON schema in a wasm custom query purpose, it redefines the existing
Expand Down

0 comments on commit cc86031

Please sign in to comment.