Skip to content

Commit c128049

Browse files
committed
fixed import issue with specs, added fork.blobs_supported()
1 parent a63fab6 commit c128049

File tree

4 files changed

+100
-52
lines changed

4 files changed

+100
-52
lines changed

src/ethereum_test_forks/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Ethereum test fork definitions."""
22

3+
from typing import Literal
4+
35
from .base_fork import Fork, ForkAttribute
46
from .forks.forks import (
57
ArrowGlacier,
@@ -98,3 +100,23 @@
98100
"transition_fork_to",
99101
"GasCosts",
100102
]
103+
104+
# blob-related constants
105+
FIELD_ELEMENTS_PER_BLOB = 4096
106+
BYTES_PER_FIELD_ELEMENT = 32
107+
BYTES_PER_BLOB = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT # 131072
108+
CELL_LENGTH = 2048
109+
BLS_MODULUS = 0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001 # EIP-2537: Main subgroup order = q # noqa: E501
110+
# due to BLS_MODULUS every blob byte (uint256) must be smaller than 116
111+
112+
# deneb constants that have not changed (https://github.com/ethereum/consensus-specs/blob/cc6996c22692d70e41b7a453d925172ee4b719ad/specs/deneb/polynomial-commitments.md?plain=1#L78)
113+
BYTES_PER_PROOF = 48
114+
BYTES_PER_COMMITMENT = 48
115+
KZG_ENDIANNESS: Literal["big"] = "big"
116+
117+
# eip-7691
118+
MAX_BLOBS_PER_BLOCK_ELECTRA = 9
119+
TARGET_BLOBS_PER_BLOCK_ELECTRA = 6
120+
MAX_BLOB_GAS_PER_BLOCK = 1179648
121+
TARGET_BLOB_GAS_PER_BLOCK = 786432
122+
BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE = 5007716

src/ethereum_test_forks/forks/forks.py

Lines changed: 31 additions & 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 List, Mapping, Optional, Sized, Tuple
7+
from typing import List, Literal, Mapping, Optional, Sized, Tuple
88

99
from semver import Version
1010

@@ -27,6 +27,26 @@
2727
CURRENT_FILE = Path(realpath(__file__))
2828
CURRENT_FOLDER = CURRENT_FILE.parent
2929

30+
# blob-related constants
31+
FIELD_ELEMENTS_PER_BLOB = 4096
32+
BYTES_PER_FIELD_ELEMENT = 32
33+
BYTES_PER_BLOB = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT # 131072
34+
CELL_LENGTH = 2048
35+
BLS_MODULUS = 0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001 # EIP-2537: Main subgroup order = q # noqa: E501
36+
# due to BLS_MODULUS every blob byte (uint256) must be smaller than 116
37+
38+
# deneb constants that have not changed (https://github.com/ethereum/consensus-specs/blob/cc6996c22692d70e41b7a453d925172ee4b719ad/specs/deneb/polynomial-commitments.md?plain=1#L78)
39+
BYTES_PER_PROOF = 48
40+
BYTES_PER_COMMITMENT = 48
41+
KZG_ENDIANNESS: Literal["big"] = "big"
42+
43+
# eip-7691
44+
MAX_BLOBS_PER_BLOCK_ELECTRA = 9
45+
TARGET_BLOBS_PER_BLOCK_ELECTRA = 6
46+
MAX_BLOB_GAS_PER_BLOCK = 1179648
47+
TARGET_BLOB_GAS_PER_BLOCK = 786432
48+
BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE = 5007716
49+
3050

3151
# All forks must be listed here !!! in the order they were introduced !!!
3252
class Frontier(BaseFork, solc_name="homestead"):
@@ -543,6 +563,11 @@ def pre_allocation_blockchain(cls) -> Mapping:
543563
"""
544564
return {}
545565

566+
@classmethod
567+
def blobs_supported(cls) -> bool:
568+
"""Return whether blobs are supported in this fork."""
569+
return False
570+
546571

