99
1010import ckzg # type: ignore
1111import platformdirs
12+ from filelock import FileLock
1213
1314from ethereum_test_base_types .base_types import Bytes , Hash
1415from ethereum_test_base_types .pydantic import CamelModel
@@ -242,14 +243,17 @@ def from_file(file_name: str) -> "Blob":
242243 # determine path where this blob would be stored if it existed
243244 blob_file_location = CACHED_BLOBS_DIRECTORY / file_name
244245
245- # check whether blob exists
246- assert blob_file_location .exists (), (
247- f"Tried to load blob from file but { blob_file_location } does not exist"
248- )
246+ # use lock to avoid race conditions
247+ lock_file_path = blob_file_location .with_suffix (".lock" )
248+ with FileLock (lock_file_path ):
249+ # check whether blob exists
250+ assert blob_file_location .exists (), (
251+ f"Tried to load blob from file but { blob_file_location } does not exist"
252+ )
249253
250- # read blob from file
251- with open (blob_file_location , "r" , encoding = "utf-8" ) as f :
252- json_str : str = f .read ()
254+ # read blob from file
255+ with open (blob_file_location , "r" , encoding = "utf-8" ) as f :
256+ json_str : str = f .read ()
253257
254258 # reconstruct and return blob object
255259 return Blob .model_validate_json (json_str )
@@ -259,12 +263,15 @@ def write_to_file(self):
259263 json_str = self .model_dump_json ()
260264 output_location = Blob .get_filepath (self .fork , self .seed )
261265
262- # warn if existing static_blob gets overwritten
263- if output_location .exists ():
264- logger .debug (f"Blob { output_location } already exists. It will be overwritten." )
266+ # use lock to avoid race conditions
267+ lock_file_path = output_location .with_suffix (".lock" )
268+ with FileLock (lock_file_path ):
269+ # warn if existing static_blob gets overwritten
270+ if output_location .exists ():
271+ logger .debug (f"Blob { output_location } already exists. It will be overwritten." )
265272
266- with open (output_location , "w" , encoding = "utf-8" ) as f : # overwrite existing
267- f .write (json_str )
273+ with open (output_location , "w" , encoding = "utf-8" ) as f : # overwrite existing
274+ f .write (json_str )
268275
269276 def verify_cell_kzg_proof_batch (self , cell_indices : list ) -> bool :
270277 """Check whether all cell proofs are valid and returns True only if that is the case."""
0 commit comments