def crawl_for_peers(v1, namespace, seed, seed_daemon_port, max_crawl_requests=10): peer_table = {} queried_peers = set() unqueried_peers = {} def contains_error(resp): try: resp['error'] return True except KeyError : return False def no_error(resp): return (not (contains_error(resp))) def add_resp(resp, direct_queried_peers): # we use ast instead of json to handle properties with single quotes instead of double quotes (which the response seems to often contain) resps = [ ast.literal_eval(s) for s in resp.split('\n') if s != '' ] peers = list(filter(no_error,resps)) error_resps = list(filter(contains_error,resps)) key_value_peers = [ ((p['node_ip_addr'], p['node_peer_id']), p) for p in peers ] for (k,v) in key_value_peers: if k not in peer_table: peer_table[k] = v queried_peers.update([ p['node_peer_id'] for p in peers ]) queried_peers.update([ p['peer_id'] for p in direct_queried_peers ]) for p in itertools.chain(*[ p['peers'] for p in peers ]): unqueried_peers[p['peer_id']] = p for p in queried_peers: if p in unqueried_peers: del unqueried_peers[p] cmd = "mina advanced node-status -daemon-port " + seed_daemon_port + " -daemon-peers" + " -show-errors" resp = util.exec_on_pod(v1, namespace, seed, 'coda', cmd) add_resp(resp, []) requests = 0 while len(unqueried_peers) > 0 and requests < max_crawl_requests: peers_to_query = list(unqueried_peers.values()) peers = ','.join(peer_to_multiaddr(p) for p in peers_to_query) print ('Queried ' + str(len(queried_peers)) + ' peers. Gathering node status on %s unqueried peers'%(str(len(unqueried_peers)))) resp = util.exec_on_pod(v1, namespace, seed, 'coda', "mina advanced node-status -daemon-port " + seed_daemon_port + " -peers " + peers + " -show-errors") add_resp(resp, peers_to_query) requests += 1 return peer_table
def get_chain_id(v1, namespace): for (pod_name, container_name) in daemon_containers(v1, namespace): resp = util.exec_on_pod(v1, namespace, pod_name, container_name, 'mina client status --json') try: resp = resp.strip() if resp[0] != '{': #first line could be 'Using password from environment variable CODA_PRIVKEY_PASS' resp = resp.split("\n", 1)[1] resp = json.loads(resp.strip()) print("Chain ID: {}".format(resp['chain_id'])) return resp['chain_id'] except Exception as e: print("Exception when extracting chain id: {}\n mina client status response: {}".format(e, resp)) continue