Skip to content

Test Beacon Root Contract

Documentation for tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py.

Generate fixtures for these test cases with:

fill -v tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
Tests beacon block root for EIP-4788: Beacon block root in the EVM

Test the exposed beacon chain root in the EVM for EIP-4788: Beacon block root in the EVM

Adding a new test

Add a function that is named test_<test_name> and takes at least the following arguments:

  • state_test
  • env
  • pre
  • tx
  • post
  • valid_call

All other pytest.fixtures can be parametrized to generate new combinations and test cases.

test_beacon_root_contract_calls(blockchain_test, beacon_root, timestamp, pre, tx, post)

Tests the beacon root contract call using various call contexts: - CALL - DELEGATECALL - CALLCODE - STATICCALL for different call gas amounts: - exact gas (valid call) - extra gas (valid call) - insufficient gas (invalid call)

The expected result is that the contract call will be executed if the gas amount is met and return the correctparent_beacon_block_root. Otherwise the call will be invalid, and not be executed. This is highlighted within storage by storing the return value of each call context.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
@pytest.mark.parametrize(
    "call_gas, valid_call",
    [
        pytest.param(Spec.BEACON_ROOTS_CALL_GAS, True),
        pytest.param(int(Spec.BEACON_ROOTS_CALL_GAS / 100), False),
    ],
)
@pytest.mark.parametrize(
    "call_type,call_value,valid_input",
    [
        (Op.CALL, 1, True),
        (Op.CALL, 0, True),
        (Op.CALLCODE, 0, False),
        (Op.DELEGATECALL, 0, False),
        (Op.STATICCALL, 0, True),
    ],
)
@pytest.mark.valid_from("Cancun")
def test_beacon_root_contract_calls(
    blockchain_test: BlockchainTestFiller,
    beacon_root: bytes,
    timestamp: int,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Tests the beacon root contract call using various call contexts:
    - `CALL`
    - `DELEGATECALL`
    - `CALLCODE`
    - `STATICCALL`
    for different call gas amounts:
    - exact gas (valid call)
    - extra gas (valid call)
    - insufficient gas (invalid call)

    The expected result is that the contract call will be executed if the gas amount is met
    and return the correct`parent_beacon_block_root`. Otherwise the call will be invalid, and not
    be executed. This is highlighted within storage by storing the return value of each call
    context.
    """
    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], parent_beacon_block_root=beacon_root, timestamp=timestamp)],
        post=post,
    )

test_beacon_root_contract_timestamps(blockchain_test, beacon_root, timestamp, pre, tx, post)

Tests the beacon root contract call across for various valid and invalid timestamps.

The expected result is that the contract call will return the correct parent_beacon_block_root for a valid input timestamp and return the zero'd 32 bytes value for an invalid input timestamp.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.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
135
136
137
138
@pytest.mark.parametrize(
    "timestamp, valid_input",
    [
        (0x0C, True),  # twelve
        (2**32, True),  # arbitrary
        (2**64 - 2, True),  # near-max
        (2**64 - 1, True),  # max
        # TODO: Update t8n to un marshal > 64-bit int
        # Exception: failed to evaluate: ERROR(10): failed un marshaling stdin
        # (2**64, False),  # overflow
        # Exception: failed to evaluate: ERROR(10): failed un marshaling stdin
        # (2**64 + 1, False),  # overflow+1
    ],
)
@pytest.mark.parametrize("auto_access_list", [False, True])
@pytest.mark.parametrize(
    "system_address_balance",
    [
        pytest.param(0, id="empty_system_address"),
        pytest.param(1, id="one_wei_system_address"),
        pytest.param(int(1e18), id="one_eth_system_address"),
    ],
)
@pytest.mark.valid_from("Cancun")
def test_beacon_root_contract_timestamps(
    blockchain_test: BlockchainTestFiller,
    beacon_root: bytes,
    timestamp: int,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Tests the beacon root contract call across for various valid and invalid timestamps.

    The expected result is that the contract call will return the correct
    `parent_beacon_block_root` for a valid input timestamp and return the zero'd 32 bytes value
    for an invalid input timestamp.
    """
    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], parent_beacon_block_root=beacon_root, timestamp=timestamp)],
        post=post,
    )

test_calldata_lengths(blockchain_test, beacon_root, timestamp, pre, tx, post)

