validators

package
v0.0.0-...-c85edb6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2019 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package validators contains libraries to shuffle validators and retrieve active validator indices from a given slot or an attestation. It also provides helper functions to locate validator based on pubic key.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActivateValidator

func ActivateValidator(state *pb.BeaconState, idx uint64, genesis bool) (*pb.BeaconState, error)

ActivateValidator takes in validator index and updates validator's activation slot.

Spec pseudocode definition: def activate_validator(state: BeaconState, index: ValidatorIndex, is_genesis: bool) -> None:

"""
Activate the validator of the given ``index``.
Note that this function mutates ``state``.
"""
validator = state.validator_registry[index]

validator.activation_epoch = GENESIS_EPOCH if is_genesis else get_entry_exit_effect_epoch(get_current_epoch(state))

func ActiveValidators

func ActiveValidators(state *pb.BeaconState, validatorIndices []uint32) []*pb.Validator

ActiveValidators returns the active validator records in a list.

Spec pseudocode definition:

[state.validator_registry[i] for i in get_active_validator_indices(state.validator_registry)]

func AllValidatorsIndices

func AllValidatorsIndices(state *pb.BeaconState) []uint64

AllValidatorsIndices returns all validator indices from 0 to the last validator.

func Attesters

func Attesters(state *pb.BeaconState, attesterIndices []uint64) []*pb.Validator

Attesters returns the validator records using validator indices.

Spec pseudocode definition:

Let current_epoch_boundary_attesters = [state.validator_registry[i]
for indices in current_epoch_boundary_attester_indices for i in indices].

func AttestingBalance

func AttestingBalance(state *pb.BeaconState, boundaryAttesterIndices []uint64) uint64

AttestingBalance returns the combined balances from the input validator records.

Spec pseudocode definition:

Let current_epoch_boundary_attesting_balance =
sum([get_effective_balance(state, i) for i in current_epoch_boundary_attester_indices])

func AttestingValidatorIndices

func AttestingValidatorIndices(
	state *pb.BeaconState,
	shard uint64,
	shardBlockRoot []byte,
	thisEpochAttestations []*pb.PendingAttestationRecord,
	prevEpochAttestations []*pb.PendingAttestationRecord) ([]uint64, error)

AttestingValidatorIndices returns the shard committee validator indices if the validator shard committee matches the input attestations.

Spec pseudocode definition: Let attesting_validator_indices(crosslink_committee, shard_block_root) be the union of the validator index sets given by [get_attestation_participants(state, a.data, a.participation_bitfield) for a in current_epoch_attestations + previous_epoch_attestations if a.shard == shard_committee.shard and a.shard_block_root == shard_block_root]

func BeaconProposerIdx

func BeaconProposerIdx(state *pb.BeaconState, slot uint64) (uint64, error)

BeaconProposerIdx returns the index of the proposer of the block at a given slot.

Spec pseudocode definition:

def get_beacon_proposer_index(state: BeaconState,slot: int) -> int:
  """
  Returns the beacon proposer index for the ``slot``.
  """
  first_committee, _ = get_crosslink_committees_at_slot(state, slot)[0]
  return first_committee[slot % len(first_committee)]

func EffectiveBalance

func EffectiveBalance(state *pb.BeaconState, idx uint64) uint64

EffectiveBalance returns the balance at stake for the validator. Beacon chain allows validators to top off their balance above MAX_DEPOSIT, but they can be slashed at most MAX_DEPOSIT at any time.

Spec pseudocode definition:

def get_effective_balance(state: State, index: int) -> int:
  """
  Returns the effective balance (also known as "balance at stake") for a ``validator`` with the given ``index``.
  """
  return min(state.validator_balances[idx], MAX_DEPOSIT)

func ExitValidator

func ExitValidator(state *pb.BeaconState, idx uint64) (*pb.BeaconState, error)

ExitValidator takes in validator index and does house keeping work to exit validator with entry exit delay.

Spec pseudocode definition: def exit_validator(state: BeaconState, index: ValidatorIndex) -> None:

