Skip to content

test_max_initcode_size_via_create_fork_transition()

Documentation for tests/amsterdam/eip7954_increase_max_contract_size/test_fork_transition.py::test_max_initcode_size_via_create_fork_transition@ffe48572.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7954_increase_max_contract_size/test_fork_transition.py::test_max_initcode_size_via_create_fork_transition --fork Amsterdam

Ensure a create opcode is held to the initcode size limit active in its own block.

Source code in tests/amsterdam/eip7954_increase_max_contract_size/test_fork_transition.py
@pytest.mark.parametrize("create_opcode", [Op.CREATE, Op.CREATE2])
@pytest.mark.parametrize(
    "initcode_size",
    [
        pytest.param(
            lambda f: f.transitions_from().max_initcode_size(),
            id="at_parent_max",
        ),
        pytest.param(
            lambda f: f.transitions_from().max_initcode_size() + 1,
            id="over_parent_max",
        ),
        pytest.param(
            lambda f: f.transitions_to().max_initcode_size(),
            id="at_max",
        ),
        pytest.param(
            lambda f: f.transitions_to().max_initcode_size() + 1,
            id="over_max",
        ),
    ],
)
def test_max_initcode_size_via_create_fork_transition(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: TransitionFork,
    initcode_size: Callable[[TransitionFork], int],
    create_opcode: Op,
) -> None:
    """
    Ensure a create opcode is held to the initcode size limit active in its
    own block.
    """
    size = initcode_size(fork)
    # The initcode returns no code, so only its leading bytes are written
    # and the rest of the size comes from zeroed memory.
    initcode_prologue = bytes(Op.RETURN(offset=0, size=0))
    initcode_bytes = initcode_prologue.ljust(size, b"\x00")

    create_call = create_op(create_opcode, size=size)
    factory_code = (
        Om.MSTORE(initcode_prologue, 0) + Op.SSTORE(0, create_call) + Op.STOP
    )

    blocks = []
    post: dict[Any, Account | None] = {}
    for timestamp in (PRE_FORK_TIMESTAMP, POST_FORK_TIMESTAMP):
        sender = pre.fund_eoa()
        factory = pre.deploy_contract(factory_code, storage={0: SENTINEL})
        block_fork = fork.fork_at(timestamp=timestamp)
        created = size <= block_fork.max_initcode_size()
        # The abort is an exceptional halt of the factory, so the whole
        # execution allowance burns and the block records exactly the cap.
        gas_limit_cap = block_fork.transaction_gas_limit_cap()
        assert gas_limit_cap is not None
        blocks.append(
            Block(
                timestamp=timestamp,
                txs=[Transaction(sender=sender, to=factory)],
                header_verify=(
                    None if created else Header(gas_used=gas_limit_cap)
                ),
            )
        )
        create_address = compute_create_address(
            address=factory,
            nonce=1,
            initcode=initcode_bytes,
            opcode=create_opcode,
        )
        # An oversized initcode aborts the create opcode before any child
        # frame runs, taking the factory frame down with it, so the sentinel
        # survives.
        post[factory] = Account(
            storage={0: create_address if created else SENTINEL}
        )
        post[create_address] = (
            Account(code=b"") if created else Account.NONEXISTENT
        )

    blockchain_test(pre=pre, blocks=blocks, post=post)

Parametrized Test Cases

This test generates 8 parametrized test cases across 1 fork.