Beispiel #1
0
    def setUp(self):
        super().setUp()

        self.compute_task_def = self._get_deserialized_compute_task_def(
            deadline=get_current_utc_timestamp() + 100
        )

        self.task_to_compute = self._get_deserialized_task_to_compute(
            timestamp=parse_timestamp_to_utc_datetime(get_current_utc_timestamp()),
            compute_task_def=self.compute_task_def,
        )

        self.report_computed_task = self._get_deserialized_report_computed_task(
            task_to_compute=self.task_to_compute,
        )

        self.force_report_computed_task = self._get_deserialized_force_report_computed_task(
            report_computed_task=self.report_computed_task
        )

        self.reject_report_computed_task = self._get_deserialized_reject_report_computed_task(
            reason=message.tasks.RejectReportComputedTask.REASON.SubtaskTimeLimitExceeded,
            task_to_compute=self.task_to_compute,
        )

        self.force_get_task_result = self._get_deserialized_force_subtask_results(
            timestamp=parse_timestamp_to_utc_datetime(get_current_utc_timestamp()),
            task_to_compute=self.task_to_compute,
        )

        self.provider_public_key = hex_to_bytes_convert(self.task_to_compute.provider_public_key)
        self.requestor_public_key = hex_to_bytes_convert(self.task_to_compute.requestor_public_key)
def test_case_2c_send_force_payment_with_no_value_to_be_paid(
        cluster_consts: ProtocolConstants, cluster_url: str) -> None:
    #  Test CASE 2C - Send ForcePayment with no value to be paid
    receive_pending_messages_for_requestor_and_provider(
        cluster_url, sci_base, CONCENT_PUBLIC_KEY)
    current_time = get_current_utc_timestamp()
    api_request(
        cluster_url,
        'send',
        sci_base.provider_private_key,
        CONCENT_PUBLIC_KEY,
        force_payment(
            timestamp=timestamp_to_isoformat(current_time),
            subtask_results_accepted_list=[
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(
                        current_time - cluster_consts.payment_due_time +
                        AVERAGE_TIME_FOR_TWO_BLOCKS),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=0,
                            provider_public_key=sci_base.provider_public_key,
                            provider_private_key=sci_base.provider_private_key,
                            requestor_public_key=sci_base.requestor_public_key,
                            requestor_private_key=sci_base.
                            requestor_private_key,
                        ),
                        provider_private_key=sci_base.provider_private_key),
                    requestor_private_key=sci_base.requestor_private_key,
                ),
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(
                        current_time - cluster_consts.payment_due_time +
                        AVERAGE_TIME_FOR_TWO_BLOCKS),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=0,
                            provider_public_key=sci_base.provider_public_key,
                            provider_private_key=sci_base.provider_private_key,
                            requestor_public_key=sci_base.requestor_public_key,
                            requestor_private_key=sci_base.
                            requestor_private_key,
                        ),
                        provider_private_key=sci_base.provider_private_key),
                    requestor_private_key=sci_base.requestor_private_key,
                ),
            ]),
        expected_status=200,
        expected_message_type=message.concents.ServiceRefused,
    )
Beispiel #3
0
def calculate_subtask_verification_time(
        report_computed_task: message.ReportComputedTask) -> int:
    """
    This function calls `subtask_verification_time` helper function from golem messages or Concent custom implementation
    of it, depending on CUSTOM_PROTOCOL_TIMES setting value.
    The reason for using custom implementation is because it has hard-coded values and we cannot make it use values
    from our settings.
    """
    assert isinstance(report_computed_task, message.ReportComputedTask)

    if settings.CUSTOM_PROTOCOL_TIMES:
        mdt = calculate_maximum_download_time(
            size=report_computed_task.size, rate=settings.MINIMUM_UPLOAD_RATE)
        ttc_dt = parse_timestamp_to_utc_datetime(
            report_computed_task.task_to_compute.timestamp, )
        subtask_dt = parse_timestamp_to_utc_datetime(
            report_computed_task.task_to_compute.compute_task_def['deadline'],
        )
        subtask_timeout = subtask_dt - ttc_dt

        return int((4 * settings.CONCENT_MESSAGING_TIME) + (3 * mdt) +
                   (0.5 * subtask_timeout.total_seconds()))
    else:
        return int(
            subtask_verification_time(report_computed_task).total_seconds())
