From 9dcf37aad669e3c3f0ff753bceb6752068273434 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Thu, 17 Jan 2019 12:09:07 -0800 Subject: [PATCH] adjust get_shuffling and get_active_validator_indices to use slot, per spec --- beacon_chain/spec/beaconstate.nim | 8 ++++---- beacon_chain/spec/helpers.nim | 5 ++--- beacon_chain/spec/validator.nim | 12 ++++-------- beacon_chain/state_transition.nim | 9 ++++----- tests/test_validator.nim | 2 +- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index e9dc57b55..6672c0db8 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -295,7 +295,7 @@ func get_initial_beacon_state*( # set initial committee shuffling let 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 for i, n in initial_shuffling: @@ -304,7 +304,7 @@ func get_initial_beacon_state*( # set initial persistent shuffling 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( active_validator_indices, ZERO_HASH), SHARD_COUNT) @@ -353,14 +353,14 @@ func process_ejections*(state: var BeaconState) = ## Iterate through the validator registry ## 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: exit_validator(state, index, EXITED_WITHOUT_PENALTY) func update_validator_registry*(state: var BeaconState) = let 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 total_balance = sum_effective_balances(state, active_validator_indices) diff --git a/beacon_chain/spec/helpers.nim b/beacon_chain/spec/helpers.nim index 92089669d..c1fe59302 100644 --- a/beacon_chain/spec/helpers.nim +++ b/beacon_chain/spec/helpers.nim @@ -185,14 +185,13 @@ proc is_surround_vote*(attestation_data_1: AttestationData, # ### Checks if validator is active # 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} func get_active_validator_indices*(validators: openArray[Validator], slot: uint64): seq[Uint24] = ## Gets indices of active validators from validators for idx, val in validators: - #if is_active_validator(val, slot): - if is_active_validator(val): + if is_active_validator(val, slot): result.add idx.Uint24 func get_committee_count_per_slot*(active_validator_count: int): uint64 = diff --git a/beacon_chain/spec/validator.nim b/beacon_chain/spec/validator.nim index 341128a1e..a60ae9c8e 100644 --- a/beacon_chain/spec/validator.nim +++ b/beacon_chain/spec/validator.nim @@ -22,22 +22,18 @@ func min_empty_validator_index*( ZERO_BALANCE_VALIDATOR_TTL.uint64 <= current_slot: 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, validators: openArray[Validator], - crosslinking_start_shard: uint64 + crosslinking_start_shard: uint64, # TODO remove + slot_nonaligned: uint64 ): array[EPOCH_LENGTH, seq[ShardCommittee]] = ## 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 - 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( len(active_validator_indices) div EPOCH_LENGTH div TARGET_COMMITTEE_SIZE, 1, SHARD_COUNT div EPOCH_LENGTH).uint64 diff --git a/beacon_chain/state_transition.nim b/beacon_chain/state_transition.nim index 84b630fe0..0ccc8f05d 100644 --- a/beacon_chain/state_transition.nim +++ b/beacon_chain/state_transition.nim @@ -304,7 +304,6 @@ proc processExits( warn("Exit: bad slot") return false - exit_validator(state, exit.validator_index, ACTIVE_PENDING_EXIT) initiate_validator_exit(state, exit.validator_index) 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 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: exit_validator(state, index.Uint24, EXITED_WITHOUT_PENALTY) @@ -443,7 +442,7 @@ func processEpoch(state: var BeaconState) = # Precomputation let 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_in_eth = total_balance div GWEI_PER_ETH @@ -714,7 +713,7 @@ func processEpoch(state: var BeaconState) = for i, v in get_shuffling( state.latest_randao_mixes[ (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 else: @@ -733,7 +732,7 @@ func processEpoch(state: var BeaconState) = for i, v in get_shuffling( state.latest_randao_mixes[ (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 # Note that `start_shard` is not changed from the last epoch. diff --git a/tests/test_validator.nim b/tests/test_validator.nim index f6283895a..e1f8ce20c 100644 --- a/tests/test_validator.nim +++ b/tests/test_validator.nim @@ -25,7 +25,7 @@ suite "Validators": ), 32*1024) # TODO the shuffling looks really odd, probably buggy - let s = get_shuffling(Eth2Digest(), validators, 0) + let s = get_shuffling(Eth2Digest(), validators, 0, 0) check: s.len == EPOCH_LENGTH # 32k validators means 2 shards validated per slot - the aim is to get