Skip to content

test_block_hashes_history()

Documentation for tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py::test_block_hashes_history@49a16fac.

Generate fixtures for these test cases for Prague with:

fill -v tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py::test_block_hashes_history --fork Prague

Tests that block hashes are stored correctly at the system contract address after the fork transition. Block hashes are stored incrementally at the transition until the HISTORY_SERVE_WINDOW ring buffer is full. Afterwards the oldest block hash is replaced by the new one.

Source code in tests/prague/eip2935_historical_block_hashes_from_state/test_block_hashes.py
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
@pytest.mark.parametrize(
    "block_count,check_contract_first",
    [
        pytest.param(1, False, id="single_block_check_blockhash_first"),
        pytest.param(1, True, id="single_block_check_contract_first"),
        pytest.param(2, False, id="two_blocks_check_blockhash_first"),
        pytest.param(2, True, id="two_blocks_check_contract_first"),
        pytest.param(
            Spec.HISTORY_SERVE_WINDOW + 1,
            False,
            marks=pytest.mark.slow,
            id="full_history_plus_one_check_blockhash_first",
        ),
    ],
)
@pytest.mark.valid_from("Prague")
def test_block_hashes_history(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    block_count: int,
    check_contract_first: bool,
):
    """
    Tests that block hashes are stored correctly at the system contract address after the fork
    transition. Block hashes are stored incrementally at the transition until the
    `HISTORY_SERVE_WINDOW` ring buffer is full. Afterwards the oldest block hash is replaced by the
    new one.
    """
    blocks: List[Block] = []

    sender = pre.fund_eoa(10_000_000_000)
    post: Dict[Address, Account] = {}
    current_block_number = 1
    fork_block_number = 0  # We fork at genesis

    for _ in range(block_count - 1):
        # Generate empty blocks after the fork.
        blocks.append(Block())
        current_block_number += 1

    txs = []
    # On these blocks, `BLOCKHASH` will still return values for the last 256 blocks, and
    # `HISTORY_STORAGE_ADDRESS` should now serve values for the previous blocks in the new
    # fork.
    code = Bytecode()
    storage = Storage()

    # Check the first block outside of the window if any
    code += generate_block_check_code(
        check_block_number=current_block_number - Spec.HISTORY_SERVE_WINDOW - 1,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    # Check the first block inside the window
    code += generate_block_check_code(
        check_block_number=current_block_number - Spec.HISTORY_SERVE_WINDOW,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    # Check the first block outside the BLOCKHASH window
    code += generate_block_check_code(
        check_block_number=current_block_number - Spec.BLOCKHASH_OLD_WINDOW - 1,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    # Check the first block inside the BLOCKHASH window
    code += generate_block_check_code(
        check_block_number=current_block_number - Spec.BLOCKHASH_OLD_WINDOW,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    # Check the previous block
    code += generate_block_check_code(
        check_block_number=current_block_number - 1,
        current_block_number=current_block_number,
        fork_block_number=fork_block_number,
        storage=storage,
        check_contract_first=check_contract_first,
    )

    check_blocks_after_fork_address = pre.deploy_contract(code)
    txs.append(
        Transaction(
            to=check_blocks_after_fork_address,
            gas_limit=10_000_000,
            sender=sender,
        )
    )
    post[check_blocks_after_fork_address] = Account(storage=storage)

    blocks.append(Block(txs=txs))
    current_block_number += 1

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

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) block_count check_contract_first
...fork_Prague-blockchain_test-single_block_check_blockhash_first 1 False
...fork_Prague-blockchain_test-single_block_check_contract_first 1 True
...fork_Prague-blockchain_test-two_blocks_check_blockhash_first 2 False
...fork_Prague-blockchain_test-two_blocks_check_contract_first 2 True
...fork_Prague-blockchain_test-full_history_plus_one_check_blockhash_first 8192 False
...fork_Osaka-blockchain_test-single_block_check_blockhash_first 1 False
...fork_Osaka-blockchain_test-single_block_check_contract_first 1 True
...fork_Osaka-blockchain_test-two_blocks_check_blockhash_first 2 False
...fork_Osaka-blockchain_test-two_blocks_check_contract_first 2 True
...fork_Osaka-blockchain_test-full_history_plus_one_check_blockhash_first 8192 False