Skip to content

Test Tstorage

Documentation for tests/cancun/eip1153_tstore/test_tstorage.py.

Generate fixtures for these test cases with:

fill -v tests/cancun/eip1153_tstore/test_tstorage.py
Tests EIP-1153: Transient Storage Opcodes

Test EIP-1153: Transient Storage Opcodes. Ports and extends some tests from ethereum/tests/src/EIPTestsFiller/StateTests/stEIP1153-transientStorage/

test_transient_storage_unset_values(state_test)

Test that tload returns zero for unset values. Loading an arbitrary value is 0 at beginning of transaction: TLOAD(x) is 0.

Based on ethereum/tests/.../01_tloadBeginningTxnFiller.yml", # noqa: E501

Source code in tests/cancun/eip1153_tstore/test_tstorage.py
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
def test_transient_storage_unset_values(state_test: StateTestFiller):
    """
    Test that tload returns zero for unset values. Loading an arbitrary value is
    0 at beginning of transaction: TLOAD(x) is 0.

    Based on [ethereum/tests/.../01_tloadBeginningTxnFiller.yml](https://github.com/ethereum/tests/blob/9b00b68593f5869eb51a6659e1cc983e875e616b/src/EIPTestsFiller/StateTests/stEIP1153-transientStorage/01_tloadBeginningTxnFiller.yml)",  # noqa: E501
    """
    env = Environment()

    slots_under_test = [0, 1, 2, 2**128, 2**256 - 1]
    code = sum(Op.SSTORE(slot, Op.TLOAD(slot)) for slot in slots_under_test)

    pre = {
        TestAddress: Account(balance=10_000_000),
        code_address: Account(code=code, storage={slot: 1 for slot in slots_under_test}),
    }

    tx = Transaction(
        to=code_address,
        data=b"",
        gas_limit=1_000_000,
    )

    post = {code_address: Account(storage={slot: 0 for slot in slots_under_test})}

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

test_tload_after_tstore(state_test)

Loading after storing returns the stored value: TSTORE(x, y), TLOAD(x) returns y.

Based on ethereum/tests/.../02_tloadAfterTstoreFiller.yml", # noqa: E501

Source code in tests/cancun/eip1153_tstore/test_tstorage.py
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
def test_tload_after_tstore(state_test: StateTestFiller):
    """
    Loading after storing returns the stored value: TSTORE(x, y), TLOAD(x)
    returns y.

    Based on [ethereum/tests/.../02_tloadAfterTstoreFiller.yml](https://github.com/ethereum/tests/blob/9b00b68593f5869eb51a6659e1cc983e875e616b/src/EIPTestsFiller/StateTests/stEIP1153-transientStorage/02_tloadAfterTstoreFiller.yml)",  # noqa: E501
    """
    env = Environment()

    slots_under_test = [0, 1, 2, 2**128, 2**256 - 1]
    code = sum(
        Op.TSTORE(slot, slot) + Op.SSTORE(slot, Op.TLOAD(slot)) for slot in slots_under_test
    )

    pre = {
        TestAddress: Account(balance=10_000_000),
        code_address: Account(code=code, storage={slot: 0xFF for slot in slots_under_test}),
    }

    tx = Transaction(
        to=code_address,
        data=b"",
        gas_limit=1_000_000,
    )

    post = {code_address: Account(storage={slot: slot for slot in slots_under_test})}

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

test_tload_after_sstore(state_test)

Loading after storing returns the stored value: TSTORE(x, y), TLOAD(x) returns y.

Based on ethereum/tests/.../18_tloadAfterStoreFiller.yml", # noqa: E501

Source code in tests/cancun/eip1153_tstore/test_tstorage.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def test_tload_after_sstore(state_test: StateTestFiller):
    """
    Loading after storing returns the stored value: TSTORE(x, y), TLOAD(x)
    returns y.

    Based on [ethereum/tests/.../18_tloadAfterStoreFiller.yml](https://github.com/ethereum/tests/blob/9b00b68593f5869eb51a6659e1cc983e875e616b/src/EIPTestsFiller/StateTests/stEIP1153-transientStorage/18_tloadAfterStoreFiller.yml)",  # noqa: E501
    """
    env = Environment()

    slots_under_test = [1, 3, 2**128, 2**256 - 1]
    code = sum(
        Op.SSTORE(slot - 1, 0xFF) + Op.SSTORE(slot, Op.TLOAD(slot - 1))
        for slot in slots_under_test
    )

    pre = {
        TestAddress: Account(balance=10_000_000),
        code_address: Account(code=code, storage={slot: 1 for slot in slots_under_test}),
    }

    tx = Transaction(
        to=code_address,
        data=b"",
        gas_limit=1_000_000,
    )

    post = {
        code_address: Account(
            code=code,
            storage={slot - 1: 0xFF for slot in slots_under_test}
            | {slot: 0 for slot in slots_under_test},
        )
    }

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

