Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyspec-SSZ: lists-rework (enable static generalized indices) + fully python class based now. #1180

Merged
merged 75 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
5ddfe34
Simplified SSZ impl
protolambda Jun 20, 2019
7c42324
Added get_container_type to get_zero_value
vbuterin Jun 14, 2019
8919f62
Update test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py
vbuterin Jun 14, 2019
d1ecfd5
typing improvements
protolambda Jun 20, 2019
b6cf809
more improvements, and implement new space-efficient merkleization wi…
protolambda Jun 20, 2019
cd5f59e
fix bytes value check, fix default-type checking
protolambda Jun 20, 2019
54a1fa9
Update test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py
protolambda Jun 15, 2019
3a9b1fb
Update test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py
protolambda Jun 15, 2019
82e7392
default method for container is recognized now
protolambda Jun 15, 2019
108410d
Change byte to explict class instead of newtype
protolambda Jun 15, 2019
4ebdcea
highly experimental typing
protolambda Jun 20, 2019
97025c5
start updating virtual sizes of lists
protolambda Jun 20, 2019
08e6f32
typing improvements, type testing
protolambda Jun 16, 2019
8bd2e87
bugfixes and typing improvements
protolambda Jun 16, 2019
0a43003
minor test improvements
protolambda Jun 17, 2019
b89183a
Update spec for new SSZ with list max length
protolambda Jun 20, 2019
4c2adcc
Update 0_beacon-chain.md
vbuterin Jun 17, 2019
73ba419
check virtual lengths, fix imports
protolambda Jun 18, 2019
8c6ddd5
container field coercion
protolambda Jun 18, 2019
4aefc07
list-rework type fixes
protolambda Jun 18, 2019
439e4d4
Build spec
protolambda Jun 20, 2019
c9747b6
improve build spec, get clean dependencies list
protolambda Jun 20, 2019
8344d50
update beacon chain doc, use new types, avoid List
protolambda Jun 20, 2019
4b4bf87
update shard doc, use new types, avoid List
protolambda Jun 18, 2019
5be0c57
fix linting + mypy
protolambda Jun 18, 2019
6f46c1d
fix typing in spec builder monkey patch
protolambda Jun 18, 2019
6b82e3f
Modifications from Vitalik, to enable SSZ Partials
protolambda Jun 20, 2019
5048b9e
temporary fix for phase-1 spec typing
protolambda Jun 19, 2019
a33c678
update ssz testing/debug utils
protolambda Jun 19, 2019
7cdec74
fix field iteration crash in ssz typing
protolambda Jun 19, 2019
4e747fb
fixes for class based ssz typing
protolambda Jun 20, 2019
977856b
ssz typing now subclasses list/bytes, much easier to work with than w…
protolambda Jun 20, 2019
82240d8
fix vector default type
protolambda Jun 20, 2019
f157745
resolve some remaining list-rework rebase details
protolambda Jun 20, 2019
224c98a
last() method, no negative index lookups
protolambda Jun 20, 2019
8c6d2b4
update ssz-pyssz decoder for fuzzing
protolambda Jun 20, 2019
8bd2048
improve type coercion; coerce between equal-length uint subclasses
protolambda Jun 20, 2019
b4ef672
deal with deepcopy modifying vector length from 0 to full length duri…
protolambda Jun 20, 2019
2d67717
fix linting issues + make spec builder remove comments in container r…
protolambda Jun 20, 2019
4dcfee2
remove unused spec-helper from spec builder
protolambda Jun 20, 2019
d8f470b
enable slicing of SSZ lists/vectors
protolambda Jun 20, 2019
6338c5b
fix custody bug, needs review from Carl
protolambda Jun 20, 2019
f27c44b
fix deposit negative index fail
protolambda Jun 20, 2019
c203724
comment out old deposit test, re-introduced soon maybe, cc Justin
protolambda Jun 20, 2019
3d8466f
make Bit check not use "is", and remove duplicate line
protolambda Jun 20, 2019
6648b3c
remove old deposits test, there is no deposit index in deposit data a…
protolambda Jun 20, 2019
e99c864
Deltas = NewType('Deltas', TypingList[Gwei])
hwwhww Jun 20, 2019
b7b2fee
uint add/sub type checking, fixes #1029
protolambda Jun 21, 2019
80c40f5
merge dev, resolve minor merge conflicts
protolambda Jun 21, 2019
d1fa3ac
remove unused dependency
protolambda Jun 21, 2019
d463ada
Merge branch 'dev' into list-rework
djrtwo Jun 21, 2019
c09e45c
fix rule_4 underflow and split out genesis finality test
djrtwo Jun 21, 2019
8f99741
remove commented old code
djrtwo Jun 21, 2019
0b0e9a5
Merge branch 'dev' into list-rework
protolambda Jun 21, 2019
16093eb
Merge branch 'dev' into list-rework
protolambda Jun 22, 2019
00aae07
type annotation clean up
protolambda Jun 22, 2019
f95e731
fix get_active_validator_indices typing usage
protolambda Jun 22, 2019
dd5ad2e
remove unnecessary (and now outdated) type hints, update List encodin…
protolambda Jun 22, 2019
e873bbd
support list casting
protolambda Jun 22, 2019
47034a6
fix imports in helper test file
protolambda Jun 22, 2019
0249cf6
fix lint, and update encoder to handle the few imported types well
protolambda Jun 22, 2019
9befe09
test merkleize chunks
protolambda Jun 22, 2019
da858f1
fix int encoding, fix list randomization size limit.
protolambda Jun 22, 2019
a5a2e29
remove unnecessary argument, typing is based on values fully now
protolambda Jun 22, 2019
1408a1e
undo tuple wrapping
protolambda Jun 22, 2019
1972cca
Merge branch 'dev' into list-rework
djrtwo Jun 24, 2019
82ae180
clean up list limit constants
protolambda Jun 24, 2019
c73417b
deserialize-basic detail, make subclass
protolambda Jun 24, 2019
5989e5c
use Bool as base name, make Bit an alias
protolambda Jun 24, 2019
8b88c3f
Merge remote-tracking branch 'origin' into list-rework
protolambda Jun 24, 2019
81a2c84
Merge branch 'list-rework' of https://github.com/ethereum/eth2.0-spec…
protolambda Jun 24, 2019
9fb5806
be explicit about input for balance sum
protolambda Jun 24, 2019
a5b7564
hash-tree-root tests
protolambda Jun 25, 2019
45dbf5a
Remove old Deltas reference
protolambda Jun 25, 2019
054a157
get rid of TypingList, add MutableSequence
protolambda Jun 25, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions configs/constant_presets/mainnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ MIN_EPOCHS_TO_INACTIVITY_PENALTY: 4
EPOCHS_PER_HISTORICAL_VECTOR: 65536
# 2**13 (= 8,192) epochs ~36 days
EPOCHS_PER_SLASHED_BALANCES_VECTOR: 8192
# 2**24 (= 16,777,216) historical roots, ~26,131 years
HISTORICAL_ROOTS_LIMIT: 16777216
# 2**40 (= 1,099,511,627,776) validator spots
VALIDATOR_REGISTRY_LIMIT: 1099511627776


