helpers

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: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActiveIndexRoot

func ActiveIndexRoot(state *pb.BeaconState, wantedEpoch uint64) ([]byte, error)

ActiveIndexRoot returns the index root of a given epoch.

Spec pseudocode definition:

def get_active_index_root(state: BeaconState,
                       epoch: EpochNumber) -> Bytes32:
 """
 Return the index root at a recent ``epoch``.
 """
 assert get_current_epoch(state) - LATEST_INDEX_ROOTS_LENGTH + ENTRY_EXIT_DELAY < epoch <= get_current_epoch(state) + ENTRY_EXIT_DELAY
 return state.latest_index_roots[epoch % LATEST_INDEX_ROOTS_LENGTH]

func ActiveValidatorIndices

func ActiveValidatorIndices(validators []*pb.Validator, epoch uint64) []uint64

ActiveValidatorIndices filters out active validators based on validator status and returns their indices in a list.

Spec pseudocode definition:

def get_active_validator_indices(validators: List[Validator], epoch: EpochNumber) -> List[ValidatorIndex]:
 """
 Get indices of active validators from ``validators``.
 """
 return [i for i, v in enumerate(validators) if is_active_validator(v, epoch)]

func AttestationCurrentEpoch

func AttestationCurrentEpoch(att *pb.AttestationData) uint64

AttestationCurrentEpoch returns the current epoch referenced by the attestation.

func AttestationParticipants

func AttestationParticipants(
	state *pb.BeaconState,
	attestationData *pb.AttestationData,
	bitfield []byte) ([]uint64, error)

AttestationParticipants returns the attesting participants indices.

Spec pseudocode definition:

  def get_attestation_participants(state: BeaconState,
    attestation_data: AttestationData,
    bitfield: bytes) -> List[ValidatorIndex]:
    """
    Returns the participant indices at for the ``attestation_data`` and ``bitfield``.
    """
    # Find the committee in the list with the desired shard
    crosslink_committees = get_crosslink_committees_at_slot(state, attestation_data.slot)

	   assert attestation_data.shard in [shard for _, shard in crosslink_committees]
    crosslink_committee = [committee for committee,
    		shard in crosslink_committees if shard == attestation_data.shard][0]

	   assert verify_bitfield(bitfield, len(crosslink_committee))

    # Find the participating attesters in the committee
    participants = []
    for i, validator_index in enumerate(crosslink_committee):
        aggregation_bit = get_bitfield_bit(bitfield, i)
        if aggregation_bit == 0b1:
           participants.append(validator_index)
   return participants

func CurrentEpoch

func CurrentEpoch(state *pb.BeaconState) uint64

CurrentEpoch returns the current epoch number calculated from the slot number stored in beacon state.

Spec pseudocode definition:

def get_current_epoch(state: BeaconState) -> EpochNumber:
 return slot_to_epoch(state.slot)

func CurrentEpochCommitteeCount

func CurrentEpochCommitteeCount(state *pb.BeaconState) uint64

CurrentEpochCommitteeCount returns the number of crosslink committees per epoch of the current epoch. Ex: Returns 100 means there's 8 committees assigned to current epoch.

Spec pseudocode definition:

