def test_split_endpoint_valid(): host, port = split_endpoint(Endpoint("https://rpc.slock.it/goerli")) assert host == "rpc.slock.it" assert port == 0 host, port = split_endpoint(Endpoint("https://rpc.slock.it:443/goerli")) assert host == "rpc.slock.it" assert port == 443
def run_app( address: Address, keystore_path: str, gas_price: Callable, eth_rpc_endpoint: str, user_deposit_contract_address: Optional[UserDepositAddress], api_address: Endpoint, rpc: bool, rpccorsdomain: str, sync_check: bool, console: bool, password_file: TextIO, web_ui: bool, datadir: Optional[str], matrix_server: str, network_id: ChainID, environment_type: Environment, unrecoverable_error_should_crash: bool, pathfinding_service_address: str, pathfinding_max_paths: int, enable_monitoring: bool, resolver_endpoint: str, default_reveal_timeout: BlockTimeout, default_settle_timeout: BlockTimeout, routing_mode: RoutingMode, flat_fee: Tuple[Tuple[TokenAddress, FeeAmount], ...], proportional_fee: Tuple[Tuple[TokenAddress, ProportionalFeeAmount], ...], proportional_imbalance_fee: Tuple[Tuple[TokenAddress, ProportionalFeeAmount], ...], blockchain_query_interval: float, cap_mediation_fees: bool, ** kwargs: Any, # FIXME: not used here, but still receives stuff in smoketest ) -> App: # pylint: disable=too-many-locals,too-many-branches,too-many-statements,unused-argument token_network_registry_deployed_at: Optional[BlockNumber] smart_contracts_start_at: BlockNumber 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 = get_account_and_private_key(account_manager, address, password_file) api_host, api_port = split_endpoint(api_address) if not api_port: api_port = DEFAULT_HTTP_SERVER_PORT domain_list = [] if rpccorsdomain: if "," in rpccorsdomain: for domain in rpccorsdomain.split(","): domain_list.append(str(domain)) else: domain_list.append(str(rpccorsdomain)) # Set up config fee_config = prepare_mediation_fee_config( cli_token_to_flat_fee=flat_fee, cli_token_to_proportional_fee=proportional_fee, cli_token_to_proportional_imbalance_fee=proportional_imbalance_fee, cli_cap_mediation_fees=cap_mediation_fees, ) rest_api_config = RestApiConfig( rest_api_enabled=rpc, web_ui_enabled=rpc and web_ui, cors_domain_list=domain_list, eth_rpc_endpoint=eth_rpc_endpoint, host=api_host, port=api_port, ) config = RaidenConfig( chain_id=network_id, environment_type=environment_type, reveal_timeout=default_reveal_timeout, settle_timeout=default_settle_timeout, console=console, mediation_fees=fee_config, unrecoverable_error_should_crash=unrecoverable_error_should_crash, resolver_endpoint=resolver_endpoint, rest_api=rest_api_config, ) config.blockchain.query_interval = blockchain_query_interval config.services.monitoring_enabled = enable_monitoring config.services.pathfinding_max_paths = pathfinding_max_paths config.transport.server = matrix_server contracts = load_deployed_contracts_data(config, network_id) rpc_client = JSONRPCClient( web3=web3, privkey=privatekey, gas_price_strategy=gas_price, block_num_confirmations=DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS, ) token_network_registry_deployed_at = None if "TokenNetworkRegistry" in contracts: token_network_registry_deployed_at = BlockNumber( contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]["block_number"]) if token_network_registry_deployed_at is None: smart_contracts_start_at = get_smart_contracts_start_at(network_id) else: smart_contracts_start_at = token_network_registry_deployed_at proxy_manager = ProxyManager( rpc_client=rpc_client, contract_manager=ContractManager(config.contracts_path), metadata=ProxyManagerMetadata( token_network_registry_deployed_at= token_network_registry_deployed_at, filters_start_at=smart_contracts_start_at, ), ) api_server: Optional[APIServer] = None if config.rest_api.rest_api_enabled: api_server = start_api_server(rpc_client=rpc_client, config=config.rest_api, eth_rpc_endpoint=eth_rpc_endpoint) if sync_check: check_synced(rpc_client) # The user has the option to launch Raiden with a custom # user deposit contract address. This can be used to load # the addresses for the rest of the deployed contracts. # The steps done here make sure that if a UDC address is provided, # the address has to be valid and all the connected contracts # are configured properly. # If a UDC address was not provided, Raiden would fall back # to using the ones deployed and provided by the raiden-contracts package. if user_deposit_contract_address is not None: if not is_address(user_deposit_contract_address): raise RaidenError("The user deposit address is invalid") deployed_addresses = load_deployment_addresses_from_udc( proxy_manager=proxy_manager, user_deposit_address=user_deposit_contract_address, block_identifier=BLOCK_ID_LATEST, ) else: deployed_addresses = load_deployment_addresses_from_contracts( contracts=contracts) # Load the available matrix servers when no matrix server is given # The list is used in a PFS check if config.transport.server == MATRIX_AUTO_SELECT_SERVER: fetch_available_matrix_servers(config.transport, environment_type) raiden_bundle = raiden_bundle_from_contracts_deployment( proxy_manager=proxy_manager, token_network_registry_address=deployed_addresses. token_network_registry_address, secret_registry_address=deployed_addresses.secret_registry_address, ) services_bundle = services_bundle_from_contracts_deployment( config=config, deployed_addresses=deployed_addresses, proxy_manager=proxy_manager, routing_mode=routing_mode, pathfinding_service_address=pathfinding_service_address, enable_monitoring=enable_monitoring, ) check_ethereum_confirmed_block_is_not_pruned( jsonrpc_client=rpc_client, secret_registry=raiden_bundle.secret_registry, confirmation_blocks=config.blockchain.confirmation_blocks, ) database_path = Path( os.path.join( datadir, f"node_{pex(address)}", f"netid_{network_id}", f"network_{pex(raiden_bundle.token_network_registry.address)}", f"v{RAIDEN_DB_VERSION}_log.db", )) config.database_path = database_path print(f"Raiden is running in {environment_type.value.lower()} mode") print("\nYou are connected to the '{}' network and the DB path is: {}". format(ID_TO_CHAINNAME.get(network_id, network_id), database_path)) matrix_transport = setup_matrix(config.transport, config.services, environment_type, routing_mode) event_handler: EventHandler = RaidenEventHandler() # User should be told how to set fees, if using default fee settings log.debug("Fee Settings", fee_settings=fee_config) has_default_fees = (len(fee_config.token_to_flat_fee) == 0 and len(fee_config.token_to_proportional_fee) == 0 and len(fee_config.token_to_proportional_imbalance_fee) == 0) if has_default_fees: click.secho( "Default fee settings are used. " "If you want use Raiden with mediation fees - flat, proportional and imbalance fees - " "see https://raiden-network.readthedocs.io/en/latest/overview_and_guide.html#firing-it-up", # noqa: E501 fg="yellow", ) # Only send feedback when PFS is used if routing_mode == RoutingMode.PFS: event_handler = PFSFeedbackEventHandler(event_handler) message_handler = MessageHandler() one_to_n_address = (services_bundle.one_to_n.address if services_bundle.one_to_n is not None else None) monitoring_service_address = (services_bundle.monitoring_service.address if services_bundle.monitoring_service is not None else None) raiden_app = App( config=config, rpc_client=rpc_client, proxy_manager=proxy_manager, query_start_block=smart_contracts_start_at, default_registry=raiden_bundle.token_network_registry, default_secret_registry=raiden_bundle.secret_registry, default_service_registry=services_bundle.service_registry, default_user_deposit=services_bundle.user_deposit, default_one_to_n_address=one_to_n_address, default_msc_address=monitoring_service_address, transport=matrix_transport, raiden_event_handler=event_handler, message_handler=message_handler, routing_mode=routing_mode, api_server=api_server, ) raiden_app.start() return raiden_app
def _start_services(self) -> None: if self._options["showconfig"]: print("Configuration Dump:") dump_cmd_options(self._options) dump_module("settings", settings) dump_module("constants", constants) app = run_app(**self._options) gevent_tasks: List[gevent.Greenlet] = list() runnable_tasks: List[Runnable] = list() runnable_tasks.append(app.raiden) domain_list = [] if self._options["rpccorsdomain"]: if "," in self._options["rpccorsdomain"]: for domain in self._options["rpccorsdomain"].split(","): domain_list.append(str(domain)) else: domain_list.append(str(self._options["rpccorsdomain"])) self.raiden_api = RaidenAPI(app.raiden) if self._options["rpc"]: rest_api = RestAPI(self.raiden_api) (api_host, api_port) = split_endpoint(self._options["api_address"]) if not api_port: api_port = Port(settings.DEFAULT_HTTP_SERVER_PORT) api_server = APIServer( rest_api, config={"host": api_host, "port": api_port}, cors_domain_list=domain_list, web_ui=self._options["web_ui"], eth_rpc_endpoint=self._options["eth_rpc_endpoint"], ) api_server.start() url = f"http://{api_host}:{api_port}/" print( f"The Raiden API RPC server is now running at {url}.\n\n See " f"the Raiden documentation for all available endpoints at\n " f"{DOC_URL}" ) runnable_tasks.append(api_server) if self._options["console"]: from raiden.ui.console import Console console = Console(app) console.start() gevent_tasks.append(console) gevent_tasks.append(gevent.spawn(check_version, get_system_spec()["raiden"])) gevent_tasks.append(gevent.spawn(check_gas_reserve, app.raiden)) gevent_tasks.append( gevent.spawn( check_network_id, app.raiden.rpc_client.chain_id, app.raiden.rpc_client.web3 ) ) spawn_user_deposit_task = app.user_deposit and ( self._options["pathfinding_service_address"] or self._options["enable_monitoring"] ) if spawn_user_deposit_task: gevent_tasks.append(gevent.spawn(check_rdn_deposits, app.raiden, app.user_deposit)) self._startup_hook() stop_event: AsyncResult[None] = AsyncResult() def sig_set(sig: Any = None, _frame: Any = None) -> None: stop_event.set(sig) gevent.signal(signal.SIGQUIT, sig_set) gevent.signal(signal.SIGTERM, sig_set) gevent.signal(signal.SIGINT, sig_set) # Make sure RaidenService is the last service in the list. runnable_tasks.reverse() # quit if any task exits, successfully or not for runnable in runnable_tasks: runnable.greenlet.link(stop_event) for task in gevent_tasks: task.link(stop_event) msg = ( "The RaidenService must be last service to stop, since the other " "services depend on it to run. Without this it is not possible to have a " "clean shutdown, e.g. the RestAPI must be stopped before " "RaidenService, otherwise it is possible for a request to be " "processed after the RaidenService was stopped and it will cause a " "crash." ) assert isinstance(runnable_tasks[-1], RaidenService), msg try: stop_event.get() print("Signal received. Shutting down ...") finally: self._shutdown_hook() for task in gevent_tasks: task.kill() for task in runnable_tasks: task.stop() gevent.joinall( set(gevent_tasks + runnable_tasks), app.config.shutdown_timeout, raise_error=True ) app.stop()
def test_split_endpoint_invalid(): with pytest.raises(ValueError): split_endpoint(Endpoint("/invalid/endpoint"))
def run_smoketest( print_step: Callable, args: Dict[str, Any], contract_addresses: Dict[str, Address], token: ContractProxy, ): print_step("Starting Raiden") app = None api_server = None try: app = run_app(**args) raiden_api = RaidenAPI(app.raiden) rest_api = RestAPI(raiden_api) (api_host, api_port) = split_endpoint(args["api_address"]) api_server = APIServer(rest_api, config={"host": api_host, "port": api_port}) api_server.start() block = BlockNumber(app.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS) # Proxies now use the confirmed block hash to query the chain for # prerequisite checks. Wait a bit here to make sure that the confirmed # block hash contains the deployed token network or else things break wait_for_block(raiden=app.raiden, block_number=block, retry_timeout=1.0) raiden_api.channel_open( registry_address=TokenNetworkRegistryAddress( contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY] ), token_address=TokenAddress(to_canonical_address(token.contract.address)), partner_address=ConnectionManager.BOOTSTRAP_ADDR, ) raiden_api.set_total_channel_deposit( registry_address=TokenNetworkRegistryAddress( contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY] ), token_address=TokenAddress(to_canonical_address(token.contract.address)), partner_address=ConnectionManager.BOOTSTRAP_ADDR, total_deposit=TEST_DEPOSIT_AMOUNT, ) token_addresses = [to_checksum_address(token.contract.address)] print_step("Running smoketest") raiden_service = app.raiden token_network_added_events = raiden_service.default_registry.filter_token_added_events() events_token_addresses = [ event["args"]["token_address"] for event in token_network_added_events ] assert events_token_addresses == token_addresses token_networks = views.get_token_identifiers( views.state_from_raiden(raiden_service), raiden_service.default_registry.address ) assert len(token_networks) == 1 channel_state = views.get_channelstate_for( chain_state=views.state_from_raiden(raiden_service), token_network_registry_address=raiden_service.default_registry.address, token_address=token_networks[0], partner_address=ConnectionManager.BOOTSTRAP_ADDR, ) assert channel_state distributable = channel.get_distributable( channel_state.our_state, channel_state.partner_state ) assert distributable == TEST_DEPOSIT_AMOUNT assert distributable == channel_state.our_state.contract_balance assert channel.get_status(channel_state) == ChannelState.STATE_OPENED port_number = raiden_service.config.api_port response = requests.get(f"http://localhost:{port_number}/api/v1/channels") assert response.status_code == HTTPStatus.OK response_json = json.loads(response.content) assert response_json[0]["partner_address"] == to_checksum_address( ConnectionManager.BOOTSTRAP_ADDR ) assert response_json[0]["state"] == "opened" assert int(response_json[0]["balance"]) > 0 finally: if api_server is not None: api_server.stop() api_server.greenlet.get() if app is not None: app.stop() app.raiden.greenlet.get()