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

@ -55,7 +55,6 @@ def merger(oldfile:str, newfile:str) -> str:
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:

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]