def test_validate_attestation_aggregate_signature(
        genesis_state, epoch_length, random, sample_attestation_data_params,
        is_valid, genesis_epoch, target_committee_size, shard_count, keymap):
    state = genesis_state

    # choose committee
    slot = 0
    crosslink_committee = get_crosslink_committees_at_slot(
        state=state,
        slot=slot,
        genesis_epoch=genesis_epoch,
        epoch_length=epoch_length,
        target_committee_size=target_committee_size,
        shard_count=shard_count,
    )[0]
    committee, shard = crosslink_committee
    committee_size = len(committee)
    assert committee_size > 0

    # randomly select 3/4 participants from committee
    votes_count = len(committee) * 3 // 4
    assert votes_count > 0

    attestation_data = AttestationData(**sample_attestation_data_params).copy(
        slot=slot,
        shard=shard,
    )

    attestation = create_mock_signed_attestation(
        state,
        attestation_data,
        committee,
        votes_count,
        keymap,
        epoch_length,
    )

    if is_valid:
        validate_attestation_aggregate_signature(
            state,
            attestation,
            genesis_epoch,
            epoch_length,
            target_committee_size,
            shard_count,
        )
    else:
        # mess up signature
        attestation = attestation.copy(
            aggregate_signature=(attestation.aggregate_signature[0] + 10,
                                 attestation.aggregate_signature[1] - 1))
        with pytest.raises(ValidationError):
            validate_attestation_aggregate_signature(
                state,
                attestation,
                genesis_epoch,
                epoch_length,
                target_committee_size,
                shard_count,
            )
Beispiel #2
0
def create_mock_signed_attestations_at_slot(
        state: BeaconState,
        config: BeaconConfig,
        attestation_slot: SlotNumber,
        keymap: Dict[BLSPubkey, int],
        voted_attesters_ratio: float = 1.0) -> Iterable[Attestation]:
    """
    Create the mocking attestations of the given ``attestation_slot`` slot with ``keymap``.
    """
    crosslink_committees_at_slot = get_crosslink_committees_at_slot(
        state.copy(slot=state.slot + 1, ),
        slot=attestation_slot,
        genesis_epoch=config.GENESIS_EPOCH,
        epoch_length=config.EPOCH_LENGTH,
        target_committee_size=config.TARGET_COMMITTEE_SIZE,
        shard_count=config.SHARD_COUNT,
    )
    for crosslink_committee in crosslink_committees_at_slot:
        committee, shard = crosslink_committee

        num_voted_attesters = int(len(committee) * voted_attesters_ratio)
        latest_crosslink_root = state.latest_crosslinks[shard].shard_block_root

        attestation_data = AttestationData(
            slot=attestation_slot,
            shard=shard,
            beacon_block_root=ZERO_HASH32,
            epoch_boundary_root=ZERO_HASH32,
            shard_block_root=ZERO_HASH32,
            latest_crosslink_root=latest_crosslink_root,
            justified_epoch=state.previous_justified_epoch,
            justified_block_root=get_block_root(
                state,
                get_epoch_start_slot(state.previous_justified_epoch,
                                     config.EPOCH_LENGTH),
                config.LATEST_BLOCK_ROOTS_LENGTH,
            ),
        )

        yield create_mock_signed_attestation(
            state,
            attestation_data,
            committee,
            num_voted_attesters,
            keymap,
            config.EPOCH_LENGTH,
        )
Beispiel #3
0
def test_get_crosslink_committees_at_slot(ten_validators_state,
                                          state_epoch_slot, slot, epoch_length,
                                          target_committee_size, shard_count):

    state = ten_validators_state.copy(slot=state_epoch_slot, )

    crosslink_committees_at_slot = get_crosslink_committees_at_slot(
        state=state,
        slot=slot,
        epoch_length=epoch_length,
        target_committee_size=target_committee_size,
        shard_count=shard_count,
    )
    assert len(crosslink_committees_at_slot) > 0
    for crosslink_committee in crosslink_committees_at_slot:
        committee, shard = crosslink_committee
        assert len(committee) > 0
        assert shard < shard_count