11"""Pre-allocation fixtures using for test filling."""
22
3- import json
43from itertools import count
54from pathlib import Path
65from 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
98import pytest
109import yaml
11- from pydantic import PrivateAttr , model_validator
10+ from pydantic import PrivateAttr
1211
1312from ethereum_test_base_types import (
1413 Bytes ,
4039from ethereum_test_types .eof .v1 import Container
4140from ethereum_test_vm import Bytecode , EVMCodeType , Opcodes
4241
42+ MAX_BYTECODE_SIZE = 24576
43+
44+ MAX_INITCODE_SIZE = MAX_BYTECODE_SIZE * 2
45+
4346
4447class 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
8392def 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 ()
0 commit comments