Skip to content

Commit

Permalink
refactor(core/gas): remove WithXXGasMeter from gas service
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed May 8, 2024
1 parent 03d70aa commit b0fd8bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
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
50 changes: 21 additions & 29 deletions runtime/gas.go
Expand Up @@ -18,22 +18,35 @@ 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) {
cgm.gm.ConsumeGas(amount, descriptor)
}

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

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 @@ -78,27 +91,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) {
cgm.gm.ConsumeGas(amount, descriptor)
}

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

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 b0fd8bd

Please sign in to comment.