From dac569c3a2a5277cec1b5ecba978074ab45def80 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 28 May 2024 15:43:27 +0200 Subject: [PATCH] refactor(core/gas): remove `WithXXGasMeter` from gas service (#20072) --- core/gas/service.go | 6 ----- runtime/gas.go | 54 +++++++++++++++++++-------------------------- 2 files changed, 23 insertions(+), 37 deletions(-) diff --git a/core/gas/service.go b/core/gas/service.go index ba04d84eed55..dbd4f099b60c 100644 --- a/core/gas/service.go +++ b/core/gas/service.go @@ -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 } diff --git a/runtime/gas.go b/runtime/gas.go index 50121da9e069..75d711f9f4da 100644 --- a/runtime/gas.go +++ b/runtime/gas.go @@ -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 @@ -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 }