github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/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/scroll-tech/go-ethereum/common" 24 "github.com/scroll-tech/go-ethereum/common/math" 25 "github.com/scroll-tech/go-ethereum/core/types" 26 "github.com/scroll-tech/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 && config.Scroll.BaseFeeEnabled() { 43 return fmt.Errorf("header is missing baseFee") 44 } 45 // Now BaseFee can be nil, because !config.Scroll.BaseFeeEnabled() 46 if header.BaseFee == nil { 47 return nil 48 } 49 // Verify the baseFee is correct based on the parent header. 50 51 var expectedBaseFee *big.Int 52 53 // compatible check with the logic in commitNewWork 54 if config.Clique == nil || config.Scroll.BaseFeeEnabled() { 55 expectedBaseFee = CalcBaseFee(config, parent) 56 } else { 57 expectedBaseFee = big.NewInt(0) 58 } 59 60 if header.BaseFee.Cmp(expectedBaseFee) != 0 { 61 return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d", 62 expectedBaseFee, header.BaseFee, parent.BaseFee, parent.GasUsed) 63 } 64 return nil 65 } 66 67 // CalcBaseFee calculates the basefee of the header. 68 func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { 69 // If the current block is the first EIP-1559 block, return the InitialBaseFee. 70 if !config.IsLondon(parent.Number) { 71 return new(big.Int).SetUint64(params.InitialBaseFee) 72 } 73 74 var ( 75 parentGasTarget = parent.GasLimit / params.ElasticityMultiplier 76 parentGasTargetBig = new(big.Int).SetUint64(parentGasTarget) 77 baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator) 78 ) 79 if !config.Scroll.BaseFeeEnabled() { 80 return nil 81 } 82 // If the parent gasUsed is the same as the target, the baseFee remains unchanged. 83 if parent.GasUsed == parentGasTarget { 84 return new(big.Int).Set(parent.BaseFee) 85 } 86 if parent.GasUsed > parentGasTarget { 87 // If the parent block used more gas than its target, the baseFee should increase. 88 gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parentGasTarget) 89 x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta) 90 y := x.Div(x, parentGasTargetBig) 91 baseFeeDelta := math.BigMax( 92 x.Div(y, baseFeeChangeDenominator), 93 common.Big1, 94 ) 95 96 return x.Add(parent.BaseFee, baseFeeDelta) 97 } else { 98 // Otherwise if the parent block used less gas than its target, the baseFee should decrease. 99 gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parent.GasUsed) 100 x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta) 101 y := x.Div(x, parentGasTargetBig) 102 baseFeeDelta := x.Div(y, baseFeeChangeDenominator) 103 104 return math.BigMax( 105 x.Sub(parent.BaseFee, baseFeeDelta), 106 common.Big0, 107 ) 108 } 109 }