Skip to content

Commit

Permalink
style(logic): improve variable naming for clarity (limit)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 3, 2024
1 parent 3c85427 commit c335b0f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
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 c335b0f

Please sign in to comment.