Skip to content

Test Basic Tload

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

Generate fixtures for these test cases with:

fill -v tests/cancun/eip1153_tstore/test_basic_tload.py

Ethereum Transient Storage EIP Tests https://eips.ethereum.org/EIPS/eip-1153

test_basic_tload_transaction_begin(state_test, pre)

Ported .json vectors:

(01_tloadBeginningTxnFiller.yml) load arbitrary value is 0 at beginning of transaction

Source code in tests/cancun/eip1153_tstore/test_basic_tload.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
@pytest.mark.valid_from("Cancun")
def test_basic_tload_transaction_begin(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Ported .json vectors:

    (01_tloadBeginningTxnFiller.yml)
    load arbitrary value is 0 at beginning of transaction
    """
    slot_tload_at_transaction_begin_result = 1
    slot_code_worked = 2

    address_to = pre.deploy_contract(
        code=Op.JUMPDEST()
        # 01 test
        + Op.SSTORE(slot_tload_at_transaction_begin_result, Op.TLOAD(0))
        + Op.SSTORE(slot_code_worked, 1),
        storage={
            slot_tload_at_transaction_begin_result: 0xFF,
        },
    )

    post = {
        address_to: Account(
            storage={
                slot_tload_at_transaction_begin_result: 0x00,
                slot_code_worked: 0x01,
            }
        )
    }

    tx = Transaction(
        sender=pre.fund_eoa(7_000_000_000_000_000_000),
        to=address_to,
        gas_price=10,
        data=b"",
        gas_limit=5000000,
        value=0,
    )

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

test_basic_tload_works(state_test, pre)

Ported .json vectors:

(02_tloadAfterTstoreFiller.yml) tload from same slot after tstore returns correct value

Source code in tests/cancun/eip1153_tstore/test_basic_tload.py
 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
@pytest.mark.valid_from("Cancun")
def test_basic_tload_works(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Ported .json vectors:

    (02_tloadAfterTstoreFiller.yml)
    tload from same slot after tstore returns correct value
    """
    tstore_value = 88

    slot_tload_after_tstore_result = 0
    slot_tload_after_tstore_result_second_time = 1
    slot_code_worked = 2

    address_to = pre.deploy_contract(
        code=Op.JUMPDEST()
        # 02 test
        + Op.TSTORE(2, tstore_value)
        + Op.SSTORE(slot_tload_after_tstore_result, Op.TLOAD(2))
        + Op.SSTORE(slot_tload_after_tstore_result_second_time, Op.TLOAD(2))
        + Op.SSTORE(slot_code_worked, 1),
        storage={
            slot_tload_after_tstore_result: 0xFF,
            slot_tload_after_tstore_result_second_time: 0xFF,
        },
    )

    post = {
        address_to: Account(
            storage={
                slot_tload_after_tstore_result: tstore_value,
                slot_tload_after_tstore_result_second_time: tstore_value,
                slot_code_worked: 0x01,
            }
        )
    }

    tx = Transaction(
        sender=pre.fund_eoa(7_000_000_000_000_000_000),
        to=address_to,
        gas_price=10,
        data=b"",
        gas_limit=5000000,
        value=0,
    )

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

test_basic_tload_other_after_tstore(state_test, pre)

Ported .json vectors:

(03_tloadAfterStoreIs0Filler.yml) Loading any other slot after storing to a slot returns 0.

Source code in tests/cancun/eip1153_tstore/test_basic_tload.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
@pytest.mark.valid_from("Cancun")
def test_basic_tload_other_after_tstore(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Ported .json vectors:

    (03_tloadAfterStoreIs0Filler.yml)
    Loading any other slot after storing to a slot returns 0.
    """
    tstore_value = 88

    slot_tload_untouched_slot_after_tstore_result = 1
    slot_code_worked = 2

    address_to = pre.deploy_contract(
        code=Op.JUMPDEST()
        # 03 test
        + Op.TSTORE(3, tstore_value)
        + Op.SSTORE(slot_tload_untouched_slot_after_tstore_result, Op.TLOAD(0))
        + Op.SSTORE(slot_code_worked, 1),
        storage={
            slot_tload_untouched_slot_after_tstore_result: 0xFF,
        },
    )

    post = {
        address_to: Account(
            storage={
                slot_tload_untouched_slot_after_tstore_result: 0x00,
                slot_code_worked: 0x01,
            }
        )
    }

    tx = Transaction(
        sender=pre.fund_eoa(7_000_000_000_000_000_000),
        to=address_to,
        gas_price=10,
        data=b"",
        gas_limit=5000000,
        value=0,
    )

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

test_basic_tload_gasprice(state_test, pre)

Ported .json vectors:

(16_tloadGasFiller.yml) tload costs 100 gas same as a warm sload

Source code in tests/cancun/eip1153_tstore/test_basic_tload.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
@pytest.mark.valid_from("Cancun")
def test_basic_tload_gasprice(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Ported .json vectors:

    (16_tloadGasFiller.yml)
    tload costs 100 gas same as a warm sload
    """
    slot_tload_nonzero_gas_price_result = 1
    slot_tload_zero_gas_price_result = 2
    slot_code_worked = 3

    """
    N         OPNAME       GAS_COST  TOTAL_GAS REMAINING_GAS     STACK
    28-1         MSTORE         2     20748   4958252    2:[4ba82f,0,]
                 MSTORE [0] = 4958255
    29-1          PUSH1         3     20754   4958246
    30-1          TLOAD       100     20757   4958243    1:[10,]
    31-1            GAS         2     20857   4958143    1:[2,]
    32-1          PUSH1         3     20859   4958141    2:[2,4ba7bd,]
    33-1         MSTORE         6     20862   4958138    3:[2,4ba7bd,20,]
                 MSTORE [32] = 4958141
    """
    extra_opcode_gas = 11  # mstore(3), push1(3),gas(2),push1(3)

    address_to = pre.deploy_contract(
        code=Op.JUMPDEST()
        # 16 test
        + Op.TSTORE(16, 2)
        + Op.MSTORE(0, Op.GAS())  # hot load the memory to make the extra_opcode_gas be 11
        + Op.MSTORE(0, Op.GAS())
        + Op.TLOAD(16)
        + Op.MSTORE(32, Op.GAS())
        + Op.SSTORE(slot_tload_nonzero_gas_price_result, Op.SUB(Op.MLOAD(0), Op.MLOAD(32)))
        + Op.SSTORE(
            slot_tload_nonzero_gas_price_result,
            Op.SUB(Op.SLOAD(slot_tload_nonzero_gas_price_result), extra_opcode_gas),
        )
        + Op.MSTORE(0, Op.GAS())
        + Op.TLOAD(5)  # tload slot at 5 is 0
        + Op.MSTORE(32, Op.GAS())
        + Op.SSTORE(slot_tload_zero_gas_price_result, Op.SUB(Op.MLOAD(0), Op.MLOAD(32)))
        + Op.SSTORE(
            slot_tload_zero_gas_price_result,
            Op.SUB(Op.SLOAD(slot_tload_zero_gas_price_result), extra_opcode_gas),
        )
        + Op.SSTORE(slot_code_worked, 1),
        storage={
            slot_tload_nonzero_gas_price_result: 0xFF,
            slot_tload_zero_gas_price_result: 0xFF,
        },
    )

    post = {
        address_to: Account(
            storage={
                slot_tload_nonzero_gas_price_result: Spec.TLOAD_GAS_COST,
                slot_tload_zero_gas_price_result: Spec.TLOAD_GAS_COST,
                slot_code_worked: 0x01,
            }
        )
    }

    tx = Transaction(
        sender=pre.fund_eoa(7_000_000_000_000_000_000),
        to=address_to,
        gas_price=10,
        data=b"",
        gas_limit=5000000,
        value=0,
    )

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

test_basic_tload_after_store(state_test, pre)

Ported .json vectors:

(18_tloadAfterStoreFiller.yml) tload from same slot after store returns 0

Source code in tests/cancun/eip1153_tstore/test_basic_tload.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@pytest.mark.valid_from("Cancun")
def test_basic_tload_after_store(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Ported .json vectors:

    (18_tloadAfterStoreFiller.yml)
    tload from same slot after store returns 0
    """
    slot_tload_from_sstore_result = 1
    slot_code_worked = 2

    address_to = pre.deploy_contract(
        code=Op.JUMPDEST()
        # 18 test
        + Op.SSTORE(slot_tload_from_sstore_result, 22)
        + Op.SSTORE(slot_tload_from_sstore_result, Op.TLOAD(slot_tload_from_sstore_result))
        + Op.SSTORE(slot_code_worked, 1),
        storage={
            slot_tload_from_sstore_result: 0xFF,
        },
    )

    post: Dict[Address, Union[Account, object]] = {}
    post[address_to] = Account(
        storage={
            slot_tload_from_sstore_result: 0x00,
            slot_code_worked: 0x01,
        }
    )

    tx = Transaction(
        sender=pre.fund_eoa(7_000_000_000_000_000_000),
        to=address_to,
        gas_price=10,
        data=b"",
        gas_limit=5000000,
        value=0,
    )

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