Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,11 @@ serve_docs: _copy_docs
# Checks
###############################################################################

FLAKE8_CONFIG = $(CURDIR)/flake8.ini
MYPY_CONFIG = $(CURDIR)/mypy.ini
PYLINT_CONFIG = $(CURDIR)/pylint.ini

PYLINT_SCOPE := $(foreach S,$(ALL_EXECUTABLE_SPEC_NAMES), $(PYSPEC_DIR)/eth2spec/$S)
MYPY_SCOPE := $(foreach S,$(ALL_EXECUTABLE_SPEC_NAMES), -p eth2spec.$S)
TEST_GENERATORS_DIR = ./tests/generators
MARKDOWN_FILES = $(wildcard $(SPEC_DIR)/*/*.md) \
$(wildcard $(SPEC_DIR)/*/*/*.md) \
$(wildcard $(SPEC_DIR)/_features/*/*.md) \
Expand All @@ -190,8 +188,7 @@ MARKDOWN_FILES = $(wildcard $(SPEC_DIR)/*/*.md) \
lint: pyspec
@$(MDFORMAT_VENV) --number $(MARKDOWN_FILES)
@$(CODESPELL_VENV) . --skip "./.git,$(VENV),$(PYSPEC_DIR)/.mypy_cache" -I .codespell-whitelist
@$(PYTHON_VENV) -m flake8 --config $(FLAKE8_CONFIG) $(PYSPEC_DIR)/eth2spec
@$(PYTHON_VENV) -m flake8 --config $(FLAKE8_CONFIG) $(TEST_GENERATORS_DIR)
@$(PYTHON_VENV) -m black $(CURDIR)/tests
@$(PYTHON_VENV) -m pylint --rcfile $(PYLINT_CONFIG) $(PYLINT_SCOPE)
@$(PYTHON_VENV) -m mypy --config-file $(MYPY_CONFIG) $(MYPY_SCOPE)

Expand Down
3 changes: 0 additions & 3 deletions flake8.ini

This file was deleted.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ test = [
"pytest==8.3.5",
]
lint = [
"black==25.1.0",
"codespell==2.4.1",
"flake8==7.2.0",
"mdformat-gfm-alerts==1.0.1",
"mdformat-gfm==0.4.1",
"mdformat-toc==0.3.0",
Expand All @@ -57,3 +57,6 @@ docs = [
"mkdocs-material==9.6.10",
"mkdocs==1.6.1",
]

[tool.black]
line-length = 100
2 changes: 1 addition & 1 deletion specs/_deprecated/sharding/validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def verify_sample(state: BeaconState, block: BeaconBlock, sample: SignedShardSam

# Verify KZG proof
verify_kzg_multiproof(block.body.payload_data.value.sharded_commitments_container.sharded_commitments[sample.row],
roots_in_rbo[sample.column * FIELD_ELEMENTS_PER_SAMPLE:(sample.column + 1) * FIELD_ELEMENTS_PER_SAMPLE]
roots_in_rbo[sample.column * FIELD_ELEMENTS_PER_SAMPLE:(sample.column + 1) * FIELD_ELEMENTS_PER_SAMPLE],
sample.data,
sample.proof)
```
Expand Down
3 changes: 2 additions & 1 deletion tests/core/pyspec/eth2spec/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# See setup.py about usage of VERSION.txt
import os
with open(os.path.join(os.path.dirname(__file__), 'VERSION.txt')) as f:

with open(os.path.join(os.path.dirname(__file__), "VERSION.txt")) as f:
__version__ = f.read().strip()
10 changes: 5 additions & 5 deletions tests/core/pyspec/eth2spec/config/config_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def parse_config_vars(conf: Dict[str, Any]) -> Dict[str, Any]:
out[k] = [int(item) if item.isdigit() else item for item in v]
elif isinstance(v, str) and v.startswith("0x"):
out[k] = bytes.fromhex(v[2:])
elif k != 'PRESET_BASE' and k != 'CONFIG_NAME':
elif k != "PRESET_BASE" and k != "CONFIG_NAME":
out[k] = int(v)
else:
out[k] = v
Expand All @@ -27,7 +27,7 @@ def load_preset(preset_files: Iterable[Union[Path, BinaryIO, TextIO]]) -> Dict[s
"""
preset = {}
for fork_file in preset_files:
yaml = YAML(typ='base')
yaml = YAML(typ="base")
fork_preset: dict = yaml.load(fork_file)
if fork_preset is None: # for empty YAML files
continue
Expand All @@ -43,7 +43,7 @@ def load_config_file(config_path: Union[Path, BinaryIO, TextIO]) -> Dict[str, An
"""
Loads the given configuration file.
"""
yaml = YAML(typ='base')
yaml = YAML(typ="base")
config_data = yaml.load(config_path)
return parse_config_vars(config_data)

Expand All @@ -56,8 +56,8 @@ def load_config_file(config_path: Union[Path, BinaryIO, TextIO]) -> Dict[str, An
def load_defaults(spec_configs_path: Path) -> None:
global mainnet_config_data, minimal_config_data

mainnet_config_data = load_config_file(spec_configs_path / 'mainnet.yaml')
minimal_config_data = load_config_file(spec_configs_path / 'minimal.yaml')
mainnet_config_data = load_config_file(spec_configs_path / "mainnet.yaml")
minimal_config_data = load_config_file(spec_configs_path / "minimal.yaml")

global loaded_defaults
loaded_defaults = True
20 changes: 14 additions & 6 deletions tests/core/pyspec/eth2spec/debug/decode.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from typing import Any
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import (
uint, Container, List, boolean,
Vector, ByteVector, ByteList, Union, View
uint,
Container,
List,
boolean,
Vector,
ByteVector,
ByteList,
Union,
View,
)


Expand All @@ -20,12 +27,13 @@ def decode(data: Any, typ):
for field_name, field_type in typ.fields().items():
temp[field_name] = decode(data[field_name], field_type)
if field_name + "_hash_tree_root" in data:
assert (data[field_name + "_hash_tree_root"][2:] ==
hash_tree_root(temp[field_name]).hex())
assert (
data[field_name + "_hash_tree_root"][2:]
== hash_tree_root(temp[field_name]).hex()
)
ret = typ(**temp)
if "hash_tree_root" in data:
assert (data["hash_tree_root"][2:] ==
hash_tree_root(ret).hex())
assert data["hash_tree_root"][2:] == hash_tree_root(ret).hex()
return ret
elif issubclass(typ, Union):
selector = int(data["selector"])
Expand Down
22 changes: 14 additions & 8 deletions tests/core/pyspec/eth2spec/debug/encode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from eth2spec.utils.ssz.ssz_impl import hash_tree_root, serialize
from eth2spec.utils.ssz.ssz_typing import (
uint, boolean,
Bitlist, Bitvector, Container, Vector, List, Union
uint,
boolean,
Bitlist,
Bitvector,
Container,
Vector,
List,
Union,
)


Expand All @@ -14,28 +20,28 @@ def encode(value, include_hash_tree_roots=False):
elif isinstance(value, boolean):
return value == 1
elif isinstance(value, (Bitlist, Bitvector)):
return '0x' + serialize(value).hex()
return "0x" + serialize(value).hex()
elif isinstance(value, list): # normal python lists
return [encode(element, include_hash_tree_roots) for element in value]
elif isinstance(value, (List, Vector)):
return [encode(element, include_hash_tree_roots) for element in value]
elif isinstance(value, bytes): # bytes, ByteList, ByteVector
return '0x' + value.hex()
return "0x" + value.hex()
elif isinstance(value, Container):
ret = {}
for field_name in value.fields().keys():
field_value = getattr(value, field_name)
ret[field_name] = encode(field_value, include_hash_tree_roots)
if include_hash_tree_roots:
ret[field_name + "_hash_tree_root"] = '0x' + hash_tree_root(field_value).hex()
ret[field_name + "_hash_tree_root"] = "0x" + hash_tree_root(field_value).hex()
if include_hash_tree_roots:
ret["hash_tree_root"] = '0x' + hash_tree_root(value).hex()
ret["hash_tree_root"] = "0x" + hash_tree_root(value).hex()
return ret
elif isinstance(value, Union):
inner_value = value.value()
return {
'selector': int(value.selector()),
'value': None if inner_value is None else encode(inner_value, include_hash_tree_roots)
"selector": int(value.selector()),
"value": None if inner_value is None else encode(inner_value, include_hash_tree_roots),
}
else:
raise Exception(f"Type not recognized: value={value}, typ={type(value)}")
63 changes: 42 additions & 21 deletions tests/core/pyspec/eth2spec/debug/random_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@
from typing import Type

from eth2spec.utils.ssz.ssz_typing import (
View, BasicView, uint, Container, List, boolean,
Vector, ByteVector, ByteList, Bitlist, Bitvector, Union
View,
BasicView,
uint,
Container,
List,
boolean,
Vector,
ByteVector,
ByteList,
Bitlist,
Bitvector,
Union,
)

# in bytes
Expand Down Expand Up @@ -35,12 +45,14 @@ def is_changing(self):
return self.value in [0, 4, 5]


def get_random_ssz_object(rng: Random,
typ: Type[View],
max_bytes_length: int,
max_list_length: int,
mode: RandomizationMode,
chaos: bool) -> View:
def get_random_ssz_object(
rng: Random,
typ: Type[View],
max_bytes_length: int,
max_list_length: int,
mode: RandomizationMode,
chaos: bool,
) -> View:
"""
Create an object for a given type, filled with random data.
:param rng: The random number generator to use.
Expand All @@ -56,24 +68,26 @@ def get_random_ssz_object(rng: Random,
if issubclass(typ, ByteList):
# ByteList array
if mode == RandomizationMode.mode_nil_count:
return typ(b'')
return typ(b"")
elif mode == RandomizationMode.mode_max_count:
return typ(get_random_bytes_list(rng, min(max_bytes_length, typ.limit())))
elif mode == RandomizationMode.mode_one_count:
return typ(get_random_bytes_list(rng, min(1, typ.limit())))
elif mode == RandomizationMode.mode_zero:
return typ(b'\x00' * min(1, typ.limit()))
return typ(b"\x00" * min(1, typ.limit()))
elif mode == RandomizationMode.mode_max:
return typ(b'\xff' * min(1, typ.limit()))
return typ(b"\xff" * min(1, typ.limit()))
else:
return typ(get_random_bytes_list(rng, rng.randint(0, min(max_bytes_length, typ.limit()))))
return typ(
get_random_bytes_list(rng, rng.randint(0, min(max_bytes_length, typ.limit())))
)
if issubclass(typ, ByteVector):
# Random byte vectors can be bigger than max bytes size, e.g. custody chunk data.
# No max-bytes-length limitation here.
if mode == RandomizationMode.mode_zero:
return typ(b'\x00' * typ.type_byte_length())
return typ(b"\x00" * typ.type_byte_length())
elif mode == RandomizationMode.mode_max:
return typ(b'\xff' * typ.type_byte_length())
return typ(b"\xff" * typ.type_byte_length())
else:
return typ(get_random_bytes_list(rng, typ.type_byte_length()))
elif issubclass(typ, (boolean, uint)):
Expand All @@ -99,7 +113,9 @@ def get_random_ssz_object(rng: Random,
elif mode == RandomizationMode.mode_nil_count:
length = 0

if typ.limit() < length: # SSZ imposes a hard limit on lists, we can't put in more than that
if (
typ.limit() < length
): # SSZ imposes a hard limit on lists, we can't put in more than that
length = typ.limit()

elem_type = typ.element_cls() if issubclass(typ, List) else boolean
Expand All @@ -110,11 +126,14 @@ def get_random_ssz_object(rng: Random,
elif issubclass(typ, Container):
fields = typ.fields()
# Container
return typ(**{
field_name:
get_random_ssz_object(rng, field_type, max_bytes_length, max_list_length, mode, chaos)
for field_name, field_type in fields.items()
})
return typ(
**{
field_name: get_random_ssz_object(
rng, field_type, max_bytes_length, max_list_length, mode, chaos
)
for field_name, field_type in fields.items()
}
)
elif issubclass(typ, Union):
options = typ.options()
selector: int
Expand All @@ -129,7 +148,9 @@ def get_random_ssz_object(rng: Random,
if elem_type is None:
elem = None
else:
elem = get_random_ssz_object(rng, elem_type, max_bytes_length, max_list_length, mode, chaos)
elem = get_random_ssz_object(
rng, elem_type, max_bytes_length, max_list_length, mode, chaos
)
return typ(selector=selector, value=elem)
else:
raise Exception(f"Type not recognized: typ={typ}")
Expand Down
Loading