def test_case_2b_send_force_payment_beyond_payment_time(
        cluster_consts: ProtocolConstants, cluster_url: str) -> None:
    #  Test CASE 2B - Send ForcePayment beyond payment time
    receive_pending_messages_for_requestor_and_provider(
        cluster_url, sci_base, CONCENT_PUBLIC_KEY)
    current_time = get_current_utc_timestamp()
    api_request(
        cluster_url,
        'send',
        sci_base.provider_private_key,
        CONCENT_PUBLIC_KEY,
        force_payment(
            timestamp=timestamp_to_isoformat(current_time),
            subtask_results_accepted_list=[
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=15000,
                            provider_public_key=sci_base.provider_public_key,
                            provider_private_key=sci_base.provider_private_key,
                            requestor_public_key=sci_base.requestor_public_key,
                            requestor_private_key=sci_base.
                            requestor_private_key,
                        ),
                        provider_private_key=sci_base.provider_private_key,
                    ),
                    requestor_private_key=sci_base.requestor_private_key),
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(
                        current_time - cluster_consts.payment_due_time +
                        AVERAGE_TIME_FOR_TWO_BLOCKS),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=15000,
                            provider_public_key=sci_base.provider_public_key,
                            provider_private_key=sci_base.provider_private_key,
                            requestor_public_key=sci_base.requestor_public_key,
                            requestor_private_key=sci_base.
                            requestor_private_key,
                        ),
                        provider_private_key=sci_base.provider_private_key,
                    ),
                    requestor_private_key=sci_base.requestor_private_key)
            ]),
        expected_status=200,
        expected_message_type=message.concents.ForcePaymentRejected,
        expected_content_type='application/octet-stream',
    )
Beispiel #5
0
def test_case_2d_send_correct_force_payment(cluster_consts, cluster_url,
                                            test_id):
    # Test CASE 2D - Send correct ForcePayment
    current_time = get_current_utc_timestamp()
    (task_id, subtask_id) = get_task_id_and_subtask_id(test_id, '2D')
    correct_force_payment = force_payment(subtask_results_accepted_list=[
        subtask_results_accepted(
            payment_ts=current_time - cluster_consts.payment_due_time -
            AVERAGE_TIME_FOR_TWO_BLOCKS,
            task_to_compute=create_signed_task_to_compute(
                timestamp=parse_timestamp_to_utc_datetime(current_time),
                task_id=task_id + 'a',
                subtask_id=subtask_id + 'A',
                deadline=current_time,
                price=1000,
            )),
        subtask_results_accepted(
            payment_ts=current_time - cluster_consts.payment_due_time -
            AVERAGE_TIME_FOR_TWO_BLOCKS,
            task_to_compute=create_signed_task_to_compute(
                timestamp=parse_timestamp_to_utc_datetime(current_time),
                task_id=task_id + 'b',
                subtask_id=subtask_id + 'B',
                deadline=current_time,
                price=1000,
            ))
    ])
    correct_force_payment.sig = None
    api_request(
        cluster_url,
        'send',
        PROVIDER_PRIVATE_KEY,
        CONCENT_PUBLIC_KEY,
        correct_force_payment,
        headers={
            'Content-Type': 'application/octet-stream',
        },
        expected_status=200,
        expected_message_type=message.concents.ForcePaymentCommitted.TYPE,
        expected_content_type='application/octet-stream',
    )
    time.sleep(5)
    api_request(
        cluster_url,
        'receive-out-of-band',
        REQUESTOR_PRIVATE_KEY,
        CONCENT_PUBLIC_KEY,
        create_client_auth_message(REQUESTOR_PRIVATE_KEY, REQUESTOR_PUBLIC_KEY,
                                   CONCENT_PUBLIC_KEY),
        headers={
            'Content-Type': 'application/octet-stream',
        },
        expected_status=200,
        expected_message_type=message.concents.ForcePaymentCommitted.TYPE,
        expected_content_type='application/octet-stream',
    )
