Skip to content

Test Blocks Beacon Root Contract

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

Generate fixtures for these test cases for Cancun with:

Cancun only:

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

abstract: 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](https://eips.ethereum.org/EIPS/eip-4788) using multi-block tests

note: Adding a new test

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

- blockchain_test
- env
- pre
- blocks
- post
- valid_call

The following arguments *need* to be parametrized or the test will not be generated:

-

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

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_blocks_beacon_root_contract.py
 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
 93
 94
 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
139
140
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
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
@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 = bytes()
        current_call_account_expected_storage = Storage()
        current_call_account_address = to_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.with_fields(
                        nonce=i,
                        to=to_address(0x100 + i),
                        data=to_hash_bytes(timestamp),
                    )
                ],
                beacon_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=0,
                    ),
                    Withdrawal(
                        address=Spec.SYSTEM_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator=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_blocks_beacon_root_contract.py
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
@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 = bytes()
        current_call_account_expected_storage = Storage()
        current_call_account_address = to_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.with_fields(
                        nonce=i,
                        to=to_address(0x100 + i),
                        data=to_hash_bytes(timestamp),
                    )
                ],
                beacon_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=0,
                    ),
                    Withdrawal(
                        address=Spec.SYSTEM_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator=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_blocks_beacon_root_contract.py
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
@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: str,
    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],
            beacon_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=0,
                ),
                Withdrawal(
                    address=Spec.SYSTEM_ADDRESS,
                    amount=1,
                    index=1,
                    validator=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_blocks_beacon_root_contract.py
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
454
455
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
@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 deployer_address == int.to_bytes(Spec.BEACON_ROOTS_DEPLOYER_ADDRESS, 20, "big")
    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],
                    beacon_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=0,
                        ),
                        Withdrawal(
                            address=Spec.SYSTEM_ADDRESS,
                            amount=1,
                            index=1,
                            validator=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],
                    beacon_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=0,
                        ),
                        Withdrawal(
                            address=Spec.SYSTEM_ADDRESS,
                            amount=1,
                            index=3,
                            validator=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(1, timestamp)[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,
    )