Beispiel #1
0
def test_get_neighbor_ids():
    from node_tools import ctlr_data as ct

    trie = ct.net_trie
    node_id = 'ee2eedb2e1'
    exit_id = 'beefea68e6'
    tail_id = 'ff2ffdb2e1'

    res = get_neighbor_ids(trie, node_id)
    assert res == ('beafde52b4a5f7ba', 'beafde52b4296ea5', 'ff2ffdb2e1',
                   'beefea68e6')

    res = get_neighbor_ids(trie, tail_id)
    assert res == ('beafde52b4a5e8ab', 'beafde52b4a5f7ba', None, 'ee2eedb2e1')

    with pytest.raises(AssertionError):
        res = get_neighbor_ids(trie, exit_id)

    NODE_SETTINGS['use_exitnode'].append(exit_id)
    res = get_neighbor_ids(trie, exit_id)
    assert res == ('beafde52b4296ea5', None, 'ee2eedb2e1', None)

    NODE_SETTINGS['use_exitnode'].clear()
Beispiel #2
0
async def unwrap_mbr_net(client, node_lst, boot_lst, min_nodes=5):
    """
    Wrapper for unwrapping the (closed) network when it gets too small.
    This should run in the ctlr state runner *after* the other handlers
    (and when there are not enough nodes to keep a closed network).
    :param client: ztcli_api client object
    :param node_lst: list of all active nodes
    :param boot_lst: list of bootstrap nodes
    :param min_nodes: minimum number of nodes for a closed network
    """
    from node_tools import ctlr_data as ct

    from node_tools.ctlr_funcs import unset_network_cfg
    from node_tools.network_funcs import publish_cfg_msg
    from node_tools.trie_funcs import cleanup_state_tries
    from node_tools.trie_funcs import find_dangling_nets
    from node_tools.trie_funcs import get_neighbor_ids
    from node_tools.trie_funcs import get_target_node_id

    if len(node_lst) < min_nodes and len(boot_lst) == 0:
        logger.debug(
            'UNWRAP: creating bootstrap list from network {}'.format(node_lst))
        tgt_id = get_target_node_id(node_lst, boot_lst)
        tgt_net, tgt_exit_net, _, _ = get_neighbor_ids(ct.net_trie, tgt_id)
        # tgt_src_net, _, _, _ = get_neighbor_ids(ct.net_trie, tgt_src_node)
        data_list = find_dangling_nets(ct.id_trie)
        exit_net = data_list[0]
        exit_node = data_list[1]
        deauth = unset_network_cfg()

        # detach and connect tgt node back to exit node
        await config_network_object(client, deauth, tgt_exit_net, tgt_id)
        cleanup_state_tries(ct.net_trie,
                            ct.id_trie,
                            tgt_exit_net,
                            tgt_id,
                            mbr_only=True)
        logger.debug('UNWRAP: deauthed node id {} from tgt exit net {}'.format(
            tgt_id, tgt_exit_net))

        await connect_mbr_node(client, tgt_id, tgt_net, exit_net, exit_node)
        publish_cfg_msg(ct.id_trie, tgt_id, addr='127.0.0.1')
    else:
        logger.debug('UNWRAP: num nodes at least {} so not unwrapping'.format(
            min_nodes))
Beispiel #3
0
async def offline_mbr_node(client, node_id):
    """
    Wrapper for handling an offline member node; removes the mbr node
    and its left-hand (src) net, and relinks both nieghbor nodes.
    This should run in the ctlr state runner *before* the state trie
    updates happen.
    :param client: ztcli_api client object
    :param node_id: node ID
    """
    from node_tools import ctlr_data as ct
    from node_tools import state_data as st

    from node_tools.ctlr_funcs import unset_network_cfg
    from node_tools.network_funcs import publish_cfg_msg
    from node_tools.trie_funcs import cleanup_state_tries
    from node_tools.trie_funcs import get_neighbor_ids

    try:
        node_net, exit_net, src_node, exit_node = get_neighbor_ids(
            ct.net_trie, node_id)
        node_nets = [node_net, exit_net]
        logger.debug('OFFLINE: got node_nets {} and nodes {} {}'.format(
            node_nets, src_node, exit_node))
        if exit_node is not None:
            st.wait_cache.set(exit_node, True, 65)
            logger.debug(
                'OFFLINE: added exit_node {} to wait cache'.format(exit_node))
    except Exception as exc:
        logger.error('OFFLINE: {}'.format(exc))
        node_nets = [None, None]

    deauth = unset_network_cfg()
    if node_nets != [None, None]:
        if src_node is not None:
            src_net, _, _, _ = get_neighbor_ids(ct.net_trie, src_node)

        if src_node is None:
            if exit_net is not None:
                await config_network_object(client, deauth, exit_net, node_id)
                cleanup_state_tries(ct.net_trie,
                                    ct.id_trie,
                                    exit_net,
                                    node_id,
                                    mbr_only=True)
                logger.debug(
                    'OFFLINE: deauthed node id {} from exit net {}'.format(
                        node_id, exit_net))
            await delete_network_object(client, node_net)
            cleanup_state_tries(ct.net_trie, ct.id_trie, node_net, node_id)
            logger.debug('OFFLINE: removed dangling net {}'.format(node_net))
        else:
            await config_network_object(client, deauth, exit_net, node_id)
            cleanup_state_tries(ct.net_trie,
                                ct.id_trie,
                                exit_net,
                                node_id,
                                mbr_only=True)
            logger.debug(
                'OFFLINE: deauthed node id {} from exit net {}'.format(
                    node_id, exit_net))
            await delete_network_object(client, node_net)
            cleanup_state_tries(ct.net_trie, ct.id_trie, node_net, node_id)
            logger.debug('OFFLINE: removed network id {} and node {}'.format(
                node_net, node_id))

            await connect_mbr_node(client, src_node, src_net, exit_net,
                                   exit_node)
            publish_cfg_msg(ct.id_trie, src_node, addr='127.0.0.1')
    else:
        logger.warning('OFFLINE: node {} has missing net list {}'.format(
            node_id, node_nets))
