github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/core/gaspool.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:34</date>
    10  //</624342616107913216>
    11  
    12  
    13  package core
    14  
    15  import (
    16  	"fmt"
    17  	"math"
    18  )
    19  
    20  //gaspool跟踪交易执行期间可用的气体量
    21  //在一个街区。零值是一个具有零气体的池。
    22  type GasPool uint64
    23  
    24  //addgas使气体可用于执行。
    25  func (gp *GasPool) AddGas(amount uint64) *GasPool {
    26  	if uint64(*gp) > math.MaxUint64-amount {
    27  		panic("gas pool pushed above uint64")
    28  	}
    29  	*(*uint64)(gp) += amount
    30  	return gp
    31  }
    32  
    33  //如果有足够的气体,子气体从池中扣除给定的量。
    34  //可用,否则返回错误。
    35  func (gp *GasPool) SubGas(amount uint64) error {
    36  	if uint64(*gp) < amount {
    37  		return ErrGasLimitReached
    38  	}
    39  	*(*uint64)(gp) -= amount
    40  	return nil
    41  }
    42  
    43  //气体返回池中剩余的气体量。
    44  func (gp *GasPool) Gas() uint64 {
    45  	return uint64(*gp)
    46  }
    47  
    48  func (gp *GasPool) String() string {
    49  	return fmt.Sprintf("%d", *gp)
    50  }
    51