Skip to content

test_factory_different_salts_produce_different_addresses()

Documentation for tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py::test_factory_different_salts_produce_different_addresses@87aba1a3.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py::test_factory_different_salts_produce_different_addresses --fork Amsterdam

Two calls to the factory with the same initcode but different salts must deploy at distinct, salt-derived addresses, proving the salt is actually plumbed through to CREATE2.

Source code in tests/amsterdam/eip7997_deterministic_factory_predeploy/test_factory.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def test_factory_different_salts_produce_different_addresses(
    state_test: StateTestFiller,
    pre: Alloc,
) -> None:
    """
    Two calls to the factory with the same initcode but different salts
    must deploy at distinct, salt-derived addresses, proving the salt is
    actually plumbed through to `CREATE2`.
    """
    salt_a = 0x11
    salt_b = 0x22
    runtime_code = Op.PUSH1(0x01) + Op.PUSH1(0x00) + Op.RETURN
    initcode = Initcode(deploy_code=runtime_code)
    addr_a = compute_create2_address(FACTORY, salt_a, initcode)
    addr_b = compute_create2_address(FACTORY, salt_b, initcode)
    assert addr_a != addr_b

    initcode_offset = 32
    args_size = initcode_offset + len(bytes(initcode))

    storage = Storage()
    salt_a_call_slot = storage.store_next(1, "salt_a_call_success")
    salt_a_addr_slot = storage.store_next(addr_a, "salt_a_address")
    salt_b_call_slot = storage.store_next(1, "salt_b_call_success")
    salt_b_addr_slot = storage.store_next(addr_b, "salt_b_address")

    caller = pre.deploy_contract(
        Op.CALLDATACOPY(initcode_offset, 0, Op.CALLDATASIZE)
        + Op.MSTORE(0, salt_a)
        + Op.SSTORE(
            salt_a_call_slot,
            Op.CALL(
                gas=Op.GAS,
                address=FACTORY,
                value=0,
                args_offset=0,
                args_size=args_size,
                ret_offset=0x20C,
                ret_size=20,
            ),
        )
        + Op.SSTORE(salt_a_addr_slot, Op.MLOAD(0x200))
        + Op.MSTORE(0, salt_b)
        + Op.SSTORE(
            salt_b_call_slot,
            Op.CALL(
                gas=Op.GAS,
                address=FACTORY,
                value=0,
                args_offset=0,
                args_size=args_size,
                ret_offset=0x20C,
                ret_size=20,
            ),
        )
        + Op.SSTORE(salt_b_addr_slot, Op.MLOAD(0x200))
        + Op.STOP,
    )

    state_test(
        pre=pre,
        tx=Transaction(
            sender=pre.fund_eoa(),
            to=caller,
            data=bytes(initcode),
        ),
        post={
            caller: Account(storage=storage),
            addr_a: Account(nonce=1, code=bytes(runtime_code)),
            addr_b: Account(nonce=1, code=bytes(runtime_code)),
        },
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.