Beispiel #6
0
 def queryset(self, request: HttpRequest, queryset: QuerySet) -> QuerySet:
     assert self.value() in {'active', 'passive', 'active_or_downloads', None}
     current_timestamp = get_current_utc_timestamp()
     if self.value() == 'active':
         return queryset.filter(state__in=ACTIVE_STATE_NAMES, next_deadline__gte=parse_timestamp_to_utc_datetime(current_timestamp))
     elif self.value() == 'passive':
         return queryset.filter(
             Q(state__in=PASSIVE_STATE_NAMES) |
             Q(state__in=ACTIVE_STATE_NAMES, next_deadline__lt=parse_timestamp_to_utc_datetime(current_timestamp))
         )
     elif self.value() == 'active_or_downloads':
         return Subtask.objects_with_timing_columns.filter(
             Q(download_deadline__gte=current_timestamp, state=Subtask.SubtaskState.RESULT_UPLOADED.name) |  # pylint: disable=no-member
             Q(state__in=ACTIVE_STATE_NAMES, next_deadline__gte=parse_timestamp_to_utc_datetime(current_timestamp))
         )
     return queryset
Beispiel #7
0
    def test_parse_timestamp_to_utc_datetime_should_return_utc_datetime(self):
        """ Tests if parse_timestamp_to_utc_datetime function works as expected. """

        for timestamp, expected_datetime in {
                946684800:
                datetime.datetime(year=2000,
                                  month=1,
                                  day=1,
                                  hour=0,
                                  minute=0,
                                  second=0,
                                  tzinfo=timezone.utc),
                946684800 + 3666:
                datetime.datetime(year=2000,
                                  month=1,
                                  day=1,
                                  hour=1,
                                  minute=1,
                                  second=6,
                                  tzinfo=timezone.utc),
                1321009871:
                datetime.datetime(year=2011,
                                  month=11,
                                  day=11,
                                  hour=11,
                                  minute=11,
                                  second=11,
                                  tzinfo=timezone.utc),
        }.items():
            self.assertEqual(parse_timestamp_to_utc_datetime(timestamp),
                             expected_datetime)
Beispiel #8
0
def store_verification_request_and_blender_subtask_definition(
    subtask_id: str,
    source_package_path: str,
    result_package_path: str,
    output_format: str,
    scene_file: str,
    verification_deadline: int,
    blender_crop_script: Optional[str],
) -> tuple:
    verification_request = VerificationRequest(
        subtask_id=subtask_id,
        source_package_path=source_package_path,
        result_package_path=result_package_path,
        verification_deadline=parse_timestamp_to_utc_datetime(
            verification_deadline),
    )
    verification_request.full_clean()
    verification_request.save()

    blender_subtask_definition = BlenderSubtaskDefinition(
        verification_request=verification_request,
        output_format=BlenderSubtaskDefinition.OutputFormat[output_format].
        name,
        scene_file=scene_file,
        blender_crop_script=blender_crop_script,
    )
    blender_subtask_definition.full_clean()
    blender_subtask_definition.save()

    return (verification_request, blender_subtask_definition)
