Skip to content

Commit c91400c

Browse files
committed
fixes
1 parent 4fb4bc5 commit c91400c

6 files changed

Lines changed: 67 additions & 65 deletions

File tree

app/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func New(
167167
ante.NewSigGasConsumeDecorator(app.AuthKeeper, ante.DefaultSigVerificationGasConsumer),
168168
ante.NewSigVerificationDecorator(app.AuthKeeper, app.txConfig.SignModeHandler()),
169169
ante.NewIncrementSequenceDecorator(app.AuthKeeper),
170-
gnovmante.NewAnteHandler(&app.GnovmKeeper),
170+
gnovmante.NewAnteHandler(),
171171
}...))
172172

173173
// A custom InitChainer can be set if extra pre-init-genesis logic is required.

x/gnovm/ante/ante.go

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@ import (
55

66
storetypes "cosmossdk.io/store/types"
77
sdk "github.com/cosmos/cosmos-sdk/types"
8-
gnostore "github.com/gnolang/gno/tm2/pkg/store"
98

10-
"github.com/ignite/gnovm/x/gnovm/keeper"
119
"github.com/ignite/gnovm/x/gnovm/types"
1210
)
1311

14-
func NewAnteHandler(k *keeper.Keeper) sdk.AnteDecorator {
15-
return &gnoAnteHandler{keeper: k}
12+
func NewAnteHandler() sdk.AnteDecorator {
13+
return &gnoAnteHandler{}
1614
}
1715

18-
type gnoAnteHandler struct {
19-
keeper *keeper.Keeper
20-
}
16+
type gnoAnteHandler struct{}
2117

22-
func (gad *gnoAnteHandler) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
18+
func (gnoAnteHandler) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
2319
msgs := tx.GetMsgs()
2420

2521
gnoVMCount := 0
@@ -37,56 +33,13 @@ func (gad *gnoAnteHandler) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
3733
}
3834

3935
// Reject transactions that mix GnoVM and non-GnoVM messages
40-
// This is necessary because they use different gas accounting mechanisms
4136
if gnoVMCount > 0 && nonGnoVMCount > 0 {
4237
return ctx, fmt.Errorf("cannot mix GnoVM messages with non-GnoVM messages in the same transaction")
4338
}
4439

45-
if gnoVMCount == 0 {
46-
return next(ctx, tx, simulate)
47-
}
48-
49-
// Get the gas limit from the current SDK context
50-
// This was set by earlier ante handlers based on the transaction's gas wanted
51-
gasLimit := ctx.GasMeter().Limit()
52-
53-
// Use infinite gas meter in the SDK context to prevent double-counting
54-
// The GnoVM has its own internal gas tracking mechanism through the transaction store
55-
newCtx = ctx.WithGasMeter(storetypes.NewInfiniteGasMeter())
56-
gnoCtx, err := gad.keeper.BuildGnoContext(newCtx)
57-
if err != nil {
58-
return ctx, fmt.Errorf("failed to build gno context: %w", err)
59-
}
60-
61-
if !simulate {
62-
gnoCtx = gnoCtx.WithGasMeter(gnostore.NewGasMeter(gnostore.Gas(gasLimit)))
63-
}
64-
65-
// Set up defer/recover to handle gas consumption and out-of-gas errors
66-
// This pattern follows the Gno tm2/pkg/sdk/auth/ante.go implementation
67-
defer func() {
68-
if r := recover(); r != nil {
69-
switch ex := r.(type) {
70-
case gnostore.OutOfGasError:
71-
gasConsumed := gnoCtx.GasMeter().GasConsumed()
72-
73-
log := fmt.Sprintf(
74-
"out of gas in location: %s; gasConsumed: %d, gasLimit: %d",
75-
ex.Descriptor, gasConsumed, gasLimit,
76-
)
77-
err = fmt.Errorf("out of gas: %s", log)
78-
default:
79-
panic(r)
80-
}
81-
}
82-
}()
83-
84-
// After transaction execution, sync the gas consumed from GnoVM to SDK context
85-
newCtx, err = next(newCtx, tx, simulate)
86-
if err == nil {
87-
gasConsumed := gnoCtx.GasMeter().GasConsumed()
88-
ctx.GasMeter().ConsumeGas(storetypes.Gas(gasConsumed), "gnovm execution")
40+
if gnoVMCount > 0 && simulate {
41+
newCtx = ctx.WithGasMeter(storetypes.NewInfiniteGasMeter())
8942
}
9043

91-
return newCtx, err
44+
return next(newCtx, tx, simulate)
9245
}

x/gnovm/keeper/keeper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (k *Keeper) initializeVMKeeper(sdkCtx sdk.Context) error {
118118
gnostore.NewStoreKey(k.memStoreKey.Name()),
119119
)
120120