Tests the beacon root contract call using multiple invalid input lengths.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
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
@pytest.mark.parametrize(
    "tx_data",
    [
        pytest.param(bytes(), id="empty_calldata"),
        pytest.param(int.to_bytes(12, length=1, byteorder="big"), id="one_byte"),
        pytest.param(int.to_bytes(12, length=31, byteorder="big"), id="31_bytes"),
        pytest.param(int.to_bytes(12, length=33, byteorder="big"), id="33_bytes"),
        pytest.param(int.to_bytes(12, length=1024, byteorder="big"), id="1024_bytes"),
    ],
)
@pytest.mark.parametrize("valid_call,valid_input", [(False, False)])
@pytest.mark.parametrize("timestamp", [12])
@pytest.mark.valid_from("Cancun")
def test_calldata_lengths(
    blockchain_test: BlockchainTestFiller,
    beacon_root: bytes,
    timestamp: int,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Tests the beacon root contract call using multiple invalid input lengths.
    """
    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], parent_beacon_block_root=beacon_root, timestamp=timestamp)],
        post=post,
    )

test_beacon_root_equal_to_timestamp(blockchain_test, beacon_root, timestamp, pre, tx, post)

Tests the beacon root contract call where the beacon root is equal to the timestamp.

The expected result is that the contract call will return the parent_beacon_block_root, as all timestamps used are valid.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
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
@pytest.mark.parametrize(
    "beacon_root, timestamp",
    [
        (12, 12),  # twelve
        (2**32, 2**32),  # arbitrary
        (2**64 - 2, 2**64 - 2),  # near-max
        (2**64 - 1, 2**64 - 1),  # max
    ],
    indirect=["beacon_root"],
)
@pytest.mark.parametrize("auto_access_list", [False, True])
@pytest.mark.valid_from("Cancun")
def test_beacon_root_equal_to_timestamp(
    blockchain_test: BlockchainTestFiller,
    beacon_root: bytes,
    timestamp: int,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Tests the beacon root contract call where the beacon root is equal to the timestamp.

    The expected result is that the contract call will return the `parent_beacon_block_root`,
    as all timestamps used are valid.
    """
    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], parent_beacon_block_root=beacon_root, timestamp=timestamp)],
        post=post,
    )

test_tx_to_beacon_root_contract(blockchain_test, beacon_root, timestamp, pre, tx, post)

Tests the beacon root contract using a transaction with different types and data lengths.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
@pytest.mark.parametrize("auto_access_list", [False, True])
@pytest.mark.parametrize("call_beacon_root_contract", [True])
@pytest.mark.with_all_tx_types
@pytest.mark.valid_from("Cancun")
def test_tx_to_beacon_root_contract(
    blockchain_test: BlockchainTestFiller,
    beacon_root: bytes,
    timestamp: int,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Tests the beacon root contract using a transaction with different types and data lengths.
    """
    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], parent_beacon_block_root=beacon_root, timestamp=timestamp)],
        post=post,
    )

test_invalid_beacon_root_calldata_value(blockchain_test, beacon_root, timestamp, pre, tx, post)

Tests the beacon root contract call using invalid input values: - zero calldata.

Contract should revert.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.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
@pytest.mark.parametrize(
    "tx_data",
    [
        pytest.param(int.to_bytes(0, length=32, byteorder="big"), id="zero_calldata"),
    ],
)
@pytest.mark.parametrize("valid_call,valid_input", [(False, False)])
@pytest.mark.parametrize("timestamp", [12])
@pytest.mark.valid_from("Cancun")
def test_invalid_beacon_root_calldata_value(
    blockchain_test: BlockchainTestFiller,
    beacon_root: bytes,
    timestamp: int,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Tests the beacon root contract call using invalid input values:
    - zero calldata.

    Contract should revert.
    """
    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], parent_beacon_block_root=beacon_root, timestamp=timestamp)],
        post=post,
    )

test_beacon_root_selfdestruct(blockchain_test, beacon_root, timestamp, pre, tx, post)

