def run_daemon(args: argparse.Namespace, conf: Config): loop = asyncio.get_event_loop() if args.verbose is not None: loop.set_debug(True) if not args.no_logging: setup_logging(logging.getLogger(), args, conf) daemon = Daemon(conf) def __exit(): raise GracefulExit() try: loop.add_signal_handler(signal.SIGINT, __exit) loop.add_signal_handler(signal.SIGTERM, __exit) except NotImplementedError: pass # Not implemented on Windows try: loop.run_until_complete(daemon.start()) loop.run_forever() except (GracefulExit, KeyboardInterrupt, asyncio.CancelledError): pass finally: loop.run_until_complete(daemon.stop()) logging.shutdown() if hasattr(loop, 'shutdown_asyncgens'): loop.run_until_complete(loop.shutdown_asyncgens())
def run_daemon(args: argparse.Namespace, conf: Config): if sys.version_info < (3, 8) and platform.system() == "Windows": # TODO: remove after we move to requiring Python 3.8 asyncio.set_event_loop(asyncio.ProactorEventLoop()) loop = asyncio.get_event_loop() if args.verbose is not None: loop.set_debug(True) if not args.no_logging: setup_logging(logging.getLogger(), args, conf) daemon = Daemon(conf) def __exit(): raise GracefulExit() try: loop.add_signal_handler(signal.SIGINT, __exit) loop.add_signal_handler(signal.SIGTERM, __exit) except NotImplementedError: pass # Not implemented on Windows try: loop.run_until_complete(daemon.start()) loop.run_forever() except (GracefulExit, KeyboardInterrupt, asyncio.CancelledError): pass finally: loop.run_until_complete(daemon.stop()) logging.shutdown() if hasattr(loop, 'shutdown_asyncgens'): loop.run_until_complete(loop.shutdown_asyncgens())
def start(): keyring.set_keyring(LbryAndroidKeyring()) private_storage_dir = lbrynet_android_utils.getAppInternalStorageDir(service.getApplicationContext()) configured_download_dir = lbrynet_android_utils.getConfiguredDownloadDirectory(service.getApplicationContext()) components_to_skip = [] dht_state = 'off' try: dht_path = f'{private_storage_dir}/dht'; with open(dht_path, 'r') as file: dht_state = file.read() except: pass dht_enabled = dht_state == 'on' if not dht_enabled: components_to_skip = [DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT] conf = Config( data_dir=f'{private_storage_dir}/lbrynet', wallet_dir=f'{private_storage_dir}/lbryum', download_dir=configured_download_dir, blob_lru_cache_size=32, components_to_skip=components_to_skip, save_blobs=False, save_files=False, share_usage_data=True, use_upnp=False ) for directory in (conf.data_dir, conf.download_dir, conf.wallet_dir): ensure_directory_exists(directory) configure_logging(conf) log.info('Starting lbry sdk {}'.format(lbrynet_version)); loop = asyncio.get_event_loop() loop.set_debug(lbrynet_android_utils.isDebug()) daemon = Daemon(conf) try: loop.run_until_complete(daemon.start()) loop.run_forever() except (GracefulExit, asyncio.CancelledError): pass finally: loop.run_until_complete(daemon.stop()) if hasattr(loop, 'shutdown_asyncgens'): loop.run_until_complete(loop.shutdown_asyncgens())
async def test_download_blob_using_jsonrpc_blob_get(self): blob_hash = "7f5ab2def99f0ddd008da71db3a3772135f4002b19b7605840ed1034c8955431bd7079549e65e6b2a3b9c17c773073ed" mock_blob_bytes = b'1' * ((2 * 2 ** 20) - 1) await self._add_blob_to_server(blob_hash, mock_blob_bytes) # setup RPC Daemon daemon_config = copy.deepcopy(self.client_config) daemon_config.fixed_peers = [(self.server_from_client.address, self.server_from_client.tcp_port)] daemon = Daemon(daemon_config) mock_node = mock.Mock(spec=Node) def _mock_accumulate_peers(q1, q2=None): async def _task(): pass q2 = q2 or asyncio.Queue(loop=self.loop) return q2, self.loop.create_task(_task()) mock_node.accumulate_peers = _mock_accumulate_peers with mock.patch('lbry.extras.daemon.componentmanager.ComponentManager.all_components_running', return_value=True): with mock.patch('lbry.extras.daemon.daemon.Daemon.dht_node', new_callable=mock.PropertyMock) \ as daemon_mock_dht: with mock.patch('lbry.extras.daemon.daemon.Daemon.blob_manager', new_callable=mock.PropertyMock) \ as daemon_mock_blob_manager: daemon_mock_dht.return_value = mock_node daemon_mock_blob_manager.return_value = self.client_blob_manager result = await daemon.jsonrpc_blob_get(blob_hash, read=True) self.assertIsNotNone(result) self.assertEqual(mock_blob_bytes.decode(), result, "Downloaded blob is different than server blob")
async def add_daemon(self, wallet_node=None, seed=None): if wallet_node is None: wallet_node = WalletNode(self.wallet_node.manager_class, self.wallet_node.ledger_class, port=self.extra_wallet_node_port) self.extra_wallet_node_port += 1 await wallet_node.start(self.conductor.spv_node, seed=seed) self.extra_wallet_nodes.append(wallet_node) upload_dir = os.path.join(wallet_node.data_path, 'uploads') os.mkdir(upload_dir) conf = Config() conf.data_dir = wallet_node.data_path conf.wallet_dir = wallet_node.data_path conf.download_dir = wallet_node.data_path conf.upload_dir = upload_dir # not a real conf setting conf.share_usage_data = False conf.use_upnp = False conf.reflect_streams = True conf.blockchain_name = 'lbrycrd_regtest' conf.lbryum_servers = [('127.0.0.1', 50001)] conf.reflector_servers = [('127.0.0.1', 5566)] conf.fixed_peers = [('127.0.0.1', 5567)] conf.known_dht_nodes = [] conf.blob_lru_cache_size = self.blob_lru_cache_size conf.transaction_cache_size = 10000 conf.components_to_skip = [ DHT_COMPONENT, UPNP_COMPONENT, HASH_ANNOUNCER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT ] if self.skip_libtorrent: conf.components_to_skip.append(LIBTORRENT_COMPONENT) wallet_node.manager.config = conf def wallet_maker(component_manager): wallet_component = WalletComponent(component_manager) wallet_component.wallet_manager = wallet_node.manager wallet_component._running = True return wallet_component daemon = Daemon( conf, ComponentManager(conf, skip_components=conf.components_to_skip, wallet=wallet_maker, exchange_rate_manager=partial( ExchangeRateManagerComponent, rates={ 'BTCLBC': 1.0, 'USDBTC': 2.0 }))) await daemon.initialize() self.daemons.append(daemon) wallet_node.manager.old_db = daemon.storage return daemon
def get_argument_parser(): root = ArgumentParser( 'lbrynet', description='An interface to the LBRY Network.', allow_abbrev=False, ) root.add_argument( '-v', '--version', dest='cli_version', action="store_true", help='Show lbrynet CLI version and exit.' ) root.set_defaults(group=None, command=None) CLIConfig.contribute_to_argparse(root) sub = root.add_subparsers(metavar='COMMAND') start = sub.add_parser( 'start', usage='lbrynet start [--config FILE] [--data-dir DIR] [--wallet-dir DIR] [--download-dir DIR] ...', help='Start LBRY Network interface.' ) start.add_argument( '--quiet', dest='quiet', action="store_true", help='Disable all console output.' ) start.add_argument( '--no-logging', dest='no_logging', action="store_true", help='Disable all logging of any kind.' ) start.add_argument( '--verbose', nargs="*", help=('Enable debug output for lbry logger and event loop. Optionally specify loggers for which debug output ' 'should selectively be applied.') ) start.add_argument( '--initial-headers', dest='initial_headers', help='Specify path to initial blockchain headers, faster than downloading them on first run.' ) Config.contribute_to_argparse(start) start.set_defaults(command='start', start_parser=start, doc=start.format_help()) api = Daemon.get_api_definitions() groups = {} for group_name in sorted(api['groups']): group_parser = sub.add_parser(group_name, group_name=group_name, help=api['groups'][group_name]) groups[group_name] = group_parser.add_subparsers(metavar='COMMAND') nicer_order = ['stop', 'get', 'publish', 'resolve'] for command_name in sorted(api['commands']): if command_name not in nicer_order: nicer_order.append(command_name) for command_name in nicer_order: command = api['commands'][command_name] if command['group'] is None: add_command_parser(sub, command) else: add_command_parser(groups[command['group']], command) return root
def write_api(f): examples = get_examples() api_definitions = Daemon.get_api_definitions() apis = {'main': {'doc': 'Ungrouped commands.', 'commands': []}} for group_name, group_doc in api_definitions['groups'].items(): apis[group_name] = {'doc': group_doc, 'commands': []} for method_name, command in api_definitions['commands'].items(): if 'replaced_by' in command: continue apis[command['group'] or 'main']['commands'].append( get_api(method_name, examples.get(method_name, []))) json.dump(apis, f, indent=4)
async def asyncSetUp(self): conf = Config() conf.data_dir = '/tmp' conf.share_usage_data = False conf.api = 'localhost:5299' conf.components_to_skip = ( DATABASE_COMPONENT, BLOB_COMPONENT, WALLET_COMPONENT, DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, FILE_MANAGER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT, UPNP_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT, WALLET_SERVER_PAYMENTS_COMPONENT, LIBTORRENT_COMPONENT ) Daemon.component_attributes = {} self.daemon = Daemon(conf) await self.daemon.start() self.addCleanup(self.daemon.stop)
def get_test_daemon(conf: Config, with_fee=False): conf.data_dir = '/tmp' rates = { 'BTCLBC': { 'spot': 3.0, 'ts': test_utils.DEFAULT_ISO_TIME + 1 }, 'USDBTC': { 'spot': 2.0, 'ts': test_utils.DEFAULT_ISO_TIME + 2 } } component_manager = ComponentManager( conf, skip_components=[ DATABASE_COMPONENT, DHT_COMPONENT, WALLET_COMPONENT, UPNP_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT, HASH_ANNOUNCER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT, BLOB_COMPONENT, RATE_LIMITER_COMPONENT ], file_manager=FakeFileManager) daemon = LBRYDaemon(conf, component_manager=component_manager) daemon.payment_rate_manager = OnlyFreePaymentsManager() daemon.wallet_manager = mock.Mock(spec=WalletManager) daemon.wallet_manager.wallet = mock.Mock(spec=Wallet) daemon.wallet_manager.use_encryption = False daemon.wallet_manager.network = FakeNetwork() daemon.storage = mock.Mock(spec=SQLiteStorage) market_feeds = [BTCLBCFeed(), USDBTCFeed()] daemon.exchange_rate_manager = DummyExchangeRateManager( market_feeds, rates) daemon.stream_manager = component_manager.get_component( FILE_MANAGER_COMPONENT) metadata = { "author": "fake author", "language": "en", "content_type": "fake/format", "description": "fake description", "license": "fake license", "license_url": "fake license url", "nsfw": False, "sources": { "lbry_sd_hash": 'd2b8b6e907dde95245fe6d144d16c2fdd60c4e0c6463ec98' 'b85642d06d8e9414e8fcfdcb7cb13532ec5454fb8fe7f280' }, "thumbnail": "fake thumbnail", "title": "fake title", "ver": "0.0.3" } if with_fee: metadata.update({ "fee": { "USD": { "address": "bQ6BGboPV2SpTMEP7wLNiAcnsZiH8ye6eA", "amount": 0.75 } } }) migrated = smart_decode(json.dumps(metadata)) daemon._resolve = daemon.resolve = lambda *_: defer.succeed( {"test": { 'claim': { 'value': migrated.claim_dict } }}) return daemon