def test_get_target_node_id(): from node_tools import ctlr_data as ct NODE_SETTINGS['use_exitnode'].append('beefea68e6') node_list = get_active_nodes(ct.id_trie) assert 'beefea68e6' not in node_list boot_list = ['deadbeef01', 'deadbeef02', 'deadbeef03'] res = get_target_node_id(node_list, boot_list) assert res in ['ee2eedb2e1', 'ff2ffdb2e1'] NODE_SETTINGS['use_exitnode'].clear()
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))
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')