update initiate_validator_exit and exit_validator helper functions to current spec; remove spec-removed EXITED_WITHOUT_PENALTY

This commit is contained in:
Dustin Brody 2019-01-17 13:01:55 -08:00 committed by zah
parent 9dcf37aad6
commit 1fe5ba07b8
3 changed files with 13 additions and 44 deletions

View File

@ -122,44 +122,21 @@ func activate_validator(state: var BeaconState,
func initiate_validator_exit(state: var BeaconState,
index: Uint24) =
## Initiate exit for the validator with the given ``index``.
let validator = addr state.validator_registry[index]
if validator.status != ACTIVE:
return
validator.status = ACTIVE_PENDING_EXIT
validator.latest_status_change_slot = state.slot
var validator = state.validator_registry[index]
validator.status_flags = validator.status_flags or INITIATED_EXIT
state.validator_registry[index] = validator
func exit_validator*(state: var BeaconState,
index: Uint24,
new_status: ValidatorStatusCodes) =
index: Uint24) =
## Exit the validator with the given ``index``.
let
validator = addr state.validator_registry[index]
prev_status = validator.status
let validator = addr state.validator_registry[index]
if prev_status == EXITED_WITH_PENALTY:
# The following updates only occur if not previous exited
if validator.exit_slot <= state.slot + ENTRY_EXIT_DELAY:
return
validator.status = new_status
validator.latest_status_change_slot = state.slot
if new_status == EXITED_WITH_PENALTY:
state.latest_penalized_exit_balances[
(state.slot div COLLECTIVE_PENALTY_CALCULATION_PERIOD).int] +=
get_effective_balance(state, index)
let
whistleblower_index =
get_beacon_proposer_index(state, state.slot)
whistleblower_reward =
get_effective_balance(state, index) div WHISTLEBLOWER_REWARD_QUOTIENT
state.validator_balances[whistleblower_index] += whistleblower_reward
state.validator_balances[index] -= whistleblower_reward
if prev_status == EXITED_WITHOUT_PENALTY:
return
validator.exit_slot = state.slot + ENTRY_EXIT_DELAY
# The following updates only occur if not previous exited
state.validator_registry_exit_count += 1
@ -173,13 +150,6 @@ func exit_validator*(state: var BeaconState,
ValidatorSetDeltaFlags.EXIT
)
# Remove validator from persistent committees
for committee in state.persistent_committees.mitems():
for i, validator_index in committee:
if validator_index == index:
committee.delete(i)
break
func process_penalties_and_exits_eligible(state: BeaconState, index: int): bool =
let validator = state.validator_registry[index]
if validator.penalized_slot <= state.slot:
@ -355,7 +325,7 @@ func process_ejections*(state: var BeaconState) =
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)
exit_validator(state, index)
func update_validator_registry*(state: var BeaconState) =
let
@ -394,7 +364,7 @@ func update_validator_registry*(state: var BeaconState) =
break
# Exit validator
exit_validator(state, index.Uint24, EXITED_WITHOUT_PENALTY)
exit_validator(state, index.Uint24)
# Perform additional updates
state.previous_epoch_calculation_slot = state.current_epoch_calculation_slot

View File

@ -453,7 +453,6 @@ type
UNUSED = 0
ACTIVE = 1
ACTIVE_PENDING_EXIT = 2
EXITED_WITHOUT_PENALTY = 3
EXITED_WITH_PENALTY = 4
ValidatorSetDeltaFlags* {.pure.} = enum

View File

@ -114,7 +114,7 @@ func processDepositRoot(state: var BeaconState, blck: BeaconBlock) =
)
func penalizeValidator(state: var BeaconState, index: Uint24) =
exit_validator(state, index, EXITED_WITH_PENALTY)
exit_validator(state, index)
var validator = state.validator_registry[index]
#state.latest_penalized_exit_balances[(state.slot div EPOCH_LENGTH) mod LATEST_PENALIZED_EXIT_LENGTH] += get_effective_balance(state, index.Uint24)
@ -237,7 +237,7 @@ proc processCasperSlashings(state: var BeaconState, blck: BeaconBlock): bool =
for i in intersection:
if state.validator_registry[i].status != EXITED_WITH_PENALTY:
exit_validator(state, i, EXITED_WITH_PENALTY)
exit_validator(state, i)
return true
@ -317,7 +317,7 @@ proc process_ejections(state: var BeaconState) =
for index, validator in state.validator_registry:
if is_active_validator(validator, state.slot) and
state.validator_balances[index] < EJECTION_BALANCE:
exit_validator(state, index.Uint24, EXITED_WITHOUT_PENALTY)
exit_validator(state, index.Uint24)
func processSlot(state: var BeaconState, previous_block_root: Eth2Digest) =
## Time on the beacon chain moves in slots. Every time we make it to a new