Skip to content

test_block_2d_gas_boundary_exact_fit()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_block_2d_gas_accounting.py::test_block_2d_gas_boundary_exact_fit@5c024cbb.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_block_2d_gas_accounting.py::test_block_2d_gas_boundary_exact_fit --fork Amsterdam

Verify a block is valid when state gas dominates regular gas.

Clients that sum regular + state will reject this valid block.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_block_2d_gas_accounting.py
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
@pytest.mark.parametrize(
    "num_txs,num_sstores",
    [
        pytest.param(1, 1, id="single_sstore_single_tx"),
        pytest.param(5, 1, id="single_sstore"),
        pytest.param(20, 1, id="single_sstore_many_txs"),
        pytest.param(10, 5, id="multi_sstore_many_txs"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_block_2d_gas_boundary_exact_fit(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    num_txs: int,
    num_sstores: int,
) -> None:
    """
    Verify a block is valid when state gas dominates regular gas.

    Clients that sum regular + state will reject this valid block.
    """
    block_gas_limit = 30_000_000
    while True:
        # We have a circular dependency to calculate the block gas limit based
        # on the transactions required gas (tx gas increments as we increase
        # the block gas limit to fit). This loops tries incrementing the
        # block gas limit by consistent steps in order to find the minimum gas
        # allows the transactions required to fit.
        env = Environment(
            gas_limit=block_gas_limit,
        )
        tx_regular, tx_state = sstore_tx_gas(fork, num_sstores)
        intrinsic_regular = fork.transaction_intrinsic_cost_calculator()()

        tx_limit = tx_regular + tx_state + tx_regular // 10

        # Per-tx worst-case state contribution: tx.gas - intrinsic_regular.
        # The block_gas_limit must leave enough state budget for every tx.
        worst_state_per_tx = tx_limit - intrinsic_regular
        minimum_block_gas_limit = max(
            # Regular dimension: last tx must fit.
            (num_txs - 1) * tx_regular + tx_limit,
            # State dimension: cumulative worst-case must fit.
            num_txs * worst_state_per_tx,
        )
        if block_gas_limit >= minimum_block_gas_limit:
            break
        block_gas_limit += 1_000_000

    block_regular = num_txs * tx_regular
    block_state = num_txs * tx_state
    expected_gas_used = max(block_regular, block_state)

    txs, post = sstore_txs(
        pre,
        fork,
        num_txs,
        num_sstores=num_sstores,
        tx_gas_limit=tx_limit,
    )

    blockchain_test(
        genesis_environment=env,
        pre=pre,
        blocks=[
            Block(
                txs=txs,
                gas_limit=block_gas_limit,
                header_verify=Header(gas_used=expected_gas_used),
            )
        ],
        post=post,
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.