# Reward and penalty quotients
Expand Down
4 changes: 4 additions & 0 deletions configs/constant_presets/minimal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS: 4096
EPOCHS_PER_HISTORICAL_VECTOR: 64
# [customized] smaller state
EPOCHS_PER_SLASHED_BALANCES_VECTOR: 64
# 2**24 (= 16,777,216) historical roots
HISTORICAL_ROOTS_LIMIT: 16777216
# 2**40 (= 1,099,511,627,776) validator spots
VALIDATOR_REGISTRY_LIMIT: 1099511627776


# Reward and penalty quotients
Expand Down
91 changes: 50 additions & 41 deletions scripts/build_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@


PHASE0_IMPORTS = '''from typing import (
Any,
Callable,
Dict,
List,
Set,
Tuple,
Any, Callable, Dict, Set, Sequence, Tuple,
)

from dataclasses import (
Expand All @@ -30,27 +25,19 @@
signing_root,
)
from eth2spec.utils.ssz.ssz_typing import (
# unused: uint8, uint16, uint32, uint128, uint256,
uint64, Container, Vector,
Bit, Bool, Container, List, Vector, Bytes, uint64,
Bytes4, Bytes32, Bytes48, Bytes96,
)
from eth2spec.utils.bls import (
bls_aggregate_pubkeys,
bls_verify,
bls_verify_multiple,
)
# Note: 'int' type defaults to being interpreted as a uint64 by SSZ implementation.

from eth2spec.utils.hash_function import hash
'''
PHASE1_IMPORTS = '''from typing import (
Any,
Callable,
Dict,
List,
Optional,
Set,
Tuple,
Any, Callable, Dict, Optional, Set, Sequence, MutableSequence, Tuple,
)

from dataclasses import (
Expand All @@ -65,8 +52,7 @@
is_empty,
)
from eth2spec.utils.ssz.ssz_typing import (
# unused: uint8, uint16, uint32, uint128, uint256,
uint64, Container, Vector,
Bit, Bool, Container, List, Vector, Bytes, uint64,
Bytes4, Bytes32, Bytes48, Bytes96,
)
from eth2spec.utils.bls import (
Expand All @@ -77,39 +63,34 @@

from eth2spec.utils.hash_function import hash
'''
BYTE_TYPES = [4, 32, 48, 96]
SUNDRY_FUNCTIONS = '''
def get_ssz_type_by_name(name: str) -> Container:
return globals()[name]
# Monkey patch hash cache
_hash = hash
hash_cache: Dict[bytes, Hash] = {}


def hash(x: bytes) -> Hash:
if x not in hash_cache:
hash_cache[x] = Hash(_hash(x))
return hash_cache[x]


# Monkey patch validator compute committee code
_compute_committee = compute_committee
committee_cache: Dict[Tuple[Hash, Hash, int, int], List[ValidatorIndex]] = {}
committee_cache: Dict[Tuple[Hash, Hash, int, int], Sequence[ValidatorIndex]] = {}


def compute_committee(indices: List[ValidatorIndex], # type: ignore
def compute_committee(indices: Sequence[ValidatorIndex], # type: ignore
seed: Hash,
index: int,
count: int) -> List[ValidatorIndex]:
param_hash = (hash_tree_root(indices), seed, index, count)
count: int) -> Sequence[ValidatorIndex]:
param_hash = (hash(b''.join(index.to_bytes(length=4, byteorder='little') for index in indices)), seed, index, count)

if param_hash not in committee_cache:
committee_cache[param_hash] = _compute_committee(indices, seed, index, count)
return committee_cache[param_hash]


# Monkey patch hash cache
_hash = hash
hash_cache: Dict[bytes, Hash] = {}


def hash(x: bytes) -> Hash:
if x not in hash_cache:
hash_cache[x] = Hash(_hash(x))
return hash_cache[x]


# Access to overwrite spec constants based on configuration
def apply_constants_preset(preset: Dict[str, Any]) -> None:
global_vars = globals()
Expand All @@ -124,6 +105,18 @@ def apply_constants_preset(preset: Dict[str, Any]) -> None:
'''


