Skip to content

Commit 77edf42

Browse files
akafle01Abhishek Kaflemarioevz
authored
feat(forks): Enforce keyword-only arguments in fork methods (#1714)
* Enforce keyword-only arguments in calldata_gas_calculator methods and calls * Apply keyword-only enforcement to all BaseFork methods and implementations * fixes --------- Co-authored-by: Abhishek Kafle <[email protected]> Co-authored-by: Mario Vega <[email protected]>
1 parent 65f2663 commit 77edf42

File tree

11 files changed

+450
-358
lines changed

11 files changed

+450
-358
lines changed

docs/writing_tests/fork_methods.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Example of adding a new method:
188188
```python
189189
@classmethod
190190
@abstractmethod
191-
def supports_new_feature(cls, block_number: int = 0, timestamp: int = 0) -> bool:
191+
def supports_new_feature(cls, *, block_number: int = 0, timestamp: int = 0) -> bool:
192192
"""Return whether the given fork supports the new feature."""
193193
pass
194194
```
@@ -197,7 +197,7 @@ Implementation in a fork class:
197197

198198
```python
199199
@classmethod
200-
def supports_new_feature(cls, block_number: int = 0, timestamp: int = 0) -> bool:
200+
def supports_new_feature(cls, *, block_number: int = 0, timestamp: int = 0) -> bool:
201201
"""Return whether the given fork supports the new feature."""
202202
return False # Frontier doesn't support this feature
203203
```
@@ -206,7 +206,7 @@ Implementation in a newer fork class:
206206

207207
```python
208208
@classmethod
209-
def supports_new_feature(cls, block_number: int = 0, timestamp: int = 0) -> bool:
209+
def supports_new_feature(cls, *, block_number: int = 0, timestamp: int = 0) -> bool:
210210
"""Return whether the given fork supports the new feature."""
211211
return True # This fork does support the feature
212212
```

src/ethereum_test_fixtures/blockchain.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ def __new__(cls, value: str) -> "HeaderForkRequirement":
103103

104104
def required(self, fork: Fork, block_number: int, timestamp: int) -> bool:
105105
"""Check if the field is required for the given fork."""
106-
return getattr(fork, f"header_{self}_required")(block_number, timestamp)
106+
return getattr(fork, f"header_{self}_required")(
107+
block_number=block_number, timestamp=timestamp
108+
)
107109

108110
@classmethod
109111
def get_from_annotation(cls, field_hints: Any) -> "HeaderForkRequirement | None":
@@ -238,9 +240,13 @@ def genesis(cls, fork: Fork, env: Environment, state_root: Hash) -> "FixtureHead
238240
environment_values["extra_data"] = env.extra_data
239241
extras = {
240242
"state_root": state_root,
241-
"requests_hash": Requests() if fork.header_requests_required(0, 0) else None,
243+
"requests_hash": Requests()
244+
if fork.header_requests_required(block_number=0, timestamp=0)
245+
else None,
242246
"block_access_list_hash": (
243-
BlockAccessList().rlp_hash if fork.header_bal_hash_required(0, 0) else None
247+
BlockAccessList().rlp_hash
248+
if fork.header_bal_hash_required(block_number=0, timestamp=0)
249+
else None
244250
),
245251
"fork": fork,
246252
}
@@ -356,14 +362,18 @@ def from_fixture_header(
356362
**kwargs,
357363
) -> "FixtureEngineNewPayload":
358364
"""Create `FixtureEngineNewPayload` from a `FixtureHeader`."""
359-
new_payload_version = fork.engine_new_payload_version(header.number, header.timestamp)
365+
new_payload_version = fork.engine_new_payload_version(
366+
block_number=header.number, timestamp=header.timestamp
367+
)
360368
forkchoice_updated_version = fork.engine_forkchoice_updated_version(
361-
header.number, header.timestamp
369+
block_number=header.number, timestamp=header.timestamp
362370
)
363371

364372
assert new_payload_version is not None, "Invalid header for engine_newPayload"
365373

366-
if fork.engine_execution_payload_block_access_list(header.number, header.timestamp):
374+
if fork.engine_execution_payload_block_access_list(
375+
block_number=header.number, timestamp=header.timestamp
376+
):
367377
if block_access_list is None:
368378
raise ValueError(
369379
f"`block_access_list` is required in engine `ExecutionPayload` for >={fork}."
@@ -377,19 +387,25 @@ def from_fixture_header(
377387
)
378388

379389
params: List[Any] = [execution_payload]
380-
if fork.engine_new_payload_blob_hashes(header.number, header.timestamp):
390+
if fork.engine_new_payload_blob_hashes(
391+
block_number=header.number, timestamp=header.timestamp
392+
):
381393
blob_hashes = Transaction.list_blob_versioned_hashes(transactions)
382394
if blob_hashes is None:
383395
raise ValueError(f"Blob hashes are required for ${fork}.")
384396
params.append(blob_hashes)
385397

386-
if fork.engine_new_payload_beacon_root(header.number, header.timestamp):
398+
if fork.engine_new_payload_beacon_root(
399+
block_number=header.number, timestamp=header.timestamp
400+
):
387401
parent_beacon_block_root = header.parent_beacon_block_root
388402
if parent_beacon_block_root is None:
389403
raise ValueError(f"Parent beacon block root is required for ${fork}.")
390404
params.append(parent_beacon_block_root)
391405

392-
if fork.engine_new_payload_requests(header.number, header.timestamp):
406+
if fork.engine_new_payload_requests(
407+
block_number=header.number, timestamp=header.timestamp
408+
):
393409
if requests is None:
394410
raise ValueError(f"Requests are required for ${fork}.")
395411
params.append(requests)

0 commit comments

Comments
 (0)