2019-05-16 14:36:35 +00:00
|
|
|
import re
|
|
|
|
import function_puller
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from typing import Tuple, List
|
|
|
|
|
|
|
|
|
2019-05-20 08:50:07 +00:00
|
|
|
PHASE0_IMPORTS = '''from typing import (
|
2019-05-16 14:36:35 +00:00
|
|
|
Any,
|
|
|
|
Dict,
|
|
|
|
List,
|
|
|
|
NewType,
|
|
|
|
Tuple,
|
|
|
|
)
|
2019-05-16 19:01:32 +00:00
|
|
|
|
2019-05-16 14:36:35 +00:00
|
|
|
from eth2spec.utils.minimal_ssz import (
|
|
|
|
SSZType,
|
|
|
|
hash_tree_root,
|
|
|
|
signing_root,
|
|
|
|
)
|
2019-05-16 19:01:32 +00:00
|
|
|
|
2019-05-16 14:36:35 +00:00
|
|
|
from eth2spec.utils.bls_stub import (
|
|
|
|
bls_aggregate_pubkeys,
|
|
|
|
bls_verify,
|
|
|
|
bls_verify_multiple,
|
|
|
|
)
|
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,
|
|
|
|
Dict,
|
|
|
|
List,
|
|
|
|
NewType,
|
|
|
|
Tuple,
|
|
|
|
)
|
|
|
|
|
|
|
|
from eth2spec.utils.minimal_ssz import (
|
|
|
|
SSZType,
|
|
|
|
hash_tree_root,
|
|
|
|
signing_root,
|
|
|
|
type_of,
|
|
|
|
empty,
|
|
|
|
)
|
|
|
|
|
|
|
|
from eth2spec.utils.bls_stub import (
|
|
|
|
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 = {
|
|
|
|
'Slot': 'int',
|
|
|
|
'Epoch': 'int',
|
|
|
|
'Shard': 'int',
|
|
|
|
'ValidatorIndex': 'int',
|
|
|
|
'Gwei': 'int',
|
|
|
|
'Bytes32': 'bytes',
|
|
|
|
'BLSPubkey': 'bytes',
|
|
|
|
'BLSSignature': 'bytes',
|
|
|
|
'Store': 'None',
|
2019-05-20 16:20:13 +00:00
|
|
|
'Hash': 'bytes'
|
2019-05-20 12:00:54 +00:00
|
|
|
}
|
2019-05-18 08:42:04 +00:00
|
|
|
SUNDRY_FUNCTIONS = '''
|
2019-05-16 14:36:35 +00:00
|
|
|
# Monkey patch validator compute committee code
|
|
|
|
_compute_committee = compute_committee
|
|
|
|
committee_cache = {}
|
|
|
|
|
|
|
|
|
|
|
|
def compute_committee(indices: List[ValidatorIndex], seed: Bytes32, index: int, count: int) -> List[ValidatorIndex]:
|
|
|
|
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
|
|
|
|
hash_cache = {}
|
|
|
|
|
|
|
|
|
|
|
|
def hash(x):
|
|
|
|
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
|
|
|
|
def apply_constants_preset(preset: Dict[str, Any]):
|
|
|
|
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-05-26 12:14:48 +00:00
|
|
|
def objects_to_spec(functions, constants, ssz_objects, inserts, imports):
|
2019-05-20 12:00:54 +00:00
|
|
|
new_type_definitions = '\n'.join(['''%s = NewType('%s', %s)''' % (key, key, value) for key, value in NEW_TYPES.items()])
|
2019-05-18 08:42:04 +00:00
|
|
|
functions_spec = '\n\n'.join(functions.values())
|
|
|
|
constants_spec = '\n'.join(map(lambda x: '%s = %s' % (x, constants[x]),constants))
|
|
|
|
ssz_objects_instantiation_spec = '\n'.join(map(lambda x: '%s = SSZType(%s)' % (x, ssz_objects[x][:-1]), ssz_objects))
|
|
|
|
ssz_objects_reinitialization_spec = '\n'.join(
|
2019-05-22 10:40:57 +00:00
|
|
|
map(lambda x: ' global_vars[\'%s\'] = SSZType(%s })' % (x, re.sub('( ){4}', ' '*8, ssz_objects[x][:-2])), ssz_objects))
|
2019-05-18 08:42:04 +00:00
|
|
|
ssz_objects_reinitialization_spec = (
|
|
|
|
'def init_SSZ_types():\n global_vars = globals()\n'
|
|
|
|
+ ssz_objects_reinitialization_spec
|
|
|
|
)
|
2019-05-24 14:51:21 +00:00
|
|
|
spec = (
|
2019-05-20 08:50:07 +00:00
|
|
|
imports
|
2019-05-20 12:00:54 +00:00
|
|
|
+ '\n' + new_type_definitions
|
|
|
|
+ '\n\n' + constants_spec
|
|
|
|
+ '\n\n' + ssz_objects_instantiation_spec
|
2019-05-18 08:42:04 +00:00
|
|
|
+ '\n\n\n' + functions_spec
|
|
|
|
+ '\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
|
|
|
|
|
|
|
def combine_functions(old_funcitons, new_functions):
|
|
|
|
for key, value in new_functions.items():
|
|
|
|
old_funcitons[key] = value
|
|
|
|
return old_funcitons
|
|
|
|
|
|
|
|
|
|
|
|
def combine_constants(old_constants, new_constants):
|
|
|
|
for key, value in new_constants.items():
|
|
|
|
old_constants[key] = value
|
|
|
|
return old_constants
|
|
|
|
|
2019-05-20 09:17:24 +00:00
|
|
|
|
2019-05-20 12:00:54 +00:00
|
|
|
def dependency_order_ssz_objects(objects):
|
|
|
|
items = list(objects.items())
|
|
|
|
for key, value in items:
|
|
|
|
dependencies = re.findall(r'(: [\[]*[A-Z][a-z][\w]+)', value)
|
|
|
|
dependencies = map(lambda x: re.sub(r'\W', '', x), dependencies)
|
|
|
|
for dep in dependencies:
|
|
|
|
if dep in NEW_TYPES:
|
|
|
|
continue
|
|
|
|
key_list = list(objects.keys())
|
|
|
|
for item in [dep, key] + key_list[key_list.index(dep)+1:]:
|
|
|
|
objects[item] = objects.pop(item)
|
|
|
|
|
|
|
|
|
2019-05-18 08:42:04 +00:00
|
|
|
def combine_ssz_objects(old_objects, new_objects):
|
|
|
|
for key, value in new_objects.items():
|
2019-05-20 09:17:24 +00:00
|
|
|
# remove leading "{" and trailing "\n}"
|
2019-05-20 12:00:54 +00:00
|
|
|
old_objects[key] = old_objects.get(key, '')[1:-3]
|
2019-05-20 09:17:24 +00:00
|
|
|
# remove leading "{"
|
|
|
|
value = value[1:]
|
2019-05-20 12:00:54 +00:00
|
|
|
old_objects[key] = '{' + old_objects.get(key, '') + value
|
|
|
|
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-05-26 12:14:48 +00:00
|
|
|
def combine_spec_objects(spec0, spec1):
|
|
|
|
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-05-18 08:42:04 +00:00
|
|
|
def build_phase0_spec(sourcefile, outfile=None):
|
2019-05-26 12:14:48 +00:00
|
|
|
functions, constants, ssz_objects, inserts = function_puller.get_spec(sourcefile)
|
|
|
|
spec = objects_to_spec(functions, constants, ssz_objects, inserts, PHASE0_IMPORTS)
|
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-05-16 14:36:35 +00:00
|
|
|
else:
|
2019-05-18 08:42:04 +00:00
|
|
|
return spec
|
2019-05-16 14:36:35 +00:00
|
|
|
|
|
|
|
|
2019-05-26 12:14:48 +00:00
|
|
|
def build_phase1_spec(phase0_sourcefile, phase1_custody_sourcefile, phase1_shard_sourcefile, outfile=None):
|
|
|
|
phase0_spec = function_puller.get_spec(phase0_sourcefile)
|
|
|
|
phase1_custody = function_puller.get_spec(phase1_custody_sourcefile)
|
|
|
|
phase1_shard_data = function_puller.get_spec(phase1_shard_sourcefile)
|
|
|
|
spec_objects = phase0_spec
|
|
|
|
for value in [phase1_custody, phase1_shard_data]:
|
|
|
|
spec_objects = combine_spec_objects(spec_objects, value)
|
|
|
|
spec = objects_to_spec(*spec_objects, PHASE1_IMPORTS)
|
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-05-16 14:36:35 +00:00
|
|
|
else:
|
2019-05-18 08:42:04 +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
|
|
|
|
2nd argument is input spec_phase1.md
|
2019-05-16 14:36:35 +00:00
|
|
|
3rd argument is output spec.py
|
|
|
|
'''
|
|
|
|
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:
|
|
|
|
build_phase0_spec(*args.files)
|
|
|
|
elif args.phase == 1:
|
2019-05-26 12:14:48 +00:00
|
|
|
print(args.files)
|
|
|
|
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))
|