Cleanups in get_active_validator_indices and shuffle

This commit is contained in:
Justin 2018-10-03 14:45:33 +01:00 committed by GitHub
parent 66e316dcb3
commit f271d8b358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 8 deletions

View File

@ -290,29 +290,25 @@ 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 assert len(lst) <= MAX_VALIDATOR_COUNT
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 = MAX_VALIDATOR_COUNT - MAX_VALIDATOR_COUNT % 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]