diff --git a/Makefile b/Makefile index f81657d86e..13c90c871b 100644 --- a/Makefile +++ b/Makefile @@ -186,8 +186,8 @@ MARKDOWN_FILES = $(CURDIR)/README.md \ lint: pyspec @$(MDFORMAT_VENV) --number --wrap=80 $(MARKDOWN_FILES) @$(CODESPELL_VENV) . --skip "./.git,$(VENV),$(PYSPEC_DIR)/.mypy_cache" -I .codespell-whitelist - @$(PYTHON_VENV) -m isort --quiet $(CURDIR)/tests - @$(PYTHON_VENV) -m black --quiet $(CURDIR)/tests + @$(PYTHON_VENV) -m isort --quiet $(CURDIR)/tests $(CURDIR)/pysetup + @$(PYTHON_VENV) -m black --quiet $(CURDIR)/tests $(CURDIR)/pysetup @$(PYTHON_VENV) -m pylint --rcfile $(PYLINT_CONFIG) $(PYLINT_SCOPE) @$(PYTHON_VENV) -m mypy --config-file $(MYPY_CONFIG) $(MYPY_SCOPE) diff --git a/pysetup/constants.py b/pysetup/constants.py index a3de870bd7..05ec3fd1d5 100644 --- a/pysetup/constants.py +++ b/pysetup/constants.py @@ -1,19 +1,19 @@ # Definitions in context.py -PHASE0 = 'phase0' -ALTAIR = 'altair' -BELLATRIX = 'bellatrix' -CAPELLA = 'capella' -DENEB = 'deneb' -ELECTRA = 'electra' -FULU = 'fulu' -EIP6800 = 'eip6800' -EIP7441 = 'eip7441' -EIP7732 = 'eip7732' -EIP7805 = 'eip7805' +PHASE0 = "phase0" +ALTAIR = "altair" +BELLATRIX = "bellatrix" +CAPELLA = "capella" +DENEB = "deneb" +ELECTRA = "electra" +FULU = "fulu" +EIP6800 = "eip6800" +EIP7441 = "eip7441" +EIP7732 = "eip7732" +EIP7805 = "eip7805" # The helper functions that are used when defining constants -CONSTANT_DEP_SUNDRY_CONSTANTS_FUNCTIONS = ''' +CONSTANT_DEP_SUNDRY_CONSTANTS_FUNCTIONS = """ def ceillog2(x: int) -> uint64: if x < 1: raise ValueError(f"ceillog2 accepts only positive values, x={x}") @@ -24,10 +24,10 @@ def floorlog2(x: int) -> uint64: if x < 1: raise ValueError(f"floorlog2 accepts only positive values, x={x}") return uint64(x.bit_length() - 1) -''' +""" -OPTIMIZED_BLS_AGGREGATE_PUBKEYS = ''' +OPTIMIZED_BLS_AGGREGATE_PUBKEYS = """ def eth_aggregate_pubkeys(pubkeys: Sequence[BLSPubkey]) -> BLSPubkey: return bls.AggregatePKs(pubkeys) -''' +""" diff --git a/pysetup/helpers.py b/pysetup/helpers.py index 3295361019..be24f78cf5 100644 --- a/pysetup/helpers.py +++ b/pysetup/helpers.py @@ -1,11 +1,11 @@ import re -from typing import TypeVar, Dict, Union, List import textwrap from functools import reduce +from typing import Dict, List, TypeVar, Union from .constants import CONSTANT_DEP_SUNDRY_CONSTANTS_FUNCTIONS -from .spec_builders import spec_builders from .md_doc_paths import PREVIOUS_FORK_OF +from .spec_builders import spec_builders from .typing import ( ProtocolDefinition, SpecObject, @@ -23,9 +23,8 @@ def collect_prev_forks(fork: str) -> list[str]: def requires_mypy_type_ignore(value: str) -> bool: - return ( - value.startswith(('ByteVector')) - or (value.startswith(('Vector')) and any(k in value for k in ['ceillog2', 'floorlog2'])) + return value.startswith(("ByteVector")) or ( + value.startswith(("Vector")) and any(k in value for k in ["ceillog2", "floorlog2"]) ) @@ -34,22 +33,23 @@ def make_function_abstract(protocol_def: ProtocolDefinition, key: str): protocol_def.functions[key] = function[0] + "..." -def objects_to_spec(preset_name: str, - spec_object: SpecObject, - fork: str, - ordered_class_objects: Dict[str, str]) -> str: +def objects_to_spec( + preset_name: str, spec_object: SpecObject, fork: str, ordered_class_objects: Dict[str, str] +) -> str: """ Given all the objects that constitute a spec, combine them into a single pyfile. """ + def gen_new_type_definitions(custom_types: Dict[str, str]) -> str: - return ( - '\n\n'.join( - [ - f"class {key}({value}):\n pass\n" if not requires_mypy_type_ignore(value) + return "\n\n".join( + [ + ( + f"class {key}({value}):\n pass\n" + if not requires_mypy_type_ignore(value) else f"class {key}({value}): # type: ignore\n pass\n" - for key, value in custom_types.items() - ] - ) + ) + for key, value in custom_types.items() + ] ) new_type_definitions = gen_new_type_definitions(spec_object.custom_types) @@ -62,16 +62,16 @@ def gen_new_type_definitions(custom_types: Dict[str, str]) -> str: def format_protocol(protocol_name: str, protocol_def: ProtocolDefinition) -> str: abstract_functions = ["verify_and_notify_new_payload"] for key in protocol_def.functions.keys(): - if key in abstract_functions: + if key in abstract_functions: make_function_abstract(protocol_def, key) protocol = f"class {protocol_name}(Protocol):" for fn_source in protocol_def.functions.values(): - fn_source = fn_source.replace("self: "+protocol_name, "self") + fn_source = fn_source.replace("self: " + protocol_name, "self") protocol += "\n\n" + textwrap.indent(fn_source, " ") return protocol - protocols_spec = '\n\n\n'.join(format_protocol(k, v) for k, v in spec_object.protocols.items()) + protocols_spec = "\n\n\n".join(format_protocol(k, v) for k, v in spec_object.protocols.items()) for k in list(spec_object.functions): if k in [ "ceillog2", @@ -80,15 +80,21 @@ def format_protocol(protocol_name: str, protocol_def: ProtocolDefinition) -> str ]: del spec_object.functions[k] - functions = reduce(lambda fns, builder: builder.implement_optimizations(fns), builders, spec_object.functions) - functions_spec = '\n\n\n'.join(functions.values()) - ordered_class_objects_spec = '\n\n\n'.join(ordered_class_objects.values()) + functions = reduce( + lambda fns, builder: builder.implement_optimizations(fns), builders, spec_object.functions + ) + functions_spec = "\n\n\n".join(functions.values()) + ordered_class_objects_spec = "\n\n\n".join(ordered_class_objects.values()) # Access global dict of config vars for runtime configurables # Ignore variable between quotes and doubles quotes for name in spec_object.config_vars.keys(): - functions_spec = re.sub(r"(? str: if isinstance(vardef, list): @@ -96,19 +102,19 @@ def format_config_var(name: str, vardef) -> str: indent = " " * 4 lines = [f"{name}=("] for d in vardef: - line = indent*2 + "frozendict({\n" + line = indent * 2 + "frozendict({\n" for k, v in d.items(): line += indent * 3 + f'"{k}": {v},\n' - line += indent*2 + "})," + line += indent * 2 + "})," lines.append(line) lines.append(indent + "),") return "\n".join(lines) elif vardef.type_name is None: - out = f'{name}={vardef.value},' + out = f"{name}={vardef.value}," else: - out = f'{name}={vardef.type_name}({vardef.value}),' + out = f"{name}={vardef.type_name}({vardef.value})," if vardef.comment is not None: - out += f' # {vardef.comment}' + out += f" # {vardef.comment}" return out def format_config_var_param(value): @@ -118,56 +124,102 @@ def format_config_var_param(value): elif isinstance(value, VariableDefinition): return value.type_name if value.type_name is not None else "int" - config_spec = 'class Configuration(NamedTuple):\n' - config_spec += ' PRESET_BASE: str\n' - config_spec += '\n'.join(f' {k}: {format_config_var_param(v)}' for k, v in spec_object.config_vars.items()) - config_spec += '\n\n\nconfig = Configuration(\n' + config_spec = "class Configuration(NamedTuple):\n" + config_spec += " PRESET_BASE: str\n" + config_spec += "\n".join( + f" {k}: {format_config_var_param(v)}" for k, v in spec_object.config_vars.items() + ) + config_spec += "\n\n\nconfig = Configuration(\n" config_spec += f' PRESET_BASE="{preset_name}",\n' - config_spec += '\n'.join(' ' + format_config_var(k, v) for k, v in spec_object.config_vars.items()) - config_spec += '\n)\n' + config_spec += "\n".join( + " " + format_config_var(k, v) for k, v in spec_object.config_vars.items() + ) + config_spec += "\n)\n" def format_constant(name: str, vardef: VariableDefinition) -> str: if vardef.type_name is None: if vardef.type_hint is None: - out = f'{name} = {vardef.value}' + out = f"{name} = {vardef.value}" else: - out = f'{name}: {vardef.type_hint} = {vardef.value}' + out = f"{name}: {vardef.type_hint} = {vardef.value}" else: - out = f'{name} = {vardef.type_name}({vardef.value})' + out = f"{name} = {vardef.type_name}({vardef.value})" if vardef.comment is not None: - out += f' # {vardef.comment}' + out += f" # {vardef.comment}" return out # Merge all constant objects - hardcoded_ssz_dep_constants = reduce(lambda obj, builder: {**obj, **builder.hardcoded_ssz_dep_constants()}, builders, {}) - hardcoded_func_dep_presets = reduce(lambda obj, builder: {**obj, **builder.hardcoded_func_dep_presets(spec_object)}, builders, {}) + hardcoded_ssz_dep_constants = reduce( + lambda obj, builder: {**obj, **builder.hardcoded_ssz_dep_constants()}, builders, {} + ) + hardcoded_func_dep_presets = reduce( + lambda obj, builder: {**obj, **builder.hardcoded_func_dep_presets(spec_object)}, + builders, + {}, + ) # Concatenate all strings - imports = reduce(lambda txt, builder: (txt + "\n\n" + builder.imports(preset_name) ).strip("\n"), builders, "") - classes = reduce(lambda txt, builder: (txt + "\n\n" + builder.classes() ).strip("\n"), builders, "") - preparations = reduce(lambda txt, builder: (txt + "\n\n" + builder.preparations() ).strip("\n"), builders, "") - sundry_functions = reduce(lambda txt, builder: (txt + "\n\n" + builder.sundry_functions() ).strip("\n"), builders, "") + imports = reduce( + lambda txt, builder: (txt + "\n\n" + builder.imports(preset_name)).strip("\n"), builders, "" + ) + classes = reduce( + lambda txt, builder: (txt + "\n\n" + builder.classes()).strip("\n"), builders, "" + ) + preparations = reduce( + lambda txt, builder: (txt + "\n\n" + builder.preparations()).strip("\n"), builders, "" + ) + sundry_functions = reduce( + lambda txt, builder: (txt + "\n\n" + builder.sundry_functions()).strip("\n"), builders, "" + ) # Keep engine from the most recent fork - execution_engine_cls = reduce(lambda txt, builder: builder.execution_engine_cls() or txt, builders, "") + execution_engine_cls = reduce( + lambda txt, builder: builder.execution_engine_cls() or txt, builders, "" + ) # Remove deprecated constants - deprecate_constants = reduce(lambda obj, builder: obj.union(builder.deprecate_constants()), builders, set()) + deprecate_constants = reduce( + lambda obj, builder: obj.union(builder.deprecate_constants()), builders, set() + ) # constant_vars = {k: v for k, v in spec_object.constant_vars.items() if k not in deprecate_constants} - filtered_ssz_dep_constants = {k: v for k, v in hardcoded_ssz_dep_constants.items() if k not in deprecate_constants} + filtered_ssz_dep_constants = { + k: v for k, v in hardcoded_ssz_dep_constants.items() if k not in deprecate_constants + } # Remove deprecated presets - deprecate_presets = reduce(lambda obj, builder: obj.union(builder.deprecate_presets()), builders, set()) + deprecate_presets = reduce( + lambda obj, builder: obj.union(builder.deprecate_presets()), builders, set() + ) # preset_vars = {k: v for k, v in spec_object.constant_vars.items() if k not in deprecate_constants} - filtered_hardcoded_func_dep_presets = {k: v for k, v in hardcoded_func_dep_presets.items() if k not in deprecate_presets} - - constant_vars_spec = '# Constant vars\n' + '\n'.join(format_constant(k, v) for k, v in spec_object.constant_vars.items()) - preset_dep_constant_vars_spec = '# Preset computed constants\n' + '\n'.join(format_constant(k, v) for k, v in spec_object.preset_dep_constant_vars.items()) - preset_vars_spec = '# Preset vars\n' + '\n'.join(format_constant(k, v) for k, v in spec_object.preset_vars.items()) - ssz_dep_constants = '\n'.join(map(lambda x: '%s = %s' % (x, hardcoded_ssz_dep_constants[x]), hardcoded_ssz_dep_constants)) - ssz_dep_constants_verification = '\n'.join(map(lambda x: 'assert %s == %s' % (x, spec_object.ssz_dep_constants[x]), filtered_ssz_dep_constants)) - func_dep_presets_verification = '\n'.join(map(lambda x: 'assert %s == %s # noqa: E501' % (x, spec_object.func_dep_presets[x]), filtered_hardcoded_func_dep_presets)) + filtered_hardcoded_func_dep_presets = { + k: v for k, v in hardcoded_func_dep_presets.items() if k not in deprecate_presets + } + + constant_vars_spec = "# Constant vars\n" + "\n".join( + format_constant(k, v) for k, v in spec_object.constant_vars.items() + ) + preset_dep_constant_vars_spec = "# Preset computed constants\n" + "\n".join( + format_constant(k, v) for k, v in spec_object.preset_dep_constant_vars.items() + ) + preset_vars_spec = "# Preset vars\n" + "\n".join( + format_constant(k, v) for k, v in spec_object.preset_vars.items() + ) + ssz_dep_constants = "\n".join( + map(lambda x: "%s = %s" % (x, hardcoded_ssz_dep_constants[x]), hardcoded_ssz_dep_constants) + ) + ssz_dep_constants_verification = "\n".join( + map( + lambda x: "assert %s == %s" % (x, spec_object.ssz_dep_constants[x]), + filtered_ssz_dep_constants, + ) + ) + func_dep_presets_verification = "\n".join( + map( + lambda x: "assert %s == %s # noqa: E501" % (x, spec_object.func_dep_presets[x]), + filtered_hardcoded_func_dep_presets, + ) + ) spec_strs = [ imports, preparations, - f"fork = \'{fork}\'\n", + f"fork = '{fork}'\n", # The helper functions that some SSZ containers require. Need to be defined before `custom_type_dep_constants` CONSTANT_DEP_SUNDRY_CONSTANTS_FUNCTIONS, # The constants that some SSZ containers require. Need to be defined before `constants_spec` @@ -191,11 +243,12 @@ def format_constant(name: str, vardef: VariableDefinition) -> str: ssz_dep_constants_verification, func_dep_presets_verification, ] - return "\n\n\n".join([str.strip("\n") for str in spec_strs if str]) + "\n" + return "\n\n\n".join([str.strip("\n") for str in spec_strs if str]) + "\n" -def combine_protocols(old_protocols: Dict[str, ProtocolDefinition], - new_protocols: Dict[str, ProtocolDefinition]) -> Dict[str, ProtocolDefinition]: +def combine_protocols( + old_protocols: Dict[str, ProtocolDefinition], new_protocols: Dict[str, ProtocolDefinition] +) -> Dict[str, ProtocolDefinition]: for key, value in new_protocols.items(): if key not in old_protocols: old_protocols[key] = value @@ -205,7 +258,7 @@ def combine_protocols(old_protocols: Dict[str, ProtocolDefinition], return old_protocols -T = TypeVar('T') +T = TypeVar("T") def combine_dicts(old_dict: Dict[str, T], new_dict: Dict[str, T]) -> Dict[str, T]: @@ -213,12 +266,42 @@ def combine_dicts(old_dict: Dict[str, T], new_dict: Dict[str, T]) -> Dict[str, T ignored_dependencies = [ - 'bit', 'boolean', 'Vector', 'List', 'Container', 'BLSPubkey', 'BLSSignature', - 'Bytes1', 'Bytes4', 'Bytes8', 'Bytes20', 'Bytes31', 'Bytes32', 'Bytes48', 'Bytes96', 'Bitlist', 'Bitvector', - 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256', - 'bytes', 'byte', 'ByteList', 'ByteVector', - 'Dict', 'dict', 'field', 'ceillog2', 'floorlog2', 'Set', - 'Optional', 'Sequence', 'Tuple', + "bit", + "Bitlist", + "Bitvector", + "BLSPubkey", + "BLSSignature", + "boolean", + "byte", + "ByteList", + "bytes", + "Bytes1", + "Bytes20", + "Bytes31", + "Bytes32", + "Bytes4", + "Bytes48", + "Bytes8", + "Bytes96", + "ByteVector", + "ceillog2", + "Container", + "dict", + "Dict", + "field", + "floorlog2", + "List", + "Optional", + "Sequence", + "Set", + "Tuple", + "uint128", + "uint16", + "uint256", + "uint32", + "uint64", + "uint8", + "Vector", ] @@ -229,21 +312,26 @@ def dependency_order_class_objects(objects: Dict[str, str], custom_types: Dict[s items = list(objects.items()) for key, value in items: dependencies = [] - for line in value.split('\n'): - if not re.match(r'\s+\w+: .+', line): + for line in value.split("\n"): + if not re.match(r"\s+\w+: .+", line): continue # skip whitespace etc. - line = line[line.index(':') + 1:] # strip of field name - if '#' in line: - line = line[:line.index('#')] # strip of comment - dependencies.extend(re.findall(r'(\w+)', line)) # catch all legible words, potential dependencies - dependencies = filter(lambda x: '_' not in x and x.upper() != x, dependencies) # filter out constants + line = line[line.index(":") + 1 :] # strip of field name + if "#" in line: + line = line[: line.index("#")] # strip of comment + dependencies.extend( + re.findall(r"(\w+)", line) + ) # catch all legible words, potential dependencies + dependencies = filter( + lambda x: "_" not in x and x.upper() != x, dependencies + ) # filter out constants dependencies = filter(lambda x: x not in ignored_dependencies, dependencies) dependencies = filter(lambda x: x not in custom_types, dependencies) for dep in dependencies: key_list = list(objects.keys()) - for item in [dep, key] + key_list[key_list.index(dep)+1:]: + for item in [dep, key] + key_list[key_list.index(dep) + 1 :]: objects[item] = objects.pop(item) + def combine_ssz_objects(old_objects: Dict[str, str], new_objects: Dict[str, str]) -> Dict[str, str]: """ Takes in old spec and new spec ssz objects, combines them, @@ -261,9 +349,13 @@ def combine_spec_objects(spec0: SpecObject, spec1: SpecObject) -> SpecObject: protocols = combine_protocols(spec0.protocols, spec1.protocols) functions = combine_dicts(spec0.functions, spec1.functions) custom_types = combine_dicts(spec0.custom_types, spec1.custom_types) - preset_dep_custom_types = combine_dicts(spec0.preset_dep_custom_types, spec1.preset_dep_custom_types) + preset_dep_custom_types = combine_dicts( + spec0.preset_dep_custom_types, spec1.preset_dep_custom_types + ) constant_vars = combine_dicts(spec0.constant_vars, spec1.constant_vars) - preset_dep_constant_vars = combine_dicts(spec0.preset_dep_constant_vars, spec1.preset_dep_constant_vars) + preset_dep_constant_vars = combine_dicts( + spec0.preset_dep_constant_vars, spec1.preset_dep_constant_vars + ) preset_vars = combine_dicts(spec0.preset_vars, spec1.preset_vars) config_vars = combine_dicts(spec0.config_vars, spec1.config_vars) ssz_dep_constants = combine_dicts(spec0.ssz_dep_constants, spec1.ssz_dep_constants) @@ -295,7 +387,9 @@ def parse_config_vars(conf: Dict[str, str]) -> Dict[str, Union[str, List[Dict[st if isinstance(v, list): # A special case for list of records out[k] = v - elif isinstance(v, str) and (v.startswith("0x") or k == "PRESET_BASE" or k == "CONFIG_NAME"): + elif isinstance(v, str) and ( + v.startswith("0x") or k == "PRESET_BASE" or k == "CONFIG_NAME" + ): # Represent byte data with string, to avoid misinterpretation as big-endian int. # Everything except PRESET_BASE and CONFIG_NAME is either byte data or an integer. out[k] = f"'{v}'" diff --git a/pysetup/md_doc_paths.py b/pysetup/md_doc_paths.py index 18b0f6cf9a..7ae3a56f83 100644 --- a/pysetup/md_doc_paths.py +++ b/pysetup/md_doc_paths.py @@ -1,20 +1,19 @@ import os from .constants import ( - PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB, - ELECTRA, - FULU, EIP6800, EIP7441, EIP7732, EIP7805, + ELECTRA, + FULU, + PHASE0, ) - PREVIOUS_FORK_OF = { PHASE0: None, ALTAIR: PHASE0, @@ -31,13 +30,9 @@ ALL_FORKS = list(PREVIOUS_FORK_OF.keys()) -IGNORE_SPEC_FILES = [ - "specs/phase0/deposit-contract.md" -] +IGNORE_SPEC_FILES = ["specs/phase0/deposit-contract.md"] -EXTRA_SPEC_FILES = { - BELLATRIX: "sync/optimistic.md" -} +EXTRA_SPEC_FILES = {BELLATRIX: "sync/optimistic.md"} DEFAULT_ORDER = ( "beacon-chain", @@ -62,10 +57,10 @@ def is_post_fork(a, b) -> bool: def get_fork_directory(fork): - dir1 = f'specs/{fork}' + dir1 = f"specs/{fork}" if os.path.exists(dir1): return dir1 - dir2 = f'specs/_features/{fork}' + dir2 = f"specs/_features/{fork}" if os.path.exists(dir2): return dir2 raise FileNotFoundError(f"No directory found for fork: {fork}") @@ -90,7 +85,7 @@ def get_md_doc_paths(spec_fork: str) -> str: filepath = os.path.join(root, filename) filepaths.append(filepath) for filepath in sorted(filepaths, key=sort_key): - if filepath.endswith('.md') and filepath not in IGNORE_SPEC_FILES: + if filepath.endswith(".md") and filepath not in IGNORE_SPEC_FILES: md_doc_paths += filepath + "\n" # Append extra files if any if fork in EXTRA_SPEC_FILES: diff --git a/pysetup/spec_builders/__init__.py b/pysetup/spec_builders/__init__.py index c53e7b0a41..ba207753b5 100644 --- a/pysetup/spec_builders/__init__.py +++ b/pysetup/spec_builders/__init__.py @@ -1,21 +1,28 @@ -from .phase0 import Phase0SpecBuilder from .altair import AltairSpecBuilder from .bellatrix import BellatrixSpecBuilder from .capella import CapellaSpecBuilder from .deneb import DenebSpecBuilder -from .electra import ElectraSpecBuilder -from .fulu import FuluSpecBuilder from .eip6800 import EIP6800SpecBuilder from .eip7441 import EIP7441SpecBuilder from .eip7732 import EIP7732SpecBuilder from .eip7805 import EIP7805SpecBuilder - +from .electra import ElectraSpecBuilder +from .fulu import FuluSpecBuilder +from .phase0 import Phase0SpecBuilder spec_builders = { builder.fork: builder for builder in ( - Phase0SpecBuilder, AltairSpecBuilder, BellatrixSpecBuilder, CapellaSpecBuilder, DenebSpecBuilder, - ElectraSpecBuilder, FuluSpecBuilder, EIP6800SpecBuilder, EIP7441SpecBuilder, EIP7732SpecBuilder, + Phase0SpecBuilder, + AltairSpecBuilder, + BellatrixSpecBuilder, + CapellaSpecBuilder, + DenebSpecBuilder, + ElectraSpecBuilder, + FuluSpecBuilder, + EIP6800SpecBuilder, + EIP7441SpecBuilder, + EIP7732SpecBuilder, EIP7805SpecBuilder, ) } diff --git a/pysetup/spec_builders/altair.py b/pysetup/spec_builders/altair.py index 779d1df606..0185172e1e 100644 --- a/pysetup/spec_builders/altair.py +++ b/pysetup/spec_builders/altair.py @@ -1,7 +1,7 @@ from typing import Dict -from .base import BaseSpecBuilder from ..constants import ALTAIR, OPTIMIZED_BLS_AGGREGATE_PUBKEYS +from .base import BaseSpecBuilder class AltairSpecBuilder(BaseSpecBuilder): @@ -9,24 +9,24 @@ class AltairSpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str) -> str: - return f''' + return f""" from typing import NewType, Union as PyUnion from eth2spec.phase0 import {preset_name} as phase0 from eth2spec.test.helpers.merkle import build_proof from eth2spec.utils.ssz.ssz_typing import Path -''' +""" @classmethod def preparations(cls): - return ''' + return """ SSZVariableName = str GeneralizedIndex = int -''' +""" @classmethod def sundry_functions(cls) -> str: - return ''' + return """ def get_generalized_index(ssz_class: Any, *path: PyUnion[int, SSZVariableName]) -> GeneralizedIndex: ssz_path = Path(ssz_class) for item in path: @@ -36,15 +36,14 @@ def get_generalized_index(ssz_class: Any, *path: PyUnion[int, SSZVariableName]) def compute_merkle_proof(object: SSZObject, index: GeneralizedIndex) -> list[Bytes32]: - return build_proof(object.get_backing(), index)''' - + return build_proof(object.get_backing(), index)""" @classmethod def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]: return { - 'FINALIZED_ROOT_GINDEX': 'GeneralizedIndex(105)', - 'CURRENT_SYNC_COMMITTEE_GINDEX': 'GeneralizedIndex(54)', - 'NEXT_SYNC_COMMITTEE_GINDEX': 'GeneralizedIndex(55)', + "FINALIZED_ROOT_GINDEX": "GeneralizedIndex(105)", + "CURRENT_SYNC_COMMITTEE_GINDEX": "GeneralizedIndex(54)", + "NEXT_SYNC_COMMITTEE_GINDEX": "GeneralizedIndex(55)", } @classmethod diff --git a/pysetup/spec_builders/base.py b/pysetup/spec_builders/base.py index 5d65a3ab33..5dc31d1942 100644 --- a/pysetup/spec_builders/base.py +++ b/pysetup/spec_builders/base.py @@ -1,6 +1,7 @@ from abc import ABC, abstractmethod -from typing import Sequence, Dict, Set from pathlib import Path +from typing import Dict, Sequence, Set + class BaseSpecBuilder(ABC): @property diff --git a/pysetup/spec_builders/bellatrix.py b/pysetup/spec_builders/bellatrix.py index a66a1b42df..43bb928c8d 100644 --- a/pysetup/spec_builders/bellatrix.py +++ b/pysetup/spec_builders/bellatrix.py @@ -1,16 +1,17 @@ -from .base import BaseSpecBuilder from ..constants import BELLATRIX +from .base import BaseSpecBuilder + class BellatrixSpecBuilder(BaseSpecBuilder): fork: str = BELLATRIX @classmethod def imports(cls, preset_name: str): - return f''' + return f""" from typing import Protocol from eth2spec.altair import {preset_name} as altair from eth2spec.utils.ssz.ssz_typing import Bytes8, Bytes20, ByteList, ByteVector -''' +""" @classmethod def sundry_functions(cls) -> str: diff --git a/pysetup/spec_builders/capella.py b/pysetup/spec_builders/capella.py index 6630b94f1e..c659b08bce 100644 --- a/pysetup/spec_builders/capella.py +++ b/pysetup/spec_builders/capella.py @@ -1,7 +1,7 @@ from typing import Dict -from .base import BaseSpecBuilder from ..constants import CAPELLA +from .base import BaseSpecBuilder class CapellaSpecBuilder(BaseSpecBuilder): @@ -9,12 +9,12 @@ class CapellaSpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str): - return f''' + return f""" from eth2spec.bellatrix import {preset_name} as bellatrix -''' +""" @classmethod def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]: return { - 'EXECUTION_PAYLOAD_GINDEX': 'GeneralizedIndex(25)', + "EXECUTION_PAYLOAD_GINDEX": "GeneralizedIndex(25)", } diff --git a/pysetup/spec_builders/deneb.py b/pysetup/spec_builders/deneb.py index d73b8612f7..3487d425db 100644 --- a/pysetup/spec_builders/deneb.py +++ b/pysetup/spec_builders/deneb.py @@ -1,6 +1,7 @@ from typing import Dict -from .base import BaseSpecBuilder + from ..constants import DENEB +from .base import BaseSpecBuilder class DenebSpecBuilder(BaseSpecBuilder): @@ -8,13 +9,13 @@ class DenebSpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str): - return f''' + return f""" from eth2spec.capella import {preset_name} as capella -''' +""" @classmethod def classes(cls): - return f''' + return f""" class BLSFieldElement(bls.Scalar): pass @@ -26,22 +27,22 @@ def __init__(self, evals: Optional[Sequence[BLSFieldElement]] = None): if len(evals) != FIELD_ELEMENTS_PER_BLOB: raise ValueError("expected FIELD_ELEMENTS_PER_BLOB evals") super().__init__(evals) -''' +""" @classmethod def preparations(cls): - return ''' + return """ T = TypeVar('T') # For generic function TPoint = TypeVar('TPoint') # For generic function. G1 or G2 point. -''' +""" @classmethod def sundry_functions(cls) -> str: - return ''' + return """ def retrieve_blobs_and_proofs(beacon_block_root: Root) -> Tuple[Sequence[Blob], Sequence[KZGProof]]: # pylint: disable=unused-argument return [], [] -''' +""" @classmethod def execution_engine_cls(cls) -> str: @@ -79,9 +80,10 @@ def verify_and_notify_new_payload(self: ExecutionEngine, EXECUTION_ENGINE = NoopExecutionEngine()""" - @classmethod def hardcoded_func_dep_presets(cls, spec_object) -> Dict[str, str]: return { - 'KZG_COMMITMENT_INCLUSION_PROOF_DEPTH': spec_object.preset_vars['KZG_COMMITMENT_INCLUSION_PROOF_DEPTH'].value, + "KZG_COMMITMENT_INCLUSION_PROOF_DEPTH": spec_object.preset_vars[ + "KZG_COMMITMENT_INCLUSION_PROOF_DEPTH" + ].value, } diff --git a/pysetup/spec_builders/eip6800.py b/pysetup/spec_builders/eip6800.py index a3a2bd1c28..0111698ada 100644 --- a/pysetup/spec_builders/eip6800.py +++ b/pysetup/spec_builders/eip6800.py @@ -1,7 +1,7 @@ from typing import Dict -from .base import BaseSpecBuilder from ..constants import EIP6800 +from .base import BaseSpecBuilder class EIP6800SpecBuilder(BaseSpecBuilder): @@ -9,7 +9,7 @@ class EIP6800SpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str): - return f''' + return f""" from eth2spec.deneb import {preset_name} as deneb from eth2spec.utils.ssz.ssz_typing import Bytes31 -''' +""" diff --git a/pysetup/spec_builders/eip7441.py b/pysetup/spec_builders/eip7441.py index 2b12014eab..998dd7270b 100644 --- a/pysetup/spec_builders/eip7441.py +++ b/pysetup/spec_builders/eip7441.py @@ -1,6 +1,7 @@ from typing import Dict -from .base import BaseSpecBuilder + from ..constants import EIP7441 +from .base import BaseSpecBuilder class EIP7441SpecBuilder(BaseSpecBuilder): @@ -8,15 +9,15 @@ class EIP7441SpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str): - return f''' + return f""" from eth2spec.capella import {preset_name} as capella import curdleproofs import json -''' +""" @classmethod def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]: constants = { - 'EXECUTION_PAYLOAD_GINDEX': 'GeneralizedIndex(41)', + "EXECUTION_PAYLOAD_GINDEX": "GeneralizedIndex(41)", } return {**super().hardcoded_ssz_dep_constants(), **constants} diff --git a/pysetup/spec_builders/eip7732.py b/pysetup/spec_builders/eip7732.py index c9f2ef8496..3a50da1072 100644 --- a/pysetup/spec_builders/eip7732.py +++ b/pysetup/spec_builders/eip7732.py @@ -1,7 +1,7 @@ from typing import Dict, Set -from .base import BaseSpecBuilder from ..constants import EIP7732 +from .base import BaseSpecBuilder class EIP7732SpecBuilder(BaseSpecBuilder): @@ -9,28 +9,31 @@ class EIP7732SpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str): - return f''' + return f""" from eth2spec.electra import {preset_name} as electra -''' +""" @classmethod def sundry_functions(cls) -> str: - return ''' + return """ def concat_generalized_indices(*indices: GeneralizedIndex) -> GeneralizedIndex: o = GeneralizedIndex(1) for i in indices: o = GeneralizedIndex(o * bit_floor(i) + (i - bit_floor(i))) - return o''' - + return o""" @classmethod def deprecate_constants(cls) -> Set[str]: - return set([ - 'EXECUTION_PAYLOAD_GINDEX', - ]) + return set( + [ + "EXECUTION_PAYLOAD_GINDEX", + ] + ) @classmethod def deprecate_presets(cls) -> Set[str]: - return set([ - 'KZG_COMMITMENT_INCLUSION_PROOF_DEPTH', - ]) + return set( + [ + "KZG_COMMITMENT_INCLUSION_PROOF_DEPTH", + ] + ) diff --git a/pysetup/spec_builders/eip7805.py b/pysetup/spec_builders/eip7805.py index 8507e8b9b1..6feaa3c5e8 100644 --- a/pysetup/spec_builders/eip7805.py +++ b/pysetup/spec_builders/eip7805.py @@ -1,5 +1,5 @@ -from .base import BaseSpecBuilder from ..constants import EIP7805 +from .base import BaseSpecBuilder class EIP7805SpecBuilder(BaseSpecBuilder): diff --git a/pysetup/spec_builders/electra.py b/pysetup/spec_builders/electra.py index f473dbadc3..fc12be2cbc 100644 --- a/pysetup/spec_builders/electra.py +++ b/pysetup/spec_builders/electra.py @@ -1,6 +1,7 @@ from typing import Dict -from .base import BaseSpecBuilder + from ..constants import ELECTRA +from .base import BaseSpecBuilder class ElectraSpecBuilder(BaseSpecBuilder): @@ -8,20 +9,19 @@ class ElectraSpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str): - return f''' + return f""" from eth2spec.deneb import {preset_name} as deneb from eth2spec.utils.ssz.ssz_impl import ssz_serialize, ssz_deserialize -''' +""" @classmethod def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]: return { - 'FINALIZED_ROOT_GINDEX_ELECTRA': 'GeneralizedIndex(169)', - 'CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA': 'GeneralizedIndex(86)', - 'NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA': 'GeneralizedIndex(87)', + "FINALIZED_ROOT_GINDEX_ELECTRA": "GeneralizedIndex(169)", + "CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA": "GeneralizedIndex(86)", + "NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA": "GeneralizedIndex(87)", } - @classmethod def execution_engine_cls(cls) -> str: return """ @@ -58,4 +58,4 @@ def verify_and_notify_new_payload(self: ExecutionEngine, return True -EXECUTION_ENGINE = NoopExecutionEngine()""" \ No newline at end of file +EXECUTION_ENGINE = NoopExecutionEngine()""" diff --git a/pysetup/spec_builders/fulu.py b/pysetup/spec_builders/fulu.py index f5cf0af329..fe97a8fb22 100644 --- a/pysetup/spec_builders/fulu.py +++ b/pysetup/spec_builders/fulu.py @@ -1,7 +1,7 @@ from typing import Dict -from .base import BaseSpecBuilder from ..constants import FULU +from .base import BaseSpecBuilder class FuluSpecBuilder(BaseSpecBuilder): @@ -9,15 +9,14 @@ class FuluSpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str): - return f''' + return f""" from frozendict import frozendict from eth2spec.electra import {preset_name} as electra -''' - +""" @classmethod def classes(cls): - return f''' + return f""" class PolynomialCoeff(list): def __init__(self, coeffs: Sequence[BLSFieldElement]): if len(coeffs) > FIELD_ELEMENTS_PER_EXT_BLOB: @@ -41,7 +40,7 @@ def __init__(self, evals: Optional[Sequence[BLSFieldElement]] = None): if len(evals) != FIELD_ELEMENTS_PER_CELL: raise ValueError("expected FIELD_ELEMENTS_PER_CELL coeffs") super().__init__(evals) -''' +""" @classmethod def sundry_functions(cls) -> str: @@ -54,5 +53,7 @@ def retrieve_column_sidecars(beacon_block_root: Root) -> Sequence[DataColumnSide @classmethod def hardcoded_func_dep_presets(cls, spec_object) -> Dict[str, str]: return { - 'KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH': spec_object.preset_vars['KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH'].value, + "KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH": spec_object.preset_vars[ + "KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH" + ].value, } diff --git a/pysetup/spec_builders/phase0.py b/pysetup/spec_builders/phase0.py index 2cc44562d3..1561907db4 100644 --- a/pysetup/spec_builders/phase0.py +++ b/pysetup/spec_builders/phase0.py @@ -1,5 +1,5 @@ -from .base import BaseSpecBuilder from ..constants import PHASE0 +from .base import BaseSpecBuilder class Phase0SpecBuilder(BaseSpecBuilder): @@ -7,7 +7,7 @@ class Phase0SpecBuilder(BaseSpecBuilder): @classmethod def imports(cls, preset_name: str) -> str: - return '''from lru import LRU + return """from lru import LRU from dataclasses import ( dataclass, field, @@ -23,13 +23,13 @@ def imports(cls, preset_name: str) -> str: from eth2spec.utils.ssz.ssz_typing import Bitvector # noqa: F401 from eth2spec.utils import bls from eth2spec.utils.hash_function import hash -''' +""" @classmethod def preparations(cls) -> str: - return ''' + return """ SSZObject = TypeVar('SSZObject', bound=View) -''' +""" @classmethod def sundry_functions(cls) -> str: diff --git a/pysetup/typing.py b/pysetup/typing.py index d66d330d22..9450a90ff3 100644 --- a/pysetup/typing.py +++ b/pysetup/typing.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Dict, NamedTuple, Optional, List +from typing import Dict, List, NamedTuple, Optional class ProtocolDefinition(NamedTuple):