Skip to content

test_tstore_unique_keys()

Documentation for tests/benchmark/stateful/bloatnet/test_transient_storage.py::test_tstore_unique_keys@892e6d1e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/benchmark/stateful/bloatnet/test_transient_storage.py::test_tstore_unique_keys --gas-benchmark-values 1

Benchmark TSTORE with unique keys per iteration.

Saturate transient storage by writing incrementing keys. Optionally follow each TSTORE with a TLOAD readback to stress both write and read paths.

Source code in tests/benchmark/stateful/bloatnet/test_transient_storage.py
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
@pytest.mark.parametrize("with_tload", [True, False])
def test_tstore_unique_keys(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    gas_benchmark_value: int,
    tx_gas_limit: int,
    with_tload: bool,
) -> None:
    """
    Benchmark TSTORE with unique keys per iteration.

    Saturate transient storage by writing incrementing keys.
    Optionally follow each TSTORE with a TLOAD readback to stress
    both write and read paths.
    """
    # Memory layout: MEM[0..31] = counter (incrementing)
    setup = (
        Op.MSTORE(
            0,
            Op.CALLDATALOAD(32),  # starting counter
            old_memory_size=0,
            new_memory_size=32,
        )
        + Op.CALLDATALOAD(0)  # [num_iters]
    )

    # TSTORE(counter, 1) — write to unique transient key
    body = Op.TSTORE(Op.MLOAD(0), 1)

    if with_tload:
        # TLOAD readback — stress write+read pattern
        body += Op.POP(Op.TLOAD(Op.MLOAD(0)))

    # Increment counter in memory
    body += Op.MSTORE(0, Op.ADD(Op.MLOAD(0), 1))

    loop = While(
        body=body,
        condition=DECREMENT_COUNTER_CONDITION,
    )

    code = setup + loop
    attack_contract_address = pre.deploy_contract(code=code)

    # Gas Accounting
    txs, total_gas_consumed = build_benchmark_txs(
        pre=pre,
        fork=fork,
        gas_benchmark_value=gas_benchmark_value,
        tx_gas_limit=tx_gas_limit,
        attack_contract_address=attack_contract_address,
        setup_cost=setup.gas_cost(fork),
        iteration_cost=loop.gas_cost(fork),
    )

    benchmark_test(
        pre=pre,
        blocks=[Block(txs=txs)],
        expected_benchmark_gas_used=total_gas_consumed,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 3 forks.