github.com/prysmaticlabs/prysm@v1.4.4/tools/specs-checker/data/specs/phase0/validator.md (about)

     1  ```python
     2  def check_if_validator_active(state: BeaconState, validator_index: ValidatorIndex) -> bool:
     3      validator = state.validators[validator_index]
     4      return is_active_validator(validator, get_current_epoch(state))
     5  ```
     6  ```python
     7  def get_committee_assignment(state: BeaconState,
     8                               epoch: Epoch,
     9                               validator_index: ValidatorIndex
    10                               ) -> Optional[Tuple[Sequence[ValidatorIndex], CommitteeIndex, Slot]]:
    11      """
    12      Return the committee assignment in the ``epoch`` for ``validator_index``.
    13      ``assignment`` returned is a tuple of the following form:
    14          * ``assignment[0]`` is the list of validators in the committee
    15          * ``assignment[1]`` is the index to which the committee is assigned
    16          * ``assignment[2]`` is the slot at which the committee is assigned
    17      Return None if no assignment.
    18      """
    19      next_epoch = Epoch(get_current_epoch(state) + 1)
    20      assert epoch <= next_epoch
    21  
    22      start_slot = compute_start_slot_at_epoch(epoch)
    23      committee_count_per_slot = get_committee_count_per_slot(state, epoch)
    24      for slot in range(start_slot, start_slot + SLOTS_PER_EPOCH):
    25          for index in range(committee_count_per_slot):
    26              committee = get_beacon_committee(state, Slot(slot), CommitteeIndex(index))
    27              if validator_index in committee:
    28                  return committee, CommitteeIndex(index), Slot(slot)
    29      return None
    30  ```
    31  ```python
    32  def is_proposer(state: BeaconState, validator_index: ValidatorIndex) -> bool:
    33      return get_beacon_proposer_index(state) == validator_index
    34  ```
    35  ```python
    36  def get_epoch_signature(state: BeaconState, block: BeaconBlock, privkey: int) -> BLSSignature:
    37      domain = get_domain(state, DOMAIN_RANDAO, compute_epoch_at_slot(block.slot))
    38      signing_root = compute_signing_root(compute_epoch_at_slot(block.slot), domain)
    39      return bls.Sign(privkey, signing_root)
    40  ```
    41  ```python
    42  def compute_time_at_slot(state: BeaconState, slot: Slot) -> uint64:
    43      return uint64(state.genesis_time + slot * SECONDS_PER_SLOT)
    44  ```
    45  ```python
    46  def voting_period_start_time(state: BeaconState) -> uint64:
    47      eth1_voting_period_start_slot = Slot(state.slot - state.slot % (EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH))
    48      return compute_time_at_slot(state, eth1_voting_period_start_slot)
    49  ```
    50  ```python
    51  def is_candidate_block(block: Eth1Block, period_start: uint64) -> bool:
    52      return (
    53          block.timestamp + SECONDS_PER_ETH1_BLOCK * ETH1_FOLLOW_DISTANCE <= period_start
    54          and block.timestamp + SECONDS_PER_ETH1_BLOCK * ETH1_FOLLOW_DISTANCE * 2 >= period_start
    55      )
    56  ```
    57  ```python
    58  def get_eth1_vote(state: BeaconState, eth1_chain: Sequence[Eth1Block]) -> Eth1Data:
    59      period_start = voting_period_start_time(state)
    60      # `eth1_chain` abstractly represents all blocks in the eth1 chain sorted by ascending block height
    61      votes_to_consider = [
    62          get_eth1_data(block) for block in eth1_chain
    63          if (
    64              is_candidate_block(block, period_start)
    65              # Ensure cannot move back to earlier deposit contract states
    66              and get_eth1_data(block).deposit_count >= state.eth1_data.deposit_count
    67          )
    68      ]
    69  
    70      # Valid votes already cast during this period
    71      valid_votes = [vote for vote in state.eth1_data_votes if vote in votes_to_consider]
    72  
    73      # Default vote on latest eth1 block data in the period range unless eth1 chain is not live
    74      # Non-substantive casting for linter
    75      state_eth1_data: Eth1Data = state.eth1_data
    76      default_vote = votes_to_consider[len(votes_to_consider) - 1] if any(votes_to_consider) else state_eth1_data
    77  
    78      return max(
    79          valid_votes,
    80          key=lambda v: (valid_votes.count(v), -valid_votes.index(v)),  # Tiebreak by smallest distance
    81          default=default_vote
    82      )
    83  ```
    84  ```python
    85  def compute_new_state_root(state: BeaconState, block: BeaconBlock) -> Root:
    86      temp_state: BeaconState = state.copy()
    87      signed_block = SignedBeaconBlock(message=block)
    88      state_transition(temp_state, signed_block, validate_result=False)
    89      return hash_tree_root(temp_state)
    90  ```
    91  ```python
    92  def get_block_signature(state: BeaconState, block: BeaconBlock, privkey: int) -> BLSSignature:
    93      domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_at_slot(block.slot))
    94      signing_root = compute_signing_root(block, domain)
    95      return bls.Sign(privkey, signing_root)
    96  ```
    97  ```python
    98  def get_attestation_signature(state: BeaconState, attestation_data: AttestationData, privkey: int) -> BLSSignature:
    99      domain = get_domain(state, DOMAIN_BEACON_ATTESTER, attestation_data.target.epoch)
   100      signing_root = compute_signing_root(attestation_data, domain)
   101      return bls.Sign(privkey, signing_root)
   102  ```
   103  ```python
   104  def compute_subnet_for_attestation(committees_per_slot: uint64, slot: Slot, committee_index: CommitteeIndex) -> uint64:
   105      """
   106      Compute the correct subnet for an attestation for Phase 0.
   107      Note, this mimics expected future behavior where attestations will be mapped to their shard subnet.
   108      """
   109      slots_since_epoch_start = uint64(slot % SLOTS_PER_EPOCH)
   110      committees_since_epoch_start = committees_per_slot * slots_since_epoch_start
   111  
   112      return uint64((committees_since_epoch_start + committee_index) % ATTESTATION_SUBNET_COUNT)
   113  ```
   114  ```python
   115  def get_slot_signature(state: BeaconState, slot: Slot, privkey: int) -> BLSSignature:
   116      domain = get_domain(state, DOMAIN_SELECTION_PROOF, compute_epoch_at_slot(slot))
   117      signing_root = compute_signing_root(slot, domain)
   118      return bls.Sign(privkey, signing_root)
   119  ```
   120  ```python
   121  def is_aggregator(state: BeaconState, slot: Slot, index: CommitteeIndex, slot_signature: BLSSignature) -> bool:
   122      committee = get_beacon_committee(state, slot, index)
   123      modulo = max(1, len(committee) // TARGET_AGGREGATORS_PER_COMMITTEE)
   124      return bytes_to_uint64(hash(slot_signature)[0:8]) % modulo == 0
   125  ```
   126  ```python
   127  def get_aggregate_signature(attestations: Sequence[Attestation]) -> BLSSignature:
   128      signatures = [attestation.signature for attestation in attestations]
   129      return bls.Aggregate(signatures)
   130  ```
   131  ```python
   132  def get_aggregate_and_proof(state: BeaconState,
   133                              aggregator_index: ValidatorIndex,
   134                              aggregate: Attestation,
   135                              privkey: int) -> AggregateAndProof:
   136      return AggregateAndProof(
   137          aggregator_index=aggregator_index,
   138          aggregate=aggregate,
   139          selection_proof=get_slot_signature(state, aggregate.data.slot, privkey),
   140      )
   141  ```
   142  ```python
   143  def get_aggregate_and_proof_signature(state: BeaconState,
   144                                        aggregate_and_proof: AggregateAndProof,
   145                                        privkey: int) -> BLSSignature:
   146      aggregate = aggregate_and_proof.aggregate
   147      domain = get_domain(state, DOMAIN_AGGREGATE_AND_PROOF, compute_epoch_at_slot(aggregate.data.slot))
   148      signing_root = compute_signing_root(aggregate_and_proof, domain)
   149      return bls.Sign(privkey, signing_root)
   150  ```