Skip to content

test_worst_storage_access_cold()

Documentation for tests/benchmark/test_worst_stateful_opcodes.py::test_worst_storage_access_cold@e9958ed2.

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/test_worst_stateful_opcodes.py::test_worst_storage_access_cold -m benchmark

Test running a block with as many cold storage slot accesses as possible.

Source code in tests/benchmark/test_worst_stateful_opcodes.py
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
240
241
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
@pytest.mark.parametrize(
    "storage_action,tx_result",
    [
        pytest.param(
            StorageAction.READ,
            TransactionResult.SUCCESS,
            id="SSLOAD",
        ),
        pytest.param(
            StorageAction.WRITE_SAME_VALUE,
            TransactionResult.SUCCESS,
            id="SSTORE same value",
        ),
        pytest.param(
            StorageAction.WRITE_SAME_VALUE,
            TransactionResult.REVERT,
            id="SSTORE same value, revert",
        ),
        pytest.param(
            StorageAction.WRITE_SAME_VALUE,
            TransactionResult.OUT_OF_GAS,
            id="SSTORE same value, out of gas",
        ),
        pytest.param(
            StorageAction.WRITE_NEW_VALUE,
            TransactionResult.SUCCESS,
            id="SSTORE new value",
        ),
        pytest.param(
            StorageAction.WRITE_NEW_VALUE,
            TransactionResult.REVERT,
            id="SSTORE new value, revert",
        ),
        pytest.param(
            StorageAction.WRITE_NEW_VALUE,
            TransactionResult.OUT_OF_GAS,
            id="SSTORE new value, out of gas",
        ),
    ],
)
@pytest.mark.parametrize(
    "absent_slots",
    [
        True,
        False,
    ],
)
def test_worst_storage_access_cold(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    storage_action: StorageAction,
    absent_slots: bool,
    env: Environment,
    gas_benchmark_value: int,
    tx_result: TransactionResult,
) -> None:
    """
    Test running a block with as many cold storage slot accesses as possible.
    """
    gas_costs = fork.gas_costs()
    intrinsic_gas_cost_calc = fork.transaction_intrinsic_cost_calculator()

    loop_cost = gas_costs.G_COLD_SLOAD  # All accesses are always cold
    if storage_action == StorageAction.WRITE_NEW_VALUE:
        if not absent_slots:
            loop_cost += gas_costs.G_STORAGE_RESET
        else:
            loop_cost += gas_costs.G_STORAGE_SET
    elif storage_action == StorageAction.WRITE_SAME_VALUE:
        if absent_slots:
            loop_cost += gas_costs.G_STORAGE_SET
        else:
            loop_cost += gas_costs.G_WARM_SLOAD
    elif storage_action == StorageAction.READ:
        loop_cost += 0  # Only G_COLD_SLOAD is charged

    # Contract code
    execution_code_body = Bytecode()
    if storage_action == StorageAction.WRITE_SAME_VALUE:
        # All the storage slots in the contract are initialized to their index.
        # That is, storage slot `i` is initialized to `i`.
        execution_code_body = Op.SSTORE(Op.DUP1, Op.DUP1)
        loop_cost += gas_costs.G_VERY_LOW * 2
    elif storage_action == StorageAction.WRITE_NEW_VALUE:
        # The new value 2^256-1 is guaranteed to be different from the initial
        # value.
        execution_code_body = Op.SSTORE(Op.DUP2, Op.NOT(0))
        loop_cost += gas_costs.G_VERY_LOW * 3
    elif storage_action == StorageAction.READ:
        execution_code_body = Op.POP(Op.SLOAD(Op.DUP1))
        loop_cost += gas_costs.G_VERY_LOW + gas_costs.G_BASE

    # Add costs jump-logic costs
    loop_cost += (
        gas_costs.G_JUMPDEST  # Prefix Jumpdest
        + gas_costs.G_VERY_LOW * 7  # ISZEROs, PUSHs, SWAPs, SUB, DUP
        + gas_costs.G_HIGH  # JUMPI
    )

    prefix_cost = (
        gas_costs.G_VERY_LOW  # Target slots push
    )

    suffix_cost = 0
    if tx_result == TransactionResult.REVERT:
        suffix_cost = (
            gas_costs.G_VERY_LOW * 2  # Revert PUSHs
        )

    num_target_slots = (
        gas_benchmark_value - intrinsic_gas_cost_calc() - prefix_cost - suffix_cost
    ) // loop_cost
    if tx_result == TransactionResult.OUT_OF_GAS:
        # Add an extra slot to make it run out-of-gas
        num_target_slots += 1

    code_prefix = Op.PUSH4(num_target_slots) + Op.JUMPDEST
    code_loop = execution_code_body + Op.JUMPI(
        len(code_prefix) - 1, Op.PUSH1(1) + Op.SWAP1 + Op.SUB + Op.DUP1 + Op.ISZERO + Op.ISZERO
    )
    execution_code = code_prefix + code_loop

    if tx_result == TransactionResult.REVERT:
        execution_code += Op.REVERT(0, 0)
    else:
        execution_code += Op.STOP

    execution_code_address = pre.deploy_contract(code=execution_code)

    total_gas_used = (
        num_target_slots * loop_cost + intrinsic_gas_cost_calc() + prefix_cost + suffix_cost
    )

    # Contract creation
    slots_init = Bytecode()
    if not absent_slots:
        slots_init = Op.PUSH4(num_target_slots) + While(
            body=Op.SSTORE(Op.DUP1, Op.DUP1),
            condition=Op.PUSH1(1) + Op.SWAP1 + Op.SUB + Op.DUP1 + Op.ISZERO + Op.ISZERO,
        )

    # To create the contract, we apply the slots_init code to initialize the
    # storage slots (int the case of absent_slots=False) and then copy the
    # execution code to the contract.
    creation_code = (
        slots_init
        + Op.EXTCODECOPY(
            address=execution_code_address,
            dest_offset=0,
            offset=0,
            size=Op.EXTCODESIZE(execution_code_address),
        )
        + Op.RETURN(0, Op.MSIZE)
    )
    sender_addr = pre.fund_eoa()
    setup_tx = Transaction(
        to=None,
        gas_limit=env.gas_limit,
        data=creation_code,
        sender=sender_addr,
    )

    blocks = [Block(txs=[setup_tx])]

    contract_address = compute_create_address(address=sender_addr, nonce=0)

    op_tx = Transaction(
        to=contract_address,
        gas_limit=gas_benchmark_value,
        sender=pre.fund_eoa(),
    )
    blocks.append(Block(txs=[op_tx]))

    benchmark_test(
        blocks=blocks,
        expected_benchmark_gas_used=(
            total_gas_used if tx_result != TransactionResult.OUT_OF_GAS else gas_benchmark_value
        ),
    )

