add some helper functions from new spec; fix some comments; more plumbing; start removing some of the status-code refs in favor of the new status bitfield

This commit is contained in:
Dustin Brody 2019-01-16 04:57:49 -08:00
parent bc65693eae
commit 2ff973e46f
3 changed files with 25 additions and 17 deletions

View File

@ -106,11 +106,8 @@ func activate_validator(state: var BeaconState,
## Activate the validator with the given ``index``.
let validator = addr state.validator_registry[index]
if validator.status != PENDING_ACTIVATION:
return
validator.status = ACTIVE
validator.latest_status_change_slot = state.slot
validator.activation_slot = if genesis: GENESIS_SLOT else: state.slot + ENTRY_EXIT_DELAY
state.validator_registry_delta_chain_tip =
get_new_validator_registry_delta_chain_tip(
state.validator_registry_delta_chain_tip,
@ -349,7 +346,8 @@ func update_validator_registry*(state: var BeaconState) =
# Exit validators within the allowable balance churn
balance_churn = 0
for index, validator in state.validator_registry:
if validator.status == ACTIVE_PENDING_EXIT:
if (validator.exit_slot > state.slot + ENTRY_EXIT_DELAY) and
((validator.status_flags and INITIATED_EXIT) == INITIATED_EXIT):
# Check the balance churn would be within the allowance
balance_churn += get_effective_balance(state, index.Uint24)
if balance_churn > max_balance_churn:

View File

@ -132,6 +132,9 @@ const
COLLECTIVE_PENALTY_CALCULATION_PERIOD* = 2'u64^20 ##\
## slots (~73 days)
ENTRY_EXIT_DELAY* = 256 ##\
## slots (~25.6 minutes)
ZERO_BALANCE_VALIDATOR_TTL* = 2'u64^22 ##\
## slots (~291 days)

View File

@ -113,6 +113,18 @@ func processDepositRoot(state: var BeaconState, blck: BeaconBlock) =
vote_count: 1
)
func penalizeValidator(state: var BeaconState, index: Uint24) =
exit_validator(state, index, EXITED_WITH_PENALTY)
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)
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
validator.penalized_slot = state.slot
proc processProposerSlashings(
state: var BeaconState, blck: BeaconBlock, flags: UpdateFlags): bool =
## https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#proposer-slashings-1
@ -159,12 +171,11 @@ proc processProposerSlashings(
warn("PropSlash: block root mismatch")
return false
if not (proposer.status != EXITED_WITH_PENALTY):
warn("PropSlash: wrong status")
if not (proposer.penalized_slot > state.slot):
warn("PropSlash: penalized slot")
return false
exit_validator(
state, proposer_slashing.proposer_index, EXITED_WITH_PENALTY)
penalizeValidator(state, proposer_slashing.proposer_index)
return true
@ -285,20 +296,16 @@ proc processExits(
warn("Exit: invalid signature")
return false
if not (validator.status == ACTIVE):
warn("Exit: validator not active")
if not (validator.exit_slot > state.slot + ENTRY_EXIT_DELAY):
warn("Exit: exit/entry too close")
return false
if not (state.slot >= exit.slot):
warn("Exit: bad slot")
return false
if not (state.slot >=
validator.latest_status_change_slot +
SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD):
warn("Exit: not within committee change period")
exit_validator(state, exit.validator_index, ACTIVE_PENDING_EXIT)
initiate_validator_exit(state, exit.validator_index)
return true
@ -567,7 +574,7 @@ func processEpoch(state: var BeaconState) =
func total_balance_sac(shard_committee: ShardCommittee): uint64 =
sum_effective_balances(statePtr[], shard_committee.committee)
block: # Receipt roots
block: # Deposit roots
if state.slot mod POW_RECEIPT_ROOT_VOTING_PERIOD == 0:
for x in state.deposit_roots:
if x.vote_count * 2 >= POW_RECEIPT_ROOT_VOTING_PERIOD: