Skip to content

Commit

Permalink
refactor(core/gas): remove WithXXGasMeter from gas service (#20072)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored May 28, 2024
1 parent 18a203b commit dac569c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
6 changes: 0 additions & 6 deletions core/gas/service.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}

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()
}

// 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

0 comments on commit dac569c

Please sign in to comment.