def setup_tps(rpc_server, config_path, channelmanager_address, asset_address, deposit): """ Creates the required contract and the fully connected Raiden network prior to running the test. Args: rpc_server (str): A string in the format '{host}:{port}' used to define the JSON-RPC end-point. config_path (str): A full/relative path to the yaml configuration file. channelmanager_address (str): The address of the channel manager contract. asset_address (str): The address of the asset used for testing. deposit (int): The default deposit that will be made for all test nodes. """ # TODO: # - create/register the channel manager rpc_connection = rpc_server.split(':') rpc_connection = (rpc_connection[0], int(rpc_connection[1])) blockchain_service = BlockChainService(rpc_connection, channelmanager_address) blockchain_service.new_channel_manager_contract(asset_address=asset_address) with codecs.open(config_path, encoding='utf8') as handler: config = yaml.load(handler) node_addresses = [] for node in config['nodes']: privkey = sha3('{}:{}'.format(node['host'], node['port'])) node_addresses.append(privtoaddr(privkey)) random_raiden_network(asset_address, blockchain_service, node_addresses, deposit)
def setup_tps(rpc_server, config_path, channelmanager_address, asset_address, deposit): """ Creates the required contract and the fully connected Raiden network prior to running the test. Args: rpc_server (str): A string in the format '{host}:{port}' used to define the JSON-RPC end-point. config_path (str): A full/relative path to the yaml configuration file. channelmanager_address (str): The address of the channel manager contract. asset_address (str): The address of the asset used for testing. deposit (int): The default deposit that will be made for all test nodes. """ # TODO: # - create/register the channel manager rpc_connection = rpc_server.split(':') rpc_connection = (rpc_connection[0], int(rpc_connection[1])) blockchain_service = BlockChainService(rpc_connection, channelmanager_address) blockchain_service.new_channel_manager_contract( asset_address=asset_address) with codecs.open(config_path, encoding='utf8') as handler: config = yaml.load(handler) node_addresses = [] for node in config['nodes']: privkey = sha3('{}:{}'.format(node['host'], node['port'])) node_addresses.append(privtoaddr(privkey)) random_raiden_network(asset_address, blockchain_service, node_addresses, deposit)
def _jsonrpc_services( deploy_key, deploy_client, private_keys, verbose, poll_timeout, registry_address=None): # we cannot instantiate BlockChainService without a registry, so first # deploy it directly with a JSONRPCClient if registry_address is None: address = privatekey_to_address(deploy_key) registry_path = get_contract_path('Registry.sol') registry_contracts = compile_file(registry_path, libraries=dict()) log.info('Deploying registry contract') registry_proxy = deploy_client.deploy_solidity_contract( address, 'Registry', registry_contracts, dict(), tuple(), contract_path=registry_path, gasprice=default_gasprice, timeout=poll_timeout, ) registry_address = registry_proxy.address # at this point the blockchain must be running, this will overwrite the # method so even if the client is patched twice, it should work fine patch_send_transaction(deploy_client) deploy_blockchain = BlockChainService( deploy_key, registry_address, deploy_client, ) host = '0.0.0.0' blockchain_services = list() for privkey in private_keys: rpc_client = JSONRPCClient( privkey=privkey, host=host, port=deploy_client.port, print_communication=False, ) patch_send_transaction(rpc_client) patch_send_message(rpc_client) blockchain = BlockChainService( privkey, registry_address, rpc_client, ) blockchain_services.append(blockchain) return BlockchainServices(deploy_blockchain, blockchain_services)
def _jsonrpc_services( deploy_key, private_keys, verbose, poll_timeout, rpc_port, registry_address=None): host = '0.0.0.0' print_communication = verbose > 6 deploy_client = JSONRPCClient( host=host, port=rpc_port, privkey=deploy_key, print_communication=print_communication, ) # we cannot instantiate BlockChainService without a registry, so first # deploy it directly with a JSONRPCClient if registry_address is None: address = privatekey_to_address(deploy_key) patch_send_transaction(deploy_client) patch_send_message(deploy_client) registry_path = get_contract_path('Registry.sol') registry_contracts = compile_file(registry_path, libraries=dict()) log.info('Deploying registry contract') registry_proxy = deploy_client.deploy_solidity_contract( address, 'Registry', registry_contracts, dict(), tuple(), contract_path=registry_path, gasprice=default_gasprice, timeout=poll_timeout, ) registry_address = registry_proxy.address deploy_blockchain = BlockChainService( deploy_key, registry_address, host, deploy_client.port, ) blockchain_services = list() for privkey in private_keys: blockchain = BlockChainService( privkey, registry_address, host, deploy_client.port, ) blockchain_services.append(blockchain) return BlockchainServices(deploy_blockchain, blockchain_services)
def tps_run(host, port, config, rpc_server, channelmanager_address, asset_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 blockchain_service = BlockChainService(rpc_server, channelmanager_address) 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) app = App(config, blockchain_service, discovery) for asset_address in blockchain_service.asset_addresses: all_netting_contracts = blockchain_service.nettingaddresses_by_asset_participant( asset, app.raiden.address, ) for netting_contract_address in all_netting_contracts: app.raiden.setup_channel( asset, netting_contract_address, app.config['reveal_timeout'], ) for _ in range(parallel): gevent.spawn(random_transfer, app, asset_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 _jsonrpc_services(private_keys, verbose, poll_timeout): print_communication = True if verbose > 7 else False privatekey = private_keys[0] address = privatekey_to_address(privatekey) jsonrpc_client = JSONRPCClient( host='0.0.0.0', privkey=privatekey, print_communication=print_communication, ) patch_send_transaction(jsonrpc_client) registry_path = get_contract_path('Registry.sol') registry_contracts = compile_file(registry_path, libraries=dict()) log.info('Deploying registry contract') registry_proxy = jsonrpc_client.deploy_solidity_contract( address, 'Registry', registry_contracts, dict(), tuple(), timeout=poll_timeout, ) registry_address = registry_proxy.address blockchain_services = list() for privkey in private_keys: blockchain = BlockChainService( privkey, registry_address, ) blockchain_services.append(blockchain) return blockchain_services
def app(privatekey, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, external_listen_address, logging, logfile): slogging.configure(logging, log_file=logfile) if not external_listen_address: # notify('if you are behind a NAT, you should set # `external_listen_address` and configure port forwarding on your router') external_listen_address = listen_address # config_file = args.config_file (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 endpoint = eth_rpc_endpoint use_ssl = False 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://"):] use_ssl = True rpc_port = 443 if ':' not in endpoint: # no port was given in url rpc_host = endpoint else: rpc_host, rpc_port = split_endpoint(endpoint) jsonrpc_client = JSONRPCClient( privkey=privatekey, host=rpc_host, port=rpc_port, print_communication=False, use_ssl=use_ssl, ) blockchain_service = BlockChainService( privatekey.decode('hex'), registry_contract_address.decode('hex'), ) discovery = ContractDiscovery( jsonrpc_client, discovery_contract_address.decode('hex')) # FIXME: double encoding app = App(config, blockchain_service, discovery) discovery.register(app.raiden.address, *split_endpoint(external_listen_address)) app.raiden.register_registry(blockchain_service.default_registry) return app
def tps_run(host, port, config, privatekey, rpc_server, 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( privkey=privatekey, host=host, port=port, print_communication=False, ) blockchain_service = BlockChainService( privatekey, registry_address, 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) app = App(config, blockchain_service, 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 app(privatekey, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, external_listen_address, logging): slogging.configure(logging) if not external_listen_address: # notify('if you are behind a NAT, you should set # `external_listen_address` and configure port forwarding on your router') external_listen_address = listen_address # config_file = args.config_file rpc_connection = split_endpoint(eth_rpc_endpoint) (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 jsonrpc_client = JSONRPCClient( privkey=privatekey, host=rpc_connection[0], port=rpc_connection[1], print_communication=False, ) blockchain_service = BlockChainService( jsonrpc_client, registry_contract_address.decode('hex'), ) discovery = ContractDiscovery( jsonrpc_client, discovery_contract_address.decode('hex')) # FIXME: double encoding app = App(config, blockchain_service, discovery) discovery.register(app.raiden.address, *split_endpoint(external_listen_address)) app.raiden.register_registry(blockchain_service.default_registry) # TODO: # - Ask for confirmation to quit if there are any locked transfers that did # not timeout. console = Console(app) console.start() # 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 setup_tps(rpc_server, config_path, privatekey, registry_address, token_address, deposit, settle_timeout): """ Creates the required contract and the fully connected Raiden network prior to running the test. Args: rpc_server (str): A string in the format '{host}:{port}' used to define the JSON-RPC end-point. config_path (str): A full/relative path to the yaml configuration file. channelmanager_address (str): The address of the channel manager contract. token_address (str): The address of the token used for testing. deposit (int): The default deposit that will be made for all test nodes. """ host, port = rpc_server.split(':') rpc_client = JSONRPCClient( privkey=privatekey, host=host, port=port, print_communication=False, ) blockchain_service = BlockChainService( privatekey, registry_address, rpc_client, ) blockchain_service.default_registry.add_token(token_address) with codecs.open(config_path, encoding='utf8') as handler: config = yaml.load(handler) node_addresses = [] for node in config['nodes']: privkey = sha3('{}:{}'.format(node['host'], node['port'])) node_addresses.append(privatekey_to_address(privkey)) random_raiden_network( token_address, blockchain_service, node_addresses, deposit, settle_timeout, )
def app(privatekey, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, logging, logfile): slogging.configure(logging, log_file=logfile) # config_file = args.config_file (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 endpoint = eth_rpc_endpoint 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) blockchain_service = BlockChainService( decode_hex(privatekey), decode_hex(registry_contract_address), host=rpc_host, port=rpc_port, ) discovery = ContractDiscovery( blockchain_service, decode_hex(discovery_contract_address) # FIXME: double encoding ) return App(config, blockchain_service, discovery)
def run(privatekey, registry_contract_address, discovery_contract_address, listen_address, logging, logfile, scenario, stage_prefix, results_filename): # pylint: disable=unused-argument # TODO: only enabled logging on "initiators" slogging.configure(logging, 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 blockchain_service = BlockChainService( decode_hex(privatekey), decode_hex(registry_contract_address), host="127.0.0.1", port="8545", ) discovery = ContractDiscovery(blockchain_service, decode_hex(discovery_contract_address)) app = App(config, blockchain_service, discovery) app.discovery.register( app.raiden.address, listen_host, listen_port, ) app.raiden.register_registry(app.raiden.chain.default_registry) 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 = app.raiden.address.encode('hex') log.warning("our address is {}".format(our_node)) for token in tokens: # skip tokens that we're not part of nodes = token['channels'] if not our_node in nodes: continue # allow for prefunded tokens if 'token_address' in token: token_address = token['token_address'] else: token_address = tools.create_token() 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") while not all( tools.ping(node) for node in nodes if node != our_node): 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] channel_manager = tools.register_token(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)) app.raiden.api.open(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)) channel = app.raiden.api.deposit( 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 xrange(total_transfers): app.raiden.api.transfer( 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() results = tools.channel_stats_for(token_address, peer) if transfer_results['total_time'] != 0: results['total_time'] = transfer_results['total_time'] if len(transfer_results['timestamps']) > 0: results['timestamps'] = transfer_results['timestamps'] results['channel'] = repr(results['channel']) # FIXME log.warning("Results: {}".format(results)) with open(results_filename, 'w') as fp: json.dump(results, fp, indent=2) 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 app(address, keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, rpccorsdomain, mapped_socket, logging, logfile, log_json, max_unresponsive_time, send_ping_time, api_address, rpc, sync_check, console, password_file, web_ui, datadir, eth_client_communication, nat): # pylint: disable=too-many-locals,too-many-branches,too-many-statements,unused-argument from raiden.app import App from raiden.network.rpc.client import BlockChainService (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) config = App.DEFAULT_CONFIG.copy() 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 else: config['socket'] = None config['external_ip'] = listen_host config['external_port'] = listen_port config['protocol']['nat_keepalive_retries'] = DEFAULT_NAT_KEEPALIVE_RETRIES timeout = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config['protocol']['nat_keepalive_timeout'] = timeout 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) privatekey_hex = privatekey_bin.encode('hex') config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint # Fallback 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, ) # this assumes the eth node is already online patch_send_transaction(rpc_client) patch_send_message(rpc_client) if not check_json_rpc(rpc_client): sys.exit(1) blockchain_service = BlockChainService( privatekey_bin, rpc_client, ) if sync_check: check_synced(blockchain_service) discovery_tx_cost = GAS_PRICE * DISCOVERY_REGISTRATION_GAS while True: balance = blockchain_service.client.balance(address) if discovery_tx_cost <= balance: break print( 'Account has insufficient funds for discovery registration.\n' 'Needed: {} ETH\n' 'Available: {} ETH.\n' 'Please deposit additional funds on this account.' .format(discovery_tx_cost / float(denoms.ether), balance / float(denoms.ether)) ) if not click.confirm('Try again?'): sys.exit(1) registry = blockchain_service.registry( registry_contract_address, ) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address) ) if datadir is None: # default database directory raiden_directory = os.path.join(os.path.expanduser('~'), '.raiden') else: raiden_directory = datadir if not os.path.exists(raiden_directory): os.makedirs(raiden_directory) user_db_dir = os.path.join(raiden_directory, address_hex[:8]) if not os.path.exists(user_db_dir): os.makedirs(user_db_dir) database_path = os.path.join(user_db_dir, 'log.db') config['database_path'] = database_path return App( config, blockchain_service, registry, discovery, )
def app(address, keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, rpccorsdomain, # pylint: disable=unused-argument mapped_socket, logging, logfile, log_json, max_unresponsive_time, send_ping_time, api_address, rpc, console, password_file, web_ui, datadir): from raiden.app import App from raiden.network.rpc.client import BlockChainService # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) config = App.DEFAULT_CONFIG.copy() 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 else: config['socket'] = None config['external_ip'] = listen_host config['external_port'] = listen_port retries = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config['protocol']['nat_keepalive_retries'] = retries config['protocol']['nat_keepalive_timeout'] = send_ping_time address_hex = address_encoder(address) if address else None address_hex, privatekey_bin = prompt_account(address_hex, keystore_path, password_file) privatekey_hex = privatekey_bin.encode('hex') config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint # Fallback 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) try: blockchain_service = BlockChainService( privatekey_bin, registry_contract_address, host=rpc_host, port=rpc_port, ) except ValueError as e: # ValueError exception raised if: # - The registry contract address doesn't have code, this might happen # if the connected geth process is not synced or if the wrong address # is provided (e.g. using the address from a smart contract deployed on # ropsten with a geth node connected to morden) print(e.message) sys.exit(1) discovery_tx_cost = GAS_PRICE * DISCOVERY_REGISTRATION_GAS while True: balance = blockchain_service.client.balance(address_hex) if discovery_tx_cost <= balance: break print( 'Account has insufficient funds for discovery registration.\n' 'Needed: {} ETH\n' 'Available: {} ETH.\n' 'Please deposit additional funds on this account.' .format(discovery_tx_cost / float(denoms.ether), balance / float(denoms.ether)) ) if not click.confirm('Try again?'): sys.exit(1) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address) ) if datadir is None: # default database directory raiden_directory = os.path.join(os.path.expanduser('~'), '.raiden') else: raiden_directory = datadir if not os.path.exists(raiden_directory): os.makedirs(raiden_directory) user_db_dir = os.path.join(raiden_directory, address_hex[:8]) if not os.path.exists(user_db_dir): os.makedirs(user_db_dir) database_path = os.path.join(user_db_dir, 'log.db') config['database_path'] = database_path return App(config, blockchain_service, discovery)
def app(address, keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, logging, logfile, max_unresponsive_time, send_ping_time): slogging.configure(logging, log_file=logfile) # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) config = App.default_config.copy() config['host'] = listen_host config['port'] = listen_port config['max_unresponsive_time'] = max_unresponsive_time config['send_ping_time'] = send_ping_time accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address): addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True while should_prompt: idx = click.prompt( "The following accounts were found in your machine:\n\n{}" "\nSelect one of them by index to continue: ".format( "\n".join(formatted_addresses)), type=int) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format( idx)) address = addresses[idx] privatekey = accmgr.get_privkey(address) config['privatekey_hex'] = encode_hex(privatekey) endpoint = eth_rpc_endpoint 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) blockchain_service = BlockChainService( privatekey, decode_hex(registry_contract_address), host=rpc_host, port=rpc_port, ) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address)) return App(config, blockchain_service, discovery)
def app(address, # pylint: disable=too-many-arguments,too-many-locals keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, logging, logfile, max_unresponsive_time, send_ping_time): slogging.configure(logging, log_file=logfile) # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) config = App.default_config.copy() config['host'] = listen_host config['port'] = listen_port config['max_unresponsive_time'] = max_unresponsive_time config['send_ping_time'] = send_ping_time accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address): addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True print('The following accounts were found in your machine:') print('') print('\n'.join(formatted_addresses)) print('') while should_prompt: idx = click.prompt('Select one of them by index to continue', type=int) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format(idx)) address = addresses[idx] try: privatekey_bin = accmgr.get_privkey(address) except ValueError as e: # ValueError exception raised if the password is incorrect, print the # exception message and exit the process, the user may try again by # re-executing Raiden. print(e.message) sys.exit(1) privatekey_hex = privatekey_bin.encode('hex') config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint 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) # user may have provided registry and discovery contracts with leading 0x registry_contract_address = address_decoder(registry_contract_address) discovery_contract_address = address_decoder(discovery_contract_address) try: blockchain_service = BlockChainService( privatekey_bin, registry_contract_address, host=rpc_host, port=rpc_port, ) except ValueError as e: # ValueError exception raised if: # - The registry contract address doesn't have code, this might happen # if the connected geth process is not synced or if the wrong address # is provided (e.g. using the address from a smart contract deployed on # ropsten with a geth node connected to morden) print(e.message) sys.exit(1) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address) ) return App(config, blockchain_service, discovery)
def app( address, # pylint: disable=too-many-arguments,too-many-locals keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, socket, logging, logfile, max_unresponsive_time, send_ping_time, api_port, rpc, console): slogging.configure(logging, log_file=logfile) # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) config = App.default_config.copy() config['host'] = listen_host config['port'] = listen_port config['max_unresponsive_time'] = max_unresponsive_time config['send_ping_time'] = send_ping_time config['console'] = console config['rpc'] = rpc config['api_port'] = api_port config['socket'] = socket accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address): addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True print('The following accounts were found in your machine:') print('') print('\n'.join(formatted_addresses)) print('') while should_prompt: idx = click.prompt('Select one of them by index to continue', type=int) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format( idx)) address = addresses[idx] unlock_tries = 3 while True: try: privatekey_bin = accmgr.get_privkey(address) break except ValueError as e: # ValueError exception raised if the password is incorrect if unlock_tries == 0: print( 'Exhausted passphrase unlock attempts for {}. Aborting ...' .format(address)) sys.exit(1) print( 'Incorrect passphrase to unlock the private key. {} tries remaining. ' 'Please try again or kill the process to quit. ' 'Usually Ctrl-c.'.format(unlock_tries)) unlock_tries -= 1 privatekey_hex = privatekey_bin.encode('hex') config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint 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) # user may have provided registry and discovery contracts with leading 0x registry_contract_address = address_decoder(registry_contract_address) discovery_contract_address = address_decoder(discovery_contract_address) try: blockchain_service = BlockChainService( privatekey_bin, registry_contract_address, host=rpc_host, port=rpc_port, ) except ValueError as e: # ValueError exception raised if: # - The registry contract address doesn't have code, this might happen # if the connected geth process is not synced or if the wrong address # is provided (e.g. using the address from a smart contract deployed on # ropsten with a geth node connected to morden) print(e.message) sys.exit(1) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address)) # default database directory raiden_directory = os.path.join(os.path.expanduser('~'), '.raiden') if not os.path.exists(raiden_directory): os.makedirs(raiden_directory) database_path = os.path.join(raiden_directory, 'log.db') config['database_path'] = database_path return App(config, blockchain_service, discovery)
def app( address, keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, rpccorsdomain, # pylint: disable=unused-argument mapped_socket, logging, logfile, max_unresponsive_time, send_ping_time, api_address, rpc, console, password_file, datadir): from raiden.app import App from raiden.network.rpc.client import BlockChainService # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) config = App.DEFAULT_CONFIG.copy() config['host'] = listen_host config['port'] = listen_port config['console'] = console config['rpc'] = rpc 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 else: config['socket'] = None config['external_ip'] = listen_host config['external_port'] = listen_port retries = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config['protocol']['nat_keepalive_retries'] = retries config['protocol']['nat_keepalive_timeout'] = send_ping_time accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address): addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True print('The following accounts were found in your machine:') print('') print('\n'.join(formatted_addresses)) print('') while should_prompt: idx = click.prompt('Select one of them by index to continue', type=int) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format( idx)) address = addresses[idx] password = None if password_file: password = password_file.read().splitlines()[0] if password: try: privatekey_bin = accmgr.get_privkey(address, password) except ValueError as e: # ValueError exception raised if the password is incorrect print('Incorrect password for {} in file. Aborting ...'.format( address)) sys.exit(1) else: unlock_tries = 3 while True: try: privatekey_bin = accmgr.get_privkey(address) break except ValueError as e: # ValueError exception raised if the password is incorrect if unlock_tries == 0: print( 'Exhausted passphrase unlock attempts for {}. Aborting ...' .format(address)) sys.exit(1) print( 'Incorrect passphrase to unlock the private key. {} tries remaining. ' 'Please try again or kill the process to quit. ' 'Usually Ctrl-c.'.format(unlock_tries)) unlock_tries -= 1 privatekey_hex = privatekey_bin.encode('hex') config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint # Fallback 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) # user may have provided registry and discovery contracts with leading 0x registry_contract_address = address_decoder(registry_contract_address) discovery_contract_address = address_decoder(discovery_contract_address) try: blockchain_service = BlockChainService( privatekey_bin, registry_contract_address, host=rpc_host, port=rpc_port, ) except ValueError as e: # ValueError exception raised if: # - The registry contract address doesn't have code, this might happen # if the connected geth process is not synced or if the wrong address # is provided (e.g. using the address from a smart contract deployed on # ropsten with a geth node connected to morden) print(e.message) sys.exit(1) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address)) if datadir is None: # default database directory raiden_directory = os.path.join(os.path.expanduser('~'), '.raiden') else: raiden_directory = datadir if not os.path.exists(raiden_directory): os.makedirs(raiden_directory) user_db_dir = os.path.join(raiden_directory, address[:6]) if not os.path.exists(user_db_dir): os.makedirs(user_db_dir) database_path = os.path.join(user_db_dir, 'log.db') config['database_path'] = database_path return App(config, blockchain_service, discovery)
def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument('rpc_server', help='The host:port of the json-rpc server') parser.add_argument('registry_address', help='The asset registry contract address') parser.add_argument('config_file', help='Configuration file for the raiden note') parser.add_argument( '-h', '--host', default='0.0.0.0', help='Local address that the raiden app will bind to', ) parser.add_argument( '-p', '--port', default=INITIAL_PORT, help='Local port that the raiden app will bind to', ) args = parser.parse_args() rpc_connection = args.rpc_server.split(':') rpc_connection = (rpc_connection[0], int(rpc_connection[1])) config_file = args.config_file host = args.host port = args.port with codecs.open(config_file, encoding='utf8') as handler: config = yaml.load(handler) config['host'] = host config['port'] = port if 'privkey' not in config: print('Missing "privkey" in the configuration file, cannot proceed') sys.exit(1) blockchain_server = BlockChainService( rpc_connection, config['privkey'], privtoaddr(config['privkey']), args.registry_address, ) discovery = Discovery() for node in config['nodes']: discovery.register(node['nodeid'], node['host'], node['port']) app = App(config, blockchain_server, discovery) for asset_address in blockchain_server.asset_addresses: app.raiden.setup_asset(asset_address, app.config['reveal_timeout']) # TODO: # - Ask for confirmation to quit if there are any locked transfers that did # not timeout. # 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 app(address, keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, rpccorsdomain, mapped_socket, logging, logfile, log_json, max_unresponsive_time, send_ping_time, api_address, rpc, sync_check, console, password_file, web_ui, datadir, eth_client_communication, nat): # pylint: disable=too-many-locals,too-many-branches,too-many-statements,unused-argument from raiden.app import App from raiden.network.rpc.client import BlockChainService (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) config = App.DEFAULT_CONFIG.copy() 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 else: config['socket'] = None config['external_ip'] = listen_host config['external_port'] = listen_port config['protocol']['nat_keepalive_retries'] = DEFAULT_NAT_KEEPALIVE_RETRIES timeout = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config['protocol']['nat_keepalive_timeout'] = timeout address_hex = address_encoder(address) if address else None address_hex, privatekey_bin = prompt_account(address_hex, keystore_path, password_file) privatekey_hex = privatekey_bin.encode('hex') config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint # Fallback 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( privkey=privatekey_bin, host=rpc_host, port=rpc_port, print_communication=eth_client_communication, ) # this assumes the eth node is already online patch_send_transaction(rpc_client) patch_send_message(rpc_client) try: blockchain_service = BlockChainService( privatekey_bin, rpc_client, ) except ValueError as e: # ValueError exception raised if: # - The registry contract address doesn't have code, this might happen # if the connected geth process is not synced or if the wrong address # is provided (e.g. using the address from a smart contract deployed on # ropsten with a geth node connected to morden) print(e.message) sys.exit(1) if sync_check: try: net_id = int(blockchain_service.client.call('net_version')) network = ID_TO_NETWORKNAME[net_id] except: # pylint: disable=bare-except print( "Couldn't determine the network the ethereum node is connected to.\n" "Because of this there is not way to determine the latest\n" "block with an oracle, and the events from the ethereum\n" "node cannot be trusted. Giving up.\n") sys.exit(1) url = ETHERSCAN_API.format( network=network, action='eth_blockNumber', ) wait_for_sync( blockchain_service, url=url, tolerance=ORACLE_BLOCKNUMBER_DRIFT_TOLERANCE, sleep=3, ) discovery_tx_cost = GAS_PRICE * DISCOVERY_REGISTRATION_GAS while True: balance = blockchain_service.client.balance(address_hex) if discovery_tx_cost <= balance: break print('Account has insufficient funds for discovery registration.\n' 'Needed: {} ETH\n' 'Available: {} ETH.\n' 'Please deposit additional funds on this account.'.format( discovery_tx_cost / float(denoms.ether), balance / float(denoms.ether))) if not click.confirm('Try again?'): sys.exit(1) registry = blockchain_service.registry(registry_contract_address, ) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address)) if datadir is None: # default database directory raiden_directory = os.path.join(os.path.expanduser('~'), '.raiden') else: raiden_directory = datadir if not os.path.exists(raiden_directory): os.makedirs(raiden_directory) user_db_dir = os.path.join(raiden_directory, address_hex[:8]) if not os.path.exists(user_db_dir): os.makedirs(user_db_dir) database_path = os.path.join(user_db_dir, 'log.db') config['database_path'] = database_path return App( config, blockchain_service, registry, discovery, )