Skip to content

Commit fdc6c14

Browse files
committed
After Justin's spec clean-up
1 parent 27b9f7d commit fdc6c14

File tree

6 files changed

+28
-21
lines changed

6 files changed

+28
-21
lines changed

eth/beacon/enums/validator_status_codes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ class ValidatorStatusCode(IntEnum):
55
PENDING_ACTIVATION = 0
66
ACTIVE = 1
77
PENDING_EXIT = 2
8-
PENDING_WITHDRAW = 3
9-
WITHDRAWN = 4
10-
PENALIZED = 127
8+
EXITED_WITHOUT_PENALTY = 3
9+
EXITED_WITH_PENALTY = 4

eth/beacon/helpers.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,12 @@ def get_attestation_indices(crystallized_state: 'CrystallizedState',
217217
@to_tuple
218218
def get_active_validator_indices(validators: Sequence['ValidatorRecord']) -> Iterable[int]:
219219
"""
220-
Return the active validators.
220+
Gets indices of active validators from ``validators``.
221221
"""
222-
o = []
223-
for index, validator in enumerate(validators):
224-
if (validator.status == ValidatorStatusCode.ACTIVE):
225-
o.append(index)
226-
return o
222+
return [
223+
i for i, v in enumerate(validators)
224+
if v.status in [ValidatorStatusCode.ACTIVE, ValidatorStatusCode.PENDING_EXIT]
225+
]
227226

228227

229228
#
@@ -235,7 +234,6 @@ def _get_shards_and_committees_for_shard_indices(
235234
start_shard: int,
236235
shard_count: int) -> Iterable[ShardAndCommittee]:
237236
"""
238-
FIXME
239237
Returns filled [ShardAndCommittee] tuple.
240238
"""
241239
for index, indices in enumerate(shard_indices):
@@ -255,7 +253,7 @@ def get_new_shuffling(*,
255253
shard_count: int) -> Iterable[Iterable[ShardAndCommittee]]:
256254
"""
257255
Return shuffled ``shard_and_committee_for_slots`` (``[[ShardAndCommittee]]``) of
258-
the given active ``validators``.
256+
the given active ``validators`` using ``seed`` as entropy.
259257
260258
Two-dimensional:
261259
The first layer is ``slot`` number
@@ -302,9 +300,10 @@ def get_new_shuffling(*,
302300
shard_count // cycle_length,
303301
active_validators_size // cycle_length // target_committee_size,
304302
)
303+
# Shuffle with seed
305304
shuffled_active_validator_indices = shuffle(active_validators, seed)
306305

307-
# Split the shuffled list into cycle_length pieces
306+
# Split the shuffled list into epoch_length pieces
308307
validators_per_slot = split(shuffled_active_validator_indices, cycle_length)
309308
for index, slot_indices in enumerate(validators_per_slot):
310309
# Split the shuffled list into committees_per_slot pieces

eth/beacon/utils/random.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def shuffle(values: Sequence[Any],
3636
"""
3737
values_count = len(values)
3838

39+
# The range of the RNG places an upper-bound on the size of the list that
40+
# may be shuffled. It is a logic error to supply an oversized list.
3941
if values_count >= RAND_MAX:
4042
raise ValueError(
4143
"values_count (%s) should less than RAND_MAX (%s)." %
@@ -84,13 +86,13 @@ def shuffle(values: Sequence[Any],
8486

8587

8688
@to_tuple
87-
def split(seq: Sequence[TItem], split_count: int) -> Iterable[Any]:
89+
def split(values: Sequence[TItem], split_count: int) -> Iterable[Any]:
8890
"""
89-
Returns the split ``seq`` in ``split_count`` pieces in protocol.
91+
Returns the split ``values`` in ``split_count`` pieces in protocol.
9092
Spec: https://github.com/ethereum/eth2.0-specs/blob/70cef14a08de70e7bd0455d75cf380eb69694bfb/specs/core/0_beacon-chain.md#helper-functions # noqa: E501
9193
"""
92-
list_length = len(seq)
94+
list_length = len(values)
9395
return [
94-
seq[(list_length * i // split_count): (list_length * (i + 1) // split_count)]
96+
values[(list_length * i // split_count): (list_length * (i + 1) // split_count)]
9597
for i in range(split_count)
9698
]

eth/utils/numeric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def clamp(inclusive_lower_bound: int,
101101
return value
102102

103103

104-
def int_sqrt(value: int) -> int:
104+
def integer_squareroot(value: int) -> int:
105105
"""
106106
Return the largest integer ``x`` such that ``x**2 <= value``.
107107
Ref: https://en.wikipedia.org/wiki/Integer_square_root

tests/beacon/test_helpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,11 @@ def test_get_active_validator_indices(sample_validator_record_params):
416416
status=ValidatorStatusCode.PENDING_EXIT,
417417
)
418418
active_validator_indices = get_active_validator_indices(validators)
419+
assert len(active_validator_indices) == 3
420+
421+
# Make one validator becomes PENDING_EXIT.
422+
validators[0] = validators[0].copy(
423+
status=ValidatorStatusCode.EXITED_WITHOUT_PENALTY,
424+
)
425+
active_validator_indices = get_active_validator_indices(validators)
419426
assert len(active_validator_indices) == 2

tests/core/numeric-utils/test_int_sqrt.py renamed to tests/core/numeric-utils/test_integer_squareroot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from eth.utils.numeric import (
4-
int_sqrt,
4+
integer_squareroot,
55
)
66

77

@@ -21,10 +21,10 @@
2121
(-1, ValueError()),
2222
)
2323
)
24-
def test_int_sqrt(value, expected):
24+
def test_integer_squareroot(value, expected):
2525
if isinstance(expected, Exception):
2626
with pytest.raises(ValueError):
27-
int_sqrt(value)
27+
integer_squareroot(value)
2828
else:
29-
actual = int_sqrt(value)
29+
actual = integer_squareroot(value)
3030
assert actual == expected

0 commit comments

Comments
 (0)