Skip to content

Improve ErrorOutOfGas handling in predicate execution #694

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

Merged
merged 5 commits into from
Jul 22, 2024
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
17 changes: 13 additions & 4 deletions x/logic/keeper/grpc_query_ask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/axone-protocol/axoned/v8/x/logic"
Expand Down Expand Up @@ -137,16 +139,18 @@ func TestGRPCAsk(t *testing.T) {
maxGas: 1000,
expectedError: "out of gas: logic <ReadPerByte> (1018/1000): limit exceeded",
},
{
query: "bank_balances(X, Y).",
maxGas: 3000,
expectedError: "out of gas: logic <panic: {ValuePerByte}> (4204/3000): limit exceeded",
},
{
query: "block_height(X).",
maxGas: 3000,
predicateCosts: map[string]uint64{
"block_height/1": 10000,
},
expectedAnswer: &types.Answer{
Variables: []string{"X"},
Results: []types.Result{{Error: "error(resource_error(gas(block_height/1,12353,3000)),block_height/1)"}},
},
expectedError: "out of gas: logic <block_height/1> (12353/3000): limit exceeded",
},
{
program: "father(bob, 'élodie').",
Expand Down Expand Up @@ -325,6 +329,11 @@ foo(a4).
bankKeeper := logictestutil.NewMockBankKeeper(ctrl)
fsProvider := logictestutil.NewMockFS(ctrl)

bankKeeper.EXPECT().GetAccountsBalances(gomock.Any()).Do(func(ctx gocontext.Context) []bankypes.Balance {
sdk.UnwrapSDKContext(ctx).GasMeter().ConsumeGas(2000, "ValuePerByte")
return nil
}).AnyTimes()

logicKeeper := keeper.NewKeeper(
encCfg.Codec,
key,
Expand Down
16 changes: 8 additions & 8 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

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())
return nil, err
}

return &types.QueryServiceAskResponse{
Expand Down Expand Up @@ -107,17 +107,17 @@

defer func() {
if r := recover(); r != nil {
if gasError, ok := r.(storetypes.ErrorOutOfGas); ok {
err = engine.ResourceError(prolog2.ResourceGas(gasError.Descriptor, gasMeter.GasConsumed(), gasMeter.Limit()), env)
return
switch rType := r.(type) {
case storetypes.ErrorOutOfGas:
err = errorsmod.Wrapf(
types.LimitExceeded, "out of gas: %s <%s> (%d/%d)",
types.ModuleName, rType.Descriptor, sdkctx.GasMeter().GasConsumed(), sdkctx.GasMeter().Limit())
default:
panic(r)

Check warning on line 116 in x/logic/keeper/interpreter.go

View check run for this annotation

Codecov / codecov/patch

x/logic/keeper/interpreter.go#L115-L116

Added lines #L115 - L116 were not covered by tests
}

panic(r)
}
}()

gasMeter.ConsumeGas(cost, predicate)

return err
}
}
Expand Down
10 changes: 0 additions & 10 deletions x/logic/prolog/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package prolog

import (
"github.com/ichiban/prolog/engine"

"cosmossdk.io/store/types"
)

var (
Expand Down Expand Up @@ -82,8 +80,6 @@ var (
// The module resource is the representation of the module with which the interaction is made.
// The module resource is denoted as a compound with the name of the module.
AtomResourceModule = engine.NewAtom("resource_module")
// AtomResourceGas is the atom denoting the "gas" resource.
AtomResourceGas = engine.NewAtom("gas")
)

// ResourceContext returns a term representing the context resource.
Expand All @@ -96,12 +92,6 @@ func ResourceModule(module string) engine.Term {
return AtomResourceModule.Apply(engine.NewAtom(module))
}

// ResourceGas returns a term representing the gas resource with the given descriptor, consumed and limit at the
// given context.
func ResourceGas(descriptor string, consumed types.Gas, limit types.Gas) engine.Term {
return AtomResourceGas.Apply(engine.NewAtom(descriptor), engine.Integer(int64(consumed)), engine.Integer(int64(limit)))
}

var (
AtomOperationInput = engine.NewAtom("input")
AtomOperationExecute = engine.NewAtom("execute")
Expand Down
21 changes: 18 additions & 3 deletions x/logic/util/prolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import (
"context"
"errors"
"strings"

"github.com/ichiban/prolog"
"github.com/ichiban/prolog/engine"
"github.com/samber/lo"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

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

"github.com/axone-protocol/axoned/v8/x/logic/types"
)

Expand All @@ -24,7 +28,7 @@
p := engine.NewParser(&i.VM, strings.NewReader(query))
t, err := p.Term()
if err != nil {
return nil, err
return nil, errorsmod.Wrapf(types.InvalidArgument, "error executing query: %v", err.Error())

Check warning on line 31 in x/logic/util/prolog.go

View check run for this annotation

Codecov / codecov/patch

x/logic/util/prolog.go#L31

Added line #L31 was not covered by tests
}

var env *engine.Env
Expand All @@ -42,13 +46,24 @@

results, err := envsToResults(envs, p.Vars, i)
if err != nil {
return nil, err
return nil, errorsmod.Wrapf(types.InvalidArgument, "error executing query: %v", err.Error())

Check warning on line 49 in x/logic/util/prolog.go

View check run for this annotation

Codecov / codecov/patch

x/logic/util/prolog.go#L49

Added line #L49 was not covered by tests
}

if callErr != nil {
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()})
sdkCtx := sdk.UnwrapSDKContext(ctx)

Check warning on line 55 in x/logic/util/prolog.go

View check run for this annotation

Codecov / codecov/patch

x/logic/util/prolog.go#L55

Added line #L55 was not covered by tests

switch {
case errors.Is(callErr, types.LimitExceeded):
return nil, callErr
case sdkCtx.GasMeter().IsOutOfGas():
return nil, errorsmod.Wrapf(
types.LimitExceeded, "out of gas: %s <%s> (%d/%d)",
types.ModuleName, callErr.Error(), sdkCtx.GasMeter().GasConsumed(), sdkCtx.GasMeter().Limit())
default:
results = append(results, types.Result{Error: callErr.Error()})

Check warning on line 65 in x/logic/util/prolog.go

View check run for this annotation

Codecov / codecov/patch

x/logic/util/prolog.go#L57-L65

Added lines #L57 - L65 were not covered by tests
}
} else {
// error is part of the look-ahead, so let's consider that there's one more solution
count = count.Incr()
Expand Down
Loading