@pytest.mark.with_all_create_opcodes
@pytest.mark.parametrize(
"modification",
["collision_only", "then_nonce_change", "then_storage_change"],
)
@pytest.mark.pre_alloc_mutable()
def test_bal_create_collision(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
fork: Fork,
create_opcode: Op,
modification: str,
) -> None:
"""
BAL with CREATE/CREATE2 collision against pre-existing contract X,
optionally followed by a tx that modifies X via call (closes #2914
nonce/storage axes). Balance axis is already covered by the suite's
existing collision-then-value-transfer tests. The `code_changes`
axis isn't reachable in forward order — see
`test_bal_create2_deploy_then_collision`.
"""
alice = pre.fund_eoa()
bob = pre.fund_eoa()
# Storage-touching init: a client that wrongly runs init on the
# collision leaks a slot-0 access into X's BAL.
init_code = Initcode(
deploy_code=Op.STOP,
initcode_prefix=Op.SSTORE(0, Op.ADD(Op.SLOAD(0), 1)),
)
init_code_bytes = bytes(init_code)
factory_code = (
Op.MSTORE(0, Op.PUSH32(init_code_bytes))
+ Op.SSTORE(
0x00,
create_opcode(
value=0,
offset=32 - len(init_code_bytes),
size=len(init_code_bytes),
),
)
+ Op.STOP
)
factory = pre.deploy_contract(
code=factory_code,
storage={0x00: 0xDEAD},
)
collision_address = compute_create_address(
address=factory,
nonce=1,
salt=0,
initcode=init_code_bytes,
opcode=create_opcode,
)
if modification == "collision_only":
x_code: Bytecode = Op.STOP
elif modification == "then_nonce_change":
inner_init = Initcode(deploy_code=Op.STOP)
x_code = (
Op.MSTORE(0, Op.PUSH32(bytes(inner_init)))
+ Op.CREATE(0, 32 - len(inner_init), len(inner_init))
+ Op.STOP
)
elif modification == "then_storage_change":
x_code = Op.SSTORE(0x01, 0xCAFE) + Op.STOP
else:
raise ValueError(f"unknown modification: {modification}")
pre[collision_address] = Account(code=x_code, nonce=1)
tx_gas_limit = fork.transaction_gas_limit_cap()
txs = [Transaction(sender=alice, to=factory, gas_limit=tx_gas_limit)]
account_expectations: dict = {
alice: BalAccountExpectation(
nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=1)],
),
# Factory's nonce bumps even on failed CREATE/CREATE2; slot 0
# records the failure return value (0).
factory: BalAccountExpectation(
nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=2)],
storage_changes=[
BalStorageSlot(
slot=0x00,
slot_changes=[
BalStorageChange(block_access_index=1, post_value=0)
],
)
],
),
}
post: dict = {
alice: Account(nonce=1),
factory: Account(nonce=2, storage={0x00: 0}),
}
if modification == "collision_only":
account_expectations[collision_address] = BalAccountExpectation.empty()
post[collision_address] = Account(
code=x_code, nonce=1, balance=0, storage={}
)
elif modification == "then_nonce_change":
txs.append(
Transaction(
sender=bob, to=collision_address, gas_limit=tx_gas_limit
)
)
account_expectations[bob] = BalAccountExpectation(
nonce_changes=[BalNonceChange(block_access_index=2, post_nonce=1)],
)
# Strict: only the inner-CREATE nonce bump appears; no spurious
# code/storage/balance entries from the index-1 collision touch.
account_expectations[collision_address] = BalAccountExpectation(
nonce_changes=[BalNonceChange(block_access_index=2, post_nonce=2)],
balance_changes=[],
code_changes=[],
storage_changes=[],
storage_reads=[],
)
# Inner CREATE deploys at addr(X, 1).
inner_created = compute_create_address(
address=collision_address, nonce=1
)
account_expectations[inner_created] = BalAccountExpectation(
nonce_changes=[BalNonceChange(block_access_index=2, post_nonce=1)],
code_changes=[
BalCodeChange(block_access_index=2, new_code=bytes(Op.STOP))
],
balance_changes=[],
storage_changes=[],
storage_reads=[],
)
post[bob] = Account(nonce=1)
post[collision_address] = Account(
code=x_code, nonce=2, balance=0, storage={}
)
post[inner_created] = Account(
nonce=1, code=bytes(Op.STOP), balance=0, storage={}
)
elif modification == "then_storage_change":
txs.append(
Transaction(
sender=bob, to=collision_address, gas_limit=tx_gas_limit
)
)
account_expectations[bob] = BalAccountExpectation(
nonce_changes=[BalNonceChange(block_access_index=2, post_nonce=1)],
)
# Strict: only the SSTORE slot appears; no spurious other entries.
account_expectations[collision_address] = BalAccountExpectation(
storage_changes=[
BalStorageSlot(
slot=0x01,
slot_changes=[
BalStorageChange(
block_access_index=2, post_value=0xCAFE
)
],
)
],
nonce_changes=[],
balance_changes=[],
code_changes=[],
storage_reads=[],
)
post[bob] = Account(nonce=1)
post[collision_address] = Account(
code=x_code, nonce=1, balance=0, storage={0x01: 0xCAFE}
)
else:
raise ValueError(f"unknown modification: {modification}")
block = Block(
txs=txs,
expected_block_access_list=BlockAccessListExpectation(
account_expectations=account_expectations,
),
)
blockchain_test(pre=pre, blocks=[block], post=post)