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*(
|
2019-01-17 18:27:11 +00:00
|
|
|
validators: seq[Validator],
|
2018-12-27 20:14:37 +00:00
|
|
|
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
|
|
|
|
2019-01-22 22:36:31 +00:00
|
|
|
func xorSeed(seed: Eth2Digest, x: uint64): Eth2Digest =
|
|
|
|
## Integers are all encoded as bigendian
|
|
|
|
## Helper for get_shuffling in lieu of generally better bitwise handling
|
|
|
|
## xor least significant/highest-index 8 bytes in place (after copy)
|
|
|
|
result = seed
|
|
|
|
for i in 0 ..< 8:
|
|
|
|
result.data[31 - i] = result.data[31 - i] xor byte((x shr i*8) and 0xff)
|
|
|
|
|
2019-01-16 10:21:06 +00:00
|
|
|
func get_shuffling*(seed: Eth2Digest,
|
2019-01-17 18:27:11 +00:00
|
|
|
validators: openArray[Validator],
|
2019-01-17 20:09:07 +00:00
|
|
|
slot_nonaligned: uint64
|
2019-01-22 22:36:31 +00:00
|
|
|
): seq[seq[Uint24]] =
|
|
|
|
## Shuffles ``validators`` into crosslink committees seeded by ``seed`` and ``slot``.
|
|
|
|
## Returns a list of ``EPOCH_LENGTH * committees_per_slot`` committees where each
|
|
|
|
## committee is itself a list of validator indices.
|
2018-11-23 22:44:43 +00:00
|
|
|
## Implementation should do the following: http://vitalik.ca/files/ShuffleAndAssign.png
|
|
|
|
|
|
|
|
let
|
2019-01-17 20:09:07 +00:00
|
|
|
slot = slot_nonaligned - slot_nonaligned mod EPOCH_LENGTH
|
2018-11-23 22:44:43 +00:00
|
|
|
|
2019-01-22 22:36:31 +00:00
|
|
|
active_validator_indices = get_active_validator_indices(validators, slot)
|
2018-11-23 22:44:43 +00:00
|
|
|
|
2019-01-22 22:36:31 +00:00
|
|
|
committees_per_slot = get_committee_count_per_slot(len(active_validator_indices)).int
|
2018-11-23 22:44:43 +00:00
|
|
|
|
2019-01-22 22:36:31 +00:00
|
|
|
# Shuffle
|
|
|
|
shuffled_active_validator_indices = shuffle(
|
|
|
|
active_validator_indices,
|
|
|
|
xorSeed(seed, slot))
|
2018-11-23 22:44:43 +00:00
|
|
|
|
2019-01-22 22:36:31 +00:00
|
|
|
# Split the shuffled list into epoch_length * committees_per_slot pieces
|
|
|
|
result = split(shuffled_active_validator_indices, committees_per_slot * EPOCH_LENGTH)
|
|
|
|
assert result.len() == committees_per_slot * EPOCH_LENGTH # what split should do..
|
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,
|
2019-01-16 11:39:16 +00:00
|
|
|
slot: uint64,
|
2018-12-03 21:41:24 +00:00
|
|
|
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,
|
2019-01-16 11:39:16 +00:00
|
|
|
slot: slot,
|
2018-12-19 04:36:10 +00:00
|
|
|
flag: flag
|
2018-12-21 23:47:55 +00:00
|
|
|
))
|
2019-01-23 01:50:51 +00:00
|
|
|
|
|
|
|
func get_previous_epoch_committee_count_per_slot(state: BeaconState): uint64 =
|
|
|
|
let previous_active_validators = get_active_validator_indices(
|
|
|
|
state.validator_registry,
|
|
|
|
state.previous_epoch_calculation_slot
|
|
|
|
)
|
2019-01-23 17:53:51 +00:00
|
|
|
get_committee_count_per_slot(len(previous_active_validators))
|
2019-01-23 01:50:51 +00:00
|
|
|
|
|
|
|
func get_current_epoch_committee_count_per_slot(state: BeaconState): uint64 =
|
|
|
|
let previous_active_validators = get_active_validator_indices(
|
|
|
|
state.validator_registry,
|
|
|
|
state.current_epoch_calculation_slot
|
|
|
|
)
|
2019-01-23 17:53:51 +00:00
|
|
|
get_committee_count_per_slot(len(previous_active_validators))
|
2019-01-23 01:50:51 +00:00
|
|
|
|
|
|
|
func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64) : seq[tuple[a: seq[Uint24], b: uint64]] =
|
|
|
|
## Returns the list of ``(committee, shard)`` tuples for the ``slot``.
|
|
|
|
|
|
|
|
let state_epoch_slot = state.slot - (state.slot mod EPOCH_LENGTH)
|
|
|
|
assert state_epoch_slot <= slot + EPOCH_LENGTH
|
|
|
|
assert slot < state_epoch_slot + EPOCH_LENGTH
|
|
|
|
let offset = slot mod EPOCH_LENGTH
|
|
|
|
|
|
|
|
if slot < state_epoch_slot:
|
|
|
|
let
|
|
|
|
committees_per_slot = get_previous_epoch_committee_count_per_slot(state)
|
|
|
|
shuffling = get_shuffling(
|
|
|
|
state.previous_epoch_randao_mix,
|
|
|
|
state.validator_registry,
|
|
|
|
state.previous_epoch_calculation_slot
|
|
|
|
)
|
|
|
|
slot_start_shard = (state.previous_epoch_start_shard + committees_per_slot * offset) mod SHARD_COUNT
|
|
|
|
|
|
|
|
## This duplication is ugly, but keeping for sake of closeness with spec code structure
|
|
|
|
## There are better approaches in general.
|
|
|
|
for i in 0 ..< committees_per_slot.int:
|
|
|
|
result.add (
|
|
|
|
shuffling[(committees_per_slot * offset + i.uint64).int],
|
|
|
|
(slot_start_shard + i.uint64) mod SHARD_COUNT
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
committees_per_slot = get_current_epoch_committee_count_per_slot(state)
|
|
|
|
shuffling = get_shuffling(
|
|
|
|
state.current_epoch_randao_mix,
|
|
|
|
state.validator_registry,
|
|
|
|
state.current_epoch_calculation_slot
|
|
|
|
)
|
|
|
|
slot_start_shard = (state.current_epoch_start_shard + committees_per_slot * offset) mod SHARD_COUNT
|
|
|
|
|
|
|
|
for i in 0 ..< committees_per_slot.int:
|
|
|
|
result.add (
|
|
|
|
shuffling[(committees_per_slot * offset + i.uint64).int],
|
|
|
|
(slot_start_shard + i.uint64) mod SHARD_COUNT
|
|
|
|
)
|
2019-01-26 19:32:10 +00:00
|
|
|
|
|
|
|
func get_shard_committees_at_slot*(
|
|
|
|
state: BeaconState, slot: uint64): seq[ShardCommittee] =
|
|
|
|
# TODO temporary adapter; remove when all users gone
|
|
|
|
# where ShardCommittee is: shard*: uint64 / committee*: seq[Uint24]
|
|
|
|
let index = state.get_shard_committees_index(slot)
|
|
|
|
#state.shard_committees_at_slots[index]
|
|
|
|
for crosslink_committee in get_crosslink_committees_at_slot(state, slot):
|
|
|
|
var sac: ShardCommittee
|
|
|
|
sac.shard = crosslink_committee.b
|
|
|
|
sac.committee = crosslink_committee.a
|
|
|
|
result.add sac
|
|
|
|
|
|
|
|
func get_beacon_proposer_index*(state: BeaconState, slot: uint64): Uint24 =
|
|
|
|
## From Casper RPJ mini-spec:
|
|
|
|
## When slot i begins, validator Vidx is expected
|
|
|
|
## to create ("propose") a block, which contains a pointer to some parent block
|
|
|
|
## that they perceive as the "head of the chain",
|
|
|
|
## and includes all of the **attestations** that they know about
|
|
|
|
## that have not yet been included into that chain.
|
|
|
|
##
|
|
|
|
## idx in Vidx == p(i mod N), pi being a random permutation of validators indices (i.e. a committee)
|
|
|
|
## Returns the beacon proposer index for the ``slot``.
|
|
|
|
# TODO this index is invalid outside of the block state transition function
|
|
|
|
# because presently, `state.slot += 1` happens before this function
|
|
|
|
# is called - see also testutil.getNextBeaconProposerIndex
|
|
|
|
let first_committee = get_crosslink_committees_at_slot(state, slot)[0][0]
|
|
|
|
first_committee[slot.int mod len(first_committee)]
|