Beispiel #9
0
    def test_that_if_stored_messages_have_incompatible_protocol_version_function_should_return_false(
            self):
        with override_settings(
                GOLEM_MESSAGES_VERSION='1.11.1',
                MAJOR_MINOR_GOLEM_MESSAGES_VERSION='1.11',
        ):
            subtask = Subtask(
                task_id=self.task_to_compute.compute_task_def['task_id'],
                subtask_id=self.task_to_compute.compute_task_def['subtask_id'],
                provider=self.provider,
                requestor=self.requestor,
                result_package_size=self.size,
                state=Subtask.SubtaskState.REPORTED.name,  # pylint: disable=no-member
                next_deadline=None,
                computation_deadline=parse_timestamp_to_utc_datetime(
                    self.compute_task_def['deadline']),
                task_to_compute=store_message(self.task_to_compute,
                                              self.task_id, self.subtask_id),
                want_to_compute_task=store_message(
                    self.task_to_compute.want_to_compute_task, self.task_id,
                    self.subtask_id),
                report_computed_task=store_message(self.report_computed_task,
                                                   self.task_id,
                                                   self.subtask_id),
                protocol_version=settings.MAJOR_MINOR_GOLEM_MESSAGES_VERSION,
            )
            subtask.full_clean()
            subtask.save()

        self.assertFalse(
            is_protocol_version_compatible(
                subtask.task_to_compute.protocol_version, ))
    def test_that_verification_result_error_should_add_pending_messages_subtask_results_settled_and_change_subtask_state_to_accepted(
            self):
        with freeze_time(
                parse_timestamp_to_utc_datetime(get_current_utc_timestamp())):
            with mock.patch('core.tasks.logger.info') as logging_info_mock:
                verification_result(  # pylint: disable=no-value-for-parameter
                    self.subtask.subtask_id,
                    VerificationResult.ERROR.name,
                    'test',
                    ErrorCode.REQUEST_BODY_NOT_EMPTY.name,
                )

        self.subtask.refresh_from_db()
        self.assertEqual(self.subtask.state_enum,
                         Subtask.SubtaskState.ACCEPTED)
        self.assertEqual(self.subtask.next_deadline, None)
        self.assertEqual(PendingResponse.objects.count(), 2)
        self.assertTrue(
            PendingResponse.objects.filter(
                client=self.subtask.provider).exists())
        self.assertTrue(
            PendingResponse.objects.filter(
                client=self.subtask.requestor).exists())

        self.assertEqual(logging_info_mock.call_count, 3)
        self.assertIn(
            f'SUBTASK_ID: {self.subtask.subtask_id}. Verification_result_task starts. Result: ERROR',
            str(logging_info_mock.call_args_list))
        self.assertIn(
            f'SUBTASK_ID: {self.subtask.subtask_id}. Verification_result_task ends. Result: ERROR',
            str(logging_info_mock.call_args_list))
    def test_that_if_stored_messages_are_compatible_with_protocol_version_function_should_return_true(
            self):
        subtask = Subtask(
            task_id=self.task_to_compute.compute_task_def['task_id'],
            subtask_id=self.task_to_compute.compute_task_def['subtask_id'],
            provider=self.provider,
            requestor=self.requestor,
            result_package_size=self.size,
            state=Subtask.SubtaskState.REPORTED.name,  # pylint: disable=no-member
            next_deadline=None,
            computation_deadline=parse_timestamp_to_utc_datetime(
                self.compute_task_def['deadline']),
            task_to_compute=store_message(self.task_to_compute, self.task_id,
                                          self.subtask_id),
            want_to_compute_task=store_message(
                self.task_to_compute.want_to_compute_task, self.task_id,
                self.subtask_id),
            report_computed_task=store_message(self.report_computed_task,
                                               self.task_id, self.subtask_id),
        )
        subtask.full_clean()
        subtask.save()

        self.assertTrue(
            is_protocol_version_compatible(
                subtask.task_to_compute.protocol_version, ))
 def test_that_exception_is_not_raised_when_subtask_is_null_and_concent_use_case_is_forced_payment(
         self):
     self.deposit_claim.subtask = None
     self.deposit_claim.concent_use_case = ConcentUseCase.FORCED_PAYMENT.value
     self.deposit_claim.closure_time = parse_timestamp_to_utc_datetime(
         get_current_utc_timestamp())
     self.deposit_claim.clean()
Beispiel #13
0
def update_subtasks_states(subtask: Subtask, client_public_key: bytes) -> None:
    if (subtask.state in [state.name for state in Subtask.ACTIVE_STATES]
            and subtask.next_deadline <= parse_timestamp_to_utc_datetime(
                get_current_utc_timestamp())):
        verify_file_status(subtask=subtask,
                           client_public_key=client_public_key)
        _update_timed_out_subtask(subtask)
