Skip to content

Commit

Permalink
POC ssz types spec build + update spec defs, typing still needs work
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed May 12, 2019
1 parent 761c9e5 commit 08faa86
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 134 deletions.
4 changes: 2 additions & 2 deletions scripts/phase0/build_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def build_phase0_spec(sourcefile, outfile):
NewType,
Tuple,
)
from eth2spec.utils.minimal_ssz import (
SSZType,
from eth2spec.utils.ssz.ssz_impl import (
hash_tree_root,
signing_root,
)
from eth2spec.utils.ssz.ssz_typing import *
from eth2spec.utils.bls_stub import (
bls_aggregate_pubkeys,
bls_verify,
Expand Down
41 changes: 24 additions & 17 deletions scripts/phase0/function_puller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
from typing import List


def translate_ssz_type_line(line: str) -> str:
if ':' not in line:
return line
start = line[:line.index(':')]
field_type = line[line.index(':')+2:]
if field_type.startswith('['):
if ',' in line:
# TODO: translate [Foobar, SOME_THING] to Vector[Foobar, SSZLen(SOME_THING)] cleanly.
# just brute it here
field_type = 'Vector[%s, SSLen(%s)]' % (field_type[1:field_type.index(',')], field_type[field_type.index(',')+2:len(field_type)-1])
else:
field_type = 'List[%s]' % field_type[1:len(field_type)-1]
line = start + ': ' + field_type
return line


def get_spec(file_name: str) -> List[str]:
code_lines = []
pulling_from = None
Expand All @@ -21,24 +37,23 @@ def get_spec(file_name: str) -> List[str]:
else:
if current_typedef is not None:
assert code_lines[-1] == '}'
code_lines[-1] = '})'
current_typedef[-1] = '})'
type_defs.append((current_name, current_typedef))
code_lines[-1] = ''
code_lines.append('')
pulling_from = None
current_typedef = None
else:
if pulling_from == linenum and line == '{':
code_lines.append('%s = SSZType({' % current_name)
current_typedef = ['global_vars["%s"] = SSZType({' % current_name]
code_lines.append('class %s(SSZContainer):' % current_name)
current_typedef = current_name
type_defs.append(current_name)
elif pulling_from is not None:
# Add some whitespace between functions
if line[:3] == 'def':
code_lines.append('')
code_lines.append('')
code_lines.append(line)
# Remember type def lines
if current_typedef is not None:
current_typedef.append(line)
line = translate_ssz_type_line(line)
code_lines.append(line)
elif pulling_from is None and len(line) > 0 and line[0] == '|':
row = line[1:].split('|')
if len(row) >= 2:
Expand All @@ -56,16 +71,8 @@ def get_spec(file_name: str) -> List[str]:
code_lines.append(row[0] + ' = ' + (row[1].replace('**TBD**', '0x1234567890123456789012345678901234567890')))
# Build type-def re-initialization
code_lines.append('\n')
code_lines.append('def init_SSZ_types():')
code_lines.append(' global_vars = globals()')
for ssz_type_name, ssz_type in type_defs:
code_lines.append('')
for type_line in ssz_type:
if len(type_line) > 0:
code_lines.append(' ' + type_line)
code_lines.append('\n')
code_lines.append('ssz_types = [\n')
for (ssz_type_name, _) in type_defs:
for ssz_type_name in type_defs:
code_lines.append(f' {ssz_type_name},\n')
code_lines.append(']')
code_lines.append('\n')
Expand Down
Loading

0 comments on commit 08faa86

Please sign in to comment.