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@21fb11c8.

Generate fixtures for these test cases for Prague with:

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

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
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
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
@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,
):
    """
    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

    salt = 0
    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, salt=salt, 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=salt,
        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