Skip to content

test_extcodehash_changed_account()

Documentation for tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_extcodehash_changed_account@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_extcodehash_changed_account --fork Amsterdam

Test EXTCODEHASH/EXTCODESIZE before and after changing account state.

A secondary contract has its nonce incremented (via CREATE), balance increased (via CALL value), and storage modified (via SSTORE) when called. The caller verifies that EXTCODEHASH and EXTCODESIZE return the same values before and after these mutations.

Source code in tests/constantinople/eip1052_extcodehash/test_extcodehash.py
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/extCodeHashChangedAccountFiller.json",  # noqa: E501
    ],
    pr=["https://github.com/ethereum/execution-specs/pull/2394"],
)
def test_extcodehash_changed_account(
    state_test: StateTestFiller,
    pre: Alloc,
) -> None:
    """
    Test EXTCODEHASH/EXTCODESIZE before and after changing account state.

    A secondary contract has its nonce incremented (via CREATE), balance
    increased (via CALL value), and storage modified (via SSTORE) when
    called. The caller verifies that EXTCODEHASH and EXTCODESIZE return
    the same values before and after these mutations.
    """
    storage = Storage()

    # CREATE bumps nonce, receives value, sets storage.
    secondary_code = Op.CREATE(value=0, offset=0, size=0) + Op.SSTORE(
        0, 0x1234
    )
    secondary = pre.deploy_contract(secondary_code)

    expected_hash = keccak256(bytes(secondary_code))
    expected_size = len(secondary_code)

    def extcode_checks() -> Bytecode:
        return Op.SSTORE(
            storage.store_next(expected_hash),
            Op.EXTCODEHASH(secondary),
        ) + Op.SSTORE(
            storage.store_next(expected_size),
            Op.EXTCODESIZE(secondary),
        )

    code = (
        extcode_checks()
        + Op.CALL(gas=Op.GAS, address=secondary, value=1)
        + Op.POP
        + extcode_checks()
    )

    code_address = pre.deploy_contract(
        code, balance=1, storage=storage.canary()
    )

    tx = Transaction(sender=pre.fund_eoa(), to=code_address)

    state_test(
        pre=pre,
        post={
            code_address: Account(storage=storage),
            secondary: Account(
                nonce=2,
                balance=1,
                storage={0: 0x1234},
            ),
        },
        tx=tx,
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 10 forks.