github.com/theQRL/go-zond@v0.2.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/theQRL/go-zond/common" 25 "github.com/theQRL/go-zond/common/math" 26 "github.com/theQRL/go-zond/consensus/misc" 27 "github.com/theQRL/go-zond/core/types" 28 "github.com/theQRL/go-zond/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 err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil { 38 return err 39 } 40 // Verify the header is not malformed 41 if header.BaseFee == nil { 42 return errors.New("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.Cmp(expectedBaseFee) != 0 { 47 return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d", 48 header.BaseFee, expectedBaseFee, 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 *types.Header) *big.Int { 55 parentGasTarget := parent.GasLimit / config.ElasticityMultiplier() 56 // If the parent gasUsed is the same as the target, the baseFee remains unchanged. 57 if parent.GasUsed == parentGasTarget { 58 return new(big.Int).Set(parent.BaseFee) 59 } 60 61 var ( 62 num = new(big.Int) 63 denom = new(big.Int) 64 ) 65 66 if parent.GasUsed > parentGasTarget { 67 // If the parent block used more gas than its target, the baseFee should increase. 68 // max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator) 69 num.SetUint64(parent.GasUsed - parentGasTarget) 70 num.Mul(num, parent.BaseFee) 71 num.Div(num, denom.SetUint64(parentGasTarget)) 72 num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator())) 73 baseFeeDelta := math.BigMax(num, common.Big1) 74 75 return num.Add(parent.BaseFee, baseFeeDelta) 76 } else { 77 // Otherwise if the parent block used less gas than its target, the baseFee should decrease. 78 // max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator) 79 num.SetUint64(parentGasTarget - parent.GasUsed) 80 num.Mul(num, parent.BaseFee) 81 num.Div(num, denom.SetUint64(parentGasTarget)) 82 num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator())) 83 baseFee := num.Sub(parent.BaseFee, num) 84 85 return math.BigMax(baseFee, common.Big0) 86 } 87 }