Exemplo n.º 1
0
def validate_reject_report_computed_task(
        client_message: RejectReportComputedTask) -> None:
    if (isinstance(client_message.cannot_compute_task,
                   message.CannotComputeTask)
            and isinstance(client_message.task_failure, message.TaskFailure)):
        raise GolemMessageValidationError(
            "RejectReportComputedTask cannot contain CannotComputeTask and TaskFailure at the same time.",
            error_code=ErrorCode.MESSAGE_INVALID,
        )

    if client_message.reason is None:
        raise GolemMessageValidationError(
            f'Error during handling RejectReportComputedTask. REASON is None, it should be message.tasks.RejectReportComputedTask.REASON instance',
            error_code=ErrorCode.MESSAGE_VALUE_WRONG_TYPE)

    if not isinstance(client_message.reason, RejectReportComputedTask.REASON):
        raise GolemMessageValidationError(
            f'Error during handling RejectReportComputedTask. REASON should be message.tasks.RejectReportComputedTask.REASON instance. '
            f'Currently it is {type(client_message.reason)} instance',
            error_code=ErrorCode.MESSAGE_VALUE_WRONG_TYPE)
    validate_task_to_compute(client_message.task_to_compute)

    validate_that_golem_messages_are_signed_with_key(
        hex_to_bytes_convert(
            client_message.task_to_compute.requestor_public_key),
        client_message.task_to_compute,
    )
Exemplo n.º 2
0
def get_validated_client_public_key_from_client_message(golem_message: message.base.Message):
    if isinstance(golem_message, message.concents.ForcePayment):
        if (
            isinstance(golem_message.subtask_results_accepted_list, list) and
            len(golem_message.subtask_results_accepted_list) > 0
        ):
            task_to_compute = golem_message.subtask_results_accepted_list[0].task_to_compute
        else:
            raise ConcentValidationError(
                "subtask_results_accepted_list must be a list type and contains at least one message",
                error_code=ErrorCode.MESSAGE_VALUE_WRONG_LENGTH,
            )

    elif isinstance(golem_message, message.tasks.TaskMessage):
        if not golem_message.is_valid():
            raise GolemMessageValidationError(
                "Golem message invalid",
                error_code=ErrorCode.MESSAGE_INVALID
            )
        task_to_compute = golem_message.task_to_compute
    else:
        raise ConcentValidationError(
            "Unknown message type",
            error_code=ErrorCode.MESSAGE_UNKNOWN,
        )

    if task_to_compute is not None:
        if isinstance(golem_message, (
            message.ForceReportComputedTask,
            message.concents.ForceSubtaskResults,
            message.concents.ForcePayment,
            message.concents.SubtaskResultsVerify,
        )):
            client_public_key = task_to_compute.provider_public_key
            validate_hex_public_key(client_public_key, 'provider_public_key')
        elif isinstance(golem_message, (
            message.AckReportComputedTask,
            message.RejectReportComputedTask,
            message.concents.ForceGetTaskResult,
            message.concents.ForceSubtaskResultsResponse,
        )):
            client_public_key = task_to_compute.requestor_public_key
            validate_hex_public_key(client_public_key, 'requestor_public_key')
        else:
            raise ConcentValidationError(
                "Unknown message type",
                error_code=ErrorCode.MESSAGE_UNKNOWN,
            )

        return hex_to_bytes_convert(client_public_key)

    return None
Exemplo n.º 3
0
def validate_subtask_results_rejected_reason(
    client_message: SubtaskResultsRejected, ) -> None:
    if client_message.reason == SubtaskResultsRejected.REASON.VerificationNegative:
        pass
    elif client_message.reason == SubtaskResultsRejected.REASON.ForcedResourcesFailure:
        force_get_task_result_failed = client_message.force_get_task_result_failed
        if isinstance(force_get_task_result_failed,
                      message.concents.ForceGetTaskResultFailed):
            if force_get_task_result_failed.subtask_id != client_message.subtask_id:
                raise GolemMessageValidationError(
                    f"ForceGetTaskResultsFailed subtask_id does not match with SubtaskResultsRejected TaskToCompute subtask_id.",
                    error_code=ErrorCode.MESSAGE_INVALID,
                )
        else:
            raise GolemMessageValidationError(
                f"SubtaskResultsRejected hasn't got instance of ForceGetTaskResultsFailed message",
                error_code=ErrorCode.MESSAGE_INVALID,
            )
    else:
        raise GolemMessageValidationError(
            f'Error during handling SubtaskResultsRejected. Only VerificationNegative or ForcedResourcesFailure reasons'
            f' are supported by Concent.',
            error_code=ErrorCode.MESSAGE_VALUE_WRONG_TYPE)