Merge pull request #30 from ethereum/JustinDrake-patch-1

Cleanups in get_active_validator_indices and shuffle
This commit is contained in:
Danny Ryan 2018-10-03 09:31:56 -05:00 committed by GitHub
commit 0941d592de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 8 deletions

View File

@ -282,29 +282,29 @@ We start off by defining some helper algorithms. First, the function that select
```python ```python
def get_active_validator_indices(validators): def get_active_validator_indices(validators):
o = [] return [i for i, v in enumerate(validators) if v.status == LOGGED_IN]
for i in range(len(validators)):
if validators[i].status == LOGGED_IN:
o.append(i)
return o
``` ```
Now, a function that shuffles this list: Now, a function that shuffles this list:
```python ```python
def shuffle(lst, seed): def shuffle(lst, seed):
assert len(lst) <= 16777216 # entropy is consumed in 3 byte chunks
# rand_max is defined to remove the modulo bias from this entropy source
rand_max = 2**24
assert len(lst) <= rand_max
o = [x for x in lst] o = [x for x in lst]
source = seed source = seed
i = 0 i = 0
while i < len(lst): while i < len(lst):
source = blake(source) source = hash(source)
for pos in range(0, 30, 3): for pos in range(0, 30, 3):
m = int.from_bytes(source[pos:pos+3], 'big') m = int.from_bytes(source[pos:pos+3], 'big')
remaining = len(lst) - i remaining = len(lst) - i
if remaining == 0: if remaining == 0:
break break
rand_max = 16777216 - 16777216 % remaining rand_max = rand_max - rand_max % remaining
if m < rand_max: if m < rand_max:
replacement_pos = (m % remaining) + i replacement_pos = (m % remaining) + i
o[i], o[replacement_pos] = o[replacement_pos], o[i] o[i], o[replacement_pos] = o[replacement_pos], o[i]