|
7 | 7 | Alloc, |
8 | 8 | Block, |
9 | 9 | BlockchainTestFiller, |
| 10 | + Initcode, |
10 | 11 | Storage, |
11 | 12 | Transaction, |
12 | 13 | compute_create_address, |
@@ -284,3 +285,109 @@ def test_bal_code_changes( |
284 | 285 | ), |
285 | 286 | }, |
286 | 287 | ) |
| 288 | + |
| 289 | + |
| 290 | +@pytest.mark.valid_from("Amsterdam") |
| 291 | +@pytest.mark.parametrize( |
| 292 | + "self_destruct_in_same_tx", |
| 293 | + [True, False], |
| 294 | + ids=["self_destruct_in_same_tx", "self_destruct_in_a_new_tx"], |
| 295 | +) |
| 296 | +def test_bal_self_destruct( |
| 297 | + pre: Alloc, |
| 298 | + blockchain_test: BlockchainTestFiller, |
| 299 | + self_destruct_in_same_tx: bool, |
| 300 | +): |
| 301 | + """Ensure BAL captures balance changes caused by `SELFDESTRUCT`.""" |
| 302 | + alice = pre.fund_eoa() |
| 303 | + bob = pre.fund_eoa(amount=0) |
| 304 | + |
| 305 | + selfdestruct_code = Op.SELFDESTRUCT(bob) |
| 306 | + # A pre existing self-destruct contract |
| 307 | + kaboom = pre.deploy_contract(code=selfdestruct_code) |
| 308 | + |
| 309 | + # A template for self-destruct contract |
| 310 | + self_destruct_init_code = Initcode(deploy_code=selfdestruct_code) |
| 311 | + template = pre.deploy_contract(code=self_destruct_init_code) |
| 312 | + |
| 313 | + if self_destruct_in_same_tx: |
| 314 | + # The goal is to create a self-destructing contract in the same |
| 315 | + # transaction to trigger deletion of code as per EIP-6780. |
| 316 | + # The factory contract below creates a new self-destructing |
| 317 | + # contract and calls it in this transaction. |
| 318 | + |
| 319 | + bytecode_size = len(self_destruct_init_code) |
| 320 | + factory_bytecode = ( |
| 321 | + # Clone template memory |
| 322 | + Op.EXTCODECOPY(template, 0, 0, bytecode_size) |
| 323 | + # Fund 100 wei and deploy the clone |
| 324 | + + Op.CREATE(100, 0, bytecode_size) |
| 325 | + # Call the clone, which self-destructs |
| 326 | + + Op.CALL(50_000, Op.DUP6, 0, 0, 0, 0, 0) |
| 327 | + + Op.STOP |
| 328 | + ) |
| 329 | + |
| 330 | + factory = pre.deploy_contract(code=factory_bytecode) |
| 331 | + kaboom_same_tx = compute_create_address(address=factory, nonce=1) |
| 332 | + |
| 333 | + tx = Transaction( |
| 334 | + sender=alice, |
| 335 | + to=factory if self_destruct_in_same_tx else kaboom, |
| 336 | + value=100, |
| 337 | + gas_limit=1_000_000, |
| 338 | + ) |
| 339 | + |
| 340 | + # Determine which account was destructed |
| 341 | + self_destructed_account = kaboom_same_tx if self_destruct_in_same_tx else kaboom |
| 342 | + |
| 343 | + block = Block( |
| 344 | + txs=[tx], |
| 345 | + expected_block_access_list=BlockAccessListExpectation( |
| 346 | + account_expectations={ |
| 347 | + alice: BalAccountExpectation( |
| 348 | + nonce_changes=[BalNonceChange(tx_index=1, post_nonce=1)], |
| 349 | + ), |
| 350 | + bob: BalAccountExpectation( |
| 351 | + balance_changes=[BalBalanceChange(tx_index=1, post_balance=100)] |
| 352 | + ), |
| 353 | + self_destructed_account: BalAccountExpectation( |
| 354 | + # When an account is deleted its post nonce is set to 0 |
| 355 | + # as per EIP-7928. |
| 356 | + nonce_changes=[BalNonceChange(tx_index=1, post_nonce=0)] |
| 357 | + if self_destruct_in_same_tx |
| 358 | + else [], |
| 359 | + balance_changes=[BalBalanceChange(tx_index=1, post_balance=0)], |
| 360 | + # Expect code to be cleared if self-destructed in same transaction. |
| 361 | + code_changes=[BalCodeChange(tx_index=1, new_code="")] |
| 362 | + if self_destruct_in_same_tx |
| 363 | + else [], |
| 364 | + ), |
| 365 | + } |
| 366 | + ), |
| 367 | + ) |
| 368 | + |
| 369 | + post = { |
| 370 | + alice: Account(nonce=1), |
| 371 | + bob: Account(balance=100), |
| 372 | + kaboom: Account(balance=0, code=selfdestruct_code), |
| 373 | + } |
| 374 | + |
| 375 | + # If the account was self-destructed in the same transaction, |
| 376 | + # we expect the account to non-existent and its balance to be 0. |
| 377 | + if self_destruct_in_same_tx: |
| 378 | + post.update( |
| 379 | + { |
| 380 | + factory: Account( |
| 381 | + nonce=2, # incremented after CREATE |
| 382 | + balance=0, # spent on CREATE |
| 383 | + code=factory_bytecode, |
| 384 | + ), |
| 385 | + kaboom_same_tx: Account.NONEXISTENT, # type: ignore |
| 386 | + } |
| 387 | + ) |
| 388 | + |
| 389 | + blockchain_test( |
| 390 | + pre=pre, |
| 391 | + blocks=[block], |
| 392 | + post=post, |
| 393 | + ) |
0 commit comments