Parametrized Test Cases

This test case is only parametrized by fork.

Test ID (Abbreviated) absent_slots storage_action tx_result
...fork_Prague-blockchain_test-absent_slots_True-SSLOAD True auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_True-SSTORE same value True auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_True-SSTORE same value, revert True auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_True-SSTORE same value, out of gas True auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_True-SSTORE new value True auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_True-SSTORE new value, revert True auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_True-SSTORE new value, out of gas True auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_False-SSLOAD False auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_False-SSTORE same value False auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_False-SSTORE same value, revert False auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_False-SSTORE same value, out of gas False auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_False-SSTORE new value False auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_False-SSTORE new value, revert False auto(_auto_null) auto(_auto_null)
...fork_Prague-blockchain_test-absent_slots_False-SSTORE new value, out of gas False auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_True-SSLOAD True auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_True-SSTORE same value True auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_True-SSTORE same value, revert True auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_True-SSTORE same value, out of gas True auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_True-SSTORE new value True auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_True-SSTORE new value, revert True auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_True-SSTORE new value, out of gas True auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_False-SSLOAD False auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_False-SSTORE same value False auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_False-SSTORE same value, revert False auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_False-SSTORE same value, out of gas False auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_False-SSTORE new value False auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_False-SSTORE new value, revert False auto(_auto_null) auto(_auto_null)
...fork_Osaka-blockchain_test-absent_slots_False-SSTORE new value, out of gas False auto(_auto_null) auto(_auto_null)