Beispiel #1
0
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.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,
        transport=new_transport,
        raiden_event_handler=RaidenEventHandler(),
        discovery=app.raiden.discovery,
    )

    app.start()

    return app
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]
    address = make_address()

    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(
        address,
        MockDiscovery,
        server._udp_socket((host, port)),  # pylint: disable=protected-access
        throttle_policy,
        config,
    )

    transport.raiden = MockRaidenService(ADDR)

    return transport
Beispiel #3
0
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_one_to_n_address=app.raiden.default_one_to_n_address,
        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
Beispiel #4
0
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
Beispiel #5
0
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,
        transport=new_transport,
        raiden_event_handler=RaidenEventHandler(),
        message_handler=MessageHandler(),
        discovery=app.raiden.discovery,
    )

    app.start()

    return app
Beispiel #6
0
def test_setup_udp_or_exit(raiden_udp_ports):
    network_id = 42
    config = deepcopy(App.DEFAULT_CONFIG)
    config["network_id"] = network_id
    config["environment_type"] = Environment.DEVELOPMENT
    host = "127.0.0.1"
    port = raiden_udp_ports[0]
    config["socket"] = server._udp_socket((host, port))  # pylint: disable=protected-access
    contracts = {}
    our_address = make_address()
    blockchain_service = MockChain(network_id=network_id,
                                   node_address=make_address())
    # set a big fake balance for us, to pass the test of sufficient gas for discovery transaction
    blockchain_service.client.balances_mapping[our_address] = 99999999999999999
    transport, discovery = setup_udp_or_exit(
        config=config,
        blockchain_service=blockchain_service,
        address=our_address,
        contracts=contracts,
        endpoint_registry_contract_address=make_address(),
    )
    assert isinstance(transport, UDPTransport)
    assert discovery
Beispiel #7
0
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()
Beispiel #8
0
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,
        [],
    )
Beispiel #9
0
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,
        [],
    )
Beispiel #10
0
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]

    app2_wait_for = WaitForMessage()
    app2.raiden.message_handler = app2_wait_for

    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'],
    )

    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,
    )

    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,
    )

    identifier = create_default_identifier()
    wait_for_payment = app2_wait_for.wait_for_message(Secret, {'payment_identifier': identifier})

    mediated_transfer(
        app2,
        app0_restart,
        token_network_identifier,
        amount,
        timeout=network_wait * number_of_nodes * 2,
    )
    mediated_transfer(
        initiator_app=app0_restart,
        target_app=app2,
        token_network_identifier=token_network_identifier,
        amount=amount,
        identifier=identifier,
        timeout=network_wait * number_of_nodes * 2,
    )

    assert_synced_channel_state(
        token_network_identifier,
        app0_restart, deposit - spent_amount, [],
        app1, deposit + spent_amount, [],
    )

    wait_for_payment.wait()
    assert_synced_channel_state(
        token_network_identifier,
        app1, deposit - spent_amount, [],
        app2, deposit + spent_amount, [],
    )
Beispiel #11
0
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,
    })
Beispiel #12
0
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()
Beispiel #13
0
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
Beispiel #14
0
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
Beispiel #15
0
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
Beispiel #16
0
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=config_copy,
            chain=blockchain,
            query_start_block=0,
            default_registry=registry,
            default_secret_registry=secret_registry,
            transport=transport,
            discovery=discovery,
        )
        apps.append(app)

    return apps
Beispiel #17
0
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
Beispiel #18
0
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
Beispiel #19
0
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,
    })
Beispiel #20
0
def test_recovery_unhappy_case(
        raiden_network,
        number_of_nodes,
        deposit,
        token_addresses,
        network_wait,
        skip_if_not_udp,
        events_poll_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],
        events_poll_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, [],
    )
Beispiel #21
0
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['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()

    import gevent
    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,
        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 must_contain_entry(restarted_state_changes, ContractReceiveChannelClosed, {})
 def get_listener(cls, address, backlog=None, family=None):
     """Creates a datagram socket used to listen to incoming requests"""
     return _udp_socket(address, reuse_addr=cls.reuse_addr, family=family)
Beispiel #23
0
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, {})
Beispiel #24
0
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, {})
Beispiel #25
0
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,
        },
    )
Beispiel #26
0
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['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(
        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

    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, [],
    )