Skip to content

Commit 18df670

Browse files
committed
unit test improvements for transition forks, wip
1 parent 883faff commit 18df670

File tree

2 files changed

+78
-31
lines changed

2 files changed

+78
-31
lines changed

src/ethereum_test_types/blob_types.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,30 @@
1313

1414
from ethereum_test_base_types.base_types import Bytes, Hash
1515
from ethereum_test_base_types.pydantic import CamelModel
16-
from ethereum_test_forks import (
17-
Cancun,
18-
Fork,
19-
Osaka,
20-
Prague,
21-
)
16+
from ethereum_test_forks import Cancun, CancunToPragueAtTime15k, Fork, Osaka, Prague
17+
from ethereum_test_forks.forks.transition import PragueToOsakaAtTime15k
18+
from ethereum_test_forks.transition_base_fork import TransitionBaseClass
2219

2320
CACHED_BLOBS_DIRECTORY: Path = (
2421
Path(platformdirs.user_cache_dir("ethereum-execution-spec-tests")) / "cached_blobs"
2522
)
2623

2724

25+
def clear_blob_cache(cached_blobs_folder_path: Path):
26+
"""Delete all cached blobs."""
27+
if not cached_blobs_folder_path.is_dir():
28+
return
29+
for f in cached_blobs_folder_path.glob("*.json"): # only delete .json files
30+
try:
31+
f.unlink() # permanently delete this file
32+
except OSError as e:
33+
print(
34+
f"Critical error while trying to delete file {f}:{e}.. "
35+
"Aborting clearing of blob cache folder."
36+
)
37+
return
38+
39+
2840
def fork_string_to_object(fork_name: str) -> type[Cancun] | type[Prague] | type[Osaka]:
2941
"""Take a fork string and return the respective fork as object."""
3042
fork_name = fork_name.lower()
@@ -201,21 +213,21 @@ def get_cells(fork: Fork, data: Bytes) -> List[Bytes] | None:
201213
f"cell proofs {amount_cell_proofs} but expected 128."
202214
)
203215

204-
assert fork.supports_blobs(), f"Provided fork {fork.name()} does not support blobs!"
205-
206216
# if this blob already exists then load from file
207217
blob_location: Path = Blob.get_filepath(fork, seed)
208218
if blob_location.exists():
209219
print(f"Blob exists already, reading it from file {blob_location}")
210220
return Blob.from_file(Blob.get_filename(fork, seed))
211221

212-
# loaded trusted setup if it is not already loaded
213-
Blob.trusted_setup()
214-
215222
# handle transition forks (not needed when default timestamp of 0 is used)
216223
if timestamp > 0:
217224
fork = fork.fork_at(timestamp=timestamp)
218225

226+
assert fork.supports_blobs(), f"Provided fork {fork.name()} does not support blobs!"
227+
228+
# loaded trusted setup if it is not already loaded
229+
Blob.trusted_setup()
230+
219231
# get data for blob parameters
220232
data: Bytes = generate_blob_data(seed)
221233
commitment: Bytes = get_commitment(data)
@@ -435,20 +447,3 @@ def corrupt_byte(b: bytes) -> Bytes:
435447
self.proof = Bytes(bytes(len(self.proof)))
436448
elif mode == self.ProofCorruptionMode.CORRUPT_ALL_BYTES:
437449
self.proof = Bytes(b"".join(corrupt_byte(bytes([byte])) for byte in self.proof))
438-
439-
440-
# test fork_at for when timestamp > 0
441-
# transitionfork: Fork = Cancun
442-
# new_timestamp = 15000 # is supposed to return prague, but still returns cancun.. TODO: ?
443-
# transitionforkblob: Blob = Blob.from_fork(transitionfork, timestamp=new_timestamp)
444-
# transitionforkblob.write_to_file()
445-
# restored_transitionforkblob: Blob = Blob.from_file("blob_0_cell_proofs_0")
446-
# assert transitionforkblob.data == restored_transitionforkblob.data
447-
# assert transitionforkblob.commitment == restored_transitionforkblob.commitment
448-
# assert transitionforkblob.proof == restored_transitionforkblob.proof
449-
# assert transitionforkblob.cells == restored_transitionforkblob.cells
450-
# assert transitionforkblob.versioned_hash == restored_transitionforkblob.versioned_hash
451-
# assert transitionforkblob.name == restored_transitionforkblob.name
452-
# assert transitionforkblob.fork == restored_transitionforkblob.fork
453-
# assert transitionforkblob.seed == restored_transitionforkblob.seed
454-
# assert transitionforkblob.timestamp == restored_transitionforkblob.timestamp

