Skip to content

test_init_collision_create_opcode()

Documentation for tests/paris/eip7610_create_collision/test_initcollision.py::test_init_collision_create_opcode@c17999c0.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/paris/eip7610_create_collision/test_initcollision.py::test_init_collision_create_opcode --fork Amsterdam

Test that a contract creation opcode exceptionally aborts when the target address has a non-empty storage, balance, nonce, or code.

Source code in tests/paris/eip7610_create_collision/test_initcollision.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
@pytest.mark.parametrize(
    "opcode",
    [
        Op.CREATE,
        pytest.param(
            Op.CREATE2, marks=pytest.mark.valid_from("Constantinople")
        ),
    ],
)
def test_init_collision_create_opcode(
    state_test: StateTestFiller,
    pre: Alloc,
    opcode: Op,
    collision_nonce: int,
    collision_balance: int,
    collision_code: bytes,
    initcode: Bytecode,
) -> None:
    """
    Test that a contract creation opcode exceptionally aborts when the target
    address has a non-empty storage, balance, nonce, or code.
    """
    assert len(initcode) <= 32
    contract_creator_code = (
        # Reverts if and only if contract creation fails. In Frontier/Homestead
        # this runs out of gas, and every other fork jumps to a non-JUMPDEST.
        Op.MSTORE(0, Op.PUSH32(bytes(initcode).ljust(32, b"\0")))
        + Op.JUMPI(
            condition=Op.ISZERO(opcode(value=0, offset=0, size=len(initcode))),
            pc=0,
        )
        + Op.STOP
    )
    contract_creator_address = pre.deploy_contract(contract_creator_code)

    gas_limiter_code = (
        # Calls the contract creator, reserving some gas to SSTORE the result.
        Op.SSTORE(
            0x01,
            Op.CALL(
                gas=Op.SUB(Op.GAS, 50_000),
                address=contract_creator_address,
                value=0,
                args_offset=0,
                args_size=0,
                ret_offset=0,
                ret_size=0,
            ),
        )
    )
    gas_limiter_address = pre.deploy_contract(
        gas_limiter_code,
        storage={0x01: 0x02},
    )

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

    tx = Transaction(
        sender=pre.fund_eoa(),
        to=gas_limiter_address,
        protected=False,
    )

    pre[created_contract_address] = Account(
        storage={0x01: 0x01},
        nonce=collision_nonce,
        balance=collision_balance,
        code=collision_code,
    )

    state_test(
        pre=pre,
        post={
            created_contract_address: Account(
                storage={0x01: 0x01},
            ),
            gas_limiter_address: Account(storage={0x01: 0x00}),
        },
        tx=tx,
    )

Parametrized Test Cases

This test generates 6 parametrized test cases across 16 forks.