Skip to content

Commit b4a529b

Browse files
committed
fixed serialization/deserialization of fork:Fork (not str anymore), other fixes
1 parent 9b6904a commit b4a529b

File tree

3 files changed

+93
-61
lines changed

3 files changed

+93
-61
lines changed

src/ethereum_test_forks/forks/forks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from hashlib import sha256
55
from os.path import realpath
66
from pathlib import Path
7-
from typing import Dict, List, Literal, Mapping, Optional, Sized, Tuple, Union
7+
from typing import List, Literal, Mapping, Optional, Sized, Tuple
88

99
from semver import Version
1010

src/ethereum_test_types/blob_types.py

Lines changed: 92 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
import ckzg # type: ignore
1111
import platformdirs
12+
from pydantic import field_serializer, field_validator
1213

1314
from ethereum_test_base_types.base_types import Bytes, Hash
1415
from ethereum_test_base_types.pydantic import CamelModel
1516
from ethereum_test_forks import (
1617
Cancun,
17-
CancunToPragueAtTime15k,
1818
Fork,
1919
Osaka,
2020
Prague,
@@ -25,6 +25,19 @@
2525
)
2626

2727

28+
def fork_string_to_object(fork_name: str) -> type[Cancun] | type[Prague] | type[Osaka]:
29+
"""Take a fork string and return the respective fork as object."""
30+
fork_name = fork_name.lower()
31+
32+
if fork_name == "cancun":
33+
return Cancun
34+
if fork_name == "prague":
35+
return Prague
36+
if fork_name == "osaka":
37+
return Osaka
38+
raise ValueError(f"Fork {fork_name} has not yet been implemented in this function.")
39+
40+
2841
class Blob(CamelModel):
2942
"""Class representing a full blob."""
3043

@@ -50,6 +63,25 @@ def trusted_setup(cls):
5063
print(f"{trusted_setup_path} has been loaded")
5164
cls._trusted_setup = trusted_setup
5265

66+
@field_validator("fork", mode="before")
67+
@classmethod
68+
def validate_fork(cls, v):
69+
"""
70+
When reading JSON file and trying to go back from fork string to fork object we must
71+
tell pydantic how to do this.
72+
"""
73+
if isinstance(v, str):
74+
return fork_string_to_object(v)
75+
return v
76+
77+
@field_serializer("fork")
78+
def serialize_fork(self, fork: Fork) -> str:
79+
"""
80+
When trying to serialize a Blob object into a JSON file we must
81+
tell pydantic how to do this.
82+
"""
83+
return fork.name()
84+
5385
@staticmethod
5486
def get_filename(fork: Fork, seed: int) -> str:
5587
"""Return filename this blob would have as string (with .json extension)."""
@@ -405,8 +437,9 @@ def corrupt_byte(b: bytes) -> Bytes:
405437
# mytestfork: Fork = CancunToPragueAtTime15k
406438
# mytestfork.fork_at(timestamp=14999) # returns cancun
407439
# mytestfork.fork_at(timestamp=15000) # returns prague
408-
# TODO: call this once only at blob creation (not when reading blob from file) + remove timestamp from json
440+
# TODO: call this once only at blob creation (not when reading blob from file) + remove timestamp from json # noqa: E501
409441

442+
# TODO: fix tests/osaka/eip7594_peerdas/test_get_blobs.py
410443
# TODO: test all functions again
411444

412445
# --------- generate static blobs ------------
@@ -436,60 +469,60 @@ def corrupt_byte(b: bytes) -> Bytes:
436469
# print(f"Generated and wrote {amount_of_blobs} blobs to disk in: {duration_ms:.3f} ms")
437470

438471

