|
| 1 | +"""Tests for BLOCKHASH opcode.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ethereum_test_tools import ( |
| 6 | + Account, |
| 7 | + Alloc, |
| 8 | + Block, |
| 9 | + BlockchainTestFiller, |
| 10 | + Storage, |
| 11 | + Transaction, |
| 12 | +) |
| 13 | +from ethereum_test_tools import Opcodes as Op |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.valid_from("Frontier") |
| 17 | +def test_genesis_hash_available(blockchain_test: BlockchainTestFiller, pre: Alloc): |
| 18 | + """ |
| 19 | + Verify BLOCKHASH returns genesis and block 1 hashes. |
| 20 | +
|
| 21 | + Regression test: Blockchain test infrastructure must populate block hashes |
| 22 | + before execution. Without this, BLOCKHASH returns 0, breaking dynamic |
| 23 | + address computations like BLOCKHASH(0) | TIMESTAMP. |
| 24 | +
|
| 25 | + Tests both genesis (block 0) and first executed block (block 1) hash |
| 26 | + insertion by calling the contract in block 2. |
| 27 | +
|
| 28 | + Bug context: revm blockchaintest runner wasn't inserting block_hashes, |
| 29 | + causing failures in tests with BLOCKHASH-derived addresses. |
| 30 | + """ |
| 31 | + storage = Storage() |
| 32 | + |
| 33 | + # Store ISZERO(BLOCKHASH(0)) and ISZERO(BLOCKHASH(1)) |
| 34 | + # Both should be 0 (false) if hashes exist |
| 35 | + code = Op.SSTORE(storage.store_next(0), Op.ISZERO(Op.BLOCKHASH(0))) + Op.SSTORE( |
| 36 | + storage.store_next(0), Op.ISZERO(Op.BLOCKHASH(1)) |
| 37 | + ) |
| 38 | + |
| 39 | + contract = pre.deploy_contract(code=code) |
| 40 | + sender = pre.fund_eoa() |
| 41 | + |
| 42 | + blocks = [ |
| 43 | + Block( |
| 44 | + txs=[ |
| 45 | + Transaction( |
| 46 | + sender=sender, |
| 47 | + to=contract, |
| 48 | + gas_limit=100_000, |
| 49 | + protected=False, |
| 50 | + ) |
| 51 | + ] |
| 52 | + ), |
| 53 | + Block( |
| 54 | + txs=[ |
| 55 | + Transaction( |
| 56 | + sender=sender, |
| 57 | + to=contract, |
| 58 | + gas_limit=100_000, |
| 59 | + protected=False, |
| 60 | + ) |
| 61 | + ] |
| 62 | + ), |
| 63 | + ] |
| 64 | + |
| 65 | + post = { |
| 66 | + contract: Account( |
| 67 | + storage={ |
| 68 | + 0: 0, # ISZERO(BLOCKHASH(0)) = 0 (genesis hash exists) |
| 69 | + 1: 0, # ISZERO(BLOCKHASH(1)) = 0 (block 1 hash exists) |
| 70 | + } |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + blockchain_test(pre=pre, post=post, blocks=blocks) |
0 commit comments