547572
class Homestead(Frontier):
548573
"""Homestead fork."""
@@ -1040,6 +1065,11 @@ def valid_opcodes(
10401065
Opcodes.MCOPY,
10411066
] + super(Cancun, cls).valid_opcodes()
10421067

1068+
@classmethod
1069+
def blobs_supported(cls) -> bool:
1070+
"""Return whether blobs are supported in this fork."""
1071+
return True
1072+
10431073

10441074
class Prague(Cancun):
10451075
"""Prague fork."""

src/ethereum_test_types/blob.py

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,41 @@
44
""" # noqa: E501
55

66
import random
7-
import sys
87
from enum import Enum
98
from hashlib import sha256
10-
from os.path import abspath, dirname, join, realpath
9+
from os.path import realpath
1110
from pathlib import Path
1211
from typing import List
1312

1413
import ckzg
1514

1615
from ethereum_test_base_types.base_types import Bytes, Hash
1716
from ethereum_test_base_types.pydantic import CamelModel
18-
from ethereum_test_forks import Cancun, Fork, Osaka, Prague
19-
20-
sys.path.insert(0, abspath(join(dirname(__file__), "../../"))) # TODO: find better workaround
21-
from tests.osaka.eip7594_peerdas.spec import Spec, ref_spec_7594
17+
from ethereum_test_forks import (
18+
# BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE,
19+
BLS_MODULUS,
20+
BYTES_PER_BLOB,
21+
BYTES_PER_COMMITMENT,
22+
BYTES_PER_FIELD_ELEMENT,
23+
# BYTES_PER_PROOF,
24+
# CELL_LENGTH,
25+
FIELD_ELEMENTS_PER_BLOB,
26+
KZG_ENDIANNESS,
27+
# MAX_BLOB_GAS_PER_BLOCK,
28+
# MAX_BLOBS_PER_BLOCK_ELECTRA,
29+
# TARGET_BLOB_GAS_PER_BLOCK,
30+
# TARGET_BLOBS_PER_BLOCK_ELECTRA,
31+
Cancun,
32+
Fork,
33+
Osaka,
34+
Prague,
35+
)
2236

2337
TRUSTED_SETUP_FILE_NAME = "blob_trusted_setup.txt"
2438
TRUSTED_SETUP_PATH = Path(realpath(__file__)).parent / TRUSTED_SETUP_FILE_NAME
2539
TRUSTED_SETUP = ckzg.load_trusted_setup(str(TRUSTED_SETUP_PATH), 0)
2640
print(f"{TRUSTED_SETUP_FILE_NAME} has been loaded")
2741

28-
REFERENCE_SPEC_GIT_PATH = ref_spec_7594.git_path
29-
REFERENCE_SPEC_VERSION = ref_spec_7594.version
30-
3142

3243
def get_eest_root_folder(marker_files=("pyproject.toml", ".git", "tests", "src")) -> Path:
3344
"""Search for a folder where all files/folders listed above exist (root of project)."""
@@ -88,6 +99,10 @@ def get_filepath(fork: str, seed: int, timestamp: int):
8899
@staticmethod
89100
def NewBlob(fork: Fork, seed: int = 0, timestamp: int = 0) -> "Blob": # noqa: N802
90101
"""Construct Blob instances. Fork-specific logic is encapsulated within nested functions.""" # noqa: E501
102+
assert fork.blobs_supported(), (
103+
f"You provided fork {fork.name()} but it does not support blobs!"
104+
) # TODO: why does mypy complain? this line works
105+
91106
fork_str: str = fork.name().lower()
92107

93108
# if this blob already exists then load from file
@@ -103,11 +118,11 @@ def generate_blob_data(rng_seed: int = 0) -> Bytes:
103118

104119
# generate blob
105120
ints: list[int] = [
106-
random.randrange(Spec.BLS_MODULUS) for _ in range(Spec.FIELD_ELEMENTS_PER_BLOB)
121+
random.randrange(BLS_MODULUS) for _ in range(FIELD_ELEMENTS_PER_BLOB)
107122
]
108123

109124
encoded: list[bytes] = [
110-
i.to_bytes(Spec.BYTES_PER_FIELD_ELEMENT, Spec.KZG_ENDIANNESS) for i in ints
125+
i.to_bytes(BYTES_PER_FIELD_ELEMENT, KZG_ENDIANNESS) for i in ints
111126
]
112127
blob: bytes = b"".join(encoded) # without 0x
113128

@@ -125,23 +140,23 @@ def get_name(fork_str: str, seed: int, timestamp: int) -> str:
125140
def get_commitment(data: Bytes) -> Bytes:
126141
"""Take a blob and returns a cryptographic commitment to it. Note: Each cell seems to hold a copy of this commitment.""" # noqa: E501
127142
# sanity check
128-
assert len(data) == Spec.BYTES_PER_BLOB, (
129-
f"Expected blob of length {Spec.BYTES_PER_BLOB} but got blob of length {len(data)}"
143+
assert len(data) == BYTES_PER_BLOB, (
144+
f"Expected blob of length {BYTES_PER_BLOB} but got blob of length {len(data)}"
130145
)
131146

132147
# calculate commitment
133148
commitment = ckzg.blob_to_kzg_commitment(data, TRUSTED_SETUP)
134149

135-
assert len(commitment) == Spec.BYTES_PER_COMMITMENT, (
136-
f"Expected {Spec.BYTES_PER_COMMITMENT} resulting commitments but got {len(commitment)} commitments" # noqa: E501
150+
assert len(commitment) == BYTES_PER_COMMITMENT, (
151+
f"Expected {BYTES_PER_COMMITMENT} resulting commitments but got {len(commitment)} commitments" # noqa: E501
137152
)
138153

139154
return commitment
140155

141156
def get_proof(data: Bytes) -> List[Bytes] | Bytes:
142157
if fork_str in ["cancun", "prague"]:
143158
z = 2 # 2 is one of many possible valid field elements z (https://github.com/ethereum/consensus-specs/blob/ad884507f7a1d5962cd3dfb5f7b3e41aab728c55/tests/core/pyspec/eth2spec/test/utils/kzg_tests.py#L58-L66)
144-
z_valid_size: bytes = z.to_bytes(Spec.BYTES_PER_FIELD_ELEMENT, byteorder="big")
159+
z_valid_size: bytes = z.to_bytes(BYTES_PER_FIELD_ELEMENT, byteorder="big")
145160
proof, _ = ckzg.compute_kzg_proof(data, z_valid_size, TRUSTED_SETUP)
146161
return proof
147162

@@ -368,7 +383,7 @@ def corrupt_byte(b: bytes) -> Bytes:
368383
self.proof = Bytes(b"".join(corrupt_byte(bytes([byte])) for byte in self.proof))
369384

370385

371-
# TODO: currently u can only run this file from ./blob.py but not from eest root, fix it
386+
# TODO: BlobFromFile supports both construction via blob parameters (works fine), and via provided path (here do some where testing which path is cwd if u use it within a test)
372387

373388
# TODO after merge: update test_blob_txs_full.py to make use of actual blobs
374389

@@ -405,12 +420,10 @@ def corrupt_byte(b: bytes) -> Bytes:
405420
# assert b.fork == restored.fork
406421
# assert b.timestamp == restored.timestamp
407422
# print(type(b.proof), len(b.proof))
408-
# print(Spec.BYTES_PER_FIELD_ELEMENT)
423+
# print(BYTES_PER_FIELD_ELEMENT)
409424
# print(len(b.data))
410425
# b.write_to_file()
411-
# c: Blob = Blob.LoadBlobFromFile(
412-
# "blob_" + "osaka" + "_" + str(myseed) + "_" + str(mytimestamp)
413-
# )
426+
# c: Blob = Blob.LoadBlobFromFile("blob_" + "osaka" + "_" + str(myseed) + "_" + str(mytimestamp))
414427
# assert b.data == c.data
415428
# assert b.commitment == c.commitment
416429
# assert b.proof == c.proof
@@ -437,25 +450,25 @@ def corrupt_byte(b: bytes) -> Bytes:
437450
# # "/home/user/Documents/execution-spec-tests/tests/cancun/eip4844_blobs/static_blobs/blob_cancun_1337.json" # noqa: E501
438451
# # ) # you must replace user with ur actual username as $USER not supported here
439452
# yyyyy: Blob = Blob.LoadBlobFromFile(
440-
# "../../tests/cancun/eip4844_blobs/static_blobs/blob_cancun_1337_999999.json"
453+
# "tests/cancun/eip4844_blobs/static_blobs/blob_cancun_1337_999999.json"
454+
# )
455+
# zzzzzzz: Blob = Blob.LoadBlobFromFile(
456+
# "./tests/cancun/eip4844_blobs/static_blobs/blob_cancun_1337_999999.json"
441457
# )
442458

443459

444460
# # test proof corruption
445461
# # osaka
446462
# testseed = 55
447-
# d: Blob = Blob.NewBlob(Osaka, testseed + 10)
448-
# oldValue = d.proof[0][5]
463+
# ddd: Blob = Blob.NewBlob(Osaka, testseed + 10)
464+
# oldValue = ddd.proof[0][5]
449465
# for m in Blob.ProofCorruptionMode:
450-
# d.corrupt_proof(m)
451-
# print("proof corruption works (osaka):", oldValue != d.proof[0][5])
466+
# ddd.corrupt_proof(m)
467+
# print("proof corruption works (osaka):", oldValue != ddd.proof[0][5])
452468
# # prague
453-
# e: Blob = Blob.NewBlob(Prague, testseed + 11)
454-
# oldValue = e.proof[5]
469+
# eeeeeeeeee: Blob = Blob.NewBlob(Prague, testseed + 11)
470+
# oldValue = eeeeeeeeee.proof[5]
455471
# for m in Blob.ProofCorruptionMode:
456-
# e.corrupt_proof(m)
457-
# print("proof corruption works (prague):", oldValue != e.proof[5])
472+
# eeeeeeeeee.corrupt_proof(m)
473+
# print("proof corruption works (prague):", oldValue != eeeeeeeeee.proof[5])
458474
# print("pydantic model works")
459-
460-
# ckzg.compute_cells(blob, TRUSTED_SETUP) returns a list of length 128
461-
# ckzg.compute_cells_and_kzg_proofs(blob, TRUSTED_SETUP)

tests/osaka/eip7594_peerdas/spec.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,4 @@ class Spec:
2222
https://eips.ethereum.org/EIPS/eip-7594.
2323
"""
2424

25-
FIELD_ELEMENTS_PER_BLOB = 4096
26-
BYTES_PER_FIELD_ELEMENT = 32
27-
BYTES_PER_BLOB = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT # 131072
28-
CELL_LENGTH = 2048
29-
BLS_MODULUS = 0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001 # EIP-2537: Main subgroup order = q # noqa: E501
30-
# due to BLS_MODULUS every blob byte (uint256) must be smaller than 116
31-
32-
# deneb constants that have not changed (https://github.com/ethereum/consensus-specs/blob/cc6996c22692d70e41b7a453d925172ee4b719ad/specs/deneb/polynomial-commitments.md?plain=1#L78)
33-
BYTES_PER_PROOF = 48
34-
BYTES_PER_COMMITMENT = 48
35-
KZG_ENDIANNESS: Literal["big"] = "big"
36-
37-
# eip-7691
38-
MAX_BLOBS_PER_BLOCK_ELECTRA = 9
39-
TARGET_BLOBS_PER_BLOCK_ELECTRA = 6
40-
MAX_BLOB_GAS_PER_BLOCK = 1179648
41-
TARGET_BLOB_GAS_PER_BLOCK = 786432
42-
BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE = 5007716
25+
pass

0 commit comments

Comments
 (0)