121-
chainID := sdkCtx.HeaderInfo().ChainID
121+
chainID := sdkCtx.ChainID()
122122
if chainID == "" {
123123
k.logger.Warn("chainID is empty when building gno context, using default", "fallback", defaultChainID)
124124
chainID = defaultChainID
@@ -182,7 +182,7 @@ func (k *Keeper) BuildGnoContext(sdkCtx sdk.Context) (gnosdk.Context, error) {
182182
gnostore.NewStoreKey(k.memStoreKey.Name()),
183183
)
184184

185-
chainID := sdkCtx.HeaderInfo().ChainID
185+
chainID := sdkCtx.ChainID()
186186
if chainID == "" {
187187
k.logger.Warn("chainID is empty when building gno context, using default", "fallback", defaultChainID)
188188
chainID = defaultChainID

x/gnovm/keeper/msg_server_add_package.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package keeper
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67

78
errorsmod "cosmossdk.io/errors"
9+
storetypes "cosmossdk.io/store/types"
810
sdk "github.com/cosmos/cosmos-sdk/types"
9-
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
11+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
1012

13+
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
1114
"github.com/gnolang/gno/tm2/pkg/std"
1215

1316
"github.com/ignite/gnovm/x/gnovm/types"
@@ -42,12 +45,28 @@ func (k msgServer) AddPackage(ctx context.Context, msg *types.MsgAddPackage) (*t
4245
Send: send,
4346
MaxDeposit: maxDep,
4447
}
48+
49+
defer func() {
50+
if r := recover(); r != nil {
51+
switch rType := r.(type) {
52+
case storetypes.ErrorOutOfGas:
53+
log := fmt.Sprintf(
54+
"out of gas from VM usage in location: %v; gasUsed: %d",
55+
rType.Descriptor, sdkCtx.GasMeter().GasConsumed())
56+
57+
err = errorsmod.Wrap(sdkerrors.ErrOutOfGas, log)
58+
default:
59+
err = fmt.Errorf("panic while calling VM: %v", r)
60+
}
61+
} else {
62+
// this commits the changes to the module store (that is only committed later)
63+
k.VMKeeper.CommitGnoTransactionStore(gnoCtx)
64+
}
65+
}()
66+
4567
if err := k.VMKeeper.AddPackage(gnoCtx, vmMsg); err != nil {
4668
return nil, errorsmod.Wrap(err, "failed to add package")
4769
}
4870

49-
// this commits the changes to the module store (that is only committed later)
50-
k.VMKeeper.CommitGnoTransactionStore(gnoCtx)
51-
5271
return &types.MsgAddPackageResponse{}, nil
5372
}

x/gnovm/keeper/msg_server_call.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66

77
errorsmod "cosmossdk.io/errors"
8+
storetypes "cosmossdk.io/store/types"
89
sdk "github.com/cosmos/cosmos-sdk/types"
10+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
911
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
1012

1113
"github.com/ignite/gnovm/x/gnovm/types"
@@ -31,9 +33,19 @@ func (k msgServer) Call(ctx context.Context, msg *types.MsgCall) (resp *types.Ms
3133
Func: msg.Function,
3234
Args: msg.Args,
3335
}
36+
3437
defer func() {
3538
if r := recover(); r != nil {
36-
err = fmt.Errorf("panic while calling VM: %v", r)
39+
switch rType := r.(type) {
40+
case storetypes.ErrorOutOfGas:
41+
log := fmt.Sprintf(
42+
"out of gas from VM usage in location: %v; gasUsed: %d",
43+
rType.Descriptor, sdkCtx.GasMeter().GasConsumed())
44+
45+
err = errorsmod.Wrap(sdkerrors.ErrOutOfGas, log)
46+
default:
47+
err = fmt.Errorf("panic while calling VM: %v (%v)", r, rType)
48+
}
3749
} else {
3850
// this commits the changes to the module store (that is only committed later)
3951
k.VMKeeper.CommitGnoTransactionStore(gnoCtx)

x/gnovm/keeper/msg_server_run.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package keeper
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67

78
errorsmod "cosmossdk.io/errors"
9+
storetypes "cosmossdk.io/store/types"
810
sdk "github.com/cosmos/cosmos-sdk/types"
11+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
912
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
1013
"github.com/gnolang/gno/tm2/pkg/std"
1114

@@ -35,6 +38,24 @@ func (k msgServer) Run(ctx context.Context, msg *types.MsgRun) (*types.MsgRunRes
3538
return nil, errorsmod.Wrap(err, "invalid package")
3639
}
3740

41+
defer func() {
42+
if r := recover(); r != nil {
43+
switch rType := r.(type) {
44+
case storetypes.ErrorOutOfGas:
45+
log := fmt.Sprintf(
46+
"out of gas from VM usage in location: %v; gasUsed: %d",
47+
rType.Descriptor, sdkCtx.GasMeter().GasConsumed())
48+
49+
err = errorsmod.Wrap(sdkerrors.ErrOutOfGas, log)
50+
default:
51+
err = fmt.Errorf("panic while calling VM: %v", r)
52+
}
53+
} else {
54+
// this commits the changes to the module store (that is only committed later)
55+
k.VMKeeper.CommitGnoTransactionStore(gnoCtx)
56+
}
57+
}()
58+
3859
resp, err := k.VMKeeper.Run(
3960
gnoCtx,
4061
vm.MsgRun{
@@ -48,9 +69,6 @@ func (k msgServer) Run(ctx context.Context, msg *types.MsgRun) (*types.MsgRunRes
4869
return nil, errorsmod.Wrap(err, "failed to run VM")
4970
}
5071

51-
// this commits the changes to the module store (that is only committed later)
52-
k.VMKeeper.CommitGnoTransactionStore(gnoCtx)
53-
5472
return &types.MsgRunResponse{
5573
Result: string(resp),
5674
}, nil

0 commit comments

Comments
 (0)