github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/gaspool.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  )
     7  
     8  // GasPool tracks the amount of gas available during execution of the transactions
     9  // in a block. The zero value is a pool with zero gas available.
    10  type GasPool uint64
    11  
    12  // AddGas makes gas available for execution.
    13  func (gp *GasPool) AddGas(amount uint64) *GasPool {
    14  	if uint64(*gp) > math.MaxUint64-amount {
    15  		panic("gas pool pushed above uint64")
    16  	}
    17  	*(*uint64)(gp) += amount
    18  	return gp
    19  }
    20  
    21  // SubGas deducts the given amount from the pool if enough gas is
    22  // available and returns an error otherwise.
    23  func (gp *GasPool) SubGas(amount uint64) error {
    24  	if uint64(*gp) < amount {
    25  		return ErrGasLimitReached
    26  	}
    27  	*(*uint64)(gp) -= amount
    28  	return nil
    29  }
    30  
    31  // Gas returns the amount of gas remaining in the pool.
    32  func (gp *GasPool) Gas() uint64 {
    33  	return uint64(*gp)
    34  }
    35  
    36  func (gp *GasPool) String() string {
    37  	return fmt.Sprintf("%d", *gp)
    38  }