github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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/holiman/uint256" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/common/math" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/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 !config.IsLondon(parent.Number) { 38 parentGasLimit = parent.GasLimit * params.ElasticityMultiplier 39 } 40 if err := VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil { 41 return err 42 } 43 // Verify the header is not malformed 44 if header.BaseFee == nil { 45 return fmt.Errorf("header is missing baseFee") 46 } 47 // Verify the baseFee is correct based on the parent header. 48 expectedBaseFee := CalcBaseFee(config, parent) 49 if header.BaseFee.Cmp(expectedBaseFee) != 0 { 50 return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d", 51 expectedBaseFee, header.BaseFee, parent.BaseFee, parent.GasUsed) 52 } 53 return nil 54 } 55 56 // CalcBaseFee calculates the basefee of the header. 57 func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { 58 // If the current block is the first EIP-1559 block, return the InitialBaseFee. 59 if !config.IsLondon(parent.Number) { 60 return new(big.Int).SetUint64(params.InitialBaseFee) 61 } 62 63 var ( 64 parentGasTarget = parent.GasLimit / params.ElasticityMultiplier 65 parentGasTargetBig = new(big.Int).SetUint64(parentGasTarget) 66 baseFeeChangeDenominatorUint64 = params.BaseFeeChangeDenominator(config.Bor, parent.Number) 67 baseFeeChangeDenominator = new(big.Int).SetUint64(baseFeeChangeDenominatorUint64) 68 ) 69 // If the parent gasUsed is the same as the target, the baseFee remains unchanged. 70 if parent.GasUsed == parentGasTarget { 71 return new(big.Int).Set(parent.BaseFee) 72 } 73 if parent.GasUsed > parentGasTarget { 74 // If the parent block used more gas than its target, the baseFee should increase. 75 gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parentGasTarget) 76 x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta) 77 y := x.Div(x, parentGasTargetBig) 78 baseFeeDelta := math.BigMax( 79 x.Div(y, baseFeeChangeDenominator), 80 common.Big1, 81 ) 82 83 return x.Add(parent.BaseFee, baseFeeDelta) 84 } else { 85 // Otherwise if the parent block used less gas than its target, the baseFee should decrease. 86 gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parent.GasUsed) 87 x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta) 88 y := x.Div(x, parentGasTargetBig) 89 baseFeeDelta := x.Div(y, baseFeeChangeDenominator) 90 91 return math.BigMax( 92 x.Sub(parent.BaseFee, baseFeeDelta), 93 common.Big0, 94 ) 95 } 96 } 97 98 // CalcBaseFee calculates the basefee of the header. 99 func CalcBaseFeeUint(config *params.ChainConfig, parent *types.Header) *uint256.Int { 100 var ( 101 initialBaseFeeUint = uint256.NewInt(params.InitialBaseFee) 102 baseFeeChangeDenominatorUint64 = params.BaseFeeChangeDenominator(config.Bor, parent.Number) 103 baseFeeChangeDenominatorUint = uint256.NewInt(baseFeeChangeDenominatorUint64) 104 ) 105 106 // If the current block is the first EIP-1559 block, return the InitialBaseFee. 107 if !config.IsLondon(parent.Number) { 108 return initialBaseFeeUint.Clone() 109 } 110 111 var ( 112 parentGasTarget = parent.GasLimit / params.ElasticityMultiplier 113 parentGasTargetBig = uint256.NewInt(parentGasTarget) 114 ) 115 116 // If the parent gasUsed is the same as the target, the baseFee remains unchanged. 117 if parent.GasUsed == parentGasTarget { 118 return math.FromBig(parent.BaseFee) 119 } 120 121 if parent.GasUsed > parentGasTarget { 122 // If the parent block used more gas than its target, the baseFee should increase. 123 gasUsedDelta := uint256.NewInt(parent.GasUsed - parentGasTarget) 124 125 parentBaseFee := math.FromBig(parent.BaseFee) 126 x := gasUsedDelta.Mul(parentBaseFee, gasUsedDelta) 127 y := x.Div(x, parentGasTargetBig) 128 baseFeeDelta := math.BigMaxUint( 129 x.Div(y, baseFeeChangeDenominatorUint), 130 math.U1, 131 ) 132 133 return x.Add(parentBaseFee, baseFeeDelta) 134 } 135 136 // Otherwise if the parent block used less gas than its target, the baseFee should decrease. 137 gasUsedDelta := uint256.NewInt(parentGasTarget - parent.GasUsed) 138 parentBaseFee := math.FromBig(parent.BaseFee) 139 x := gasUsedDelta.Mul(parentBaseFee, gasUsedDelta) 140 y := x.Div(x, parentGasTargetBig) 141 baseFeeDelta := x.Div(y, baseFeeChangeDenominatorUint) 142 143 return math.BigMaxUint( 144 x.Sub(parentBaseFee, baseFeeDelta), 145 math.U0.Clone(), 146 ) 147 }