Skip to content

Commit 61a096a

Browse files
committed
fix: review suggestions
1 parent f5bbca9 commit 61a096a

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

src/pytest_plugins/execute/pre_alloc.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
"""Pre-allocation fixtures using for test filling."""
22

3-
import json
43
from itertools import count
54
from pathlib import Path
65
from random import randint
7-
from typing import Any, Dict, Generator, Iterator, List, Literal, Tuple
6+
from typing import Dict, Generator, Iterator, List, Literal, Self, Tuple
87

98
import pytest
109
import yaml
11-
from pydantic import PrivateAttr, model_validator
10+
from pydantic import PrivateAttr
1211

1312
from ethereum_test_base_types import (
1413
Bytes,
@@ -40,6 +39,10 @@
4039
from ethereum_test_types.eof.v1 import Container
4140
from ethereum_test_vm import Bytecode, EVMCodeType, Opcodes
4241

42+
MAX_BYTECODE_SIZE = 24576
43+
44+
MAX_INITCODE_SIZE = MAX_BYTECODE_SIZE * 2
45+
4346

4447
class AddressStubs(EthereumTestRootModel[Dict[str, Address]]):
4548
"""
@@ -59,25 +62,31 @@ def __getitem__(self, item: str) -> Address:
5962
"""Get an item from the address stubs."""
6063
return self.root[item]
6164

62-
@model_validator(mode="before")
6365
@classmethod
64-
def load_contents_from_file(cls, value: Any) -> Any:
66+
def model_validate_json_or_file(cls, json_data_or_path: str) -> Self:
6567
"""
6668
Try to load from file if the value resembles a path that ends with .json/.yml and the
6769
file exists.
6870
"""
69-
if isinstance(value, str):
70-
if value.endswith(".json") or value.endswith(".yml") or value.endswith(".yaml"):
71-
path = Path(value)
72-
if path.is_file():
73-
if value.endswith(".json"):
74-
return json.loads(path.read_text())
75-
elif value.endswith(".yml") or value.endswith(".yaml"):
76-
loaded_yaml = yaml.safe_load(path.read_text())
77-
if loaded_yaml is None:
78-
return {}
79-
return loaded_yaml
80-
return value
71+
lower_json_data_or_path = json_data_or_path.lower()
72+
if (
73+
lower_json_data_or_path.endswith(".json")
74+
or lower_json_data_or_path.endswith(".yml")
75+
or lower_json_data_or_path.endswith(".yaml")
76+
):
77+
path = Path(json_data_or_path)
78+
if path.is_file():
79+
path_suffix = path.suffix.lower()
80+
if path_suffix == ".json":
81+
return cls.model_validate_json(path.read_text())
82+
elif path_suffix in [".yml", ".yaml"]:
83+
loaded_yaml = yaml.safe_load(path.read_text())
84+
if loaded_yaml is None:
85+
return cls(root={})
86+
return cls.model_validate(loaded_yaml)
87+
if json_data_or_path.strip() == "":
88+
return cls(root={})
89+
return cls.model_validate_json(json_data_or_path)
8190

8291

8392
def pytest_addoption(parser):
@@ -115,7 +124,7 @@ def pytest_addoption(parser):
115124
action="store",
116125
dest="address_stubs",
117126
default=AddressStubs(root={}),
118-
type=AddressStubs.model_validate,
127+
type=AddressStubs.model_validate_json_or_file,
119128
help="The address stubs for contracts that have already been placed in the chain and to "
120129
"use for the test. Can be a JSON formatted string or a path to a YAML or JSON file.",
121130
)
@@ -239,9 +248,7 @@ def deploy_contract(
239248
)
240249
code = self.code_pre_processor(code, evm_code_type=evm_code_type)
241250

242-
assert len(code) <= self._fork.max_code_size(), (
243-
f"code too large: {len(code)} > {self._fork.max_code_size()}"
244-
)
251+
assert len(code) <= MAX_BYTECODE_SIZE, f"code too large: {len(code)} > {MAX_BYTECODE_SIZE}"
245252

246253
deploy_gas_limit += len(bytes(code)) * 200
247254

@@ -255,8 +262,8 @@ def deploy_contract(
255262
memory_expansion_gas_calculator = self._fork.memory_expansion_gas_calculator()
256263
deploy_gas_limit += memory_expansion_gas_calculator(new_bytes=len(bytes(initcode)))
257264

258-
assert len(initcode) <= self._fork.max_initcode_size(), (
259-
f"initcode too large {len(initcode)} > {self._fork.max_initcode_size()}"
265+
assert len(initcode) <= MAX_INITCODE_SIZE, (
266+
f"initcode too large {len(initcode)} > {MAX_INITCODE_SIZE}"
260267
)
261268

262269
calldata_gas_calculator = self._fork.calldata_gas_calculator()

src/pytest_plugins/execute/tests/test_pre_alloc.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313
"input_value,expected",
1414
[
1515
pytest.param(
16-
{},
16+
"{}",
1717
AddressStubs({}),
18-
id="empty_address_stubs",
18+
id="empty_address_stubs_string",
19+
),
20+
pytest.param(
21+
'{"some_address": "0x0000000000000000000000000000000000000001"}',
22+
AddressStubs({"some_address": Address("0x0000000000000000000000000000000000000001")}),
23+
id="address_stubs_string_with_some_address",
1924
),
2025
],
2126
)
2227
def test_address_stubs(input_value: Any, expected: AddressStubs):
2328
"""Test the address stubs."""
24-
assert AddressStubs.model_validate(input_value) == expected
29+
assert AddressStubs.model_validate_json_or_file(input_value) == expected
2530

2631

2732
@pytest.mark.parametrize(
@@ -75,4 +80,4 @@ def test_address_stubs_from_files(
7580
filename = pytester.path.joinpath(file_name)
7681
filename.write_text(file_contents)
7782

78-
assert AddressStubs.model_validate(str(filename)) == expected
83+
assert AddressStubs.model_validate_json_or_file(str(filename)) == expected

0 commit comments

Comments
 (0)