Skip to content
Snippets Groups Projects
Commit 3dccbbfd authored by Moul's avatar Moul
Browse files

[mod] #264: Move 'net' command to net.py

- Clean unused imports
parent 664c4430
No related branches found
No related tags found
2 merge requests!146Merge dev into master branch to complete v0.8.0 development cycle,!133Disable 'net' command | Move convert_time() to tui.py
...@@ -26,10 +26,10 @@ from silkaj.cert import send_certification ...@@ -26,10 +26,10 @@ from silkaj.cert import send_certification
from silkaj.commands import ( from silkaj.commands import (
currency_info, currency_info,
difficulties, difficulties,
network_info,
argos_info, argos_info,
list_blocks, list_blocks,
) )
from silkaj.net import network_info
from silkaj.wot import received_sent_certifications, id_pubkey_correspondence from silkaj.wot import received_sent_certifications, id_pubkey_correspondence
from silkaj.auth import generate_auth_file from silkaj.auth import generate_auth_file
from silkaj.license import license_command from silkaj.license import license_command
......
...@@ -15,9 +15,8 @@ You should have received a copy of the GNU Affero General Public License ...@@ -15,9 +15,8 @@ You should have received a copy of the GNU Affero General Public License
along with Silkaj. If not, see <https://www.gnu.org/licenses/>. along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
""" """
from click import command, option, argument, IntRange, get_terminal_size from click import command, option, argument, IntRange
from datetime import datetime from os import system
from os import system, popen
from collections import OrderedDict from collections import OrderedDict
from tabulate import tabulate from tabulate import tabulate
from operator import itemgetter from operator import itemgetter
...@@ -26,19 +25,17 @@ import aiohttp ...@@ -26,19 +25,17 @@ import aiohttp
from _socket import gaierror from _socket import gaierror
import jsonschema import jsonschema
from duniterpy.api.client import Client, parse_text
from duniterpy.api import bma from duniterpy.api import bma
from silkaj.tools import coroutine from silkaj.tools import coroutine
from silkaj.wot import identity_of from silkaj.wot import identity_of
from silkaj.network_tools import ( from silkaj.network_tools import (
discover_peers,
best_endpoint_address, best_endpoint_address,
EndPoint, EndPoint,
ClientInstance, ClientInstance,
HeadBlock, HeadBlock,
) )
from silkaj.tools import convert_time, message_exit, CurrencySymbol from silkaj.tools import convert_time, CurrencySymbol
from silkaj.constants import ASYNC_SLEEP from silkaj.constants import ASYNC_SLEEP
...@@ -144,111 +141,6 @@ Common Proof-of-Work difficulty level: {5}, hash starting with `{6}`\n{7}".forma ...@@ -144,111 +141,6 @@ Common Proof-of-Work difficulty level: {5}, hash starting with `{6}`\n{7}".forma
) )
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"))
@command("blocks", help="Display blocks: default: 0 for current window size") @command("blocks", help="Display blocks: default: 0 for current window size")
@argument("number", default=0, type=IntRange(0, 5000)) @argument("number", default=0, type=IntRange(0, 5000))
@option( @option(
......
"""
Copyright 2016-2020 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 convert_time, message_exit
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"))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment