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