Skip to content

Commit

Permalink
refactor: return limit exceeded error on out of gas
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Jul 22, 2024
1 parent f117c7c commit c242379
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions x/logic/keeper/grpc_query_ask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ func TestGRPCAsk(t *testing.T) {
{
query: "bank_balances(X, Y).",
maxGas: 3000,
expectedError: "error executing query: out of gas in location: panic: {ValuePerByte}: out of gas: invalid argument",
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,
},
expectedError: "error executing query: out of gas in location: block_height/1: out of gas: invalid argument",
expectedError: "out of gas: logic <block_height/1> (12353/3000): limit exceeded",
},
{
program: "father(bob, 'élodie').",
Expand Down
9 changes: 4 additions & 5 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
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 @@ -67,7 +66,7 @@ func (k Keeper) execute(

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 @@ -110,15 +109,15 @@ func (k Keeper) newInterpreter(ctx context.Context) (*prolog.Interpreter, fmt.St
if r := recover(); r != nil {
switch rType := r.(type) {
case storetypes.ErrorOutOfGas:
err = errorsmod.Wrapf(sdkerrors.ErrOutOfGas, "out of gas in location: %v", rType.Descriptor)
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
}
}
}()

gasMeter.ConsumeGas(cost, predicate)

return err
}
}
Expand Down
15 changes: 9 additions & 6 deletions x/logic/util/prolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
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 All @@ -29,7 +28,7 @@ func QueryInterpreter(
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 @@ -47,17 +46,21 @@ func QueryInterpreter(

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
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, sdkerrors.ErrOutOfGas):
case errors.Is(callErr, types.LimitExceeded):
return nil, callErr
case sdk.UnwrapSDKContext(ctx).GasMeter().IsOutOfGas():
return nil, errorsmod.Wrapf(sdkerrors.ErrOutOfGas, "out of gas in location: %v", callErr.Error())
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
}
Expand Down

0 comments on commit c242379

Please sign in to comment.