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

feat(baseapp): add option to disable block gas meter #19626

Merged
merged 7 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (x/auth) [#19549](https://github.com/cosmos/cosmos-sdk/pull/19549) Accept custom get signers when injecting `x/auth/tx`.

### Features

* (baseapp) [#19626](https://github.com/cosmos/cosmos-sdk/pull/19626) Adds `DisableBlockGasMeter` option to `BaseApp`, which removes the block gas meter during transaction execution.

## [v0.50.4](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.4) - 2023-02-19

### Features
Expand Down
11 changes: 11 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ type BaseApp struct {
// including the goroutine handling.This is experimental and must be enabled
// by developers.
optimisticExec *oe.OptimisticExecution

// 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 `nil`.
yihuang marked this conversation as resolved.
Show resolved Hide resolved
//
// SAFTY: it's safe to do if validators validates the total gas wanted in the proposal.
yihuang marked this conversation as resolved.
Show resolved Hide resolved
disableBlockGasMeter bool
Copy link
Contributor

Choose a reason for hiding this comment

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

The addition of the disableBlockGasMeter field to the BaseApp struct introduces the capability to disable the block gas meter. This change is crucial for supporting parallel transaction execution without the complications of block gas metering. However, it's important to ensure that this feature is well-documented, especially the safety aspects mentioned in the comment. The comment mentions that it's safe if validators validate the total gas wanted in the proposal, which implies a reliance on external validation mechanisms. This should be clearly documented in the official documentation to avoid misuse of the feature.

Consider enhancing the documentation around this feature, both in code comments and the official Cosmos SDK documentation, to clearly explain its usage, the conditions under which it is safe to disable the block gas meter, and the responsibilities of validators in this context.

}

// NewBaseApp returns a reference to an initialized BaseApp. It accepts a
Expand Down Expand Up @@ -640,6 +647,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 @@ -117,6 +117,11 @@ func SetOptimisticExecution(opts ...func(*oe.OptimisticExecution)) func(*BaseApp
}
}

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

func (app *BaseApp) SetName(name string) {
if app.sealed {
panic("SetName() on sealed BaseApp")
Expand Down Expand Up @@ -362,3 +367,8 @@ func (app *BaseApp) SetStoreMetrics(gatherer metrics.StoreMetrics) {
func (app *BaseApp) SetStreamingManager(manager storetypes.StreamingManager) {
app.streamingManager = manager
}

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