gitee.com/liu-zhao234568/cntest@v1.0.0/consensus/misc/eip1559.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  	"fmt"
    21  	"math/big"
    22  
    23  	"gitee.com/liu-zhao234568/cntest/common"
    24  	"gitee.com/liu-zhao234568/cntest/common/math"
    25  	"gitee.com/liu-zhao234568/cntest/core/types"
    26  	"gitee.com/liu-zhao234568/cntest/params"
    27  )
    28  
    29  // VerifyEip1559Header verifies some header attributes which were changed in EIP-1559,
    30  // - gas limit check
    31  // - basefee check
    32  func VerifyEip1559Header(config *params.ChainConfig, parent, header *types.Header) error {
    33  	// Verify that the gas limit remains within allowed bounds
    34  	parentGasLimit := parent.GasLimit
    35  	if !config.IsLondon(parent.Number) {
    36  		parentGasLimit = parent.GasLimit * params.ElasticityMultiplier
    37  	}
    38  	if err := VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
    39  		return err
    40  	}
    41  	// Verify the header is not malformed
    42  	if header.BaseFee == nil {
    43  		return fmt.Errorf("header is missing baseFee")
    44  	}
    45  	// Verify the baseFee is correct based on the parent header.
    46  	expectedBaseFee := CalcBaseFee(config, parent)
    47  	if header.BaseFee.Cmp(expectedBaseFee) != 0 {
    48  		return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d",
    49  			expectedBaseFee, header.BaseFee, parent.BaseFee, parent.GasUsed)
    50  	}
    51  	return nil
    52  }
    53  
    54  // CalcBaseFee calculates the basefee of the header.
    55  func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
    56  	// If the current block is the first EIP-1559 block, return the InitialBaseFee.
    57  	if !config.IsLondon(parent.Number) {
    58  		return new(big.Int).SetUint64(params.InitialBaseFee)
    59  	}
    60  
    61  	var (
    62  		parentGasTarget          = parent.GasLimit / params.ElasticityMultiplier
    63  		parentGasTargetBig       = new(big.Int).SetUint64(parentGasTarget)
    64  		baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator)
    65  	)
    66  	// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
    67  	if parent.GasUsed == parentGasTarget {
    68  		return new(big.Int).Set(parent.BaseFee)
    69  	}
    70  	if parent.GasUsed > parentGasTarget {
    71  		// If the parent block used more gas than its target, the baseFee should increase.
    72  		gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parentGasTarget)
    73  		x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
    74  		y := x.Div(x, parentGasTargetBig)
    75  		baseFeeDelta := math.BigMax(
    76  			x.Div(y, baseFeeChangeDenominator),
    77  			common.Big1,
    78  		)
    79  
    80  		return x.Add(parent.BaseFee, baseFeeDelta)
    81  	} else {
    82  		// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
    83  		gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parent.GasUsed)
    84  		x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
    85  		y := x.Div(x, parentGasTargetBig)
    86  		baseFeeDelta := x.Div(y, baseFeeChangeDenominator)
    87  
    88  		return math.BigMax(
    89  			x.Sub(parent.BaseFee, baseFeeDelta),
    90  			common.Big0,
    91  		)
    92  	}
    93  }