Skip to content

Commit 883faff

Browse files
committed
added unit test for blobs, removed most unneeded comments
1 parent e469a29 commit 883faff

File tree

2 files changed

+77
-85
lines changed

2 files changed

+77
-85
lines changed

src/ethereum_test_types/blob_types.py

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -437,73 +437,6 @@ def corrupt_byte(b: bytes) -> Bytes:
437437
self.proof = Bytes(b"".join(corrupt_byte(bytes([byte])) for byte in self.proof))
438438

439439

440-
# --------- generate static blobs ------------
441-
# fork_list = [Cancun, Prague, Osaka]
442-
# amount_static_blobs_per_fork = 10
443-
# for f in fork_list:
444-
# for seed in range(amount_static_blobs_per_fork):
445-
# if seed < 4:
446-
# timestamp = 0
447-
# elif seed < 7:
448-
# timestamp = 1000000000
449-
# else:
450-
# timestamp = 1747905411
451-
452-
# static_blob: Blob = Blob.from_fork(f, seed, timestamp)
453-
# static_blob.write_to_file()
454-
# --------------------------------------------
455-
456-
# myosaka: Fork = Osaka
457-
# start = time.perf_counter()
458-
# amount_of_blobs = 1000
459-
# for s in range(amount_of_blobs):
460-
# b: Blob = Blob.from_fork(myosaka, s)
461-
# b.write_to_file()
462-
# end = time.perf_counter()
463-
# duration_ms = (end - start) * 1000
464-
# print(f"Generated and wrote {amount_of_blobs} blobs to disk in: {duration_ms:.3f} ms")
465-
466-
467-
# myosaka: Fork = Osaka
468-
# print(f"Fork test print: {myosaka.name()}")
469-
# myprague: Fork = Prague
470-
# mycancun: Fork = Cancun
471-
# myseed: int = 1337 # fork+seed is the unique ID of a blob
472-
# mytimestamp: int = 168123123
473-
# # test 1: blob construction
474-
# b: Blob = Blob.from_fork(myosaka, myseed, mytimestamp)
475-
# # test 2: reconstruction from json
476-
# json_str: str = b.write_to_file()
477-
# restored: Blob = Blob.from_file("blob_1337_cell_proofs_128")
478-
# assert b.data == restored.data
479-
# assert b.commitment == restored.commitment
480-
# assert b.proof == restored.proof
481-
# assert b.cells == restored.cells
482-
# assert b.versioned_hash == restored.versioned_hash
483-
# assert b.name == restored.name
484-
# assert b.fork == restored.fork
485-
# assert b.seed == restored.seed
486-
# assert b.timestamp == restored.timestamp
487-
# print(type(b.proof), len(b.proof))
488-
# # test 3: write to file
489-
# b.write_to_file()
490-
# # test 4: read from file
491-
# bzzz = Blob.from_file("blob_1337_cell_proofs_128")
492-
# print("read osaka blob file from cache dir")
493-
# # test 5: prague blob creation + write
494-
# d: Blob = Blob.from_fork(myprague, myseed, 123)
495-
# d.write_to_file()
496-
# # test 6: reading prague from file
497-
# e: Blob = Blob.from_file("blob_1337_cell_proofs_0")
498-
# print("read prague blob file from cache dir")
499-
# # test 7: cancun (will overwrite prague blob cuz same target filename)
500-
# dddddde: Blob = Blob.from_fork(mycancun, myseed, 123)
501-
# dddddde.write_to_file()
502-
# print("line above should say file existed already (cancun overwrites prague)")
503-
# # test 8: non-blob forks can't create blobs
504-
# # myparis: Fork = Paris
505-
# # parisapriaparip: Blob = Blob.from_fork(myparis, myseed, 123)
506-
507440
# test fork_at for when timestamp > 0
508441
# transitionfork: Fork = Cancun
509442
# new_timestamp = 15000 # is supposed to return prague, but still returns cancun.. TODO: ?
@@ -519,21 +452,3 @@ def corrupt_byte(b: bytes) -> Bytes:
519452
# assert transitionforkblob.fork == restored_transitionforkblob.fork
520453
# assert transitionforkblob.seed == restored_transitionforkblob.seed
521454
# assert transitionforkblob.timestamp == restored_transitionforkblob.timestamp
522-
523-
# # test proof corruption
524-
# # osaka
525-
# testseed = 55
526-
# ddd: Blob = Blob.from_fork(Osaka, testseed + 10)
527-
# assert isinstance(ddd.proof[0], bytes) # mypy fix
528-
# oldValue = ddd.proof[0][5]
529-
# for m in Blob.ProofCorruptionMode:
530-
# ddd.corrupt_proof(m)
531-
# print("proof corruption works (osaka):", oldValue != ddd.proof[0][5])
532-
# # prague
533-
# eeeeeeeeee: Blob = Blob.from_fork(Prague, testseed + 11)
534-
# assert isinstance(eeeeeeeeee.proof[5], int) # mypy fix
535-
# oldValue = eeeeeeeeee.proof[5]
536-
# for m in Blob.ProofCorruptionMode:
537-
# eeeeeeeeee.corrupt_proof(m)
538-
# print("proof corruption works (prague):", oldValue != eeeeeeeeee.proof[5])
539-
# print("pydantic model works")
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Test suite for blobs."""
2+
3+
import copy
4+
5+
import pytest
6+
7+
from ethereum_test_forks import (
8+
Cancun,
9+
Osaka,
10+
Prague,
11+
)
12+
13+
from ..blob_types import Blob
14+
15+
16+
@pytest.mark.parametrize("seed", [0, 10, 100])
17+
@pytest.mark.parametrize("fork", [Cancun, Prague, Osaka])
18+
def test_blob_creation_and_writing_and_reading(
19+
seed,
20+
fork,
21+
): # noqa: F811
22+
"""
23+
Generates blobs for different forks and ensures writing to file
24+
and reading from file works as expected.
25+
"""
26+
timestamp = 100
27+
b = Blob.from_fork(fork=fork, seed=seed, timestamp=timestamp)
28+
b.write_to_file()
29+
30+
# read from file
31+
# determine what filename would be
32+
cell_proof_amount = str(fork.get_blob_constant("AMOUNT_CELL_PROOFS"))
33+
file_name = "blob_" + str(seed) + "_cell_proofs_" + cell_proof_amount + ".json"
34+
# read
35+
restored = Blob.from_file(file_name)
36+
37+
# ensure file you read equals file you wrote
38+
assert b.data == restored.data
39+
assert b.commitment == restored.commitment
40+
assert b.proof == restored.proof
41+
assert b.cells == restored.cells
42+
assert b.versioned_hash == restored.versioned_hash
43+
assert b.name == restored.name
44+
assert b.fork == restored.fork
45+
assert b.seed == restored.seed
46+
assert b.timestamp == restored.timestamp
47+
48+
print("SUCCESSFUL UNIT TEST")
49+
50+
51+
@pytest.mark.parametrize(
52+
"corruption_mode",
53+
[
54+
Blob.ProofCorruptionMode.CORRUPT_ALL_BYTES,
55+
Blob.ProofCorruptionMode.CORRUPT_FIRST_BYTE,
56+
Blob.ProofCorruptionMode.CORRUPT_LAST_BYTE,
57+
Blob.ProofCorruptionMode.CORRUPT_TO_ALL_ZEROES,
58+
],
59+
)
60+
@pytest.mark.parametrize("fork", [Cancun, Prague, Osaka])
61+
def test_blob_proof_corruption(
62+
corruption_mode,
63+
fork,
64+
):
65+
"""
66+
Generates blobs for different forks, corrupts their proofs and ensures that
67+
the corrupted proof is not equal to the correct proof.
68+
"""
69+
timestamp = 100
70+
b = Blob.from_fork(fork=fork, timestamp=timestamp)
71+
old_valid_proof = copy.deepcopy(b.proof) # important to deepcopy
72+
73+
b.corrupt_proof(corruption_mode)
74+
assert b.proof != old_valid_proof, (
75+
f"Proof corruption mode {corruption_mode} for fork {fork.name()} failed, "
76+
"proof is unchanged!"
77+
)

0 commit comments

Comments
 (0)