Exemplo n.º 1
0
async def update_state_tries(client, net_trie, id_trie):
    """
    Wrapper to update ctlr state tries from ZT client API.  Loads net/id
    tries with new data (does not remove any stale trie data).
    :param client: ztcli_api client object
    :param net_trie: zt network/member data
    :param id_trie: network/node state
    """
    from node_tools.trie_funcs import load_id_trie

    await get_network_object_ids(client)
    logger.debug('{} networks found'.format(len(client.data)))
    net_list = client.data
    for net_id in net_list:
        mbr_list = []
        # get details about each network and update trie data
        await get_network_object_data(client, net_id)
        net_trie[net_id] = client.data
        await get_network_object_ids(client, net_id)
        logger.debug('network {} has {} possible member(s)'.format(
            net_id, len(client.data)))
        member_dict = client.data
        for mbr_id in member_dict.keys():
            # get details about each network member and update trie data
            await get_network_object_data(client, net_id, mbr_id)
            if client.data['authorized']:
                logger.debug('adding member: {}'.format(mbr_id))
                net_trie[net_id + mbr_id] = client.data
                load_id_trie(net_trie, id_trie, [], [mbr_id])
                mbr_list.append(mbr_id)
        load_id_trie(net_trie, id_trie, [net_id], mbr_list, nw=True)
        logger.debug('member key suffixes: {}'.format(
            net_trie.suffixes(net_id)))
Exemplo n.º 2
0
def test_cleanup_state_tries():
    from node_tools import ctlr_data as ct

    ct.id_trie.clear()
    ct.net_trie.clear()
    load_net_trie_data(ct.net_trie)

    for net_id in ['beafde52b4296ea5', 'beafde52b4a5f7ba', 'beafde52b4a5e8ab']:
        load_id_trie(ct.net_trie, ct.id_trie, [net_id], [], nw=True)
    for node_id in ['beefea68e6', 'ee2eedb2e1', 'ff2ffdb2e1']:
        load_id_trie(ct.net_trie, ct.id_trie, [], [node_id])

    net1 = 'beafde52b4296ea5'
    node1 = 'beefea68e6'
    net2 = 'beafde52b4a5f7ba'
    node2 = 'ee2eedb2e1'
    net3 = 'beafde52b4a5e8ab'
    node3 = 'ff2ffdb2e1'

    assert len(list(ct.net_trie)) == 8
    res = get_active_nodes(ct.id_trie)
    assert len(res) == 3
    assert res == ['beefea68e6', 'ee2eedb2e1', 'ff2ffdb2e1']

    cleanup_state_tries(ct.net_trie, ct.id_trie, net2, node3, mbr_only=True)

    assert len(list(ct.net_trie)) == 7
    assert node3 not in ct.id_trie
    res = get_active_nodes(ct.id_trie)
    assert len(res) == 2
    assert res == ['beefea68e6', 'ee2eedb2e1']

    cleanup_state_tries(ct.net_trie, ct.id_trie, net3, node3)

    assert len(list(ct.net_trie)) == 5
    assert len(list(ct.id_trie)) == 4
    for key in ct.net_trie.keys():
        assert net3 not in key
        assert node3 not in key
    for key in ct.id_trie.keys():
        assert net3 not in key
        assert node3 not in key

    cleanup_state_tries(ct.net_trie, ct.id_trie, net1, node2, mbr_only=True)
    cleanup_state_tries(ct.net_trie, ct.id_trie, net2, node2)
    for key in ct.net_trie.keys():
        assert net2 not in key
        assert node2 not in key
        assert net1 in key
    for key in ct.id_trie.keys():
        assert net2 not in key
        assert node2 not in key
    cleanup_state_tries(ct.net_trie, ct.id_trie, net1, node1)
    assert node1 not in ct.id_trie
Exemplo n.º 3
0
def test_find_orphans():
    from node_tools import ctlr_data as ct

    exit_id = 'beefea68e6'
    dead_key = 'dead99dead'
    empty_nets = ['beafde52b4296eee']

    res = find_orphans(ct.net_trie, ct.id_trie)
    assert res == ([('beafde52b4296ea5', exit_id)], [dead_key])

    NODE_SETTINGS['use_exitnode'].append(exit_id)
    res = find_orphans(ct.net_trie, ct.id_trie)
    assert res == ([], [dead_key])

    del ct.id_trie[dead_key]
    load_id_trie(ct.net_trie, ct.id_trie, empty_nets, [], nw=True)
    res = find_orphans(ct.net_trie, ct.id_trie)
    assert res == (empty_nets, [])

    NODE_SETTINGS['use_exitnode'].clear()
Exemplo n.º 4
0
def test_load_id_from_net_trie():
    from node_tools import ctlr_data as ct

    NODE_SETTINGS['use_exitnode'].append('beefea68e6')
    dead_key = 'dead99dead'

    res = trie_is_empty(ct.net_trie)
    assert res is True

    load_net_trie_data(ct.net_trie)
    assert len(list(ct.net_trie)) == 8

    for net_id in ['beafde52b4296ea5', 'beafde52b4a5f7ba', 'beafde52b4a5e8ab']:
        load_id_trie(ct.net_trie, ct.id_trie, [net_id], [], nw=True)
    for node_id in ['beefea68e6', 'ee2eedb2e1', 'ff2ffdb2e1', dead_key]:
        load_id_trie(ct.net_trie, ct.id_trie, [], [node_id])

    for key in [
            'beafde52b4296ea5', 'beafde52b4a5f7ba', 'ee2eedb2e1', 'ff2ffdb2e1'
    ]:
        links, needs = ct.id_trie[key]
        for item in [links, needs]:
            assert isinstance(item, list)
        assert len(links) == 2
        assert len(needs) == 2

    assert ct.id_trie['beefea68e6'] == (['beafde52b4296ea5'], [False, False])
    assert ct.id_trie['beafde52b4a5f7ba'] == (['ee2eedb2e1',
                                               'ff2ffdb2e1'], [False, False])
    assert ct.id_trie['beafde52b4a5e8ab'] == (['ff2ffdb2e1'], [False, True])

    res = find_exit_net(ct.id_trie)
    assert isinstance(res, list)
    assert res == ['beafde52b4296ea5']
    exit_net = find_exit_net(ct.id_trie)[0]
    assert exit_net == res[0]
    # print(ct.id_trie.items())

    node_id = 'beefea68e6'
    with pytest.raises(AssertionError):
        load_id_trie(ct.net_trie, ct.id_trie, [], [node_id], needs=[True])
    with pytest.raises(AssertionError):
        load_id_trie(ct.net_trie, ct.id_trie, [], [node_id, node_id, node_id])
    with pytest.raises(AssertionError):
        load_id_trie(ct.net_trie, ct.id_trie, [], node_id)
    with pytest.raises(AssertionError):
        load_id_trie(ct.net_trie, ct.id_trie, [], [])

    NODE_SETTINGS['use_exitnode'].clear()