2020-01-25 00:26:10 +00:00
|
|
|
from setuptools import setup, find_packages, Command
|
|
|
|
from setuptools.command.build_py import build_py
|
|
|
|
from distutils import dir_util
|
|
|
|
from distutils.util import convert_path
|
2021-05-18 10:12:01 +00:00
|
|
|
from pathlib import Path
|
2020-01-25 00:26:10 +00:00
|
|
|
import os
|
|
|
|
import re
|
2021-04-19 13:41:45 +00:00
|
|
|
import string
|
2021-05-12 00:40:23 +00:00
|
|
|
import textwrap
|
2021-05-19 17:18:42 +00:00
|
|
|
from typing import Dict, NamedTuple, List, Sequence, Optional, TypeVar
|
2021-04-09 12:34:51 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2021-04-20 21:55:46 +00:00
|
|
|
import ast
|
2021-09-24 14:33:14 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2021-04-20 21:55:46 +00:00
|
|
|
# NOTE: have to programmatically include third-party dependencies in `setup.py`.
|
2021-09-24 14:33:14 +00:00
|
|
|
def installPackage(package: str):
|
|
|
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package])
|
|
|
|
|
2021-05-18 10:12:01 +00:00
|
|
|
RUAMEL_YAML_VERSION = "ruamel.yaml==0.16.5"
|
|
|
|
try:
|
|
|
|
import ruamel.yaml
|
|
|
|
except ImportError:
|
2021-09-24 14:33:14 +00:00
|
|
|
installPackage(RUAMEL_YAML_VERSION)
|
2021-05-18 10:12:01 +00:00
|
|
|
|
|
|
|
from ruamel.yaml import YAML
|
|
|
|
|
2021-04-20 21:55:46 +00:00
|
|
|
MARKO_VERSION = "marko==1.0.2"
|
|
|
|
try:
|
|
|
|
import marko
|
|
|
|
except ImportError:
|
2021-09-24 14:33:14 +00:00
|
|
|
installPackage(MARKO_VERSION)
|
2021-04-20 21:55:46 +00:00
|
|
|
|
|
|
|
from marko.block import Heading, FencedCode, LinkRefDef, BlankLine
|
|
|
|
from marko.inline import CodeSpan
|
|
|
|
from marko.ext.gfm import gfm
|
2021-09-24 10:45:37 +00:00
|
|
|
from marko.ext.gfm.elements import Table
|
2021-04-20 21:55:46 +00:00
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2021-03-12 12:26:26 +00:00
|
|
|
# Definitions in context.py
|
|
|
|
PHASE0 = 'phase0'
|
|
|
|
ALTAIR = 'altair'
|
2021-04-05 14:59:09 +00:00
|
|
|
MERGE = 'merge'
|
2021-03-12 12:26:26 +00:00
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
# The helper functions that are used when defining constants
|
|
|
|
CONSTANT_DEP_SUNDRY_CONSTANTS_FUNCTIONS = '''
|
|
|
|
def ceillog2(x: int) -> uint64:
|
|
|
|
if x < 1:
|
|
|
|
raise ValueError(f"ceillog2 accepts only positive values, x={x}")
|
|
|
|
return uint64((x - 1).bit_length())
|
|
|
|
|
|
|
|
|
|
|
|
def floorlog2(x: int) -> uint64:
|
|
|
|
if x < 1:
|
|
|
|
raise ValueError(f"floorlog2 accepts only positive values, x={x}")
|
|
|
|
return uint64(x.bit_length() - 1)
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
2021-05-24 18:51:08 +00:00
|
|
|
OPTIMIZED_BLS_AGGREGATE_PUBKEYS = '''
|
2021-08-03 13:46:11 +00:00
|
|
|
def eth_aggregate_pubkeys(pubkeys: Sequence[BLSPubkey]) -> BLSPubkey:
|
2021-05-24 18:51:08 +00:00
|
|
|
return bls.AggregatePKs(pubkeys)
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
2021-05-12 00:40:23 +00:00
|
|
|
class ProtocolDefinition(NamedTuple):
|
|
|
|
# just function definitions currently. May expand with configuration vars in future.
|
|
|
|
functions: Dict[str, str]
|
|
|
|
|
|
|
|
|
2021-05-18 10:12:01 +00:00
|
|
|
class VariableDefinition(NamedTuple):
|
2021-05-18 11:59:26 +00:00
|
|
|
type_name: Optional[str]
|
2021-05-18 10:12:01 +00:00
|
|
|
value: str
|
|
|
|
comment: Optional[str] # e.g. "noqa: E501"
|
|
|
|
|
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
class SpecObject(NamedTuple):
|
|
|
|
functions: Dict[str, str]
|
2021-05-12 00:40:23 +00:00
|
|
|
protocols: Dict[str, ProtocolDefinition]
|
2020-01-25 00:26:10 +00:00
|
|
|
custom_types: Dict[str, str]
|
2021-05-18 10:12:01 +00:00
|
|
|
constant_vars: Dict[str, VariableDefinition]
|
|
|
|
preset_vars: Dict[str, VariableDefinition]
|
|
|
|
config_vars: Dict[str, VariableDefinition]
|
2020-12-09 09:35:22 +00:00
|
|
|
ssz_dep_constants: Dict[str, str] # the constants that depend on ssz_objects
|
2020-01-25 00:26:10 +00:00
|
|
|
ssz_objects: Dict[str, str]
|
2020-07-03 02:48:57 +00:00
|
|
|
dataclasses: Dict[str, str]
|
|
|
|
|
|
|
|
|
2021-04-20 21:55:46 +00:00
|
|
|
def _get_name_from_heading(heading: Heading) -> Optional[str]:
|
|
|
|
last_child = heading.children[-1]
|
|
|
|
if isinstance(last_child, CodeSpan):
|
|
|
|
return last_child.children
|
|
|
|
return None
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
2021-04-20 21:55:46 +00:00
|
|
|
def _get_source_from_code_block(block: FencedCode) -> str:
|
|
|
|
return block.children[0].children.strip()
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2021-04-20 21:55:46 +00:00
|
|
|
|
|
|
|
def _get_function_name_from_source(source: str) -> str:
|
|
|
|
fn = ast.parse(source).body[0]
|
|
|
|
return fn.name
|
|
|
|
|
|
|
|
|
2021-05-12 00:40:23 +00:00
|
|
|
def _get_self_type_from_source(source: str) -> Optional[str]:
|
|
|
|
fn = ast.parse(source).body[0]
|
|
|
|
args = fn.args.args
|
|
|
|
if len(args) == 0:
|
|
|
|
return None
|
|
|
|
if args[0].arg != 'self':
|
|
|
|
return None
|
|
|
|
if args[0].annotation is None:
|
|
|
|
return None
|
|
|
|
return args[0].annotation.id
|
|
|
|
|
|
|
|
|
2021-04-20 21:55:46 +00:00
|
|
|
def _get_class_info_from_source(source: str) -> (str, Optional[str]):
|
|
|
|
class_def = ast.parse(source).body[0]
|
|
|
|
base = class_def.bases[0]
|
|
|
|
if isinstance(base, ast.Name):
|
|
|
|
parent_class = base.id
|
|
|
|
else:
|
|
|
|
# NOTE: SSZ definition derives from earlier phase...
|
|
|
|
# e.g. `phase0.SignedBeaconBlock`
|
|
|
|
# TODO: check for consistency with other phases
|
|
|
|
parent_class = None
|
|
|
|
return class_def.name, parent_class
|
|
|
|
|
|
|
|
|
|
|
|
def _is_constant_id(name: str) -> bool:
|
|
|
|
if name[0] not in string.ascii_uppercase + '_':
|
|
|
|
return False
|
|
|
|
return all(map(lambda c: c in string.ascii_uppercase + '_' + string.digits, name[1:]))
|
|
|
|
|
|
|
|
|
|
|
|
ETH2_SPEC_COMMENT_PREFIX = "eth2spec:"
|
|
|
|
|
|
|
|
|
|
|
|
def _get_eth2_spec_comment(child: LinkRefDef) -> Optional[str]:
|
|
|
|
_, _, title = child._parse_info
|
|
|
|
if not (title[0] == "(" and title[len(title)-1] == ")"):
|
|
|
|
return None
|
|
|
|
title = title[1:len(title)-1]
|
|
|
|
if not title.startswith(ETH2_SPEC_COMMENT_PREFIX):
|
|
|
|
return None
|
|
|
|
return title[len(ETH2_SPEC_COMMENT_PREFIX):].strip()
|
|
|
|
|
|
|
|
|
2021-05-18 10:12:01 +00:00
|
|
|
def _parse_value(name: str, typed_value: str) -> VariableDefinition:
|
|
|
|
comment = None
|
|
|
|
if name == "BLS12_381_Q":
|
2021-05-19 15:15:34 +00:00
|
|
|
comment = "noqa: E501"
|
2021-05-18 10:12:01 +00:00
|
|
|
|
|
|
|
typed_value = typed_value.strip()
|
|
|
|
if '(' not in typed_value:
|
2021-05-18 11:59:26 +00:00
|
|
|
return VariableDefinition(type_name=None, value=typed_value, comment=comment)
|
2021-05-18 10:12:01 +00:00
|
|
|
i = typed_value.index('(')
|
|
|
|
type_name = typed_value[:i]
|
|
|
|
|
|
|
|
return VariableDefinition(type_name=type_name, value=typed_value[i+1:-1], comment=comment)
|
|
|
|
|
|
|
|
|
|
|
|
def get_spec(file_name: Path, preset: Dict[str, str], config: Dict[str, str]) -> SpecObject:
|
2020-01-25 00:26:10 +00:00
|
|
|
functions: Dict[str, str] = {}
|
2021-05-12 00:40:23 +00:00
|
|
|
protocols: Dict[str, ProtocolDefinition] = {}
|
2021-05-18 10:12:01 +00:00
|
|
|
constant_vars: Dict[str, VariableDefinition] = {}
|
|
|
|
preset_vars: Dict[str, VariableDefinition] = {}
|
|
|
|
config_vars: Dict[str, VariableDefinition] = {}
|
2020-12-09 09:35:22 +00:00
|
|
|
ssz_dep_constants: Dict[str, str] = {}
|
2020-01-25 00:26:10 +00:00
|
|
|
ssz_objects: Dict[str, str] = {}
|
2020-07-03 02:48:57 +00:00
|
|
|
dataclasses: Dict[str, str] = {}
|
2020-01-25 00:26:10 +00:00
|
|
|
custom_types: Dict[str, str] = {}
|
2021-04-20 21:55:46 +00:00
|
|
|
|
|
|
|
with open(file_name) as source_file:
|
|
|
|
document = gfm.parse(source_file.read())
|
|
|
|
|
|
|
|
current_name = None
|
|
|
|
should_skip = False
|
|
|
|
for child in document.children:
|
|
|
|
if isinstance(child, BlankLine):
|
|
|
|
continue
|
|
|
|
if should_skip:
|
|
|
|
should_skip = False
|
|
|
|
continue
|
|
|
|
if isinstance(child, Heading):
|
|
|
|
current_name = _get_name_from_heading(child)
|
|
|
|
elif isinstance(child, FencedCode):
|
|
|
|
if child.lang != "python":
|
|
|
|
continue
|
|
|
|
source = _get_source_from_code_block(child)
|
|
|
|
if source.startswith("def"):
|
|
|
|
current_name = _get_function_name_from_source(source)
|
2021-05-12 00:40:23 +00:00
|
|
|
self_type_name = _get_self_type_from_source(source)
|
|
|
|
function_def = "\n".join(line.rstrip() for line in source.splitlines())
|
|
|
|
if self_type_name is None:
|
|
|
|
functions[current_name] = function_def
|
|
|
|
else:
|
|
|
|
if self_type_name not in protocols:
|
|
|
|
protocols[self_type_name] = ProtocolDefinition(functions={})
|
|
|
|
protocols[self_type_name].functions[current_name] = function_def
|
2021-04-20 21:55:46 +00:00
|
|
|
elif source.startswith("@dataclass"):
|
|
|
|
dataclasses[current_name] = "\n".join(line.rstrip() for line in source.splitlines())
|
|
|
|
elif source.startswith("class"):
|
|
|
|
class_name, parent_class = _get_class_info_from_source(source)
|
|
|
|
# check consistency with spec
|
|
|
|
assert class_name == current_name
|
|
|
|
if parent_class:
|
|
|
|
assert parent_class == "Container"
|
|
|
|
# NOTE: trim whitespace from spec
|
|
|
|
ssz_objects[current_name] = "\n".join(line.rstrip() for line in source.splitlines())
|
|
|
|
else:
|
2021-10-05 15:01:12 +00:00
|
|
|
raise Exception("unrecognized python code element: " + source)
|
2021-04-20 21:55:46 +00:00
|
|
|
elif isinstance(child, Table):
|
|
|
|
for row in child.children:
|
|
|
|
cells = row.children
|
|
|
|
if len(cells) >= 2:
|
|
|
|
name_cell = cells[0]
|
|
|
|
name = name_cell.children[0].children
|
2021-05-18 10:12:01 +00:00
|
|
|
|
2021-04-20 21:55:46 +00:00
|
|
|
value_cell = cells[1]
|
|
|
|
value = value_cell.children[0].children
|
|
|
|
if isinstance(value, list):
|
|
|
|
# marko parses `**X**` as a list containing a X
|
|
|
|
value = value[0].children
|
2021-05-18 10:12:01 +00:00
|
|
|
|
|
|
|
if not _is_constant_id(name):
|
|
|
|
# Check for short type declarations
|
2021-08-27 01:12:19 +00:00
|
|
|
if value.startswith(("uint", "Bytes", "ByteList", "Union")):
|
2021-05-18 10:12:01 +00:00
|
|
|
custom_types[name] = value
|
|
|
|
continue
|
|
|
|
|
|
|
|
if value.startswith("get_generalized_index"):
|
|
|
|
ssz_dep_constants[name] = value
|
|
|
|
continue
|
|
|
|
|
|
|
|
value_def = _parse_value(name, value)
|
|
|
|
if name in preset:
|
|
|
|
preset_vars[name] = VariableDefinition(value_def.type_name, preset[name], value_def.comment)
|
|
|
|
elif name in config:
|
|
|
|
config_vars[name] = VariableDefinition(value_def.type_name, config[name], value_def.comment)
|
|
|
|
else:
|
|
|
|
constant_vars[name] = value_def
|
|
|
|
|
2021-04-20 21:55:46 +00:00
|
|
|
elif isinstance(child, LinkRefDef):
|
|
|
|
comment = _get_eth2_spec_comment(child)
|
2021-04-23 15:03:49 +00:00
|
|
|
if comment == "skip":
|
|
|
|
should_skip = True
|
2021-05-06 17:23:50 +00:00
|
|
|
|
2020-12-09 09:35:22 +00:00
|
|
|
return SpecObject(
|
|
|
|
functions=functions,
|
2021-05-12 00:40:23 +00:00
|
|
|
protocols=protocols,
|
2020-12-09 09:35:22 +00:00
|
|
|
custom_types=custom_types,
|
2021-05-18 10:12:01 +00:00
|
|
|
constant_vars=constant_vars,
|
|
|
|
preset_vars=preset_vars,
|
|
|
|
config_vars=config_vars,
|
2020-12-09 09:35:22 +00:00
|
|
|
ssz_dep_constants=ssz_dep_constants,
|
|
|
|
ssz_objects=ssz_objects,
|
|
|
|
dataclasses=dataclasses,
|
|
|
|
)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
2021-04-16 03:29:10 +00:00
|
|
|
class SpecBuilder(ABC):
|
2021-04-09 14:26:32 +00:00
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def fork(self) -> str:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
@classmethod
|
|
|
|
@abstractmethod
|
2021-05-18 11:59:26 +00:00
|
|
|
def imports(cls, preset_name: str) -> str:
|
2021-04-09 12:34:51 +00:00
|
|
|
"""
|
2021-04-09 14:26:32 +00:00
|
|
|
Import objects from other libraries.
|
2021-04-09 14:17:01 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@abstractmethod
|
|
|
|
def preparations(cls) -> str:
|
|
|
|
"""
|
|
|
|
Define special types/constants for building pyspec or call functions.
|
2021-04-09 12:34:51 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@abstractmethod
|
|
|
|
def sundry_functions(cls) -> str:
|
|
|
|
"""
|
|
|
|
The functions that are (1) defined abstractly in specs or (2) adjusted for getting better performance.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@abstractmethod
|
|
|
|
def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]:
|
|
|
|
"""
|
|
|
|
The constants that are required for SSZ objects.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@abstractmethod
|
2021-05-18 10:12:01 +00:00
|
|
|
def hardcoded_custom_type_dep_constants(cls) -> Dict[str, str]: # TODO
|
2021-04-09 12:34:51 +00:00
|
|
|
"""
|
|
|
|
The constants that are required for custom types.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2021-05-24 18:51:08 +00:00
|
|
|
@classmethod
|
2021-05-27 19:02:44 +00:00
|
|
|
@abstractmethod
|
2021-05-24 18:51:08 +00:00
|
|
|
def implement_optimizations(cls, functions: Dict[str, str]) -> Dict[str, str]:
|
2021-05-27 19:02:44 +00:00
|
|
|
raise NotImplementedError()
|
2021-05-24 18:51:08 +00:00
|
|
|
|
2021-04-16 03:29:10 +00:00
|
|
|
@classmethod
|
|
|
|
@abstractmethod
|
2021-05-18 11:59:26 +00:00
|
|
|
def build_spec(cls, preset_name: str,
|
|
|
|
source_files: List[Path], preset_files: Sequence[Path], config_file: Path) -> str:
|
2021-04-16 03:29:10 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
|
|
|
|
#
|
2021-04-16 03:29:10 +00:00
|
|
|
# Phase0SpecBuilder
|
2021-04-09 12:34:51 +00:00
|
|
|
#
|
2021-04-16 03:29:10 +00:00
|
|
|
class Phase0SpecBuilder(SpecBuilder):
|
2021-04-09 14:26:32 +00:00
|
|
|
fork: str = PHASE0
|
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
@classmethod
|
2021-05-18 11:59:26 +00:00
|
|
|
def imports(cls, preset_name: str) -> str:
|
2021-04-09 13:28:58 +00:00
|
|
|
return '''from lru import LRU
|
2020-01-25 00:26:10 +00:00
|
|
|
from dataclasses import (
|
|
|
|
dataclass,
|
|
|
|
field,
|
|
|
|
)
|
2021-04-09 13:28:58 +00:00
|
|
|
from typing import (
|
2021-05-18 14:08:30 +00:00
|
|
|
Any, Callable, Dict, Set, Sequence, Tuple, Optional, TypeVar, NamedTuple
|
2021-04-09 13:28:58 +00:00
|
|
|
)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2020-07-15 17:36:19 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_impl import hash_tree_root, copy, uint_to_bytes
|
2020-01-25 00:26:10 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import (
|
2020-06-25 09:52:05 +00:00
|
|
|
View, boolean, Container, List, Vector, uint8, uint32, uint64,
|
2021-04-09 14:17:01 +00:00
|
|
|
Bytes1, Bytes4, Bytes32, Bytes48, Bytes96, Bitlist)
|
|
|
|
from eth2spec.utils.ssz.ssz_typing import Bitvector # noqa: F401
|
2020-01-25 00:26:10 +00:00
|
|
|
from eth2spec.utils import bls
|
|
|
|
from eth2spec.utils.hash_function import hash
|
2021-04-09 14:17:01 +00:00
|
|
|
'''
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2021-04-09 14:17:01 +00:00
|
|
|
@classmethod
|
|
|
|
def preparations(cls) -> str:
|
2021-04-09 14:26:32 +00:00
|
|
|
return '''
|
|
|
|
SSZObject = TypeVar('SSZObject', bound=View)
|
2020-01-25 00:26:10 +00:00
|
|
|
'''
|
2020-08-12 12:48:52 +00:00
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
@classmethod
|
|
|
|
def sundry_functions(cls) -> str:
|
|
|
|
return '''
|
2020-04-17 09:27:57 +00:00
|
|
|
def get_eth1_data(block: Eth1Block) -> Eth1Data:
|
|
|
|
"""
|
|
|
|
A stub function return mocking Eth1Data.
|
|
|
|
"""
|
2020-05-20 19:23:59 +00:00
|
|
|
return Eth1Data(
|
|
|
|
deposit_root=block.deposit_root,
|
|
|
|
deposit_count=block.deposit_count,
|
|
|
|
block_hash=hash_tree_root(block))
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
2020-03-20 19:38:36 +00:00
|
|
|
def cache_this(key_fn, value_fn, lru_size): # type: ignore
|
|
|
|
cache_dict = LRU(size=lru_size)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
def wrapper(*args, **kw): # type: ignore
|
|
|
|
key = key_fn(*args, **kw)
|
|
|
|
nonlocal cache_dict
|
|
|
|
if key not in cache_dict:
|
|
|
|
cache_dict[key] = value_fn(*args, **kw)
|
|
|
|
return cache_dict[key]
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2020-03-20 19:38:36 +00:00
|
|
|
_compute_shuffled_index = compute_shuffled_index
|
|
|
|
compute_shuffled_index = cache_this(
|
|
|
|
lambda index, index_count, seed: (index, index_count, seed),
|
|
|
|
_compute_shuffled_index, lru_size=SLOTS_PER_EPOCH * 3)
|
|
|
|
|
|
|
|
_get_total_active_balance = get_total_active_balance
|
|
|
|
get_total_active_balance = cache_this(
|
2020-03-23 19:35:50 +00:00
|
|
|
lambda state: (state.validators.hash_tree_root(), compute_epoch_at_slot(state.slot)),
|
2020-03-20 19:38:36 +00:00
|
|
|
_get_total_active_balance, lru_size=10)
|
|
|
|
|
2020-01-25 21:10:03 +00:00
|
|
|
_get_base_reward = get_base_reward
|
2020-01-25 00:26:10 +00:00
|
|
|
get_base_reward = cache_this(
|
2020-03-20 19:38:36 +00:00
|
|
|
lambda state, index: (state.validators.hash_tree_root(), state.slot, index),
|
2020-03-23 19:35:50 +00:00
|
|
|
_get_base_reward, lru_size=2048)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2020-06-17 18:04:40 +00:00
|
|
|
_get_committee_count_per_slot = get_committee_count_per_slot
|
|
|
|
get_committee_count_per_slot = cache_this(
|
2020-01-25 00:26:10 +00:00
|
|
|
lambda state, epoch: (state.validators.hash_tree_root(), epoch),
|
2020-06-17 18:04:40 +00:00
|
|
|
_get_committee_count_per_slot, lru_size=SLOTS_PER_EPOCH * 3)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2020-01-25 21:10:03 +00:00
|
|
|
_get_active_validator_indices = get_active_validator_indices
|
2020-01-25 00:26:10 +00:00
|
|
|
get_active_validator_indices = cache_this(
|
|
|
|
lambda state, epoch: (state.validators.hash_tree_root(), epoch),
|
2020-03-20 19:38:36 +00:00
|
|
|
_get_active_validator_indices, lru_size=3)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2020-01-25 21:10:03 +00:00
|
|
|
_get_beacon_committee = get_beacon_committee
|
2020-01-25 00:26:10 +00:00
|
|
|
get_beacon_committee = cache_this(
|
|
|
|
lambda state, slot, index: (state.validators.hash_tree_root(), state.randao_mixes.hash_tree_root(), slot, index),
|
2020-03-20 19:38:36 +00:00
|
|
|
_get_beacon_committee, lru_size=SLOTS_PER_EPOCH * MAX_COMMITTEES_PER_SLOT * 3)
|
2020-03-11 18:46:47 +00:00
|
|
|
|
|
|
|
_get_matching_target_attestations = get_matching_target_attestations
|
|
|
|
get_matching_target_attestations = cache_this(
|
|
|
|
lambda state, epoch: (state.hash_tree_root(), epoch),
|
2020-03-20 19:38:36 +00:00
|
|
|
_get_matching_target_attestations, lru_size=10)
|
2020-03-11 18:46:47 +00:00
|
|
|
|
|
|
|
_get_matching_head_attestations = get_matching_head_attestations
|
|
|
|
get_matching_head_attestations = cache_this(
|
|
|
|
lambda state, epoch: (state.hash_tree_root(), epoch),
|
2020-03-20 19:38:36 +00:00
|
|
|
_get_matching_head_attestations, lru_size=10)
|
|
|
|
|
|
|
|
_get_attesting_indices = get_attesting_indices
|
2020-03-23 19:35:50 +00:00
|
|
|
get_attesting_indices = cache_this(
|
2020-06-29 12:56:36 +00:00
|
|
|
lambda state, data, bits: (
|
|
|
|
state.randao_mixes.hash_tree_root(),
|
|
|
|
state.validators.hash_tree_root(), data.hash_tree_root(), bits.hash_tree_root()
|
|
|
|
),
|
2020-03-23 19:35:50 +00:00
|
|
|
_get_attesting_indices, lru_size=SLOTS_PER_EPOCH * MAX_COMMITTEES_PER_SLOT * 3)'''
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
@classmethod
|
|
|
|
def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def hardcoded_custom_type_dep_constants(cls) -> Dict[str, str]:
|
|
|
|
return {}
|
|
|
|
|
2021-05-27 19:02:44 +00:00
|
|
|
@classmethod
|
|
|
|
def implement_optimizations(cls, functions: Dict[str, str]) -> Dict[str, str]:
|
|
|
|
return functions
|
|
|
|
|
2021-04-16 03:29:10 +00:00
|
|
|
@classmethod
|
2021-05-18 11:59:26 +00:00
|
|
|
def build_spec(cls, preset_name: str,
|
|
|
|
source_files: Sequence[Path], preset_files: Sequence[Path], config_file: Path) -> str:
|
|
|
|
return _build_spec(preset_name, cls.fork, source_files, preset_files, config_file)
|
2021-04-16 03:29:10 +00:00
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
|
|
|
|
#
|
2021-04-16 03:29:10 +00:00
|
|
|
# AltairSpecBuilder
|
2021-04-09 12:34:51 +00:00
|
|
|
#
|
2021-04-16 03:29:10 +00:00
|
|
|
class AltairSpecBuilder(Phase0SpecBuilder):
|
2021-04-09 14:26:32 +00:00
|
|
|
fork: str = ALTAIR
|
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
@classmethod
|
2021-05-18 11:59:26 +00:00
|
|
|
def imports(cls, preset_name: str) -> str:
|
|
|
|
return super().imports(preset_name) + '\n' + f'''
|
2021-07-22 14:36:41 +00:00
|
|
|
from typing import NewType, Union as PyUnion
|
2021-04-09 12:34:51 +00:00
|
|
|
|
2021-05-18 11:59:26 +00:00
|
|
|
from eth2spec.phase0 import {preset_name} as phase0
|
2021-04-09 14:17:01 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import Path
|
|
|
|
'''
|
2021-04-09 12:34:51 +00:00
|
|
|
|
2021-04-09 14:17:01 +00:00
|
|
|
@classmethod
|
|
|
|
def preparations(cls):
|
|
|
|
return super().preparations() + '\n' + '''
|
2021-04-09 12:34:51 +00:00
|
|
|
SSZVariableName = str
|
|
|
|
GeneralizedIndex = NewType('GeneralizedIndex', int)
|
|
|
|
'''
|
2021-04-09 14:17:01 +00:00
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
@classmethod
|
|
|
|
def sundry_functions(cls) -> str:
|
|
|
|
return super().sundry_functions() + '\n\n' + '''
|
2021-07-22 14:36:41 +00:00
|
|
|
def get_generalized_index(ssz_class: Any, *path: Sequence[PyUnion[int, SSZVariableName]]) -> GeneralizedIndex:
|
2020-12-09 09:35:22 +00:00
|
|
|
ssz_path = Path(ssz_class)
|
|
|
|
for item in path:
|
|
|
|
ssz_path = ssz_path / item
|
2021-03-12 12:26:26 +00:00
|
|
|
return GeneralizedIndex(ssz_path.gindex())'''
|
2020-12-09 09:35:22 +00:00
|
|
|
|
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
@classmethod
|
|
|
|
def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]:
|
|
|
|
constants = {
|
|
|
|
'FINALIZED_ROOT_INDEX': 'GeneralizedIndex(105)',
|
|
|
|
'NEXT_SYNC_COMMITTEE_INDEX': 'GeneralizedIndex(55)',
|
|
|
|
}
|
|
|
|
return {**super().hardcoded_ssz_dep_constants(), **constants}
|
|
|
|
|
2021-05-24 18:51:08 +00:00
|
|
|
@classmethod
|
|
|
|
def implement_optimizations(cls, functions: Dict[str, str]) -> Dict[str, str]:
|
2021-08-03 13:46:11 +00:00
|
|
|
if "eth_aggregate_pubkeys" in functions:
|
|
|
|
functions["eth_aggregate_pubkeys"] = OPTIMIZED_BLS_AGGREGATE_PUBKEYS.strip()
|
2021-05-27 19:21:04 +00:00
|
|
|
return super().implement_optimizations(functions)
|
2021-04-09 12:34:51 +00:00
|
|
|
|
|
|
|
#
|
2021-04-16 03:29:10 +00:00
|
|
|
# MergeSpecBuilder
|
2021-04-09 12:34:51 +00:00
|
|
|
#
|
2021-07-22 14:36:41 +00:00
|
|
|
class MergeSpecBuilder(AltairSpecBuilder):
|
2021-04-09 14:26:32 +00:00
|
|
|
fork: str = MERGE
|
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
@classmethod
|
2021-05-18 11:59:26 +00:00
|
|
|
def imports(cls, preset_name: str):
|
|
|
|
return super().imports(preset_name) + f'''
|
2021-05-12 00:40:23 +00:00
|
|
|
from typing import Protocol
|
2021-07-22 14:36:41 +00:00
|
|
|
from eth2spec.altair import {preset_name} as altair
|
2021-10-19 21:46:18 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import Bytes8, Bytes20, ByteList, ByteVector, uint256
|
2021-04-09 14:17:01 +00:00
|
|
|
'''
|
2021-04-09 12:34:51 +00:00
|
|
|
|
2021-04-09 14:17:01 +00:00
|
|
|
@classmethod
|
|
|
|
def preparations(cls):
|
2021-05-18 11:59:26 +00:00
|
|
|
return super().preparations()
|
2021-04-09 12:34:51 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def sundry_functions(cls) -> str:
|
|
|
|
return super().sundry_functions() + '\n\n' + """
|
2021-04-09 12:21:51 +00:00
|
|
|
ExecutionState = Any
|
2021-04-05 14:59:09 +00:00
|
|
|
|
|
|
|
|
2021-10-18 05:32:29 +00:00
|
|
|
def get_pow_block(hash: Bytes32) -> Optional[PowBlock]:
|
2021-09-10 10:07:26 +00:00
|
|
|
return PowBlock(block_hash=hash, parent_hash=Bytes32(), total_difficulty=uint256(0), difficulty=uint256(0))
|
2021-04-05 14:59:09 +00:00
|
|
|
|
|
|
|
|
2021-04-09 12:21:51 +00:00
|
|
|
def get_execution_state(execution_state_root: Bytes32) -> ExecutionState:
|
2021-04-05 14:59:09 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def get_pow_chain_head() -> PowBlock:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-05-12 00:40:23 +00:00
|
|
|
class NoopExecutionEngine(ExecutionEngine):
|
2021-04-05 14:59:09 +00:00
|
|
|
|
2021-09-22 17:52:15 +00:00
|
|
|
def execute_payload(self: ExecutionEngine, execution_payload: ExecutionPayload) -> bool:
|
2021-05-12 00:40:23 +00:00
|
|
|
return True
|
|
|
|
|
2021-10-19 00:10:27 +00:00
|
|
|
def notify_forkchoice_updated(self: ExecutionEngine,
|
|
|
|
head_block_hash: Hash32,
|
|
|
|
finalized_block_hash: Hash32,
|
2021-11-04 09:58:48 +00:00
|
|
|
payload_attributes: Optional[PayloadAttributes]) -> Optional[PayloadId]:
|
2021-09-20 14:57:45 +00:00
|
|
|
pass
|
|
|
|
|
2021-09-23 09:03:43 +00:00
|
|
|
def get_payload(self: ExecutionEngine, payload_id: PayloadId) -> ExecutionPayload:
|
2021-05-12 00:40:23 +00:00
|
|
|
raise NotImplementedError("no default block production")
|
2021-04-05 14:59:09 +00:00
|
|
|
|
|
|
|
|
2021-05-12 00:40:23 +00:00
|
|
|
EXECUTION_ENGINE = NoopExecutionEngine()"""
|
2021-04-05 14:59:09 +00:00
|
|
|
|
|
|
|
|
2021-04-09 12:34:51 +00:00
|
|
|
@classmethod
|
|
|
|
def hardcoded_custom_type_dep_constants(cls) -> str:
|
|
|
|
constants = {
|
2021-10-31 13:05:02 +00:00
|
|
|
'MAX_BYTES_PER_TRANSACTION': 'uint64(2**30)',
|
2021-04-09 12:34:51 +00:00
|
|
|
}
|
|
|
|
return {**super().hardcoded_custom_type_dep_constants(), **constants}
|
2021-04-05 16:08:30 +00:00
|
|
|
|
|
|
|
|
2021-04-16 03:29:10 +00:00
|
|
|
spec_builders = {
|
|
|
|
builder.fork: builder
|
2021-04-20 21:56:07 +00:00
|
|
|
for builder in (Phase0SpecBuilder, AltairSpecBuilder, MergeSpecBuilder)
|
2021-04-05 14:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-18 10:09:30 +00:00
|
|
|
def is_spec_defined_type(value: str) -> bool:
|
|
|
|
return value.startswith('ByteList') or value.startswith('Union')
|
|
|
|
|
|
|
|
|
2021-05-18 11:59:26 +00:00
|
|
|
def objects_to_spec(preset_name: str,
|
|
|
|
spec_object: SpecObject,
|
|
|
|
builder: SpecBuilder,
|
|
|
|
ordered_class_objects: Dict[str, str]) -> str:
|
2020-01-25 00:26:10 +00:00
|
|
|
"""
|
|
|
|
Given all the objects that constitute a spec, combine them into a single pyfile.
|
|
|
|
"""
|
|
|
|
new_type_definitions = (
|
|
|
|
'\n\n'.join(
|
|
|
|
[
|
|
|
|
f"class {key}({value}):\n pass\n"
|
|
|
|
for key, value in spec_object.custom_types.items()
|
2021-06-18 10:09:30 +00:00
|
|
|
if not is_spec_defined_type(value)
|
2021-04-05 14:59:09 +00:00
|
|
|
]
|
|
|
|
)
|
2021-06-18 10:09:30 +00:00
|
|
|
+ ('\n\n' if len([key for key, value in spec_object.custom_types.items() if is_spec_defined_type(value)]) > 0 else '')
|
2021-04-05 14:59:09 +00:00
|
|
|
+ '\n\n'.join(
|
|
|
|
[
|
|
|
|
f"{key} = {value}\n"
|
|
|
|
for key, value in spec_object.custom_types.items()
|
2021-06-18 10:09:30 +00:00
|
|
|
if is_spec_defined_type(value)
|
2020-01-25 00:26:10 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2021-05-12 00:40:23 +00:00
|
|
|
|
|
|
|
def format_protocol(protocol_name: str, protocol_def: ProtocolDefinition) -> str:
|
|
|
|
protocol = f"class {protocol_name}(Protocol):"
|
|
|
|
for fn_source in protocol_def.functions.values():
|
|
|
|
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())
|
2020-01-25 00:26:10 +00:00
|
|
|
for k in list(spec_object.functions):
|
2020-12-16 07:00:06 +00:00
|
|
|
if "ceillog2" in k or "floorlog2" in k:
|
2020-01-25 00:26:10 +00:00
|
|
|
del spec_object.functions[k]
|
2021-05-24 18:51:08 +00:00
|
|
|
functions = builder.implement_optimizations(spec_object.functions)
|
|
|
|
functions_spec = '\n\n\n'.join(functions.values())
|
2021-05-18 10:12:01 +00:00
|
|
|
|
2021-05-18 10:48:42 +00:00
|
|
|
# Access global dict of config vars for runtime configurables
|
|
|
|
for name in spec_object.config_vars.keys():
|
2021-10-19 03:25:50 +00:00
|
|
|
functions_spec = re.sub(r"\b%s\b" % name, 'config.' + name, functions_spec)
|
2021-05-18 10:48:42 +00:00
|
|
|
|
|
|
|
def format_config_var(name: str, vardef: VariableDefinition) -> str:
|
2021-05-18 11:59:26 +00:00
|
|
|
if vardef.type_name is None:
|
2021-05-31 13:20:50 +00:00
|
|
|
out = f'{name}={vardef.value},'
|
2021-05-18 11:59:26 +00:00
|
|
|
else:
|
|
|
|
out = f'{name}={vardef.type_name}({vardef.value}),'
|
2021-05-18 10:48:42 +00:00
|
|
|
if vardef.comment is not None:
|
|
|
|
out += f' # {vardef.comment}'
|
|
|
|
return out
|
|
|
|
|
2021-05-18 14:08:30 +00:00
|
|
|
config_spec = 'class Configuration(NamedTuple):\n'
|
2021-05-18 11:59:26 +00:00
|
|
|
config_spec += ' PRESET_BASE: str\n'
|
|
|
|
config_spec += '\n'.join(f' {k}: {v.type_name if v.type_name is not None else "int"}'
|
|
|
|
for k, v in spec_object.config_vars.items())
|
2021-05-18 10:48:42 +00:00
|
|
|
config_spec += '\n\n\nconfig = Configuration(\n'
|
2021-05-18 11:59:26 +00:00
|
|
|
config_spec += f' PRESET_BASE="{preset_name}",\n'
|
2021-05-18 10:48:42 +00:00
|
|
|
config_spec += '\n'.join(' ' + format_config_var(k, v) for k, v in spec_object.config_vars.items())
|
|
|
|
config_spec += '\n)\n'
|
|
|
|
|
2021-05-18 10:12:01 +00:00
|
|
|
def format_constant(name: str, vardef: VariableDefinition) -> str:
|
2021-05-18 11:59:26 +00:00
|
|
|
if vardef.type_name is None:
|
|
|
|
out = f'{name} = {vardef.value}'
|
|
|
|
else:
|
|
|
|
out = f'{name} = {vardef.type_name}({vardef.value})'
|
2021-05-18 10:12:01 +00:00
|
|
|
if vardef.comment is not None:
|
2021-05-18 10:48:42 +00:00
|
|
|
out += f' # {vardef.comment}'
|
2021-05-18 10:12:01 +00:00
|
|
|
return out
|
|
|
|
|
2021-05-18 12:31:50 +00:00
|
|
|
constant_vars_spec = '# Constant vars\n' + '\n'.join(format_constant(k, v) for k, v in spec_object.constant_vars.items())
|
|
|
|
preset_vars_spec = '# Preset vars\n' + '\n'.join(format_constant(k, v) for k, v in spec_object.preset_vars.items())
|
2021-04-20 21:55:46 +00:00
|
|
|
ordered_class_objects_spec = '\n\n\n'.join(ordered_class_objects.values())
|
2021-04-16 03:29:10 +00:00
|
|
|
ssz_dep_constants = '\n'.join(map(lambda x: '%s = %s' % (x, builder.hardcoded_ssz_dep_constants()[x]), builder.hardcoded_ssz_dep_constants()))
|
|
|
|
ssz_dep_constants_verification = '\n'.join(map(lambda x: 'assert %s == %s' % (x, spec_object.ssz_dep_constants[x]), builder.hardcoded_ssz_dep_constants()))
|
|
|
|
custom_type_dep_constants = '\n'.join(map(lambda x: '%s = %s' % (x, builder.hardcoded_custom_type_dep_constants()[x]), builder.hardcoded_custom_type_dep_constants()))
|
2020-01-25 00:26:10 +00:00
|
|
|
spec = (
|
2021-05-18 11:59:26 +00:00
|
|
|
builder.imports(preset_name)
|
2021-04-16 03:29:10 +00:00
|
|
|
+ builder.preparations()
|
|
|
|
+ '\n\n' + f"fork = \'{builder.fork}\'\n"
|
2021-04-05 14:59:09 +00:00
|
|
|
# The constants that some SSZ containers require. Need to be defined before `new_type_definitions`
|
2021-04-09 12:34:51 +00:00
|
|
|
+ ('\n\n' + custom_type_dep_constants + '\n' if custom_type_dep_constants != '' else '')
|
2020-01-25 00:26:10 +00:00
|
|
|
+ '\n\n' + new_type_definitions
|
2021-04-09 12:34:51 +00:00
|
|
|
+ '\n' + CONSTANT_DEP_SUNDRY_CONSTANTS_FUNCTIONS
|
2021-03-12 12:26:26 +00:00
|
|
|
# The constants that some SSZ containers require. Need to be defined before `constants_spec`
|
2021-04-09 12:34:51 +00:00
|
|
|
+ ('\n\n' + ssz_dep_constants if ssz_dep_constants != '' else '')
|
2021-05-18 10:12:01 +00:00
|
|
|
+ '\n\n' + constant_vars_spec
|
|
|
|
+ '\n\n' + preset_vars_spec
|
2021-05-18 10:48:42 +00:00
|
|
|
+ '\n\n\n' + config_spec
|
2020-07-03 02:48:57 +00:00
|
|
|
+ '\n\n' + ordered_class_objects_spec
|
2021-05-12 00:40:23 +00:00
|
|
|
+ ('\n\n\n' + protocols_spec if protocols_spec != '' else '')
|
2021-04-20 21:55:46 +00:00
|
|
|
+ '\n\n\n' + functions_spec
|
|
|
|
+ '\n\n' + builder.sundry_functions()
|
2021-04-09 12:34:51 +00:00
|
|
|
# Since some constants are hardcoded in setup.py, the following assertions verify that the hardcoded constants are
|
|
|
|
# as same as the spec definition.
|
|
|
|
+ ('\n\n\n' + ssz_dep_constants_verification if ssz_dep_constants_verification != '' else '')
|
|
|
|
+ '\n'
|
2020-01-25 00:26:10 +00:00
|
|
|
)
|
|
|
|
return spec
|
|
|
|
|
|
|
|
|
2021-05-12 00:40:23 +00:00
|
|
|
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
|
|
|
|
else:
|
2021-05-19 17:18:42 +00:00
|
|
|
functions = combine_dicts(old_protocols[key].functions, value.functions)
|
2021-05-12 00:40:23 +00:00
|
|
|
old_protocols[key] = ProtocolDefinition(functions=functions)
|
|
|
|
return old_protocols
|
|
|
|
|
|
|
|
|
2021-05-19 17:18:42 +00:00
|
|
|
T = TypeVar('T')
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
2021-05-19 17:18:42 +00:00
|
|
|
def combine_dicts(old_dict: Dict[str, T], new_dict: Dict[str, T]) -> Dict[str, T]:
|
|
|
|
return {**old_dict, **new_dict}
|
2021-05-18 10:12:01 +00:00
|
|
|
|
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
ignored_dependencies = [
|
|
|
|
'bit', 'boolean', 'Vector', 'List', 'Container', 'BLSPubkey', 'BLSSignature',
|
2021-10-19 00:10:27 +00:00
|
|
|
'Bytes1', 'Bytes4', 'Bytes8', 'Bytes20', 'Bytes32', 'Bytes48', 'Bytes96', 'Bitlist', 'Bitvector',
|
2020-01-25 00:26:10 +00:00
|
|
|
'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256',
|
2020-07-03 02:48:57 +00:00
|
|
|
'bytes', 'byte', 'ByteList', 'ByteVector',
|
2021-05-06 18:36:08 +00:00
|
|
|
'Dict', 'dict', 'field', 'ceillog2', 'floorlog2', 'Set',
|
2020-01-25 00:26:10 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-07-03 02:48:57 +00:00
|
|
|
def dependency_order_class_objects(objects: Dict[str, str], custom_types: Dict[str, str]) -> None:
|
2020-01-25 00:26:10 +00:00
|
|
|
"""
|
|
|
|
Determines which SSZ Object is dependent on which other and orders them appropriately
|
|
|
|
"""
|
|
|
|
items = list(objects.items())
|
|
|
|
for key, value in items:
|
|
|
|
dependencies = []
|
|
|
|
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
|
|
|
|
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:]:
|
|
|
|
objects[item] = objects.pop(item)
|
|
|
|
|
|
|
|
|
|
|
|
def combine_ssz_objects(old_objects: Dict[str, str], new_objects: Dict[str, str], custom_types) -> Dict[str, str]:
|
|
|
|
"""
|
|
|
|
Takes in old spec and new spec ssz objects, combines them,
|
2020-01-31 11:04:20 +00:00
|
|
|
and returns the newer versions of the objects in dependency order.
|
2020-01-25 00:26:10 +00:00
|
|
|
"""
|
|
|
|
for key, value in new_objects.items():
|
|
|
|
old_objects[key] = value
|
|
|
|
return old_objects
|
|
|
|
|
|
|
|
|
|
|
|
def combine_spec_objects(spec0: SpecObject, spec1: SpecObject) -> SpecObject:
|
|
|
|
"""
|
|
|
|
Takes in two spec variants (as tuples of their objects) and combines them using the appropriate combiner function.
|
|
|
|
"""
|
2021-05-18 10:12:01 +00:00
|
|
|
protocols = combine_protocols(spec0.protocols, spec1.protocols)
|
2021-05-19 17:18:42 +00:00
|
|
|
functions = combine_dicts(spec0.functions, spec1.functions)
|
|
|
|
custom_types = combine_dicts(spec0.custom_types, spec1.custom_types)
|
|
|
|
constant_vars = combine_dicts(spec0.constant_vars, spec1.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)
|
2021-05-18 10:12:01 +00:00
|
|
|
ssz_objects = combine_ssz_objects(spec0.ssz_objects, spec1.ssz_objects, custom_types)
|
2021-05-19 17:18:42 +00:00
|
|
|
dataclasses = combine_dicts(spec0.dataclasses, spec1.dataclasses)
|
2020-12-09 09:35:22 +00:00
|
|
|
return SpecObject(
|
|
|
|
functions=functions,
|
2021-05-12 00:40:23 +00:00
|
|
|
protocols=protocols,
|
2020-12-09 09:35:22 +00:00
|
|
|
custom_types=custom_types,
|
2021-05-18 10:12:01 +00:00
|
|
|
constant_vars=constant_vars,
|
|
|
|
preset_vars=preset_vars,
|
|
|
|
config_vars=config_vars,
|
2020-12-09 09:35:22 +00:00
|
|
|
ssz_dep_constants=ssz_dep_constants,
|
|
|
|
ssz_objects=ssz_objects,
|
|
|
|
dataclasses=dataclasses,
|
|
|
|
)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
2021-05-18 10:12:01 +00:00
|
|
|
def parse_config_vars(conf: Dict[str, str]) -> Dict[str, str]:
|
|
|
|
"""
|
|
|
|
Parses a dict of basic str/int/list types into a dict for insertion into the spec code.
|
|
|
|
"""
|
|
|
|
out: Dict[str, str] = dict()
|
|
|
|
for k, v in conf.items():
|
|
|
|
if isinstance(v, str) and (v.startswith("0x") or k == 'PRESET_BASE'):
|
|
|
|
# Represent byte data with string, to avoid misinterpretation as big-endian int.
|
|
|
|
# Everything is either byte data or an integer, with PRESET_BASE as one exception.
|
|
|
|
out[k] = f"'{v}'"
|
|
|
|
else:
|
|
|
|
out[k] = str(int(v))
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
def load_preset(preset_files: Sequence[Path]) -> Dict[str, str]:
|
|
|
|
"""
|
|
|
|
Loads the a directory of preset files, merges the result into one preset.
|
|
|
|
"""
|
|
|
|
preset = {}
|
|
|
|
for fork_file in preset_files:
|
|
|
|
yaml = YAML(typ='base')
|
|
|
|
fork_preset: dict = yaml.load(fork_file)
|
|
|
|
if fork_preset is None: # for empty YAML files
|
|
|
|
continue
|
|
|
|
if not set(fork_preset.keys()).isdisjoint(preset.keys()):
|
|
|
|
duplicates = set(fork_preset.keys()).intersection(set(preset.keys()))
|
|
|
|
raise Exception(f"duplicate config var(s) in preset files: {', '.join(duplicates)}")
|
|
|
|
preset.update(fork_preset)
|
|
|
|
assert preset != {}
|
|
|
|
return parse_config_vars(preset)
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(config_path: Path) -> Dict[str, str]:
|
|
|
|
"""
|
|
|
|
Loads the given configuration file.
|
|
|
|
"""
|
|
|
|
yaml = YAML(typ='base')
|
|
|
|
config_data = yaml.load(config_path)
|
|
|
|
return parse_config_vars(config_data)
|
|
|
|
|
|
|
|
|
2021-05-18 11:59:26 +00:00
|
|
|
def _build_spec(preset_name: str, fork: str,
|
|
|
|
source_files: Sequence[Path], preset_files: Sequence[Path], config_file: Path) -> str:
|
2021-05-18 10:12:01 +00:00
|
|
|
preset = load_preset(preset_files)
|
|
|
|
config = load_config(config_file)
|
|
|
|
all_specs = [get_spec(spec, preset, config) for spec in source_files]
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2020-01-25 21:10:03 +00:00
|
|
|
spec_object = all_specs[0]
|
|
|
|
for value in all_specs[1:]:
|
2020-01-25 00:26:10 +00:00
|
|
|
spec_object = combine_spec_objects(spec_object, value)
|
|
|
|
|
2020-07-03 02:48:57 +00:00
|
|
|
class_objects = {**spec_object.ssz_objects, **spec_object.dataclasses}
|
|
|
|
dependency_order_class_objects(class_objects, spec_object.custom_types)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
2021-05-18 11:59:26 +00:00
|
|
|
return objects_to_spec(preset_name, spec_object, spec_builders[fork], class_objects)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
2021-05-18 10:12:01 +00:00
|
|
|
class BuildTarget(NamedTuple):
|
|
|
|
name: str
|
|
|
|
preset_paths: List[Path]
|
|
|
|
config_path: Path
|
|
|
|
|
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
class PySpecCommand(Command):
|
|
|
|
"""Convert spec markdown files to a spec python file"""
|
|
|
|
|
|
|
|
description = "Convert spec markdown files to a spec python file"
|
|
|
|
|
2020-01-31 10:52:30 +00:00
|
|
|
spec_fork: str
|
2020-01-25 00:26:10 +00:00
|
|
|
md_doc_paths: str
|
|
|
|
parsed_md_doc_paths: List[str]
|
2021-05-18 10:12:01 +00:00
|
|
|
build_targets: str
|
|
|
|
parsed_build_targets: List[BuildTarget]
|
2020-01-25 00:26:10 +00:00
|
|
|
out_dir: str
|
|
|
|
|
|
|
|
# The format is (long option, short option, description).
|
|
|
|
user_options = [
|
2020-01-31 10:52:30 +00:00
|
|
|
('spec-fork=', None, "Spec fork to tag build with. Used to select md-docs defaults."),
|
2020-01-25 00:26:10 +00:00
|
|
|
('md-doc-paths=', None, "List of paths of markdown files to build spec with"),
|
2021-05-18 10:12:01 +00:00
|
|
|
('build-targets=', None, "Names, directory paths of compile-time presets, and default config paths."),
|
2020-01-25 00:26:10 +00:00
|
|
|
('out-dir=', None, "Output directory to write spec package to")
|
|
|
|
]
|
|
|
|
|
|
|
|
def initialize_options(self):
|
|
|
|
"""Set default values for options."""
|
|
|
|
# Each user option must be listed here with their default value.
|
2021-03-12 12:26:26 +00:00
|
|
|
self.spec_fork = PHASE0
|
2020-01-25 00:26:10 +00:00
|
|
|
self.md_doc_paths = ''
|
|
|
|
self.out_dir = 'pyspec_output'
|
2021-05-18 10:12:01 +00:00
|
|
|
self.build_targets = """
|
2021-05-19 15:15:34 +00:00
|
|
|
minimal:presets/minimal:configs/minimal.yaml
|
2021-05-24 18:51:08 +00:00
|
|
|
mainnet:presets/mainnet:configs/mainnet.yaml
|
2021-05-18 10:12:01 +00:00
|
|
|
"""
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
def finalize_options(self):
|
|
|
|
"""Post-process options."""
|
|
|
|
if len(self.md_doc_paths) == 0:
|
2020-01-25 21:10:03 +00:00
|
|
|
print("no paths were specified, using default markdown file paths for pyspec"
|
2020-01-31 10:52:30 +00:00
|
|
|
" build (spec fork: %s)" % self.spec_fork)
|
2021-07-22 14:36:41 +00:00
|
|
|
if self.spec_fork in (PHASE0, ALTAIR, MERGE):
|
2020-01-25 00:26:10 +00:00
|
|
|
self.md_doc_paths = """
|
|
|
|
specs/phase0/beacon-chain.md
|
|
|
|
specs/phase0/fork-choice.md
|
|
|
|
specs/phase0/validator.md
|
2020-09-17 18:01:25 +00:00
|
|
|
specs/phase0/weak-subjectivity.md
|
2020-01-25 00:26:10 +00:00
|
|
|
"""
|
2021-07-22 14:36:41 +00:00
|
|
|
if self.spec_fork in (ALTAIR, MERGE):
|
|
|
|
self.md_doc_paths += """
|
2021-03-11 13:22:38 +00:00
|
|
|
specs/altair/beacon-chain.md
|
2021-05-27 19:21:04 +00:00
|
|
|
specs/altair/bls.md
|
2021-03-11 13:22:38 +00:00
|
|
|
specs/altair/fork.md
|
2021-03-19 15:38:59 +00:00
|
|
|
specs/altair/validator.md
|
2021-04-20 21:55:46 +00:00
|
|
|
specs/altair/p2p-interface.md
|
2021-03-11 16:42:25 +00:00
|
|
|
specs/altair/sync-protocol.md
|
2020-11-17 02:41:26 +00:00
|
|
|
"""
|
2021-07-22 14:36:41 +00:00
|
|
|
if self.spec_fork == MERGE:
|
|
|
|
self.md_doc_paths += """
|
2021-04-05 14:59:09 +00:00
|
|
|
specs/merge/beacon-chain.md
|
2021-05-31 13:30:54 +00:00
|
|
|
specs/merge/fork.md
|
2021-04-05 14:59:09 +00:00
|
|
|
specs/merge/fork-choice.md
|
|
|
|
specs/merge/validator.md
|
|
|
|
"""
|
2021-07-22 14:36:41 +00:00
|
|
|
if len(self.md_doc_paths) == 0:
|
2020-01-31 10:52:30 +00:00
|
|
|
raise Exception('no markdown files specified, and spec fork "%s" is unknown', self.spec_fork)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
self.parsed_md_doc_paths = self.md_doc_paths.split()
|
|
|
|
|
|
|
|
for filename in self.parsed_md_doc_paths:
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
raise Exception('Pyspec markdown input file "%s" does not exist.' % filename)
|
|
|
|
|
2021-05-18 10:12:01 +00:00
|
|
|
self.parsed_build_targets = []
|
|
|
|
for target in self.build_targets.split():
|
|
|
|
target = target.strip()
|
|
|
|
data = target.split(':')
|
|
|
|
if len(data) != 3:
|
|
|
|
raise Exception('invalid target, expected "name:preset_dir:config_file" format, but got: %s' % target)
|
|
|
|
name, preset_dir_path, config_path = data
|
|
|
|
if any((c not in string.digits + string.ascii_letters) for c in name):
|
|
|
|
raise Exception('invalid target name: "%s"' % name)
|
|
|
|
if not os.path.exists(preset_dir_path):
|
|
|
|
raise Exception('Preset dir "%s" does not exist' % preset_dir_path)
|
|
|
|
_, _, preset_file_names = next(os.walk(preset_dir_path))
|
|
|
|
preset_paths = [(Path(preset_dir_path) / name) for name in preset_file_names]
|
|
|
|
|
|
|
|
if not os.path.exists(config_path):
|
|
|
|
raise Exception('Config file "%s" does not exist' % config_path)
|
|
|
|
self.parsed_build_targets.append(BuildTarget(name, preset_paths, Path(config_path)))
|
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
def run(self):
|
2021-05-18 10:12:01 +00:00
|
|
|
if not self.dry_run:
|
2020-01-25 00:26:10 +00:00
|
|
|
dir_util.mkpath(self.out_dir)
|
2021-05-18 10:12:01 +00:00
|
|
|
|
|
|
|
for (name, preset_paths, config_path) in self.parsed_build_targets:
|
2021-05-18 11:59:26 +00:00
|
|
|
spec_str = spec_builders[self.spec_fork].build_spec(
|
|
|
|
name, self.parsed_md_doc_paths, preset_paths, config_path)
|
2021-05-18 10:12:01 +00:00
|
|
|
if self.dry_run:
|
|
|
|
self.announce('dry run successfully prepared contents for spec.'
|
|
|
|
f' out dir: "{self.out_dir}", spec fork: "{self.spec_fork}", build target: "{name}"')
|
|
|
|
self.debug_print(spec_str)
|
|
|
|
else:
|
|
|
|
with open(os.path.join(self.out_dir, name+'.py'), 'w') as out:
|
|
|
|
out.write(spec_str)
|
|
|
|
|
|
|
|
if not self.dry_run:
|
2020-01-25 00:26:10 +00:00
|
|
|
with open(os.path.join(self.out_dir, '__init__.py'), 'w') as out:
|
2021-05-25 13:13:12 +00:00
|
|
|
# `mainnet` is the default spec.
|
2021-05-25 13:40:10 +00:00
|
|
|
out.write("from . import mainnet as spec # noqa:F401\n")
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BuildPyCommand(build_py):
|
|
|
|
"""Customize the build command to run the spec-builder on setup.py build"""
|
|
|
|
|
|
|
|
def initialize_options(self):
|
|
|
|
super(BuildPyCommand, self).initialize_options()
|
|
|
|
|
2020-01-31 10:52:30 +00:00
|
|
|
def run_pyspec_cmd(self, spec_fork: str, **opts):
|
2020-01-25 00:26:10 +00:00
|
|
|
cmd_obj: PySpecCommand = self.distribution.reinitialize_command("pyspec")
|
2020-01-31 10:52:30 +00:00
|
|
|
cmd_obj.spec_fork = spec_fork
|
|
|
|
cmd_obj.out_dir = os.path.join(self.build_lib, 'eth2spec', spec_fork)
|
2020-01-25 00:26:10 +00:00
|
|
|
for k, v in opts.items():
|
|
|
|
setattr(cmd_obj, k, v)
|
|
|
|
self.run_command('pyspec')
|
|
|
|
|
|
|
|
def run(self):
|
2021-04-16 03:29:10 +00:00
|
|
|
for spec_fork in spec_builders:
|
2020-01-31 10:52:30 +00:00
|
|
|
self.run_pyspec_cmd(spec_fork=spec_fork)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
super(BuildPyCommand, self).run()
|
|
|
|
|
|
|
|
|
|
|
|
class PyspecDevCommand(Command):
|
|
|
|
"""Build the markdown files in-place to their source location for testing."""
|
|
|
|
description = "Build the markdown files in-place to their source location for testing."
|
|
|
|
user_options = []
|
|
|
|
|
|
|
|
def initialize_options(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def finalize_options(self):
|
|
|
|
pass
|
|
|
|
|
2020-01-31 10:52:30 +00:00
|
|
|
def run_pyspec_cmd(self, spec_fork: str, **opts):
|
2020-01-25 00:26:10 +00:00
|
|
|
cmd_obj: PySpecCommand = self.distribution.reinitialize_command("pyspec")
|
2020-01-31 10:52:30 +00:00
|
|
|
cmd_obj.spec_fork = spec_fork
|
2020-01-25 00:26:10 +00:00
|
|
|
eth2spec_dir = convert_path(self.distribution.package_dir['eth2spec'])
|
2020-01-31 10:52:30 +00:00
|
|
|
cmd_obj.out_dir = os.path.join(eth2spec_dir, spec_fork)
|
2020-01-25 00:26:10 +00:00
|
|
|
for k, v in opts.items():
|
|
|
|
setattr(cmd_obj, k, v)
|
|
|
|
self.run_command('pyspec')
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
print("running build_py command")
|
2021-04-16 03:29:10 +00:00
|
|
|
for spec_fork in spec_builders:
|
2020-01-31 10:52:30 +00:00
|
|
|
self.run_pyspec_cmd(spec_fork=spec_fork)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
commands = {
|
|
|
|
'pyspec': PySpecCommand,
|
|
|
|
'build_py': BuildPyCommand,
|
|
|
|
'pyspecdev': PyspecDevCommand,
|
|
|
|
}
|
|
|
|
|
|
|
|
with open("README.md", "rt", encoding="utf8") as f:
|
|
|
|
readme = f.read()
|
|
|
|
|
2020-02-05 17:38:21 +00:00
|
|
|
# How to use "VERSION.txt" file:
|
|
|
|
# - dev branch contains "X.Y.Z.dev", where "X.Y.Z" is the target version to release dev into.
|
|
|
|
# -> Changed as part of 'master' backport to 'dev'
|
|
|
|
# - master branch contains "X.Y.Z", where "X.Y.Z" is the current version.
|
|
|
|
# -> Changed as part of 'dev' release (or other branch) into 'master'
|
|
|
|
# -> In case of a commit on master without git tag, target the next version
|
|
|
|
# with ".postN" (release candidate, numbered) suffixed.
|
|
|
|
# See https://www.python.org/dev/peps/pep-0440/#public-version-identifiers
|
|
|
|
with open(os.path.join('tests', 'core', 'pyspec', 'eth2spec', 'VERSION.txt')) as f:
|
|
|
|
spec_version = f.read().strip()
|
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
setup(
|
|
|
|
name='eth2spec',
|
2020-02-05 17:38:21 +00:00
|
|
|
version=spec_version,
|
2020-01-25 00:26:10 +00:00
|
|
|
description="Eth2 spec, provided as Python package for tooling and testing",
|
|
|
|
long_description=readme,
|
|
|
|
long_description_content_type="text/markdown",
|
|
|
|
author="ethereum",
|
|
|
|
url="https://github.com/ethereum/eth2.0-specs",
|
|
|
|
include_package_data=False,
|
|
|
|
package_data={'configs': ['*.yaml'],
|
2021-05-19 15:15:34 +00:00
|
|
|
'presets': ['*.yaml'],
|
2020-02-05 17:38:21 +00:00
|
|
|
'specs': ['**/*.md'],
|
|
|
|
'eth2spec': ['VERSION.txt']},
|
2020-01-25 00:26:10 +00:00
|
|
|
package_dir={
|
|
|
|
"eth2spec": "tests/core/pyspec/eth2spec",
|
|
|
|
"configs": "configs",
|
2021-05-19 15:15:34 +00:00
|
|
|
"presets": "presets",
|
2021-02-18 09:51:01 +00:00
|
|
|
"specs": "specs",
|
2020-01-25 00:26:10 +00:00
|
|
|
},
|
|
|
|
packages=find_packages(where='tests/core/pyspec') + ['configs', 'specs'],
|
|
|
|
py_modules=["eth2spec"],
|
|
|
|
cmdclass=commands,
|
|
|
|
python_requires=">=3.8, <4",
|
|
|
|
extras_require={
|
2020-01-25 21:10:03 +00:00
|
|
|
"test": ["pytest>=4.4", "pytest-cov", "pytest-xdist"],
|
2021-06-03 12:49:29 +00:00
|
|
|
"lint": ["flake8==3.7.7", "mypy==0.812"],
|
2021-03-11 00:18:11 +00:00
|
|
|
"generator": ["python-snappy==0.5.4"],
|
2020-01-25 00:26:10 +00:00
|
|
|
},
|
|
|
|
install_requires=[
|
|
|
|
"eth-utils>=1.3.0,<2",
|
|
|
|
"eth-typing>=2.1.0,<3.0.0",
|
|
|
|
"pycryptodome==3.9.4",
|
2021-03-10 13:55:50 +00:00
|
|
|
"py_ecc==5.2.0",
|
2021-02-20 15:29:34 +00:00
|
|
|
"milagro_bls_binding==1.6.3",
|
2020-01-25 00:26:10 +00:00
|
|
|
"dataclasses==0.6",
|
2021-10-15 23:15:31 +00:00
|
|
|
"remerkleable==0.1.24",
|
2021-05-18 10:12:01 +00:00
|
|
|
RUAMEL_YAML_VERSION,
|
2021-03-10 23:43:02 +00:00
|
|
|
"lru-dict==1.1.6",
|
2021-05-18 10:12:01 +00:00
|
|
|
MARKO_VERSION,
|
2020-01-25 00:26:10 +00:00
|
|
|
]
|
|
|
|
)
|