def check(): """ The main function to run every interval :return: None :rtype: None """ global config, dna logger.log("Checking PlebNet", log_name) config = PlebNetConfig() # TODO: DNA static singular maken --> dan kan dit weg dna = DNA() dna.read_dictionary() # these require time to setup, continue in the next iteration if not check_tribler(): return if not check_tunnel_helper(): return # Prepare Cloudomate if not settings.wallets_initiate_once(): create_wallet() select_provider() update_offer() attempt_purchase() install_vps()
def test_purchase_choice_error(self): self.config = PlebNetConfig.get self.triblerwallet = TriblerWallet.__init__ self.settings = plebnet_settings.Init.wallets_testnet_created self.purchase = blueAngel.BlueAngelHost.purchase self.logger = Logger.warning self.error = Logger.error self.issue = git_issuer.handle_error PlebNetConfig.get = MagicMock(side_effect=self.side_effect) plebnet_settings.Init.wallets_testnet_created = MagicMock( return_value=None) TriblerWallet.__init__ = MagicMock(return_value=None) blueAngel.BlueAngelHost.purchase = MagicMock(side_effect=Exception) Logger.warning = MagicMock() Logger.error = MagicMock() git_issuer.handle_error = MagicMock() self.assertEquals(cloudomate.purchase_choice(PlebNetConfig()), plebnet_settings.FAILURE) PlebNetConfig.get = self.config TriblerWallet.__init__ = self.triblerwallet plebnet_settings.Init.wallets_testnet_created = self.settings blueAngel.BlueAngelHost.purchase = self.purchase Logger.warning = self.logger Logger.error = self.error git_issuer.handle_error = self.issue
def test_purchase_choice_vpn(self): self.config = PlebNetConfig.get self.triblerwallet = TriblerWallet.__init__ self.settings = plebnet_settings.Init.wallets_testnet_created self.purchase = azirevpn.AzireVpn.purchase self.logger = Logger.warning self.host = plebnet_settings.Init.vpn_host PlebNetConfig.get = MagicMock(side_effect=self.side_effect) plebnet_settings.Init.wallets_testnet_created = MagicMock( return_value=None) TriblerWallet.__init__ = MagicMock(return_value=None) azirevpn.AzireVpn.purchase = MagicMock(return_value=('Hash', 0)) Logger.warning = MagicMock() plebnet_settings.Init.vpn_host = MagicMock(return_value='AzireVPN') self.assertEquals(cloudomate.purchase_choice_vpn(PlebNetConfig()), plebnet_settings.SUCCESS) PlebNetConfig.get = self.config TriblerWallet.__init__ = self.triblerwallet plebnet_settings.Init.wallets_testnet_created = self.settings azirevpn.AzireVpn.purchase = self.purchase Logger.warning = self.logger plebnet_settings.Init.vpn_host = self.host
def setup(args): logger.log("Setting up PlebNet") # Prepare Cloudomate if args.test_net: settings.wallets_testnet("1") settings.settings.write() fake_generator.generate_child_account() # TODO: change --> Prepare plebnet config = PlebNetConfig() config.set('expiration_date', time.time() + 30 * plebnet_settings.TIME_IN_DAY) config.save() # handle the DNA dna = DNA() dna.read_dictionary(cloudomate_controller.get_vps_providers()) dna.write_dictionary() # Prepare the IRC Client irc_handler.init_irc_client() irc_handler.start_irc_client() logger.success("PlebNet is ready to roll!")
def check(): """ The method is the main function which should run periodically. It controls the behaviour of the agent, starting Tribler and buying servers. """ global config, dna logger.log("Checking PlebNet", log_name) # Read general configuration if settings.wallets_testnet_created(): os.environ['TESTNET'] = '1' config = PlebNetConfig() dna = DNA() dna.read_dictionary() # check if own vpn is installed before continuing if not check_vpn_install(): logger.error("!!! VPN is not installed, child may get banned !!!", "Plebnet Check") # Requires time to setup, continue in the next iteration. if not check_tribler(): return if not settings.wallets_initiate_once(): create_wallet() select_provider() # These need a matchmaker, otherwise agent will be stuck waiting. if market_controller.has_matchmakers(): update_offer() attempt_purchase() install_vps()
def test_install_available_servers(self, mock1, mock2, mock3, mock4): config = PlebNetConfig() config.get('bought').append(test_bought) config.save() server_installer.install_available_servers(config, self.test_dna) self.assertEqual(config.get('installed'), [{'linevast': False}]) self.assertEqual(config.get('bought'), [])
def test_install_available_servers(self, mock1, mock2, mock3, mock4, mock5, mock6, mock7, mock8, mock9, mock10, mock11): config = PlebNetConfig() config.get('bought').append(test_bought) config.save() server_installer.install_available_servers(config, self.test_qtable) self.assertEqual(config.get('installed'), [test_bought]) self.assertEqual(config.get('bought'), [])
def test_place_offer_zero_mb(self): self.mb = market.get_balance self.logger = Logger.log Logger.log = MagicMock() market.get_balance = MagicMock(return_value=0) self.assertFalse(cloudomate.place_offer(5, PlebNetConfig())) market.get_balance = self.mb Logger.log = self.logger
def test_update_offer(self): self.config = PlebNetConfig.get self.calculate_price = cloudomate.calculate_price self.placeoffer = cloudomate.place_offer PlebNetConfig.get = MagicMock(return_value=False) cloudomate.place_offer = MagicMock(return_value=True) cloudomate.update_offer(PlebNetConfig()) cloudomate.place_offer.assert_not_called() cloudomate.calculate_price = MagicMock(return_value=9) PlebNetConfig.get = MagicMock(return_value=(blueAngel.BlueAngelHost, 'test', 'test')) cloudomate.update_offer(PlebNetConfig()) cloudomate.place_offer.assert_called_once() PlebNetConfig.get = self.config cloudomate.calculate_price = self.calculate_price cloudomate.place_offer = self.placeoffer
def setup(args): """ This method should only be called once and is responsible for the initial setup of the PlebNet agent. All necessary configuration files are created and IRC communication is started. :param args: If running in Testnet mode. """ global qtable, config logger.log("Setting up PlebNet") # Set general info about the PlebNet agent settings.irc_nick(settings.irc_nick_def() + str(random.randint(1000, 10000))) config = PlebNetConfig() config.set('expiration_date', time.time() + TIME_ALIVE) # Prepare the QTable configuration qtable = QTable() if args.test_net: settings.wallets_testnet("1") qtable.read_dictionary({ 'proxhost': cloudomate_controller.get_vps_providers()['proxhost'] }) else: providers = cloudomate_controller.get_vps_providers() if providers.has_key('proxhost'): del providers["proxhost"] # Create QTable if it does not exist qtable.read_dictionary(providers) if args.exit_node: logger.log("Running as exitnode") settings.tribler_exitnode('1') settings.settings.write() # Prepare first child configuration fake_generator.generate_child_account() # Prepare the IRC Client irc_handler.init_irc_client() irc_handler.start_irc_client() config.save() # add learning_consumer as a consumer for qtable channel in addressbook qtable.address_book.receiver.register_consumer("qtable", learning_consumer) logger.success("PlebNet is ready to roll!")
def setup(args): """ This method should only be called once and is responsible for the initial setup of the PlebNet agent. All necessary configuration files are created and IRC communication is started. :param args: If running in Testnet mode. """ global dna, config logger.log("Setting up PlebNet") # Prepare the DNA configuration dna = DNA() if args.test_net: settings.wallets_testnet("1") settings.settings.write() dna.read_dictionary({ 'proxhost': cloudomate_controller.get_vps_providers()['proxhost'] }) else: dna.read_dictionary(cloudomate_controller.get_vps_providers()) if 'proxhost' in dna.vps.keys(): dna.remove_provider('proxhost') dna.write_dictionary() if args.exit_node: logger.log("Running as exitnode") settings.tribler_exitnode('1') # Prepare first child configuration fake_generator.generate_child_account() # Set general info about the PlebNet agent settings.irc_nick(settings.irc_nick_def() + str(random.randint(1000, 10000))) config = PlebNetConfig() config.set('expiration_date', time.time() + 30 * plebnet_settings.TIME_IN_DAY) # Prepare the IRC Client irc_handler.init_irc_client() irc_handler.start_irc_client() if dna.get_own_tree() == '': logger.log("tree set to %s" % settings.irc_nick()) dna.set_own_tree(settings.irc_nick()) config.save() logger.success("PlebNet is ready to roll!")
def test_place_offer(self): mb_amount = 100 self.strategy = self.StrategyWrapper() self.put = market.put_bid self.true_settings = plebnet_settings.Init.wallets_testnet market.put_bid = MagicMock() plebnet_settings.Init.wallets_testnet = MagicMock(return_value=False) self.strategy.place_offer(mb_amount, 5, plebnet_settings.TIME_IN_HOUR, PlebNetConfig()) market.put_bid.assert_called_once() market.put_bid = self.put
def test_place_offer(self): self.mb = market.get_balance self.logger = Logger.log self.put = market.put_ask Logger.log = MagicMock() market.get_balance = MagicMock(return_value=56) market.put_ask = MagicMock() cloudomate.place_offer(5, PlebNetConfig()) market.put_ask.assert_called_once() market.get_balance = self.mb Logger.log = self.logger market.put_ask = self.put
def child_account(index=None): """ This method returns the configuration for a certain child number. :param index: The number of the child :type index: Integer :return: configuration of the child :rtype: Settings """ if index is not None: account = AccountSettings() account.read_settings( os.path.join(user_config_dir(), 'child_config' + str(index) + '.cfg')) else: account = AccountSettings() account.read_settings( os.path.join(user_config_dir(), 'child_config' + str(PlebNetConfig().get("child_index")) + '.cfg')) return account
def test_place_offer(self): self.mb = market.get_balance self.logger = Logger.log self.put = market.put_ask self.true_settings = plebnet_settings.Init.wallets_testnet Logger.log = MagicMock() market.get_balance = MagicMock(return_value=56) market.put_ask = MagicMock() plebnet_settings.Init.wallets_testnet = MagicMock(return_value=False) cloudomate.place_offer(5, PlebNetConfig()) market.put_ask.assert_called_once() market.get_balance = self.mb Logger.log = self.logger market.put_ask = self.put
def test_purchase_choice(self): self.config = PlebNetConfig.get self.triblerwallet = TriblerWallet.__init__ self.settings = plebnet_settings.Init.wallets_testnet_created self.purchase = blueAngel.BlueAngelHost.purchase self.logger = Logger.warning PlebNetConfig.get = MagicMock(side_effect=self.side_effect) plebnet_settings.Init.wallets_testnet_created = MagicMock(return_value=None) TriblerWallet.__init__ = MagicMock(return_value=None) blueAngel.BlueAngelHost.purchase = MagicMock(return_value=('Hash', 0)) Logger.warning = MagicMock() self.assertEquals(cloudomate.purchase_choice(PlebNetConfig()), plebnet_settings.SUCCESS) PlebNetConfig.get = self.config TriblerWallet.__init__ = self.triblerwallet plebnet_settings.Init.wallets_testnet_created = self.settings blueAngel.BlueAngelHost.purchase = self.purchase Logger.warning = self.logger
def check(): """ The method is the main function which should run periodically. It controls the behaviour of the agent, starting Tribler and buying servers. """ global config, qtable global sold_mb_tokens, previous_mb_tokens logger.log("Checking PlebNet", log_name) # Read general configuration if settings.wallets_testnet_created(): os.environ['TESTNET'] = '1' config = PlebNetConfig() qtable = QTable() qtable.read_dictionary() # check if own vpn is installed before continuing if not check_vpn_install(): logger.error("!!! VPN is not installed, child may get banned !!!", "Plebnet Check") # Requires time to setup, continue in the next iteration. if not check_tribler(): return check_irc() if not settings.wallets_initiate_once(): create_wallet() select_provider() # if is going to die, move all currency to a wallet if config.time_to_expiration() < plebnet_settings.TIME_IN_HOUR: save_all_currency() # These need a matchmaker, otherwise agent will be stuck waiting. if market_controller.has_matchmakers(): strategies[plebnet_settings.get_instance().strategy_name()]().apply() install_vps()
def purchase_choice(config): """ Purchase the cheapest provider in chosen_providers. If buying is successful this provider is moved to bought. In any case the provider is removed from choices. :param config: config :return: success """ (provider, option, _) = config.get('chosen_provider') provider_instance = cloudomate_providers['vps'][provider](child_account()) PlebNetConfig().increment_child_index() fake_generator.generate_child_account() wallet = TriblerWallet( plebnet_settings.get_instance().wallets_testnet_created()) c = cloudomate_providers['vps'][provider] configurations = c.get_options() option = configurations[option] transaction_hash, _ = provider_instance.purchase(wallet, option) if not transaction_hash: logger.warning("Failed to purchase server") return plebnet_settings.FAILURE # TODO: how to spot the difference? if False: logger.warning("Insufficient funds to purchase server") return plebnet_settings.UNKNOWN config.get('bought').append( (provider, transaction_hash, config.get('child_index') - 1)) config.get('transactions').append(transaction_hash) config.set('chosen_provider', None) config.save() return plebnet_settings.SUCCESS
def test_purchase_choice_failure(self): self.config = PlebNetConfig.get self.triblerwallet = TriblerWallet.__init__ self.settings = plebnet_settings.Init.wallets_testnet_created self.purchase = linevast.LineVast.purchase self.logger = Logger.warning PlebNetConfig.get = MagicMock(side_effect=self.side_effect) plebnet_settings.Init.wallets_testnet_created = MagicMock( return_value=None) TriblerWallet.__init__ = MagicMock(return_value=None) linevast.LineVast.purchase = MagicMock( side_effect=Exception("Purchase failed")) Logger.warning = MagicMock() self.assertEquals(cloudomate.purchase_choice(PlebNetConfig()), plebnet_settings.FAILURE) PlebNetConfig.get = self.config TriblerWallet.__init__ = self.triblerwallet plebnet_settings.Init.wallets_testnet_created = self.settings linevast.LineVast.purchase = self.purchase Logger.warning = self.logger
def _child_file(): return os.path.join( user_config_dir(), 'child_config' + str(PlebNetConfig().get('child_index')) + '.cfg')
def test_place_offer_zero_mb(self): self.strategy = self.StrategyWrapper() self.assertFalse( self.strategy.place_offer(0, 5, plebnet_settings.TIME_IN_HOUR, PlebNetConfig()))
def __init__(self): self.config = PlebNetConfig()