Skip to content

test_tstore_unique_keys()

Documentation for tests/benchmark/stateful/bloatnet/test_transient_storage.py::test_tstore_unique_keys@622620d8.

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 a unique key per iteration.

Source code in tests/benchmark/stateful/bloatnet/test_transient_storage.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@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 a unique key per iteration."""
    # 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.