github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/evmcore/tx_pool_helper.go (about)

     1  package evmcore
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/unicornultrafoundation/go-u2u/common"
     7  	"github.com/unicornultrafoundation/go-u2u/common/math"
     8  	"github.com/unicornultrafoundation/go-u2u/core/types"
     9  	"github.com/unicornultrafoundation/go-u2u/params"
    10  )
    11  
    12  // calcBaseFee calculates the BaseFee of the header.
    13  func calcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
    14  	// If the current block is the first EIP-1559 block, return the InitialBaseFee.
    15  	if !config.IsLondon(parent.Number) {
    16  		return new(big.Int).SetUint64(params.InitialBaseFee)
    17  	}
    18  
    19  	var (
    20  		parentGasTarget          = parent.GasLimit / params.ElasticityMultiplier
    21  		parentGasTargetBig       = new(big.Int).SetUint64(parentGasTarget)
    22  		baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator)
    23  	)
    24  	// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
    25  	if parent.GasUsed == parentGasTarget {
    26  		return new(big.Int).Set(parent.BaseFee)
    27  	}
    28  	if parent.GasUsed > parentGasTarget {
    29  		// If the parent block used more gas than its target, the baseFee should increase.
    30  		gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parentGasTarget)
    31  		x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
    32  		y := x.Div(x, parentGasTargetBig)
    33  		baseFeeDelta := math.BigMax(
    34  			x.Div(y, baseFeeChangeDenominator),
    35  			common.Big1,
    36  		)
    37  
    38  		return x.Add(parent.BaseFee, baseFeeDelta)
    39  	} else {
    40  		// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
    41  		gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parent.GasUsed)
    42  		x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
    43  		y := x.Div(x, parentGasTargetBig)
    44  		baseFeeDelta := x.Div(y, baseFeeChangeDenominator)
    45  
    46  		return math.BigMax(
    47  			x.Sub(parent.BaseFee, baseFeeDelta),
    48  			common.Big0,
    49  		)
    50  	}
    51  }