Skip to content

test_worst_storage_access_warm()

Documentation for tests/zkevm/test_worst_stateful_opcodes.py::test_worst_storage_access_warm@64f949d0.

Generate fixtures for these test cases for Prague with:

fill -v tests/zkevm/test_worst_stateful_opcodes.py::test_worst_storage_access_warm --fork Prague

Test running a block with as many warm storage slot accesses as possible.

Source code in tests/zkevm/test_worst_stateful_opcodes.py
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
@pytest.mark.valid_from("Cancun")
@pytest.mark.parametrize(
    "storage_action",
    [
        pytest.param(StorageAction.READ, id="SLOAD"),
        pytest.param(StorageAction.WRITE_SAME_VALUE, id="SSTORE same value"),
        pytest.param(StorageAction.WRITE_NEW_VALUE, id="SSTORE new value"),
    ],
)
@pytest.mark.slow()
def test_worst_storage_access_warm(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    storage_action: StorageAction,
):
    """Test running a block with as many warm storage slot accesses as possible."""
    env = Environment(gas_limit=100_000_000_000)
    attack_gas_limit = Environment().gas_limit

    blocks = []

    # The target storage slot for the warm access is storage slot 0.
    storage_slot_initial_value = 10

    # Contract code
    execution_code_body = Bytecode()
    if storage_action == StorageAction.WRITE_SAME_VALUE:
        execution_code_body = Op.SSTORE(0, Op.DUP1)
    elif storage_action == StorageAction.WRITE_NEW_VALUE:
        execution_code_body = Op.PUSH1(1) + Op.ADD + Op.SSTORE(0, Op.DUP1)
    elif storage_action == StorageAction.READ:
        execution_code_body = Op.POP(Op.SLOAD(0))

    execution_code = Op.PUSH1(storage_slot_initial_value) + While(
        body=execution_code_body,
    )
    execution_code_address = pre.deploy_contract(code=execution_code)

    creation_code = (
        Op.SSTORE(0, storage_slot_initial_value)
        + Op.EXTCODECOPY(
            address=execution_code_address,
            dest_offset=0,
            offset=0,
            size=Op.EXTCODESIZE(execution_code_address),
        )
        + Op.RETURN(0, Op.MSIZE)
    )
    sender_addr = pre.fund_eoa()
    setup_tx = Transaction(
        to=None,
        gas_limit=env.gas_limit,
        data=creation_code,
        sender=sender_addr,
    )
    blocks.append(Block(txs=[setup_tx]))

    contract_address = compute_create_address(address=sender_addr, nonce=0)

    op_tx = Transaction(
        to=contract_address,
        gas_limit=attack_gas_limit,
        sender=pre.fund_eoa(),
    )
    blocks.append(Block(txs=[op_tx]))

    blockchain_test(
        genesis_environment=env,
        pre=pre,
        post={},
        blocks=blocks,
    )

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) storage_action
...fork_Cancun-blockchain_test-SLOAD 1
...fork_Cancun-blockchain_test-SSTORE same value 2
...fork_Cancun-blockchain_test-SSTORE new value 3
...fork_Prague-blockchain_test-SLOAD 1
...fork_Prague-blockchain_test-SSTORE same value 2
...fork_Prague-blockchain_test-SSTORE new value 3
...fork_Osaka-blockchain_test-SLOAD 1
...fork_Osaka-blockchain_test-SSTORE same value 2
...fork_Osaka-blockchain_test-SSTORE new value 3