src/ethereum_test_types/tests/test_blob_types.py

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

33
import copy
4+
from pathlib import Path
45

6+
import platformdirs
57
import pytest
68

79
from ethereum_test_forks import (
810
Cancun,
911
Osaka,
1012
Prague,
1113
)
14+
from ethereum_test_forks.base_fork import Fork
15+
from ethereum_test_forks.forks.transition import (
16+
CancunToPragueAtTime15k,
17+
PragueToOsakaAtTime15k,
18+
ShanghaiToCancunAtTime15k,
19+
)
20+
21+
from ..blob_types import Blob, clear_blob_cache
1222

13-
from ..blob_types import Blob
23+
CACHED_BLOBS_DIRECTORY: Path = (
24+
Path(platformdirs.user_cache_dir("ethereum-execution-spec-tests")) / "cached_blobs"
25+
)
1426

1527

1628
@pytest.mark.parametrize("seed", [0, 10, 100])
@@ -45,8 +57,6 @@ def test_blob_creation_and_writing_and_reading(
4557
assert b.seed == restored.seed
4658
assert b.timestamp == restored.timestamp
4759

48-
print("SUCCESSFUL UNIT TEST")
49-
5060

5161
@pytest.mark.parametrize(
5262
"corruption_mode",
@@ -75,3 +85,45 @@ def test_blob_proof_corruption(
7585
f"Proof corruption mode {corruption_mode} for fork {fork.name()} failed, "
7686
"proof is unchanged!"
7787
)
88+
89+
90+
@pytest.mark.parametrize("timestamp", [14999, 15000])
91+
@pytest.mark.parametrize(
92+
"fork", [ShanghaiToCancunAtTime15k, CancunToPragueAtTime15k, PragueToOsakaAtTime15k]
93+
)
94+
def test_transition_fork_blobs(
95+
fork,
96+
timestamp,
97+
):
98+
"""Generates blobs for transition forks (time 14999 is old fork, time 15000 is new fork)."""
99+
clear_blob_cache(CACHED_BLOBS_DIRECTORY)
100+
101+
print(f"Original fork: {fork}, Timestamp: {timestamp}")
102+
pre_transition_fork = fork.transitions_from()
103+
post_transition_fork_at_15k = fork.transitions_to() # only reached if timestamp >= 15000
104+
105+
if not pre_transition_fork.supports_blobs() and timestamp < 15000:
106+
print(
107+
f"Skipping blob creation because pre-transition fork is {pre_transition_fork} "
108+
f"and timestamp is {timestamp}"
109+
)
110+
return
111+
112+
# b has already applied transition if requirements were met
113+
b = Blob.from_fork(fork=fork, timestamp=timestamp)
114+
print(f"Fork of created blob: {b.fork.name()}")
115+
116+
if timestamp == 14999: # case: no transition yet
117+
assert b.fork.name() == pre_transition_fork.name(), (
118+
f"Transition fork failure! Fork {fork.name()} at timestamp: {timestamp} should have "
119+
f"stayed at fork {pre_transition_fork.name()} but has unexpectedly transitioned "
120+
f"to {b.fork.name()}"
121+
)
122+
elif timestamp == 15000: # case: transition to next fork has happened
123+
assert b.fork.name() == post_transition_fork_at_15k.name(), (
124+
f"Transition fork failure! Fork {fork.name()} at timestamp: {timestamp} should have "
125+
f"transitioned to {post_transition_fork_at_15k.name()} but is still at {b.fork.name()}"
126+
)
127+
128+
129+
# TODO: fix issue with ShanghaiToCancunAtTime15k

0 commit comments

Comments
 (0)