2020-07-03 02:48:57 +00:00
|
|
|
from enum import Enum, auto
|
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
|
|
|
|
import os
|
|
|
|
import re
|
2021-04-19 13:41:45 +00:00
|
|
|
import string
|
2020-01-25 00:26:10 +00:00
|
|
|
from typing import Dict, NamedTuple, List
|
|
|
|
|
|
|
|
FUNCTION_REGEX = r'^def [\w_]*'
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
class SpecObject(NamedTuple):
|
|
|
|
functions: Dict[str, str]
|
|
|
|
custom_types: Dict[str, str]
|
|
|
|
constants: Dict[str, str]
|
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]
|
|
|
|
|
|
|
|
|
|
|
|
class CodeBlockType(Enum):
|
|
|
|
SSZ = auto()
|
|
|
|
DATACLASS = auto()
|
|
|
|
FUNCTION = auto()
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_spec(file_name: str) -> SpecObject:
|
|
|
|
"""
|
|
|
|
Takes in the file name of a spec.md file, opens it and returns a parsed spec object.
|
|
|
|
|
|
|
|
Note: This function makes heavy use of the inherent ordering of dicts,
|
|
|
|
if this is not supported by your python version, it will not work.
|
|
|
|
"""
|
|
|
|
pulling_from = None # line number of start of latest object
|
|
|
|
current_name = None # most recent section title
|
|
|
|
functions: Dict[str, str] = {}
|
|
|
|
constants: Dict[str, str] = {}
|
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
|
|
|
function_matcher = re.compile(FUNCTION_REGEX)
|
2020-07-03 02:48:57 +00:00
|
|
|
block_type = CodeBlockType.FUNCTION
|
2020-01-25 00:26:10 +00:00
|
|
|
custom_types: Dict[str, str] = {}
|
|
|
|
for linenum, line in enumerate(open(file_name).readlines()):
|
|
|
|
line = line.rstrip()
|
|
|
|
if pulling_from is None and len(line) > 0 and line[0] == '#' and line[-1] == '`':
|
|
|
|
current_name = line[line[:-1].rfind('`') + 1: -1]
|
|
|
|
if line[:9] == '```python':
|
|
|
|
assert pulling_from is None
|
|
|
|
pulling_from = linenum + 1
|
|
|
|
elif line[:3] == '```':
|
|
|
|
pulling_from = None
|
|
|
|
else:
|
|
|
|
# Handle function definitions & ssz_objects
|
|
|
|
if pulling_from is not None:
|
2020-11-17 02:41:26 +00:00
|
|
|
if len(line) > 18 and line[:6] == 'class ' and (line[-12:] == '(Container):' or '(phase' in line):
|
|
|
|
end = -12 if line[-12:] == '(Container):' else line.find('(')
|
|
|
|
name = line[6:end]
|
2020-01-25 00:26:10 +00:00
|
|
|
# Check consistency with markdown header
|
|
|
|
assert name == current_name
|
2020-07-03 02:48:57 +00:00
|
|
|
block_type = CodeBlockType.SSZ
|
|
|
|
elif line[:10] == '@dataclass':
|
|
|
|
block_type = CodeBlockType.DATACLASS
|
2020-01-25 00:26:10 +00:00
|
|
|
elif function_matcher.match(line) is not None:
|
|
|
|
current_name = function_matcher.match(line).group(0)
|
2020-07-03 02:48:57 +00:00
|
|
|
block_type = CodeBlockType.FUNCTION
|
|
|
|
|
|
|
|
if block_type == CodeBlockType.SSZ:
|
2020-01-25 00:26:10 +00:00
|
|
|
ssz_objects[current_name] = ssz_objects.get(current_name, '') + line + '\n'
|
2020-07-03 02:48:57 +00:00
|
|
|
elif block_type == CodeBlockType.DATACLASS:
|
|
|
|
dataclasses[current_name] = dataclasses.get(current_name, '') + line + '\n'
|
|
|
|
elif block_type == CodeBlockType.FUNCTION:
|
2020-01-25 00:26:10 +00:00
|
|
|
functions[current_name] = functions.get(current_name, '') + line + '\n'
|
2020-07-03 02:48:57 +00:00
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
# Handle constant and custom types table entries
|
|
|
|
elif pulling_from is None and len(line) > 0 and line[0] == '|':
|
|
|
|
row = line[1:].split('|')
|
|
|
|
if len(row) >= 2:
|
|
|
|
for i in range(2):
|
|
|
|
row[i] = row[i].strip().strip('`')
|
|
|
|
if '`' in row[i]:
|
|
|
|
row[i] = row[i][:row[i].find('`')]
|
|
|
|
is_constant_def = True
|
2021-04-19 13:41:45 +00:00
|
|
|
if row[0][0] not in string.ascii_uppercase + '_':
|
2020-01-25 00:26:10 +00:00
|
|
|
is_constant_def = False
|
|
|
|
for c in row[0]:
|
2021-04-19 13:41:45 +00:00
|
|
|
if c not in string.ascii_uppercase + '_' + string.digits:
|
2020-01-25 00:26:10 +00:00
|
|
|
is_constant_def = False
|
|
|
|
if is_constant_def:
|
2020-12-09 09:35:22 +00:00
|
|
|
if row[1].startswith('get_generalized_index'):
|
|
|
|
ssz_dep_constants[row[0]] = row[1]
|
|
|
|
else:
|
|
|
|
constants[row[0]] = row[1].replace('**TBD**', '2**32')
|
2021-04-05 14:59:09 +00:00
|
|
|
elif row[1].startswith('uint') or row[1].startswith('Bytes') or row[1].startswith('ByteList'):
|
2020-01-25 00:26:10 +00:00
|
|
|
custom_types[row[0]] = row[1]
|
2020-12-09 09:35:22 +00:00
|
|
|
return SpecObject(
|
|
|
|
functions=functions,
|
|
|
|
custom_types=custom_types,
|
|
|
|
constants=constants,
|
|
|
|
ssz_dep_constants=ssz_dep_constants,
|
|
|
|
ssz_objects=ssz_objects,
|
|
|
|
dataclasses=dataclasses,
|
|
|
|
)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
CONFIG_LOADER = '''
|
|
|
|
apply_constants_config(globals())
|
|
|
|
'''
|
|
|
|
|
|
|
|
PHASE0_IMPORTS = '''from eth2spec.config.config_util import apply_constants_config
|
|
|
|
from typing import (
|
|
|
|
Any, Callable, Dict, Set, Sequence, Tuple, Optional, TypeVar
|
|
|
|
)
|
|
|
|
|
|
|
|
from dataclasses import (
|
|
|
|
dataclass,
|
|
|
|
field,
|
|
|
|
)
|
|
|
|
|
2020-03-20 19:38:36 +00:00
|
|
|
from lru import LRU
|
|
|
|
|
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,
|
2020-03-05 16:21:32 +00:00
|
|
|
Bytes1, Bytes4, Bytes32, Bytes48, Bytes96, Bitlist, Bitvector,
|
2020-01-25 00:26:10 +00:00
|
|
|
)
|
|
|
|
from eth2spec.utils import bls
|
|
|
|
|
|
|
|
from eth2spec.utils.hash_function import hash
|
|
|
|
|
|
|
|
SSZObject = TypeVar('SSZObject', bound=View)
|
2020-08-12 12:48:52 +00:00
|
|
|
|
|
|
|
CONFIG_NAME = 'mainnet'
|
2020-01-25 00:26:10 +00:00
|
|
|
'''
|
2020-08-12 12:48:52 +00:00
|
|
|
|
2021-03-11 16:42:25 +00:00
|
|
|
ALTAIR_IMPORTS = '''from eth2spec.phase0 import spec as phase0
|
2020-11-17 02:41:26 +00:00
|
|
|
from eth2spec.config.config_util import apply_constants_config
|
|
|
|
from typing import (
|
2020-12-09 09:35:22 +00:00
|
|
|
Any, Dict, Set, Sequence, NewType, Tuple, TypeVar, Callable, Optional, Union
|
2020-11-17 02:41:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
from dataclasses import (
|
|
|
|
dataclass,
|
|
|
|
field,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lru import LRU
|
|
|
|
|
|
|
|
from eth2spec.utils.ssz.ssz_impl import hash_tree_root, copy, uint_to_bytes
|
|
|
|
from eth2spec.utils.ssz.ssz_typing import (
|
|
|
|
View, boolean, Container, List, Vector, uint8, uint32, uint64,
|
2020-12-15 05:18:20 +00:00
|
|
|
Bytes1, Bytes4, Bytes32, Bytes48, Bytes96, Bitlist, Bitvector,
|
2020-12-09 09:35:22 +00:00
|
|
|
Path,
|
2020-11-17 02:41:26 +00:00
|
|
|
)
|
|
|
|
from eth2spec.utils import bls
|
|
|
|
|
|
|
|
from eth2spec.utils.hash_function import hash
|
|
|
|
|
2021-03-11 13:22:38 +00:00
|
|
|
# Whenever altair is loaded, make sure we have the latest phase0
|
2020-11-17 02:41:26 +00:00
|
|
|
from importlib import reload
|
|
|
|
reload(phase0)
|
|
|
|
|
|
|
|
|
|
|
|
SSZVariableName = str
|
|
|
|
GeneralizedIndex = NewType('GeneralizedIndex', int)
|
|
|
|
SSZObject = TypeVar('SSZObject', bound=View)
|
|
|
|
|
|
|
|
CONFIG_NAME = 'mainnet'
|
|
|
|
'''
|
|
|
|
|
2021-04-05 14:59:09 +00:00
|
|
|
MERGE_IMPORTS = '''from eth2spec.phase0 import spec as phase0
|
|
|
|
from eth2spec.config.config_util import apply_constants_config
|
|
|
|
from typing import (
|
|
|
|
Any, Callable, Dict, Set, Sequence, Tuple, Optional, TypeVar
|
|
|
|
)
|
|
|
|
|
|
|
|
from dataclasses import (
|
|
|
|
dataclass,
|
|
|
|
field,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lru import LRU
|
|
|
|
|
|
|
|
from eth2spec.utils.ssz.ssz_impl import hash_tree_root, copy, uint_to_bytes
|
|
|
|
from eth2spec.utils.ssz.ssz_typing import (
|
|
|
|
View, boolean, Container, List, Vector, uint8, uint32, uint64, uint256,
|
|
|
|
Bytes1, Bytes4, Bytes20, Bytes32, Bytes48, Bytes96, Bitlist,
|
|
|
|
ByteList, ByteVector
|
|
|
|
)
|
|
|
|
from eth2spec.utils import bls
|
|
|
|
|
|
|
|
from eth2spec.utils.hash_function import hash
|
|
|
|
|
|
|
|
SSZObject = TypeVar('SSZObject', bound=View)
|
|
|
|
|
|
|
|
CONFIG_NAME = 'mainnet'
|
|
|
|
'''
|
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
SUNDRY_CONSTANTS_FUNCTIONS = '''
|
2020-09-01 08:07:44 +00:00
|
|
|
def ceillog2(x: int) -> uint64:
|
|
|
|
if x < 1:
|
|
|
|
raise ValueError(f"ceillog2 accepts only positive values, x={x}")
|
2020-08-31 16:41:57 +00:00
|
|
|
return uint64((x - 1).bit_length())
|
2020-12-16 07:00:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def floorlog2(x: int) -> uint64:
|
|
|
|
if x < 1:
|
|
|
|
raise ValueError(f"floorlog2 accepts only positive values, x={x}")
|
|
|
|
return uint64(x.bit_length() - 1)
|
2020-01-25 00:26:10 +00:00
|
|
|
'''
|
2020-05-29 15:50:18 +00:00
|
|
|
PHASE0_SUNDRY_FUNCTIONS = '''
|
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-03-11 16:42:25 +00:00
|
|
|
ALTAIR_SUNDRY_FUNCTIONS = '''
|
2020-12-09 09:35:22 +00:00
|
|
|
|
|
|
|
def get_generalized_index(ssz_class: Any, *path: Sequence[Union[int, SSZVariableName]]) -> GeneralizedIndex:
|
|
|
|
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-05 14:59:09 +00:00
|
|
|
MERGE_SUNDRY_FUNCTIONS = """
|
2021-04-09 12:21:51 +00:00
|
|
|
ExecutionState = Any
|
2021-04-05 14:59:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_pow_block(hash: Bytes32) -> PowBlock:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
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-04-14 13:02:09 +00:00
|
|
|
def verify_execution_state_transition(execution_payload: ExecutionPayload) -> bool:
|
|
|
|
return True
|
2021-04-05 14:59:09 +00:00
|
|
|
|
|
|
|
|
2021-04-09 19:04:27 +00:00
|
|
|
def produce_execution_payload(parent_hash: Hash32, timestamp: uint64) -> ExecutionPayload:
|
2021-04-05 14:59:09 +00:00
|
|
|
pass"""
|
|
|
|
|
|
|
|
|
2020-12-09 09:35:22 +00:00
|
|
|
# The constants that depend on SSZ objects
|
|
|
|
# Will verify the value at the end of the spec
|
2021-03-11 16:42:25 +00:00
|
|
|
ALTAIR_HARDCODED_SSZ_DEP_CONSTANTS = {
|
2020-12-09 09:35:22 +00:00
|
|
|
'FINALIZED_ROOT_INDEX': 'GeneralizedIndex(105)',
|
2021-03-15 15:17:59 +00:00
|
|
|
'NEXT_SYNC_COMMITTEE_INDEX': 'GeneralizedIndex(55)',
|
2020-12-09 09:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-14 19:37:25 +00:00
|
|
|
ALTAIR_INVARIANT_CHECKS = '''
|
2021-04-05 16:08:30 +00:00
|
|
|
assert (
|
|
|
|
TIMELY_HEAD_WEIGHT + TIMELY_SOURCE_WEIGHT + TIMELY_TARGET_WEIGHT + SYNC_REWARD_WEIGHT + PROPOSER_WEIGHT
|
|
|
|
) == WEIGHT_DENOMINATOR'''
|
|
|
|
|
|
|
|
|
2021-04-05 14:59:09 +00:00
|
|
|
MERGE_HARDCODED_CUSTOM_TYPE_DEP_CONSTANTS = {
|
|
|
|
'MAX_BYTES_PER_OPAQUE_TRANSACTION': 'uint64(2**20)',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-12 12:26:26 +00:00
|
|
|
def is_phase0(fork):
|
|
|
|
return fork == PHASE0
|
|
|
|
|
|
|
|
|
|
|
|
def is_altair(fork):
|
|
|
|
return fork == ALTAIR
|
|
|
|
|
|
|
|
|
2021-04-05 14:59:09 +00:00
|
|
|
def is_merge(fork):
|
|
|
|
return fork == MERGE
|
|
|
|
|
|
|
|
|
2020-07-03 02:48:57 +00:00
|
|
|
def objects_to_spec(spec_object: SpecObject, imports: str, fork: str, 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-04-05 14:59:09 +00:00
|
|
|
if not value.startswith('ByteList')
|
|
|
|
]
|
|
|
|
)
|
|
|
|
+ ('\n\n' if len([key for key, value in spec_object.custom_types.items() if value.startswith('ByteList')]) > 0 else '')
|
|
|
|
+ '\n\n'.join(
|
|
|
|
[
|
|
|
|
f"{key} = {value}\n"
|
|
|
|
for key, value in spec_object.custom_types.items()
|
|
|
|
if value.startswith('ByteList')
|
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]
|
|
|
|
functions_spec = '\n\n'.join(spec_object.functions.values())
|
|
|
|
for k in list(spec_object.constants.keys()):
|
|
|
|
if k == "BLS12_381_Q":
|
|
|
|
spec_object.constants[k] += " # noqa: E501"
|
|
|
|
constants_spec = '\n'.join(map(lambda x: '%s = %s' % (x, spec_object.constants[x]), spec_object.constants))
|
2020-07-03 02:48:57 +00:00
|
|
|
ordered_class_objects_spec = '\n\n'.join(ordered_class_objects.values())
|
2021-03-12 12:26:26 +00:00
|
|
|
|
|
|
|
if is_altair(fork):
|
|
|
|
altair_ssz_dep_constants = '\n'.join(map(lambda x: '%s = %s' % (x, ALTAIR_HARDCODED_SSZ_DEP_CONSTANTS[x]), ALTAIR_HARDCODED_SSZ_DEP_CONSTANTS))
|
|
|
|
|
2021-04-05 14:59:09 +00:00
|
|
|
if is_merge(fork):
|
|
|
|
merge_custom_type_dep_constants = '\n'.join(map(lambda x: '%s = %s' % (x, MERGE_HARDCODED_CUSTOM_TYPE_DEP_CONSTANTS[x]), MERGE_HARDCODED_CUSTOM_TYPE_DEP_CONSTANTS))
|
|
|
|
|
|
|
|
|
2020-01-25 00:26:10 +00:00
|
|
|
spec = (
|
|
|
|
imports
|
2020-01-31 10:52:30 +00:00
|
|
|
+ '\n\n' + f"fork = \'{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`
|
|
|
|
+ ('\n\n' + merge_custom_type_dep_constants + '\n' if is_merge(fork) else '')
|
2020-01-25 00:26:10 +00:00
|
|
|
+ '\n\n' + new_type_definitions
|
|
|
|
+ '\n' + 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`
|
|
|
|
+ ('\n\n' + altair_ssz_dep_constants if is_altair(fork) else '')
|
|
|
|
+ '\n\n' + constants_spec
|
2020-01-25 00:26:10 +00:00
|
|
|
+ '\n\n' + CONFIG_LOADER
|
2020-07-03 02:48:57 +00:00
|
|
|
+ '\n\n' + ordered_class_objects_spec
|
2020-01-25 00:26:10 +00:00
|
|
|
+ '\n\n' + functions_spec
|
2021-03-12 12:26:26 +00:00
|
|
|
# Functions to make pyspec work
|
2020-05-29 15:50:18 +00:00
|
|
|
+ '\n' + PHASE0_SUNDRY_FUNCTIONS
|
2021-03-12 12:26:26 +00:00
|
|
|
+ ('\n' + ALTAIR_SUNDRY_FUNCTIONS if is_altair(fork) else '')
|
2021-04-05 14:59:09 +00:00
|
|
|
+ ('\n' + MERGE_SUNDRY_FUNCTIONS if is_merge(fork) else '')
|
2020-01-25 00:26:10 +00:00
|
|
|
)
|
2020-12-09 09:35:22 +00:00
|
|
|
|
2021-03-12 12:29:28 +00:00
|
|
|
# Since some constants are hardcoded in setup.py, the following assertions verify that the hardcoded constants are
|
2021-03-12 12:26:26 +00:00
|
|
|
# as same as the spec definition.
|
|
|
|
if is_altair(fork):
|
2021-03-11 16:42:25 +00:00
|
|
|
altair_ssz_dep_constants_verification = '\n'.join(map(lambda x: 'assert %s == %s' % (x, spec_object.ssz_dep_constants[x]), ALTAIR_HARDCODED_SSZ_DEP_CONSTANTS))
|
2021-03-12 12:26:26 +00:00
|
|
|
spec += '\n\n\n' + altair_ssz_dep_constants_verification
|
2021-04-14 19:37:25 +00:00
|
|
|
spec += '\n' + ALTAIR_INVARIANT_CHECKS
|
2020-12-09 09:35:22 +00:00
|
|
|
|
2020-05-29 15:50:18 +00:00
|
|
|
spec += '\n'
|
2020-01-25 00:26:10 +00:00
|
|
|
return spec
|
|
|
|
|
|
|
|
|
|
|
|
def combine_functions(old_functions: Dict[str, str], new_functions: Dict[str, str]) -> Dict[str, str]:
|
|
|
|
for key, value in new_functions.items():
|
|
|
|
old_functions[key] = value
|
|
|
|
return old_functions
|
|
|
|
|
|
|
|
|
|
|
|
def combine_constants(old_constants: Dict[str, str], new_constants: Dict[str, str]) -> Dict[str, str]:
|
|
|
|
for key, value in new_constants.items():
|
|
|
|
old_constants[key] = value
|
|
|
|
return old_constants
|
|
|
|
|
|
|
|
|
|
|
|
ignored_dependencies = [
|
|
|
|
'bit', 'boolean', 'Vector', 'List', 'Container', 'BLSPubkey', 'BLSSignature',
|
2021-04-05 14:59:09 +00:00
|
|
|
'Bytes1', 'Bytes4', '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',
|
2020-12-16 07:00:06 +00:00
|
|
|
'Dict', 'dict', 'field', 'ceillog2', 'floorlog2',
|
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.
|
|
|
|
"""
|
2020-12-09 09:35:22 +00:00
|
|
|
functions0, custom_types0, constants0, ssz_dep_constants0, ssz_objects0, dataclasses0 = spec0
|
|
|
|
functions1, custom_types1, constants1, ssz_dep_constants1, ssz_objects1, dataclasses1 = spec1
|
2020-01-25 00:26:10 +00:00
|
|
|
functions = combine_functions(functions0, functions1)
|
|
|
|
custom_types = combine_constants(custom_types0, custom_types1)
|
|
|
|
constants = combine_constants(constants0, constants1)
|
2020-12-09 09:35:22 +00:00
|
|
|
ssz_dep_constants = combine_constants(ssz_dep_constants0, ssz_dep_constants1)
|
2020-01-25 00:26:10 +00:00
|
|
|
ssz_objects = combine_ssz_objects(ssz_objects0, ssz_objects1, custom_types)
|
2020-07-03 02:48:57 +00:00
|
|
|
dataclasses = combine_functions(dataclasses0, dataclasses1)
|
2020-12-09 09:35:22 +00:00
|
|
|
return SpecObject(
|
|
|
|
functions=functions,
|
|
|
|
custom_types=custom_types,
|
|
|
|
constants=constants,
|
|
|
|
ssz_dep_constants=ssz_dep_constants,
|
|
|
|
ssz_objects=ssz_objects,
|
|
|
|
dataclasses=dataclasses,
|
|
|
|
)
|
2020-01-25 00:26:10 +00:00
|
|
|
|
|
|
|
|
2020-01-31 10:52:30 +00:00
|
|
|
fork_imports = {
|
2020-01-25 00:26:10 +00:00
|
|
|
'phase0': PHASE0_IMPORTS,
|
2021-03-11 16:42:25 +00:00
|
|
|
'altair': ALTAIR_IMPORTS,
|
2021-04-05 14:59:09 +00:00
|
|
|
'merge': MERGE_IMPORTS,
|
2020-01-25 00:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-31 10:52:30 +00:00
|
|
|
def build_spec(fork: str, source_files: List[str]) -> str:
|
2020-01-25 21:10:03 +00:00
|
|
|
all_specs = [get_spec(spec) 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
|
|
|
|
2020-07-03 02:48:57 +00:00
|
|
|
return objects_to_spec(spec_object, fork_imports[fork], fork, class_objects)
|
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]
|
|
|
|
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"),
|
|
|
|
('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'
|
|
|
|
|
|
|
|
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-03-12 12:26:26 +00:00
|
|
|
if is_phase0(self.spec_fork):
|
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-03-12 12:26:26 +00:00
|
|
|
elif is_altair(self.spec_fork):
|
2020-11-17 02:41:26 +00:00
|
|
|
self.md_doc_paths = """
|
|
|
|
specs/phase0/beacon-chain.md
|
|
|
|
specs/phase0/fork-choice.md
|
|
|
|
specs/phase0/validator.md
|
|
|
|
specs/phase0/weak-subjectivity.md
|
2021-03-11 13:22:38 +00:00
|
|
|
specs/altair/beacon-chain.md
|
|
|
|
specs/altair/fork.md
|
2021-03-19 15:38:59 +00:00
|
|
|
specs/altair/validator.md
|
2021-03-11 16:42:25 +00:00
|
|
|
specs/altair/sync-protocol.md
|
2020-11-17 02:41:26 +00:00
|
|
|
"""
|
2021-04-05 14:59:09 +00:00
|
|
|
elif is_merge(self.spec_fork):
|
|
|
|
self.md_doc_paths = """
|
|
|
|
specs/phase0/beacon-chain.md
|
|
|
|
specs/phase0/fork-choice.md
|
|
|
|
specs/phase0/validator.md
|
|
|
|
specs/phase0/weak-subjectivity.md
|
|
|
|
specs/merge/beacon-chain.md
|
|
|
|
specs/merge/fork-choice.md
|
|
|
|
specs/merge/validator.md
|
|
|
|
"""
|
2020-01-25 00:26:10 +00:00
|
|
|
else:
|
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)
|
|
|
|
|
|
|
|
def run(self):
|
2020-01-31 10:52:30 +00:00
|
|
|
spec_str = build_spec(self.spec_fork, self.parsed_md_doc_paths)
|
2020-01-25 00:26:10 +00:00
|
|
|
if self.dry_run:
|
|
|
|
self.announce('dry run successfully prepared contents for spec.'
|
2020-01-31 10:52:30 +00:00
|
|
|
f' out dir: "{self.out_dir}", spec fork: "{self.spec_fork}"')
|
2020-01-25 00:26:10 +00:00
|
|
|
self.debug_print(spec_str)
|
|
|
|
else:
|
|
|
|
dir_util.mkpath(self.out_dir)
|
|
|
|
with open(os.path.join(self.out_dir, 'spec.py'), 'w') as out:
|
|
|
|
out.write(spec_str)
|
|
|
|
with open(os.path.join(self.out_dir, '__init__.py'), 'w') as out:
|
|
|
|
out.write("")
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
2020-01-31 10:52:30 +00:00
|
|
|
for spec_fork in fork_imports:
|
|
|
|
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")
|
2020-01-31 10:52:30 +00:00
|
|
|
for spec_fork in fork_imports:
|
|
|
|
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'],
|
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-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"],
|
|
|
|
"lint": ["flake8==3.7.7", "mypy==0.750"],
|
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-04-12 22:11:21 +00:00
|
|
|
"remerkleable==0.1.19",
|
2020-03-20 19:38:36 +00:00
|
|
|
"ruamel.yaml==0.16.5",
|
2021-03-10 23:43:02 +00:00
|
|
|
"lru-dict==1.1.6",
|
2020-01-25 00:26:10 +00:00
|
|
|
]
|
|
|
|
)
|