Skip to content

test_call_value_to_self_destructed_header_gas_used()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_call_value_to_self_destructed_header_gas_used@636b5f17.

Generate fixtures for these test cases for Bogota with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_call_value_to_self_destructed_header_gas_used --fork Bogota

Verify block gas accounting for CALL with value to a same transaction selfdestructed account.

Reservoir is sized for the CREATE's state charge only. Under the spec no new account charge fires on the CALL, so block state gas used equals exactly the single account creation charge and the header reports that value. The created account is queued for destruction regardless of whether SELFDESTRUCT targeted itself or an external beneficiary, so the no charge behavior holds across both cases.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
@pytest.mark.parametrize(
    "selfdestruct_beneficiary",
    [
        pytest.param("self", id="self_beneficiary"),
        pytest.param("external", id="external_beneficiary"),
    ],
)
@pytest.mark.parametrize(
    "create_opcode",
    [
        pytest.param(Op.CREATE, id="create"),
        pytest.param(Op.CREATE2, id="create2"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_call_value_to_self_destructed_header_gas_used(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
    selfdestruct_beneficiary: str,
) -> None:
    """
    Verify block gas accounting for CALL with value to a same
    transaction selfdestructed account.

    Reservoir is sized for the CREATE's state charge only. Under
    the spec no new account charge fires on the CALL, so block
    state gas used equals exactly the single account creation
    charge and the header reports that value. The created account
    is queued for destruction regardless of whether SELFDESTRUCT
    targeted itself or an external beneficiary, so the no charge
    behavior holds across both cases.
    """
    if selfdestruct_beneficiary == "self":
        inner_code = Op.SELFDESTRUCT(Op.ADDRESS)
    else:
        # Alive EOA so the SELFDESTRUCT itself does not charge a
        # new account state gas for the beneficiary.
        alive_beneficiary = pre.fund_eoa(amount=1)
        inner_code = Op.SELFDESTRUCT(alive_beneficiary)
    mstore_value, size = init_code_at_high_bytes(inner_code)

    orchestrator_code = (
        Op.MSTORE(0, mstore_value)
        + create_op(create_opcode, value=1, size=size)
        + Op.MSTORE(0x20, Op.DUP1)
        + Op.POP
        + Op.POP(
            Op.CALL(
                gas=Op.GAS,
                address=Op.MLOAD(0x20),
                value=1,
                # gas accounting
                value_transfer=True,
            )
        )
    )
    orchestrator = pre.deploy_contract(code=orchestrator_code, balance=3)
    created = compute_create_address(
        address=orchestrator,
        nonce=1,
        salt=0,
        initcode=bytes(inner_code),
        opcode=create_opcode,
    )

    state_gas = orchestrator_code.state_cost(fork)
    # The codeless target returns the forwarded value-call stipend
    # unused, so it is charged but never consumed.
    execution_gas = (
        fork.transaction_intrinsic_cost_calculator()()
        + orchestrator_code.execution_cost(fork)
        + inner_code.execution_cost(fork)
        - fork.call_value_stipend()
    )
    expected_gas_used = max(execution_gas, state_gas)
    assert expected_gas_used == state_gas, (
        "expected state gas to dominate execution gas"
    )

    tx = Transaction(
        to=orchestrator,
        state_gas_reservoir=state_gas,
        sender=pre.fund_eoa(),
    )

    # End of transaction destruction clears the nonce, code and storage
    # but leaves every wei the account received in place.
    swept = 0 if selfdestruct_beneficiary == "self" else 1
    post: dict = {
        orchestrator: Account(balance=1),
        created: Account(balance=2 - swept, nonce=0, code=b"", storage={}),
    }
    if selfdestruct_beneficiary != "self":
        post[alive_beneficiary] = Account(balance=1 + swept)

    blockchain_test(
        pre=pre,
        blocks=[
            Block(txs=[tx], header_verify=Header(gas_used=expected_gas_used))
        ],
        post=post,
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 2 forks.