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