"""
Exit the validator of the given ``index``.
Note that this function mutates ``state``.
"""
validator = state.validator_registry[index]

# The following updates only occur if not previous exited
if validator.exit_epoch <= get_entry_exit_effect_epoch(get_current_epoch(state)):
    return

validator.exit_epoch = get_entry_exit_effect_epoch(get_current_epoch(state))

func InitialValidatorRegistry

func InitialValidatorRegistry() []*pb.Validator

InitialValidatorRegistry creates a new validator set that is used to generate a new bootstrapped state.

func InitiateValidatorExit

func InitiateValidatorExit(state *pb.BeaconState, idx uint64) *pb.BeaconState

InitiateValidatorExit takes in validator index and updates validator with INITIATED_EXIT status flag.

Spec pseudocode definition: def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> None:

validator = state.validator_registry[index]
validator.status_flags |= INITIATED_EXIT

func PenalizeValidator

func PenalizeValidator(state *pb.BeaconState, idx uint64) (*pb.BeaconState, error)

PenalizeValidator slashes the malicious validator's balance and awards the whistleblower's balance.

Spec pseudocode definition: def penalize_validator(state: BeaconState, index: ValidatorIndex) -> None:

"""
Penalize the validator of the given ``index``.
Note that this function mutates ``state``.
"""
exit_validator(state, index)
validator = state.validator_registry[index]
state.latest_penalized_balances[get_current_epoch(state) % LATEST_PENALIZED_EXIT_LENGTH] += get_effective_balance(state, index)

whistleblower_index = get_beacon_proposer_index(state, state.slot)
whistleblower_reward = get_effective_balance(state, index) // WHISTLEBLOWER_REWARD_QUOTIENT
state.validator_balances[whistleblower_index] += whistleblower_reward
state.validator_balances[index] -= whistleblower_reward
validator.penalized_epoch = get_current_epoch(state)

func PrepareValidatorForWithdrawal

func PrepareValidatorForWithdrawal(state *pb.BeaconState, idx uint64) *pb.BeaconState

PrepareValidatorForWithdrawal sets validator's status flag to WITHDRAWABLE.

Spec pseudocode definition: def prepare_validator_for_withdrawal(state: BeaconState, index: ValidatorIndex) -> None:

"""
Set the validator with the given ``index`` with ``WITHDRAWABLE`` flag.
Note that this function mutates ``state``.
"""
validator = state.validator_registry[index]
validator.status_flags |= WITHDRAWABLE

func ProcessDeposit

func ProcessDeposit(
	state *pb.BeaconState,
	validatorIdxMap map[[32]byte]int,
	pubkey []byte,
	amount uint64,
	_ []byte,
	withdrawalCredentials []byte,
	randaoCommitment []byte,
) (*pb.BeaconState, error)

ProcessDeposit mutates a corresponding index in the beacon state for a validator depositing ETH into the beacon chain. Specifically, this function adds a validator balance or tops up an existing validator's balance by some deposit amount. This function returns a mutated beacon state and the validator index corresponding to the validator in the processed deposit.

func ProcessPenaltiesAndExits

func ProcessPenaltiesAndExits(state *pb.BeaconState) *pb.BeaconState

ProcessPenaltiesAndExits prepares the validators and the penalized validators for withdrawal.

Spec pseudocode definition: def process_penalties_and_exits(state: BeaconState) -> None:

"""
Process the penalties and prepare the validators who are eligible to withdrawal.
Note that this function mutates ``state``.
"""
current_epoch = get_current_epoch(state)
# The active validators
active_validator_indices = get_active_validator_indices(state.validator_registry, current_epoch)
# The total effective balance of active validators
total_balance = sum(get_effective_balance(state, i) for i in active_validator_indices)

for index, validator in enumerate(state.validator_registry):
    if current_epoch == validator.penalized_epoch + LATEST_PENALIZED_EXIT_LENGTH // 2:
        epoch_index = current_epoch % LATEST_PENALIZED_EXIT_LENGTH
        total_at_start = state.latest_penalized_balances[(epoch_index + 1) % LATEST_PENALIZED_EXIT_LENGTH]
        total_at_end = state.latest_penalized_balances[epoch_index]
        total_penalties = total_at_end - total_at_start
        penalty = get_effective_balance(state, index) * min(total_penalties * 3, total_balance) // total_balance
        state.validator_balances[index] -= penalty

