Skip to content

Commit b37336d

Browse files
bshastryclaude
andauthored
test(tests): add BLOCKHASH genesis hash availability test (#2228)
* test(tests): add BLOCKHASH genesis hash availability test Add regression test verifying BLOCKHASH(0) returns genesis hash in block 1. Tests blockchain test infrastructure properly populates genesis hash before execution, preventing BLOCKHASH(0) from returning 0. Regression context: revm blockchaintest runner wasn't inserting block_hashes into state, causing BLOCKHASH(0) to return 0. This broke tests with dynamic address computations like BLOCKHASH(0) | TIMESTAMP, where the computed address would be incorrect, leading to balance transfer failures. Test validates infrastructure setup by storing ISZERO(BLOCKHASH(0)) which should be 0 (false) when genesis hash is properly available. * test(tests): extend BLOCKHASH test to verify block 1 hash availability Extends test_genesis_hash_available to verify both genesis (block 0) and first executed block (block 1) hash insertion by adding a second block that calls the contract, testing BLOCKHASH(0) and BLOCKHASH(1). Addresses PR feedback to test complete block hash infrastructure. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent 8d3a51e commit b37336d

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)