From 46c8ec8fc944e1dcc38702003270b12df4abcf21 Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 30 May 2023 12:18:54 +0200 Subject: [PATCH] feat(core): add gas meter (#16310) --- core/gas/meter.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 core/gas/meter.go diff --git a/core/gas/meter.go b/core/gas/meter.go new file mode 100644 index 0000000000..2bb735facc --- /dev/null +++ b/core/gas/meter.go @@ -0,0 +1,40 @@ +// Package gas provides a basic API for app modules to track gas usage. +package gas + +import "context" + +type Gas = uint64 + +// Service represents a gas service which can retrieve and set a gas meter in a context. +// gas.Service is a core API type that should be provided by the runtime module being used to +// build an app via depinject. +type Service interface { + // GetGasMeter returns the current transaction-level gas meter. A non-nil meter + // is always returned. When one is unavailable in the context an infinite gas meter + // will be returned. + GetGasMeter(context.Context) Meter + + // GetBlockGasMeter 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. + GetBlockGasMeter(context.Context) Meter + + // WithGasMeter returns a new context with the provided transaction-level gas meter. + WithGasMeter(ctx context.Context, meter Meter) context.Context + + // WithBlockGasMeter returns a new context with the provided block-level gas meter. + WithBlockGasMeter(ctx context.Context, meter Meter) context.Context +} + +// Meter represents a gas meter. +type Meter interface { + GasConsumed() Gas + GasConsumedToLimit() Gas + GasRemaining() Gas + Limit() Gas + ConsumeGas(amount Gas, descriptor string) + RefundGas(amount Gas, descriptor string) + IsPastLimit() bool + IsOutOfGas() bool + String() string +}