2019-05-16 14:36:35 +00:00
|
|
|
import re
|
2019-06-03 12:22:03 +00:00
|
|
|
from function_puller import (
|
|
|
|
get_spec,
|
|
|
|
SpecObject,
|
|
|
|
)
|
2019-05-16 14:36:35 +00:00
|
|
|
from argparse import ArgumentParser
|
2019-06-03 12:22:03 +00:00
|
|
|
from typing import (
|
|
|
|
Dict,
|
|
|
|
Optional,
|
|
|
|
)
|
2019-05-16 14:36:35 +00:00
|
|
|
|
2019-11-19 19:16:40 +00:00
|
|
|
CONFIG_LOADER = '''
|
|
|
|
apply_constants_preset(globals())
|
|
|
|
'''
|
2019-05-16 14:36:35 +00:00
|
|
|
|
2019-11-19 19:16:40 +00:00
|
|
|
PHASE0_IMPORTS = '''from eth2spec.config.apply_config import apply_constants_preset
|
|
|
|
from typing import (
|
2020-01-09 13:56:06 +00:00
|
|
|
Any, Callable, Dict, Set, Sequence, Tuple, Optional, TypeVar
|
2019-05-16 14:36:35 +00:00
|
|
|
)
|
2019-05-16 19:01:32 +00:00
|
|
|
|
2019-06-17 14:48:33 +00:00
|
|
|
from dataclasses import (
|
|
|
|
dataclass,
|
|
|
|
field,
|
|
|
|
)
|
|
|
|
|
2019-11-21 22:13:45 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
|
2019-06-05 13:29:26 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import (
|
2020-01-24 23:43:43 +00:00
|
|
|
View, boolean, Container, List, Vector, uint64,
|
2019-07-01 01:36:13 +00:00
|
|
|
Bytes1, Bytes4, Bytes8, Bytes32, Bytes48, Bytes96, Bitlist, Bitvector,
|
2019-06-05 13:29:26 +00:00
|
|
|
)
|
2019-12-20 06:41:46 +00:00
|
|
|
from eth2spec.utils import bls
|
2019-05-16 19:01:32 +00:00
|
|
|
|
2019-05-16 14:36:35 +00:00
|
|
|
from eth2spec.utils.hash_function import hash
|
2019-12-17 10:04:56 +00:00
|
|
|
|
2020-01-24 23:43:43 +00:00
|
|
|
SSZObject = TypeVar('SSZObject', bound=View)
|
2019-05-18 08:42:04 +00:00
|
|
|
'''
|
2019-11-19 19:16:40 +00:00
|
|
|
PHASE1_IMPORTS = '''from eth2spec.phase0 import spec as phase0
|
|
|
|
from eth2spec.config.apply_config import apply_constants_preset
|
|
|
|
from typing import (
|
2020-01-24 20:49:22 +00:00
|
|
|
Any, Callable, Dict, Set, Sequence, NewType, Optional, Tuple, TypeVar
|
2019-08-15 07:30:01 +00:00
|
|
|
)
|
2019-05-20 08:50:07 +00:00
|
|
|
|
2019-06-17 14:48:33 +00:00
|
|
|
from dataclasses import (
|
|
|
|
dataclass,
|
|
|
|
field,
|
|
|
|
)
|
|
|
|
|
2019-12-05 20:49:52 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
|
2019-06-05 13:29:26 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import (
|
2020-01-24 23:43:43 +00:00
|
|
|
View, boolean, Container, List, Vector, uint64, uint8, bit,
|
2020-01-08 10:19:18 +00:00
|
|
|
ByteList, Bytes1, Bytes4, Bytes8, Bytes32, Bytes48, Bytes96, Bitlist, Bitvector,
|
2019-05-20 08:50:07 +00:00
|
|
|
)
|
2019-12-20 06:41:46 +00:00
|
|
|
from eth2spec.utils import bls
|
2019-05-20 08:50:07 +00:00
|
|
|
|
|
|
|
from eth2spec.utils.hash_function import hash
|
2019-08-20 10:55:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
SSZVariableName = str
|
2019-08-23 12:16:46 +00:00
|
|
|
GeneralizedIndex = NewType('GeneralizedIndex', int)
|
2020-01-24 23:43:43 +00:00
|
|
|
SSZObject = TypeVar('SSZObject', bound=View)
|
2019-05-20 08:50:07 +00:00
|
|
|
'''
|
2019-08-11 17:05:17 +00:00
|
|
|
SUNDRY_CONSTANTS_FUNCTIONS = '''
|
|
|
|
def ceillog2(x: uint64) -> int:
|
|
|
|
return (x - 1).bit_length()
|
|
|
|
'''
|
2019-05-18 08:42:04 +00:00
|
|
|
SUNDRY_FUNCTIONS = '''
|
2019-06-20 18:25:22 +00:00
|
|
|
# Monkey patch hash cache
|
|
|
|
_hash = hash
|
2019-11-12 20:29:58 +00:00
|
|
|
hash_cache: Dict[bytes, Bytes32] = {}
|
2019-06-20 18:25:22 +00:00
|
|
|
|
|
|
|
|
2019-11-12 20:29:58 +00:00
|
|
|
def get_eth1_data(distance: uint64) -> Bytes32:
|
2019-06-30 08:58:04 +00:00
|
|
|
return hash(distance)
|
|
|
|
|
|
|
|
|
2019-12-17 14:22:46 +00:00
|
|
|
def hash(x: bytes) -> Bytes32: # type: ignore
|
2019-06-20 18:25:22 +00:00
|
|
|
if x not in hash_cache:
|
2019-11-12 20:29:58 +00:00
|
|
|
hash_cache[x] = Bytes32(_hash(x))
|
2019-06-20 18:25:22 +00:00
|
|
|
return hash_cache[x]
|
|
|
|
|
|
|
|
|
2020-01-24 23:43:43 +00:00
|
|
|
def cache_this(key_fn, value_fn): # type: ignore
|
|
|
|
cache_dict = {} # type: ignore
|
2019-05-16 14:36:35 +00:00
|
|
|
|
2020-01-24 23:43:43 +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
|
2019-05-16 14:36:35 +00:00
|
|
|
|
|
|
|
|
2020-01-24 23:43:43 +00:00
|
|
|
get_base_reward = cache_this(
|
|
|
|
lambda state, index: (state.validators.hash_tree_root(), state.slot),
|
|
|
|
get_base_reward)
|
|
|
|
|
|
|
|
get_committee_count_at_slot = cache_this(
|
|
|
|
lambda state, epoch: (state.validators.hash_tree_root(), epoch),
|
|
|
|
get_committee_count_at_slot)
|
|
|
|
|
|
|
|
get_active_validator_indices = cache_this(
|
|
|
|
lambda state, epoch: (state.validators.hash_tree_root(), epoch),
|
|
|
|
get_active_validator_indices)
|
|
|
|
|
|
|
|
get_beacon_committee = cache_this(
|
|
|
|
lambda state, slot, index: (state.validators.hash_tree_root(), state.randao_mixes.hash_tree_root(), slot, index),
|
|
|
|
get_beacon_committee)'''
|
2019-06-20 19:42:55 +00:00
|
|
|
|
|
|
|
|
2019-06-03 12:22:03 +00:00
|
|
|
def objects_to_spec(functions: Dict[str, str],
|
2019-06-15 20:57:50 +00:00
|
|
|
custom_types: Dict[str, str],
|
2019-06-03 12:22:03 +00:00
|
|
|
constants: Dict[str, str],
|
|
|
|
ssz_objects: Dict[str, str],
|
2020-01-24 23:43:43 +00:00
|
|
|
imports: str,
|
2019-12-05 22:06:32 +00:00
|
|
|
version: str,
|
2019-06-05 19:42:55 +00:00
|
|
|
) -> str:
|
2019-06-03 12:22:03 +00:00
|
|
|
"""
|
|
|
|
Given all the objects that constitute a spec, combine them into a single pyfile.
|
|
|
|
"""
|
2019-06-11 03:16:59 +00:00
|
|
|
new_type_definitions = (
|
|
|
|
'\n\n'.join(
|
|
|
|
[
|
2019-06-25 16:42:34 +00:00
|
|
|
f"class {key}({value}):\n pass\n"
|
2019-06-15 20:57:50 +00:00
|
|
|
for key, value in custom_types.items()
|
2019-06-11 03:16:59 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2019-08-11 17:05:17 +00:00
|
|
|
for k in list(functions):
|
|
|
|
if "ceillog2" in k:
|
|
|
|
del functions[k]
|
2019-05-18 08:42:04 +00:00
|
|
|
functions_spec = '\n\n'.join(functions.values())
|
2019-06-30 20:12:02 +00:00
|
|
|
for k in list(constants.keys()):
|
2019-08-11 17:05:17 +00:00
|
|
|
if k == "BLS12_381_Q":
|
|
|
|
constants[k] += " # noqa: E501"
|
2019-05-30 20:57:18 +00:00
|
|
|
constants_spec = '\n'.join(map(lambda x: '%s = %s' % (x, constants[x]), constants))
|
2019-06-05 13:29:26 +00:00
|
|
|
ssz_objects_instantiation_spec = '\n\n'.join(ssz_objects.values())
|
2019-05-24 14:51:21 +00:00
|
|
|
spec = (
|
2019-05-20 08:50:07 +00:00
|
|
|
imports
|
2019-12-05 22:06:32 +00:00
|
|
|
+ '\n\n' + f"version = \'{version}\'\n"
|
2019-06-11 03:16:59 +00:00
|
|
|
+ '\n\n' + new_type_definitions
|
2019-08-11 17:05:17 +00:00
|
|
|
+ '\n' + SUNDRY_CONSTANTS_FUNCTIONS
|
2019-05-20 12:00:54 +00:00
|
|
|
+ '\n\n' + constants_spec
|
2019-11-19 19:16:40 +00:00
|
|
|
+ '\n\n' + CONFIG_LOADER
|
|
|
|
+ '\n\n' + ssz_objects_instantiation_spec
|
2019-06-05 13:29:26 +00:00
|
|
|
+ '\n\n' + functions_spec
|
2019-05-18 08:42:04 +00:00
|
|
|
+ '\n' + SUNDRY_FUNCTIONS
|
|
|
|
+ '\n'
|
|
|
|
)
|
2019-05-24 14:51:21 +00:00
|
|
|
return spec
|
|
|
|
|
2019-05-18 08:42:04 +00:00
|
|
|
|
2019-06-03 13:14:20 +00:00
|
|
|
def combine_functions(old_functions: Dict[str, str], new_functions: Dict[str, str]) -> Dict[str, str]:
|
2019-05-18 08:42:04 +00:00
|
|
|
for key, value in new_functions.items():
|
2019-06-03 13:14:20 +00:00
|
|
|
old_functions[key] = value
|
|
|
|
return old_functions
|
2019-05-18 08:42:04 +00:00
|
|
|
|
|
|
|
|
2019-06-03 12:22:03 +00:00
|
|
|
def combine_constants(old_constants: Dict[str, str], new_constants: Dict[str, str]) -> Dict[str, str]:
|
2019-05-18 08:42:04 +00:00
|
|
|
for key, value in new_constants.items():
|
|
|
|
old_constants[key] = value
|
|
|
|
return old_constants
|
|
|
|
|
2019-05-20 09:17:24 +00:00
|
|
|
|
2019-06-20 18:13:55 +00:00
|
|
|
ignored_dependencies = [
|
2019-12-05 19:36:48 +00:00
|
|
|
'bit', 'boolean', 'Vector', 'List', 'Container', 'BLSPubkey', 'BLSSignature',
|
2019-06-30 19:51:10 +00:00
|
|
|
'Bytes1', 'Bytes4', 'Bytes32', 'Bytes48', 'Bytes96', 'Bitlist', 'Bitvector',
|
2019-06-20 18:13:55 +00:00
|
|
|
'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256',
|
2019-12-05 19:36:48 +00:00
|
|
|
'bytes', 'byte', 'ByteList', 'ByteVector'
|
2019-06-20 18:13:55 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-06-15 21:23:44 +00:00
|
|
|
def dependency_order_ssz_objects(objects: Dict[str, str], custom_types: Dict[str, str]) -> None:
|
2019-06-03 12:22:03 +00:00
|
|
|
"""
|
2019-08-19 11:05:44 +00:00
|
|
|
Determines which SSZ Object is dependent on which other and orders them appropriately
|
2019-06-03 12:22:03 +00:00
|
|
|
"""
|
2019-05-20 12:00:54 +00:00
|
|
|
items = list(objects.items())
|
|
|
|
for key, value in items:
|
2019-06-20 18:13:55 +00:00
|
|
|
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
|
2019-06-20 18:12:17 +00:00
|
|
|
dependencies = filter(lambda x: '_' not in x and x.upper() != x, dependencies) # filter out constants
|
2019-06-20 18:13:55 +00:00
|
|
|
dependencies = filter(lambda x: x not in ignored_dependencies, dependencies)
|
|
|
|
dependencies = filter(lambda x: x not in custom_types, dependencies)
|
2019-05-20 12:00:54 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2019-06-15 21:23:44 +00:00
|
|
|
def combine_ssz_objects(old_objects: Dict[str, str], new_objects: Dict[str, str], custom_types) -> Dict[str, str]:
|
2019-06-03 12:22:03 +00:00
|
|
|
"""
|
2019-06-03 13:14:20 +00:00
|
|
|
Takes in old spec and new spec ssz objects, combines them,
|
2019-06-03 12:22:03 +00:00
|
|
|
and returns the newer versions of the objects in dependency order.
|
|
|
|
"""
|
2019-05-18 08:42:04 +00:00
|
|
|
for key, value in new_objects.items():
|
2019-11-19 19:16:40 +00:00
|
|
|
old_objects[key] = value
|
2019-05-20 09:17:24 +00:00
|
|
|
return old_objects
|
2019-05-18 08:42:04 +00:00
|
|
|
|
|
|
|
|
2019-06-03 12:22:03 +00:00
|
|
|
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.
|
|
|
|
"""
|
2019-11-19 19:16:40 +00:00
|
|
|
functions0, custom_types0, constants0, ssz_objects0 = spec0
|
|
|
|
functions1, custom_types1, constants1, ssz_objects1 = spec1
|
2019-05-26 12:14:48 +00:00
|
|
|
functions = combine_functions(functions0, functions1)
|
2019-06-15 20:57:50 +00:00
|
|
|
custom_types = combine_constants(custom_types0, custom_types1)
|
2019-05-26 12:14:48 +00:00
|
|
|
constants = combine_constants(constants0, constants1)
|
2019-06-15 21:23:44 +00:00
|
|
|
ssz_objects = combine_ssz_objects(ssz_objects0, ssz_objects1, custom_types)
|
2020-01-24 23:43:43 +00:00
|
|
|
return SpecObject((functions, custom_types, constants, ssz_objects))
|
2019-05-26 12:14:48 +00:00
|
|
|
|
|
|
|
|
2019-11-20 03:43:32 +00:00
|
|
|
def dependency_order_spec(objs: SpecObject):
|
|
|
|
functions, custom_types, constants, ssz_objects = objs
|
|
|
|
dependency_order_ssz_objects(ssz_objects, custom_types)
|
2019-05-26 12:14:48 +00:00
|
|
|
|
|
|
|
|
2019-06-30 08:58:04 +00:00
|
|
|
def build_phase0_spec(phase0_sourcefile: str, fork_choice_sourcefile: str,
|
|
|
|
v_guide_sourcefile: str, outfile: str=None) -> Optional[str]:
|
2019-06-15 22:42:03 +00:00
|
|
|
phase0_spec = get_spec(phase0_sourcefile)
|
|
|
|
fork_choice_spec = get_spec(fork_choice_sourcefile)
|
2019-06-30 08:58:04 +00:00
|
|
|
v_guide = get_spec(v_guide_sourcefile)
|
|
|
|
spec_objects = phase0_spec
|
|
|
|
for value in [fork_choice_spec, v_guide]:
|
|
|
|
spec_objects = combine_spec_objects(spec_objects, value)
|
2019-11-20 03:43:32 +00:00
|
|
|
dependency_order_spec(spec_objects)
|
2019-12-05 22:06:32 +00:00
|
|
|
spec = objects_to_spec(*spec_objects, PHASE0_IMPORTS, 'phase0')
|
2019-05-16 14:36:35 +00:00
|
|
|
if outfile is not None:
|
|
|
|
with open(outfile, 'w') as out:
|
2019-05-18 08:42:04 +00:00
|
|
|
out.write(spec)
|
2019-06-05 19:42:55 +00:00
|
|
|
return spec
|
2019-05-16 14:36:35 +00:00
|
|
|
|
|
|
|
|
2019-08-23 18:37:19 +00:00
|
|
|
def build_phase1_spec(phase0_beacon_sourcefile: str,
|
|
|
|
phase0_fork_choice_sourcefile: str,
|
2019-06-03 12:22:03 +00:00
|
|
|
phase1_custody_sourcefile: str,
|
2019-11-18 21:04:29 +00:00
|
|
|
phase1_beacon_sourcefile: str,
|
|
|
|
phase1_fraud_sourcefile: str,
|
2020-01-15 23:03:07 +00:00
|
|
|
phase1_fork_choice_sourcefile: str,
|
2019-11-18 21:04:29 +00:00
|
|
|
phase1_fork_sourcefile: str,
|
2019-06-03 12:22:03 +00:00
|
|
|
outfile: str=None) -> Optional[str]:
|
2019-08-15 07:07:44 +00:00
|
|
|
all_sourcefiles = (
|
2019-08-23 18:37:19 +00:00
|
|
|
phase0_beacon_sourcefile,
|
|
|
|
phase0_fork_choice_sourcefile,
|
2019-08-15 07:07:44 +00:00
|
|
|
phase1_custody_sourcefile,
|
2019-11-18 21:04:29 +00:00
|
|
|
phase1_beacon_sourcefile,
|
|
|
|
phase1_fraud_sourcefile,
|
2020-01-15 23:03:07 +00:00
|
|
|
phase1_fork_choice_sourcefile,
|
2019-11-18 21:04:29 +00:00
|
|
|
phase1_fork_sourcefile,
|
2019-08-15 07:07:44 +00:00
|
|
|
)
|
|
|
|
all_spescs = [get_spec(spec) for spec in all_sourcefiles]
|
|
|
|
spec_objects = all_spescs[0]
|
|
|
|
for value in all_spescs[1:]:
|
2019-05-26 12:14:48 +00:00
|
|
|
spec_objects = combine_spec_objects(spec_objects, value)
|
2019-11-20 03:43:32 +00:00
|
|
|
dependency_order_spec(spec_objects)
|
2019-12-05 22:06:32 +00:00
|
|
|
spec = objects_to_spec(*spec_objects, PHASE1_IMPORTS, 'phase1')
|
2019-05-16 14:36:35 +00:00
|
|
|
if outfile is not None:
|
|
|
|
with open(outfile, 'w') as out:
|
2019-05-18 08:42:04 +00:00
|
|
|
out.write(spec)
|
2019-06-05 19:42:55 +00:00
|
|
|
return spec
|
2019-05-16 14:36:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
description = '''
|
|
|
|
Build the specs from the md docs.
|
|
|
|
If building phase 0:
|
2020-01-10 18:42:55 +00:00
|
|
|
1st argument is input phase0/beacon-chain.md
|
|
|
|
2nd argument is input phase0/fork-choice.md
|
|
|
|
3rd argument is input phase0/validator.md
|
2019-06-30 08:58:04 +00:00
|
|
|
4th argument is output spec.py
|
2019-05-16 14:36:35 +00:00
|
|
|
|
|
|
|
If building phase 1:
|
2020-01-10 18:42:55 +00:00
|
|
|
1st argument is input phase0/beacon-chain.md
|
|
|
|
2nd argument is input phase0/fork-choice.md
|
2020-01-13 17:55:21 +00:00
|
|
|
3rd argument is input phase1/custody-game.md
|
|
|
|
4th argument is input phase1/beacon-chain.md
|
|
|
|
5th argument is input phase1/fraud-proofs.md
|
2020-01-15 23:03:07 +00:00
|
|
|
6th argument is input phase1/fork-choice.md
|
|
|
|
7th argument is input phase1/phase1-fork.md
|
|
|
|
8th argument is output spec.py
|
2019-05-16 14:36:35 +00:00
|
|
|
'''
|
|
|
|
parser = ArgumentParser(description=description)
|
|
|
|
parser.add_argument("-p", "--phase", dest="phase", type=int, default=0, help="Build for phase #")
|
|
|
|
parser.add_argument(dest="files", help="Input and output files", nargs="+")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.phase == 0:
|
2019-06-30 08:58:04 +00:00
|
|
|
if len(args.files) == 4:
|
2019-06-05 19:42:55 +00:00
|
|
|
build_phase0_spec(*args.files)
|
|
|
|
else:
|
2019-06-30 08:58:04 +00:00
|
|
|
print(" Phase 0 requires spec, forkchoice, and v-guide inputs as well as an output file.")
|
2019-05-16 14:36:35 +00:00
|
|
|
elif args.phase == 1:
|
2020-01-15 23:03:07 +00:00
|
|
|
if len(args.files) == 8:
|
2019-05-16 14:36:35 +00:00
|
|
|
build_phase1_spec(*args.files)
|
|
|
|
else:
|
2019-08-15 07:07:44 +00:00
|
|
|
print(
|
2019-08-15 10:26:22 +00:00
|
|
|
" Phase 1 requires input files as well as an output file:\n"
|
2020-01-10 18:42:55 +00:00
|
|
|
"\t phase0: (beacon-chain.md, fork-choice.md)\n"
|
2020-01-15 23:03:07 +00:00
|
|
|
"\t phase1: (custody-game.md, beacon-chain.md, fraud-proofs.md, fork-choice.md, phase1-fork.md)\n"
|
2019-08-15 07:07:44 +00:00
|
|
|
"\t and output.py"
|
|
|
|
)
|
2019-05-16 14:36:35 +00:00
|
|
|
else:
|
|
|
|
print("Invalid phase: {0}".format(args.phase))
|