Beispiel #14
0
def update_all_timed_out_subtasks_of_a_client(
        client_public_key: bytes) -> None:
    """
    Function looks for all subtasks in active state of client. All found subtasks are processed in separate transactions,
    locked in database, file status is verified (check additional conditions in verify_file_status) and subtask's state
    is updated in _update_timed_out_subtask
    """

    encoded_client_public_key = b64encode(client_public_key)

    clients_subtask_list = Subtask.objects.filter(
        Q(requestor__public_key=encoded_client_public_key)
        | Q(provider__public_key=encoded_client_public_key),
        state__in=[state.name for state in Subtask.ACTIVE_STATES],
    )
    # Check if files are uploaded for all clients subtasks. It is checked for all clients subtasks, not only timeouted.
    for subtask in clients_subtask_list:
        with transaction.atomic(using='control'):
            Subtask.objects.select_for_update().filter(
                subtask_id=subtask.subtask_id)
            verify_file_status(subtask=subtask,
                               client_public_key=client_public_key)

            # Subtask may change it's state to passive (RESULT UPLOADED) in verify_file_status. In this case there
            # is no need to call _update_timed_out_subtask any more. Next_deadline will be set to None, so it is
            # necessary to check it before checking if deadline is exceeded.
            if subtask.next_deadline is not None and subtask.next_deadline <= parse_timestamp_to_utc_datetime(
                    get_current_utc_timestamp()):
                _update_timed_out_subtask(subtask)
    def test_that_update_subtask_passes_validations_from_passive_to_active_state(
            self):
        subtask = store_subtask(
            task_id=self.task_to_compute.compute_task_def['task_id'],
            subtask_id=self.task_to_compute.compute_task_def['subtask_id'],
            provider_public_key=self.provider_public_key,
            requestor_public_key=self.requestor_public_key,
            state=Subtask.SubtaskState.REPORTED,
            next_deadline=None,
            task_to_compute=self.task_to_compute,
            report_computed_task=self.report_computed_task,
        )
        subtask_state = Subtask.objects.get(
            subtask_id=self.task_to_compute.compute_task_def['subtask_id']
        ).state
        self.assertEqual(subtask_state, Subtask.SubtaskState.REPORTED.name)  # pylint: disable=no-member
        self.assertEqual(subtask.next_deadline, None)

        next_deadline = int(self.task_to_compute.compute_task_def['deadline']
                            ) + settings.CONCENT_MESSAGING_TIME
        update_and_return_updated_subtask(
            subtask=subtask,
            state=Subtask.SubtaskState.FORCING_RESULT_TRANSFER,
            next_deadline=next_deadline,
            force_get_task_result=self.force_get_task_result,
            set_next_deadline=True,
        )
        subtask_state = Subtask.objects.get(
            subtask_id=self.task_to_compute.compute_task_def['subtask_id']
        ).state
        self.assertEqual(subtask_state,
                         Subtask.SubtaskState.FORCING_RESULT_TRANSFER.name)  # pylint: disable=no-member
        self.assertEqual(subtask.next_deadline,
                         parse_timestamp_to_utc_datetime(next_deadline))
Beispiel #16
0
def store_pending_message(
    response_type: PendingResponse.ResponseType,
    client_public_key: bytes,
    queue: PendingResponse.Queue,
    subtask: Optional[Subtask] = None,
    payment_message: Optional[message.concents.ForcePaymentCommitted] = None,
) -> None:
    try:
        client = Client.objects.get_or_create_full_clean(client_public_key)
        receive_queue = PendingResponse(
            response_type=response_type.name,
            client=client,
            queue=queue.name,
            subtask=subtask,
        )
        receive_queue.full_clean()
        receive_queue.save()
        if payment_message is not None:
            payment_committed_message = PaymentInfo(
                payment_ts=parse_timestamp_to_utc_datetime(
                    payment_message.payment_ts),
                task_owner_key=payment_message.task_owner_key,
                provider_eth_account=payment_message.provider_eth_account,
                amount_paid=payment_message.amount_paid,
                recipient_type=payment_message.recipient_type.name,  # pylint: disable=no-member
                amount_pending=payment_message.amount_pending,
                pending_response=receive_queue)
            payment_committed_message.full_clean()
            payment_committed_message.save()
    except IntegrityError:
        raise CreateModelIntegrityError

    logging.log_new_pending_response(logger, response_type.name, queue.name,
                                     subtask)
