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

Update LimitSimulationGasDecorator with custom Gas Meter Setter #45

Merged
merged 1 commit into from
Apr 19, 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
4 changes: 2 additions & 2 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, sdk.AnteDepGenerat
}

anteDecorators := []sdk.AnteFullDecorator{
sdk.DefaultWrappedAnteDecorator(ante.NewDefaultSetUpContextDecorator()), // outermost AnteDecorator. SetUpContext must be called first
sdk.DefaultWrappedAnteDecorator(wasmkeeper.NewLimitSimulationGasDecorator(options.WasmConfig.SimulationGasLimit)), // after setup context to enforce limits early
sdk.DefaultWrappedAnteDecorator(ante.NewDefaultSetUpContextDecorator()), // outermost AnteDecorator. SetUpContext must be called first
sdk.DefaultWrappedAnteDecorator(wasmkeeper.NewLimitSimulationGasDecorator(options.WasmConfig.SimulationGasLimit, wasmkeeper.DefaultGasMeterSetter())), // after setup context to enforce limits early
sdk.DefaultWrappedAnteDecorator(wasmkeeper.NewCountTXDecorator(options.TXCounterStoreKey)),
sdk.DefaultWrappedAnteDecorator(ante.NewRejectExtensionOptionsDecorator()),
sdk.DefaultWrappedAnteDecorator(ante.NewValidateBasicDecorator()),
Expand Down
17 changes: 13 additions & 4 deletions x/wasm/keeper/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,22 @@ func decodeHeightCounter(bz []byte) (int64, uint32) {
// LimitSimulationGasDecorator ante decorator to limit gas in simulation calls
type LimitSimulationGasDecorator struct {
gasLimit *sdk.Gas
// inputs: simulate, ctx, gas limit, tx
gasMeterSetter func(bool, sdk.Context, uint64, sdk.Tx) sdk.Context
Kbhat1 marked this conversation as resolved.
Show resolved Hide resolved
}

// NewLimitSimulationGasDecorator constructor accepts nil value to fallback to block gas limit.
func NewLimitSimulationGasDecorator(gasLimit *sdk.Gas) *LimitSimulationGasDecorator {
func NewLimitSimulationGasDecorator(gasLimit *sdk.Gas, gasMeterSetter func(bool, sdk.Context, uint64, sdk.Tx) sdk.Context) *LimitSimulationGasDecorator {
if gasLimit != nil && *gasLimit == 0 {
panic("gas limit must not be zero")
}
return &LimitSimulationGasDecorator{gasLimit: gasLimit}
return &LimitSimulationGasDecorator{gasLimit, gasMeterSetter}
}

func DefaultGasMeterSetter() func(bool, sdk.Context, uint64, sdk.Tx) sdk.Context {
return func(simulate bool, ctx sdk.Context, gasLimit uint64, tx sdk.Tx) sdk.Context {
return ctx.WithGasMeter(sdk.NewGasMeter(gasLimit))
}
}

// AnteHandle that limits the maximum gas available in simulations only.
Expand All @@ -85,12 +93,13 @@ func (d LimitSimulationGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu

// apply custom node gas limit
if d.gasLimit != nil {
return next(ctx.WithGasMeter(sdk.NewGasMeter(*d.gasLimit)), tx, simulate)
return next(d.gasMeterSetter(simulate, ctx, *d.gasLimit, tx), tx, simulate)
}

// default to max block gas when set, to be on the safe side
if maxGas := ctx.ConsensusParams().GetBlock().MaxGas; maxGas > 0 {
return next(ctx.WithGasMeter(sdk.NewGasMeter(sdk.Gas(maxGas))), tx, simulate)
return next(d.gasMeterSetter(simulate, ctx, sdk.Gas(maxGas), tx), tx, simulate)
}

return next(ctx, tx, simulate)
}
4 changes: 2 additions & 2 deletions x/wasm/keeper/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ func TestLimitSimulationGasDecorator(t *testing.T) {
// when
if spec.expErr != nil {
require.PanicsWithValue(t, spec.expErr, func() {
ante := keeper.NewLimitSimulationGasDecorator(spec.customLimit)
ante := keeper.NewLimitSimulationGasDecorator(spec.customLimit, keeper.DefaultGasMeterSetter())
ante.AnteHandle(ctx, nil, spec.simulation, nextAnte)
})
return
}
ante := keeper.NewLimitSimulationGasDecorator(spec.customLimit)
ante := keeper.NewLimitSimulationGasDecorator(spec.customLimit, keeper.DefaultGasMeterSetter())
ante.AnteHandle(ctx, nil, spec.simulation, nextAnte)
})
}
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/recurse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ func TestGasCostOnQuery(t *testing.T) {
Depth: 1,
Work: 50,
},
expectedGas: 2*GasWork50 + GasReturnHashed,
expectedGas: 2*GasWork50 + GasReturnHashed + 1,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verifying this update

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mind shedding light as to why we expect 1 extra gas here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fyi @philipsu522 @udpatil this feels like it may be unrelated to this PR and possible rounding issues. Check this comment from before: https://github.com/sei-protocol/sei-wasmd/blob/UpdateLimitSimulationGasDecorator/x/wasm/keeper/recurse_test.go#L248

},
"recursion 4, some work": {
gasLimit: 400_000,
msg: Recurse{
Depth: 4,
Work: 50,
},
expectedGas: 5*GasWork50 + 4*GasReturnHashed,
expectedGas: 5*GasWork50 + 4*GasReturnHashed + 1,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verifying this update

},
}

Expand Down
Loading