Skip to content

Test Tstorage

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

Generate fixtures for these test cases for Cancun with:

Cancun only:

fill -v tests/cancun/eip1153_tstore/test_tstorage.py --fork=Cancun --evm-bin=/path/to/evm-tool-dev-version
For all forks up to and including Cancun:
fill -v tests/cancun/eip1153_tstore/test_tstorage.py --until=Cancun --evm-bin=/path/to/evm-tool-dev-version

abstract: Tests EIP-1153: Transient Storage Opcodes

Test [EIP-1153: Transient Storage Opcodes](https://eips.ethereum.org/EIPS/eip-1153). Ports
and extends some tests from
[ethereum/tests/src/EIPTestsFiller/StateTests/stEIP1153-transientStorage/](https://github.com/ethereum/tests/blob/9b00b68593f5869eb51a6659e1cc983e875e616b/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
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
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 = b"".join([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}),
    }

    txs = [
        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,
        txs=txs,
    )

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
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
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 = b"".join(
        [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: 0 for slot in slots_under_test}),
    }

    txs = [
        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,
        txs=txs,
    )

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
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
135
136
137
138
139
140
141
142
143
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 = b"".join(
        [
            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}),
    }

    txs = [
        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,
        txs=txs,
    )

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
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
178
179
180
181
182
183
184
185
186
187
188
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 = b"".join([Op.TSTORE(slot, 1234) for slot in slots_to_write]) + b"".join(
        [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}
        ),
    }

    txs = [
        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,
        txs=txs,
    )

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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
@GasMeasureTestCases.parametrize()
def test_gas_usage(
    state_test: StateTestFiller,
    bytecode: Code,
    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),
    }
    txs = [
        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, txs=txs, post=post)