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 = maximum_download_time(size=report_computed_task.size, ) ttc_dt = datetime.datetime.utcfromtimestamp( report_computed_task.task_to_compute.timestamp, ) subtask_dt = datetime.datetime.utcfromtimestamp( 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.total_seconds()) + (0.5 * subtask_timeout.total_seconds())) else: return int( subtask_verification_time(report_computed_task).total_seconds())
def prepare_report_computed_task(self, mode, **kwargs): """Returns ReportComputedTask with open force acceptance window Can be modified by delta """ report_computed_task = msg_factories.tasks.ReportComputedTaskFactory( **self.gen_rtc_kwargs(), **self.gen_ttc_kwargs('task_to_compute__'), **kwargs, ) # Difference between timestamp and deadline has to be constant # because it's part of SVT formula deadline_delta = 3600 deadline_timedelta = datetime.timedelta(seconds=deadline_delta) report_computed_task.task_to_compute.compute_task_def['deadline'] = \ report_computed_task.task_to_compute.timestamp + deadline_delta svt = helpers.subtask_verification_time(report_computed_task) now = datetime.datetime.utcnow() if mode == 'before': # We're one moment before window opens ttc_dt = now - deadline_timedelta - svt + moment elif mode == 'after': # We're one moment after window closed ttc_dt = now - deadline_timedelta - svt - constants.FAT - moment else: # We're a the beginning of the window (moment after to be sure) ttc_dt = now - deadline_timedelta - svt - moment ttc_timestamp = calendar.timegm(ttc_dt.utctimetuple()) msg_factories.helpers.override_timestamp( msg=report_computed_task.task_to_compute, timestamp=ttc_timestamp, ) report_computed_task.task_to_compute.compute_task_def['deadline'] = \ report_computed_task.task_to_compute.timestamp + deadline_delta # Sign again after timestamp modification report_computed_task.task_to_compute.sig = None report_computed_task.sig = None report_computed_task.task_to_compute.sign_message( self.requestor_priv_key, ) report_computed_task.sign_message(self.provider_priv_key) self.assertTrue( report_computed_task.verify_owners( provider_public_key=self.provider_pub_key, requestor_public_key=self.requestor_pub_key, concent_public_key=self.variant['pubkey'], ), ) print('*' * 80) print('TTC:', ttc_dt) print('WINDOW: {} ---------- {}'.format( ttc_dt + deadline_timedelta + svt, ttc_dt + deadline_timedelta + svt + constants.FAT, )) print('NOW:', now) print('*' * 80) return report_computed_task
def test_that_both_subtask_verification_time_implementation_should_return_same_result_when_golem_messages_constants_match_concent_settings(self): current_time = get_current_utc_timestamp() for size, timestamp, deadline in [ (7, current_time + 7, current_time + 7), (10, current_time, current_time + 10), (19, current_time + 19, current_time + 19), (100, current_time + 100, current_time + 100), (331, current_time + 331, current_time + 331), (999, current_time + 999, current_time + 999), ]: report_computed_task = self._get_deserialized_report_computed_task( size=size, task_to_compute=self._get_deserialized_task_to_compute( timestamp=parse_timestamp_to_utc_datetime(timestamp), deadline=deadline, ) ) self.assertEqual( calculate_subtask_verification_time(report_computed_task), int(helpers.subtask_verification_time(report_computed_task).total_seconds()) )
def test_svt(self, mdt_mock): result = helpers.subtask_verification_time(self.msg) mdt_mock.assert_called_once_with(size=self.msg.size) self.assertEqual(result, datetime.timedelta(seconds=14530))