439-
myosaka: Fork = Osaka
440-
print(f"Fork test print: {myosaka.name()}")
441-
myprague: Fork = Prague
442-
mycancun: Fork = Cancun
443-
myseed: int = 1337 # fork+seed is the unique ID of a blob
444-
mytimestamp: int = 168123123
445-
# test 1: blob construction
446-
b: Blob = Blob.from_fork(myosaka, myseed, mytimestamp)
447-
# test 2: reconstruction from json
448-
json_str: str = b.model_dump_json()
449-
restored: Blob = Blob.model_validate_json(json_str)
450-
assert b.data == restored.data
451-
assert b.commitment == restored.commitment
452-
assert b.proof == restored.proof
453-
assert b.cells == restored.cells
454-
assert b.versioned_hash == restored.versioned_hash
455-
assert b.name == restored.name
456-
assert b.fork == restored.fork
457-
assert b.seed == restored.seed
458-
assert b.timestamp == restored.timestamp
459-
print(type(b.proof), len(b.proof))
460-
# test 3: write to file
461-
b.write_to_file()
462-
# test 4: read from file
463-
bzzz = Blob.from_file("blob_1337_cell_proofs_128")
464-
print("read osaka blob file from cache dir")
465-
# test 5: prague blob creation + write
466-
d: Blob = Blob.from_fork(myprague, myseed, 123)
467-
d.write_to_file()
468-
# test 6: reading prague from file
469-
e: Blob = Blob.from_file("blob_1337_cell_proofs_0")
470-
print("read prague blob file from cache dir")
471-
# test 7: cancun (will overwrite prague blob cuz same target filename)
472-
dddddde: Blob = Blob.from_fork(mycancun, myseed, 123)
473-
dddddde.write_to_file()
474-
print("line above should say file existed already (cancun overwrites prague)")
475-
# test 8: non-blob forks can't create blobs
476-
# myparis: Fork = Paris
477-
# parisapriaparip: Blob = Blob.from_fork(myparis, myseed, 123)
478-
479-
# test proof corruption
480-
# osaka
481-
testseed = 55
482-
ddd: Blob = Blob.from_fork(Osaka, testseed + 10)
483-
assert isinstance(ddd.proof[0], bytes) # mypy fix
484-
oldValue = ddd.proof[0][5]
485-
for m in Blob.ProofCorruptionMode:
486-
ddd.corrupt_proof(m)
487-
print("proof corruption works (osaka):", oldValue != ddd.proof[0][5])
488-
# prague
489-
eeeeeeeeee: Blob = Blob.from_fork(Prague, testseed + 11)
490-
assert isinstance(eeeeeeeeee.proof[5], int) # mypy fix
491-
oldValue = eeeeeeeeee.proof[5]
492-
for m in Blob.ProofCorruptionMode:
493-
eeeeeeeeee.corrupt_proof(m)
494-
print("proof corruption works (prague):", oldValue != eeeeeeeeee.proof[5])
495-
print("pydantic model works")
472+
# myosaka: Fork = Osaka
473+
# print(f"Fork test print: {myosaka.name()}")
474+
# myprague: Fork = Prague
475+
# mycancun: Fork = Cancun
476+
# myseed: int = 1337 # fork+seed is the unique ID of a blob
477+
# mytimestamp: int = 168123123
478+
# # test 1: blob construction
479+
# b: Blob = Blob.from_fork(myosaka, myseed, mytimestamp)
480+
# # test 2: reconstruction from json
481+
# json_str: str = b.write_to_file()
482+
# restored: Blob = Blob.from_file("blob_1337_cell_proofs_128")
483+
# assert b.data == restored.data
484+
# assert b.commitment == restored.commitment
485+
# assert b.proof == restored.proof
486+
# assert b.cells == restored.cells
487+
# assert b.versioned_hash == restored.versioned_hash
488+
# assert b.name == restored.name
489+
# assert b.fork == restored.fork
490+
# assert b.seed == restored.seed
491+
# assert b.timestamp == restored.timestamp
492+
# print(type(b.proof), len(b.proof))
493+
# # test 3: write to file
494+
# b.write_to_file()
495+
# # test 4: read from file
496+
# bzzz = Blob.from_file("blob_1337_cell_proofs_128")
497+
# print("read osaka blob file from cache dir")
498+
# # test 5: prague blob creation + write
499+
# d: Blob = Blob.from_fork(myprague, myseed, 123)
500+
# d.write_to_file()
501+
# # test 6: reading prague from file
502+
# e: Blob = Blob.from_file("blob_1337_cell_proofs_0")
503+
# print("read prague blob file from cache dir")
504+
# # test 7: cancun (will overwrite prague blob cuz same target filename)
505+
# dddddde: Blob = Blob.from_fork(mycancun, myseed, 123)
506+
# dddddde.write_to_file()
507+
# print("line above should say file existed already (cancun overwrites prague)")
508+
# # test 8: non-blob forks can't create blobs
509+
# # myparis: Fork = Paris
510+
# # parisapriaparip: Blob = Blob.from_fork(myparis, myseed, 123)
511+
512+
# # test proof corruption
513+
# # osaka
514+
# testseed = 55
515+
# ddd: Blob = Blob.from_fork(Osaka, testseed + 10)
516+
# assert isinstance(ddd.proof[0], bytes) # mypy fix
517+
# oldValue = ddd.proof[0][5]
518+
# for m in Blob.ProofCorruptionMode:
519+
# ddd.corrupt_proof(m)
520+
# print("proof corruption works (osaka):", oldValue != ddd.proof[0][5])
521+
# # prague
522+
# eeeeeeeeee: Blob = Blob.from_fork(Prague, testseed + 11)
523+
# assert isinstance(eeeeeeeeee.proof[5], int) # mypy fix
524+
# oldValue = eeeeeeeeee.proof[5]
525+
# for m in Blob.ProofCorruptionMode:
526+
# eeeeeeeeee.corrupt_proof(m)
527+
# print("proof corruption works (prague):", oldValue != eeeeeeeeee.proof[5])
528+
# print("pydantic model works")

tests/osaka/eip7594_peerdas/spec.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Defines EIP-7594 specification constants and functions."""
22

33
from dataclasses import dataclass
4-
from typing import Literal
54

65

76
@dataclass(frozen=True)

0 commit comments

Comments
 (0)