def test_update_min_price_success(self): config = fetch_config(inject_contract=True) thread = UpdateMinPriceThread(config) with mock.patch( 'audit.threads.update_min_price_thread.send_signed_transaction', return_value="hash"): thread.update_min_price()
def test_gas_price_computation_static(self): config = fetch_config(inject_contract=True) thread = ComputeGasPriceThread(config) config._Config__default_gas_price_wei = 12345 config._Config__gas_price_strategy = "static" thread.compute_gas_price() self.assertEqual(config.gas_price_wei, 12345)
def test_change_min_price(self): """ Tests that the node updates the min_price on the blockchain if the config value changes """ config = fetch_config(inject_contract=True) set_audit_price_filter = config.audit_contract.events.setAuditNodePrice_called.createFilter( fromBlock=max(0, config.event_pool_manager.get_latest_block_number())) # this make a one-off call config._Config__min_price_in_qsp = 1 thread = UpdateMinPriceThread(config) thread.update_min_price() success = False while not success: events = self.__evt_wait_loop(set_audit_price_filter) for event in events: self.assertEqual(event['event'], 'setAuditNodePrice_called') if event['args']['price'] == 10**18: success = True break if not success: sleep(TestUpdateMinPriceThread.__SLEEP_INTERVAL)
def test_check_and_update_min_price(self): config = fetch_config(inject_contract=True) thread = UpdateMinPriceThread(config) with mock.patch('audit.threads.update_min_price_thread.send_signed_transaction', return_value="hash"), \ mock.patch('audit.threads.update_min_price_thread.mk_read_only_call', return_value=config.min_price_in_qsp + 5): thread.check_and_update_min_price()
def test_start_stop(self): # start the thread, signal stop and exit. use mock not to make work config = fetch_config(inject_contract=True) thread = PollRequestsThread(config) thread.start() while not thread.exec: sleep(0.1) thread.stop() self.assertFalse(thread.exec)
def test_gas_price_computation_empty_blockchain(self): config = fetch_config(inject_contract=True) thread = ComputeGasPriceThread(config) # tests for errors when there are too few blocks in the blockchain history try: thread.compute_gas_price() self.assertTrue(config.gas_price_wei > 0) except Exception: self.fail("Computing gas price on an empty blockchain failed.")
def setUp(self): self.config = fetch_config(inject_contract=True) self.thread = MonitorSubmissionThread(self.config) self.evt_pool_manager = self.thread.config.event_pool_manager self.evt_pool_manager.set_evt_status_to_error = MagicMock() self.evt_pool_manager.set_evt_status_to_be_submitted = MagicMock() self.evt_pool_manager.set_evt_status_to_done = MagicMock() self.timeout_limit_blocks = mk_read_only_call( self.config, self.config.audit_contract.functions.getAuditTimeoutInBlocks())
def test_start_stop(self): # start the thread, signal stop and exit. use mock not to make work config = fetch_config(inject_contract=True) block_mined_polling_thread = BlockMinedPollingThread(config) thread = ComputeGasPriceThread(config, block_mined_polling_thread) thread.start() while not thread.exec: sleep(0.1) thread.stop() self.assertFalse(thread.exec)
def test_start_stop(self): # start the thread, signal stop and exit. use mock not to make work config = fetch_config(inject_contract=True) thread = CollectMetricsThread(config) with mock.patch( 'audit.threads.collect_metrics_thread.MetricCollector.collect_and_send', return_value=True): thread.start() while not thread.exec: sleep(0.1) thread.stop() self.assertFalse(thread.exec)
def test_start_stop(self): # start the thread, signal stop and exit. use mock not to make work config = fetch_config(inject_contract=True) thread = UpdateMinPriceThread(config) with mock.patch( 'audit.threads.update_min_price_thread.send_signed_transaction', return_value="hash"): thread.start() while not thread.exec: sleep(0.1) thread.stop() self.assertFalse(thread.exec)
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_update_min_price_exceptions(self): config = fetch_config(inject_contract=True) thread = UpdateMinPriceThread(config) with mock.patch( 'audit.threads.update_min_price_thread.send_signed_transaction', side_effect=Exception): try: thread.update_min_price() self.fail("Exception was not propagated") except Exception: # expected pass with mock.patch( 'audit.threads.update_min_price_thread.send_signed_transaction', side_effect=TransactionNotConfirmedException): try: thread.update_min_price() self.fail("Exception was not propagated") except TransactionNotConfirmedException: # expected pass with mock.patch( 'audit.threads.update_min_price_thread.send_signed_transaction', side_effect=Timeout): try: thread.update_min_price() self.fail("Exception was not propagated") except Timeout: # expected pass with mock.patch( 'audit.threads.update_min_price_thread.send_signed_transaction', side_effect=DeduplicationException): try: thread.update_min_price() self.fail("Exception was not propagated") except DeduplicationException: # expected pass
def test_get_metadata(self): """ Checks that metadata is loaded correctly """ config = fetch_config() data = [] for meta in [self.__MYTHRIL_METADATA, self.__SECURIFY_METADATA]: start = meta["command"].find("-v /tmp/.") + len("-v /tmp/.") end = meta["command"].find("shared/ -i qspprotocol") meta["command"] = meta["command"][0:start] + meta["command"][end:] data += [meta] for analyzer in config.analyzers: meta = analyzer.wrapper.get_metadata("x", 1, "x") self.assertNotEqual( -1, meta["command"].find(analyzer.wrapper.storage_dir)) start = meta["command"].find("-v /tmp/.") + len("-v /tmp/.") end = meta["command"].find("shared/ -i qspprotocol") meta["command"] = meta["command"][0:start] + meta["command"][end:] self.assertTrue(meta in data)
def setUp(self): """ Starts the execution of the QSP audit node as a separate thread. """ self.__config = fetch_config(inject_contract=True) self.__submit_thread = SubmitReportThread(self.__config)
def setUp(self): self.__config = fetch_config(inject_contract=True) self.__claim_rewards_thread = ClaimRewardsThread(self.__config)
def test_init(self): config = fetch_config(inject_contract=True) block_mined_polling_thread = BlockMinedPollingThread(config) thread = ComputeGasPriceThread(config, block_mined_polling_thread) self.assertEqual(config, thread.config)
def setUp(self): self.__config = fetch_config(inject_contract=True) self.__block_mined_polling_thread = BlockMinedPollingThread( self.__config) self.__audit_node = QSPAuditNode(self.__config) self.__audit_node.config._Config__upload_provider = DummyProvider()
def test_init(self): config = fetch_config(inject_contract=True) thread = ComputeGasPriceThread(config) self.assertEqual(config, thread.config)
def test_call_to_is_police_officer_no_exception(self): """ Tests whether calling the smart contract to assess whether a node is a police works. """ config = fetch_config(inject_contract=True) QSPAuditNode.is_police_officer(config)
def test_stop(self): config = fetch_config(inject_contract=True) thread = CollectMetricsThread(config) thread.stop() self.assertFalse(thread.exec)
def test_init(self): config = fetch_config(inject_contract=True) thread = UpdateMinPriceThread(config) self.assertEqual(config, thread.config)
def test_stop(self): config = fetch_config(inject_contract=True) thread = UpdateMinPriceThread(config) thread.stop() self.assertFalse(thread.exec)
def setUpClass(cls): QSPTest.setUpClass() config = fetch_config(inject_contract=True) remove(config.evt_db_path)
def setUp(self): self.__config = fetch_config(inject_contract=True) self.__poll_requests_thread = PollRequestsThread(self.__config)
def test_stop(self): config = fetch_config(inject_contract=True) thread = PerformAuditThread(config) thread.stop() self.assertFalse(thread.exec)
def setUp(self): """ Starts the execution of the QSP audit node as a separate thread. """ self.__config = fetch_config(inject_contract=True) self.__thread = PerformAuditThread(self.__config)
def setUp(self): self.__config = fetch_config(inject_contract=True) self.__block_mined_polling_thread = BlockMinedPollingThread( self.__config) self.__poll_requests_thread = PollRequestsThread( self.__config, self.__block_mined_polling_thread)
def test_init(self): config = fetch_config(inject_contract=True) thread = CollectMetricsThread(config) self.assertEqual(config, thread.config)
def test_init(self): config = fetch_config(inject_contract=True) thread = PollRequestsThread(config) self.assertEqual(config, thread.config)
def test_init(self): config = fetch_config(inject_contract=True) thread = PollRequestsThread(config, self.__block_mined_polling_thread) self.assertEqual(config, thread.config)