adjust get_shuffling and get_active_validator_indices to use slot, per spec

This commit is contained in:
Dustin Brody 2019-01-17 12:09:07 -08:00 committed by zah
parent f30b4f822e
commit 9dcf37aad6
5 changed files with 15 additions and 21 deletions

View File

@ -295,7 +295,7 @@ func get_initial_beacon_state*(
# set initial committee shuffling # set initial committee shuffling
let let
initial_shuffling = initial_shuffling =
get_shuffling(Eth2Digest(), state.validator_registry, 0) get_shuffling(Eth2Digest(), state.validator_registry, 0, state.slot)
# initial_shuffling + initial_shuffling in spec, but more ugly # initial_shuffling + initial_shuffling in spec, but more ugly
for i, n in initial_shuffling: for i, n in initial_shuffling:
@ -304,7 +304,7 @@ func get_initial_beacon_state*(
# set initial persistent shuffling # set initial persistent shuffling
let active_validator_indices = let active_validator_indices =
get_active_validator_indices(state.validator_registry) get_active_validator_indices(state.validator_registry, state.slot)
state.persistent_committees = split(shuffle( state.persistent_committees = split(shuffle(
active_validator_indices, ZERO_HASH), SHARD_COUNT) active_validator_indices, ZERO_HASH), SHARD_COUNT)
@ -353,14 +353,14 @@ func process_ejections*(state: var BeaconState) =
## Iterate through the validator registry ## Iterate through the validator registry
## and eject active validators with balance below ``EJECTION_BALANCE``. ## and eject active validators with balance below ``EJECTION_BALANCE``.
for index in get_active_validator_indices(state.validator_registry): for index in get_active_validator_indices(state.validator_registry, state.slot):
if state.validator_balances[index] < EJECTION_BALANCE: if state.validator_balances[index] < EJECTION_BALANCE:
exit_validator(state, index, EXITED_WITHOUT_PENALTY) exit_validator(state, index, EXITED_WITHOUT_PENALTY)
func update_validator_registry*(state: var BeaconState) = func update_validator_registry*(state: var BeaconState) =
let let
active_validator_indices = active_validator_indices =
get_active_validator_indices(state.validator_registry) get_active_validator_indices(state.validator_registry, state.slot)
# The total effective balance of active validators # The total effective balance of active validators
total_balance = sum_effective_balances(state, active_validator_indices) total_balance = sum_effective_balances(state, active_validator_indices)

View File

@ -185,14 +185,13 @@ proc is_surround_vote*(attestation_data_1: AttestationData,
# ### Checks if validator is active # ### Checks if validator is active
# validator.activation_slot <= slot and slot < validator.exit_slot # validator.activation_slot <= slot and slot < validator.exit_slot
func is_active_validator*(validator: Validator): bool = func is_active_validator*(validator: Validator, slot: uint64): bool =
validator.status in {ACTIVE, ACTIVE_PENDING_EXIT} validator.status in {ACTIVE, ACTIVE_PENDING_EXIT}
func get_active_validator_indices*(validators: openArray[Validator], slot: uint64): seq[Uint24] = func get_active_validator_indices*(validators: openArray[Validator], slot: uint64): seq[Uint24] =
## Gets indices of active validators from validators ## Gets indices of active validators from validators
for idx, val in validators: for idx, val in validators:
#if is_active_validator(val, slot): if is_active_validator(val, slot):
if is_active_validator(val):
result.add idx.Uint24 result.add idx.Uint24
func get_committee_count_per_slot*(active_validator_count: int): uint64 = func get_committee_count_per_slot*(active_validator_count: int): uint64 =

View File

@ -22,22 +22,18 @@ func min_empty_validator_index*(
ZERO_BALANCE_VALIDATOR_TTL.uint64 <= current_slot: ZERO_BALANCE_VALIDATOR_TTL.uint64 <= current_slot:
return some(i) return some(i)
func get_active_validator_indices*(validators: openArray[Validator]): seq[Uint24] =
## Select the active validators
for idx, val in validators:
if is_active_validator(val):
result.add idx.Uint24
func get_shuffling*(seed: Eth2Digest, func get_shuffling*(seed: Eth2Digest,
validators: openArray[Validator], validators: openArray[Validator],
crosslinking_start_shard: uint64 crosslinking_start_shard: uint64, # TODO remove
slot_nonaligned: uint64
): array[EPOCH_LENGTH, seq[ShardCommittee]] = ): array[EPOCH_LENGTH, seq[ShardCommittee]] =
## Split up validators into groups at the start of every epoch, ## 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 ## 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 ## Implementation should do the following: http://vitalik.ca/files/ShuffleAndAssign.png
let let
active_validator_indices = get_active_validator_indices(validators) slot = slot_nonaligned - slot_nonaligned mod EPOCH_LENGTH
active_validator_indices = get_active_validator_indices(validators, slot)
committees_per_slot = clamp( committees_per_slot = clamp(
len(active_validator_indices) div EPOCH_LENGTH div TARGET_COMMITTEE_SIZE, len(active_validator_indices) div EPOCH_LENGTH div TARGET_COMMITTEE_SIZE,
1, SHARD_COUNT div EPOCH_LENGTH).uint64 1, SHARD_COUNT div EPOCH_LENGTH).uint64

View File

@ -304,7 +304,6 @@ proc processExits(
warn("Exit: bad slot") warn("Exit: bad slot")
return false return false
exit_validator(state, exit.validator_index, ACTIVE_PENDING_EXIT)
initiate_validator_exit(state, exit.validator_index) initiate_validator_exit(state, exit.validator_index)
return true return true
@ -316,7 +315,7 @@ proc process_ejections(state: var BeaconState) =
## https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#ejections ## https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#ejections
for index, validator in state.validator_registry: for index, validator in state.validator_registry:
if is_active_validator(validator) and if is_active_validator(validator, state.slot) and
state.validator_balances[index] < EJECTION_BALANCE: state.validator_balances[index] < EJECTION_BALANCE:
exit_validator(state, index.Uint24, EXITED_WITHOUT_PENALTY) exit_validator(state, index.Uint24, EXITED_WITHOUT_PENALTY)
@ -443,7 +442,7 @@ func processEpoch(state: var BeaconState) =
# Precomputation # Precomputation
let let
active_validator_indices = active_validator_indices =
get_active_validator_indices(state.validator_registry) get_active_validator_indices(state.validator_registry, state.slot)
total_balance = sum_effective_balances(state, active_validator_indices) total_balance = sum_effective_balances(state, active_validator_indices)
total_balance_in_eth = total_balance div GWEI_PER_ETH total_balance_in_eth = total_balance div GWEI_PER_ETH
@ -714,7 +713,7 @@ func processEpoch(state: var BeaconState) =
for i, v in get_shuffling( for i, v in get_shuffling(
state.latest_randao_mixes[ state.latest_randao_mixes[
(state.slot - EPOCH_LENGTH) mod LATEST_RANDAO_MIXES_LENGTH], (state.slot - EPOCH_LENGTH) mod LATEST_RANDAO_MIXES_LENGTH],
state.validator_registry, next_start_shard): state.validator_registry, next_start_shard, state.slot):
state.shard_committees_at_slots[i + EPOCH_LENGTH] = v state.shard_committees_at_slots[i + EPOCH_LENGTH] = v
else: else:
@ -733,7 +732,7 @@ func processEpoch(state: var BeaconState) =
for i, v in get_shuffling( for i, v in get_shuffling(
state.latest_randao_mixes[ state.latest_randao_mixes[
(state.slot - EPOCH_LENGTH) mod LATEST_RANDAO_MIXES_LENGTH], (state.slot - EPOCH_LENGTH) mod LATEST_RANDAO_MIXES_LENGTH],
state.validator_registry, start_shard): state.validator_registry, start_shard, state.slot):
state.shard_committees_at_slots[i + EPOCH_LENGTH] = v state.shard_committees_at_slots[i + EPOCH_LENGTH] = v
# Note that `start_shard` is not changed from the last epoch. # Note that `start_shard` is not changed from the last epoch.

View File

@ -25,7 +25,7 @@ suite "Validators":
), 32*1024) ), 32*1024)
# TODO the shuffling looks really odd, probably buggy # TODO the shuffling looks really odd, probably buggy
let s = get_shuffling(Eth2Digest(), validators, 0) let s = get_shuffling(Eth2Digest(), validators, 0, 0)
check: check:
s.len == EPOCH_LENGTH s.len == EPOCH_LENGTH
# 32k validators means 2 shards validated per slot - the aim is to get # 32k validators means 2 shards validated per slot - the aim is to get