Beispiel #17
0
    def test_send_should_return_http_400_if_task_to_compute_deadline_exceeded(self):
        compute_task_def = ComputeTaskDefFactory()
        compute_task_def['deadline'] = self.message_timestamp - 9000

        with freeze_time(parse_timestamp_to_utc_datetime(self.message_timestamp - 10000)):
            task_to_compute = self._get_deserialized_task_to_compute(
                compute_task_def=self.compute_task_def,
                provider_public_key=self._get_provider_hex_public_key(),
                requestor_public_key=self._get_requestor_hex_public_key(),
            )

        report_computed_task = message.ReportComputedTask(
            task_to_compute=task_to_compute
        )
        report_computed_task.sign_message(self.PROVIDER_PRIVATE_KEY)

        ack_report_computed_task = message.tasks.AckReportComputedTask(
            report_computed_task=report_computed_task
        )

        response_400 = self.send_request(
            url='core:send',
            data=dump(
                ack_report_computed_task,
                self.PROVIDER_PRIVATE_KEY,
                CONCENT_PUBLIC_KEY
            ),
        )

        self._test_400_response(
            response_400,
            error_code=ErrorCode.QUEUE_COMMUNICATION_NOT_STARTED
        )
Beispiel #18
0
def test_case_2d_send_correct_force_payment(cluster_consts: ProtocolConstants,
                                            cluster_url: str) -> None:
    # Test CASE 2D - Send correct ForcePayment
    current_time = get_current_utc_timestamp()
    correct_force_payment = force_payment(subtask_results_accepted_list=[
        create_signed_subtask_results_accepted(
            payment_ts=current_time - cluster_consts.payment_due_time -
            AVERAGE_TIME_FOR_TWO_BLOCKS,
            report_computed_task=create_signed_report_computed_task(
                task_to_compute=create_signed_task_to_compute(
                    timestamp=parse_timestamp_to_utc_datetime(current_time),
                    deadline=current_time,
                    price=1000,
                ))),
        create_signed_subtask_results_accepted(
            payment_ts=current_time - cluster_consts.payment_due_time -
            AVERAGE_TIME_FOR_TWO_BLOCKS,
            report_computed_task=create_signed_report_computed_task(
                task_to_compute=create_signed_task_to_compute(
                    timestamp=parse_timestamp_to_utc_datetime(current_time),
                    deadline=current_time,
                    price=1000,
                )))
    ])
    correct_force_payment.sig = None
    api_request(
        cluster_url,
        'send',
        PROVIDER_PRIVATE_KEY,
        CONCENT_PUBLIC_KEY,
        correct_force_payment,
        expected_status=200,
        expected_message_type=message.concents.ForcePaymentCommitted,
        expected_content_type='application/octet-stream',
    )
    time.sleep(5)
    api_request(
        cluster_url,
        'receive',
        REQUESTOR_PRIVATE_KEY,
        CONCENT_PUBLIC_KEY,
        create_client_auth_message(REQUESTOR_PRIVATE_KEY, REQUESTOR_PUBLIC_KEY,
                                   CONCENT_PUBLIC_KEY),
        expected_status=200,
        expected_message_type=message.concents.ForcePaymentCommitted,
        expected_content_type='application/octet-stream',
    )
Beispiel #19
0
def populate_subtask_deadline_field(apps, _schema_editor):
    Subtask = apps.get_model('core', 'Subtask')
    for subtask in Subtask.objects.all():
        subtask.computation_deadline = parse_timestamp_to_utc_datetime(
            deserialize_message(
                subtask.task_to_compute.data).compute_task_def['deadline'])
        subtask.full_clean()
        subtask.save()
 def test_that_exception_is_raised_when_closure_time_is_set_in_use_case_other_than_forced_payment(
         self):
     self.deposit_claim.concent_use_case = ConcentUseCase.ADDITIONAL_VERIFICATION.value
     self.deposit_claim.closure_time = parse_timestamp_to_utc_datetime(
         get_current_utc_timestamp())
     with pytest.raises(ValidationError) as exception_info:
         self.deposit_claim.clean()
     self.assertIn('closure_time', exception_info.value.error_dict)
