core/vm, params: raise codesize limit and initcode limit

This commit is contained in:
Felix Lange 2025-06-10 14:58:34 +02:00
parent 0983cd789e
commit b2e78c4861
8 changed files with 41 additions and 13 deletions

View file

@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/tests" "github.com/ethereum/go-ethereum/tests"
@ -180,8 +181,10 @@ func Transaction(ctx *cli.Context) error {
r.Error = errors.New("gas * maxFeePerGas exceeds 256 bits") r.Error = errors.New("gas * maxFeePerGas exceeds 256 bits")
} }
// Check whether the init code size has been exceeded. // Check whether the init code size has been exceeded.
if chainConfig.IsShanghai(new(big.Int), 0) && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize { if tx.To() == nil {
r.Error = errors.New("max initcode size exceeded") if err := vm.CheckInitCodeSize(&rules, uint64(len(tx.Data()))); err != nil {
r.Error = err
}
} }
results = append(results, r) results = append(results, r)
} }

View file

@ -20,6 +20,7 @@ import (
"errors" "errors"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
) )
var ( var (
@ -60,7 +61,7 @@ var (
// ErrMaxInitCodeSizeExceeded is returned if creation transaction provides the init code bigger // ErrMaxInitCodeSizeExceeded is returned if creation transaction provides the init code bigger
// than init code size limit. // than init code size limit.
ErrMaxInitCodeSizeExceeded = errors.New("max initcode size exceeded") ErrMaxInitCodeSizeExceeded = vm.ErrMaxInitCodeSizeExceeded
// ErrInsufficientBalanceWitness is returned if the transaction sender has enough // ErrInsufficientBalanceWitness is returned if the transaction sender has enough
// funds to cover the transfer, but not enough to pay for witness access/modification // funds to cover the transfer, but not enough to pay for witness access/modification

View file

@ -471,8 +471,10 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
} }
// Check whether the init code size has been exceeded. // Check whether the init code size has been exceeded.
if rules.IsShanghai && contractCreation && len(msg.Data) > params.MaxInitCodeSize { if contractCreation {
return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), params.MaxInitCodeSize) if err := vm.CheckInitCodeSize(&rules, uint64(len(msg.Data))); err != nil {
return nil, err
}
} }
// Execute the preparatory steps for state transition which includes: // Execute the preparatory steps for state transition which includes:

View file

@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto/kzg4844" "github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
@ -87,8 +88,10 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
return fmt.Errorf("%w: type %d rejected, pool not yet in Prague", core.ErrTxTypeNotSupported, tx.Type()) return fmt.Errorf("%w: type %d rejected, pool not yet in Prague", core.ErrTxTypeNotSupported, tx.Type())
} }
// Check whether the init code size has been exceeded // Check whether the init code size has been exceeded
if rules.IsShanghai && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize { if tx.To() == nil {
return fmt.Errorf("%w: code size %v, limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize) if err := vm.CheckInitCodeSize(&rules, uint64(len(tx.Data()))); err != nil {
return err
}
} }
// Transactions can't be negative. This may never happen using RLP decoded // Transactions can't be negative. This may never happen using RLP decoded
// transactions but may occur for transactions created using the RPC. // transactions but may occur for transactions created using the RPC.

View file

@ -17,12 +17,26 @@
package vm package vm
import ( import (
"fmt"
"math" "math"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
// CheckInitCodeSize checks the size of contract initcode against the protocol-defined limit.
func CheckInitCodeSize(rules *params.Rules, size uint64) error {
switch {
case rules.IsOsaka && size > params.MaxInitCodeSizeOsaka:
return fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, size, params.MaxInitCodeSizeOsaka)
case rules.IsShanghai && size > params.MaxInitCodeSize:
return fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, size, params.MaxInitCodeSize)
default:
return nil
}
}
// calcMemSize64 calculates the required memory size, and returns // calcMemSize64 calculates the required memory size, and returns
// the size and whether the result overflowed uint64 // the size and whether the result overflowed uint64
func calcMemSize64(off, l *uint256.Int) (uint64, bool) { func calcMemSize64(off, l *uint256.Int) (uint64, bool) {

View file

@ -526,7 +526,9 @@ func (evm *EVM) initNewContract(contract *Contract, address common.Address) ([]b
} }
// Check whether the max code size has been exceeded, assign err if the case. // Check whether the max code size has been exceeded, assign err if the case.
if evm.chainRules.IsEIP158 && len(ret) > params.MaxCodeSize { if evm.chainRules.IsOsaka && len(ret) > params.MaxCodeSizeOsaka {
return ret, ErrMaxCodeSizeExceeded
} else if evm.chainRules.IsEIP158 && len(ret) > params.MaxCodeSize {
return ret, ErrMaxCodeSizeExceeded return ret, ErrMaxCodeSizeExceeded
} }

View file

@ -18,7 +18,6 @@ package vm
import ( import (
"errors" "errors"
"fmt"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
@ -314,8 +313,8 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m
if overflow { if overflow {
return 0, ErrGasUintOverflow return 0, ErrGasUintOverflow
} }
if size > params.MaxInitCodeSize { if err := CheckInitCodeSize(&evm.chainRules, size); err != nil {
return 0, fmt.Errorf("%w: size %d", ErrMaxInitCodeSizeExceeded, size) return 0, err
} }
// Since size <= params.MaxInitCodeSize, these multiplication cannot overflow // Since size <= params.MaxInitCodeSize, these multiplication cannot overflow
moreGas := params.InitCodeWordGas * ((size + 31) / 32) moreGas := params.InitCodeWordGas * ((size + 31) / 32)
@ -324,6 +323,7 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m
} }
return gas, nil return gas, nil
} }
func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
gas, err := memoryGasCost(mem, memorySize) gas, err := memoryGasCost(mem, memorySize)
if err != nil { if err != nil {
@ -333,8 +333,8 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory,
if overflow { if overflow {
return 0, ErrGasUintOverflow return 0, ErrGasUintOverflow
} }
if size > params.MaxInitCodeSize { if err := CheckInitCodeSize(&evm.chainRules, size); err != nil {
return 0, fmt.Errorf("%w: size %d", ErrMaxInitCodeSizeExceeded, size) return 0, err
} }
// Since size <= params.MaxInitCodeSize, these multiplication cannot overflow // Since size <= params.MaxInitCodeSize, these multiplication cannot overflow
moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32) moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32)

View file

@ -135,6 +135,9 @@ const (
MaxCodeSize = 24576 // Maximum bytecode to permit for a contract MaxCodeSize = 24576 // Maximum bytecode to permit for a contract
MaxInitCodeSize = 2 * MaxCodeSize // Maximum initcode to permit in a creation transaction and create instructions MaxInitCodeSize = 2 * MaxCodeSize // Maximum initcode to permit in a creation transaction and create instructions
MaxCodeSizeOsaka = 0xC000 // Maximum bytecode to permit for a contract
MaxInitCodeSizeOsaka = 0x12000 // Maximum initcode to permit in a creation transaction and create instructions
// Precompiled contract gas prices // Precompiled contract gas prices
EcrecoverGas uint64 = 3000 // Elliptic curve sender recovery gas price EcrecoverGas uint64 = 3000 // Elliptic curve sender recovery gas price