Moves fork-choice objects away from SSZ
This commit is contained in:
parent
20d8156f46
commit
2ea6cede3e
|
@ -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,
|
||||
|
|
|
@ -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] == '`':
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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')
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue