Skip to content

Commit fa21817

Browse files
authored
Merge pull request #615 from okp4/feat/cli-ask-limit
Feat/cli ask limit
2 parents 3c85427 + 83f3ae7 commit fa21817

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

docs/command/okp4d_query_logic_ask.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ $ okp4d query logic ask "chain_id(X)." # returns the chain-id
2727
--grpc-insecure allow gRPC over insecure channels, if not the server must use TLS
2828
--height int Use a specific height to query state at (this can error if the node is pruning state)
2929
-h, --help help for ask
30+
--limit uint limit the maximum number of solutions to return.
31+
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)
3032
--node string <host>:<port> to CometBFT RPC interface for this chain (default "tcp://localhost:26657")
3133
-o, --output string Output format (text|json) (default "text")
3234
--program string reads the program from the given string.

x/logic/client/cli/query_ask.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ import (
66

77
"github.com/spf13/cobra"
88

9+
sdkmath "cosmossdk.io/math"
10+
911
"github.com/cosmos/cosmos-sdk/client"
1012
"github.com/cosmos/cosmos-sdk/client/flags"
1113
"github.com/cosmos/cosmos-sdk/version"
1214

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

16-
var program string
18+
var (
19+
program string
20+
limit uint64
21+
)
1722

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

41+
limit := sdkmath.NewUint(limit)
3642
res, err := queryClient.Ask(context.Background(), &types.QueryServiceAskRequest{
3743
Program: program,
3844
Query: query,
45+
Limit: &limit,
3946
})
4047
if err != nil {
4148
return
@@ -50,6 +57,13 @@ func CmdQueryAsk() *cobra.Command {
5057
"program",
5158
"",
5259
`reads the program from the given string.`)
60+
//nolint:lll
61+
cmd.Flags().Uint64Var(
62+
&limit,
63+
"limit",
64+
1,
65+
`limit the maximum number of solutions to return.
66+
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.`)
5367

5468
flags.AddQueryFlagsToCmd(cmd)
5569

x/logic/keeper/grpc_query_ask.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/okp4/okp4d/v7/x/logic/util"
1515
)
1616

17-
var defaultLimits = sdkmath.OneUint()
17+
var defaultSolutionsLimit = sdkmath.OneUint()
1818

1919
func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (response *types.QueryServiceAskResponse, err error) {
2020
sdkCtx := sdk.UnwrapSDKContext(ctx)
@@ -48,7 +48,7 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo
4848
sdkCtx,
4949
req.Program,
5050
req.Query,
51-
util.DerefOrDefault(req.Limit, defaultLimits))
51+
util.DerefOrDefault(req.Limit, defaultSolutionsLimit))
5252
}
5353

5454
// withGasMeter returns a new context with a gas meter that has the given limit.

x/logic/keeper/interpreter.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ func (k Keeper) enhanceContext(ctx context.Context) context.Context {
4949
return sdkCtx
5050
}
5151

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

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

7880
// queryInterpreter executes the given query on the given interpreter and returns the answer.
79-
func (k Keeper) queryInterpreter(ctx context.Context, i *prolog.Interpreter, query string, limit sdkmath.Uint) (*types.Answer, error) {
80-
return util.QueryInterpreter(ctx, i, query, limit)
81+
func (k Keeper) queryInterpreter(
82+
ctx context.Context, i *prolog.Interpreter, query string, solutionsLimit sdkmath.Uint,
83+
) (*types.Answer, error) {
84+
return util.QueryInterpreter(ctx, i, query, solutionsLimit)
8185
}
8286

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

147151
func checkLimits(request *types.QueryServiceAskRequest, limits types.Limits) error {
148152
size := sdkmath.NewUint(uint64(len(request.GetQuery())))
149-
limit := util.DerefOrDefault(limits.MaxSize, sdkmath.NewUint(math.MaxInt64))
150-
if size.GT(limit) {
151-
return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxSize: %d", size, limit)
153+
maxSize := util.DerefOrDefault(limits.MaxSize, sdkmath.NewUint(math.MaxInt64))
154+
if size.GT(maxSize) {
155+
return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxSize: %d", size, maxSize)
152156
}
153157

154158
return nil

x/logic/util/prolog.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919

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

3030
var env *engine.Env
3131
count := sdkmath.ZeroUint()
32-
envs := make([]*engine.Env, 0, sdkmath.MinUint(limit, sdkmath.NewUint(defaultEnvCap)).Uint64())
32+
envs := make([]*engine.Env, 0, sdkmath.MinUint(solutionsLimit, sdkmath.NewUint(defaultEnvCap)).Uint64())
3333
_, callErr := engine.Call(&i.VM, t, func(env *engine.Env) *engine.Promise {
34-
if count.LT(limit) {
34+
if count.LT(solutionsLimit) {
3535
envs = append(envs, env)
3636
}
3737
count = count.Incr()
38-
return engine.Bool(count.GT(limit))
38+
return engine.Bool(count.GT(solutionsLimit))
3939
}, env).Force(ctx)
4040

4141
vars := parsedVarsToVars(p.Vars)
@@ -46,7 +46,7 @@ func QueryInterpreter(
4646
}
4747

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

5858
return &types.Answer{
59-
HasMore: count.GT(limit),
59+
HasMore: count.GT(solutionsLimit),
6060
Variables: vars,
6161
Results: results,
6262
}, nil

0 commit comments

Comments
 (0)