def get_current_epoch_committee_count(state: BeaconState) -> int:
 """
 Return the number of committees in the current epoch of the given ``state``.
 """
 current_active_validators = get_active_validator_indices(
     state.validator_registry,
     state.current_calculation_epoch,
 )
 return get_epoch_committee_count(len(current_active_validators)

func DomainVersion

func DomainVersion(fork *pb.Fork, epoch uint64, domainType uint64) uint64

DomainVersion returns the domain version for BLS private key to sign and verify.

Spec pseudocode definition:

	def get_domain(fork: Fork,
              epoch: EpochNumber,
              domain_type: int) -> int:
   """
   Get the domain number that represents the fork meta and signature domain.
   """
   fork_version = get_fork_version(fork, epoch)
   return fork_version * 2**32 + domain_type

func EntryExitEffectEpoch

func EntryExitEffectEpoch(epoch uint64) uint64

EntryExitEffectEpoch takes in epoch number and returns when the validator is eligible for activation and exit.

Spec pseudocode definition: def get_entry_exit_effect_epoch(epoch: EpochNumber) -> EpochNumber:

"""
An entry or exit triggered in the ``epoch`` given by the input takes effect at
the epoch given by the output.
"""
return epoch + 1 + ENTRY_EXIT_DELAY

func EpochCommitteeCount

func EpochCommitteeCount(activeValidatorCount uint64) uint64

EpochCommitteeCount returns the number of crosslink committees of an epoch.

Spec pseudocode definition:

def get_epoch_committee_count(active_validator_count: int) -> int:
 """
 Return the number of committees in one epoch.
 """
 return max(
     1,
     min(
         SHARD_COUNT // EPOCH_LENGTH,
         active_validator_count // EPOCH_LENGTH // TARGET_COMMITTEE_SIZE,
     )
 ) * EPOCH_LENGTH

func ForkVersion

func ForkVersion(fork *pb.Fork, epoch uint64) uint64

ForkVersion returns the fork version of the given epoch number.

Spec pseudocode definition:

	def get_fork_version(fork: Fork,
                    epoch: EpochNumber) -> int:
   """
   Return the fork version of the given ``epoch``.
   """
   if epoch < fork.epoch:
       return fork.previous_version
   else:
       return fork.current_version

func GenerateSeed

func GenerateSeed(state *pb.BeaconState, wantedEpoch uint64) ([32]byte, error)

GenerateSeed generates the randao seed of a given epoch.

Spec pseudocode definition:

def generate_seed(state: BeaconState,
               epoch: EpochNumber) -> Bytes32:
 """
 Generate a seed for the given ``epoch``.
 """
 return hash(
     get_randao_mix(state, epoch - SEED_LOOKAHEAD) +
     get_active_index_root(state, epoch)
 )

func IsActiveValidator

func IsActiveValidator(validator *pb.Validator, epoch uint64) bool

IsActiveValidator returns the boolean value on whether the validator is active or not.

Spec pseudocode definition:

def is_active_validator(validator: Validator, epoch: EpochNumber) -> bool:
 """
 Check if ``validator`` is active.
 """
 return validator.activation_epoch <= epoch < validator.exit_epoch

func NextEpoch

func NextEpoch(state *pb.BeaconState) uint64

NextEpoch returns the next epoch number calculated form the slot number stored in beacon state.

func NextEpochCommitteeCount

func NextEpochCommitteeCount(state *pb.BeaconState) uint64

NextEpochCommitteeCount returns the number of committees per slot of the next epoch.

Spec pseudocode definition:

def get_next_epoch_committee_count(state: BeaconState) -> int:
 """
 Return the number of committees in the next epoch of the given ``state``.
 """
 next_active_validators = get_active_validator_indices(
     state.validator_registry,
     get_current_epoch(state) + 1,
 )
 return get_epoch_committee_count(len(next_active_validators))

func PrevEpoch

func PrevEpoch(state *pb.BeaconState) uint64

PrevEpoch returns the previous epoch number calculated from the slot number stored in beacon state. It also checks for underflow condition.

func PrevEpochCommitteeCount

func PrevEpochCommitteeCount(state *pb.BeaconState) uint64

PrevEpochCommitteeCount returns the number of committees per slot of the previous epoch.

Spec pseudocode definition:

def get_previous_epoch_committee_count(state: BeaconState) -> int:
 """
 Return the number of committees in the previous epoch of the given ``state``.
 """
 previous_active_validators = get_active_validator_indices(
     state.validator_registry,
     state.previous_calculation_epoch,
 )
 return get_epoch_committee_count(len(previous_active_validators))

func RandaoMix

func RandaoMix(state *pb.BeaconState, wantedEpoch uint64) ([]byte, error)

RandaoMix returns the randao mix (xor'ed seed) of a given slot. It is used to shuffle validators.

Spec pseudocode definition:

def get_randao_mix(state: BeaconState,
                epoch: EpochNumber) -> Bytes32:
 """
 Return the randao mix at a recent ``epoch``.
 """
 assert get_current_epoch(state) - LATEST_RANDAO_MIXES_LENGTH < epoch <= get_current_epoch(state)
 return state.latest_randao_mixes[epoch % LATEST_RANDAO_MIXES_LENGTH]

func Shuffling

func Shuffling(
	seed [32]byte,
	validators []*pb.Validator,
	slot uint64) ([][]uint64, error)

Shuffling shuffles input validator indices and splits them by slot and shard.

Spec pseudocode definition:

def get_shuffling(seed: Bytes32,
               validators: List[Validator],
               epoch: EpochNumber) -> List[List[ValidatorIndex]]
 """
 Shuffle ``validators`` into crosslink committees seeded by ``seed`` and ``epoch``.
 Return a list of ``committees_per_epoch`` committees where each
 committee is itself a list of validator indices.
 """

 active_validator_indices = get_active_validator_indices(validators, epoch)

 committees_per_epoch = get_epoch_committee_count(len(active_validator_indices))

 # Shuffle
 seed = xor(seed, int_to_bytes32(epoch))
 shuffled_active_validator_indices = shuffle(active_validator_indices, seed)

 # Split the shuffled list into committees_per_epoch pieces
 return split(shuffled_active_validator_indices, committees_per_epoch)

func SlotToEpoch

func SlotToEpoch(slot uint64) uint64

SlotToEpoch returns the epoch number of the input slot.

Spec pseudocode definition:

def slot_to_epoch(slot: SlotNumber) -> EpochNumber:
 return slot // EPOCH_LENGTH

func StartSlot

func StartSlot(epoch uint64) uint64

StartSlot returns the first slot number of the current epoch.

Spec pseudocode definition:

def get_epoch_start_slot(epoch: EpochNumber) -> SlotNumber:
 return epoch * EPOCH_LENGTH

Types

type CrosslinkCommittee

type CrosslinkCommittee struct {
	Committee []uint64
	Shard     uint64
}

CrosslinkCommittee defines the validator committee of slot and shard combinations.

func CrosslinkCommitteesAtSlot

func CrosslinkCommitteesAtSlot(
	state *pb.BeaconState,
	slot uint64,
	registryChange bool) ([]*CrosslinkCommittee, error)

CrosslinkCommitteesAtSlot returns the list of crosslink committees, it contains the shard associated with the committee and the validator indices in that committee.

def get_crosslink_committees_at_slot(state: BeaconState,
                                  slot: SlotNumber,
                                  registry_change=False: bool) -> List[Tuple[List[ValidatorIndex], ShardNumber]]:
 """
 Return the list of ``(committee, shard)`` tuples for the ``slot``.

 Note: There are two possible shufflings for crosslink committees for a
 ``slot`` in the next epoch -- with and without a `registry_change`
 """
 epoch = slot_to_epoch(slot)
 current_epoch = get_current_epoch(state)
 previous_epoch = current_epoch - 1 if current_epoch > GENESIS_EPOCH else current_epoch
 next_epoch = current_epoch + 1

 assert previous_epoch <= epoch <= next_epoch

 if epoch == previous_epoch:
     committees_per_epoch = get_previous_epoch_committee_count(state)
     seed = state.previous_epoch_seed
     shuffling_epoch = state.previous_calculation_epoch
     shuffling_start_shard = state.previous_epoch_start_shard
 elif epoch == current_epoch:
     committees_per_epoch = get_current_epoch_committee_count(state)
     seed = state.current_epoch_seed
     shuffling_epoch = state.current_calculation_epoch
     shuffling_start_shard = state.current_epoch_start_shard
 elif epoch == next_epoch:
     current_committees_per_epoch = get_current_epoch_committee_count(state)
     committees_per_epoch = get_next_epoch_committee_count(state)
     shuffling_epoch = next_epoch

     epochs_since_last_registry_update = current_epoch - state.validator_registry_update_epoch
     if registry_change:
         seed = generate_seed(state, next_epoch)
         shuffling_start_shard = (state.current_epoch_start_shard + current_committees_per_epoch) % SHARD_COUNT
     elif epochs_since_last_registry_update > 1 and is_power_of_two(epochs_since_last_registry_update):
         seed = generate_seed(state, next_epoch)
         shuffling_start_shard = state.current_epoch_start_shard
     else:
         seed = state.current_epoch_seed
         shuffling_start_shard = state.current_epoch_start_shard

 shuffling = get_shuffling(
     seed,
     state.validator_registry,
     shuffling_epoch,
 )
 offset = slot % EPOCH_LENGTH
 committees_per_slot = committees_per_epoch // EPOCH_LENGTH
 slot_start_shard = (shuffling_start_shard + committees_per_slot * offset) % SHARD_COUNT

 return [
     (
         shuffling[committees_per_slot * offset + i],
         (slot_start_shard + i) % SHARD_COUNT,
     )
     for i in range(committees_per_slot)
 ]

Jump to

Keyboard shortcuts

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