Beispiel #4
0
async def close_mbr_net(client, node_lst, boot_lst, min_nodes=5):
    """
    Wrapper for closing the bootstrap chain or adding it to an existing
    (closed) network. This should run in the ctlr state runner *after*
    the other handlers.
    :param client: ztcli_api client object
    :param node_lst: list of all active nodes
    :param boot_lst: list of bootstrap nodes
    :param min_nodes: minimum number of nodes for a closed network
    """
    from node_tools import ctlr_data as ct
    from node_tools import state_data as st

    from node_tools.ctlr_funcs import unset_network_cfg
    from node_tools.network_funcs import publish_cfg_msg
    from node_tools.trie_funcs import cleanup_state_tries
    from node_tools.trie_funcs import find_dangling_nets
    from node_tools.trie_funcs import find_exit_net
    from node_tools.trie_funcs import get_neighbor_ids
    from node_tools.trie_funcs import get_target_node_id

    head_id = boot_lst[-1]
    tail_id = boot_lst[0]
    head_exit_net = find_exit_net(ct.id_trie)[0]
    head_src_net, _, _, _ = get_neighbor_ids(ct.net_trie, head_id)
    tail_exit_net = find_dangling_nets(ct.id_trie)[0]
    deauth = unset_network_cfg()

    # if true, we only have a boot list
    if len(node_lst) == len(boot_lst):
        # check if we have enough nodes for a network
        if len(boot_lst) >= min_nodes:
            logger.debug(
                'CLOSURE: creating network from boot_list {}'.format(boot_lst))
            for mbr_id in [head_id, tail_id]:
                st.wait_cache.set(mbr_id, True, 90)
            # detach and connect head to tail
            await config_network_object(client, deauth, head_exit_net, head_id)
            cleanup_state_tries(ct.net_trie,
                                ct.id_trie,
                                head_exit_net,
                                head_id,
                                mbr_only=True)
            logger.debug(
                'CLOSURE: deauthed head id {} from exit net {}'.format(
                    head_id, head_exit_net))

            await connect_mbr_node(client, head_id, head_src_net,
                                   tail_exit_net, tail_id)
            publish_cfg_msg(ct.id_trie, head_id, addr='127.0.0.1')
        else:
            logger.debug('CLOSURE: not enough bootstrap nodes to wrap')
    else:
        logger.debug(
            'CLOSURE: adding bootstrap list {} to network'.format(boot_lst))
        tgt_id = get_target_node_id(node_lst, boot_lst)
        tgt_net, tgt_exit_net, tgt_src_node, tgt_exit_node = get_neighbor_ids(
            ct.net_trie, tgt_id)
        tgt_src_net, _, _, _ = get_neighbor_ids(ct.net_trie, tgt_src_node)

        for mbr_id in [tgt_id, tail_id, head_id, tgt_exit_node]:
            st.wait_cache.set(mbr_id, True, 120)
        # detach and connect tgt to tail
        await config_network_object(client, deauth, tgt_exit_net, tgt_id)
        cleanup_state_tries(ct.net_trie,
                            ct.id_trie,
                            tgt_exit_net,
                            tgt_id,
                            mbr_only=True)
        logger.debug('CLOSURE: deauthed tgt id {} from tgt exit net {}'.format(
            tgt_id, tgt_exit_net))

        await connect_mbr_node(client, tgt_id, tgt_src_net, tail_exit_net,
                               tail_id)
        publish_cfg_msg(ct.id_trie, tgt_id, addr='127.0.0.1')

        # detach and connect head to tgt exit net
        await config_network_object(client, deauth, head_exit_net, head_id)
        cleanup_state_tries(ct.net_trie,
                            ct.id_trie,
                            head_exit_net,
                            head_id,
                            mbr_only=True)
        logger.debug(
            'CLOSURE: deauthed node id {} from head exit net {}'.format(
                head_id, head_exit_net))

        await connect_mbr_node(client, head_id, head_src_net, tgt_exit_net,
                               tgt_exit_node)
        time.sleep(0.02)
        publish_cfg_msg(ct.id_trie, head_id, addr='127.0.0.1')