def create_mock_signed_attestations_at_slot( state: BeaconState, config: Eth2Config, state_machine: BaseBeaconStateMachine, attestation_slot: Slot, beacon_block_root: SigningRoot, keymap: Dict[BLSPubkey, int], voted_attesters_ratio: float = 1.0, ) -> Iterable[Attestation]: """ Create the mocking attestations of the given ``attestation_slot`` slot with ``keymap``. """ committees_per_slot = get_committee_count_at_slot( state, attestation_slot, config.MAX_COMMITTEES_PER_SLOT, config.SLOTS_PER_EPOCH, config.TARGET_COMMITTEE_SIZE, ) # Get `target_root` target_root = _get_target_root(state, config, beacon_block_root) target_epoch = compute_epoch_at_slot(state.slot, config.SLOTS_PER_EPOCH) for committee, committee_index, _ in iterate_committees_at_slot( state, attestation_slot, committees_per_slot, CommitteeConfig(config)): attestation_data = AttestationData( slot=attestation_slot, index=CommitteeIndex(committee_index), beacon_block_root=beacon_block_root, source=Checkpoint( epoch=state.current_justified_checkpoint.epoch, root=state.current_justified_checkpoint.root, ), target=Checkpoint(root=target_root, epoch=target_epoch), ) num_voted_attesters = int(len(committee) * voted_attesters_ratio) yield _create_mock_signed_attestation( state, attestation_data, attestation_slot, committee, num_voted_attesters, keymap, config.SLOTS_PER_EPOCH, )
def _get_committees(state, target_slot, config, sampling_fraction): committees_per_slot = get_committee_count_at_slot( state, target_slot, config.MAX_COMMITTEES_PER_SLOT, config.SLOTS_PER_EPOCH, config.TARGET_COMMITTEE_SIZE, ) committees_at_slot = () for committee, _, _ in iterate_committees_at_slot(state, target_slot, committees_per_slot, config): committees_at_slot += (committee, ) return tuple( random.sample(committees_at_slot, int((sampling_fraction * committees_per_slot))))
def test_get_committee_count_at_slot( validator_count, slots_per_epoch, target_committee_size, max_committees_per_slot, expected_committee_count, genesis_state, ): state = genesis_state assert expected_committee_count == get_committee_count_at_slot( state, state.slot, max_committees_per_slot=max_committees_per_slot, slots_per_epoch=slots_per_epoch, target_committee_size=target_committee_size, )
def _validate_eligible_committee_index( state: BeaconState, attestation_slot: Slot, committee_index: CommitteeIndex, max_committees_per_slot: int, slots_per_epoch: int, target_committee_size: int, ) -> None: committees_per_slot = get_committee_count_at_slot( state, attestation_slot, max_committees_per_slot, slots_per_epoch, target_committee_size, ) if committee_index >= committees_per_slot: raise ValidationError( f"Attestation with committee index ({committee_index}) must be" f" less than the calculated committee per slot ({committees_per_slot})" f" of slot {attestation_slot}")
def test_get_beacon_committee(genesis_state, config): state = genesis_state indices = tuple() epoch_start_slot = state.slot for slot in range(epoch_start_slot, epoch_start_slot + config.SLOTS_PER_EPOCH): committees_at_slot = get_committee_count_at_slot( state, slot, config.MAX_COMMITTEES_PER_SLOT, config.SLOTS_PER_EPOCH, config.TARGET_COMMITTEE_SIZE, ) for committee_index in range(committees_at_slot): some_committee = get_beacon_committee(state, slot, committee_index, config) indices += tuple(some_committee) assert set(indices) == set(range(len(genesis_state.validators))) assert len(indices) == len(genesis_state.validators)
def create_mock_slashable_attestation( state: BeaconState, config: Eth2Config, keymap: Dict[BLSPubkey, int], attestation_slot: Slot, ) -> IndexedAttestation: """ Create an `IndexedAttestation` that is signed by one attester. """ attester_index = ValidatorIndex(0) committee = (attester_index, ) # Use genesis block root as `beacon_block_root`, only for tests. beacon_block_root = get_block_root_at_slot( state, attestation_slot, config.SLOTS_PER_HISTORICAL_ROOT) # Get `target_root` target_root = _get_target_root(state, config, beacon_block_root) # Get `source_root` source_root = get_block_root_at_slot( state, compute_start_slot_at_epoch(state.current_justified_checkpoint.epoch, config.SLOTS_PER_EPOCH), config.SLOTS_PER_HISTORICAL_ROOT, ) committees_per_slot = get_committee_count_at_slot( state, Slot(attestation_slot), config.MAX_COMMITTEES_PER_SLOT, config.SLOTS_PER_EPOCH, config.TARGET_COMMITTEE_SIZE, ) # Use the first committee assert committees_per_slot > 0 committee_index = CommitteeIndex(0) attestation_data = AttestationData.create( slot=attestation_slot, index=committee_index, beacon_block_root=beacon_block_root, source=Checkpoint.create( epoch=state.current_justified_checkpoint.epoch, root=source_root), target=Checkpoint.create( epoch=compute_epoch_at_slot(attestation_slot, config.SLOTS_PER_EPOCH), root=target_root, ), ) message_hash = attestation_data.hash_tree_root attesting_indices = _get_mock_attesting_indices(committee, num_voted_attesters=1) signature = sign_transaction( message_hash=message_hash, privkey=keymap[state.validators[attesting_indices[0]].pubkey], state=state, slot=attestation_slot, signature_domain=SignatureDomain.DOMAIN_BEACON_ATTESTER, slots_per_epoch=config.SLOTS_PER_EPOCH, ) validator_indices = tuple(committee[i] for i in attesting_indices) return IndexedAttestation.create(attesting_indices=validator_indices, data=attestation_data, signature=signature)