diff --git a/cmd/evm/internal/t8ntool/transaction.go b/cmd/evm/internal/t8ntool/transaction.go index 2bc4f73b60..f0b1729e9e 100644 --- a/cmd/evm/internal/t8ntool/transaction.go +++ b/cmd/evm/internal/t8ntool/transaction.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core" "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/rlp" "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") } // Check whether the init code size has been exceeded. - if chainConfig.IsShanghai(new(big.Int), 0) && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize { - r.Error = errors.New("max initcode size exceeded") + if tx.To() == nil { + if err := vm.CheckInitCodeSize(&rules, uint64(len(tx.Data()))); err != nil { + r.Error = err + } } results = append(results, r) } diff --git a/core/error.go b/core/error.go index de95e64636..14448f7f1a 100644 --- a/core/error.go +++ b/core/error.go @@ -20,6 +20,7 @@ import ( "errors" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" ) var ( @@ -60,7 +61,7 @@ var ( // ErrMaxInitCodeSizeExceeded is returned if creation transaction provides the init code bigger // than init code size limit. - ErrMaxInitCodeSizeExceeded = errors.New("max initcode size exceeded") + ErrMaxInitCodeSizeExceeded = vm.ErrMaxInitCodeSizeExceeded // ErrInsufficientBalanceWitness is returned if the transaction sender has enough // funds to cover the transfer, but not enough to pay for witness access/modification diff --git a/core/state_transition.go b/core/state_transition.go index 4f172682d0..3d3ceb7cdb 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -471,8 +471,10 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { } // Check whether the init code size has been exceeded. - if rules.IsShanghai && contractCreation && len(msg.Data) > params.MaxInitCodeSize { - return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), params.MaxInitCodeSize) + if contractCreation { + if err := vm.CheckInitCodeSize(&rules, uint64(len(msg.Data))); err != nil { + return nil, err + } } // Execute the preparatory steps for state transition which includes: diff --git a/core/txpool/validation.go b/core/txpool/validation.go index fef1a99f7b..2f319e48a8 100644 --- a/core/txpool/validation.go +++ b/core/txpool/validation.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" "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/log" "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()) } // Check whether the init code size has been exceeded - if rules.IsShanghai && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize { - return fmt.Errorf("%w: code size %v, limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize) + if tx.To() == nil { + 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 but may occur for transactions created using the RPC. diff --git a/core/vm/common.go b/core/vm/common.go index 658803b820..cddeb7d94c 100644 --- a/core/vm/common.go +++ b/core/vm/common.go @@ -17,12 +17,26 @@ package vm import ( + "fmt" "math" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" "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 // the size and whether the result overflowed uint64 func calcMemSize64(off, l *uint256.Int) (uint64, bool) { diff --git a/core/vm/evm.go b/core/vm/evm.go index b45a434545..7e79e4b88b 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -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. - 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 } diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index f711aa4a18..d8ebf1e0f4 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -18,7 +18,6 @@ package vm import ( "errors" - "fmt" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" @@ -314,8 +313,8 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m if overflow { return 0, ErrGasUintOverflow } - if size > params.MaxInitCodeSize { - return 0, fmt.Errorf("%w: size %d", ErrMaxInitCodeSizeExceeded, size) + if err := CheckInitCodeSize(&evm.chainRules, size); err != nil { + return 0, err } // Since size <= params.MaxInitCodeSize, these multiplication cannot overflow moreGas := params.InitCodeWordGas * ((size + 31) / 32) @@ -324,6 +323,7 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m } return gas, nil } + func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { gas, err := memoryGasCost(mem, memorySize) if err != nil { @@ -333,8 +333,8 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, if overflow { return 0, ErrGasUintOverflow } - if size > params.MaxInitCodeSize { - return 0, fmt.Errorf("%w: size %d", ErrMaxInitCodeSizeExceeded, size) + if err := CheckInitCodeSize(&evm.chainRules, size); err != nil { + return 0, err } // Since size <= params.MaxInitCodeSize, these multiplication cannot overflow moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32) diff --git a/params/protocol_params.go b/params/protocol_params.go index 6b06dadaef..7f27dbc992 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -135,6 +135,9 @@ const ( MaxCodeSize = 24576 // Maximum bytecode to permit for a contract 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 EcrecoverGas uint64 = 3000 // Elliptic curve sender recovery gas price