def restart_app(app): host_port = ( app.raiden.config["transport"]["udp"]["host"], app.raiden.config["transport"]["udp"]["port"], ) socket = server._udp_socket(host_port) # pylint: disable=protected-access new_transport = UDPTransport( app.raiden.address, app.discovery, socket, app.raiden.transport.throttle_policy, app.raiden.config["transport"]["udp"], ) app = App( config=app.config, chain=app.raiden.chain, query_start_block=0, default_registry=app.raiden.default_registry, default_secret_registry=app.raiden.default_secret_registry, default_service_registry=app.raiden.default_service_registry, transport=new_transport, raiden_event_handler=RaidenEventHandler(), message_handler=MessageHandler(), discovery=app.raiden.discovery, ) app.start() return app
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 test_recovery_happy_case( raiden_network, number_of_nodes, deposit, token_addresses, network_wait, skip_if_not_udp, # pylint: disable=unused-argument ): app0, app1, app2 = raiden_network token_address = token_addresses[0] chain_state = views.state_from_app(app0) payment_network_id = app0.raiden.default_registry.address token_network_identifier = views.get_token_network_identifier_by_token_address( chain_state, payment_network_id, token_address) # make a few transfers from app0 to app2 amount = 1 spent_amount = deposit - 2 identifier = 0 for identifier in range(spent_amount): transfer_and_assert_path( path=raiden_network, token_address=token_address, amount=amount, identifier=identifier, timeout=network_wait * number_of_nodes, ) app0.raiden.stop() host_port = ( app0.raiden.config["transport"]["udp"]["host"], app0.raiden.config["transport"]["udp"]["port"], ) socket = server._udp_socket(host_port) new_transport = UDPTransport( app0.raiden.address, app0.discovery, socket, app0.raiden.transport.throttle_policy, app0.raiden.config["transport"]["udp"], ) raiden_event_handler = RaidenEventHandler() message_handler = WaitForMessage() app0_restart = App( config=app0.config, chain=app0.raiden.chain, query_start_block=0, default_registry=app0.raiden.default_registry, default_secret_registry=app0.raiden.default_secret_registry, default_service_registry=app0.raiden.default_service_registry, transport=new_transport, raiden_event_handler=raiden_event_handler, message_handler=message_handler, discovery=app0.raiden.discovery, ) app0.stop() del app0 # from here on the app0_restart should be used app0_restart.start() assert_synced_channel_state( token_network_identifier, app0_restart, deposit - spent_amount, [], app1, deposit + spent_amount, [], ) assert_synced_channel_state( token_network_identifier, app1, deposit - spent_amount, [], app2, deposit + spent_amount, [], ) # wait for the nodes' healthcheck to update the network statuses waiting.wait_for_healthy(app0_restart.raiden, app1.raiden.address, network_wait) waiting.wait_for_healthy(app1.raiden, app0_restart.raiden.address, network_wait) transfer( initiator_app=app2, target_app=app0_restart, token_address=token_address, amount=amount, identifier=create_default_identifier(), timeout=network_wait * number_of_nodes * 2, ) transfer( initiator_app=app0_restart, target_app=app2, token_address=token_address, amount=amount, identifier=create_default_identifier(), timeout=network_wait * number_of_nodes * 2, ) assert_synced_channel_state( token_network_identifier, app0_restart, deposit - spent_amount, [], app1, deposit + spent_amount, [], ) assert_synced_channel_state( token_network_identifier, app1, deposit - spent_amount, [], app2, deposit + spent_amount, [], )
def test_recovery_blockchain_events( raiden_network, token_addresses, network_wait, skip_if_not_udp, # pylint: disable=unused-argument ): """ Close one of the two raiden apps that have a channel between them, have the counterparty close the channel and then make sure the restarted app sees the change """ app0, app1 = raiden_network token_address = token_addresses[0] app0.raiden.stop() host_port = ( app0.raiden.config["transport"]["udp"]["host"], app0.raiden.config["transport"]["udp"]["port"], ) socket = server._udp_socket(host_port) new_transport = UDPTransport( app0.raiden.address, app0.discovery, socket, app0.raiden.transport.throttle_policy, app0.raiden.config["transport"]["udp"], ) app1_api = RaidenAPI(app1.raiden) app1_api.channel_close( registry_address=app0.raiden.default_registry.address, token_address=token_address, partner_address=app0.raiden.address, ) app0.stop() gevent.sleep(1) raiden_event_handler = RaidenEventHandler() message_handler = MessageHandler() app0_restart = App( config=app0.config, chain=app0.raiden.chain, query_start_block=0, default_registry=app0.raiden.default_registry, default_secret_registry=app0.raiden.default_secret_registry, default_service_registry=app0.raiden.default_service_registry, transport=new_transport, raiden_event_handler=raiden_event_handler, message_handler=message_handler, discovery=app0.raiden.discovery, ) del app0 # from here on the app0_restart should be used app0_restart.raiden.start() # wait for the nodes' healthcheck to update the network statuses waiting.wait_for_healthy(app0_restart.raiden, app1.raiden.address, network_wait) waiting.wait_for_healthy(app1.raiden, app0_restart.raiden.address, network_wait) restarted_state_changes = app0_restart.raiden.wal.storage.get_statechanges_by_identifier( 0, "latest") assert search_for_item(restarted_state_changes, ContractReceiveChannelClosed, {})
def test_recovery_unhappy_case( raiden_network, number_of_nodes, deposit, token_addresses, network_wait, skip_if_not_udp, # pylint: disable=unused-argument retry_timeout, ): app0, app1, app2 = raiden_network token_address = token_addresses[0] chain_state = views.state_from_app(app0) payment_network_id = app0.raiden.default_registry.address token_network_identifier = views.get_token_network_identifier_by_token_address( chain_state, payment_network_id, token_address) # make a few transfers from app0 to app2 amount = 1 spent_amount = deposit - 2 for identifier in range(spent_amount): transfer( initiator_app=app0, target_app=app2, token_address=token_address, amount=amount, identifier=identifier, timeout=network_wait * number_of_nodes, ) app0.raiden.stop() host_port = ( app0.raiden.config["transport"]["udp"]["host"], app0.raiden.config["transport"]["udp"]["port"], ) socket = server._udp_socket(host_port) new_transport = UDPTransport( app0.raiden.address, app0.discovery, socket, app0.raiden.transport.throttle_policy, app0.raiden.config["transport"]["udp"], ) app0.stop() RaidenAPI(app1.raiden).channel_close(app1.raiden.default_registry.address, token_address, app0.raiden.address) channel01 = views.get_channelstate_for( views.state_from_app(app1), app1.raiden.default_registry.address, token_address, app0.raiden.address, ) waiting.wait_for_settle( app1.raiden, app1.raiden.default_registry.address, token_address, [channel01.identifier], retry_timeout, ) raiden_event_handler = RaidenEventHandler() message_handler = MessageHandler() app0_restart = App( config=app0.config, chain=app0.raiden.chain, query_start_block=0, default_registry=app0.raiden.default_registry, default_secret_registry=app0.raiden.default_secret_registry, default_service_registry=app0.raiden.default_service_registry, transport=new_transport, raiden_event_handler=raiden_event_handler, message_handler=message_handler, discovery=app0.raiden.discovery, ) del app0 # from here on the app0_restart should be used app0_restart.start() state_changes = app0_restart.raiden.wal.storage.get_statechanges_by_identifier( from_identifier=0, to_identifier="latest") assert search_for_item( state_changes, ContractReceiveChannelSettled, { "token_network_identifier": token_network_identifier, "channel_identifier": channel01.identifier, }, )
class DummyProtocol: def __init__(self): self.raiden = None def receive(self, data): print(data) if __name__ == "__main__": slogging.configure(':DEBUG') options = docopt(__doc__) port = int(options['--port']) target = options['<target_ip_optional_port>'] stun_host = options['--stun-host'] ip = options['--ip'] if target and ':' in target: target, target_port = target.split(':') target_port = int(target_port) else: target_port = port with socket_factory(ip, port, stun_host=stun_host) as mapped_socket: print(mapped_socket) t = UDPTransport(mapped_socket.socket, protocol=DummyProtocol()) while True: time.sleep(1) if target: t.send(None, (target, target_port), b'hello')
def test_recovery_unhappy_case( raiden_network, number_of_nodes, deposit, token_addresses, network_wait, skip_if_not_udp, retry_timeout, ): app0, app1, app2 = raiden_network token_address = token_addresses[0] chain_state = views.state_from_app(app0) payment_network_id = app0.raiden.default_registry.address token_network_identifier = views.get_token_network_identifier_by_token_address( chain_state, payment_network_id, token_address, ) # make a few transfers from app0 to app2 amount = 1 spent_amount = deposit - 2 for _ in range(spent_amount): mediated_transfer( app0, app2, token_network_identifier, amount, timeout=network_wait * number_of_nodes, ) app0.raiden.stop() host_port = ( app0.raiden.config['transport']['udp']['host'], app0.raiden.config['transport']['udp']['port'], ) socket = server._udp_socket(host_port) new_transport = UDPTransport( app0.raiden.address, app0.discovery, socket, app0.raiden.transport.throttle_policy, app0.raiden.config['transport']['udp'], ) app0.stop() RaidenAPI(app1.raiden).channel_close( app1.raiden.default_registry.address, token_address, app0.raiden.address, ) channel01 = views.get_channelstate_for( views.state_from_app(app1), app1.raiden.default_registry.address, token_address, app0.raiden.address, ) waiting.wait_for_settle( app1.raiden, app1.raiden.default_registry.address, token_address, [channel01.identifier], retry_timeout, ) raiden_event_handler = RaidenEventHandler() message_handler = MessageHandler() app0_restart = App( config=app0.config, chain=app0.raiden.chain, query_start_block=0, default_registry=app0.raiden.default_registry, default_secret_registry=app0.raiden.default_secret_registry, transport=new_transport, raiden_event_handler=raiden_event_handler, message_handler=message_handler, discovery=app0.raiden.discovery, ) del app0 # from here on the app0_restart should be used app0_restart.start() state_changes = app0_restart.raiden.wal.storage.get_statechanges_by_identifier( from_identifier=0, to_identifier='latest', ) assert must_contain_entry(state_changes, ContractReceiveChannelSettled, { 'token_network_identifier': token_network_identifier, 'channel_identifier': channel01.identifier, })
def test_recovery_unhappy_case( raiden_network, number_of_nodes, deposit, token_addresses, network_wait, skip_if_not_udp, retry_timeout, ): app0, app1, app2 = raiden_network token_address = token_addresses[0] node_state = views.state_from_app(app0) payment_network_id = app0.raiden.default_registry.address token_network_identifier = views.get_token_network_identifier_by_token_address( node_state, payment_network_id, token_address, ) # make a few transfers from app0 to app2 amount = 1 spent_amount = deposit - 2 for _ in range(spent_amount): mediated_transfer( app0, app2, token_network_identifier, amount, timeout=network_wait * number_of_nodes, ) app0.raiden.stop() host_port = (app0.raiden.config['host'], app0.raiden.config['port']) socket = server._udp_socket(host_port) new_transport = UDPTransport( app0.discovery, socket, app0.raiden.transport.throttle_policy, app0.raiden.config['transport'], ) app0.stop() RaidenAPI(app1.raiden).channel_close( app1.raiden.default_registry.address, token_address, app0.raiden.address, ) channel01 = views.get_channelstate_for( views.state_from_app(app1), app1.raiden.default_registry.address, token_address, app0.raiden.address, ) waiting.wait_for_settle( app1.raiden, app1.raiden.default_registry.address, token_address, [channel01.identifier], retry_timeout, ) app0_restart = App( app0.config, app0.raiden.chain, app0.raiden.default_registry, app0.raiden.default_secret_registry, new_transport, app0.raiden.discovery, ) del app0 # from here on the app0_restart should be used assert_synched_channel_state( token_network_identifier, app0_restart, deposit - spent_amount, [], app1, deposit + spent_amount, [], ) assert_synched_channel_state( token_network_identifier, app1, deposit - spent_amount, [], app2, deposit + spent_amount, [], )
from raiden.network.sockfactory import socket_factory class DummyProtocol(object): def __init__(self): self.raiden = None def receive(self, data): print data if __name__ == "__main__": slogging.configure(':DEBUG') options = docopt(__doc__) port = int(options['--port']) target = options['<target_ip_optional_port>'] stun_host = options['--stun-host'] ip = options['--ip'] if target and ':' in target: target, target_port = target.split(':') target_port = int(target_port) else: target_port = port with socket_factory(ip, port, stun_host=stun_host) as mapped_socket: print mapped_socket t = UDPTransport(mapped_socket.socket, protocol=DummyProtocol()) while True: time.sleep(1) if target: t.send(None, (target, target_port), b'hello')
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 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) 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: registry = blockchain_service.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': transport = MatrixTransport(config['matrix']) else: raise RuntimeError(f'Unknown transport type "{transport}" given') raiden_app = App( config, blockchain_service, registry, secret_registry, transport, discovery, ) return raiden_app
def test_recovery_happy_case( raiden_network, number_of_nodes, deposit, token_addresses, network_wait, skip_if_not_udp, ): app0, app1, app2 = raiden_network token_address = token_addresses[0] chain_state = views.state_from_app(app0) payment_network_id = app0.raiden.default_registry.address token_network_identifier = views.get_token_network_identifier_by_token_address( chain_state, payment_network_id, token_address, ) # make a few transfers from app0 to app2 amount = 1 spent_amount = deposit - 2 for _ in range(spent_amount): mediated_transfer( app0, app2, token_network_identifier, amount, timeout=network_wait * number_of_nodes, ) app0.raiden.stop() host_port = (app0.raiden.config['host'], app0.raiden.config['port']) socket = server._udp_socket(host_port) new_transport = UDPTransport( app0.discovery, socket, app0.raiden.transport.throttle_policy, app0.raiden.config['transport'], ) app0_restart = App( config=app0.config, chain=app0.raiden.chain, query_start_block=0, default_registry=app0.raiden.default_registry, default_secret_registry=app0.raiden.default_secret_registry, transport=new_transport, discovery=app0.raiden.discovery, ) app0.stop() del app0 # from here on the app0_restart should be used assert_synched_channel_state( token_network_identifier, app0_restart, deposit - spent_amount, [], app1, deposit + spent_amount, [], ) assert_synched_channel_state( token_network_identifier, app1, deposit - spent_amount, [], app2, deposit + spent_amount, [], ) # wait for the nodes' healthcheck to update the network statuses waiting.wait_for_healthy( app0_restart.raiden, app1.raiden.address, network_wait, ) waiting.wait_for_healthy( app1.raiden, app0_restart.raiden.address, network_wait, ) mediated_transfer( app2, app0_restart, token_network_identifier, amount, timeout=network_wait * number_of_nodes * 2, ) mediated_transfer( app0_restart, app2, token_network_identifier, amount, timeout=network_wait * number_of_nodes * 2, ) assert_synched_channel_state( token_network_identifier, app0_restart, deposit - spent_amount, [], app1, deposit + spent_amount, [], ) assert_synched_channel_state( token_network_identifier, app1, deposit - spent_amount, [], app2, deposit + spent_amount, [], )
def test_recovery_blockchain_events( raiden_network, number_of_nodes, deposit, token_addresses, network_wait, skip_if_not_udp, ): """ Close one of the two raiden apps that have a channel between them, have the counterparty close the channel and then make sure the restarted app sees the change """ app0, app1 = raiden_network token_address = token_addresses[0] app0.raiden.stop() host_port = (app0.raiden.config['host'], app0.raiden.config['port']) socket = server._udp_socket(host_port) new_transport = UDPTransport( app0.discovery, socket, app0.raiden.transport.throttle_policy, app0.raiden.config['transport'], ) app1_api = RaidenAPI(app1.raiden) app1_api.channel_close( registry_address=app0.raiden.default_registry.address, token_address=token_address, partner_address=app0.raiden.address, ) app0.stop() import gevent gevent.sleep(1) app0_restart = App( config=app0.config, chain=app0.raiden.chain, query_start_block=0, default_registry=app0.raiden.default_registry, default_secret_registry=app0.raiden.default_secret_registry, transport=new_transport, discovery=app0.raiden.discovery, ) del app0 # from here on the app0_restart should be used # wait for the nodes' healthcheck to update the network statuses waiting.wait_for_healthy( app0_restart.raiden, app1.raiden.address, network_wait, ) waiting.wait_for_healthy( app1.raiden, app0_restart.raiden.address, network_wait, ) restarted_state_changes = app0_restart.raiden.wal.storage.get_statechanges_by_identifier( 0, 'latest', ) assert must_contain_entry(restarted_state_changes, ContractReceiveChannelClosed, {})
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