Skip to content

Commit

Permalink
Merge pull request #615 from okp4/feat/cli-ask-limit
Browse files Browse the repository at this point in the history
Feat/cli ask limit
  • Loading branch information
ccamel authored Apr 3, 2024
2 parents 3c85427 + 83f3ae7 commit fa21817
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
2 changes: 2 additions & 0 deletions docs/command/okp4d_query_logic_ask.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ $ okp4d query logic ask "chain_id(X)." # returns the chain-id
--grpc-insecure allow gRPC over insecure channels, if not the server must use TLS
--height int Use a specific height to query state at (this can error if the node is pruning state)
-h, --help help for ask
--limit uint limit the maximum number of solutions to return.
This parameter is constrained by the 'max_result_count' setting in the module configuration, which specifies the maximum number of results that can be requested per query. (default 1)
--node string <host>:<port> to CometBFT RPC interface for this chain (default "tcp://localhost:26657")
-o, --output string Output format (text|json) (default "text")
--program string reads the program from the given string.
Expand Down
16 changes: 15 additions & 1 deletion x/logic/client/cli/query_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import (

"github.com/spf13/cobra"

sdkmath "cosmossdk.io/math"

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

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

var program string
var (
program string
limit uint64
)

func CmdQueryAsk() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -33,9 +38,11 @@ func CmdQueryAsk() *cobra.Command {
query := args[0]
queryClient := types.NewQueryServiceClient(clientCtx)

limit := sdkmath.NewUint(limit)
res, err := queryClient.Ask(context.Background(), &types.QueryServiceAskRequest{
Program: program,
Query: query,
Limit: &limit,
})
if err != nil {
return
Expand All @@ -50,6 +57,13 @@ func CmdQueryAsk() *cobra.Command {
"program",
"",
`reads the program from the given string.`)
//nolint:lll
cmd.Flags().Uint64Var(
&limit,
"limit",
1,
`limit the maximum number of solutions to return.
This parameter is constrained by the 'max_result_count' setting in the module configuration, which specifies the maximum number of results that can be requested per query.`)

flags.AddQueryFlagsToCmd(cmd)

Expand Down
4 changes: 2 additions & 2 deletions x/logic/keeper/grpc_query_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/okp4/okp4d/v7/x/logic/util"
)

var defaultLimits = sdkmath.OneUint()
var defaultSolutionsLimit = 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 @@ -48,7 +48,7 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo
sdkCtx,
req.Program,
req.Query,
util.DerefOrDefault(req.Limit, defaultLimits))
util.DerefOrDefault(req.Limit, defaultSolutionsLimit))
}

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

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

answer, err := k.queryInterpreter(ctx, i, query, sdkmath.MinUint(limit, *limits.MaxResultCount))
answer, err := k.queryInterpreter(ctx, i, query, sdkmath.MinUint(solutionsLimit, *limits.MaxResultCount))
if err != nil {
return nil, errorsmod.Wrapf(types.InvalidArgument, "error executing query: %v", err.Error())
}
Expand All @@ -76,8 +78,10 @@ func (k Keeper) execute(ctx context.Context, program, query string, limit sdkmat
}

// queryInterpreter executes the given query on the given interpreter and returns the answer.
func (k Keeper) queryInterpreter(ctx context.Context, i *prolog.Interpreter, query string, limit sdkmath.Uint) (*types.Answer, error) {
return util.QueryInterpreter(ctx, i, query, limit)
func (k Keeper) queryInterpreter(
ctx context.Context, i *prolog.Interpreter, query string, solutionsLimit sdkmath.Uint,
) (*types.Answer, error) {
return util.QueryInterpreter(ctx, i, query, solutionsLimit)
}

// newInterpreter creates a new interpreter properly configured.
Expand Down Expand Up @@ -146,9 +150,9 @@ func (k Keeper) newInterpreter(ctx context.Context) (*prolog.Interpreter, fmt.St

func checkLimits(request *types.QueryServiceAskRequest, limits types.Limits) error {
size := sdkmath.NewUint(uint64(len(request.GetQuery())))
limit := util.DerefOrDefault(limits.MaxSize, sdkmath.NewUint(math.MaxInt64))
if size.GT(limit) {
return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxSize: %d", size, limit)
maxSize := util.DerefOrDefault(limits.MaxSize, sdkmath.NewUint(math.MaxInt64))
if size.GT(maxSize) {
return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxSize: %d", size, maxSize)
}

return nil
Expand Down
12 changes: 6 additions & 6 deletions x/logic/util/prolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (

// QueryInterpreter interprets a query and returns the solutions up to the given limit.
func QueryInterpreter(
ctx context.Context, i *prolog.Interpreter, query string, limit sdkmath.Uint,
ctx context.Context, i *prolog.Interpreter, query string, solutionsLimit sdkmath.Uint,
) (*types.Answer, error) {
p := engine.NewParser(&i.VM, strings.NewReader(query))
t, err := p.Term()
Expand All @@ -29,13 +29,13 @@ func QueryInterpreter(

var env *engine.Env
count := sdkmath.ZeroUint()
envs := make([]*engine.Env, 0, sdkmath.MinUint(limit, sdkmath.NewUint(defaultEnvCap)).Uint64())
envs := make([]*engine.Env, 0, sdkmath.MinUint(solutionsLimit, sdkmath.NewUint(defaultEnvCap)).Uint64())
_, callErr := engine.Call(&i.VM, t, func(env *engine.Env) *engine.Promise {
if count.LT(limit) {
if count.LT(solutionsLimit) {
envs = append(envs, env)
}
count = count.Incr()
return engine.Bool(count.GT(limit))
return engine.Bool(count.GT(solutionsLimit))
}, env).Force(ctx)

vars := parsedVarsToVars(p.Vars)
Expand All @@ -46,7 +46,7 @@ func QueryInterpreter(
}

if callErr != nil {
if sdkmath.NewUint(uint64(len(results))).LT(limit) {
if sdkmath.NewUint(uint64(len(results))).LT(solutionsLimit) {
// error is not part of the look-ahead and should be included in the solutions
results = append(results, types.Result{Error: callErr.Error()})
} else {
Expand All @@ -56,7 +56,7 @@ func QueryInterpreter(
}

return &types.Answer{
HasMore: count.GT(limit),
HasMore: count.GT(solutionsLimit),
Variables: vars,
Results: results,
}, nil
Expand Down

0 comments on commit fa21817

Please sign in to comment.