Skip to content

test_create_insufficient_balance_returns_reservoir()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_create_insufficient_balance_returns_reservoir@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_create_insufficient_balance_returns_reservoir --fork Amsterdam

Test CREATE with insufficient balance returns reservoir to parent.

When CREATE is called but the sender doesn't have enough balance for the endowment, the operation fails and both gas and state gas reservoir are returned to the parent frame. A probe handed only its SSTORE's execution cost then succeeds, proving the state gas came back to the reservoir rather than to gas_left.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
@pytest.mark.valid_from("EIP8037")
def test_create_insufficient_balance_returns_reservoir(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Test CREATE with insufficient balance returns reservoir to parent.

    When CREATE is called but the sender doesn't have enough balance
    for the endowment, the operation fails and both gas and state gas
    reservoir are returned to the parent frame. A probe handed only its
    SSTORE's execution cost then succeeds, proving the state gas came
    back to the reservoir rather than to `gas_left`.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    init_code = Op.STOP
    mstore_value, init_code_size = init_code_at_high_bytes(init_code)

    probe_code = Op.SSTORE(0, 1)
    probe = pre.deploy_contract(code=probe_code)
    probe_gas = probe_code.execution_cost(fork)

    storage = Storage()
    create_slot = storage.store_next(0, "create_fails")
    probe_slot = storage.store_next(1, "probe_succeeds")
    contract_code = (
        Op.MSTORE(0, mstore_value)
        + Op.SSTORE(create_slot, Op.CREATE(1, 0, init_code_size))
        + Op.SSTORE(
            probe_slot,
            Op.CALL(gas=probe_gas, address=probe),
            # gas accounting
            original_value=1,
            current_value=1,
            new_value=1,
            key_warm=False,
        )
    )
    contract = pre.deploy_contract(code=contract_code, storage={probe_slot: 1})

    tx = Transaction(
        to=contract,
        state_gas_reservoir=sstore_state_gas,
        sender=pre.fund_eoa(),
    )

    post = {
        contract: Account(storage=storage),
        probe: Account(storage={0: 1}),
    }
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.