2021-06-13 13:01:11 +00:00
|
|
|
// Copyright 2021 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2021-04-22 17:11:37 +00:00
|
|
|
package misc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/math"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2021-04-22 17:11:37 +00:00
|
|
|
)
|
|
|
|
|
2021-06-13 13:01:11 +00:00
|
|
|
// VerifyEip1559Header verifies some header attributes which were changed in EIP-1559,
|
|
|
|
// - gas limit check
|
|
|
|
// - basefee check
|
2021-05-11 09:54:20 +00:00
|
|
|
func VerifyEip1559Header(config *params.ChainConfig, parent, header *types.Header) error {
|
2021-06-13 13:01:11 +00:00
|
|
|
// Verify that the gas limit remains within allowed bounds
|
|
|
|
parentGasLimit := parent.GasLimit
|
|
|
|
if !config.IsLondon(parent.Number.Uint64()) {
|
|
|
|
parentGasLimit = parent.GasLimit * params.ElasticityMultiplier
|
|
|
|
}
|
|
|
|
if err := VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-11 09:54:20 +00:00
|
|
|
// Verify the header is not malformed
|
|
|
|
if header.BaseFee == nil {
|
2021-06-13 13:01:11 +00:00
|
|
|
return fmt.Errorf("header is missing baseFee")
|
2021-04-22 17:11:37 +00:00
|
|
|
}
|
|
|
|
// Verify the baseFee is correct based on the parent header.
|
2021-05-11 09:54:20 +00:00
|
|
|
expectedBaseFee := CalcBaseFee(config, parent)
|
2021-04-22 17:11:37 +00:00
|
|
|
if header.BaseFee.Cmp(expectedBaseFee) != 0 {
|
2021-05-11 09:54:20 +00:00
|
|
|
return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d",
|
|
|
|
expectedBaseFee, header.BaseFee, parent.BaseFee, parent.GasUsed)
|
|
|
|
}
|
2021-04-22 17:11:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-13 13:01:11 +00:00
|
|
|
// CalcBaseFee calculates the basefee of the header.
|
2021-05-11 09:54:20 +00:00
|
|
|
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
|
|
|
|
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
|
|
|
|
if !config.IsLondon(parent.Number.Uint64()) {
|
|
|
|
return new(big.Int).SetUint64(params.InitialBaseFee)
|
2021-04-22 17:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-05-11 09:54:20 +00:00
|
|
|
parentGasTarget = parent.GasLimit / params.ElasticityMultiplier
|
|
|
|
parentGasTargetBig = new(big.Int).SetUint64(parentGasTarget)
|
2021-04-22 17:11:37 +00:00
|
|
|
baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator)
|
|
|
|
)
|
2021-05-11 09:54:20 +00:00
|
|
|
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
|
|
|
|
if parent.GasUsed == parentGasTarget {
|
|
|
|
return new(big.Int).Set(parent.BaseFee)
|
2021-04-22 17:11:37 +00:00
|
|
|
}
|
2021-05-11 09:54:20 +00:00
|
|
|
if parent.GasUsed > parentGasTarget {
|
2021-04-22 17:11:37 +00:00
|
|
|
// If the parent block used more gas than its target, the baseFee should increase.
|
2021-05-11 09:54:20 +00:00
|
|
|
gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parentGasTarget)
|
2021-04-22 17:11:37 +00:00
|
|
|
x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
|
2021-05-11 09:54:20 +00:00
|
|
|
y := x.Div(x, parentGasTargetBig)
|
2021-04-22 17:11:37 +00:00
|
|
|
baseFeeDelta := math.BigMax(
|
2021-05-11 09:54:20 +00:00
|
|
|
x.Div(y, baseFeeChangeDenominator),
|
|
|
|
common.Big1,
|
2021-04-22 17:11:37 +00:00
|
|
|
)
|
|
|
|
|
2021-05-11 09:54:20 +00:00
|
|
|
return x.Add(parent.BaseFee, baseFeeDelta)
|
2021-04-22 17:11:37 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
|
2021-05-11 09:54:20 +00:00
|
|
|
gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parent.GasUsed)
|
|
|
|
x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta)
|
|
|
|
y := x.Div(x, parentGasTargetBig)
|
|
|
|
baseFeeDelta := x.Div(y, baseFeeChangeDenominator)
|
2021-04-22 17:11:37 +00:00
|
|
|
|
|
|
|
return math.BigMax(
|
2021-05-11 09:54:20 +00:00
|
|
|
x.Sub(parent.BaseFee, baseFeeDelta),
|
|
|
|
common.Big0,
|
2021-04-22 17:11:37 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|