def eligible(index):
    validator = state.validator_registry[index]
    if validator.penalized_epoch <= current_epoch:
        penalized_withdrawal_epochs = LATEST_PENALIZED_EXIT_LENGTH // 2
        return current_epoch >= validator.penalized_epoch + penalized_withdrawal_epochs
    else:
        return current_epoch >= validator.exit_epoch + MIN_VALIDATOR_WITHDRAWAL_EPOCHS

all_indices = list(range(len(state.validator_registry)))
eligible_indices = filter(eligible, all_indices)
# Sort in order of exit epoch, and validators that exit within the same epoch exit in order of validator index
sorted_indices = sorted(eligible_indices, key=lambda index: state.validator_registry[index].exit_epoch)
withdrawn_so_far = 0
for index in sorted_indices:
    prepare_validator_for_withdrawal(state, index)
    withdrawn_so_far += 1
    if withdrawn_so_far >= MAX_WITHDRAWALS_PER_EPOCH:
        break

func TotalEffectiveBalance

func TotalEffectiveBalance(state *pb.BeaconState, validatorIndices []uint64) uint64

TotalEffectiveBalance returns the total deposited amount at stake in Gwei of all active validators.

Spec pseudocode definition:

sum([get_effective_balance(state, i) for i in active_validator_indices])

func UpdateRegistry

func UpdateRegistry(state *pb.BeaconState) (*pb.BeaconState, error)

UpdateRegistry rotates validators in and out of active pool. the amount to rotate is determined by max validator balance churn.

Spec pseudocode definition: def update_validator_registry(state: BeaconState) -> None:

"""
Update validator registry.
Note that this function mutates ``state``.
"""
current_epoch = get_current_epoch(state)
# The active validators
active_validator_indices = get_active_validator_indices(state.validator_registry, current_epoch)
# The total effective balance of active validators
total_balance = sum([get_effective_balance(state, i) for i in active_validator_indices])

# The maximum balance churn in Gwei (for deposits and exits separately)
max_balance_churn = max(
    MAX_DEPOSIT_AMOUNT,
    total_balance // (2 * MAX_BALANCE_CHURN_QUOTIENT)
)

# Activate validators within the allowable balance churn
balance_churn = 0
for index, validator in enumerate(state.validator_registry):
    if validator.activation_epoch > get_entry_exit_effect_epoch(current_epoch) and state.validator_balances[index] >= MAX_DEPOSIT_AMOUNT:
        # Check the balance churn would be within the allowance
        balance_churn += get_effective_balance(state, index)
        if balance_churn > max_balance_churn:
            break

        # Activate validator
        activate_validator(state, index, is_genesis=False)

# Exit validators within the allowable balance churn
balance_churn = 0
for index, validator in enumerate(state.validator_registry):
    if validator.exit_epoch > get_entry_exit_effect_epoch(current_epoch) and validator.status_flags & INITIATED_EXIT:
        # Check the balance churn would be within the allowance
        balance_churn += get_effective_balance(state, index)
        if balance_churn > max_balance_churn:
            break

        # Exit validator
        exit_validator(state, index)

state.validator_registry_update_epoch = current_epoch

func ValidatorIdx

func ValidatorIdx(pubKey []byte, validators []*pb.Validator) (uint64, error)

ValidatorIdx returns the idx of the validator given an input public key.

func ValidatorIndices

func ValidatorIndices(
	state *pb.BeaconState,
	attestations []*pb.PendingAttestationRecord,
) ([]uint64, error)

ValidatorIndices returns all the validator indices from the input attestations and state.

Spec pseudocode definition:

Let attester_indices be the union of the validator
index sets given by [get_attestation_participants(state, a.data, a.a.aggregation_bitfield)
for a in attestations]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL