Commit f04a9103 authored by Moul's avatar Moul
Browse files

[enh] add `--discover` option to network subcmd which to discover network:

- add discovering f() which scan recursively the network.
- % display state.
- comment f(), display at which step is the program.
parent 15d0898d
......@@ -49,17 +49,18 @@ def difficulties(ep):
.format(current["powMin"], match, diffi["block"], issuers, len(diffi["levels"]),
tabulate(sorted_diffi, headers="keys", tablefmt="orgtbl")))
def network_info(ep):
def network_info(ep, discover):
rows, columns = os.popen('stty size', 'r').read().split()
# print(rows, columns) # debug
wide = int(columns)
if wide < 146:
print("Wide screen need to be larger than 146. Current wide:", wide)
exit()
endpoints = discover_peers(ep)
endpoints = discover_peers(ep, discover)
### Todo : renommer endpoints en info ###
diffi = request(ep, "blockchain/difficulties")
i, members = 0, 0
print("Getting informations about nodes:")
while (i < len(endpoints)):
print("{0:.0f}%".format(i/len(endpoints) * 100, 1), end = " ")
best_ep = best_node(endpoints[i], 0)
......
......@@ -4,14 +4,34 @@ import json
import socket
import urllib.request
def discover_peers(ep):
peers = request(ep, "network/peers")["peers"]
endpoints = parse_endpoints(peers)
for ep in endpoints:
def discover_peers(ep, 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.
"""
endpoints = parse_endpoints(request(ep, "network/peers")["peers"])
if discover: print("Discovering network")
for i, ep in enumerate(endpoints):
if discover: print("{0:.0f}%".format(i/len(endpoints) * 100)) #, ep) # debug
if best_node(ep, 0) is None: endpoints.remove(ep)
# Todo: discover network, some nodes are missing
### Todo: search for other nodes asking on other nodes because nodes are missing comperated to Sakia ###
elif discover: endpoints = recursive_discovering(endpoints, ep)
return (endpoints)
def recursive_discovering(endpoints, ep):
"""
Discover recursively new nodes.
If new node found add it and try to found new node from his known nodes.
"""
news = parse_endpoints(request(ep, "network/peers")["peers"])
for new in news:
# print("-", new) # debug
if best_node(new, 0) is not None and new not in endpoints:
endpoints.append(new)
recursive_discovering(endpoints, new)
return (endpoints)
# Check why network discovering stayed stuck when checking some node without domain name: it happen sometimes
def parse_endpoints(rep):
"""
......
......@@ -22,7 +22,7 @@ def manage_cmd(ep, c):
elif c.subcmd == "diffi":
difficulties(ep)
elif c.subcmd == "network":
network_info(ep)
network_info(ep, c.contains_switches("discover"))
elif c.subcmd == "issuers" and c.subsubcmd and int(c.subsubcmd) >= 0:
list_issuers(ep, int(c.subsubcmd), c.contains_switches('last'))
else: usage()
......@@ -35,6 +35,7 @@ def usage():
\n - info: display information about currency \
\n - diffi: list proof-of-work difficulty to generate next block \
\n - network: display current network with many information \
\n - `--discover` option to discover all network (could take a while) \
\n - issuers n: display last n issuers (`0` for all blockchain) \
\n - last issuers are displayed under n ≤ 30. To force display last ones, use `--last` option \
\ncustom endpoint with options `-p` or `--peer` and <domain>:<port>")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment