def prepare_test(self): self.__config = fetch_config(inject_contract=True) self.__audit_node = QSPAuditNode(self.__config) self.__audit_node.config._Config__upload_provider = DummyProvider() self.__mk_unique_storage_dir() # Forces analyzer wrapper to always execute their `once` script prior # to executing `run` for analyzer in self.__config.analyzers: remove(analyzer.wrapper.storage_dir + '/.once') self.__getNextAuditRequest_filter = \ self.__config.audit_contract.events.getNextAuditRequest_called.createFilter( fromBlock=max(0, self.__config.event_pool_manager.get_latest_block_number()) ) self.__submitReport_filter = \ self.__config.audit_contract.events.submitReport_called.createFilter( fromBlock=max(0, self.__config.event_pool_manager.get_latest_block_number()) ) self.__setAnyRequestAvailableResult_filter = \ self.__config.audit_contract.events.setAnyRequestAvailableResult_called.createFilter( fromBlock=max(0, self.__config.event_pool_manager.get_latest_block_number()) ) # Disables the claim rewards threading from continuously running ahead; # negate the default mocking behaviour of always having rewards # available claim_rewards_instance = ClaimRewardsThread(self.__config) claim_rewards_instance._ClaimRewardsThread__has_available_rewards = MagicMock( ) claim_rewards_instance._ClaimRewardsThread__has_available_rewards.return_value = False replace_thread(self.__audit_node, ClaimRewardsThread, claim_rewards_instance) def exec_audit_node(): self.__audit_node.start() audit_node_thread = Thread(target=exec_audit_node, name="Audit node") audit_node_thread.start() max_initialization_seconds = 5 num_checks = 0 while not self.__audit_node.is_initialized: sleep(TestQSPAuditNode.__SLEEP_INTERVAL) num_checks += 100 if num_checks == max_initialization_seconds: self.__audit_node.stop() raise Exception("Node threads could not be initialized")
def test_successful_police_audit(self): uncompressed_report = load_json( fetch_file(resource_uri("reports/DAOBug.json"))) request_id = uncompressed_report['request_id'] encoder = ReportEncoder() compressed_report = encoder.compress_report(uncompressed_report, request_id) # Creates a mocked method for retrieving the audit result from the blockchain. submit_report_instance = SubmitReportThread(self.__config) submit_report_instance._SubmitReportThread__get_report_in_blockchain = MagicMock( ) submit_report_instance._SubmitReportThread__get_report_in_blockchain.return_value = \ compressed_report replace_thread(self.__audit_node, SubmitReportThread, submit_report_instance) # Adds a police event to the database to trigger the flow of a police # check. Since no other thread should be writing to the DB at this # point, the write can be performed without a lock. poll_requests_instance = PollRequestsThread( self.__config, self.__block_mined_polling_thread) poll_requests_instance._PollRequestsThread__add_evt_to_db( request_id=request_id, requestor=self.__audit_node.config.audit_contract_address, price=100, uri=resource_uri("reports/DAOBug.json"), assigned_block_nbr=100, is_audit=False) replace_thread(self.__audit_node, PollRequestsThread, poll_requests_instance) # Disables the claim rewards threading from continuously running ahead; # negate the default mocking behaviour of always having rewards # available claim_rewards_instance = ClaimRewardsThread(self.__config) claim_rewards_instance._ClaimRewardsThread__has_available_rewards = MagicMock( ) claim_rewards_instance._ClaimRewardsThread__has_available_rewards.return_value = False replace_thread(self.__audit_node, ClaimRewardsThread, claim_rewards_instance) # Sets the node as a police officer. self.__audit_node.is_police_officer = MagicMock() self.__audit_node.is_police_officer.return_value = True # Sets the audit report value itself to be returned by the audit node. self.__audit_node.audit = MagicMock() self.__audit_node.audit.return_value = { 'audit_state': uncompressed_report['audit_state'], 'audit_uri': 'http://some-url.com', 'audit_hash': 'some-hash', 'full_report': json.dumps(uncompressed_report), 'compressed_report': compressed_report } self.__run_audit_node() sql3lite_worker = self.__audit_node.config.event_pool_manager.sql3lite_worker result_found = False # Waits till the record moves from assigned status to submitted. sql = "select * from audit_evt where request_id = {0} and fk_status == 'SB' and fk_type='PC'" while not result_found: rows = sql3lite_worker.execute(sql.format(request_id)) if len(rows) == 0: sleep(0.1) continue self.assertTrue(len(rows), 1) result_found = True