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
def get_active_validator_indices(validators):
o = []
for i in range(len(validators)):
if validators[i].status == LOGGED_IN:
o.append(i)
return o
return [i for i, v in enumerate(validators) if v.status == LOGGED_IN]
```
Now, a function that shuffles this list:
```python
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]
source = seed
i = 0
while i < len(lst):
source = blake(source)
source = hash(source)
for pos in range(0, 30, 3):
m = int.from_bytes(source[pos:pos+3], 'big')
remaining = len(lst) - i
if remaining == 0:
break
rand_max = 16777216 - 16777216 % remaining
rand_max = rand_max - rand_max % remaining
if m < rand_max:
replacement_pos = (m % remaining) + i
o[i], o[replacement_pos] = o[replacement_pos], o[i]