Skip to content

Commit

Permalink
feat(baseapp): add option to disable block gas meter (cosmos#19626)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang authored and mmsqe committed Jan 2, 2025
1 parent 059b656 commit 5746096
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ type BaseApp struct {
// includeNestedMsgsGas holds a set of message types for which gas costs for its nested messages are calculated.
includeNestedMsgsGas map[string]struct{}

// disableBlockGasMeter will disable the block gas meter if true, block gas meter is tricky to support
// when executing transactions in parallel.
// when disabled, the block gas meter in context is a noop one.
//
// SAFETY: it's safe to do if validators validate the total gas wanted in the `ProcessProposal`, which is the case in the default handler.
disableBlockGasMeter bool

// Optional alternative tx executor, used for block-stm parallel transaction execution.
txExecutor TxExecutor
}
Expand Down Expand Up @@ -665,6 +672,10 @@ func (app *BaseApp) getState(mode execMode) *state {
}

func (app *BaseApp) getBlockGasMeter(ctx sdk.Context) storetypes.GasMeter {
if app.disableBlockGasMeter {
return noopGasMeter{}
}

if maxGas := app.GetMaximumBlockGas(ctx); maxGas > 0 {
return storetypes.NewGasMeter(maxGas)
}
Expand Down
17 changes: 17 additions & 0 deletions baseapp/noopgasmeter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package baseapp

import storetypes "cosmossdk.io/store/types"

type noopGasMeter struct{}

var _ storetypes.GasMeter = noopGasMeter{}

func (noopGasMeter) GasConsumed() storetypes.Gas { return 0 }
func (noopGasMeter) GasConsumedToLimit() storetypes.Gas { return 0 }
func (noopGasMeter) GasRemaining() storetypes.Gas { return 0 }
func (noopGasMeter) Limit() storetypes.Gas { return 0 }
func (noopGasMeter) ConsumeGas(storetypes.Gas, string) {}
func (noopGasMeter) RefundGas(storetypes.Gas, string) {}
func (noopGasMeter) IsPastLimit() bool { return false }
func (noopGasMeter) IsOutOfGas() bool { return false }
func (noopGasMeter) String() string { return "noopGasMeter" }
10 changes: 10 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func SetIncludeNestedMsgsGas(msgs []sdk.Msg) func(*BaseApp) {
}
}

// DisableBlockGasMeter disables the block gas meter.
func DisableBlockGasMeter() func(*BaseApp) {
return func(app *BaseApp) { app.SetDisableBlockGasMeter(true) }
}

// SetTxExecutor sets a custom tx executor for the BaseApp, usually for parallel execution.
func SetTxExecutor(executor TxExecutor) func(*BaseApp) {
return func(app *BaseApp) { app.txExecutor = executor }
Expand Down Expand Up @@ -426,6 +431,11 @@ func (app *BaseApp) SetGRPCQueryRouter(grpcQueryRouter *GRPCQueryRouter) {
app.grpcQueryRouter = grpcQueryRouter
}

// SetDisableBlockGasMeter sets the disableBlockGasMeter flag for the BaseApp.
func (app *BaseApp) SetDisableBlockGasMeter(disableBlockGasMeter bool) {
app.disableBlockGasMeter = disableBlockGasMeter
}

// SetTxExecutor sets a custom tx executor for the BaseApp, usually for parallel execution.
func (app *BaseApp) SetTxExecutor(executor TxExecutor) {
app.txExecutor = executor
Expand Down

0 comments on commit 5746096

Please sign in to comment.