test_tload_after_tstore_is_zero(state_test)

Test that tload returns zero after tstore is called with zero.

Based on ethereum/tests/.../03_tloadAfterStoreIs0Filler.yml", # noqa: E501

Source code in tests/cancun/eip1153_tstore/test_tstorage.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def test_tload_after_tstore_is_zero(state_test: StateTestFiller):
    """
    Test that tload returns zero after tstore is called with zero.

    Based on [ethereum/tests/.../03_tloadAfterStoreIs0Filler.yml](https://github.com/ethereum/tests/blob/9b00b68593f5869eb51a6659e1cc983e875e616b/src/EIPTestsFiller/StateTests/stEIP1153-transientStorage/03_tloadAfterStoreIs0Filler.yml)",  # noqa: E501
    """
    env = Environment()

    slots_to_write = [1, 4, 2**128, 2**256 - 2]
    slots_to_read = [slot - 1 for slot in slots_to_write] + [slot + 1 for slot in slots_to_write]
    assert set.intersection(set(slots_to_write), set(slots_to_read)) == set()

    code = sum(Op.TSTORE(slot, 1234) for slot in slots_to_write) + sum(
        Op.SSTORE(slot, Op.TLOAD(slot)) for slot in slots_to_read
    )

    pre = {
        TestAddress: Account(balance=10_000_000),
        code_address: Account(
            code=code, storage={slot: 0xFFFF for slot in slots_to_write + slots_to_read}
        ),
    }

    tx = Transaction(
        to=code_address,
        data=b"",
        gas_limit=1_000_000,
    )

    post = {
        code_address: Account(
            storage={slot: 0 for slot in slots_to_read} | {slot: 0xFFFF for slot in slots_to_write}
        )
    }

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

test_gas_usage(state_test, bytecode, expected_gas, overhead_cost, extra_stack_items)

Test that tstore and tload consume the expected gas.

Source code in tests/cancun/eip1153_tstore/test_tstorage.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
@GasMeasureTestCases.parametrize()
def test_gas_usage(
    state_test: StateTestFiller,
    bytecode: Bytecode,
    expected_gas: int,
    overhead_cost: int,
    extra_stack_items: int,
):
    """
    Test that tstore and tload consume the expected gas.
    """
    gas_measure_bytecode = CodeGasMeasure(
        code=bytecode, overhead_cost=overhead_cost, extra_stack_items=extra_stack_items
    )

    env = Environment()
    pre = {
        TestAddress: Account(balance=10_000_000, nonce=0),
        code_address: Account(code=gas_measure_bytecode),
    }
    tx = Transaction(
        to=code_address,
        data=b"",
        gas_limit=1_000_000,
    )
    post = {
        code_address: Account(code=gas_measure_bytecode, storage={0: expected_gas}),
        TestAddress: Account(nonce=1),
    }
    state_test(env=env, pre=pre, tx=tx, post=post)

test_run_until_out_of_gas(state_test, repeat_bytecode, bytecode_repeat_times)

Use TSTORE over and over to different keys until we run out of gas.

Source code in tests/cancun/eip1153_tstore/test_tstorage.py
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
@LoopRunUntilOutOfGasCases.parametrize()
def test_run_until_out_of_gas(
    state_test: StateTestFiller,
    repeat_bytecode: Bytecode,
    bytecode_repeat_times: int,
):
    """
    Use TSTORE over and over to different keys until we run out of gas.
    """
    bytecode = Op.JUMPDEST + repeat_bytecode * bytecode_repeat_times + Op.JUMP(Op.PUSH0)
    pre = {
        TestAddress: Account(balance=10_000_000_000_000, nonce=0),
        code_address: Account(code=bytecode),
    }
    tx = Transaction(
        to=code_address,
        data=b"",
        gas_limit=30_000_000,
    )
    post = {
        code_address: Account(code=bytecode, storage={}),
        TestAddress: Account(nonce=1),
    }
    state_test(env=Environment(), pre=pre, tx=tx, post=post)