def test_get_beacon_proposer_index(genesis_state, config): # TODO(hwwhww) find a way to normalize validator effective balance distribution. state = genesis_state validators = tuple([ state.validators[index].set("effective_balance", config.MAX_EFFECTIVE_BALANCE) for index in range(len(state.validators)) ]) for slot in range(0, config.SLOTS_PER_EPOCH): state = state.mset("slot", slot, "validators", validators) proposer_index = get_beacon_proposer_index(state, config) assert proposer_index current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH) domain_type = signature_domain_to_domain_type( SignatureDomain.DOMAIN_BEACON_PROPOSER) seed = hash_eth2( get_seed(state, current_epoch, domain_type, config) + state.slot.to_bytes(8, "little")) random_byte = hash_eth2(seed + (proposer_index // 32).to_bytes(8, "little"))[proposer_index % 32] # Verify if proposer_index matches the condition. assert (state.validators[proposer_index].effective_balance * MAX_RANDOM_BYTE >= config.MAX_EFFECTIVE_BALANCE * random_byte)
def get_beacon_committee( state: BeaconState, slot: Slot, index: CommitteeIndex, config: CommitteeConfig) -> Tuple[ValidatorIndex, ...]: epoch = compute_epoch_at_slot(slot, config.SLOTS_PER_EPOCH) committees_per_slot = get_committee_count_at_slot( state, slot, config.MAX_COMMITTEES_PER_SLOT, config.SLOTS_PER_EPOCH, config.TARGET_COMMITTEE_SIZE, ) active_validator_indices = get_active_validator_indices( state.validators, epoch) domain_type = signature_domain_to_domain_type( SignatureDomain.DOMAIN_BEACON_ATTESTER) return _compute_committee( indices=active_validator_indices, seed=get_seed(state, epoch, domain_type, config), index=(slot % config.SLOTS_PER_EPOCH) * committees_per_slot + index, count=committees_per_slot * config.SLOTS_PER_EPOCH, shuffle_round_count=config.SHUFFLE_ROUND_COUNT, )
def get_beacon_proposer_index(state: BeaconState, config: Eth2Config) -> ValidatorIndex: """ Return the current beacon proposer index. """ current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH) domain_type = signature_domain_to_domain_type( SignatureDomain.DOMAIN_BEACON_PROPOSER) seed = hash_eth2( get_seed(state, current_epoch, domain_type, config) + state.slot.to_bytes(8, "little")) indices = get_active_validator_indices(state.validators, current_epoch) return compute_proposer_index( state.validators, indices, seed, config.MAX_EFFECTIVE_BALANCE, config.SHUFFLE_ROUND_COUNT, )
def get_crosslink_committee( state: BeaconState, epoch: Epoch, shard: Shard, config: CommitteeConfig) -> Iterable[ValidatorIndex]: target_shard = (shard + config.SHARD_COUNT - get_start_shard(state, epoch, config)) % config.SHARD_COUNT active_validator_indices = get_active_validator_indices( state.validators, epoch) return _compute_committee( indices=active_validator_indices, seed=get_seed(state, epoch, config), index=target_shard, count=get_committee_count( len(active_validator_indices), config.SHARD_COUNT, config.SLOTS_PER_EPOCH, config.TARGET_COMMITTEE_SIZE, ), shuffle_round_count=config.SHUFFLE_ROUND_COUNT, )
def get_beacon_proposer_index( state: BeaconState, committee_config: CommitteeConfig) -> ValidatorIndex: """ Return the current beacon proposer index. """ first_committee = _calculate_first_committee_at_slot( state, state.slot, committee_config) current_epoch = state.current_epoch(committee_config.SLOTS_PER_EPOCH) seed = get_seed(state, current_epoch, committee_config) return _find_proposer_in_committee( state.validators, first_committee, current_epoch, seed, committee_config.MAX_EFFECTIVE_BALANCE, )