Example #1
0
    def setUp(self):
        super().setUp()
        self.compute_task_def = ComputeTaskDefFactory()
        self.compute_task_def['deadline'] = get_current_utc_timestamp() - 60
        self.want_to_compute_task = WantToComputeTaskFactory(
            node_name=1,
            task_id=self._get_uuid(),
            perf_index=3,
            price=4,
            max_resource_size=5,
            max_memory_size=6,
            num_cores=7,
        )
        self.task_to_compute = tasks.TaskToComputeFactory(
            compute_task_def=self.compute_task_def,
            want_to_compute_task=self.want_to_compute_task,
            provider_public_key=self._get_provider_hex_public_key(),
            requestor_public_key=self._get_requestor_hex_public_key(),
        )
        self.size = 58

        with freeze_time("2017-11-17 10:00:00"):
            self.report_computed_task = message.tasks.ReportComputedTask(
                task_to_compute=self.task_to_compute,
                size=self.size
            )
            self.force_golem_data = message.concents.ForceReportComputedTask(
                report_computed_task=self.report_computed_task
            )
        message_timestamp = parse_timestamp_to_utc_datetime(get_current_utc_timestamp())
        new_message = StoredMessage(
            type=self.force_golem_data.report_computed_task.header.type_,
            timestamp=message_timestamp,
            data=self.force_golem_data.report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        new_message.full_clean()
        new_message.save()

        want_to_compute_message = StoredMessage(
            type=self.want_to_compute_task.header.type_,
            timestamp=message_timestamp,
            data=self.want_to_compute_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        want_to_compute_message.full_clean()
        want_to_compute_message.save()

        task_to_compute_message = StoredMessage(
            type=self.task_to_compute.header.type_,
            timestamp=message_timestamp,
            data=self.task_to_compute.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        task_to_compute_message.full_clean()
        task_to_compute_message.save()

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

        stored_ack_report_computed_task = StoredMessage(
            type=ack_report_computed_task.header.type_,
            timestamp=message_timestamp,
            data=ack_report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        stored_ack_report_computed_task.full_clean()
        stored_ack_report_computed_task.save()

        client_provider = Client(
            public_key_bytes=self.PROVIDER_PUBLIC_KEY
        )
        client_provider.full_clean()
        client_provider.save()

        client_requestor = Client(
            public_key_bytes=self.REQUESTOR_PUBLIC_KEY
        )
        client_requestor.full_clean()
        client_requestor.save()

        subtask = Subtask(
            task_id                  = self.compute_task_def['task_id'],
            subtask_id               = self.compute_task_def['subtask_id'],
            report_computed_task     = new_message,
            task_to_compute          = task_to_compute_message,
            want_to_compute_task=want_to_compute_message,
            ack_report_computed_task = stored_ack_report_computed_task,
            state                    = Subtask.SubtaskState.REPORTED.name,  # pylint: disable=no-member
            provider                 = client_provider,
            requestor                = client_requestor,
            result_package_size=self.size,
            computation_deadline=parse_timestamp_to_utc_datetime(self.compute_task_def['deadline'])
        )
        subtask.full_clean()
        subtask.save()

        new_message_inbox = PendingResponse(
            response_type=PendingResponse.ResponseType.ForceReportComputedTask.name,  # pylint: disable=no-member
            client=client_requestor,
            queue=PendingResponse.Queue.ReceiveOutOfBand.name,  # pylint: disable=no-member
            subtask=subtask,
        )
        new_message_inbox.full_clean()
        new_message_inbox.save()
Example #2
0
class SettleOverdueAcceptancesBanksterTest(ConcentIntegrationTestCase):
    def setUp(self):
        super().setUp()
        self.get_deposit_value_return_value_default = 15000
        self.task_to_compute = None
        self.subtask_results_accepted_list = None
        self.requestor_client = None
        self.requestor_deposit_account = None
        self.validate_list_of_transaction_mock = None
        self.get_deposit_value_mock = None
        self.get_list_of_payments_mock = None

    def create_subtask_results_accepted_list(self, price, number_of_items=1):
        self.task_to_compute = self._get_deserialized_task_to_compute(
            price=price)
        self.subtask_results_accepted_list = [
            self._get_deserialized_subtask_results_accepted(
                report_computed_task=self.
                _get_deserialized_report_computed_task(
                    timestamp="2018-02-04 10:00:05",
                    task_to_compute=self.task_to_compute))
            for _ in range(number_of_items)
        ]

    def create_client_and_related_deposit_account(self):
        self.requestor_client = Client(
            public_key=self.task_to_compute.requestor_id)
        self.requestor_client.full_clean()
        self.requestor_client.save()

        self.requestor_deposit_account = DepositAccount(
            client=self.requestor_client,
            ethereum_address=self.task_to_compute.requestor_ethereum_address,
        )
        self.requestor_deposit_account.full_clean()
        self.requestor_deposit_account.save()

    def create_deposit_claim(self, amount, tx_hash):
        deposit_claim = DepositClaim(
            payee_ethereum_address=self.task_to_compute.
            provider_ethereum_address,
            payer_deposit_account=self.requestor_deposit_account,
            amount=amount,
            concent_use_case=ConcentUseCase.FORCED_PAYMENT,
            tx_hash=tx_hash,
            closure_time=parse_timestamp_to_utc_datetime(
                get_current_utc_timestamp()),
        )
        deposit_claim.full_clean()
        deposit_claim.save()

    def call_settle_overdue_acceptances_with_mocked_sci_functions(
        self,
        get_deposit_value_return_value=None,
        get_list_of_payments_return_value=None,
    ):
        with freeze_time("2018-02-05 10:00:25"):
            with mock.patch(
                    'core.payments.bankster.validate_list_of_transaction_timestamp'
            ) as self.validate_list_of_transaction_mock:
                with mock.patch(
                        'core.payments.bankster.service.get_deposit_value',
                        return_value=(
                            get_deposit_value_return_value
                            if get_deposit_value_return_value is not None else
                            self.get_deposit_value_return_value_default),
                ) as self.get_deposit_value_mock:
                    with mock.patch(
                            'core.payments.bankster.service.get_list_of_payments',
                            side_effect=[
                                (get_list_of_payments_return_value
                                 if get_list_of_payments_return_value
                                 is not None else
                                 self._get_list_of_settlement_transactions()),
                                self._get_list_of_batch_transactions()
                            ],
                    ) as self.get_list_of_payments_mock:
                        claim_against_requestor = settle_overdue_acceptances(
                            requestor_ethereum_address=self.task_to_compute.
                            requestor_ethereum_address,
                            provider_ethereum_address=self.task_to_compute.
                            provider_ethereum_address,
                            acceptances=self.subtask_results_accepted_list,
                            requestor_public_key=hex_to_bytes_convert(
                                self.task_to_compute.requestor_public_key),
                        )

        return claim_against_requestor

    def assert_mocked_sci_functions_were_called(
            self, get_list_of_payments_call_count=2):
        self.get_deposit_value_mock.assert_called_once()
        self.validate_list_of_transaction_mock.assert_called_once()
        self.assertEqual(self.get_list_of_payments_mock.call_count,
                         get_list_of_payments_call_count)

    def test_that_settle_overdue_acceptances_should_raise_no_unsettled_tasks_exception_when_all_tasks_are_paid_off(
            self):
        """
        In this test we have following calculations:

        TaskToCompute price:                                     3000
        Sum of amounts from list of settlement transactions:     3000
        Sum of amounts from list of transactions:                4000 (does not matter in this case)
        Requestor deposit value:                                15000 (does not matter in this case)

        Amount pending = 3000 - 3000 = 0
        """
        self.create_subtask_results_accepted_list(price=3000)

        with self.assertRaises(BanksterNoUnsettledTasksError):
            self.call_settle_overdue_acceptances_with_mocked_sci_functions()

        self.assert_mocked_sci_functions_were_called()

    def test_that_settle_overdue_acceptances_should_raise_too_small_requestor_deposit_exception_when_requestor_has_insufficient_funds(
            self):
        """
        In this test we have following calculations
        (functions are mocked, but values are set to better understand calculations):

        TaskToCompute price:                                    12000
        Sum of amounts from list of settlement transactions:    3000
        Sum of amounts from list of transactions:               4000
        Requestor deposit value:                                0
        """

        subtasks_collective_price = 12000
        already_paid_in_transactions = 3000 + 4000

        self.create_subtask_results_accepted_list(
            price=subtasks_collective_price)

        with mock.patch(
                'core.payments.bankster.find_unconfirmed_settlement_payments'):
            with mock.patch('core.payments.bankster.get_provider_payment_info',
                            return_value=(already_paid_in_transactions,
                                          subtasks_collective_price -
                                          already_paid_in_transactions)):
                with self.assertRaises(BanksterTooSmallRequestorDepositError):
                    self.call_settle_overdue_acceptances_with_mocked_sci_functions(
                        get_deposit_value_return_value=0, )

        self.assert_mocked_sci_functions_were_called()

    def test_that_settle_overdue_acceptances_should_return_claim_deposit_with_amount_paid(
            self):
        """
        In this test we have following calculations:

        TaskToCompute price:                                    15000
        Sum of amounts from list of settlement transactions:     3000
        Sum of amounts from list of transactions:                4000
        Requestor deposit value:                                15000

        Amount pending = 15000 - (3000 + 4000) = 8000
        Payable amount = min(15000, 8000) = 8000
        """
        self.create_subtask_results_accepted_list(price=15000)

        claim_against_requestor = self.call_settle_overdue_acceptances_with_mocked_sci_functions(
        )

        self.assertIsNotNone(claim_against_requestor.tx_hash)
        self.assertEqual(claim_against_requestor.amount, 8000)
        self.assert_mocked_sci_functions_were_called()

    def test_that_settle_overdue_acceptances_should_return_claim_deposit_with_amount_paid_if_there_was_no_previous_settlement_transactions(
            self):
        """
        In this test we have following calculations:

        TaskToCompute price:                                    15000
        Sum of amounts from list of settlement transactions:        0
        Sum of amounts from list of transactions:                4000
        Requestor deposit value:                                20000

        Amount pending = 15000 - 4000 = 11000
        Payable amount = min(11000, 20000) = 11000
        """
        self.create_subtask_results_accepted_list(price=15000)

        claim_against_requestor = self.call_settle_overdue_acceptances_with_mocked_sci_functions(
            get_deposit_value_return_value=20000,
            get_list_of_payments_return_value=[],
        )

        self.assertIsNotNone(claim_against_requestor.tx_hash)
        self.assertEqual(claim_against_requestor.amount, 11000)
        self.assert_mocked_sci_functions_were_called()

    def test_that_settle_overdue_acceptances_should_return_claim_deposit_with_amount_paid_when_requesting_payment_for_multiple_subtasks(
            self):
        """
        In this test we have following calculations:

        TaskToCompute price:                         2 x 7500 = 15000
        Sum of amounts from list of settlement transactions:     3000
        Sum of amounts from list of transactions:                4000
        Requestor deposit value:                                15000

        Amount pending = 15000 - (3000 + 4000) = 8000
        Payable amount = min(8000, 15000) = 8000
        """
        self.create_subtask_results_accepted_list(price=7500,
                                                  number_of_items=2)

        claim_against_requestor = self.call_settle_overdue_acceptances_with_mocked_sci_functions(
        )

        self.assertIsNotNone(claim_against_requestor.tx_hash)
        self.assertEqual(claim_against_requestor.amount, 8000)
        self.assert_mocked_sci_functions_were_called()

    def test_that_settle_overdue_acceptances_should_return_claim_deposit_with_amount_available_on_requestor_deposit_if_it_is_greater_then_left_amount(
            self):
        """
        In this test we have following calculations:

        TaskToCompute price:                                    15000
        Sum of amounts from list of settlement transactions:     3000
        Sum of amounts from list of transactions:                4000
        Requestor deposit value:                                 5000

        Amount pending = 15000 - (3000 + 4000) = 8000
        Payable amount = min(8000, 5000) = 5000
        """
        self.create_subtask_results_accepted_list(price=15000)

        claim_against_requestor = self.call_settle_overdue_acceptances_with_mocked_sci_functions(
            get_deposit_value_return_value=5000, )

        self.assertIsNotNone(claim_against_requestor.tx_hash)
        self.assertEqual(claim_against_requestor.amount, 5000)
        self.assert_mocked_sci_functions_were_called()

    def test_that_settle_overdue_acceptances_should_return_claim_deposit_with_amount_paid_when_there_are_existing_claims(
            self):
        """
        In this test we have following calculations:

        TaskToCompute price:                                    15000
        Sum of amounts from list of settlement transactions:        0
        Sum of amounts from list of transactions:                4000
        Requestor deposit value:                                15000
        Sum of existing claims:                                  7000

        Amount pending = 15000 - (4000 + 7000) = 4000
        Payable amount = min(4000, 15000) = 4000
        """
        self.create_subtask_results_accepted_list(price=15000)
        self.create_client_and_related_deposit_account()
        self.create_deposit_claim(
            amount=3000,
            tx_hash=64 * 'A',
        )
        self.create_deposit_claim(
            amount=4000,
            tx_hash=64 * 'B',
        )

        claim_against_requestor = self.call_settle_overdue_acceptances_with_mocked_sci_functions(
            get_list_of_payments_return_value=[])

        self.assertIsNotNone(claim_against_requestor.tx_hash)
        self.assertEqual(claim_against_requestor.amount, 4000)
        self.assert_mocked_sci_functions_were_called()

    def test_that_settle_overdue_acceptances_should_return_claim_deposit_with_amount_paid_when_there_are_both_existing_claims_and_payments(
            self):
        """
        In this test we have following calculations:

        TaskToCompute price:                                    15000
        Sum of amounts from list of settlement transactions:     3000
        Sum of amounts from list of transactions:                4000
        Requestor deposit value:                                15000
        Sum of existing claims:                                  7000

        Amount pending = 15000 - (3000 + 4000 + 7000) = 1000
        Payable amount = min(1000, 15000) = 1000
        """
        self.create_subtask_results_accepted_list(price=15000)
        self.create_client_and_related_deposit_account()
        self.create_deposit_claim(
            amount=3000,
            tx_hash=64 * 'A',
        )
        self.create_deposit_claim(
            amount=4000,
            tx_hash=64 * 'B',
        )

        claim_against_requestor = self.call_settle_overdue_acceptances_with_mocked_sci_functions(
        )

        self.assertIsNotNone(claim_against_requestor.tx_hash)
        self.assertEqual(claim_against_requestor.amount, 1000)
        self.assert_mocked_sci_functions_were_called()

    def test_that_settle_overdue_acceptances_should_return_claim_deposit_with_amount_paid_when_there_are_both_existing_claims_and_payments_with_the_same_transaction_hash(
            self):
        """
        In this test we have following calculations:

        TaskToCompute price:                                    15000
        Sum of amounts from list of settlement transactions:     2000
        Sum of amounts from list of transactions:                4000
        Requestor deposit value:                                15000
        Sum of existing claims:                                  6000

        Amount pending = 15000 - (2000 + 4000 + 6000 - 2000) = 5000  // (2000 from claim matching blockchain transaction is ignored)
        Payable amount = min(5000, 15000) = 5000
        """
        self.create_subtask_results_accepted_list(price=15000)
        self.create_client_and_related_deposit_account()

        with freeze_time("2018-02-05 10:00:25"):
            self.create_deposit_claim(
                amount=2000,
                tx_hash=MOCK_TRANSACTION_HASH,
            )

        self.create_deposit_claim(
            amount=4000,
            tx_hash=64 * 'B',
        )

        with freeze_time("2018-02-05 10:00:25"):
            list_of_payments_return_value = [
                self._create_settlement_payment_object(amount=2000, )
            ]

        claim_against_requestor = self.call_settle_overdue_acceptances_with_mocked_sci_functions(
            get_list_of_payments_return_value=list_of_payments_return_value, )

        self.assertIsNotNone(claim_against_requestor.tx_hash)
        self.assertEqual(claim_against_requestor.amount, 5000)
        self.assert_mocked_sci_functions_were_called()

    def test_that_settle_overdue_acceptances_should_raise_exception_if_transaction_from_blockchain_will_not_match_database_claim(
            self):
        """
        In this test we have following calculations:

        TaskToCompute price:                                    15000
        Sum of amounts from list of settlement transactions:     3000
        Sum of amounts from list of transactions:                4000
        Requestor deposit value:                                15000
        Sum of existing claims:                                  7000
        """
        self.create_subtask_results_accepted_list(price=15000)
        self.create_client_and_related_deposit_account()

        self.create_deposit_claim(
            amount=3000,
            tx_hash=MOCK_TRANSACTION_HASH,
        )

        with self.assertRaises(BanksterTransactionMismatchError):
            self.call_settle_overdue_acceptances_with_mocked_sci_functions()

        self.assert_mocked_sci_functions_were_called(
            get_list_of_payments_call_count=1)
Example #3
0
    def test_receive_should_return_first_messages_in_order_they_were_added_to_queue_if_the_receive_queue_contains_only_force_report_and_its_past_deadline(self):
        message_timestamp = parse_timestamp_to_utc_datetime(get_current_utc_timestamp())
        new_message = StoredMessage(
            type=self.force_golem_data.report_computed_task.header.type_,
            timestamp=message_timestamp,
            data=self.force_golem_data.report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        new_message.full_clean()
        new_message.save()

        want_to_compute_message = StoredMessage(
            type=self.want_to_compute_task.header.type_,
            timestamp=message_timestamp,
            data=self.want_to_compute_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        want_to_compute_message.full_clean()
        want_to_compute_message.save()

        task_to_compute_message = StoredMessage(
            type=self.task_to_compute.header.type_,
            timestamp=message_timestamp,
            data=self.task_to_compute.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        task_to_compute_message.full_clean()
        task_to_compute_message.save()

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

        stored_ack_report_computed_task = StoredMessage(
            type=ack_report_computed_task.header.type_,
            timestamp=message_timestamp,
            data=ack_report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        stored_ack_report_computed_task.full_clean()
        stored_ack_report_computed_task.save()

        client_provider = Client(
            public_key_bytes=self.PROVIDER_PUBLIC_KEY
        )
        client_provider.full_clean()
        client_provider.save()

        client_requestor = Client(
            public_key_bytes=self.REQUESTOR_PUBLIC_KEY
        )
        client_requestor.full_clean()
        client_requestor.save()

        subtask = Subtask(
            task_id                  = self.compute_task_def['task_id'],
            subtask_id               = self.compute_task_def['subtask_id'],
            report_computed_task     = new_message,
            task_to_compute          = task_to_compute_message,
            want_to_compute_task=want_to_compute_message,
            ack_report_computed_task = stored_ack_report_computed_task,
            state                    = Subtask.SubtaskState.REPORTED.name,  # pylint: disable=no-member
            provider                 = client_provider,
            requestor                = client_requestor,
            result_package_size=self.size,
            computation_deadline=parse_timestamp_to_utc_datetime(self.compute_task_def['deadline'])
        )
        subtask.full_clean()
        subtask.save()

        new_message_inbox = PendingResponse(
            response_type=PendingResponse.ResponseType.ForceReportComputedTask.name,  # pylint: disable=no-member
            client=client_requestor,
            queue=PendingResponse.Queue.Receive.name,  # pylint: disable=no-member
            subtask=subtask,
        )
        new_message_inbox.full_clean()
        new_message_inbox.save()

        new_message_inbox_out_of_band = PendingResponse(
            response_type=PendingResponse.ResponseType.VerdictReportComputedTask.name,  # pylint: disable=no-member
            client=client_requestor,
            queue=PendingResponse.Queue.ReceiveOutOfBand.name,  # pylint: disable=no-member
            subtask=subtask,
        )
        new_message_inbox_out_of_band.full_clean()
        new_message_inbox_out_of_band.save()

        with freeze_time("2017-11-17 12:00:00"):
            response = self.send_request(
                url='core:receive',
                data=self._create_client_auth_message(self.REQUESTOR_PRIVATE_KEY, self.REQUESTOR_PUBLIC_KEY),
            )

        decoded_message = load(
            response.content,
            self.REQUESTOR_PRIVATE_KEY,
            CONCENT_PUBLIC_KEY,
            check_time=False,
        )

        self.assertIsInstance(decoded_message, message.concents.ForceReportComputedTask)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(decoded_message.timestamp, self._create_timestamp_from_string("2017-11-17 12:00:00"))
        self.assertEqual(decoded_message.report_computed_task.task_to_compute.compute_task_def,
                         self.task_to_compute.compute_task_def)  # pylint: disable=no-member
        self.assertEqual(decoded_message.report_computed_task.task_to_compute.sig, self.task_to_compute.sig)

        with freeze_time("2017-11-17 12:00:00"):
            response = self.send_request(
                url='core:receive',
                data=self._create_client_auth_message(self.REQUESTOR_PRIVATE_KEY, self.REQUESTOR_PUBLIC_KEY),
            )

        decoded_message = load(
            response.content,
            self.REQUESTOR_PRIVATE_KEY,
            CONCENT_PUBLIC_KEY,
            check_time=False,
        )

        self.assertIsInstance(decoded_message, message.concents.VerdictReportComputedTask)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(decoded_message.timestamp, self._create_timestamp_from_string("2017-11-17 12:00:00"))
        self.assertEqual(decoded_message.ack_report_computed_task.report_computed_task.task_to_compute.compute_task_def,
                         self.task_to_compute.compute_task_def)  # pylint: disable=no-member
        self.assertEqual(decoded_message.ack_report_computed_task.report_computed_task.task_to_compute.sig,
                         self.task_to_compute.sig)
Example #4
0
    def test_receive_should_accept_valid_message(self):
        message_timestamp = parse_timestamp_to_utc_datetime(get_current_utc_timestamp())
        new_message = StoredMessage(
            type=self.force_golem_data.report_computed_task.header.type_,
            timestamp=message_timestamp,
            data=self.force_golem_data.report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        new_message.full_clean()
        new_message.save()

        client_provider = Client(
            public_key_bytes=self.PROVIDER_PUBLIC_KEY
        )
        client_provider.full_clean()
        client_provider.save()

        client_requestor = Client(
            public_key_bytes=self.REQUESTOR_PUBLIC_KEY
        )
        client_requestor.full_clean()
        client_requestor.save()

        want_to_compute_message = StoredMessage(
            type=self.want_to_compute_task.header.type_,
            timestamp=message_timestamp,
            data=self.want_to_compute_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        want_to_compute_message.full_clean()
        want_to_compute_message.save()

        task_to_compute_message = StoredMessage(
            type=self.task_to_compute.header.type_,
            timestamp=message_timestamp,
            data=self.task_to_compute.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
            protocol_version=settings.GOLEM_MESSAGES_VERSION,
        )
        task_to_compute_message.full_clean()
        task_to_compute_message.save()

        subtask = Subtask(
            task_id                 = self.compute_task_def['task_id'],
            subtask_id              = self.compute_task_def['subtask_id'],
            task_to_compute         = task_to_compute_message,
            want_to_compute_task=want_to_compute_message,
            report_computed_task    = new_message,
            state                   = Subtask.SubtaskState.REPORTED.name,  # pylint: disable=no-member
            provider                = client_provider,
            requestor               = client_requestor,
            result_package_size=self.size,
            computation_deadline=parse_timestamp_to_utc_datetime(self.compute_task_def['deadline'])
        )
        subtask.full_clean()
        subtask.save()

        new_message_inbox = PendingResponse(
            response_type=PendingResponse.ResponseType.ForceReportComputedTask.name,  # pylint: disable=no-member
            client=client_requestor,
            queue=PendingResponse.Queue.Receive.name,  # pylint: disable=no-member
            subtask=subtask,
        )
        new_message_inbox.full_clean()
        new_message_inbox.save()

        response = self.send_request(
            url='core:receive',
            data=self._create_client_auth_message(self.REQUESTOR_PRIVATE_KEY, self.REQUESTOR_PUBLIC_KEY),
        )
        decoded_response = load(
            response.content,
            self.REQUESTOR_PRIVATE_KEY,
            CONCENT_PUBLIC_KEY,
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(new_message.task_id,
                         decoded_response.report_computed_task.task_to_compute.compute_task_def['task_id'])
        self.assertEqual(new_message.subtask_id,
                         decoded_response.report_computed_task.task_to_compute.compute_task_def['subtask_id'])
Example #5
0
    def setUp(self):
        super().setUp()
        self.compute_task_def = message.ComputeTaskDef()
        self.compute_task_def['task_id'] = '1'
        self.compute_task_def['subtask_id'] = '1'
        self.compute_task_def['deadline'] = get_current_utc_timestamp() - 60
        self.task_to_compute = tasks.TaskToComputeFactory(
            compute_task_def=self.compute_task_def,
            provider_public_key=PROVIDER_PUBLIC_KEY,
            requestor_public_key=REQUESTOR_PUBLIC_KEY,
            price=0,
        )
        self.size = 58

        with freeze_time("2017-11-17 10:00:00"):
            self.force_golem_data = message.ForceReportComputedTask(
                report_computed_task=message.tasks.ReportComputedTask(
                    task_to_compute=self.task_to_compute, size=self.size))
        message_timestamp = datetime.datetime.now(timezone.utc)
        new_message = StoredMessage(
            type=self.force_golem_data.report_computed_task.TYPE,
            timestamp=message_timestamp,
            data=self.force_golem_data.report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
        )
        new_message.full_clean()
        new_message.save()

        task_to_compute_message = StoredMessage(
            type=self.task_to_compute.TYPE,
            timestamp=message_timestamp,
            data=self.task_to_compute.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
        )
        task_to_compute_message.full_clean()
        task_to_compute_message.save()

        ack_report_computed_task = message.AckReportComputedTask(
            report_computed_task=message.tasks.ReportComputedTask(
                task_to_compute=self.task_to_compute, ))

        stored_ack_report_computed_task = StoredMessage(
            type=ack_report_computed_task.TYPE,
            timestamp=message_timestamp,
            data=ack_report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
        )
        stored_ack_report_computed_task.full_clean()
        stored_ack_report_computed_task.save()

        client_provider = Client(public_key_bytes=PROVIDER_PUBLIC_KEY)
        client_provider.full_clean()
        client_provider.save()

        client_requestor = Client(public_key_bytes=REQUESTOR_PUBLIC_KEY)
        client_requestor.full_clean()
        client_requestor.save()

        subtask = Subtask(
            task_id=self.compute_task_def['task_id'],
            subtask_id=self.compute_task_def['subtask_id'],
            report_computed_task=new_message,
            task_to_compute=task_to_compute_message,
            ack_report_computed_task=stored_ack_report_computed_task,
            state=Subtask.SubtaskState.REPORTED.name,  # pylint: disable=no-member
            provider=client_provider,
            requestor=client_requestor,
            result_package_size=self.size,
            computation_deadline=parse_timestamp_to_utc_datetime(
                self.compute_task_def['deadline']))
        subtask.full_clean()
        subtask.save()

        new_message_inbox = PendingResponse(
            response_type=PendingResponse.ResponseType.ForceReportComputedTask.
            name,  # pylint: disable=no-member
            client=client_requestor,
            queue=PendingResponse.Queue.ReceiveOutOfBand.name,  # pylint: disable=no-member
            subtask=subtask,
        )
        new_message_inbox.full_clean()
        new_message_inbox.save()
Example #6
0
    def test_receive_should_return_first_messages_in_order_they_were_added_to_queue_if_the_receive_queue_contains_only_force_report_and_its_past_deadline(
            self):
        message_timestamp = datetime.datetime.now(timezone.utc)
        new_message = StoredMessage(
            type=self.force_golem_data.report_computed_task.TYPE,
            timestamp=message_timestamp,
            data=self.force_golem_data.report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
        )
        new_message.full_clean()
        new_message.save()

        task_to_compute_message = StoredMessage(
            type=self.task_to_compute.TYPE,
            timestamp=message_timestamp,
            data=self.task_to_compute.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
        )
        task_to_compute_message.full_clean()
        task_to_compute_message.save()

        ack_report_computed_task = message.AckReportComputedTask(
            report_computed_task=message.ReportComputedTask(
                task_to_compute=self.task_to_compute, ))

        stored_ack_report_computed_task = StoredMessage(
            type=ack_report_computed_task.TYPE,
            timestamp=message_timestamp,
            data=ack_report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
        )
        stored_ack_report_computed_task.full_clean()
        stored_ack_report_computed_task.save()

        client_provider = Client(public_key_bytes=PROVIDER_PUBLIC_KEY)
        client_provider.full_clean()
        client_provider.save()

        client_requestor = Client(public_key_bytes=REQUESTOR_PUBLIC_KEY)
        client_requestor.full_clean()
        client_requestor.save()

        subtask = Subtask(
            task_id=self.compute_task_def['task_id'],
            subtask_id=self.compute_task_def['subtask_id'],
            report_computed_task=new_message,
            task_to_compute=task_to_compute_message,
            ack_report_computed_task=stored_ack_report_computed_task,
            state=Subtask.SubtaskState.REPORTED.name,  # pylint: disable=no-member
            provider=client_provider,
            requestor=client_requestor,
            result_package_size=self.size,
            computation_deadline=parse_timestamp_to_utc_datetime(
                self.compute_task_def['deadline']))
        subtask.full_clean()
        subtask.save()

        new_message_inbox = PendingResponse(
            response_type=PendingResponse.ResponseType.ForceReportComputedTask.
            name,  # pylint: disable=no-member
            client=client_requestor,
            queue=PendingResponse.Queue.Receive.name,  # pylint: disable=no-member
            subtask=subtask,
        )
        new_message_inbox.full_clean()
        new_message_inbox.save()

        new_message_inbox_out_of_band = PendingResponse(
            response_type=PendingResponse.ResponseType.
            VerdictReportComputedTask.name,  # pylint: disable=no-member
            client=client_requestor,
            queue=PendingResponse.Queue.ReceiveOutOfBand.name,  # pylint: disable=no-member
            subtask=subtask,
        )
        new_message_inbox_out_of_band.full_clean()
        new_message_inbox_out_of_band.save()

        with freeze_time("2017-11-17 12:00:00"):
            response = self.client.post(
                reverse('core:receive'),
                content_type='application/octet-stream',
                data=self._create_client_auth_message(REQUESTOR_PRIVATE_KEY,
                                                      REQUESTOR_PUBLIC_KEY),
            )

        decoded_message = load(
            response.content,
            REQUESTOR_PRIVATE_KEY,
            CONCENT_PUBLIC_KEY,
            check_time=False,
        )

        self.assertIsInstance(decoded_message,
                              message.concents.ForceReportComputedTask)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            decoded_message.timestamp,
            int(dateutil.parser.parse("2017-11-17 12:00:00").timestamp()))
        self.assertEqual(decoded_message.report_computed_task.task_to_compute.
                         compute_task_def,
                         self.task_to_compute.compute_task_def)  # pylint: disable=no-member
        self.assertEqual(
            decoded_message.report_computed_task.task_to_compute.sig,
            self.task_to_compute.sig)

        with freeze_time("2017-11-17 12:00:00"):
            response = self.client.post(
                reverse('core:receive_out_of_band'),
                content_type='application/octet-stream',
                data=self._create_client_auth_message(REQUESTOR_PRIVATE_KEY,
                                                      REQUESTOR_PUBLIC_KEY),
            )

        decoded_message = load(
            response.content,
            REQUESTOR_PRIVATE_KEY,
            CONCENT_PUBLIC_KEY,
            check_time=False,
        )

        self.assertIsInstance(decoded_message,
                              message.concents.VerdictReportComputedTask)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            decoded_message.timestamp,
            int(dateutil.parser.parse("2017-11-17 12:00:00").timestamp()))
        self.assertEqual(decoded_message.ack_report_computed_task.
                         report_computed_task.task_to_compute.compute_task_def,
                         self.task_to_compute.compute_task_def)  # pylint: disable=no-member
        self.assertEqual(
            decoded_message.ack_report_computed_task.report_computed_task.
            task_to_compute.sig, self.task_to_compute.sig)
Example #7
0
    def test_receive_should_accept_valid_message(self):
        message_timestamp = datetime.datetime.now(timezone.utc)
        new_message = StoredMessage(
            type=self.force_golem_data.report_computed_task.TYPE,
            timestamp=message_timestamp,
            data=self.force_golem_data.report_computed_task.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
        )
        new_message.full_clean()
        new_message.save()

        client_provider = Client(public_key_bytes=PROVIDER_PUBLIC_KEY)
        client_provider.full_clean()
        client_provider.save()

        client_requestor = Client(public_key_bytes=REQUESTOR_PUBLIC_KEY)
        client_requestor.full_clean()
        client_requestor.save()

        task_to_compute_message = StoredMessage(
            type=self.task_to_compute.TYPE,
            timestamp=message_timestamp,
            data=self.task_to_compute.serialize(),
            task_id=self.compute_task_def['task_id'],  # pylint: disable=no-member
            subtask_id=self.compute_task_def['subtask_id'],  # pylint: disable=no-member
        )
        task_to_compute_message.full_clean()
        task_to_compute_message.save()

        subtask = Subtask(
            task_id=self.compute_task_def['task_id'],
            subtask_id=self.compute_task_def['subtask_id'],
            task_to_compute=task_to_compute_message,
            report_computed_task=new_message,
            state=Subtask.SubtaskState.REPORTED.name,  # pylint: disable=no-member
            provider=client_provider,
            requestor=client_requestor,
            result_package_size=self.size,
            computation_deadline=parse_timestamp_to_utc_datetime(
                self.compute_task_def['deadline']))
        subtask.full_clean()
        subtask.save()

        new_message_inbox = PendingResponse(
            response_type=PendingResponse.ResponseType.ForceReportComputedTask.
            name,  # pylint: disable=no-member
            client=client_requestor,
            queue=PendingResponse.Queue.Receive.name,  # pylint: disable=no-member
            subtask=subtask,
        )
        new_message_inbox.full_clean()
        new_message_inbox.save()

        response = self.client.post(
            reverse('core:receive'),
            content_type='application/octet-stream',
            data=self._create_client_auth_message(REQUESTOR_PRIVATE_KEY,
                                                  REQUESTOR_PUBLIC_KEY),
        )
        decoded_response = load(
            response.content,
            REQUESTOR_PRIVATE_KEY,
            CONCENT_PUBLIC_KEY,
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            new_message.task_id, decoded_response.report_computed_task.
            task_to_compute.compute_task_def['task_id'])
        self.assertEqual(
            new_message.subtask_id, decoded_response.report_computed_task.
            task_to_compute.compute_task_def['subtask_id'])