Skip to content

Commit eab85da

Browse files
authored
Merge pull request #256 from spencer-tb/forkchoice-in-fixtures
feature: Add engine API forkchoice updated information in fixtures
2 parents 805980d + 150d8cb commit eab85da

File tree

8 files changed

+47
-6
lines changed

8 files changed

+47
-6
lines changed

src/ethereum_test_forks/base_fork.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ def engine_new_payload_beacon_root(cls, block_number: int = 0, timestamp: int =
174174
"""
175175
pass
176176

177+
@classmethod
178+
@abstractmethod
179+
def engine_forkchoice_updated_version(
180+
cls, block_number: int = 0, timestamp: int = 0
181+
) -> Optional[int]:
182+
"""
183+
Returns `None` if the forks canonical chain cannot be set using the forkchoice method.
184+
"""
185+
pass
186+
177187
# Meta information about the fork
178188
@classmethod
179189
def name(cls) -> str:

src/ethereum_test_forks/forks/forks.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ def engine_new_payload_beacon_root(cls, block_number: int = 0, timestamp: int =
9191
"""
9292
return False
9393

94+
@classmethod
95+
def engine_forkchoice_updated_version(
96+
cls, block_number: int = 0, timestamp: int = 0
97+
) -> Optional[int]:
98+
"""
99+
At genesis, forkchoice updates cannot be sent through the engine API.
100+
"""
101+
return cls.engine_new_payload_version(block_number, timestamp)
102+
94103
@classmethod
95104
def get_reward(cls, block_number: int = 0, timestamp: int = 0) -> int:
96105
"""

src/ethereum_test_tools/common/types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,11 @@ class Fixture:
27072707
to_json=True,
27082708
),
27092709
)
2710+
fcu_version: Optional[int] = field(
2711+
json_encoder=JSONEncoder.Field(
2712+
name="engineFcuVersion",
2713+
),
2714+
)
27102715
genesis: FixtureHeader = field(
27112716
json_encoder=JSONEncoder.Field(
27122717
name="genesisBlockHeader",

src/ethereum_test_tools/filling/fill.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def fill_test(
2626

2727
pre, genesis_rlp, genesis = test_spec.make_genesis(t8n, fork)
2828

29-
(blocks, head, alloc) = test_spec.make_blocks(
29+
(blocks, head, alloc, fcu_version) = test_spec.make_blocks(
3030
t8n,
3131
genesis,
3232
pre,
@@ -45,6 +45,7 @@ def fill_test(
4545
post_state=alloc_to_accounts(alloc),
4646
seal_engine=engine,
4747
name=test_spec.tag,
48+
fcu_version=fcu_version,
4849
)
4950
fixture.fill_info(t8n, spec)
5051

src/ethereum_test_tools/spec/base_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def make_blocks(
127127
fork: Fork,
128128
chain_id: int = 1,
129129
eips: Optional[List[int]] = None,
130-
) -> Tuple[List[FixtureBlock], Hash, Dict[str, Any]]:
130+
) -> Tuple[List[FixtureBlock], Hash, Dict[str, Any], Optional[int]]:
131131
"""
132132
Generate the blockchain that must be executed sequentially during test.
133133
"""

src/ethereum_test_tools/spec/blockchain_test.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def make_blocks(
264264
fork: Fork,
265265
chain_id=1,
266266
eips: Optional[List[int]] = None,
267-
) -> Tuple[List[FixtureBlock], Hash, Dict[str, Any]]:
267+
) -> Tuple[List[FixtureBlock], Hash, Dict[str, Any], Optional[int]]:
268268
"""
269269
Create a block list from the blockchain test definition.
270270
Performs checks against the expected behavior of the test.
@@ -273,6 +273,9 @@ def make_blocks(
273273
alloc = to_json(pre)
274274
env = Environment.from_parent_header(genesis)
275275
blocks: List[FixtureBlock] = []
276+
fcu_version: Optional[int] = None
277+
last_valid: Optional[FixtureHeader] = None
278+
276279
head = genesis.hash if genesis.hash is not None else Hash(0)
277280
for block in self.blocks:
278281
fixture_block, env, alloc, head = self.make_block(
@@ -286,14 +289,22 @@ def make_blocks(
286289
eips=eips,
287290
)
288291
blocks.append(fixture_block)
292+
if block.exception is None:
293+
last_valid = fixture_block.block_header
294+
295+
if not self.base_test_config.disable_hive and last_valid is not None:
296+
fcu_version = fork.engine_forkchoice_updated_version(
297+
block_number=last_valid.number,
298+
timestamp=last_valid.timestamp,
299+
)
289300

290301
try:
291302
verify_post_alloc(self.post, alloc)
292303
except Exception as e:
293304
print_traces(t8n.get_traces())
294305
raise e
295306

296-
return (blocks, head, alloc)
307+
return (blocks, head, alloc, fcu_version)
297308

298309

299310
BlockchainTestSpec = Callable[[str], Generator[BlockchainTest, None, None]]

src/ethereum_test_tools/spec/state_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def make_blocks(
127127
fork: Fork,
128128
chain_id=1,
129129
eips: Optional[List[int]] = None,
130-
) -> Tuple[List[FixtureBlock], Hash, Dict[str, Any]]:
130+
) -> Tuple[List[FixtureBlock], Hash, Dict[str, Any], Optional[int]]:
131131
"""
132132
Create a block from the state test definition.
133133
Performs checks against the expected behavior of the test.
@@ -178,15 +178,18 @@ def make_blocks(
178178
withdrawals=env.withdrawals,
179179
)
180180

181+
# Hive specific fields
181182
new_payload: FixtureEngineNewPayload | None = None
183+
fcu_version: int | None = None
182184
if not self.base_test_config.disable_hive:
183185
new_payload = FixtureEngineNewPayload.from_fixture_header(
184186
fork=fork,
185187
header=header,
186188
transactions=txs,
187189
withdrawals=env.withdrawals,
188-
error_code=self.engine_api_error_code,
190+
error_code=None,
189191
)
192+
fcu_version = fork.engine_forkchoice_updated_version(header.number, header.timestamp)
190193

191194
return (
192195
[
@@ -201,6 +204,7 @@ def make_blocks(
201204
],
202205
header.hash,
203206
alloc,
207+
fcu_version,
204208
)
205209

206210

whitelist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ extcodesize
8484
fn
8585
fname
8686
forkchoice
87+
fcu
8788
formatOnSave
8889
formatter
8990
fromhex

0 commit comments

Comments
 (0)