Tests that self destructing the beacon root address transfers actors balance correctly.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
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
@pytest.mark.parametrize("timestamp", [12])
@pytest.mark.valid_from("Cancun")
def test_beacon_root_selfdestruct(
    blockchain_test: BlockchainTestFiller,
    beacon_root: bytes,
    timestamp: int,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Tests that self destructing the beacon root address transfers actors balance correctly.
    """
    # self destruct actor
    pre[Address(0x1337)] = Account(
        code=Op.SELFDESTRUCT(Spec.BEACON_ROOTS_ADDRESS),
        balance=0xBA1,
    )
    # self destruct caller
    pre[Address(0xCC)] = Account(
        code=Op.CALL(100000, Address(0x1337), 0, 0, 0, 0, 0)
        + Op.SSTORE(0, Op.BALANCE(Spec.BEACON_ROOTS_ADDRESS)),
    )
    post = {
        Address(0xCC): Account(
            storage=Storage({0: 0xBA1}),  # type: ignore
        )
    }
    blockchain_test(
        pre=pre,
        blocks=[
            Block(txs=[Transaction(nonce=0, to=Address(0xCC), gas_limit=100000, gas_price=10)])
        ],
        post=post,
    )

test_multi_block_beacon_root_timestamp_calls(blockchain_test, timestamps, beacon_roots, block_count, tx, call_gas, call_value)

Tests multiple blocks where each block writes a timestamp to storage and contains one transaction that calls the beacon root contract multiple times.

The blocks might overwrite the historical roots buffer, or not, depending on the timestamps, and whether they increment in multiples of Spec.HISTORY_BUFFER_LENGTH or not.

By default, the beacon roots are the keccak of the block number.

Each transaction checks the current timestamp and also all previous timestamps, and verifies that the beacon root is correct for all of them if the timestamp is supposed to be in the buffer, which might have been overwritten by a later block.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
@pytest.mark.parametrize(
    "timestamps",
    [
        pytest.param(
            count(
                start=Spec.HISTORY_BUFFER_LENGTH - 5,
                step=1,
            ),
            id="buffer_wraparound",
        ),
        pytest.param(
            count(
                start=12,
                step=Spec.HISTORY_BUFFER_LENGTH,
            ),
            id="buffer_wraparound_overwrite",
        ),
        pytest.param(
            count(
                start=2**32,
                step=Spec.HISTORY_BUFFER_LENGTH,
            ),
            id="buffer_wraparound_overwrite_high_timestamp",
        ),
        pytest.param(
            count(
                start=5,
                step=Spec.HISTORY_BUFFER_LENGTH - 1,
            ),
            id="buffer_wraparound_no_overwrite",
        ),
        pytest.param(
            count(
                start=Spec.HISTORY_BUFFER_LENGTH - 3,
                step=Spec.HISTORY_BUFFER_LENGTH + 1,
            ),
            id="buffer_wraparound_no_overwrite_2",
        ),
    ],
)
@pytest.mark.parametrize("block_count", [10])  # All tests use 10 blocks
@pytest.mark.valid_from("Cancun")
def test_multi_block_beacon_root_timestamp_calls(
    blockchain_test: BlockchainTestFiller,
    timestamps: Iterator[int],
    beacon_roots: Iterator[bytes],
    block_count: int,
    tx: Transaction,
    call_gas: int,
    call_value: int,
):
    """
    Tests multiple blocks where each block writes a timestamp to storage and contains one
    transaction that calls the beacon root contract multiple times.

    The blocks might overwrite the historical roots buffer, or not, depending on the `timestamps`,
    and whether they increment in multiples of `Spec.HISTORY_BUFFER_LENGTH` or not.

    By default, the beacon roots are the keccak of the block number.

    Each transaction checks the current timestamp and also all previous timestamps, and verifies
    that the beacon root is correct for all of them if the timestamp is supposed to be in the
    buffer, which might have been overwritten by a later block.
    """
    blocks: List[Block] = []
    pre = {
        TestAddress: Account(
            nonce=0,
            balance=0x10**10,
        ),
    }
    post = {}

    timestamps_storage: Dict[int, int] = {}
    roots_storage: Dict[int, bytes] = {}

    all_timestamps: List[int] = []

    for timestamp, beacon_root, i in zip(timestamps, beacon_roots, range(block_count)):
        timestamp_index = timestamp % Spec.HISTORY_BUFFER_LENGTH
        timestamps_storage[timestamp_index] = timestamp
        roots_storage[timestamp_index] = beacon_root

        all_timestamps.append(timestamp)

        withdraw_index = count(0)

        current_call_account_code = Bytecode()
        current_call_account_expected_storage = Storage()
        current_call_account_address = Address(0x100 + i)

        # We are going to call the beacon roots contract once for every timestamp of the current
        # and all previous blocks, and check that the returned beacon root is still correct only
        # if it was not overwritten.
        for t in all_timestamps:
            current_call_account_code += Op.MSTORE(0, t)
            call_valid = (
                timestamp_index in timestamps_storage
                and timestamps_storage[t % Spec.HISTORY_BUFFER_LENGTH] == t
            )
            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(0x01 if call_valid else 0x00),
                Op.CALL(
                    call_gas,
                    Spec.BEACON_ROOTS_ADDRESS,
                    call_value,
                    0x00,
                    0x20,
                    0x20,
                    0x20,
                ),
            )

            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(
                    roots_storage[t % Spec.HISTORY_BUFFER_LENGTH] if call_valid else 0x00
                ),
                Op.MLOAD(0x20),
            )

        pre[current_call_account_address] = Account(
            code=current_call_account_code,
        )
        post[current_call_account_address] = Account(
            storage=current_call_account_expected_storage,
        )
        blocks.append(
            Block(
                txs=[
                    tx.copy(
                        nonce=i,
                        to=Address(0x100 + i),
                        data=Hash(timestamp),
                    )
                ],
                parent_beacon_block_root=beacon_root,
                timestamp=timestamp,
                withdrawals=[
                    # Also withdraw to the beacon root contract and the system address
                    Withdrawal(
                        address=Spec.BEACON_ROOTS_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator_index=0,
                    ),
                    Withdrawal(
                        address=Spec.SYSTEM_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator_index=1,
                    ),
                ],
            )
        )

    blockchain_test(
        pre=pre,
        blocks=blocks,
        post=post,
    )

test_beacon_root_transition(blockchain_test, timestamps, beacon_roots, block_count, tx, call_gas, call_value, fork)

Tests the fork transition to cancun and verifies that blocks with timestamp lower than the transition timestamp do not contain beacon roots in the pre-deployed contract.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
@pytest.mark.parametrize(
    "timestamps",
    [pytest.param(count(start=1000, step=1000), id="fork_transition")],
)
@pytest.mark.parametrize("block_count", [20])
@pytest.mark.valid_at_transition_to("Cancun")
def test_beacon_root_transition(
    blockchain_test: BlockchainTestFiller,
    timestamps: Iterator[int],
    beacon_roots: Iterator[bytes],
    block_count: int,
    tx: Transaction,
    call_gas: int,
    call_value: int,
    fork: Fork,
):
    """
    Tests the fork transition to cancun and verifies that blocks with timestamp lower than the
    transition timestamp do not contain beacon roots in the pre-deployed contract.
    """
    blocks: List[Block] = []
    pre = {
        TestAddress: Account(
            nonce=0,
            balance=0x10**10,
        ),
    }
    post = {}

    timestamps_storage: Dict[int, int] = {}
    roots_storage: Dict[int, bytes] = {}

    all_timestamps: List[int] = []
    timestamps_in_beacon_root_contract: List[int] = []

    for timestamp, beacon_root, i in zip(timestamps, beacon_roots, range(block_count)):
        timestamp_index = timestamp % Spec.HISTORY_BUFFER_LENGTH

        transitioned = fork.header_beacon_root_required(i, timestamp)
        if transitioned:
            # We've transitioned, the current timestamp must contain a value in the contract
            timestamps_in_beacon_root_contract.append(timestamp)
            timestamps_storage[timestamp_index] = timestamp
            roots_storage[timestamp_index] = beacon_root

        all_timestamps.append(timestamp)

        withdraw_index = count(0)

        current_call_account_code = Bytecode()
        current_call_account_expected_storage = Storage()
        current_call_account_address = Address(0x100 + i)

        # We are going to call the beacon roots contract once for every timestamp of the current
        # and all previous blocks, and check that the returned beacon root is correct only
        # if it was after the transition timestamp.
        for t in all_timestamps:
            current_call_account_code += Op.MSTORE(0, t)
            call_valid = (
                t in timestamps_in_beacon_root_contract
                and timestamp_index in timestamps_storage
                and timestamps_storage[t % Spec.HISTORY_BUFFER_LENGTH] == t
            )
            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(0x01 if call_valid else 0x00),
                Op.CALL(
                    call_gas,
                    Spec.BEACON_ROOTS_ADDRESS,
                    call_value,
                    0x00,
                    0x20,
                    0x20,
                    0x20,
                ),
            )

            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(
                    roots_storage[t % Spec.HISTORY_BUFFER_LENGTH] if call_valid else 0x00
                ),
                Op.MLOAD(0x20),
            )

        pre[current_call_account_address] = Account(
            code=current_call_account_code,
        )
        post[current_call_account_address] = Account(
            storage=current_call_account_expected_storage,
        )
        blocks.append(
            Block(
                txs=[
                    tx.copy(
                        nonce=i,
                        to=Address(0x100 + i),
                        data=Hash(timestamp),
                    )
                ],
                parent_beacon_block_root=beacon_root if transitioned else None,
                timestamp=timestamp,
                withdrawals=[
                    # Also withdraw to the beacon root contract and the system address
                    Withdrawal(
                        address=Spec.BEACON_ROOTS_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator_index=0,
                    ),
                    Withdrawal(
                        address=Spec.SYSTEM_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator_index=1,
                    ),
                ],
            )
        )

    blockchain_test(
        pre=pre,
        blocks=blocks,
        post=post,
    )

test_no_beacon_root_contract_at_transition(blockchain_test, pre, beacon_roots, tx, timestamp, caller_address, fork)

Tests the fork transition to cancun in the case where the beacon root pre-deploy was not deployed in time for the fork.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
@pytest.mark.parametrize("timestamp", [15_000])
@pytest.mark.valid_at_transition_to("Cancun")
def test_no_beacon_root_contract_at_transition(
    blockchain_test: BlockchainTestFiller,
    pre: Dict,
    beacon_roots: Iterator[bytes],
    tx: Transaction,
    timestamp: int,
    caller_address: Address,
    fork: Fork,
):
    """
    Tests the fork transition to cancun in the case where the beacon root pre-deploy was not
    deployed in time for the fork.
    """
    assert fork.header_beacon_root_required(1, timestamp)
    blocks: List[Block] = [
        Block(
            txs=[tx],
            parent_beacon_block_root=next(beacon_roots),
            timestamp=timestamp,
            withdrawals=[
                # Also withdraw to the beacon root contract and the system address
                Withdrawal(
                    address=Spec.BEACON_ROOTS_ADDRESS,
                    amount=1,
                    index=0,
                    validator_index=0,
                ),
                Withdrawal(
                    address=Spec.SYSTEM_ADDRESS,
                    amount=1,
                    index=1,
                    validator_index=1,
                ),
            ],
        )
    ]
    pre[Spec.BEACON_ROOTS_ADDRESS] = Account(
        code=b"",  # Remove the code that is automatically allocated on Cancun fork
        nonce=0,
        balance=0,
    )
    post = {
        Spec.BEACON_ROOTS_ADDRESS: Account(
            storage={
                timestamp % Spec.HISTORY_BUFFER_LENGTH: 0,
                (timestamp % Spec.HISTORY_BUFFER_LENGTH) + Spec.HISTORY_BUFFER_LENGTH: 0,
            },
            code=b"",
            nonce=0,
            balance=int(1e9),
        ),
        caller_address: Account(
            storage={
                0: 1
            },  # Successful call because the contract is not there, but nothing else is stored
        ),
    }
    blockchain_test(
        pre=pre,
        blocks=blocks,
        post=post,
    )

test_beacon_root_contract_deploy(blockchain_test, pre, beacon_root, tx, timestamp, post, fork)

Tests the fork transition to cancun deploying the contract during Shanghai and verifying the code deployed and its functionality after Cancun.

Source code in tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
@pytest.mark.parametrize(
    "timestamp",
    [
        pytest.param(15_000, id="deploy_on_shanghai"),
        pytest.param(30_000, id="deploy_on_cancun"),
    ],
)
@pytest.mark.valid_at_transition_to("Cancun")
def test_beacon_root_contract_deploy(
    blockchain_test: BlockchainTestFiller,
    pre: Dict,
    beacon_root: bytes,
    tx: Transaction,
    timestamp: int,
    post: Dict,
    fork: Fork,
):
    """
    Tests the fork transition to cancun deploying the contract during Shanghai and verifying the
    code deployed and its functionality after Cancun.
    """
    assert fork.header_beacon_root_required(1, timestamp)
    tx_gas_limit = 0x3D090
    tx_gas_price = 0xE8D4A51000
    deployer_required_balance = tx_gas_limit * tx_gas_price
    deploy_tx = Transaction(
        ty=0,
        nonce=0,
        to=None,
        gas_limit=tx_gas_limit,
        gas_price=tx_gas_price,
        value=0,
        data=bytes.fromhex(
            "60618060095f395ff33373fffffffffffffffffffffffffffffffffffffffe14604d576020361460"
            "24575f5ffd5b5f35801560495762001fff810690815414603c575f5ffd5b62001fff01545f526020"
            "5ff35b5f5ffd5b62001fff42064281555f359062001fff015500"
        ),
        v=0x1B,
        r=0x539,
        s=0x1B9B6EB1F0,
        protected=False,
    ).with_signature_and_sender()
    deployer_address = deploy_tx.sender
    assert deployer_address is not None
    assert Address(deployer_address) == Spec.BEACON_ROOTS_DEPLOYER_ADDRESS
    blocks: List[Block] = []

    beacon_root_contract_storage: Dict = {}
    for i, current_timestamp in enumerate(range(timestamp // 2, timestamp + 1, timestamp // 2)):
        if i == 0:
            blocks.append(
                Block(  # Deployment block
                    txs=[deploy_tx],
                    parent_beacon_block_root=(
                        beacon_root
                        if fork.header_beacon_root_required(1, current_timestamp)
                        else None
                    ),
                    timestamp=timestamp // 2,
                    withdrawals=[
                        # Also withdraw to the beacon root contract and the system address
                        Withdrawal(
                            address=Spec.BEACON_ROOTS_ADDRESS,
                            amount=1,
                            index=0,
                            validator_index=0,
                        ),
                        Withdrawal(
                            address=Spec.SYSTEM_ADDRESS,
                            amount=1,
                            index=1,
                            validator_index=1,
                        ),
                    ],
                )
            )
            beacon_root_contract_storage[current_timestamp % Spec.HISTORY_BUFFER_LENGTH] = 0
            beacon_root_contract_storage[
                (current_timestamp % Spec.HISTORY_BUFFER_LENGTH) + Spec.HISTORY_BUFFER_LENGTH
            ] = 0
        elif i == 1:
            blocks.append(
                Block(  # Contract already deployed
                    txs=[tx],
                    parent_beacon_block_root=beacon_root,
                    timestamp=timestamp,
                    withdrawals=[
                        # Also withdraw to the beacon root contract and the system address
                        Withdrawal(
                            address=Spec.BEACON_ROOTS_ADDRESS,
                            amount=1,
                            index=2,
                            validator_index=0,
                        ),
                        Withdrawal(
                            address=Spec.SYSTEM_ADDRESS,
                            amount=1,
                            index=3,
                            validator_index=1,
                        ),
                    ],
                ),
            )
            beacon_root_contract_storage[
                current_timestamp % Spec.HISTORY_BUFFER_LENGTH
            ] = current_timestamp
            beacon_root_contract_storage[
                (current_timestamp % Spec.HISTORY_BUFFER_LENGTH) + Spec.HISTORY_BUFFER_LENGTH
            ] = beacon_root
        else:
            assert False, "This test should only have two blocks"

    expected_code = fork.pre_allocation_blockchain()[Spec.BEACON_ROOTS_ADDRESS]["code"]
    pre[Spec.BEACON_ROOTS_ADDRESS] = Account(
        code=b"",  # Remove the code that is automatically allocated on Cancun fork
        nonce=0,
        balance=0,
    )
    pre[deployer_address] = Account(
        balance=deployer_required_balance,
    )

    post[Spec.BEACON_ROOTS_ADDRESS] = Account(
        storage=beacon_root_contract_storage,
        code=expected_code,
        nonce=1,
        balance=int(2e9),
    )
    post[Spec.SYSTEM_ADDRESS] = Account(
        storage={},
        code=b"",
        nonce=0,
        balance=int(2e9),
    )
    post[deployer_address] = Account(
        balance=175916000000000000,  # It doesn't consume all the balance :(
        nonce=1,
    )
    blockchain_test(
        pre=pre,
        blocks=blocks,
        post=post,
    )