epoch

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2020 License: GPL-3.0 Imports: 12 Imported by: 16

Documentation

Overview

Package epoch contains epoch processing libraries. These libraries process new balance for the validators, justify and finalize new check points, shuffle and reassign validators to different slots and shards.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttestingBalance

func AttestingBalance(state *stateTrie.BeaconState, atts []*pb.PendingAttestation) (uint64, error)

AttestingBalance returns the total balance from all the attesting indices.

WARNING: This method allocates a new copy of the attesting validator indices set and is considered to be very memory expensive. Avoid using this unless you really need to get attesting balance from attestations.

Spec pseudocode definition:

def get_attesting_balance(state: BeaconState, attestations: List[PendingAttestation]) -> Gwei:
  return get_total_balance(state, get_unslashed_attesting_indices(state, attestations))

func BaseReward

func BaseReward(state *stateTrie.BeaconState, index uint64) (uint64, error)

BaseReward takes state and validator index and calculate individual validator's base reward quotient.

Note: Adjusted quotient is calculated of base reward because it's too inefficient to repeat the same calculation for every validator versus just doing it once.

Spec pseudocode definition:

 def get_base_reward(state: BeaconState, index: ValidatorIndex) -> Gwei:
     total_balance = get_total_active_balance(state)
	    effective_balance = state.validator_registry[index].effective_balance
	    return effective_balance * BASE_REWARD_FACTOR // integer_squareroot(total_balance) // BASE_REWARDS_PER_EPOCH

func ProcessFinalUpdates

func ProcessFinalUpdates(state *stateTrie.BeaconState) (*stateTrie.BeaconState, error)

ProcessFinalUpdates processes the final updates during epoch processing.

Spec pseudocode definition:

def process_final_updates(state: BeaconState) -> None:
  current_epoch = get_current_epoch(state)
  next_epoch = Epoch(current_epoch + 1)
  # Reset eth1 data votes
  if (state.slot + 1) % SLOTS_PER_ETH1_VOTING_PERIOD == 0:
      state.eth1_data_votes = []
  # Update effective balances with hysteresis
  for index, validator in enumerate(state.validators):
      balance = state.balances[index]
      HALF_INCREMENT = EFFECTIVE_BALANCE_INCREMENT // 2
      if balance < validator.effective_balance or validator.effective_balance + 3 * HALF_INCREMENT < balance:
          validator.effective_balance = min(balance - balance % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE)
  # Set active index root
  index_epoch = Epoch(next_epoch + ACTIVATION_EXIT_DELAY)
  index_root_position = index_epoch % EPOCHS_PER_HISTORICAL_VECTOR
  indices_list = List[ValidatorIndex, VALIDATOR_REGISTRY_LIMIT](get_active_validator_indices(state, index_epoch))
  state.active_index_roots[index_root_position] = hash_tree_root(indices_list)
  # Set committees root
  committee_root_position = next_epoch % EPOCHS_PER_HISTORICAL_VECTOR
  state.compact_committees_roots[committee_root_position] = get_compact_committees_root(state, next_epoch)
  # Reset slashings
  state.slashings[next_epoch % EPOCHS_PER_SLASHINGS_VECTOR] = Gwei(0)
  # Set randao mix
  state.randao_mixes[next_epoch % EPOCHS_PER_HISTORICAL_VECTOR] = get_randao_mix(state, current_epoch)
  # Set historical root accumulator
  if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0:
      historical_batch = HistoricalBatch(block_roots=state.block_roots, state_roots=state.state_roots)
      state.historical_roots.append(hash_tree_root(historical_batch))
  # Update start shard
  state.start_shard = Shard((state.start_shard + get_shard_delta(state, current_epoch)) % SHARD_COUNT)
  # Rotate current/previous epoch attestations
  state.previous_epoch_attestations = state.current_epoch_attestations
  state.current_epoch_attestations = []

func ProcessRegistryUpdates

func ProcessRegistryUpdates(state *stateTrie.BeaconState) (*stateTrie.BeaconState, error)

ProcessRegistryUpdates rotates validators in and out of active pool. the amount to rotate is determined churn limit.

Spec pseudocode definition:

def process_registry_updates(state: BeaconState) -> None:
 # Process activation eligibility and ejections
 for index, validator in enumerate(state.validators):
     if is_eligible_for_activation_queue(validator):
         validator.activation_eligibility_epoch = get_current_epoch(state) + 1

     if is_active_validator(validator, get_current_epoch(state)) and validator.effective_balance <= EJECTION_BALANCE:
         initiate_validator_exit(state, ValidatorIndex(index))

 # Queue validators eligible for activation and not yet dequeued for activation
 activation_queue = sorted([
     index for index, validator in enumerate(state.validators)
     if is_eligible_for_activation(state, validator)
     # Order by the sequence of activation_eligibility_epoch setting and then index
 ], key=lambda index: (state.validators[index].activation_eligibility_epoch, index))
 # Dequeued validators for activation up to churn limit
 for index in activation_queue[:get_validator_churn_limit(state)]:
     validator = state.validators[index]
     validator.activation_epoch = compute_activation_exit_epoch(get_current_epoch(state))

func ProcessSlashings

func ProcessSlashings(state *stateTrie.BeaconState) (*stateTrie.BeaconState, error)

ProcessSlashings processes the slashed validators during epoch processing,

 def process_slashings(state: BeaconState) -> None:
   epoch = get_current_epoch(state)
   total_balance = get_total_active_balance(state)
   for index, validator in enumerate(state.validators):
       if validator.slashed and epoch + EPOCHS_PER_SLASHINGS_VECTOR // 2 == validator.withdrawable_epoch:
           increment = EFFECTIVE_BALANCE_INCREMENT  # Factored out from penalty numerator to avoid uint64 overflow
			  penalty_numerator = validator.effective_balance // increment * min(sum(state.slashings) * 3, total_balance)
           penalty = penalty_numerator // total_balance * increment
           decrease_balance(state, ValidatorIndex(index), penalty)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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