github.com/amazechain/amc@v0.1.3/internal/consensus/misc/eip1559.go (about)

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