def test_setup_udp_or_exit_insufficient_balance(): network_id = 42 config = deepcopy(App.DEFAULT_CONFIG) config["network_id"] = network_id config["environment_type"] = Environment.DEVELOPMENT contracts = {} our_address = make_address() blockchain_service = MockChain(network_id=network_id, node_address=make_address()) # we don't have sufficient balance, so client should exit with a message blockchain_service.client.balances_mapping[our_address] = 1 with pytest.raises(SystemExit): setup_udp_or_exit( config=config, blockchain_service=blockchain_service, address=our_address, contracts=contracts, endpoint_registry_contract_address=make_address(), )
def test_setup_udp_or_exit(raiden_udp_ports): network_id = 42 config = deepcopy(App.DEFAULT_CONFIG) config["network_id"] = network_id config["environment_type"] = Environment.DEVELOPMENT host = "127.0.0.1" port = raiden_udp_ports[0] config["socket"] = server._udp_socket((host, port)) # pylint: disable=protected-access contracts = {} our_address = make_address() blockchain_service = MockChain(network_id=network_id, node_address=make_address()) # set a big fake balance for us, to pass the test of sufficient gas for discovery transaction blockchain_service.client.balances_mapping[our_address] = 99999999999999999 transport, discovery = setup_udp_or_exit( config=config, blockchain_service=blockchain_service, address=our_address, contracts=contracts, endpoint_registry_contract_address=make_address(), ) assert isinstance(transport, UDPTransport) assert discovery
def run_app( address, keystore_path, gas_price, eth_rpc_endpoint, tokennetwork_registry_contract_address, secret_registry_contract_address, service_registry_contract_address, endpoint_registry_contract_address, user_deposit_contract_address, listen_address, mapped_socket, max_unresponsive_time, api_address, rpc, sync_check, console, password_file, web_ui, datadir, transport, matrix_server, network_id, environment_type, unrecoverable_error_should_crash, pathfinding_service_address, pathfinding_eth_address, pathfinding_max_paths, enable_monitoring, resolver_endpoint, routing_mode, config=None, extra_config=None, **kwargs, ): # pylint: disable=too-many-locals,too-many-branches,too-many-statements,unused-argument from raiden.app import App check_sql_version() if transport == "udp" and not mapped_socket: raise RuntimeError("Missing socket") if datadir is None: datadir = os.path.join(os.path.expanduser("~"), ".raiden") account_manager = AccountManager(keystore_path) check_has_accounts(account_manager) if not address: address_hex = prompt_account(account_manager) else: address_hex = to_normalized_address(address) if password_file: privatekey_bin = unlock_account_with_passwordfile( account_manager=account_manager, address_hex=address_hex, password_file=password_file) else: privatekey_bin = unlock_account_with_passwordprompt( account_manager=account_manager, address_hex=address_hex) address = to_canonical_address(address_hex) (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) config["transport"]["udp"]["host"] = listen_host config["transport"]["udp"]["port"] = listen_port config["console"] = console config["rpc"] = rpc config["web_ui"] = rpc and web_ui config["api_host"] = api_host config["api_port"] = api_port config["resolver_endpoint"] = resolver_endpoint if mapped_socket: config["socket"] = mapped_socket.socket config["transport"]["udp"]["external_ip"] = mapped_socket.external_ip config["transport"]["udp"][ "external_port"] = mapped_socket.external_port config["transport_type"] = transport config["transport"]["matrix"]["server"] = matrix_server config["transport"]["udp"][ "nat_keepalive_retries"] = DEFAULT_NAT_KEEPALIVE_RETRIES timeout = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config["transport"]["udp"]["nat_keepalive_timeout"] = timeout config[ "unrecoverable_error_should_crash"] = unrecoverable_error_should_crash config["services"]["pathfinding_max_paths"] = pathfinding_max_paths config["services"]["monitoring_enabled"] = enable_monitoring parsed_eth_rpc_endpoint = urlparse(eth_rpc_endpoint) if not parsed_eth_rpc_endpoint.scheme: eth_rpc_endpoint = f"http://{eth_rpc_endpoint}" web3 = Web3(HTTPProvider(eth_rpc_endpoint)) check_ethereum_version(web3) check_network_id(network_id, web3) config["chain_id"] = network_id setup_environment(config, environment_type) contracts = setup_contracts_or_exit(config, network_id) rpc_client = JSONRPCClient( web3, privatekey_bin, gas_price_strategy=gas_price, block_num_confirmations=DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS, uses_infura="infura.io" in eth_rpc_endpoint, ) blockchain_service = BlockChainService(jsonrpc_client=rpc_client, contract_manager=ContractManager( config["contracts_path"])) if sync_check: check_synced(blockchain_service) proxies = setup_proxies_or_exit( config=config, tokennetwork_registry_contract_address= tokennetwork_registry_contract_address, secret_registry_contract_address=secret_registry_contract_address, endpoint_registry_contract_address=endpoint_registry_contract_address, user_deposit_contract_address=user_deposit_contract_address, service_registry_contract_address=service_registry_contract_address, blockchain_service=blockchain_service, contracts=contracts, routing_mode=routing_mode, pathfinding_service_address=pathfinding_service_address, pathfinding_eth_address=pathfinding_eth_address, ) database_path = os.path.join( datadir, f"node_{pex(address)}", f"netid_{network_id}", f"network_{pex(proxies.token_network_registry.address)}", f"v{RAIDEN_DB_VERSION}_log.db", ) config["database_path"] = database_path print("\nYou are connected to the '{}' network and the DB path is: {}". format(ID_TO_NETWORKNAME.get(network_id, network_id), database_path)) discovery = None if transport == "udp": transport, discovery = setup_udp_or_exit( config, blockchain_service, address, contracts, endpoint_registry_contract_address) elif transport == "matrix": transport = _setup_matrix(config) else: raise RuntimeError(f'Unknown transport type "{transport}" given') raiden_event_handler = RaidenEventHandler() message_handler = MessageHandler() try: start_block = 0 if "TokenNetworkRegistry" in contracts: start_block = contracts["TokenNetworkRegistry"]["block_number"] raiden_app = App( config=config, chain=blockchain_service, query_start_block=start_block, default_registry=proxies.token_network_registry, default_secret_registry=proxies.secret_registry, default_service_registry=proxies.service_registry, transport=transport, raiden_event_handler=raiden_event_handler, message_handler=message_handler, discovery=discovery, user_deposit=proxies.user_deposit, ) except RaidenError as e: click.secho(f"FATAL: {e}", fg="red") sys.exit(1) try: raiden_app.start() except RuntimeError as e: click.secho(f"FATAL: {e}", fg="red") sys.exit(1) except filelock.Timeout: name_or_id = ID_TO_NETWORKNAME.get(network_id, network_id) click.secho( f"FATAL: Another Raiden instance already running for account {address_hex} on " f"network id {name_or_id}", fg="red", ) sys.exit(1) return raiden_app
def run_app( address, keystore_path, gas_price, eth_rpc_endpoint, tokennetwork_registry_contract_address, secret_registry_contract_address, service_registry_contract_address, endpoint_registry_contract_address, user_deposit_contract_address, listen_address, mapped_socket, max_unresponsive_time, api_address, rpc, sync_check, console, password_file, web_ui, datadir, transport, matrix_server, network_id, environment_type, unrecoverable_error_should_crash, pathfinding_service_address, pathfinding_eth_address, pathfinding_max_paths, enable_monitoring, routing_mode, config=None, extra_config=None, **kwargs, ): # pylint: disable=too-many-locals,too-many-branches,too-many-statements,unused-argument from raiden.app import App _assert_sql_version() if transport == 'udp' and not mapped_socket: raise RuntimeError('Missing socket') if datadir is None: datadir = os.path.join(os.path.expanduser('~'), '.raiden') account_manager = AccountManager(keystore_path) check_has_accounts(account_manager) if not address: address_hex = prompt_account(account_manager) else: address_hex = to_normalized_address(address) if password_file: privatekey_bin = unlock_account_with_passwordfile( account_manager, address_hex, password_file, ) else: privatekey_bin = unlock_account_with_passwordprompt( account_manager, address_hex, ) address = to_canonical_address(address_hex) (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) config['transport']['udp']['host'] = listen_host config['transport']['udp']['port'] = listen_port config['console'] = console config['rpc'] = rpc config['web_ui'] = rpc and web_ui config['api_host'] = api_host config['api_port'] = api_port if mapped_socket: config['socket'] = mapped_socket.socket config['transport']['udp']['external_ip'] = mapped_socket.external_ip config['transport']['udp'][ 'external_port'] = mapped_socket.external_port config['transport_type'] = transport config['transport']['matrix']['server'] = matrix_server config['transport']['udp'][ 'nat_keepalive_retries'] = DEFAULT_NAT_KEEPALIVE_RETRIES timeout = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config['transport']['udp']['nat_keepalive_timeout'] = timeout config[ 'unrecoverable_error_should_crash'] = unrecoverable_error_should_crash config['services']['pathfinding_max_paths'] = pathfinding_max_paths config['services']['monitoring_enabled'] = enable_monitoring parsed_eth_rpc_endpoint = urlparse(eth_rpc_endpoint) if not parsed_eth_rpc_endpoint.scheme: eth_rpc_endpoint = f'http://{eth_rpc_endpoint}' web3 = _setup_web3(eth_rpc_endpoint) node_network_id, known_node_network_id = setup_network_id_or_exit( config, network_id, web3) environment_type = setup_environment(config, environment_type) contracts, contract_addresses_known = setup_contracts_or_exit( config, node_network_id) rpc_client = JSONRPCClient( web3, privatekey_bin, gas_price_strategy=gas_price, block_num_confirmations=DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS, uses_infura='infura.io' in eth_rpc_endpoint, ) blockchain_service = BlockChainService( jsonrpc_client=rpc_client, contract_manager=ContractManager(config['contracts_path']), ) if sync_check: check_synced(blockchain_service, known_node_network_id) proxies = setup_proxies_or_exit( config=config, tokennetwork_registry_contract_address= tokennetwork_registry_contract_address, secret_registry_contract_address=secret_registry_contract_address, endpoint_registry_contract_address=endpoint_registry_contract_address, user_deposit_contract_address=user_deposit_contract_address, service_registry_contract_address=service_registry_contract_address, contract_addresses_known=contract_addresses_known, blockchain_service=blockchain_service, contracts=contracts, routing_mode=routing_mode, pathfinding_service_address=pathfinding_service_address, pathfinding_eth_address=pathfinding_eth_address, ) database_path = os.path.join( datadir, f'node_{pex(address)}', f'netid_{node_network_id}', f'network_{pex(proxies.token_network_registry.address)}', f'v{RAIDEN_DB_VERSION}_log.db', ) config['database_path'] = database_path print( '\nYou are connected to the \'{}\' network and the DB path is: {}'. format( ID_TO_NETWORKNAME.get(node_network_id, node_network_id), database_path, ), ) discovery = None if transport == 'udp': transport, discovery = setup_udp_or_exit( config, blockchain_service, address, contracts, endpoint_registry_contract_address, ) elif transport == 'matrix': transport = _setup_matrix(config) else: raise RuntimeError(f'Unknown transport type "{transport}" given') raiden_event_handler = RaidenEventHandler() message_handler = MessageHandler() try: start_block = 0 if 'TokenNetworkRegistry' in contracts: start_block = contracts['TokenNetworkRegistry']['block_number'] raiden_app = App( config=config, chain=blockchain_service, query_start_block=start_block, default_registry=proxies.token_network_registry, default_secret_registry=proxies.secret_registry, default_service_registry=proxies.service_registry, transport=transport, raiden_event_handler=raiden_event_handler, message_handler=message_handler, discovery=discovery, user_deposit=proxies.user_deposit, ) except RaidenError as e: click.secho(f'FATAL: {e}', fg='red') sys.exit(1) try: raiden_app.start() except RuntimeError as e: click.secho(f'FATAL: {e}', fg='red') sys.exit(1) except filelock.Timeout: name_or_id = ID_TO_NETWORKNAME.get(node_network_id, node_network_id) click.secho( f'FATAL: Another Raiden instance already running for account {address_hex} on ' f'network id {name_or_id}', fg='red', ) sys.exit(1) return raiden_app
def run_app( address: Address, keystore_path: str, gas_price: Callable, eth_rpc_endpoint: str, tokennetwork_registry_contract_address: Address, one_to_n_contract_address: Address, secret_registry_contract_address: Address, service_registry_contract_address: Address, endpoint_registry_contract_address: Address, user_deposit_contract_address: Address, listen_address: str, mapped_socket, max_unresponsive_time: int, api_address: str, rpc: bool, sync_check: bool, console: bool, password_file: TextIO, web_ui: bool, datadir: str, transport: str, matrix_server: str, network_id: int, environment_type: Environment, unrecoverable_error_should_crash: bool, pathfinding_service_address: str, pathfinding_max_paths: int, enable_monitoring: bool, resolver_endpoint: str, routing_mode: RoutingMode, config: Dict[str, Any], **kwargs: Any, # FIXME: not used here, but still receives stuff in smoketest ): # pylint: disable=too-many-locals,too-many-branches,too-many-statements,unused-argument from raiden.app import App if transport == "udp" and not mapped_socket: raise RuntimeError("Missing socket") if datadir is None: datadir = os.path.join(os.path.expanduser("~"), ".raiden") account_manager = AccountManager(keystore_path) web3 = Web3(HTTPProvider(rpc_normalized_endpoint(eth_rpc_endpoint))) check_sql_version() check_ethereum_has_accounts(account_manager) check_ethereum_client_is_supported(web3) check_ethereum_network_id(network_id, web3) (address, privatekey_bin, pubkey_bin) = get_account_and_private_key( account_manager, address, password_file ) (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) print("Private key: " + encode_hex(privatekey_bin)) print("Public key: " + encode_hex(pubkey_bin)) config["pubkey"] = pubkey_bin config["privatekey"] = privatekey_bin config["transport"]["udp"]["host"] = listen_host config["transport"]["udp"]["port"] = listen_port config["console"] = console config["rpc"] = rpc config["web_ui"] = rpc and web_ui config["api_host"] = api_host config["api_port"] = api_port config["resolver_endpoint"] = resolver_endpoint if mapped_socket: config["socket"] = mapped_socket.socket config["transport"]["udp"]["external_ip"] = mapped_socket.external_ip config["transport"]["udp"]["external_port"] = mapped_socket.external_port config["transport_type"] = transport config["transport"]["matrix"]["server"] = matrix_server config["transport"]["udp"]["nat_keepalive_retries"] = DEFAULT_NAT_KEEPALIVE_RETRIES timeout = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config["transport"]["udp"]["nat_keepalive_timeout"] = timeout config["unrecoverable_error_should_crash"] = unrecoverable_error_should_crash config["services"]["pathfinding_max_paths"] = pathfinding_max_paths config["services"]["monitoring_enabled"] = enable_monitoring config["chain_id"] = network_id setup_environment(config, environment_type) contracts = setup_contracts_or_exit(config, network_id) rpc_client = JSONRPCClient( web3, privatekey_bin, gas_price_strategy=gas_price, block_num_confirmations=DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS, uses_infura="infura.io" in eth_rpc_endpoint, ) blockchain_service = BlockChainService( jsonrpc_client=rpc_client, contract_manager=ContractManager(config["contracts_path"]) ) if sync_check: check_synced(blockchain_service) proxies = setup_proxies_or_exit( config=config, tokennetwork_registry_contract_address=tokennetwork_registry_contract_address, secret_registry_contract_address=secret_registry_contract_address, endpoint_registry_contract_address=endpoint_registry_contract_address, user_deposit_contract_address=user_deposit_contract_address, service_registry_contract_address=service_registry_contract_address, blockchain_service=blockchain_service, contracts=contracts, routing_mode=routing_mode, pathfinding_service_address=pathfinding_service_address, ) database_path = os.path.join( datadir, f"node_{pex(address)}", f"netid_{network_id}", f"network_{pex(proxies.token_network_registry.address)}", f"v{RAIDEN_DB_VERSION}_log.db", ) config["database_path"] = database_path print( "\nYou are connected to the '{}' network and the DB path is: {}".format( ID_TO_NETWORKNAME.get(network_id, network_id), database_path ) ) # FIXME mmartinez this must be checksummed or compared on a standard way # running_network = {"network_id": network_id, # "token_network_registry": encode_hex(tokennetwork_registry_contract_address), # "secret_registry": encode_hex(secret_registry_contract_address), # "endpoint_registry": encode_hex(endpoint_registry_contract_address)} # check_network_params(running_network) discovery = None if transport == "udp": transport, discovery = setup_udp_or_exit( config, blockchain_service, address, contracts, endpoint_registry_contract_address ) elif transport == "matrix": transport = _setup_matrix(config) else: raise RuntimeError(f'Unknown transport type "{transport}" given') raiden_event_handler = RaidenEventHandler() message_handler = MessageHandler() try: start_block = 0 if "TokenNetworkRegistry" in contracts: start_block = contracts["TokenNetworkRegistry"]["block_number"] raiden_app = App( config=config, chain=blockchain_service, query_start_block=BlockNumber(start_block), default_one_to_n_address=one_to_n_contract_address, default_registry=proxies.token_network_registry, default_secret_registry=proxies.secret_registry, default_service_registry=proxies.service_registry, transport=transport, raiden_event_handler=raiden_event_handler, message_handler=message_handler, discovery=discovery, user_deposit=proxies.user_deposit, ) except RaidenError as e: click.secho(f"FATAL: {e}", fg="red") sys.exit(1) try: raiden_app.start() except RuntimeError as e: click.secho(f"FATAL: {e}", fg="red") sys.exit(1) except filelock.Timeout: name_or_id = ID_TO_NETWORKNAME.get(network_id, network_id) click.secho( f"FATAL: Another Raiden instance already running for account " f"{to_normalized_address(address)} on network id {name_or_id}", fg="red", ) sys.exit(1) return raiden_app