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,
|
2019-06-05 19:42:55 +00:00
|
|
|
List,
|
2019-06-03 12:22:03 +00:00
|
|
|
Optional,
|
|
|
|
)
|
2019-05-16 14:36:35 +00:00
|
|
|
|
|
|
|
|
2019-05-20 08:50:07 +00:00
|
|
|
PHASE0_IMPORTS = '''from typing import (
|
2019-05-16 14:36:35 +00:00
|
|
|
Any,
|
2019-06-12 18:54:00 +00:00
|
|
|
Callable,
|
2019-05-16 14:36:35 +00:00
|
|
|
Dict,
|
|
|
|
List,
|
2019-06-11 04:15:52 +00:00
|
|
|
Set,
|
2019-05-16 14:36:35 +00:00
|
|
|
Tuple,
|
|
|
|
)
|
2019-05-16 19:01:32 +00:00
|
|
|
|
2019-06-05 13:29:26 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_impl import (
|
2019-05-16 14:36:35 +00:00
|
|
|
hash_tree_root,
|
|
|
|
signing_root,
|
|
|
|
)
|
2019-06-05 13:29:26 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import (
|
|
|
|
# unused: uint8, uint16, uint32, uint128, uint256,
|
2019-06-10 15:10:13 +00:00
|
|
|
uint64, Container, Vector,
|
|
|
|
Bytes4, Bytes32, Bytes48, Bytes96,
|
2019-06-05 13:29:26 +00:00
|
|
|
)
|
2019-05-30 20:57:18 +00:00
|
|
|
from eth2spec.utils.bls import (
|
2019-05-16 14:36:35 +00:00
|
|
|
bls_aggregate_pubkeys,
|
|
|
|
bls_verify,
|
|
|
|
bls_verify_multiple,
|
|
|
|
)
|
2019-06-05 13:29:26 +00:00
|
|
|
# Note: 'int' type defaults to being interpreted as a uint64 by SSZ implementation.
|
2019-05-16 19:01:32 +00:00
|
|
|
|
2019-05-16 14:36:35 +00:00
|
|
|
from eth2spec.utils.hash_function import hash
|
2019-05-18 08:42:04 +00:00
|
|
|
'''
|
2019-05-20 08:50:07 +00:00
|
|
|
PHASE1_IMPORTS = '''from typing import (
|
|
|
|
Any,
|
2019-06-12 18:54:00 +00:00
|
|
|
Callable,
|
2019-05-20 08:50:07 +00:00
|
|
|
Dict,
|
|
|
|
List,
|
2019-06-13 00:08:19 +00:00
|
|
|
Optional,
|
2019-06-11 04:15:52 +00:00
|
|
|
Set,
|
2019-05-20 08:50:07 +00:00
|
|
|
Tuple,
|
|
|
|
)
|
|
|
|
|
2019-06-05 13:29:26 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_impl import (
|
2019-05-20 08:50:07 +00:00
|
|
|
hash_tree_root,
|
|
|
|
signing_root,
|
2019-05-28 07:58:51 +00:00
|
|
|
serialize,
|
2019-06-05 13:29:26 +00:00
|
|
|
is_empty,
|
|
|
|
)
|
|
|
|
from eth2spec.utils.ssz.ssz_typing import (
|
|
|
|
# unused: uint8, uint16, uint32, uint128, uint256,
|
2019-06-10 15:10:13 +00:00
|
|
|
uint64, Container, Vector,
|
|
|
|
Bytes4, Bytes32, Bytes48, Bytes96,
|
2019-05-20 08:50:07 +00:00
|
|
|
)
|
2019-05-30 20:57:18 +00:00
|
|
|
from eth2spec.utils.bls import (
|
2019-05-20 08:50:07 +00:00
|
|
|
bls_aggregate_pubkeys,
|
|
|
|
bls_verify,
|
|
|
|
bls_verify_multiple,
|
|
|
|
)
|
|
|
|
|
|
|
|
from eth2spec.utils.hash_function import hash
|
|
|
|
'''
|
2019-05-20 12:00:54 +00:00
|
|
|
NEW_TYPES = {
|
2019-06-11 03:16:59 +00:00
|
|
|
'Slot': 'uint64',
|
|
|
|
'Epoch': 'uint64',
|
|
|
|
'Shard': 'uint64',
|
|
|
|
'ValidatorIndex': 'uint64',
|
|
|
|
'Gwei': 'uint64',
|
2019-05-20 12:00:54 +00:00
|
|
|
}
|
2019-06-05 13:29:26 +00:00
|
|
|
BYTE_TYPES = [4, 32, 48, 96]
|
2019-05-18 08:42:04 +00:00
|
|
|
SUNDRY_FUNCTIONS = '''
|
2019-06-05 13:29:26 +00:00
|
|
|
def get_ssz_type_by_name(name: str) -> Container:
|
|
|
|
return globals()[name]
|
|
|
|
|
|
|
|
|
2019-05-16 14:36:35 +00:00
|
|
|
# Monkey patch validator compute committee code
|
|
|
|
_compute_committee = compute_committee
|
2019-06-11 04:15:52 +00:00
|
|
|
committee_cache: Dict[Tuple[Bytes32, Bytes32, int, int], List[ValidatorIndex]] = {}
|
2019-05-16 14:36:35 +00:00
|
|
|
|
|
|
|
|
2019-06-11 04:15:52 +00:00
|
|
|
def compute_committee(indices: List[ValidatorIndex], # type: ignore
|
|
|
|
seed: Bytes32,
|
|
|
|
index: int,
|
|
|
|
count: int) -> List[ValidatorIndex]:
|
2019-05-16 14:36:35 +00:00
|
|
|
param_hash = (hash_tree_root(indices), seed, index, count)
|
|
|
|
|
|
|
|
if param_hash in committee_cache:
|
|
|
|
return committee_cache[param_hash]
|
|
|
|
else:
|
|
|
|
ret = _compute_committee(indices, seed, index, count)
|
|
|
|
committee_cache[param_hash] = ret
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
# Monkey patch hash cache
|
|
|
|
_hash = hash
|
2019-06-11 03:16:59 +00:00
|
|
|
hash_cache: Dict[bytes, Bytes32] = {}
|
2019-05-16 14:36:35 +00:00
|
|
|
|
|
|
|
|
2019-06-11 03:16:59 +00:00
|
|
|
def hash(x: bytes) -> Bytes32:
|
2019-05-16 14:36:35 +00:00
|
|
|
if x in hash_cache:
|
|
|
|
return hash_cache[x]
|
|
|
|
else:
|
|
|
|
ret = _hash(x)
|
|
|
|
hash_cache[x] = ret
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
# Access to overwrite spec constants based on configuration
|
2019-06-11 03:16:59 +00:00
|
|
|
def apply_constants_preset(preset: Dict[str, Any]) -> None:
|
2019-05-16 14:36:35 +00:00
|
|
|
global_vars = globals()
|
|
|
|
for k, v in preset.items():
|
|
|
|
global_vars[k] = v
|
|
|
|
|
|
|
|
# Deal with derived constants
|
|
|
|
global_vars['GENESIS_EPOCH'] = slot_to_epoch(GENESIS_SLOT)
|
|
|
|
|
|
|
|
# Initialize SSZ types again, to account for changed lengths
|
|
|
|
init_SSZ_types()
|
2019-05-18 08:42:04 +00:00
|
|
|
'''
|
|
|
|
|
2019-05-20 08:50:07 +00:00
|
|
|
|
2019-06-03 12:22:03 +00:00
|
|
|
def objects_to_spec(functions: Dict[str, str],
|
|
|
|
constants: Dict[str, str],
|
|
|
|
ssz_objects: Dict[str, str],
|
|
|
|
inserts: Dict[str, str],
|
2019-06-05 19:42:55 +00:00
|
|
|
imports: Dict[str, str],
|
|
|
|
new_types: Dict[str, str],
|
|
|
|
byte_types: List[int],
|
|
|
|
) -> 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(
|
|
|
|
[
|
|
|
|
f"class {key}({value}):\n"
|
|
|
|
f" def __init__(self, _x: uint64) -> None:\n"
|
|
|
|
f" ...\n"
|
|
|
|
for key, value in new_types.items()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2019-05-18 08:42:04 +00:00
|
|
|
functions_spec = '\n\n'.join(functions.values())
|
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-18 08:42:04 +00:00
|
|
|
ssz_objects_reinitialization_spec = (
|
2019-06-11 03:16:59 +00:00
|
|
|
'def init_SSZ_types() -> None:\n global_vars = globals()\n\n '
|
2019-06-05 13:29:26 +00:00
|
|
|
+ '\n\n '.join([re.sub(r'(?!\n\n)\n', r'\n ', value[:-1]) for value in ssz_objects.values()])
|
|
|
|
+ '\n\n'
|
|
|
|
+ '\n'.join(map(lambda x: ' global_vars[\'%s\'] = %s' % (x, x), ssz_objects.keys()))
|
2019-05-18 08:42:04 +00:00
|
|
|
)
|
2019-05-24 14:51:21 +00:00
|
|
|
spec = (
|
2019-05-20 08:50:07 +00:00
|
|
|
imports
|
2019-06-11 03:16:59 +00:00
|
|
|
+ '\n\n' + new_type_definitions
|
2019-05-20 12:00:54 +00:00
|
|
|
+ '\n\n' + constants_spec
|
2019-06-05 13:29:26 +00:00
|
|
|
+ '\n\n\n' + ssz_objects_instantiation_spec
|
|
|
|
+ '\n\n' + functions_spec
|
2019-05-18 08:42:04 +00:00
|
|
|
+ '\n' + SUNDRY_FUNCTIONS
|
|
|
|
+ '\n\n' + ssz_objects_reinitialization_spec
|
|
|
|
+ '\n'
|
|
|
|
)
|
2019-05-24 14:51:21 +00:00
|
|
|
# Handle @inserts
|
|
|
|
for key, value in inserts.items():
|
|
|
|
spec = re.sub('[ ]*# %s\\n' % key, value, spec)
|
|
|
|
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-04 15:08:18 +00:00
|
|
|
def dependency_order_ssz_objects(objects: Dict[str, str]) -> None:
|
2019-06-03 12:22:03 +00:00
|
|
|
"""
|
|
|
|
Determines which SSZ Object is depenedent on which other and orders them appropriately
|
|
|
|
"""
|
2019-05-20 12:00:54 +00:00
|
|
|
items = list(objects.items())
|
|
|
|
for key, value in items:
|
2019-06-05 13:29:26 +00:00
|
|
|
dependencies = re.findall(r'(: [A-Z][\w[]*)', value)
|
|
|
|
dependencies = map(lambda x: re.sub(r'\W|Vector|List|Container|uint\d+|Bytes\d+|bytes', '', x), dependencies)
|
2019-05-20 12:00:54 +00:00
|
|
|
for dep in dependencies:
|
2019-06-05 13:29:26 +00:00
|
|
|
if dep in NEW_TYPES or len(dep) == 0:
|
2019-05-20 12:00:54 +00:00
|
|
|
continue
|
|
|
|
key_list = list(objects.keys())
|
|
|
|
for item in [dep, key] + key_list[key_list.index(dep)+1:]:
|
|
|
|
objects[item] = objects.pop(item)
|
|
|
|
|
|
|
|
|
2019-06-03 12:22:03 +00:00
|
|
|
def combine_ssz_objects(old_objects: Dict[str, str], new_objects: Dict[str, str]) -> Dict[str, str]:
|
|
|
|
"""
|
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-06-05 13:29:26 +00:00
|
|
|
if key in old_objects:
|
|
|
|
# remove trailing newline
|
|
|
|
old_objects[key] = old_objects[key]
|
|
|
|
# remove leading variable name
|
|
|
|
value = re.sub(r'^class [\w]*\(Container\):\n', '', value)
|
|
|
|
old_objects[key] = old_objects.get(key, '') + value
|
2019-05-20 12:00:54 +00:00
|
|
|
dependency_order_ssz_objects(old_objects)
|
2019-05-20 09:17:24 +00:00
|
|
|
return old_objects
|
2019-05-18 08:42:04 +00:00
|
|
|
|
|
|
|
|
2019-05-24 14:51:21 +00:00
|
|
|
# inserts are handeled the same way as functions
|
|
|
|
combine_inserts = combine_functions
|
|
|
|
|
|
|
|
|
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-05-26 12:14:48 +00:00
|
|
|
functions0, constants0, ssz_objects0, inserts0 = spec0
|
|
|
|
functions1, constants1, ssz_objects1, inserts1 = spec1
|
|
|
|
functions = combine_functions(functions0, functions1)
|
|
|
|
constants = combine_constants(constants0, constants1)
|
|
|
|
ssz_objects = combine_ssz_objects(ssz_objects0, ssz_objects1)
|
|
|
|
inserts = combine_inserts(inserts0, inserts1)
|
|
|
|
return functions, constants, ssz_objects, inserts
|
|
|
|
|
|
|
|
|
2019-06-03 12:22:03 +00:00
|
|
|
def build_phase0_spec(sourcefile: str, outfile: str=None) -> Optional[str]:
|
|
|
|
functions, constants, ssz_objects, inserts = get_spec(sourcefile)
|
2019-06-05 19:42:55 +00:00
|
|
|
spec = objects_to_spec(functions, constants, ssz_objects, inserts, PHASE0_IMPORTS, NEW_TYPES, BYTE_TYPES)
|
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-06-03 12:22:03 +00:00
|
|
|
def build_phase1_spec(phase0_sourcefile: str,
|
|
|
|
phase1_custody_sourcefile: str,
|
|
|
|
phase1_shard_sourcefile: str,
|
|
|
|
outfile: str=None) -> Optional[str]:
|
|
|
|
phase0_spec = get_spec(phase0_sourcefile)
|
|
|
|
phase1_custody = get_spec(phase1_custody_sourcefile)
|
|
|
|
phase1_shard_data = get_spec(phase1_shard_sourcefile)
|
2019-05-26 12:14:48 +00:00
|
|
|
spec_objects = phase0_spec
|
|
|
|
for value in [phase1_custody, phase1_shard_data]:
|
|
|
|
spec_objects = combine_spec_objects(spec_objects, value)
|
2019-06-05 19:42:55 +00:00
|
|
|
spec = objects_to_spec(*spec_objects, PHASE1_IMPORTS, NEW_TYPES, BYTE_TYPES)
|
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:
|
2019-05-16 19:01:32 +00:00
|
|
|
1st argument is input spec.md
|
2019-05-16 14:36:35 +00:00
|
|
|
2nd argument is output spec.py
|
|
|
|
|
|
|
|
If building phase 1:
|
2019-05-16 19:01:32 +00:00
|
|
|
1st argument is input spec_phase0.md
|
2019-05-30 20:57:18 +00:00
|
|
|
2nd argument is input spec_phase1_custody.md
|
|
|
|
3rd argument is input spec_phase1_shard_data.md
|
|
|
|
4th 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-05 19:42:55 +00:00
|
|
|
if len(args.files) == 2:
|
|
|
|
build_phase0_spec(*args.files)
|
|
|
|
else:
|
|
|
|
print(" Phase 0 requires an output as well as an input file.")
|
2019-05-16 14:36:35 +00:00
|
|
|
elif args.phase == 1:
|
2019-05-26 12:14:48 +00:00
|
|
|
if len(args.files) == 4:
|
2019-05-16 14:36:35 +00:00
|
|
|
build_phase1_spec(*args.files)
|
|
|
|
else:
|
2019-05-26 12:14:48 +00:00
|
|
|
print(" Phase 1 requires an output as well as 3 input files (phase0.md and phase1.md, phase1.md)")
|
2019-05-16 14:36:35 +00:00
|
|
|
else:
|
|
|
|
print("Invalid phase: {0}".format(args.phase))
|