diff --git a/silkaj/cli.py b/silkaj/cli.py index 200298e845e03bb972a20aab3a0890c01d394a62..524140917867ad1dd983f1ff40c08c2e4af04bcc 100644 --- a/silkaj/cli.py +++ b/silkaj/cli.py @@ -29,6 +29,7 @@ from silkaj.commands import ( network_info, argos_info, list_blocks, + smiths, ) from silkaj.wot import received_sent_certifications, id_pubkey_correspondence from silkaj.auth import generate_auth_file @@ -101,6 +102,7 @@ cli.add_command(argos_info) cli.add_command(generate_auth_file) cli.add_command(cmd_amount) cli.add_command(list_blocks) +cli.add_command(smiths) cli.add_command(send_certification) cli.add_command(difficulties) cli.add_command(transaction_history) diff --git a/silkaj/commands.py b/silkaj/commands.py index bc8247e3233cfac50752920bf9709afe864ef262..5a77136bdd68560b7f75bcac471a31b63c05d787 100644 --- a/silkaj/commands.py +++ b/silkaj/commands.py @@ -249,6 +249,92 @@ async def network_info(discover, sort): print(tabulate(infos, headers="keys", tablefmt="orgtbl", stralign="center")) +@command("smiths", help="Display smiths informations (version, blocks on frame, diff)") +@coroutine +async def smiths(): + head_block = await HeadBlock().head_block + current_nbr = head_block["number"] + frame_size = head_block["issuersFrame"] + client = ClientInstance().client + # Get current frame blocks + blocks = await client( + bma.blockchain.blocks, frame_size, current_nbr - frame_size + 1 + ) + # Get issuers difficulties + difficulties_obj = await client(bma.blockchain.difficulties) + difficulties_dict = dict() + for entry in difficulties_obj["levels"]: + difficulties_dict[entry["uid"]] = entry["level"] + # Get ws2p heads + heads_obj = await client(bma.network.ws2p_heads) + heads_dict = dict() + for entry in heads_obj["heads"]: + head_v2 = entry["messageV2"].split(":") + heads_dict[head_v2[3]] = head_v2 + + # Collect issuers and count number of forged blocks for each + issuers_dict = dict() + versions_dict = dict() + for block in blocks: + if block["issuer"] in issuers_dict: + # Update issuer + issuer = issuers_dict[block["issuer"]] + issuer["blocks_on_frame"] += 1 + else: + # Create issuer + issuer = OrderedDict() + + # get issuer version + version = "unknown" + if block["issuer"] in heads_dict: + version = heads_dict[block["issuer"]][7] + # Counting version occurrences + if version in versions_dict: + versions_dict[version] += 1 + else: + versions_dict[version] = 1 + + # Fill issuer infos + issuer["uid"] = "" + issuer["version"] = version + issuer["blocks_on_frame"] = 1 + issuer["diff"] = 0 + issuers_dict[block["issuer"]] = issuer + # Get username of each issuer (and fill their difficulty) + for pubkey, issuer in issuers_dict.items(): + idty = await identity_of(pubkey) + issuer["uid"] = idty["uid"] + issuer["diff"] = difficulties_dict[issuer["uid"]] + await sleep(ASYNC_SLEEP) + await client.close() + # print datas + print( + "Current frame: {0} issuers [{1} blocks (#{2} to #{3})]\n".format( + len(issuers_dict), frame_size, current_nbr - frame_size + 1, current_nbr + ) + ) + for version, count in versions_dict.items(): + print( + "{0}: {1} smith{2} [{3} %]".format( + version, count, "s" if count > 1 else "", round(count / len(issuers_dict) * 100, 2) + ) + ) + sorted_list = sorted( + issuers_dict.values(), key=itemgetter("blocks_on_frame"), reverse=True + ) + print( + "{0}".format( + tabulate( + sorted_list, + headers="keys", + tablefmt="orgtbl", + floatfmt=".1f", + stralign="center", + ), + ) + ) + + @command("blocks", help="Display blocks: default: 0 for current window size") @argument("number", default=0, type=IntRange(0, 5000)) @option( @@ -278,6 +364,7 @@ async def list_blocks(number, detailed): issuer["powMin"] = block["powMin"] issuers_dict[issuer["pubkey"]] = issuer issuers.append(issuer) + # Get username of each issuer for pubkey in issuers_dict.keys(): issuer = issuers_dict[pubkey] idty = await identity_of(issuer["pubkey"]) @@ -291,6 +378,7 @@ async def list_blocks(number, detailed): issuer2.pop("pubkey") await sleep(ASYNC_SLEEP) await client.close() + print( "Last {0} blocks from n°{1} to n°{2}".format( number, current_nbr - number + 1, current_nbr