99
1010import ckzg # type: ignore
1111import platformdirs
12- from pydantic import field_serializer , field_validator
1312
1413from ethereum_test_base_types .base_types import Bytes , Hash
1514from ethereum_test_base_types .pydantic import CamelModel
16- from ethereum_test_forks import Cancun , Fork , Osaka , Prague
15+ from ethereum_test_forks import Fork
1716from pytest_plugins .logging import get_logger
1817
1918CACHED_BLOBS_DIRECTORY : Path = (
@@ -29,27 +28,14 @@ def clear_blob_cache(cached_blobs_folder_path: Path):
2928 for f in cached_blobs_folder_path .glob ("*.json" ): # only delete .json files
3029 try :
3130 f .unlink () # permanently delete this file
32- except OSError as e :
31+ except Exception as e :
3332 print (
3433 f"Critical error while trying to delete file { f } :{ e } .. "
3534 "Aborting clearing of blob cache folder."
3635 )
3736 return
3837
3938
40- def fork_string_to_object (fork_name : str ) -> type [Cancun ] | type [Prague ] | type [Osaka ]:
41- """Take a fork string and return the respective fork as object."""
42- fork_name = fork_name .lower ()
43-
44- if fork_name == "cancun" :
45- return Cancun
46- if fork_name == "prague" :
47- return Prague
48- if fork_name == "osaka" :
49- return Osaka
50- raise ValueError (f"Fork { fork_name } has not yet been implemented in this function." )
51-
52-
5339class Blob (CamelModel ):
5440 """Class representing a full blob."""
5541
@@ -72,29 +58,8 @@ def trusted_setup(cls):
7258 if cls ._trusted_setup is None :
7359 trusted_setup_path = Path (realpath (__file__ )).parent / "kzg_trusted_setup.txt"
7460 trusted_setup = ckzg .load_trusted_setup (str (trusted_setup_path ), 0 )
75- print (f"{ trusted_setup_path } has been loaded" )
7661 cls ._trusted_setup = trusted_setup
77-
78- @field_validator ("fork" , mode = "before" )
79- @classmethod
80- def validate_fork (cls , v ):
81- """
82- When reading JSON file and trying to go back from fork string to fork object we must
83- tell pydantic how to do this.
84- """
85- if isinstance (v , str ):
86- fork_object = fork_string_to_object (v )
87- cls .fork = fork_object
88- return fork_object
89- return v
90-
91- @field_serializer ("fork" )
92- def serialize_fork (self , fork : Fork ) -> str :
93- """
94- When trying to serialize a Blob object into a JSON file we must
95- tell pydantic how to do this.
96- """
97- return fork .name ()
62+ print ("I HAVE LOADED THE TRUSTED SETUP" )
9863
9964 @staticmethod
10065 def get_filename (fork : Fork , seed : int ) -> str :
@@ -108,12 +73,6 @@ def get_filepath(fork: Fork, seed: int):
10873 # determine amount of cell proofs for this fork (0 or 128)
10974 would_be_filename : str = Blob .get_filename (fork , seed )
11075
111- # create cached blobs dir if necessary
112- if not CACHED_BLOBS_DIRECTORY .exists ():
113- CACHED_BLOBS_DIRECTORY .mkdir (
114- parents = True , exist_ok = True
115- ) # create all necessary dirs on the way
116-
11776 # return path to blob
11877 return CACHED_BLOBS_DIRECTORY / would_be_filename
11978
@@ -162,7 +121,7 @@ def get_commitment(data: Bytes) -> Bytes:
162121 )
163122
164123 # calculate commitment
165- commitment = ckzg .blob_to_kzg_commitment (data , Blob ._trusted_setup )
124+ commitment = ckzg .blob_to_kzg_commitment (data , Blob .trusted_setup () )
166125 assert len (commitment ) == fork .get_blob_constant ("BYTES_PER_COMMITMENT" ), (
167126 f"Expected { fork .get_blob_constant ('BYTES_PER_COMMITMENT' )} "
168127 f"resulting commitments but got { len (commitment )} commitments"
@@ -215,7 +174,13 @@ def get_cells(fork: Fork, data: Bytes) -> List[Bytes] | None:
215174 f"cell proofs { amount_cell_proofs } but expected 128."
216175 )
217176
218- # first, handle transition forks
177+ # first, create cached blobs dir if necessary
178+ if not CACHED_BLOBS_DIRECTORY .exists ():
179+ CACHED_BLOBS_DIRECTORY .mkdir (
180+ parents = True , exist_ok = True
181+ ) # create all necessary dirs on the way
182+
183+ # handle transition forks
219184 # (blob related constants are needed and only available for normal forks)
220185 fork = fork .fork_at (timestamp = timestamp )
221186
@@ -238,7 +203,7 @@ def get_cells(fork: Fork, data: Bytes) -> List[Bytes] | None:
238203 versioned_hash : Hash = get_versioned_hash (commitment )
239204 name : str = Blob .get_filename (fork , seed )
240205
241- return Blob (
206+ blob = Blob (
242207 data = data ,
243208 commitment = commitment ,
244209 proof = proof ,
@@ -249,6 +214,10 @@ def get_cells(fork: Fork, data: Bytes) -> List[Bytes] | None:
249214 seed = seed ,
250215 timestamp = timestamp ,
251216 )
217+ # for most effective caching temporarily persist every blob that is created in cache
218+ blob .write_to_file ()
219+
220+ return blob
252221
253222 @staticmethod
254223 def from_file (file_name : str ) -> "Blob" :
0 commit comments