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

refactor(core/gas): remove WithXXGasMeter from gas service #20072

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 0 additions & 6 deletions core/gas/service.go
Expand Up @@ -29,17 +29,11 @@ type Service interface {
// will be returned.
GasMeter(context.Context) Meter

// WithGasMeter returns a new context with the provided transaction-level gas meter.
WithGasMeter(ctx context.Context, meter Meter) context.Context

// BlockGasMeter returns the current block-level gas meter. A non-nil meter
// is always returned. When one is unavailable in the context an infinite gas meter
// will be returned.
BlockGasMeter(context.Context) Meter

// WithBlockGasMeter returns a new context with the provided block-level gas meter.
WithBlockGasMeter(ctx context.Context, meter Meter) context.Context

// GasConfig returns the gas costs.
GasConfig(ctx context.Context) GasConfig
}
Expand Down
54 changes: 23 additions & 31 deletions runtime/gas.go
Expand Up @@ -18,22 +18,37 @@ func (g GasService) GasMeter(ctx context.Context) gas.Meter {
return CoreGasmeter{gm: sdk.UnwrapSDKContext(ctx).GasMeter()}
}

func (g GasService) WithGasMeter(ctx context.Context, meter gas.Meter) context.Context {
return sdk.UnwrapSDKContext(ctx).WithGasMeter(SDKGasMeter{gm: meter})
}

func (g GasService) BlockGasMeter(ctx context.Context) gas.Meter {
return CoreGasmeter{gm: sdk.UnwrapSDKContext(ctx).BlockGasMeter()}
}

func (g GasService) WithBlockGasMeter(ctx context.Context, meter gas.Meter) context.Context {
return sdk.UnwrapSDKContext(ctx).WithGasMeter(SDKGasMeter{gm: meter})
}

func (g GasService) GasConfig(ctx context.Context) gas.GasConfig {
return gas.GasConfig(sdk.UnwrapSDKContext(ctx).KVGasConfig())
}

// CoreGasmeter is a wrapper around the SDK's GasMeter that implements the GasMeter interface.
type CoreGasmeter struct {
gm storetypes.GasMeter
}

func (cgm CoreGasmeter) Consume(amount gas.Gas, descriptor string) error {
cgm.gm.ConsumeGas(amount, descriptor)
return nil
}
Comment on lines +34 to +37
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure error handling in Consume method.

The ConsumeGas method of storetypes.GasMeter might return an error which is currently ignored. Consider handling this potential error to avoid runtime panics or incorrect gas tracking.

- cgm.gm.ConsumeGas(amount, descriptor)
+ err := cgm.gm.ConsumeGas(amount, descriptor)
+ if err != nil {
+     return err
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
func (cgm CoreGasmeter) Consume(amount gas.Gas, descriptor string) error {
cgm.gm.ConsumeGas(amount, descriptor)
return nil
}
func (cgm CoreGasmeter) Consume(amount gas.Gas, descriptor string) error {
err := cgm.gm.ConsumeGas(amount, descriptor)
if err != nil {
return err
}
return nil
}


func (cgm CoreGasmeter) Refund(amount gas.Gas, descriptor string) error {
cgm.gm.RefundGas(amount, descriptor)
return nil
}
Comment on lines +39 to +42
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure error handling in Refund method.

Similar to the Consume method, the RefundGas method might return an error that is not currently handled. This could lead to incorrect gas calculations if ignored.

- cgm.gm.RefundGas(amount, descriptor)
+ err := cgm.gm.RefundGas(amount, descriptor)
+ if err != nil {
+     return err
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
func (cgm CoreGasmeter) Refund(amount gas.Gas, descriptor string) error {
cgm.gm.RefundGas(amount, descriptor)
return nil
}
func (cgm CoreGasmeter) Refund(amount gas.Gas, descriptor string) error {
err := cgm.gm.RefundGas(amount, descriptor)
if err != nil {
return err
}
return nil
}


func (cgm CoreGasmeter) Remaining() gas.Gas {
return cgm.gm.GasRemaining()
}

func (cgm CoreGasmeter) Limit() gas.Gas {
return cgm.gm.Limit()
}

// SDKGasMeter is a wrapper around the SDK's GasMeter that implements the GasMeter interface.
type SDKGasMeter struct {
gm gas.Meter
Expand Down Expand Up @@ -82,29 +97,6 @@ func (gm SDKGasMeter) String() string {
return fmt.Sprintf("BasicGasMeter:\n limit: %d\n consumed: %d", gm.gm.Limit(), gm.gm.Remaining())
}

// CoreGasmeter is a wrapper around the SDK's GasMeter that implements the GasMeter interface.
type CoreGasmeter struct {
gm storetypes.GasMeter
}

func (cgm CoreGasmeter) Consume(amount gas.Gas, descriptor string) error {
cgm.gm.ConsumeGas(amount, descriptor)
return nil
}

func (cgm CoreGasmeter) Refund(amount gas.Gas, descriptor string) error {
cgm.gm.RefundGas(amount, descriptor)
return nil
}

func (cgm CoreGasmeter) Remaining() gas.Gas {
return cgm.gm.GasRemaining()
}

func (cgm CoreGasmeter) Limit() gas.Gas {
return cgm.gm.Limit()
}

type GasConfig struct {
gc gas.GasConfig
}
Expand Down