def strip_comments(raw: str) -> str:
comment_line_regex = re.compile('^\s+# ')
lines = raw.split('\n')
out = []
for line in lines:
if not comment_line_regex.match(line):
if ' #' in line:
line = line[:line.index(' #')]
out.append(line)
return '\n'.join(out)


def objects_to_spec(functions: Dict[str, str],
custom_types: Dict[str, str],
constants: Dict[str, str],
Expand Down Expand Up @@ -151,7 +144,8 @@ def objects_to_spec(functions: Dict[str, str],
ssz_objects_instantiation_spec = '\n\n'.join(ssz_objects.values())
ssz_objects_reinitialization_spec = (
'def init_SSZ_types() -> None:\n global_vars = globals()\n\n '
+ '\n\n '.join([re.sub(r'(?!\n\n)\n', r'\n ', value[:-1]) for value in ssz_objects.values()])
+ '\n\n '.join([strip_comments(re.sub(r'(?!\n\n)\n', r'\n ', value[:-1]))
for value in ssz_objects.values()])
+ '\n\n'
+ '\n'.join(map(lambda x: ' global_vars[\'%s\'] = %s' % (x, x), ssz_objects.keys()))
)
Expand Down Expand Up @@ -183,17 +177,32 @@ def combine_constants(old_constants: Dict[str, str], new_constants: Dict[str, st
return old_constants


ignored_dependencies = [
'Bit', 'Bool', 'Vector', 'List', 'Container', 'Hash', 'BLSPubkey', 'BLSSignature', 'Bytes', 'BytesN'
'Bytes4', 'Bytes32', 'Bytes48', 'Bytes96',
'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256',
'bytes' # to be removed after updating spec doc
]


def dependency_order_ssz_objects(objects: Dict[str, str], custom_types: Dict[str, str]) -> None:
"""
Determines which SSZ Object is depenedent on which other and orders them appropriately
"""
items = list(objects.items())
for key, value in items:
dependencies = re.findall(r'(: [A-Z][\w[]*)', value)
dependencies = map(lambda x: re.sub(r'\W|Vector|List|Container|Hash|BLSPubkey|BLSSignature|uint\d+|Bytes\d+|bytes', '', x), dependencies)
dependencies = []
for line in value.split('\n'):
if not re.match(r'\s+\w+: .+', line):
continue # skip whitespace etc.
line = line[line.index(':') + 1:] # strip of field name
if '#' in line:
line = line[:line.index('#')] # strip of comment
dependencies.extend(re.findall(r'(\w+)', line)) # catch all legible words, potential dependencies
dependencies = filter(lambda x: '_' not in x and x.upper() != x, dependencies) # filter out constants
dependencies = filter(lambda x: x not in ignored_dependencies, dependencies)
dependencies = filter(lambda x: x not in custom_types, dependencies)
for dep in dependencies:
if dep in custom_types or len(dep) == 0:
continue
key_list = list(objects.keys())
for item in [dep, key] + key_list[key_list.index(dep)+1:]:
objects[item] = objects.pop(item)
Expand Down
Loading