github.com/dominant-strategies/go-quai@v0.28.2/consensus/misc/basefee.go (about)

     1  // Copyright 2021 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package misc
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/dominant-strategies/go-quai/common/math"
    23  	"github.com/dominant-strategies/go-quai/core/types"
    24  	"github.com/dominant-strategies/go-quai/params"
    25  )
    26  
    27  // CalcBaseFee calculates the basefee of the header taking into account the basefee ceiling
    28  func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
    29  	calculatedBaseFee := calcBaseFee(config, parent)
    30  	ceiling := big.NewInt(params.MaxBaseFee)
    31  	if calculatedBaseFee.Cmp(ceiling) > 0 {
    32  		return ceiling
    33  	}
    34  	return calculatedBaseFee
    35  }
    36  
    37  // calcBaseFee calculates the basefee of the header.
    38  func calcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
    39  	var (
    40  		parentGasTarget          = parent.GasLimit() / params.ElasticityMultiplier
    41  		parentGasTargetBig       = new(big.Int).SetUint64(parentGasTarget)
    42  		baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator)
    43  	)
    44  	// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
    45  	if parent.GasUsed() == parentGasTarget {
    46  		return new(big.Int).Set(parent.BaseFee())
    47  	}
    48  	if parent.GasUsed() > parentGasTarget {
    49  		// If the parent block used more gas than its target, the baseFee should increase.
    50  		gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed() - parentGasTarget)
    51  		x := new(big.Int).Mul(parent.BaseFee(), gasUsedDelta)
    52  		y := x.Div(x, parentGasTargetBig)
    53  		baseFeeDelta := math.BigMax(x.Div(y, baseFeeChangeDenominator), big.NewInt(params.GWei))
    54  
    55  		return x.Add(parent.BaseFee(), baseFeeDelta)
    56  	} else {
    57  		// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
    58  		gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parent.GasUsed())
    59  		x := new(big.Int).Mul(parent.BaseFee(), gasUsedDelta)
    60  		y := x.Div(x, parentGasTargetBig)
    61  		baseFeeDelta := x.Div(y, baseFeeChangeDenominator)
    62  
    63  		return math.BigMax(x.Sub(parent.BaseFee(), baseFeeDelta), big.NewInt(params.GWei))
    64  	}
    65  }