Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logic): ensure keepers in interpreter exec ctx #279

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions x/logic/keeper/grpc_query_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okp4/okp4d/x/logic/types"
"golang.org/x/net/context"
)

func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (response *types.QueryServiceAskResponse, err error) {
Expand Down Expand Up @@ -39,7 +38,7 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo

//nolint:contextcheck
return k.execute(
context.WithValue(sdkCtx, sdk.SdkContextKey, sdkCtx),
sdkCtx,
req.Program,
req.Query)
}
Expand Down
25 changes: 16 additions & 9 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@ func (k Keeper) limits(ctx goctx.Context) types.Limits {
return params.GetLimits()
}

func (k Keeper) execute(goctx goctx.Context, program, query string) (*types.QueryServiceAskResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(goctx)
i, err := k.newInterpreter(goctx)
func (k Keeper) enhanceContext(ctx goctx.Context) goctx.Context {
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx = sdkCtx.WithValue(types.AuthKeeperContextKey, k.authKeeper)
sdkCtx = sdkCtx.WithValue(types.BankKeeperContextKey, k.bankKeeper)

return sdkCtx
}

func (k Keeper) execute(ctx goctx.Context, program, query string) (*types.QueryServiceAskResponse, error) {
ctx = k.enhanceContext(ctx)
sdkCtx := sdk.UnwrapSDKContext(ctx)

i, err := k.newInterpreter(ctx)
if err != nil {
return nil, sdkerrors.Wrapf(types.Internal, "error creating interpreter: %v", err.Error())
}

if err := i.ExecContext(goctx, program); err != nil {
if err := i.ExecContext(ctx, program); err != nil {
return nil, sdkerrors.Wrapf(types.InvalidArgument, "error compiling query: %v", err.Error())
}

sols, err := i.QueryContext(goctx, query)
sols, err := i.QueryContext(ctx, query)
if err != nil {
return nil, sdkerrors.Wrapf(types.InvalidArgument, "error executing query: %v", err.Error())
}
Expand All @@ -38,7 +48,7 @@ func (k Keeper) execute(goctx goctx.Context, program, query string) (*types.Quer
}()

success := false
limits := k.limits(goctx)
limits := k.limits(ctx)
var variables []string
results := make([]types.Result, 0)
for nb := sdkmath.ZeroUint(); nb.LT(*limits.MaxResultCount) && sols.Next(); nb = nb.Incr() {
Expand Down Expand Up @@ -81,9 +91,6 @@ func checkLimits(request *types.QueryServiceAskRequest, limits types.Limits) err
// newInterpreter creates a new interpreter properly configured.
func (k Keeper) newInterpreter(ctx goctx.Context) (*prolog.Interpreter, error) {
sdkctx := sdk.UnwrapSDKContext(ctx)
sdkctx = sdkctx.WithValue(types.AuthKeeperContextKey, k.authKeeper)
sdkctx = sdkctx.WithValue(types.BankKeeperContextKey, k.bankKeeper)

params := k.GetParams(sdkctx)

interpreterParams := params.GetInterpreter()
Expand Down