diff --git a/silkaj/cli.py b/silkaj/cli.py index 4db519ee16555c25465392de4c96145d1dc6d66e..f7a3818530e8d5ee4bff28295d02d2821187bd62 100644 --- a/silkaj/cli.py +++ b/silkaj/cli.py @@ -32,7 +32,6 @@ from silkaj.commands import ( list_blocks, ) -# from silkaj.net import network_info from silkaj.wot import received_sent_certifications, id_pubkey_correspondence from silkaj.auth import generate_auth_file from silkaj.license import license_command @@ -143,7 +142,6 @@ cli.add_command(id_pubkey_correspondence) cli.add_command(currency_info) cli.add_command(license_command) cli.add_command(send_membership) -# cli.add_command(network_info) cli.add_command(send_transaction) cli.add_command(verify_blocks_signatures) cli.add_command(received_sent_certifications) diff --git a/silkaj/net.py b/silkaj/net.py deleted file mode 100644 index 94c7493b34d5c7647e4d4f11efc5f971eca9a803..0000000000000000000000000000000000000000 --- a/silkaj/net.py +++ /dev/null @@ -1,145 +0,0 @@ -""" -Copyright 2016-2021 Maël Azimi <m.a@moul.re> - -Silkaj is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -Silkaj is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with Silkaj. If not, see <https://www.gnu.org/licenses/>. -""" - -""" -from click import command, option, get_terminal_size -from datetime import datetime -from os import system -from collections import OrderedDict -from tabulate import tabulate -from asyncio import sleep - -from duniterpy.api.client import Client -from duniterpy.api import bma - -from silkaj.tools import coroutine -from silkaj.wot import identity_of -from silkaj.network_tools import ( - discover_peers, - best_endpoint_address, - ClientInstance, -) -from silkaj.tools import message_exit -from silkaj.tui import convert_time -from silkaj.constants import ASYNC_SLEEP - - -def get_network_sort_key(endpoint): - t = list() - for akey in network_sort_keys: - if akey == "diffi" or akey == "block" or akey == "port": - t.append(int(endpoint[akey]) if akey in endpoint else 0) - else: - t.append(str(endpoint[akey]) if akey in endpoint else "") - return tuple(t) - - -@command("net", help="Display network view") -@option( - "--discover", "-d", is_flag=True, help="Discover the network (could take a while)" -) -@option( - "--sort", - "-s", - default="block,member,diffi,uid", - show_default=True, - help="Sort column names comma-separated", -) -@coroutine -async def network_info(discover, sort): - global network_sort_keys - network_sort_keys = sort.split(",") - width = get_terminal_size()[0] - if width < 146: - message_exit( - "Wide screen need to be larger than 146. Current width: " + str(width) - ) - # discover peers - # and make sure fields are always ordered the same - infos = [ - OrderedDict( - (i, p.get(i, None)) for i in ("domain", "port", "ip4", "ip6", "pubkey") - ) - for p in await discover_peers(discover) - ] - client = ClientInstance().client - diffi = await client(bma.blockchain.difficulties) - members = 0 - print("Getting informations about nodes:") - for i, info in enumerate(infos): - ep = info - api = "BASIC_MERKLED_API " if ep["port"] != "443" else "BMAS " - api += ep.get("domain") + " " if ep["domain"] else "" - api += ep.get("ip4") + " " if ep["ip4"] else "" - api += ep.get("ip6") + " " if ep["ip6"] else "" - api += ep.get("port") - print("{0:.0f}%".format(i / len(infos) * 100, 1), end=" ") - best_ep = best_endpoint_address(info, False) - print(best_ep if best_ep is None else info[best_ep], end=" ") - print(info["port"]) - await sleep(ASYNC_SLEEP) - try: - info["uid"] = await identity_of(info["pubkey"]) - info["uid"] = info["uid"]["uid"] - info["member"] = "yes" - members += 1 - except: - info["uid"] = None - info["member"] = "no" - info["pubkey"] = info["pubkey"][:5] + "…" - for d in diffi["levels"]: - if info.get("uid") is not None: - if info["uid"] == d["uid"]: - info["diffi"] = d["level"] - if len(info["uid"]) > 10: - info["uid"] = info["uid"][:9] + "…" - sub_client = Client(api) - current_blk = await sub_client(bma.blockchain.current) - if current_blk is not None: - info["gen_time"] = convert_time(current_blk["time"], "hour") - if width > 171: - info["mediantime"] = convert_time(current_blk["medianTime"], "hour") - if width > 185: - info["difftime"] = convert_time( - current_blk["time"] - current_blk["medianTime"], "hour" - ) - info["block"] = current_blk["number"] - info["hash"] = current_blk["hash"][:10] + "…" - summary = await sub_client(bma.node.summary) - info["version"] = summary["duniter"]["version"] - await sub_client.close() - if info.get("domain") is not None and len(info["domain"]) > 20: - info["domain"] = "…" + info["domain"][-20:] - if info.get("ip6") is not None: - if width < 156: - info.pop("ip6") - else: - info["ip6"] = info["ip6"][:8] + "…" - await client.close() - print( - len(infos), - "peers ups, with", - members, - "members and", - len(infos) - members, - "non-members at", - datetime.now().strftime("%H:%M:%S"), - ) - infos = sorted(infos, key=get_network_sort_key) - print(tabulate(infos, headers="keys", tablefmt="orgtbl", stralign="center")) - -""" diff --git a/silkaj/network_tools.py b/silkaj/network_tools.py index d269ae52e002d522d144d5baef7d2ed285c417b8..742be9adbb1dce1596c9fdcd08319265274368b3 100644 --- a/silkaj/network_tools.py +++ b/silkaj/network_tools.py @@ -20,70 +20,17 @@ import re import socket import logging from sys import exit, stderr -from asyncio import sleep from duniterpy.api.client import Client -from duniterpy.api.bma import network from duniterpy.constants import IPV4_REGEX, IPV6_REGEX from silkaj.constants import ( G1_DEFAULT_ENDPOINT, G1_TEST_DEFAULT_ENDPOINT, CONNECTION_TIMEOUT, - ASYNC_SLEEP, FAILURE_EXIT_STATUS, ) -async def discover_peers(discover): - """ - From first node, discover his known nodes. - Remove from know nodes if nodes are down. - If discover option: scan all network to know all nodes. - display percentage discovering. - """ - client = ClientInstance().client - endpoints = await get_peers_among_leaves(client) - if discover: - print("Discovering network") - for i, endpoint in enumerate(endpoints): - if discover: - print("{0:.0f}%".format(i / len(endpoints) * 100)) - if best_endpoint_address(endpoint, False) is None: - endpoints.remove(endpoint) - elif discover: - endpoints = await recursive_discovering(endpoints, endpoint) - return endpoints - - -async def recursive_discovering(endpoints, endpoint): - """ - Discover recursively new nodes. - If new node found add it and try to found new node from his known nodes. - """ - api = generate_duniterpy_endpoint_format(endpoint) - sub_client = Client(api) - news = await get_peers_among_leaves(sub_client) - await sub_client.close() - for new in news: - if best_endpoint_address(new, False) is not None and new not in endpoints: - endpoints.append(new) - await recursive_discovering(endpoints, new) - return endpoints - - -async def get_peers_among_leaves(client): - """ - Browse among leaves of peers to retrieve the other peers’ endpoints - """ - leaves = await client(network.peers, leaves=True) - peers = list() - for leaf in leaves["leaves"]: - await sleep(ASYNC_SLEEP + 0.05) - leaf_response = await client(network.peers, leaf=leaf) - peers.append(leaf_response["leaf"]["value"]) - return parse_endpoints(peers) - - def parse_endpoints(rep): """ endpoints[]{"domain", "ip4", "ip6", "pubkey"} @@ -105,15 +52,6 @@ def parse_endpoints(rep): return endpoints -def generate_duniterpy_endpoint_format(ep): - api = "BASIC_MERKLED_API " if ep["port"] != "443" else "BMAS " - api += ep.get("domain") + " " if "domain" in ep else "" - api += ep.get("ip4") + " " if "ip4" in ep else "" - api += ep.get("ip6") + " " if "ip6" in ep else "" - api += ep.get("port") - return api - - def singleton(class_): instances = {}