Skip to content

test_code_deposit_state_gas_exact_fit_boundary()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_code_deposit_state_gas_exact_fit_boundary@5c024cbb.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_code_deposit_state_gas_exact_fit_boundary --fork Amsterdam

Pin the code-deposit state gas at its exact-fit boundary.

A CREATE tx deploys code_size bytes with gas_limit set so the deposit lands exactly at the available gas (deploys) or one gas short (halts: state restored, the top-frame NEW_ACCOUNT refilled, no code). Under EIP-2780 the created account's NEW_ACCOUNT state gas is charged at the top frame (not bundled in the intrinsic), so exact_fit_gas includes it explicitly. The two regimes pin the halt billing: over-cap reservoir rolls the reservoir back so the sender pays the cap; in-cap spill refills the spilled state gas into gas_left and burns it all, billing the full gas_limit. The scaling tests assert success only.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
@pytest.mark.parametrize(
    ("funding", "gas_delta"),
    [
        pytest.param("reservoir", 0, id="reservoir_success"),
        pytest.param("reservoir", -1, id="reservoir_oog"),
        pytest.param("spill", 0, id="spill_success"),
        pytest.param("spill", -1, id="spill_oog"),
    ],
)
@EIPChecklist.GasCostChanges.Test.OutOfGas()
@pytest.mark.valid_from("EIP8037")
def test_code_deposit_state_gas_exact_fit_boundary(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    funding: str,
    gas_delta: int,
) -> None:
    """
    Pin the code-deposit state gas at its exact-fit boundary.

    A CREATE tx deploys ``code_size`` bytes with ``gas_limit`` set so the
    deposit lands exactly at the available gas (deploys) or one gas short
    (halts: state restored, the top-frame ``NEW_ACCOUNT`` refilled, no
    code). Under EIP-2780 the created account's ``NEW_ACCOUNT`` state gas
    is charged at the top frame (not bundled in the intrinsic), so
    ``exact_fit_gas`` includes it explicitly. The two regimes pin the halt
    billing: over-cap ``reservoir`` rolls the reservoir back so the sender
    pays the cap; in-cap ``spill`` refills the spilled state gas into
    ``gas_left`` and burns it all, billing the full ``gas_limit``. The
    scaling tests assert success only.
    """
    gas_costs = fork.gas_costs()
    cap = fork.transaction_gas_limit_cap()
    assert cap is not None

    code_size = fork.max_code_size() if funding == "reservoir" else 1000

    words = (code_size + 31) // 32
    memory_gas = gas_costs.MEMORY_PER_WORD * words + words * words // 512
    init_code = Op.RETURN(0, code_size)
    init_exec_regular = init_code.regular_cost(fork) + memory_gas
    keccak_gas = gas_costs.OPCODE_KECCAK256_PER_WORD * words
    deposit_state_gas = fork.code_deposit_state_gas(code_size=code_size)

    intrinsic_regular = fork.transaction_intrinsic_cost_calculator()(
        calldata=bytes(init_code),
        contract_creation=True,
        return_cost_deducted_prior_execution=True,
    )
    # The fresh target's NEW_ACCOUNT is a top-frame state charge under
    # EIP-2780, no longer folded into the intrinsic.
    exact_fit_gas = (
        intrinsic_regular
        + gas_costs.NEW_ACCOUNT
        + init_exec_regular
        + keccak_gas
        + deposit_state_gas
    )
    if funding == "reservoir":
        assert exact_fit_gas > cap
    else:
        assert exact_fit_gas <= cap

    sender = pre.fund_eoa()
    created = compute_create_address(address=sender, nonce=0)
    gas_limit = exact_fit_gas + gas_delta

    post: dict
    if gas_delta == 0:
        receipt_gas_used = exact_fit_gas
        post = {created: Account(code=b"\x00" * code_size)}
    else:
        # reservoir: the deposit OOG refills the reservoir, so the sender
        # pays the regular cap. spill: the refilled NEW_ACCOUNT lands in
        # gas_left and is burned, so the sender pays the full gas_limit.
        receipt_gas_used = cap if funding == "reservoir" else gas_limit
        post = {created: Account.NONEXISTENT}

    tx = Transaction(
        to=None,
        data=init_code,
        gas_limit=gas_limit,
        sender=sender,
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=receipt_gas_used
        ),
    )

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.