Beispiel #21
0
def test_case_2_a_force_payment_with_subtask_result_accepted_where_ethereum_accounts_are_different(
        cluster_consts, cluster_url, test_id):
    # Test CASE 2A - Send ForcePayment with SubtaskResultsAccepted where ethereum accounts are different
    current_time = get_current_utc_timestamp()
    (task_id, subtask_id) = get_task_id_and_subtask_id(test_id, '2A')
    api_request(
        cluster_url,
        'send',
        PROVIDER_PRIVATE_KEY,
        CONCENT_PUBLIC_KEY,
        force_payment(
            timestamp=timestamp_to_isoformat(current_time),
            subtask_results_accepted_list=[
                subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    task_to_compute=create_signed_task_to_compute(
                        timestamp=parse_timestamp_to_utc_datetime(
                            current_time),
                        task_id=task_id + 'a',
                        subtask_id=subtask_id + 'A',
                        deadline=current_time,
                        price=15000,
                    )),
                subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    task_to_compute=create_signed_task_to_compute(
                        timestamp=parse_timestamp_to_utc_datetime(
                            current_time),
                        task_id=task_id + 'b',
                        subtask_id=subtask_id + 'B',
                        deadline=current_time,
                        price=15000,
                        requestor_ethereum_public_key=encode_hex(
                            b'0' * GOLEM_PUBLIC_KEY_LENGTH)))
            ]),
        headers={
            'Content-Type': 'application/octet-stream',
        },
        expected_status=200,
        expected_message_type=message.concents.ServiceRefused.TYPE,
        expected_content_type='application/octet-stream',
    )
Beispiel #22
0
def test_case_2_a_force_payment_with_subtask_result_accepted_where_ethereum_accounts_are_different(
    cluster_consts: ProtocolConstants,
    cluster_url: str,
) -> None:
    # Test CASE 2A - Send ForcePayment with SubtaskResultsAccepted where ethereum accounts are different
    current_time = get_current_utc_timestamp()
    api_request(
        cluster_url,
        'send',
        PROVIDER_PRIVATE_KEY,
        CONCENT_PUBLIC_KEY,
        force_payment(
            timestamp=timestamp_to_isoformat(current_time),
            subtask_results_accepted_list=[
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=15000,
                        ))),
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=15000,
                            requestor_ethereum_public_key=
                            DIFFERENT_REQUESTOR_ETHEREUM_PUBLIC_KEY,
                            requestor_ethereum_private_key=
                            DIFFERENT_REQUESTOR_ETHEREUM_PRIVATE_KEY,
                        )))
            ]),
        expected_status=200,
        expected_message_type=message.concents.ServiceRefused,
        expected_content_type='application/octet-stream',
    )
Beispiel #23
0
def test_case_2c_send_force_payment_with_no_value_to_be_paid(
        cluster_consts, cluster_url, test_id):
    #  Test CASE 2C - Send ForcePayment with no value to be paid
    current_time = get_current_utc_timestamp()
    (task_id, subtask_id) = get_task_id_and_subtask_id(test_id, '2C')
    api_request(
        cluster_url,
        'send',
        PROVIDER_PRIVATE_KEY,
        CONCENT_PUBLIC_KEY,
        force_payment(
            timestamp=timestamp_to_isoformat(current_time),
            subtask_results_accepted_list=[
                subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    task_to_compute=create_signed_task_to_compute(
                        timestamp=parse_timestamp_to_utc_datetime(
                            current_time),
                        task_id=task_id + 'a',
                        subtask_id=subtask_id + 'A',
                        deadline=current_time,
                        price=0,
                    )),
                subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    task_to_compute=create_signed_task_to_compute(
                        timestamp=parse_timestamp_to_utc_datetime(
                            current_time),
                        task_id=task_id + 'b',
                        subtask_id=subtask_id + 'B',
                        deadline=current_time,
                        price=0,
                    ))
            ]),
        headers={
            'Content-Type': 'application/octet-stream',
        },
        expected_status=200,
        expected_message_type=message.concents.ForcePaymentRejected.TYPE,
        expected_content_type='application/octet-stream',
    )
Beispiel #24
0
 def _prepare_file_transfer_token(self, subtask_results_verify):
     date_time = parse_timestamp_to_utc_datetime(
         subtask_results_verify.timestamp)
     with freeze_time(date_time):
         file_transfer_token = create_file_transfer_token_for_verification_use_case(
             subtask_results_verify,
             self.PROVIDER_PUBLIC_KEY,
         )
     return file_transfer_token
