Files
zeekay 895a6c1859 refactor: remove all SubnetEVM references
- Rename SubnetEVMTimestamp to EVMTimestamp (JSON: evmTimestamp)
- Rename IsSubnetEVM method to IsEVM
- Rename subnet_scan.go to evm_scan.go
- Rename subnet_compat.go to evm_compat.go
- Rename ScanSubnetEVMHeaders to ScanEVMHeaders
- Rename BuildCanonicalMappingsFromSubnetEVM to BuildCanonicalMappingsFromEVM
- Update all comments referencing SubnetEVM to EVM

Breaking change: This removes all SubnetEVM naming - only "EVM" is supported.
2025-12-21 10:26:59 -08:00

120 lines
4.4 KiB
Go

// 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/>.
package eip1559
import (
"errors"
"fmt"
"math/big"
"github.com/luxfi/geth/common"
"github.com/luxfi/geth/consensus/misc"
"github.com/luxfi/geth/core/types"
"github.com/luxfi/geth/params"
)
// VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559,
// - gas limit check
// - basefee check
func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Header) error {
// Verify that the gas limit remains within allowed bounds
parentGasLimit := parent.GasLimit
if !config.IsLondon(parent.Number) {
parentGasLimit = parent.GasLimit * config.ElasticityMultiplier()
}
if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
return err
}
// Verify the header is not malformed
if header.BaseFee == nil {
return errors.New("header is missing baseFee")
}
// EVM/C-Chain uses a completely different fee mechanism:
// - Custom baseFeeChangeDenominator (36 vs standard 8)
// - minBaseFee floor (25 gwei) that prevents baseFee from going below
// - Different gas target calculations
// When EVM is active, trust the baseFee from the header
// as it was calculated by the EVM consensus
if config.IsEVM(header.Time) {
// For EVM chains, only verify baseFee is present and >= minBaseFee
minBaseFee := new(big.Int).SetUint64(25000000000) // 25 gwei
if header.BaseFee.Cmp(minBaseFee) < 0 {
return fmt.Errorf("invalid baseFee: have %s, want at least minBaseFee %s",
header.BaseFee, minBaseFee)
}
return nil
}
// Verify the baseFee is correct based on the parent header.
expectedBaseFee := CalcBaseFee(config, parent)
if header.BaseFee.Cmp(expectedBaseFee) != 0 {
// EVM/C-Chain uses minBaseFee enforcement which may keep baseFee higher
// than standard Ethereum calculations. Allow the actual fee to be >= expected.
if header.BaseFee.Cmp(expectedBaseFee) < 0 {
return fmt.Errorf("invalid baseFee: have %s, want at least %s, parentBaseFee %s, parentGasUsed %d",
header.BaseFee, expectedBaseFee, parent.BaseFee, parent.GasUsed)
}
}
return nil
}
// CalcBaseFee calculates the basefee of the header.
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) {
return new(big.Int).SetUint64(params.InitialBaseFee)
}
parentGasTarget := parent.GasLimit / config.ElasticityMultiplier()
// 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)
}
var (
num = new(big.Int)
denom = new(big.Int)
)
if parent.GasUsed > parentGasTarget {
// If the parent block used more gas than its target, the baseFee should increase.
// max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
num.SetUint64(parent.GasUsed - parentGasTarget)
num.Mul(num, parent.BaseFee)
num.Div(num, denom.SetUint64(parentGasTarget))
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
if num.Cmp(common.Big1) < 0 {
return num.Add(parent.BaseFee, common.Big1)
}
return num.Add(parent.BaseFee, num)
} else {
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
// max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
num.SetUint64(parentGasTarget - parent.GasUsed)
num.Mul(num, parent.BaseFee)
num.Div(num, denom.SetUint64(parentGasTarget))
num.Div(num, denom.SetUint64(config.BaseFeeChangeDenominator()))
baseFee := num.Sub(parent.BaseFee, num)
if baseFee.Cmp(common.Big0) < 0 {
baseFee = common.Big0
}
return baseFee
}
}