Skip to content

test_blobhash_opcode_contexts()

Documentation for tests/cancun/eip4844_blobs/test_blobhash_opcode_contexts.py::test_blobhash_opcode_contexts@892e6d1e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/cancun/eip4844_blobs/test_blobhash_opcode_contexts.py::test_blobhash_opcode_contexts --fork Amsterdam

Tests that the BLOBHASH opcode functions correctly when called in different contexts.

  • BLOBHASH opcode on the top level of the call stack.
  • BLOBHASH opcode on the max value.
  • BLOBHASH opcode on CALL, DELEGATECALL, STATICCALL, and CALLCODE.
  • BLOBHASH opcode on Initcode.
  • BLOBHASH opcode on CREATE and CREATE2.
  • BLOBHASH opcode on transaction types 0, 1 and 2.
Source code in tests/cancun/eip4844_blobs/test_blobhash_opcode_contexts.py
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
@pytest.mark.parametrize(
    "test_case",
    [
        "on_top_level_call_stack",
        "on_max_value",
        "on_CALL",
        "on_DELEGATECALL",
        "on_STATICCALL",
        "on_CALLCODE",
        "on_CREATE",
        "on_CREATE2",
    ],
    ids=lambda x: x,
)
def test_blobhash_opcode_contexts(
    pre: Alloc,
    test_case: str,
    max_blobs_per_tx: int,
    simple_blob_hashes: List[bytes],
    fork: Fork,
    state_test: StateTestFiller,
) -> None:
    """
    Tests that the `BLOBHASH` opcode functions correctly when called in
    different contexts.

    - `BLOBHASH` opcode on the top level of the call stack.
    - `BLOBHASH` opcode on the max value.
    - `BLOBHASH` opcode on `CALL`, `DELEGATECALL`, `STATICCALL`, and
        `CALLCODE`.
    - `BLOBHASH` opcode on Initcode.
    - `BLOBHASH` opcode on `CREATE` and `CREATE2`.
    - `BLOBHASH` opcode on transaction types 0, 1 and 2.
    """
    tx_to: Address
    post: dict[Address, Account]

    match test_case:
        case "on_top_level_call_stack":
            blobhash_sstore_address = (
                BlobhashContext.BLOBHASH_SSTORE.deploy_contract(
                    pre=pre, indexes=range(max_blobs_per_tx + 1)
                )
            )
            tx_to = blobhash_sstore_address
            post = {
                blobhash_sstore_address: Account(
                    storage=dict(
                        zip(
                            range(len(simple_blob_hashes)),
                            simple_blob_hashes,
                            strict=False,
                        )
                    )
                ),
            }
        case "on_max_value":
            blobhash_sstore_address = (
                BlobhashContext.BLOBHASH_SSTORE.deploy_contract(
                    pre=pre, indexes=[2**256 - 1]
                )
            )
            tx_to = blobhash_sstore_address
            post = {
                blobhash_sstore_address: Account(storage={}),
            }
        case "on_CALL" | "on_DELEGATECALL" | "on_STATICCALL" | "on_CALLCODE":
            call_context: BlobhashContext
            match test_case:
                case "on_CALL":
                    call_context = BlobhashContext.CALL
                case "on_DELEGATECALL":
                    call_context = BlobhashContext.DELEGATECALL
                case "on_STATICCALL":
                    call_context = BlobhashContext.STATICCALL
                case "on_CALLCODE":
                    call_context = BlobhashContext.CALLCODE
            call_address = call_context.deploy_contract(
                pre=pre, indexes=range(max_blobs_per_tx + 1)
            )
            tx_to = call_address
            post = {
                call_address: Account(
                    storage=dict(
                        zip(
                            range(len(simple_blob_hashes)),
                            simple_blob_hashes,
                            strict=False,
                        )
                    )
                ),
            }
        case "on_CREATE" | "on_CREATE2":
            create_context: BlobhashContext
            opcode: Op
            match test_case:
                case "on_CREATE":
                    create_context = BlobhashContext.CREATE
                    opcode = Op.CREATE
                case "on_CREATE2":
                    create_context = BlobhashContext.CREATE2
                    opcode = Op.CREATE2
            factory_address = create_context.deploy_contract(
                pre=pre, indexes=range(max_blobs_per_tx + 1)
            )
            created_contract_address = compute_create_address(
                address=factory_address,
                # the create contract will have nonce 1 for its first create
                nonce=1,
                salt=0,
                initcode=BlobhashContext.INITCODE.code(
                    indexes=range(max_blobs_per_tx + 1)
                ),
                opcode=opcode,
            )
            tx_to = factory_address
            post = {
                created_contract_address: Account(
                    storage=dict(
                        zip(
                            range(len(simple_blob_hashes)),
                            simple_blob_hashes,
                            strict=False,
                        )
                    )
                ),
            }
        case _:
            raise Exception(f"Unknown test case {test_case}")

    state_test(
        pre=pre,
        tx=Transaction(
            ty=Spec.BLOB_TX_TYPE,
            to=tx_to,
            gas_limit=500_000,
            max_fee_per_blob_gas=fork.min_base_fee_per_blob_gas() * 10,
            blob_versioned_hashes=simple_blob_hashes,
            sender=pre.fund_eoa(),
        ),
        post=post,
    )

Parametrized Test Cases

This test generates 8 parametrized test cases across 4 forks.