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

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 using multi-block tests

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, system_address_balance)

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

The blocks might overwrite the historical roots buffer, or not, depending on the timestamps, and whether they increment in multiples of HISTORICAL_ROOTS_MODULUS 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
 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
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
@pytest.mark.parametrize(
    "timestamps",
    [
        pytest.param(
            count(
                start=HISTORICAL_ROOTS_MODULUS - 5,
                step=1,
            ),
            id="buffer_wraparound",
        ),
        pytest.param(
            count(
                start=12,
                step=HISTORICAL_ROOTS_MODULUS,
            ),
            id="buffer_wraparound_overwrite",
        ),
        pytest.param(
            count(
                start=2**32,
                step=HISTORICAL_ROOTS_MODULUS,
            ),
            id="buffer_wraparound_overwrite_high_timestamp",
        ),
        pytest.param(
            count(
                start=5,
                step=HISTORICAL_ROOTS_MODULUS - 1,
            ),
            id="buffer_wraparound_no_overwrite",
        ),
        pytest.param(
            count(
                start=HISTORICAL_ROOTS_MODULUS - 3,
                step=HISTORICAL_ROOTS_MODULUS + 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: Iterable[int],
    beacon_roots: Iterable[bytes],
    block_count: int,
    tx: Transaction,
    call_gas: int,
    call_value: int,
    system_address_balance: int,
):
    """
    Tests multiple blocks where each block writes a timestamp to storage and contains one
    transaction that calls the beacon root precompile multiple times.

    The blocks might overwrite the historical roots buffer, or not, depending on the `timestamps`,
    and whether they increment in multiples of `HISTORICAL_ROOTS_MODULUS` 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,
        ),
        SYSTEM_ADDRESS: Account(
            nonce=0,
            balance=system_address_balance,
        ),
    }
    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 % HISTORICAL_ROOTS_MODULUS
        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 % HISTORICAL_ROOTS_MODULUS] == t
            )
            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(0x01 if call_valid else 0x00),
                Op.CALL(
                    call_gas,
                    BEACON_ROOT_CONTRACT_ADDRESS,
                    call_value,
                    0x00,
                    0x20,
                    0x20,
                    0x20,
                ),
            )

            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(
                    roots_storage[t % HISTORICAL_ROOTS_MODULUS] 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=BEACON_ROOT_CONTRACT_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator=0,
                    ),
                    Withdrawal(
                        address=SYSTEM_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator=1,
                    ),
                ],
            )
        )

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

test_beacon_root_transition_test(blockchain_test, timestamps, beacon_roots, block_count, tx, call_gas, call_value, system_address_balance, 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
@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_test(
    blockchain_test: BlockchainTestFiller,
    timestamps: Iterable[int],
    beacon_roots: Iterable[bytes],
    block_count: int,
    tx: Transaction,
    call_gas: int,
    call_value: int,
    system_address_balance: 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,
        ),
        SYSTEM_ADDRESS: Account(
            nonce=0,
            balance=system_address_balance,
        ),
    }
    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 % HISTORICAL_ROOTS_MODULUS

        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 % HISTORICAL_ROOTS_MODULUS] == t
            )
            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(0x01 if call_valid else 0x00),
                Op.CALL(
                    call_gas,
                    BEACON_ROOT_CONTRACT_ADDRESS,
                    call_value,
                    0x00,
                    0x20,
                    0x20,
                    0x20,
                ),
            )

            current_call_account_code += Op.SSTORE(
                current_call_account_expected_storage.store_next(
                    roots_storage[t % HISTORICAL_ROOTS_MODULUS] 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=BEACON_ROOT_CONTRACT_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator=0,
                    ),
                    Withdrawal(
                        address=SYSTEM_ADDRESS,
                        amount=1,
                        index=next(withdraw_index),
                        validator=1,
                    ),
                ],
            )
        )

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