github.com/InjectiveLabs/sdk-go@v1.53.0/chain/types/gas.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"sync"
     7  
     8  	storetypes "cosmossdk.io/store/types"
     9  )
    10  
    11  type infiniteGasMeter struct {
    12  	consumed storetypes.Gas
    13  	mux      sync.RWMutex
    14  }
    15  
    16  // NewThreadsafeInfiniteGasMeter returns a reference to a new thread-safe infiniteGasMeter.
    17  func NewThreadsafeInfiniteGasMeter() storetypes.GasMeter {
    18  	return &infiniteGasMeter{
    19  		consumed: 0,
    20  	}
    21  }
    22  
    23  func (g *infiniteGasMeter) GasRemaining() storetypes.Gas {
    24  	return math.MaxUint64
    25  }
    26  
    27  func (g *infiniteGasMeter) GasConsumed() storetypes.Gas {
    28  	g.mux.RLock()
    29  	defer g.mux.RUnlock()
    30  
    31  	return g.consumed
    32  }
    33  
    34  func (g *infiniteGasMeter) GasConsumedToLimit() storetypes.Gas {
    35  	g.mux.RLock()
    36  	defer g.mux.RUnlock()
    37  
    38  	return g.consumed
    39  }
    40  
    41  func (g *infiniteGasMeter) Limit() storetypes.Gas {
    42  	return 0
    43  }
    44  
    45  func (g *infiniteGasMeter) ConsumeGas(amount storetypes.Gas, descriptor string) {
    46  	g.mux.Lock()
    47  	defer g.mux.Unlock()
    48  
    49  	var overflow bool
    50  	// TODO: Should we set the consumed field after overflow checking?
    51  	g.consumed, overflow = addUint64Overflow(g.consumed, amount)
    52  	if overflow {
    53  		panic(storetypes.ErrorGasOverflow{Descriptor: descriptor})
    54  	}
    55  }
    56  
    57  // RefundGas will deduct the given amount from the gas consumed. If the amount is greater than the
    58  // gas consumed, the function will panic.
    59  //
    60  // Use case: This functionality enables refunding gas to the trasaction or block gas pools so that
    61  // EVM-compatible chains can fully support the go-ethereum StateDb interface.
    62  // See https://github.com/cosmos/cosmos-sdk/pull/9403 for reference.
    63  func (g *infiniteGasMeter) RefundGas(amount storetypes.Gas, descriptor string) {
    64  	g.mux.Lock()
    65  	defer g.mux.Unlock()
    66  
    67  	if g.consumed < amount {
    68  		panic(storetypes.ErrorNegativeGasConsumed{Descriptor: descriptor})
    69  	}
    70  
    71  	g.consumed -= amount
    72  }
    73  
    74  func (g *infiniteGasMeter) IsPastLimit() bool {
    75  	return false
    76  }
    77  
    78  func (g *infiniteGasMeter) IsOutOfGas() bool {
    79  	return false
    80  }
    81  
    82  func (g *infiniteGasMeter) String() string {
    83  	g.mux.RLock()
    84  	defer g.mux.RUnlock()
    85  
    86  	return fmt.Sprintf("InfiniteGasMeter:\n  consumed: %d", g.consumed)
    87  }
    88  
    89  // addUint64Overflow performs the addition operation on two uint64 integers and
    90  // returns a boolean on whether or not the result overflows.
    91  func addUint64Overflow(a, b uint64) (uint64, bool) {
    92  	if math.MaxUint64-a < b {
    93  		return 0, true
    94  	}
    95  
    96  	return a + b, false
    97  }