|
| 1 | +// Copyright 2017 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 | + "errors" |
| 21 | + "fmt" |
| 22 | + "math/big" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/common" |
| 25 | + "github.com/ethereum/go-ethereum/core/types" |
| 26 | + "github.com/ethereum/go-ethereum/params" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + errInvalidBaseFee = errors.New("invalid BaseFee") |
| 31 | + errMissingParentBaseFee = errors.New("parent header is missing BaseFee") |
| 32 | + errMissingBaseFee = errors.New("current header is missing BaseFee") |
| 33 | + errHaveBaseFee = fmt.Errorf("BaseFee should not be set before block %d", params.EIP1559ForkBlockNumber) |
| 34 | +) |
| 35 | + |
| 36 | +// VerifyEIP1559BaseFee verifies that the EIP1559 BaseFee field is valid for the current block height |
| 37 | +func VerifyEIP1559BaseFee(config *params.ChainConfig, header, parent *types.Header) error { |
| 38 | + if config.IsEIP1559(parent.Number) { |
| 39 | + if parent.BaseFee == nil { |
| 40 | + return errMissingParentBaseFee |
| 41 | + } |
| 42 | + } |
| 43 | + expectedBaseFee := CalcBaseFee(config, parent) |
| 44 | + if header.BaseFee == nil { |
| 45 | + if expectedBaseFee != nil { |
| 46 | + return errMissingBaseFee |
| 47 | + } |
| 48 | + return nil |
| 49 | + } |
| 50 | + if expectedBaseFee == nil { |
| 51 | + if header.BaseFee != nil { |
| 52 | + return errHaveBaseFee |
| 53 | + } |
| 54 | + return nil |
| 55 | + } |
| 56 | + if header.BaseFee.Cmp(expectedBaseFee) != 0 { |
| 57 | + return errInvalidBaseFee |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// CalcBaseFee returns the baseFee for the current block provided the parent header and config parameters |
| 63 | +func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { |
| 64 | + height := new(big.Int).Add(parent.Number, common.Big1) |
| 65 | + // If we are before EIP1559 activation, the baseFee is nil |
| 66 | + if !config.IsEIP1559(height) { |
| 67 | + return nil |
| 68 | + } |
| 69 | + // If we are at the block of EIP1559 activation then the BaseFee is set to the initial value |
| 70 | + if config.EIP1559Block.Cmp(height) == 0 { |
| 71 | + return new(big.Int).SetUint64(config.EIP1559.InitialBaseFee) |
| 72 | + } |
| 73 | + |
| 74 | + // Otherwise, |
| 75 | + // BASEFEE = PARENT_BASEFEE + PARENT_BASEFEE * delta // EIP1559_GAS_TARGET // BASEFEE_MAX_CHANGE_DENOMINATOR |
| 76 | + // Where delta = block.gas_used - EIP1559_GAS_TARGET |
| 77 | + parentGasTarget := CalcEIP1559GasTarget(config, parent.Number, new(big.Int).SetUint64(parent.GasLimit)) |
| 78 | + delta := new(big.Int).Sub(new(big.Int).SetUint64(parent.GasUsed), parentGasTarget) |
| 79 | + mul := new(big.Int).Mul(parent.BaseFee, delta) |
| 80 | + div := new(big.Int).Div(mul, parentGasTarget) |
| 81 | + div2 := new(big.Int).Div(div, new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator)) |
| 82 | + |
| 83 | + baseFee := new(big.Int).Add(parent.BaseFee, div2) |
| 84 | + |
| 85 | + // A valid BASEFEE is one such that abs(BASEFEE - PARENT_BASEFEE) <= max(1, PARENT_BASEFEE // BASEFEE_MAX_CHANGE_DENOMINATOR) |
| 86 | + diff := new(big.Int).Sub(baseFee, parent.BaseFee) |
| 87 | + neg := false |
| 88 | + if diff.Sign() < 0 { |
| 89 | + neg = true |
| 90 | + diff.Neg(diff) |
| 91 | + } |
| 92 | + max := new(big.Int).Div(parent.BaseFee, new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator)) |
| 93 | + if max.Cmp(common.Big1) < 0 { |
| 94 | + max = common.Big1 |
| 95 | + } |
| 96 | + // If derived BaseFee is not valid, restrict it within the bounds |
| 97 | + if diff.Cmp(max) > 0 { |
| 98 | + if neg { |
| 99 | + max.Neg(max) |
| 100 | + } |
| 101 | + baseFee.Set(new(big.Int).Add(parent.BaseFee, max)) |
| 102 | + } |
| 103 | + return baseFee |
| 104 | +} |
| 105 | + |
| 106 | +// CalcEIP1559GasTarget returns the EIP1559GasTarget at the current height and header.GasLimit |
| 107 | +// This should only be called at or above the block height of EIP1559 activation and below finalization |
| 108 | +func CalcEIP1559GasTarget(chainConfig *params.ChainConfig, height, gasLimit *big.Int) *big.Int { |
| 109 | + // After EIP1559 finalization the entire header.GasLimit field instead represents the EIP1559GasTarget |
| 110 | + if chainConfig.IsEIP1559Finalized(height) { |
| 111 | + return gasLimit |
| 112 | + } else if chainConfig.IsEIP1559(height) { |
| 113 | + // During transition, |
| 114 | + // EIP1559GasTarget = (header.GasLimit/2) + (header.GasLimit/2) * (blockNumber-initBlockNumber) / migrationBlockDuration |
| 115 | + // migrationBlockDuration cannot be 0 or IsEIP1559Finalized would be true |
| 116 | + halfLim := new(big.Int).Div(gasLimit, big.NewInt(2)) |
| 117 | + blockDiff := new(big.Int).Sub(height, chainConfig.EIP1559Block) |
| 118 | + migrationBlockDuration := new(big.Int).SetUint64(chainConfig.EIP1559.MigrationBlockDuration) |
| 119 | + return new(big.Int).Add(halfLim, new(big.Int).Div(new(big.Int).Mul(halfLim, blockDiff), migrationBlockDuration)) |
| 120 | + } |
| 121 | + // Before EIP1559 activation the target is 0 |
| 122 | + return big.NewInt(0) |
| 123 | +} |
0 commit comments