Skip to content

test_set_code_to_account_deployed_in_same_tx()

Documentation for tests/prague/eip7702_set_code_tx/test_set_code_txs.py::test_set_code_to_account_deployed_in_same_tx@e9958ed2.

Generate fixtures for these test cases for Osaka with:

fill -v tests/prague/eip7702_set_code_tx/test_set_code_txs.py::test_set_code_to_account_deployed_in_same_tx --fork Osaka

Test setting the code of an account to an address that is deployed in the same transaction, and test calling the set-code address and the deployed contract.

Source code in tests/prague/eip7702_set_code_tx/test_set_code_txs.py
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
@pytest.mark.with_all_create_opcodes
def test_set_code_to_account_deployed_in_same_tx(
    state_test: StateTestFiller,
    pre: Alloc,
    create_opcode: Op,
    evm_code_type: EVMCodeType,
) -> None:
    """
    Test setting the code of an account to an address that is deployed in the
    same transaction, and test calling the set-code address and the deployed
    contract.
    """
    auth_signer = pre.fund_eoa(auth_account_start_balance)

    success_slot = 1

    deployed_code: Bytecode | Container = Op.SSTORE(success_slot, 1) + Op.STOP
    initcode: Bytecode | Container

    if evm_code_type == EVMCodeType.LEGACY:
        initcode = Initcode(deploy_code=deployed_code)
    elif evm_code_type == EVMCodeType.EOF_V1:
        deployed_code = Container.Code(deployed_code)
        initcode = Container.Init(deploy_container=deployed_code)
    else:
        raise ValueError(f"Unsupported EVM code type: {evm_code_type}")

    deployed_contract_address_slot = 1
    signer_call_return_code_slot = 2
    deployed_contract_call_return_code_slot = 3

    call_opcode = Op.CALL if evm_code_type == EVMCodeType.LEGACY else Op.EXTCALL

    if create_opcode == Op.EOFCREATE:
        create_opcode = Op.EOFCREATE[0]  # type: ignore

    contract_creator_code: Bytecode | Container = (
        Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)  # NOOP on EOF
        + Op.SSTORE(
            deployed_contract_address_slot,
            create_opcode(offset=0, size=Op.CALLDATASIZE),
        )
        + Op.SSTORE(signer_call_return_code_slot, call_opcode(address=auth_signer))
        + Op.SSTORE(
            deployed_contract_call_return_code_slot,
            call_opcode(address=Op.SLOAD(deployed_contract_address_slot)),
        )
        + Op.STOP()
    )

    if evm_code_type == EVMCodeType.EOF_V1:
        contract_creator_code = Container(
            sections=[
                Section.Code(contract_creator_code),
                Section.Container(container=initcode),
            ],
        )

    contract_creator_address = pre.deploy_contract(contract_creator_code)

    deployed_contract_address = compute_create_address(
        address=contract_creator_address,
        nonce=1,
        salt=0,
        initcode=initcode,
        opcode=create_opcode,
    )

    tx = Transaction(
        gas_limit=10_000_000,
        to=contract_creator_address,
        value=0,
        data=initcode if evm_code_type == EVMCodeType.LEGACY else b"",
        authorization_list=[
            AuthorizationTuple(
                address=deployed_contract_address,
                nonce=0,
                signer=auth_signer,
            ),
        ],
        sender=pre.fund_eoa(),
    )

    state_test(
        env=Environment(),
        pre=pre,
        tx=tx,
        post={
            deployed_contract_address: Account(
                storage={success_slot: 1},
            ),
            auth_signer: Account(
                nonce=1,
                code=Spec.delegation_designation(deployed_contract_address),
                storage={success_slot: 1},
            ),
            contract_creator_address: Account(
                storage={
                    deployed_contract_address_slot: deployed_contract_address,
                    signer_call_return_code_slot: 1,
                    deployed_contract_call_return_code_slot: 1,
                }
            ),
        },
    )

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) create_opcode evm_code_type
...fork_Prague-create_opcode_CREATE2-evm_code_type_LEGACY-state_test CREATE2 LEGACY
...fork_Prague-create_opcode_CREATE2-evm_code_type_LEGACY-blockchain_test_from_state_test CREATE2 LEGACY
...fork_Prague-create_opcode_CREATE-evm_code_type_LEGACY-state_test CREATE LEGACY
...fork_Prague-create_opcode_CREATE-evm_code_type_LEGACY-blockchain_test_from_state_test CREATE LEGACY
...fork_Osaka-create_opcode_CREATE2-evm_code_type_LEGACY-state_test CREATE2 LEGACY
...fork_Osaka-create_opcode_CREATE2-evm_code_type_LEGACY-blockchain_test_from_state_test CREATE2 LEGACY
...fork_Osaka-create_opcode_CREATE-evm_code_type_LEGACY-state_test CREATE LEGACY
...fork_Osaka-create_opcode_CREATE-evm_code_type_LEGACY-blockchain_test_from_state_test CREATE LEGACY