github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/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 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/common/math" 25 "github.com/ethereum/go-ethereum/core/types" 26 "github.com/ethereum/go-ethereum/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 header.BaseFee, expectedBaseFee, 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 parentGasTarget := parent.GasLimit / params.ElasticityMultiplier 62 // If the parent gasUsed is the same as the target, the baseFee remains unchanged. 63 if parent.GasUsed == parentGasTarget { 64 return new(big.Int).Set(parent.BaseFee) 65 } 66 67 var ( 68 num = new(big.Int) 69 denom = new(big.Int) 70 ) 71 72 if parent.GasUsed > parentGasTarget { 73 // If the parent block used more gas than its target, the baseFee should increase. 74 // max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator) 75 num.SetUint64(parent.GasUsed - parentGasTarget) 76 num.Mul(num, parent.BaseFee) 77 num.Div(num, denom.SetUint64(parentGasTarget)) 78 num.Div(num, denom.SetUint64(params.BaseFeeChangeDenominator)) 79 baseFeeDelta := math.BigMax(num, common.Big1) 80 81 return num.Add(parent.BaseFee, baseFeeDelta) 82 } else { 83 // Otherwise if the parent block used less gas than its target, the baseFee should decrease. 84 // max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator) 85 num.SetUint64(parentGasTarget - parent.GasUsed) 86 num.Mul(num, parent.BaseFee) 87 num.Div(num, denom.SetUint64(parentGasTarget)) 88 num.Div(num, denom.SetUint64(params.BaseFeeChangeDenominator)) 89 baseFee := num.Sub(parent.BaseFee, num) 90 91 return math.BigMax(baseFee, common.Big0) 92 } 93 }