Simplify minimal SSZ again

This commit is contained in:
Carl Beekhuizen 2019-05-16 21:01:32 +02:00
parent 9f2d410a17
commit 306a613c8a
No known key found for this signature in database
GPG Key ID: D05CA176D0020646
2 changed files with 13 additions and 28 deletions

View File

@ -27,7 +27,7 @@ def inserter(oldfile: str, newfile: str) -> Tuple[str, str]:
label = re.match(r"@[a-zA-Z0-9_]*\n", labeled_text) label = re.match(r"@[a-zA-Z0-9_]*\n", labeled_text)
if label is not None: if label is not None:
label = label.group(0) label = label.group(0)
labeled_text = re.sub(label, '', labeled_text) labeled_text = re.sub(label, '', labeled_text)
return {'label': label, 'text': labeled_text} return {'label': label, 'text': labeled_text}
new_insert_objects = map(get_labeled_object, new_insert_objects) new_insert_objects = map(get_labeled_object, new_insert_objects)
# Find and replace labels # Find and replace labels
@ -53,9 +53,8 @@ def merger(oldfile:str, newfile:str) -> str:
old_object_tuples = list(map(lambda x: [re.match(object_regex, x).group(0),x], old_objects)) old_object_tuples = list(map(lambda x: [re.match(object_regex, x).group(0),x], old_objects))
for new_item in new_objects: for new_item in new_objects:
found_old = False found_old = False
for old_item in old_object_tuples: for old_item in old_object_tuples:
if old_item[0] == re.match(object_regex, new_item).group(0): if old_item[0] == re.match(object_regex, new_item).group(0):
print(old_item[0])
old_item[1] = new_item old_item[1] = new_item
found_old = True found_old = True
break break
@ -67,7 +66,6 @@ def merger(oldfile:str, newfile:str) -> str:
def build_phase0_spec(sourcefile, outfile=None): def build_phase0_spec(sourcefile, outfile=None):
code_lines = [] code_lines = []
code_lines.append(""" code_lines.append("""
from typing import ( from typing import (
Any, Any,
Dict, Dict,
@ -75,16 +73,19 @@ from typing import (
NewType, NewType,
Tuple, Tuple,
) )
from eth2spec.utils.minimal_ssz import ( from eth2spec.utils.minimal_ssz import (
SSZType, SSZType,
hash_tree_root, hash_tree_root,
signing_root, signing_root,
) )
from eth2spec.utils.bls_stub import ( from eth2spec.utils.bls_stub import (
bls_aggregate_pubkeys, bls_aggregate_pubkeys,
bls_verify, bls_verify,
bls_verify_multiple, bls_verify_multiple,
) )
from eth2spec.utils.hash_function import hash from eth2spec.utils.hash_function import hash
@ -146,6 +147,7 @@ def apply_constants_preset(preset: Dict[str, Any]):
# Initialize SSZ types again, to account for changed lengths # Initialize SSZ types again, to account for changed lengths
init_SSZ_types() init_SSZ_types()
""") """)
if outfile is not None: if outfile is not None:
@ -160,7 +162,7 @@ def build_phase1_spec(phase0_sourcefile, phase1_sourcefile, outfile=None):
phase1_code = build_phase0_spec(phase1_sourcefile) phase1_code = build_phase0_spec(phase1_sourcefile)
phase0_code, phase1_code = inserter(phase0_code, phase1_code) phase0_code, phase1_code = inserter(phase0_code, phase1_code)
phase1_code = merger(phase0_code, phase1_code) phase1_code = merger(phase0_code, phase1_code)
if outfile is not None: if outfile is not None:
with open(outfile, 'w') as out: with open(outfile, 'w') as out:
out.write(phase1_code) out.write(phase1_code)
@ -172,12 +174,12 @@ if __name__ == '__main__':
description = ''' description = '''
Build the specs from the md docs. Build the specs from the md docs.
If building phase 0: If building phase 0:
1st argument is input spec.md 1st argument is input spec.md
2nd argument is output spec.py 2nd argument is output spec.py
If building phase 1: If building phase 1:
1st argument is input spec_phase0.md 1st argument is input spec_phase0.md
2nd argument is input spec_phase1.md 2nd argument is input spec_phase1.md
3rd argument is output spec.py 3rd argument is output spec.py
''' '''
parser = ArgumentParser(description=description) parser = ArgumentParser(description=description)

View File

@ -9,12 +9,8 @@ ZERO_CHUNK = b'\x00' * BYTES_PER_CHUNK
cached_typedefs = {} cached_typedefs = {}
def SSZType(name, fields): def SSZType(fields):
class SSZObject(): class SSZObject():
if name is not None:
__name__ = name
__qualname__ = name
def __init__(self, **kwargs): def __init__(self, **kwargs):
for f, t in fields.items(): for f, t in fields.items():
if f not in kwargs: if f not in kwargs:
@ -31,12 +27,9 @@ def SSZType(name, fields):
def __str__(self): def __str__(self):
output = [] output = []
for field in self.fields: for field in self.fields:
output.append(f'{field}: {repr(getattr(self, field))},') output.append(f'{field}: {getattr(self, field)}')
return "\n".join(output) return "\n".join(output)
def __repr__(self):
return name + "(**{\n " + str(self).replace("\n", "\n ") + "\n})"
def serialize(self): def serialize(self):
return serialize_value(self, self.__class__) return serialize_value(self, self.__class__)
@ -44,19 +37,9 @@ def SSZType(name, fields):
return hash_tree_root(self, self.__class__) return hash_tree_root(self, self.__class__)
SSZObject.fields = fields SSZObject.fields = fields
if name is not None:
cached_typedefs[name] = SSZObject
return SSZObject return SSZObject
def SSZTypeExtension(original_type, new_fields):
typedef = cached_typedefs[original_type]
typedef.fields.update(new_fields)
return typedef
class Vector(): class Vector():
def __init__(self, items): def __init__(self, items):
self.items = items self.items = items
@ -334,7 +317,7 @@ def truncate(container):
key: container.fields[key] key: container.fields[key]
for key in field_keys[:-1] for key in field_keys[:-1]
} }
truncated_class = SSZType(None, truncated_fields) truncated_class = SSZType(truncated_fields)
kwargs = { kwargs = {
field: getattr(container, field) field: getattr(container, field)
for field in field_keys[:-1] for field in field_keys[:-1]