diff --git a/src/pytest_plugins/filler/pre_alloc.py b/src/pytest_plugins/filler/pre_alloc.py index 49a9a4594bc..0de14dc52c8 100644 --- a/src/pytest_plugins/filler/pre_alloc.py +++ b/src/pytest_plugins/filler/pre_alloc.py @@ -96,6 +96,7 @@ class Alloc(BaseAlloc): _contract_address_iterator: Iterator[Address] = PrivateAttr() _eoa_iterator: Iterator[EOA] = PrivateAttr() _evm_code_type: EVMCodeType | None = PrivateAttr(None) + _fork: Fork = PrivateAttr() def __init__( self, @@ -103,6 +104,7 @@ def __init__( alloc_mode: AllocMode, contract_address_iterator: Iterator[Address], eoa_iterator: Iterator[EOA], + fork: Fork, evm_code_type: EVMCodeType | None = None, **kwargs, ): @@ -112,6 +114,7 @@ def __init__( self._contract_address_iterator = contract_address_iterator self._eoa_iterator = eoa_iterator self._evm_code_type = evm_code_type + self._fork = fork def __setitem__(self, address: Address | FixedSizeBytesConvertible, account: Account | None): """Set account associated with an address.""" @@ -162,12 +165,19 @@ def deploy_contract( if self._alloc_mode == AllocMode.STRICT: assert Number(nonce) >= 1, "impossible to deploy contract with nonce lower than one" + code = self.code_pre_processor(code, evm_code_type=evm_code_type) + code_bytes = bytes(code) if not isinstance(code, (bytes, str)) else code + max_code_size = self._fork.max_code_size() + assert len(code_bytes) <= max_code_size, ( + f"code too large: {len(code_bytes)} > {max_code_size}" + ) + super().__setitem__( contract_address, Account( nonce=nonce, balance=balance, - code=self.code_pre_processor(code, evm_code_type=evm_code_type), + code=code, storage=storage, ), ) @@ -416,11 +426,13 @@ def pre( contract_address_iterator: Iterator[Address], eoa_iterator: Iterator[EOA], evm_code_type: EVMCodeType, + fork: Fork, ) -> Alloc: """Return default pre allocation for all tests (Empty alloc).""" return Alloc( alloc_mode=alloc_mode, contract_address_iterator=contract_address_iterator, eoa_iterator=eoa_iterator, + fork=fork, evm_code_type=evm_code_type, ) diff --git a/src/pytest_plugins/filler/tests/test_pre_alloc.py b/src/pytest_plugins/filler/tests/test_pre_alloc.py index e7c09a64255..ba7b9290d4f 100644 --- a/src/pytest_plugins/filler/tests/test_pre_alloc.py +++ b/src/pytest_plugins/filler/tests/test_pre_alloc.py @@ -5,6 +5,7 @@ import pytest from ethereum_test_base_types import Address, TestPrivateKey, TestPrivateKey2 +from ethereum_test_forks import Fork, Prague from ethereum_test_types import EOA from ethereum_test_vm import EVMCodeType from ethereum_test_vm import Opcodes as Op @@ -18,7 +19,9 @@ def create_test_alloc( - alloc_mode: AllocMode = AllocMode.PERMISSIVE, evm_code_type: EVMCodeType = EVMCodeType.LEGACY + alloc_mode: AllocMode = AllocMode.PERMISSIVE, + fork: Fork = Prague, + evm_code_type: EVMCodeType = EVMCodeType.LEGACY, ) -> Alloc: """Create a test Alloc instance with default iterators.""" contract_iter = iter( @@ -33,6 +36,7 @@ def create_test_alloc( alloc_mode=alloc_mode, contract_address_iterator=contract_iter, eoa_iterator=eoa_iter, + fork=fork, evm_code_type=evm_code_type, ) diff --git a/tests/benchmark/test_worst_compute.py b/tests/benchmark/test_worst_compute.py index 90ce819a7eb..e685a727ea6 100644 --- a/tests/benchmark/test_worst_compute.py +++ b/tests/benchmark/test_worst_compute.py @@ -2872,13 +2872,20 @@ def test_worst_push( ): """Test running a block with as many PUSH as possible.""" op = opcode[1] if opcode.has_data_portion() else opcode - opcode_sequence = op * fork.max_stack_height() - target_contract_address = pre.deploy_contract(code=opcode_sequence) + + op_seq = Bytecode() + for _ in range(fork.max_stack_height()): + if len(op_seq) + len(op) > fork.max_code_size(): + break + op_seq += op + + target_contract_address = pre.deploy_contract(code=op_seq) calldata = Bytecode() attack_block = Op.POP(Op.STATICCALL(Op.GAS, target_contract_address, 0, 0, 0, 0)) code = code_loop_precompile_call(calldata, attack_block, fork) + code_address = pre.deploy_contract(code=code) tx = Transaction(