def switch_stored_message_timestamp_to_message_creation_time(apps, _schema_editor):
    StoredMessage = apps.get_model('core', 'StoredMessage')

    for stored_message in StoredMessage.objects.all():
        stored_message.timestamp = parse_timestamp_to_utc_datetime(
            deserialize_message(stored_message.data.tobytes()).timestamp
        )
        stored_message.full_clean()
        stored_message.save()
Beispiel #26
0
 def _get_verification_deadline_as_datetime(
     self,
     subtask_results_rejected_timestamp: int,
     task_to_compute: message.tasks.TaskToCompute,
 ) -> datetime.datetime:
     return parse_timestamp_to_utc_datetime(
         self._get_verification_deadline_as_timestamp(
             subtask_results_rejected_timestamp,
             task_to_compute,
         ))
Beispiel #27
0
def update_subtask_state(subtask: Subtask, state: str, next_deadline: Union[int, float, None] = None) -> None:
    old_state = subtask.state
    subtask.state = state
    subtask.next_deadline = None if next_deadline is None else parse_timestamp_to_utc_datetime(next_deadline)
    subtask.full_clean()
    subtask.save()

    log(
        logger,
        f'Subtask changed its state from {old_state} to {subtask.state}',
        subtask_id=subtask.subtask_id
    )
    def test_that_verification_result_mismatch_should_add_pending_messages_subtask_results_rejected(self):
        with freeze_time(parse_timestamp_to_utc_datetime(get_current_utc_timestamp())):
            verification_result(  # pylint: disable=no-value-for-parameter
                self.subtask.subtask_id,
                VerificationResult.MISMATCH.name,
            )

        self.subtask.refresh_from_db()
        self.assertEqual(self.subtask.state_enum, Subtask.SubtaskState.FAILED)
        self.assertEqual(self.subtask.next_deadline, None)
        self.assertEqual(PendingResponse.objects.count(), 2)
        self.assertTrue(PendingResponse.objects.filter(client=self.subtask.provider).exists())
        self.assertTrue(PendingResponse.objects.filter(client=self.subtask.requestor).exists())
Beispiel #29
0
def test_case_2b_send_force_payment_beyond_payment_time(
        cluster_consts: ProtocolConstants, cluster_url: str) -> None:
    #  Test CASE 2B - Send ForcePayment beyond payment time
    current_time = get_current_utc_timestamp()
    api_request(
        cluster_url,
        'send',
        PROVIDER_PRIVATE_KEY,
        CONCENT_PUBLIC_KEY,
        force_payment(
            timestamp=timestamp_to_isoformat(current_time),
            subtask_results_accepted_list=[
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=15000,
                        ))),
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=15000,
                        )))
            ]),
        expected_status=200,
        expected_message_type=message.concents.ForcePaymentRejected,
        expected_content_type='application/octet-stream',
    )
Beispiel #30
0
def test_case_2c_send_force_payment_with_no_value_to_be_paid(
        cluster_consts: ProtocolConstants, cluster_url: str) -> None:
    #  Test CASE 2C - Send ForcePayment with no value to be paid
    current_time = get_current_utc_timestamp()
    api_request(
        cluster_url,
        'send',
        PROVIDER_PRIVATE_KEY,
        CONCENT_PUBLIC_KEY,
        force_payment(
            timestamp=timestamp_to_isoformat(current_time),
            subtask_results_accepted_list=[
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=0,
                        ))),
                create_signed_subtask_results_accepted(
                    timestamp=timestamp_to_isoformat(current_time),
                    payment_ts=current_time - cluster_consts.payment_due_time -
                    AVERAGE_TIME_FOR_TWO_BLOCKS,
                    report_computed_task=create_signed_report_computed_task(
                        task_to_compute=create_signed_task_to_compute(
                            timestamp=parse_timestamp_to_utc_datetime(
                                current_time),
                            deadline=current_time,
                            price=0,
                        )))
            ]),
        expected_status=400,
        expected_error_code='message.value_negative',
    )