diff --git a/scripts/build_spec.py b/scripts/build_spec.py index 4213c38b6..1091e7bed 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -19,6 +19,13 @@ PHASE0_IMPORTS = '''from typing import ( Tuple, ) +from dataclasses import ( + dataclass, + field, +) + +from copy import deepcopy + from eth2spec.utils.ssz.ssz_impl import ( hash_tree_root, signing_root, @@ -44,6 +51,13 @@ PHASE1_IMPORTS = '''from typing import ( Tuple, ) +from dataclasses import ( + dataclass, + field, +) + +from copy import deepcopy + from eth2spec.utils.ssz.ssz_impl import ( hash_tree_root, signing_root, diff --git a/scripts/function_puller.py b/scripts/function_puller.py index 303d4ec2f..776fcfa82 100644 --- a/scripts/function_puller.py +++ b/scripts/function_puller.py @@ -29,6 +29,7 @@ def get_spec(file_name: str) -> SpecObject: inserts = {} function_matcher = re.compile(FUNCTION_REGEX) inserts_matcher = re.compile(BEGIN_INSERT_REGEX) + is_ssz = False for linenum, line in enumerate(open(file_name).readlines()): line = line.rstrip() if pulling_from is None and len(line) > 0 and line[0] == '#' and line[-1] == '`': diff --git a/specs/core/0_fork-choice.md b/specs/core/0_fork-choice.md index f3d57feaa..1a1465d25 100644 --- a/specs/core/0_fork-choice.md +++ b/specs/core/0_fork-choice.md @@ -58,7 +58,8 @@ The head block root associated with a `store` is defined as `get_head(store)`. A #### `Target` ```python -class Target(Container): +@dataclass +class Target(object): epoch: Epoch root: Bytes32 ``` @@ -66,13 +67,14 @@ class Target(Container): #### `Store` ```python -class Store(Container): - blocks: Dict[Bytes32, BeaconBlock] - states: Dict[Bytes32, BeaconState] - time: uint64 - latest_targets: Dict[ValidatorIndex, Target] - justified_root: Bytes32 - finalized_root: Bytes32 +@dataclass +class Store(object): + blocks: Dict[Bytes32, BeaconBlock] = field(default_factory=dict) + states: Dict[Bytes32, BeaconState] = field(default_factory=dict) + time: int = 0 + latest_targets: Dict[ValidatorIndex, Target] = field(default_factory=dict) + justified_root: Bytes32 = ZERO_HASH + finalized_root: Bytes32 = ZERO_HASH ``` ### Helpers @@ -139,7 +141,7 @@ def on_block(store: Store, block: BeaconBlock) -> None: # Check block is a descendant of the finalized block assert get_ancestor(store, signing_root(block), store.blocks[store.finalized_root].slot) == store.finalized_root # Check block slot against Unix time - pre_state = store.states[block.parent_root].copy() + pre_state = deepcopy(store.states[block.parent_root]) assert store.time >= pre_state.genesis_time + block.slot * SECONDS_PER_SLOT # Check the block is valid and compute the post-state state = state_transition(pre_state, block) diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 3bd7dd062..d4b481ef2 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -1,11 +1,9 @@ -import copy from typing import ( List, Iterable, TypeVar, Type, NewType, - Dict, Union, ) from typing_inspect import get_origin @@ -132,9 +130,6 @@ class Container(object): cls = self.__class__ return [getattr(self, field) for field in cls.get_field_names()] - def copy(self): - return copy.deepcopy(self) - def __repr__(self): return repr({field: getattr(self, field) for field in self.get_field_names()}) @@ -418,8 +413,6 @@ def get_zero_value(typ): return b'' elif is_container_type(typ): return typ(**{f: get_zero_value(t) for f, t in typ.get_fields()}) - elif is_dict_type(typ): - return dict() else: raise Exception("Type not supported: {}".format(typ)) @@ -511,13 +504,6 @@ def is_container_type(typ): return isinstance(typ, type) and issubclass(typ, Container) -def is_dict_type(typ): - """ - Check of the given type is a Dict. (Which are a part of the super-secret undocumented SSZ spec) - """ - return get_origin(typ) is Dict or get_origin(typ) is dict - - T = TypeVar('T') L = TypeVar('L') diff --git a/test_libs/pyspec/requirements.txt b/test_libs/pyspec/requirements.txt index 3b38930bd..18e5d2a4c 100644 --- a/test_libs/pyspec/requirements.txt +++ b/test_libs/pyspec/requirements.txt @@ -3,3 +3,4 @@ eth-typing>=2.1.0,<3.0.0 pycryptodome==3.7.3 py_ecc>=1.6.0 typing_inspect==0.4.0 +dataclasses==0.6