2018-11-23 22:44:43 +00:00
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# Helpers and functions pertaining to managing the validator set
|
|
|
|
|
2018-11-23 19:42:47 +00:00
|
|
|
import
|
2018-12-04 18:45:30 +00:00
|
|
|
options, nimcrypto, sequtils, math,
|
2018-11-27 23:10:09 +00:00
|
|
|
eth_common,
|
2018-12-03 21:41:24 +00:00
|
|
|
../ssz,
|
2018-11-28 19:49:03 +00:00
|
|
|
./crypto, ./datatypes, ./digest, ./helpers
|
2018-11-23 19:42:47 +00:00
|
|
|
|
2018-12-27 20:14:37 +00:00
|
|
|
func min_empty_validator_index*(
|
|
|
|
validators: seq[ValidatorRecord],
|
|
|
|
validator_balances: seq[uint64],
|
|
|
|
current_slot: uint64): Option[int] =
|
2018-11-23 19:42:47 +00:00
|
|
|
for i, v in validators:
|
2018-12-27 20:14:37 +00:00
|
|
|
if validator_balances[i] == 0 and
|
2018-12-06 02:07:04 +00:00
|
|
|
v.latest_status_change_slot +
|
|
|
|
ZERO_BALANCE_VALIDATOR_TTL.uint64 <= current_slot:
|
2018-12-03 21:41:24 +00:00
|
|
|
return some(i)
|
2018-11-23 19:42:47 +00:00
|
|
|
|
2018-11-29 05:23:40 +00:00
|
|
|
func get_active_validator_indices*(validators: openArray[ValidatorRecord]): seq[Uint24] =
|
2018-11-23 22:44:43 +00:00
|
|
|
## Select the active validators
|
|
|
|
for idx, val in validators:
|
2018-12-11 17:55:45 +00:00
|
|
|
if is_active_validator(val):
|
2018-11-23 22:44:43 +00:00
|
|
|
result.add idx.Uint24
|
|
|
|
|
2019-01-16 10:21:06 +00:00
|
|
|
func get_shuffling*(seed: Eth2Digest,
|
|
|
|
validators: openArray[ValidatorRecord],
|
|
|
|
crosslinking_start_shard: uint64
|
|
|
|
): array[EPOCH_LENGTH, seq[ShardCommittee]] =
|
2018-11-23 22:44:43 +00:00
|
|
|
## Split up validators into groups at the start of every epoch,
|
|
|
|
## determining at what height they can make attestations and what shard they are making crosslinks for
|
|
|
|
## Implementation should do the following: http://vitalik.ca/files/ShuffleAndAssign.png
|
|
|
|
|
|
|
|
let
|
2018-12-06 02:07:04 +00:00
|
|
|
active_validator_indices = get_active_validator_indices(validators)
|
2018-11-23 22:44:43 +00:00
|
|
|
committees_per_slot = clamp(
|
2018-12-06 02:07:04 +00:00
|
|
|
len(active_validator_indices) div EPOCH_LENGTH div TARGET_COMMITTEE_SIZE,
|
2018-12-04 18:45:30 +00:00
|
|
|
1, SHARD_COUNT div EPOCH_LENGTH).uint64
|
2018-11-23 22:44:43 +00:00
|
|
|
# Shuffle with seed
|
2018-12-06 02:07:04 +00:00
|
|
|
shuffled_active_validator_indices = shuffle(active_validator_indices, seed)
|
2018-11-23 22:44:43 +00:00
|
|
|
# Split the shuffled list into cycle_length pieces
|
2018-12-03 17:46:22 +00:00
|
|
|
validators_per_slot = split(shuffled_active_validator_indices, EPOCH_LENGTH)
|
2018-11-23 22:44:43 +00:00
|
|
|
|
2018-12-03 17:46:22 +00:00
|
|
|
assert validators_per_slot.len() == EPOCH_LENGTH # what split should do..
|
2018-11-23 22:44:43 +00:00
|
|
|
|
|
|
|
for slot, slot_indices in validators_per_slot:
|
|
|
|
let
|
|
|
|
shard_indices = split(slot_indices, committees_per_slot)
|
2018-12-04 18:45:30 +00:00
|
|
|
shard_id_start =
|
|
|
|
crosslinking_start_shard + slot.uint64 * committees_per_slot
|
2018-11-23 22:44:43 +00:00
|
|
|
|
2018-12-06 02:07:04 +00:00
|
|
|
var committees = newSeq[ShardCommittee](shard_indices.len)
|
2018-11-23 22:44:43 +00:00
|
|
|
for shard_position, indices in shard_indices:
|
2018-11-29 05:23:40 +00:00
|
|
|
committees[shard_position].shard =
|
2018-12-11 17:55:45 +00:00
|
|
|
shard_id_start + shard_position.uint64 mod SHARD_COUNT.uint64
|
2018-11-23 22:44:43 +00:00
|
|
|
committees[shard_position].committee = indices
|
2018-12-06 02:07:04 +00:00
|
|
|
committees[shard_position].total_validator_count =
|
|
|
|
len(active_validator_indices).uint64
|
2018-11-23 22:44:43 +00:00
|
|
|
|
|
|
|
result[slot] = committees
|
2018-12-03 21:41:24 +00:00
|
|
|
|
2018-12-13 16:00:55 +00:00
|
|
|
func get_new_validator_registry_delta_chain_tip*(
|
2018-12-03 21:41:24 +00:00
|
|
|
current_validator_registry_delta_chain_tip: Eth2Digest,
|
|
|
|
index: Uint24,
|
|
|
|
pubkey: ValidatorPubKey,
|
|
|
|
flag: ValidatorSetDeltaFlags): Eth2Digest =
|
|
|
|
## Compute the next hash in the validator registry delta hash chain.
|
|
|
|
|
2018-12-21 23:47:55 +00:00
|
|
|
hash_tree_root_final(ValidatorRegistryDeltaBlock(
|
2018-12-19 04:36:10 +00:00
|
|
|
latest_registry_delta_root: current_validator_registry_delta_chain_tip,
|
|
|
|
validator_index: index,
|
|
|
|
pubkey: pubkey,
|
|
|
|
flag: flag
|
2018-12-21 23:47:55 +00:00
|
|
|
))
|