Skip to content

Commit 74d49a4

Browse files
committed
implemented feedback
1 parent ab3a59c commit 74d49a4

File tree

7 files changed

+28
-89
lines changed

7 files changed

+28
-89
lines changed

src/ethereum_test_base_types/conversions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def to_bytes(input_bytes: BytesConvertible) -> bytes:
5555
# handle wrapper_version field for >= osaka
5656
if isinstance(input_bytes, int):
5757
assert input_bytes < 256, f"Expected int that fits into one byte, but got {input_bytes}"
58-
return input_bytes.to_bytes(1, "big")
58+
return input_bytes.to_bytes(1, byteorder="big")
5959

6060
logger.error(
6161
f"This type of input_bytes is not yet supported: {type(input_bytes)}\n\n"

src/ethereum_test_rpc/rpc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def post_request(self, method: str, *params: Any, extra_headers: Dict | None = N
137137
payload_size_bytes = len(payload_json.encode("utf-8"))
138138
payload_size_mb = payload_size_bytes / (1024 * 1024)
139139
logger.debug(
140-
f"I am about to send a POST request of approximated size: {payload_size_mb:.2f} MB "
140+
f"POST request of approximated size: {payload_size_mb:.2f} MB "
141141
f"({payload_size_bytes} bytes)"
142142
)
143143

src/ethereum_test_types/blob_types.py

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99

1010
import ckzg # type: ignore
1111
import platformdirs
12-
from pydantic import field_serializer, field_validator
1312

1413
from ethereum_test_base_types.base_types import Bytes, Hash
1514
from ethereum_test_base_types.pydantic import CamelModel
16-
from ethereum_test_forks import Cancun, Fork, Osaka, Prague
15+
from ethereum_test_forks import Fork
1716
from pytest_plugins.logging import get_logger
1817

1918
CACHED_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-
5339
class 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":

src/ethereum_test_types/tests/test_blob_types.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"""Test suite for blobs."""
22

33
import copy
4-
from pathlib import Path
54

6-
import platformdirs
75
import pytest
86

97
from ethereum_test_forks import (
@@ -17,11 +15,7 @@
1715
ShanghaiToCancunAtTime15k,
1816
)
1917

20-
from ..blob_types import Blob, clear_blob_cache
21-
22-
CACHED_BLOBS_DIRECTORY: Path = (
23-
Path(platformdirs.user_cache_dir("ethereum-execution-spec-tests")) / "cached_blobs"
24-
)
18+
from ..blob_types import CACHED_BLOBS_DIRECTORY, Blob, clear_blob_cache
2519

2620

2721
@pytest.mark.parametrize("seed", [0, 10, 100])
@@ -46,15 +40,7 @@ def test_blob_creation_and_writing_and_reading(
4640
restored = Blob.from_file(file_name)
4741

4842
# ensure file you read equals file you wrote
49-
assert b.data == restored.data
50-
assert b.commitment == restored.commitment
51-
assert b.proof == restored.proof
52-
assert b.cells == restored.cells
53-
assert b.versioned_hash == restored.versioned_hash
54-
assert b.name == restored.name
55-
assert b.fork == restored.fork
56-
assert b.seed == restored.seed
57-
assert b.timestamp == restored.timestamp
43+
assert b.model_dump() == restored.model_dump()
5844

5945

6046
@pytest.mark.parametrize(

src/ethereum_test_types/transaction_types.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -730,22 +730,6 @@ def get_rlp_fields(self) -> List[str]:
730730
if self.cell_proofs is not None:
731731
rlp_cell_proofs = ["cell_proofs"]
732732

733-
# Geth expects:
734-
# type blobTxWithBlobs struct {
735-
# BlobTx *BlobTx
736-
# Blobs []kzg4844.Blob
737-
# Commitments []kzg4844.Commitment
738-
# Proofs []kzg4844.Proof
739-
# }
740-
741-
# type versionedBlobTxWithBlobs struct {
742-
# BlobTx *BlobTx
743-
# Version byte
744-
# Blobs []kzg4844.Blob
745-
# Commitments []kzg4844.Commitment
746-
# Proofs []kzg4844.Proof
747-
# }
748-
749733
rlp_fields: List[
750734
str
751735
] = [ # structure explained in https://eips.ethereum.org/EIPS/eip-7594#Networking
@@ -761,7 +745,7 @@ def get_rlp_fields(self) -> List[str]:
761745
"Neither proofs nor cell_proofs are in rlp_fields. Critical error!"
762746
)
763747

764-
logger.debug(f"Ended up with this rlp field list: {rlp_fields}")
748+
# logger.debug(f"Ended up with this rlp field list: {rlp_fields}")
765749

766750
return rlp_fields
767751

tests/cancun/eip4844_blobs/test_blob_txs_full.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,19 @@ def generate_full_blob_tests(
254254
pytest.param(
255255
[ # Txs
256256
[ # Blobs per transaction
257-
Blob.from_fork(fork),
257+
Blob.from_fork(fork, s),
258258
]
259-
for _ in range(max_blobs)
259+
for s in range(max_blobs)
260260
],
261261
[True] + ([False] * (max_blobs - 1)),
262262
id="one_full_blob_max_txs",
263263
),
264264
pytest.param(
265265
[ # Txs
266266
[ # Blobs per transaction
267-
Blob.from_fork(fork),
267+
Blob.from_fork(fork, s),
268268
]
269-
for _ in range(max_blobs)
269+
for s in range(max_blobs)
270270
],
271271
([False] * (max_blobs - 1)) + [True],
272272
id="one_full_blob_at_the_end_max_txs",

tests/osaka/eip7594_peerdas/test_get_blobs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,17 @@ def generate_full_blob_tests(
222222
pytest.param(
223223
[ # Txs
224224
[ # Blobs per transaction
225-
Blob.from_fork(fork) for _ in range(max_blobs)
225+
Blob.from_fork(fork, s) for s in range(max_blobs)
226226
]
227227
],
228228
id="max_blobs_transaction",
229229
),
230230
pytest.param(
231231
[ # Txs
232232
[ # Blobs per transaction
233-
Blob.from_fork(fork),
233+
Blob.from_fork(fork, s),
234234
]
235-
for _ in range(max_blobs)
235+
for s in range(max_blobs)
236236
],
237237
id="single_blob_max_txs",
238238
),

0 commit comments

Comments
 (0)