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 4 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: "error executing query: out of gas in location: panic: {ValuePerByte}: out of gas: invalid argument",
},
{
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: "error executing query: out of gas in location: block_height/1: out of gas: invalid argument",
},
{
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
11 changes: 6 additions & 5 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
storetypes "cosmossdk.io/store/types"

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

"github.com/axone-protocol/axoned/v8/x/logic/fs/filtered"
"github.com/axone-protocol/axoned/v8/x/logic/interpreter"
Expand Down Expand Up @@ -107,12 +108,12 @@

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(sdkerrors.ErrOutOfGas, "out of gas in location: %v", rType.Descriptor)
default:
panic(r)

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

View check run for this annotation

Codecov / codecov/patch

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

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

panic(r)
}
}()

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
14 changes: 13 additions & 1 deletion x/logic/util/prolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

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"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

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

Expand Down Expand Up @@ -48,7 +53,14 @@
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()})
switch {
case errors.Is(callErr, sdkerrors.ErrOutOfGas):
return nil, callErr
case sdk.UnwrapSDKContext(ctx).GasMeter().IsOutOfGas():
return nil, errorsmod.Wrapf(sdkerrors.ErrOutOfGas, "out of gas in location: %v", callErr.Error())
default:
results = append(results, types.Result{Error: callErr.Error()})

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

View check run for this annotation

Codecov / codecov/patch

x/logic/util/prolog.go#L56-L62

Added lines #L56 - L62 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