diff --git a/scripts/cli.py b/scripts/cli.py index 9bd994b03311..bced6937fdc2 100755 --- a/scripts/cli.py +++ b/scripts/cli.py @@ -1,12 +1,19 @@ import asyncio import argparse import aiohttp +import pprint +import json +import datetime import time from time import struct_time, localtime -from src.server.connection import NodeType +from typing import Callable, List, Optional, Tuple, Dict -# from src.types.header_block import SmallHeaderBlock, HeaderBlock +from src.server.connection import NodeType +from src.types.full_block import FullBlock +from src.types.header_block import HeaderBlock +from src.types.sized_bytes import bytes32 +from src.util.ints import uint64 from src.rpc.rpc_client import RpcClient from src.util.byte_types import hexstr_to_bytes @@ -48,7 +55,7 @@ async def main(): nargs="?", const=True, default=False, - help="Get state", + help="Get connections status", ) parser.add_argument( @@ -61,11 +68,12 @@ async def main(): args = parser.parse_args() - # print(args) + #print(args) try: client = await RpcClient.create(args.rpc_port) + #print (dir(client)) # TODO: Add other rpc calls # TODO: pretty print response if args.state: @@ -76,43 +84,68 @@ async def main(): ips = blockchain_state["ips"] sync_mode = blockchain_state["sync_mode"] total_iters = lca_block.data.total_iters + num_blocks: int = 10 - # print (dir(lca_block)) if sync_mode: - print("Current Blockchain Status. Full Node Syncing") + sync_max_block = await client.get_heaviest_block_seen() + #print (max_block) + print ("Current Blockchain Status. Full Node Syncing to", sync_max_block.data.height) else: - print("Current Blockchain Status. Full Node Synced") + print ("Current Blockchain Status. Full Node Synced") print("Current least common ancestor ", lca_block.header_hash) - # print ("LCA time",time.ctime(lca_block.data.timestamp),"LCA height:",lca_block.height) + #print ("LCA time",time.ctime(lca_block.data.timestamp),"LCA height:",lca_block.height) lca_time = struct_time(localtime(lca_block.data.timestamp)) - print( - "LCA time", - time.strftime("%a %b %d %Y %T %Z", lca_time), - "LCA height:", - lca_block.height, - ) - heads_text = "Heights of tips: " + str([h.height for h in tips]) + print ("LCA time",time.strftime("%a %b %d %Y %T %Z", lca_time),"LCA height:",lca_block.height) + heads_text="Heights of tips: " + str([h.height for h in tips]) difficulty_label = f"Current difficulty: {difficulty}" print(heads_text) - print(difficulty_label) - print("Current VDF iterations per second:", ips) - # print("LCA data:\n", lca_block.data) - print("Total iterations since genesis:", total_iters) + print (difficulty_label) + print ("Current VDF iterations per second:",ips) + #print("LCA data:\n", lca_block.data) + print("Total iterations since genesis:",total_iters) + print ("") + heads: List[HeaderBlock] = tips + added_blocks: List[HeaderBlock] = [] + while len(added_blocks) < num_blocks and len(heads) > 0: + heads = sorted(heads, key=lambda b: b.height, reverse=True) + max_block = heads[0] + if max_block not in added_blocks: + added_blocks.append(max_block) + heads.remove(max_block) + prev: Optional[HeaderBlock] = await client.get_header( + max_block.prev_header_hash + ) + if prev is not None: + heads.append(prev) + latest_blocks_labels = [] + for i, b in enumerate(added_blocks): + latest_blocks_labels.append ( + f"{b.height}:{b.header_hash}" + f" {'LCA' if b.header_hash == lca_block.header_hash else ''}" + f" {'TIP' if b.header_hash in [h.header_hash for h in tips] else ''}" + ) + for i in range(len(latest_blocks_labels)): + if (i<2): + print (latest_blocks_labels[i]) + elif (i==2): + print (latest_blocks_labels[i],"\n"," -----") + else: + print ("",latest_blocks_labels[i]) + #if called together with other arguments, leave a blank line + if args.connections: + print ("") if args.connections: connections = await client.get_connections() - print("Connections") - print( - f"Type IP Ports NodeID " - f"Last Connect MB Up|Dwn" - ) + print ("Connections") + print ("Type IP Ports NodeID Last Connect MB Up|Dwn") for con in connections: - last_connect_tuple = struct_time(localtime(con["last_message_time"])) - # last_connect = time.ctime(con['last_message_time']) + last_connect_tuple = struct_time(localtime(con['last_message_time'])) + #last_connect = time.ctime(con['last_message_time']) last_connect = time.strftime("%b %d %T", last_connect_tuple) - mb_down = con["bytes_read"] / 1024 - mb_up = con["bytes_written"] / 1024 - # print (last_connect) + mb_down = con['bytes_read']/1024 + mb_up = con['bytes_written']/1024 + #print (last_connect) con_str = ( f"{NodeType(con['type']).name:9} {con['peer_host']:39} " f"{con['peer_port']:5}/{con['peer_server_port']:<5}" @@ -120,17 +153,20 @@ async def main(): f"{last_connect} " f"{mb_down:7.1f}|{mb_up:<7.1f}" ) - print(con_str) + print (con_str) + #if called together with other arguments, leave a blank line + if args.state: + print ("") elif args.block_header_hash != "": block = await client.get_block(hexstr_to_bytes(args.block_header_hash)) - # print(dir(block)) + #print(dir(block)) if block is not None: - print("Block header:") - print(block.header) + print ("Block header:") + print (block.header) block_time = struct_time(localtime(block.header.data.timestamp)) - print("Block time:", time.strftime("%a %b %d %Y %T %Z", block_time)) + print ("Block time:",time.strftime("%a %b %d %Y %T %Z", block_time)) else: - print("Block hash", args.block_header_hash, "not found.") + print ("Block hash", args.block_header_hash, "not found.") except Exception as e: if isinstance(e, aiohttp.client_exceptions.ClientConnectorError): @@ -141,5 +177,4 @@ async def main(): client.close() await client.await_closed() - asyncio.run(main())