def test_token_bucket(): capacity = 2 fill_rate = 2 token_refill = 1.0 / fill_rate # return constant time to have a predictable refill result time = lambda: 1 bucket = TokenBucket(capacity, fill_rate, time) assert bucket.consume(1) == 0 assert bucket.consume(1) == 0 for num in range(1, 9): assert num * token_refill == bucket.consume(1)
def mock_udp( raiden_udp_ports, throttle_capacity, throttle_fill_rate, retry_interval, retries_before_backoff, nat_invitation_timeout, nat_keepalive_retries, nat_keepalive_timeout, ): throttle_policy = TokenBucket(throttle_capacity, throttle_fill_rate) host = '127.0.0.1' port = raiden_udp_ports[0] config = dict( retry_interval=retry_interval, retries_before_backoff=retries_before_backoff, nat_invitation_timeout=nat_invitation_timeout, nat_keepalive_retries=nat_keepalive_retries, nat_keepalive_timeout=nat_keepalive_timeout, ) transport = UDPTransport( MockDiscovery, server._udp_socket((host, port)), # pylint: disable=protected-access throttle_policy, config, ) transport.raiden = MockRaidenService(ADDR) return transport
def setup_udp_or_exit(config, blockchain_service, address, contracts, endpoint_registry_contract_address): check_discovery_registration_gas(blockchain_service, address) try: dicovery_proxy = blockchain_service.discovery( endpoint_registry_contract_address or to_canonical_address( contracts[CONTRACT_ENDPOINT_REGISTRY]["address"])) discovery = ContractDiscovery(blockchain_service.node_address, dicovery_proxy) except ContractVersionMismatch as e: handle_contract_version_mismatch(e) except AddressWithoutCode: handle_contract_no_code("Endpoint Registry", endpoint_registry_contract_address) except AddressWrongContract: handle_contract_wrong_address("Endpoint Registry", endpoint_registry_contract_address) throttle_policy = TokenBucket( config["transport"]["udp"]["throttle_capacity"], config["transport"]["udp"]["throttle_fill_rate"], ) transport = UDPTransport(address, discovery, config["socket"], throttle_policy, config["transport"]["udp"]) return transport, discovery
def test_token_bucket(): capacity = 2 fill_rate = 2 token_refill = 1. / fill_rate # return constant time to have a predictable refill result time = lambda: 1 bucket = TokenBucket( capacity, fill_rate, time, ) assert bucket.consume(1) == 0 assert bucket.consume(1) == 0 for num in range(1, 9): assert num * token_refill == bucket.consume(1)
def _setup_udp( config, blockchain_service, address, contracts, endpoint_registry_contract_address, ): check_discovery_registration_gas(blockchain_service, address) try: dicovery_proxy = blockchain_service.discovery( endpoint_registry_contract_address or to_canonical_address( contracts[CONTRACT_ENDPOINT_REGISTRY]['address'], ), ) discovery = ContractDiscovery( blockchain_service.node_address, dicovery_proxy, ) except ContractVersionMismatch as e: handle_contract_version_mismatch(e) except AddressWithoutCode: handle_contract_no_code('Endpoint Registry', endpoint_registry_contract_address) except AddressWrongContract: handle_contract_wrong_address('Endpoint Registry', endpoint_registry_contract_address) throttle_policy = TokenBucket( config['transport']['udp']['throttle_capacity'], config['transport']['udp']['throttle_fill_rate'], ) transport = UDPTransport( address, discovery, config['socket'], throttle_policy, config['transport']['udp'], ) return transport, discovery
def create_apps( chain_id, blockchain_services, endpoint_discovery_services, token_network_registry_address, secret_registry_address, raiden_udp_ports, reveal_timeout, settle_timeout, database_paths, retry_interval, retries_before_backoff, throttle_capacity, throttle_fill_rate, nat_invitation_timeout, nat_keepalive_retries, nat_keepalive_timeout, environment_type, unrecoverable_error_should_crash, local_matrix_url=None, private_rooms=None, ): """ Create the apps.""" # pylint: disable=too-many-locals services = zip(blockchain_services, endpoint_discovery_services) apps = [] for idx, (blockchain, discovery) in enumerate(services): port = raiden_udp_ports[idx] private_key = blockchain.private_key address = privatekey_to_address(private_key) host = '127.0.0.1' config = { 'chain_id': chain_id, 'environment_type': environment_type, 'unrecoverable_error_should_crash': unrecoverable_error_should_crash, 'privatekey_hex': encode_hex(private_key), 'reveal_timeout': reveal_timeout, 'settle_timeout': settle_timeout, 'database_path': database_paths[idx], 'blockchain': { 'confirmation_blocks': DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS, }, 'transport': { 'udp': { 'external_ip': host, 'external_port': port, 'host': host, 'nat_invitation_timeout': nat_invitation_timeout, 'nat_keepalive_retries': nat_keepalive_retries, 'nat_keepalive_timeout': nat_keepalive_timeout, 'port': port, 'retries_before_backoff': retries_before_backoff, 'retry_interval': retry_interval, 'throttle_capacity': throttle_capacity, 'throttle_fill_rate': throttle_fill_rate, }, }, 'rpc': True, 'console': False, } use_matrix = local_matrix_url is not None if use_matrix: merge_dict( config, { 'transport_type': 'matrix', 'transport': { 'matrix': { 'discovery_room': 'discovery', 'retries_before_backoff': retries_before_backoff, 'retry_interval': retry_interval, 'server': local_matrix_url, 'server_name': local_matrix_url.netloc, 'available_servers': [], 'private_rooms': private_rooms, }, }, }, ) config_copy = App.DEFAULT_CONFIG.copy() config_copy.update(config) registry = blockchain.token_network_registry( token_network_registry_address) secret_registry = blockchain.secret_registry(secret_registry_address) if use_matrix: transport = MatrixTransport(config['transport']['matrix']) else: throttle_policy = TokenBucket( config['transport']['udp']['throttle_capacity'], config['transport']['udp']['throttle_fill_rate'], ) transport = UDPTransport( address, discovery, server._udp_socket((host, port)), # pylint: disable=protected-access throttle_policy, config['transport']['udp'], ) raiden_event_handler = RaidenEventHandler() message_handler = MessageHandler() app = App( config=config_copy, chain=blockchain, query_start_block=0, default_registry=registry, default_secret_registry=secret_registry, transport=transport, raiden_event_handler=raiden_event_handler, message_handler=message_handler, discovery=discovery, ) apps.append(app) return apps
def run_app( address, keystore_path, gas_price, eth_rpc_endpoint, registry_contract_address, secret_registry_contract_address, discovery_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, extra_config=None, **kwargs, ): # 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') address_hex = to_normalized_address(address) if address else None address_hex, privatekey_bin = prompt_account(address_hex, keystore_path, password_file) address = to_canonical_address(address_hex) (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) if datadir is None: datadir = os.path.join(os.path.expanduser('~'), '.raiden') config = deepcopy(App.DEFAULT_CONFIG) if extra_config: merge_dict(config, extra_config) 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 privatekey_hex = hexlify(privatekey_bin) config['privatekey_hex'] = privatekey_hex 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)) try: node_version = web3.version.node # pylint: disable=no-member except ConnectTimeout: raise EthNodeCommunicationError( "Couldn't connect to the ethereum node") supported, _ = is_supported_client(node_version) if not supported: print( 'You need a Byzantium enabled ethereum node. Parity >= 1.7.6 or Geth >= 1.7.2' ) sys.exit(1) rpc_client = JSONRPCClient( web3, privatekey_bin, gasprice=gas_price, ) blockchain_service = BlockChainService(privatekey_bin, rpc_client) net_id = blockchain_service.network_id if net_id != network_id: if network_id in constants.ID_TO_NETWORKNAME and net_id in constants.ID_TO_NETWORKNAME: print(( "The chosen ethereum network '{}' differs from the ethereum client '{}'. " 'Please update your settings.').format( constants.ID_TO_NETWORKNAME[network_id], constants.ID_TO_NETWORKNAME[net_id])) else: print(( "The chosen ethereum network id '{}' differs from the ethereum client '{}'. " 'Please update your settings.').format(network_id, net_id)) sys.exit(1) config['chain_id'] = network_id if sync_check: check_synced(blockchain_service) database_path = os.path.join(datadir, 'netid_%s' % net_id, address_hex[:8], 'log.db') config['database_path'] = database_path print( '\nYou are connected to the \'{}\' network and the DB path is: {}'. format( constants.ID_TO_NETWORKNAME.get(net_id) or net_id, database_path, ), ) contract_addresses_given = (registry_contract_address is not None and secret_registry_contract_address is not None and discovery_contract_address is not None) contract_addresses_known = net_id in constants.ID_TO_NETWORK_CONFIG if not contract_addresses_given and not contract_addresses_known: print(( "There are known contract addresses for network id '{}'. Please provide " 'them in the command line or the configuration file.' ).format(net_id)) sys.exit(1) contract_addresses = constants.ID_TO_NETWORK_CONFIG.get(net_id, dict()) try: token_network_registry = blockchain_service.token_network_registry( registry_contract_address or contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY], ) except ContractVersionMismatch: handle_contract_version_mismatch('token network registry', registry_contract_address) except AddressWithoutCode: handle_contract_no_code('token network registry', registry_contract_address) except AddressWrongContract: handle_contract_wrong_address('token network registry', registry_contract_address) try: secret_registry = blockchain_service.secret_registry( secret_registry_contract_address or contract_addresses[CONTRACT_SECRET_REGISTRY], ) except ContractVersionMismatch: handle_contract_version_mismatch('secret registry', secret_registry_contract_address) except AddressWithoutCode: handle_contract_no_code('secret registry', secret_registry_contract_address) except AddressWrongContract: handle_contract_wrong_address('secret registry', secret_registry_contract_address) discovery = None if transport == 'udp': check_discovery_registration_gas(blockchain_service, address) try: dicovery_proxy = blockchain_service.discovery( discovery_contract_address or contract_addresses[CONTRACT_ENDPOINT_REGISTRY], ) discovery = ContractDiscovery( blockchain_service.node_address, dicovery_proxy, ) except ContractVersionMismatch: handle_contract_version_mismatch('discovery', discovery_contract_address) except AddressWithoutCode: handle_contract_no_code('discovery', discovery_contract_address) except AddressWrongContract: handle_contract_wrong_address('discovery', discovery_contract_address) throttle_policy = TokenBucket( config['transport']['udp']['throttle_capacity'], config['transport']['udp']['throttle_fill_rate'], ) transport = UDPTransport( discovery, mapped_socket.socket, throttle_policy, config['transport']['udp'], ) elif transport == 'matrix': try: transport = MatrixTransport(config['transport']['matrix']) except RaidenError as ex: click.secho(f'FATAL: {ex}', fg='red') sys.exit(1) else: raise RuntimeError(f'Unknown transport type "{transport}" given') try: chain_config = constants.ID_TO_NETWORK_CONFIG.get(net_id, {}) start_block = chain_config.get(constants.START_QUERY_BLOCK_KEY, 0) raiden_app = App( config=config, chain=blockchain_service, query_start_block=start_block, default_registry=token_network_registry, default_secret_registry=secret_registry, transport=transport, discovery=discovery, ) except RaidenError as e: click.secho(f'FATAL: {e}', fg='red') sys.exit(1) try: raiden_app.start() except filelock.Timeout: name_or_id = constants.ID_TO_NETWORKNAME.get(network_id, network_id) print( f'FATAL: Another Raiden instance already running for account {address_hex} on ' f'network id {name_or_id}', ) sys.exit(1) return raiden_app
def run( privatekey, registry_contract_address, secret_registry_contract_address, discovery_contract_address, listen_address, structlog, logfile, scenario, stage_prefix, ): # pylint: disable=unused-argument # TODO: only enabled structlog on "initiators" structlog.configure(structlog, log_file=logfile) (listen_host, listen_port) = split_endpoint(listen_address) config = App.DEFAULT_CONFIG.copy() config['host'] = listen_host config['port'] = listen_port config['privatekey_hex'] = privatekey privatekey_bin = decode_hex(privatekey) rpc_client = JSONRPCClient( '127.0.0.1', 8545, privatekey_bin, ) blockchain_service = BlockChainService( privatekey_bin, rpc_client, GAS_PRICE, ) discovery = ContractDiscovery( blockchain_service, decode_hex(discovery_contract_address), ) registry = blockchain_service.registry( registry_contract_address, ) secret_registry = blockchain_service.secret_registry( secret_registry_contract_address, ) throttle_policy = TokenBucket( config['protocol']['throttle_capacity'], config['protocol']['throttle_fill_rate'], ) transport = UDPTransport( discovery, server._udp_socket((listen_host, listen_port)), throttle_policy, config['protocol'], dict(), ) app = App( config, blockchain_service, registry, secret_registry, transport, discovery, ) app.discovery.register( app.raiden.address, listen_host, listen_port, ) app.raiden.install_payment_network_filters(app.raiden.default_registry.address) if scenario: script = json.load(scenario) tools = ConsoleTools( app.raiden, app.discovery, app.config['settle_timeout'], app.config['reveal_timeout'], ) transfers_by_peer = {} tokens = script['tokens'] token_address = None peer = None our_node = hexlify(app.raiden.address) log.warning('our address is {}'.format(our_node)) for token in tokens: # skip tokens that we're not part of nodes = token['channels'] if our_node not in nodes: continue partner_nodes = [ node for node in nodes if node != our_node ] # allow for prefunded tokens if 'token_address' in token: token_address = token['token_address'] else: token_address = tools.create_token(registry_contract_address) transfers_with_amount = token['transfers_with_amount'] # FIXME: in order to do bidirectional channels, only one side # (i.e. only token['channels'][0]) should # open; others should join by calling # raiden.api.deposit, AFTER the channel came alive! # NOTE: leaving unidirectional for now because it most # probably will get to higher throughput log.warning('Waiting for all nodes to come online') api = RaidenAPI(app.raiden) for node in partner_nodes: api.start_health_check_for(node) while True: all_reachable = all( api.get_node_network_state(node) == NODE_NETWORK_REACHABLE for node in partner_nodes ) if all_reachable: break gevent.sleep(5) log.warning('All nodes are online') if our_node != nodes[-1]: our_index = nodes.index(our_node) peer = nodes[our_index + 1] tools.token_network_register(app.raiden.default_registry.address, token_address) amount = transfers_with_amount[nodes[-1]] while True: try: app.discovery.get(peer.decode('hex')) break except KeyError: log.warning('Error: peer {} not found in discovery'.format(peer)) time.sleep(random.randrange(30)) while True: try: log.warning('Opening channel with {} for {}'.format(peer, token_address)) api.channel_open(app.raiden.default_registry.address, token_address, peer) break except KeyError: log.warning('Error: could not open channel with {}'.format(peer)) time.sleep(random.randrange(30)) while True: try: log.warning('Funding channel with {} for {}'.format(peer, token_address)) api.channel_deposit( app.raiden.default_registry.address, token_address, peer, amount, ) break except Exception: log.warning('Error: could not deposit {} for {}'.format(amount, peer)) time.sleep(random.randrange(30)) if our_index == 0: last_node = nodes[-1] transfers_by_peer[last_node] = int(amount) else: peer = nodes[-2] if stage_prefix is not None: open('{}.stage1'.format(stage_prefix), 'a').close() log.warning('Done with initialization, waiting to continue...') event = gevent.event.Event() gevent.signal(signal.SIGUSR2, event.set) event.wait() transfer_results = {'total_time': 0, 'timestamps': []} def transfer(token_address, amount_per_transfer, total_transfers, peer, is_async): def transfer_(): log.warning('Making {} transfers to {}'.format(total_transfers, peer)) initial_time = time.time() times = [0] * total_transfers for index in range(total_transfers): RaidenAPI(app.raiden).transfer( app.raiden.default_registry.address, token_address.decode('hex'), amount_per_transfer, peer, ) times[index] = time.time() transfer_results['total_time'] = time.time() - initial_time transfer_results['timestamps'] = times log.warning('Making {} transfers took {}'.format( total_transfers, transfer_results['total_time'])) log.warning('Times: {}'.format(times)) if is_async: return gevent.spawn(transfer_) else: transfer_() # If sending to multiple targets, do it asynchronously, otherwise # keep it simple and just send to the single target on my thread. if len(transfers_by_peer) > 1: greenlets = [] for peer_, amount in transfers_by_peer.items(): greenlet = transfer(token_address, 1, amount, peer_, True) if greenlet is not None: greenlets.append(greenlet) gevent.joinall(greenlets) elif len(transfers_by_peer) == 1: for peer_, amount in transfers_by_peer.items(): transfer(token_address, 1, amount, peer_, False) log.warning('Waiting for termination') open('{}.stage2'.format(stage_prefix), 'a').close() log.warning('Waiting for transfers to finish, will write results...') event = gevent.event.Event() gevent.signal(signal.SIGUSR2, event.set) event.wait() open('{}.stage3'.format(stage_prefix), 'a').close() event = gevent.event.Event() gevent.signal(signal.SIGQUIT, event.set) gevent.signal(signal.SIGTERM, event.set) gevent.signal(signal.SIGINT, event.set) event.wait() else: log.warning('No scenario file supplied, doing nothing!') open('{}.stage2'.format(stage_prefix), 'a').close() event = gevent.event.Event() gevent.signal(signal.SIGQUIT, event.set) gevent.signal(signal.SIGTERM, event.set) gevent.signal(signal.SIGINT, event.set) event.wait() app.stop()
def create_apps( chain_id, blockchain_services, endpoint_discovery_services, token_network_registry_address, secret_registry_address, raiden_udp_ports, reveal_timeout, settle_timeout, database_paths, retry_interval, retries_before_backoff, throttle_capacity, throttle_fill_rate, nat_invitation_timeout, nat_keepalive_retries, nat_keepalive_timeout, local_matrix_url=None, ): """ Create the apps.""" # pylint: disable=too-many-locals services = zip(blockchain_services, endpoint_discovery_services) apps = [] for idx, (blockchain, discovery) in enumerate(services): port = raiden_udp_ports[idx] private_key = blockchain.private_key nodeid = privatekey_to_address(private_key) host = '127.0.0.1' discovery.register(nodeid, host, port) config = { 'chain_id': chain_id, 'host': host, 'port': port, 'external_ip': host, 'external_port': port, 'privatekey_hex': hexlify(private_key), 'reveal_timeout': reveal_timeout, 'settle_timeout': settle_timeout, 'database_path': database_paths[idx], 'transport': { 'retry_interval': retry_interval, 'retries_before_backoff': retries_before_backoff, 'throttle_capacity': throttle_capacity, 'throttle_fill_rate': throttle_fill_rate, 'nat_invitation_timeout': nat_invitation_timeout, 'nat_keepalive_retries': nat_keepalive_retries, 'nat_keepalive_timeout': nat_keepalive_timeout, }, 'rpc': True, 'console': False, } use_matrix = local_matrix_url is not None if use_matrix: config.update({ 'transport_type': 'matrix', 'matrix': { 'server': local_matrix_url, 'server_name': 'matrix.local.raiden', 'discovery_room': { 'alias_fragment': 'discovery', 'server': 'matrix.local.raiden', }, }, }) if 'TRAVIS' in environ: config.update({'login_retry_wait': 1.5}) config_copy = App.DEFAULT_CONFIG.copy() config_copy.update(config) registry = blockchain.token_network_registry( token_network_registry_address) secret_registry = blockchain.secret_registry(secret_registry_address) if use_matrix: transport = MatrixTransport(config['matrix']) else: throttle_policy = TokenBucket( config['transport']['throttle_capacity'], config['transport']['throttle_fill_rate'], ) transport = UDPTransport( discovery, server._udp_socket((host, port)), # pylint: disable=protected-access throttle_policy, config['transport'], ) app = App( config_copy, blockchain, registry, secret_registry, transport, discovery, ) apps.append(app) return apps
def tps_run( host, port, config, privatekey, rpc_server, registry_address, secret_registry_address, token_address, transfer_amount, parallel): # pylint: disable=too-many-locals,too-many-arguments ourprivkey, _ = hostport_to_privkeyaddr(host, port) rpc_connection = rpc_server.split(':') rpc_connection = (rpc_connection[0], int(rpc_connection[1])) with codecs.open(config, encoding='utf8') as handler: config = yaml.load(handler) config['host'] = host config['port'] = port config['privkey'] = ourprivkey rpc_connection = rpc_server.split(':') host, port = (rpc_connection[0], int(rpc_connection[1])) rpc_client = JSONRPCClient( host, port, privatekey, ) blockchain_service = BlockChainService(privatekey, rpc_client) discovery = Discovery() found_ouraddress = False for node in config['nodes']: _, address = hostport_to_privkeyaddr(node['host'], node['port']) discovery.register(address, node['host'], node['port']) if host == node['host'] and str(port) == node['port']: found_ouraddress = True if not found_ouraddress: print('We are not registered in the configuration file') sys.exit(1) throttle_policy = TokenBucket( config['protocol']['throttle_capacity'], config['protocol']['throttle_fill_rate'], ) transport = UDPTransport( discovery, server._udp_socket((host, port)), throttle_policy, config['protocol'], ) app = App( config=config, chain=blockchain_service, query_start_block=0, default_registry=registry_address, default_secret_registry=secret_registry_address, transport=transport, discovery=discovery, ) for _ in range(parallel): gevent.spawn(random_transfer, app, token_address, transfer_amount) # wait for interrupt event = gevent.event.Event() gevent.signal(signal.SIGQUIT, event.set) gevent.signal(signal.SIGTERM, event.set) gevent.signal(signal.SIGINT, event.set) event.wait() app.stop()
def create_apps( blockchain_services, endpoint_discovery_services, registry_address, raiden_udp_ports, reveal_timeout, settle_timeout, database_paths, retry_interval, retries_before_backoff, throttle_capacity, throttle_fill_rate, nat_invitation_timeout, nat_keepalive_retries, nat_keepalive_timeout, use_matrix=False, local_matrix_url=None, ): """ Create the apps.""" # pylint: disable=too-many-locals services = zip(blockchain_services, endpoint_discovery_services) apps = [] for idx, (blockchain, discovery) in enumerate(services): port = raiden_udp_ports[idx] private_key = blockchain.private_key nodeid = privatekey_to_address(private_key) host = '127.0.0.1' discovery.register(nodeid, host, port) config = { 'host': host, 'port': port, 'external_ip': host, 'external_port': port, 'privatekey_hex': hexlify(private_key), 'reveal_timeout': reveal_timeout, 'settle_timeout': settle_timeout, 'database_path': database_paths[idx], 'protocol': { 'retry_interval': retry_interval, 'retries_before_backoff': retries_before_backoff, 'throttle_capacity': throttle_capacity, 'throttle_fill_rate': throttle_fill_rate, 'nat_invitation_timeout': nat_invitation_timeout, 'nat_keepalive_retries': nat_keepalive_retries, 'nat_keepalive_timeout': nat_keepalive_timeout, }, 'rpc': True, 'console': False, } config_copy = App.DEFAULT_CONFIG.copy() config_copy.update(config) if use_matrix: if local_matrix_url is not None: matrix_config = { 'server': local_matrix_url, } else: matrix_config = { 'client_class': MockMatrixClient, 'server': 'http://matrix.mock', } config.update({ 'transport_type': 'matrix', 'matrix': matrix_config }) registry = blockchain.registry(registry_address) if use_matrix: transport = MatrixTransport(config['matrix']) else: throttle_policy = TokenBucket( config['protocol']['throttle_capacity'], config['protocol']['throttle_fill_rate']) transport = UDPTransport( discovery, server._udp_socket((host, port)), # pylint: disable=protected-access throttle_policy, config['protocol'], ) app = App( config_copy, blockchain, registry, transport, discovery, ) apps.append(app) return apps
def run_app( address, keystore_path, gas_price, eth_rpc_endpoint, registry_contract_address, secret_registry_contract_address, discovery_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, lnd_address, extra_config=None, **kwargs, ): # 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') address_hex = to_normalized_address(address) if address else None address_hex, privatekey_bin = prompt_account(address_hex, keystore_path, password_file) address = to_canonical_address(address_hex) (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) if datadir is None: datadir = os.path.join(os.path.expanduser('~'), '.raiden') config = deepcopy(App.DEFAULT_CONFIG) if extra_config: merge_dict(config, extra_config) config['host'] = listen_host config['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['external_ip'] = mapped_socket.external_ip config['external_port'] = mapped_socket.external_port config['transport_type'] = transport config['matrix']['server'] = matrix_server config['transport']['nat_keepalive_retries'] = DEFAULT_NAT_KEEPALIVE_RETRIES timeout = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config['transport']['nat_keepalive_timeout'] = timeout privatekey_hex = hexlify(privatekey_bin) config['privatekey_hex'] = privatekey_hex rpc_host, rpc_port = eth_endpoint_to_hostport(eth_rpc_endpoint) rpc_client = JSONRPCClient( rpc_host, rpc_port, privatekey_bin, gas_price, ) blockchain_service = BlockChainService(privatekey_bin, rpc_client) net_id = blockchain_service.network_id if net_id != network_id: if network_id in constants.ID_TO_NETWORKNAME and net_id in constants.ID_TO_NETWORKNAME: print(( "The chosen ethereum network '{}' differs from the ethereum client '{}'. " 'Please update your settings.' ).format(constants.ID_TO_NETWORKNAME[network_id], constants.ID_TO_NETWORKNAME[net_id])) else: print(( "The chosen ethereum network id '{}' differs from the ethereum client '{}'. " 'Please update your settings.' ).format(network_id, net_id)) sys.exit(1) config['chain_id'] = network_id if sync_check: check_synced(blockchain_service) database_path = os.path.join(datadir, 'netid_%s' % net_id, address_hex[:8], 'log.db') config['database_path'] = database_path print( '\nYou are connected to the \'{}\' network and the DB path is: {}'.format( constants.ID_TO_NETWORKNAME.get(net_id) or net_id, database_path, ), ) contract_addresses_given = ( registry_contract_address is not None and secret_registry_contract_address is not None and discovery_contract_address is not None ) contract_addresses_known = net_id in constants.ID_TO_NETWORK_CONFIG if not contract_addresses_given and not contract_addresses_known: print(( "There are known contract addresses for network id '{}'. Please provide " 'them in the command line or the configuration file.' ).format(net_id)) sys.exit(1) contract_addresses = constants.ID_TO_NETWORK_CONFIG.get(net_id, dict()) try: token_network_registry = blockchain_service.token_network_registry( registry_contract_address or contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY], ) except ContractVersionMismatch: print( 'Deployed registry contract version mismatch. ' 'Please update your Raiden installation.', ) sys.exit(1) try: secret_registry = blockchain_service.secret_registry( secret_registry_contract_address or contract_addresses[CONTRACT_SECRET_REGISTRY], ) except ContractVersionMismatch: print( 'Deployed secret registry contract version mismatch. ' 'Please update your Raiden installation.', ) sys.exit(1) discovery = None if transport == 'udp': check_discovery_registration_gas(blockchain_service, address) try: dicovery_proxy = blockchain_service.discovery( discovery_contract_address or contract_addresses[CONTRACT_ENDPOINT_REGISTRY], ) discovery = ContractDiscovery( blockchain_service.node_address, dicovery_proxy, ) except ContractVersionMismatch: print('Deployed discovery contract version mismatch. ' 'Please update your Raiden installation.') sys.exit(1) throttle_policy = TokenBucket( config['transport']['throttle_capacity'], config['transport']['throttle_fill_rate'], ) transport = UDPTransport( discovery, mapped_socket.socket, throttle_policy, config['transport'], ) elif transport == 'matrix': # matrix gets spammed with the default retry-interval of 1s, wait a little more if config['transport']['retry_interval'] == DEFAULT_TRANSPORT_RETRY_INTERVAL: config['transport']['retry_interval'] *= 5 try: transport = MatrixTransport(config['matrix']) except RaidenError as ex: click.secho(f'FATAL: {ex}', fg='red') sys.exit(1) else: raise RuntimeError(f'Unknown transport type "{transport}" given') try: chain_config = constants.ID_TO_NETWORK_CONFIG.get(net_id, {}) start_block = chain_config.get(constants.START_QUERY_BLOCK_KEY, 0) #vincent config['lnd_address'] = lnd_address raiden_app = App( config=config, chain=blockchain_service, query_start_block=start_block, default_registry=token_network_registry, default_secret_registry=secret_registry, transport=transport, discovery=discovery, ) except RaidenError as e: click.secho(f'FATAL: {e}', fg='red') sys.exit(1) return raiden_app
def app( address, keystore_path, gas_price, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, rpccorsdomain, mapped_socket, log_config, log_file, log_json, max_unresponsive_time, send_ping_time, api_address, rpc, sync_check, console, password_file, web_ui, datadir, eth_client_communication, nat, transport, matrix_server, network_id, ): # pylint: disable=too-many-locals,too-many-branches,too-many-statements,unused-argument from raiden.app import App from raiden.network.blockchain_service import BlockChainService if transport == 'udp' and not mapped_socket: raise RuntimeError('Missing socket') address_hex = address_encoder(address) if address else None address_hex, privatekey_bin = prompt_account(address_hex, keystore_path, password_file) address = address_decoder(address_hex) (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) if datadir is None: datadir = os.path.join(os.path.expanduser('~'), '.raiden') config = deepcopy(App.DEFAULT_CONFIG) config['host'] = listen_host config['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['external_ip'] = mapped_socket.external_ip config['external_port'] = mapped_socket.external_port config['transport_type'] = transport config['matrix']['server'] = matrix_server config['protocol']['nat_keepalive_retries'] = DEFAULT_NAT_KEEPALIVE_RETRIES timeout = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config['protocol']['nat_keepalive_timeout'] = timeout privatekey_hex = hexlify(privatekey_bin) config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint # Fallback to default port if only an IP address is given rpc_port = 8545 if eth_rpc_endpoint.startswith('http://'): endpoint = eth_rpc_endpoint[len('http://'):] rpc_port = 80 elif eth_rpc_endpoint.startswith('https://'): endpoint = eth_rpc_endpoint[len('https://'):] rpc_port = 443 if ':' not in endpoint: # no port was given in url rpc_host = endpoint else: rpc_host, rpc_port = split_endpoint(endpoint) rpc_client = JSONRPCClient( rpc_host, rpc_port, privatekey_bin, gas_price, ) blockchain_service = BlockChainService( privatekey_bin, rpc_client, gas_price, ) # this assumes the eth node is already online check_json_rpc(rpc_client) net_id = blockchain_service.network_id if net_id != network_id: if network_id in ID_TO_NETWORKNAME and net_id in ID_TO_NETWORKNAME: print(( "The chosen ethereum network '{}' differs from the ethereum client '{}'. " 'Please update your settings.').format( ID_TO_NETWORKNAME[network_id], ID_TO_NETWORKNAME[net_id])) else: print(( "The chosen ethereum network id '{}' differs from the ethereum client '{}'. " 'Please update your settings.').format(network_id, net_id)) sys.exit(1) if sync_check: check_synced(blockchain_service) database_path = os.path.join(datadir, 'netid_%s' % net_id, address_hex[:8], 'log.db') config['database_path'] = database_path print('You are connected to the \'{}\' network and the DB path is: {}'. format( ID_TO_NETWORKNAME[net_id], database_path, )) try: registry = blockchain_service.registry(registry_contract_address, ) except ContractVersionMismatch: print('Deployed registry contract version mismatch. ' 'Please update your Raiden installation.') sys.exit(1) discovery = None if transport == 'udp': check_discovery_registration_gas(blockchain_service, address) try: discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address)) except ContractVersionMismatch: print('Deployed discovery contract version mismatch. ' 'Please update your Raiden installation.') sys.exit(1) throttle_policy = TokenBucket(config['protocol']['throttle_capacity'], config['protocol']['throttle_fill_rate']) transport = UDPTransport( discovery, mapped_socket.socket, throttle_policy, config['protocol'], ) elif transport == 'matrix': transport = MatrixTransport(config['matrix']) else: raise RuntimeError(f'Unknown transport type "{transport}" given') raiden_app = App( config, blockchain_service, registry, transport, discovery, ) return raiden_app
def create_apps( chain_id, contracts_path, blockchain_services, endpoint_discovery_services, token_network_registry_address, one_to_n_address: Optional[Address], secret_registry_address, service_registry_address, user_deposit_address, raiden_udp_ports, reveal_timeout, settle_timeout, database_basedir, retry_interval, retries_before_backoff, throttle_capacity, throttle_fill_rate, nat_invitation_timeout, nat_keepalive_retries, nat_keepalive_timeout, environment_type, unrecoverable_error_should_crash, local_matrix_url=None, private_rooms=None, global_rooms=None, ): """ Create the apps.""" # pylint: disable=too-many-locals services = zip(blockchain_services, endpoint_discovery_services, raiden_udp_ports) apps = [] for idx, (blockchain, discovery, port) in enumerate(services): address = blockchain.client.address host = "127.0.0.1" database_path = database_from_privatekey(base_dir=database_basedir, app_number=idx) config = { "chain_id": chain_id, "environment_type": environment_type, "unrecoverable_error_should_crash": unrecoverable_error_should_crash, "reveal_timeout": reveal_timeout, "settle_timeout": settle_timeout, "contracts_path": contracts_path, "database_path": database_path, "blockchain": {"confirmation_blocks": DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS}, "transport": { "udp": { "external_ip": host, "external_port": port, "host": host, "nat_invitation_timeout": nat_invitation_timeout, "nat_keepalive_retries": nat_keepalive_retries, "nat_keepalive_timeout": nat_keepalive_timeout, "port": port, "retries_before_backoff": retries_before_backoff, "retry_interval": retry_interval, "throttle_capacity": throttle_capacity, "throttle_fill_rate": throttle_fill_rate, } }, "rpc": True, "console": False, } use_matrix = local_matrix_url is not None if use_matrix: merge_dict( config, { "transport_type": "matrix", "transport": { "matrix": { "global_rooms": global_rooms, "retries_before_backoff": retries_before_backoff, "retry_interval": retry_interval, "server": local_matrix_url, "server_name": local_matrix_url.netloc, "available_servers": [], "private_rooms": private_rooms, } }, }, ) config_copy = deepcopy(App.DEFAULT_CONFIG) config_copy.update(config) registry = blockchain.token_network_registry(token_network_registry_address) secret_registry = blockchain.secret_registry(secret_registry_address) service_registry = None if service_registry_address: service_registry = blockchain.service_registry(service_registry_address) user_deposit = None if user_deposit_address: user_deposit = blockchain.user_deposit(user_deposit_address) if use_matrix: transport = MatrixTransport(config["transport"]["matrix"]) else: throttle_policy = TokenBucket( config["transport"]["udp"]["throttle_capacity"], config["transport"]["udp"]["throttle_fill_rate"], ) transport = UDPTransport( address, discovery, server._udp_socket((host, port)), # pylint: disable=protected-access throttle_policy, config["transport"]["udp"], ) raiden_event_handler = RaidenEventHandler() hold_handler = HoldRaidenEventHandler(raiden_event_handler) message_handler = WaitForMessage() app = App( config=config_copy, chain=blockchain, query_start_block=BlockNumber(0), default_registry=registry, default_one_to_n_address=one_to_n_address, default_secret_registry=secret_registry, default_service_registry=service_registry, transport=transport, raiden_event_handler=hold_handler, message_handler=message_handler, discovery=discovery, user_deposit=user_deposit, ) apps.append(app) return apps
def app( address, keystore_path, gas_price, eth_rpc_endpoint, registry_contract_address, secret_registry_contract_address, discovery_contract_address, listen_address, rpccorsdomain, mapped_socket, log_config, log_file, log_json, max_unresponsive_time, send_ping_time, api_address, rpc, sync_check, console, password_file, web_ui, datadir, nat, transport, matrix_server, network_id, config_file, extra_config=None, ): # 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') address_hex = to_normalized_address(address) if address else None address_hex, privatekey_bin = prompt_account(address_hex, keystore_path, password_file) address = to_canonical_address(address_hex) (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) if datadir is None: datadir = os.path.join(os.path.expanduser('~'), '.raiden') config = deepcopy(App.DEFAULT_CONFIG) if extra_config: merge_dict(config, extra_config) config['host'] = listen_host config['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['external_ip'] = mapped_socket.external_ip config['external_port'] = mapped_socket.external_port config['transport_type'] = transport config['matrix']['server'] = matrix_server config['transport'][ 'nat_keepalive_retries'] = DEFAULT_NAT_KEEPALIVE_RETRIES timeout = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config['transport']['nat_keepalive_timeout'] = timeout privatekey_hex = hexlify(privatekey_bin) config['privatekey_hex'] = privatekey_hex rpc_host, rpc_port = eth_endpoint_to_hostport(eth_rpc_endpoint) rpc_client = JSONRPCClient( rpc_host, rpc_port, privatekey_bin, gas_price, ) blockchain_service = BlockChainService(privatekey_bin, rpc_client) # this assumes the eth node is already online check_json_rpc(blockchain_service) net_id = blockchain_service.network_id if net_id != network_id: if network_id in constants.ID_TO_NETWORKNAME and net_id in constants.ID_TO_NETWORKNAME: print(( "The chosen ethereum network '{}' differs from the ethereum client '{}'. " 'Please update your settings.').format( constants.ID_TO_NETWORKNAME[network_id], constants.ID_TO_NETWORKNAME[net_id])) else: print(( "The chosen ethereum network id '{}' differs from the ethereum client '{}'. " 'Please update your settings.').format(network_id, net_id)) sys.exit(1) config['chain_id'] = network_id if sync_check: check_synced(blockchain_service) database_path = os.path.join(datadir, 'netid_%s' % net_id, address_hex[:8], 'log.db') config['database_path'] = database_path print( 'You are connected to the \'{}\' network and the DB path is: {}'. format( constants.ID_TO_NETWORKNAME.get(net_id) or net_id, database_path, ), ) try: token_network_registry = blockchain_service.token_network_registry( registry_contract_address, ) except ContractVersionMismatch: print( 'Deployed registry contract version mismatch. ' 'Please update your Raiden installation.', ) sys.exit(1) try: secret_registry = blockchain_service.secret_registry( secret_registry_contract_address, ) except ContractVersionMismatch: print( 'Deployed secret registry contract version mismatch. ' 'Please update your Raiden installation.', ) sys.exit(1) discovery = None if transport == 'udp': check_discovery_registration_gas(blockchain_service, address) try: discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address), ) except ContractVersionMismatch: print('Deployed discovery contract version mismatch. ' 'Please update your Raiden installation.') sys.exit(1) throttle_policy = TokenBucket( config['transport']['throttle_capacity'], config['transport']['throttle_fill_rate'], ) transport = UDPTransport( discovery, mapped_socket.socket, throttle_policy, config['transport'], ) elif transport == 'matrix': # matrix gets spammed with the default retry-interval of 1s, wait a little more if config['transport'][ 'retry_interval'] == DEFAULT_TRANSPORT_RETRY_INTERVAL: config['transport']['retry_interval'] *= 5 transport = MatrixTransport(config['matrix']) else: raise RuntimeError(f'Unknown transport type "{transport}" given') try: raiden_app = App( config, blockchain_service, token_network_registry, secret_registry, transport, discovery, ) except (InvalidSettleTimeout, InvalidDBData) as e: print(f'FATAL: {str(e)}') sys.exit(1) return raiden_app