Coverage for silkaj/blockchain/blocks.py: 20%
79 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-22 12:04 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-22 12:04 +0000
1# Copyright 2016-2025 Maël Azimi <m.a@moul.re>
2#
3# Silkaj is free software: you can redistribute it and/or modify
4# it under the terms of the GNU Affero General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# Silkaj is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU Affero General Public License for more details.
12#
13# You should have received a copy of the GNU Affero General Public License
14# along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
16import time
17from operator import itemgetter
18from urllib.error import HTTPError
20import pendulum
21import rich_click as click
22from duniterpy.api import bma
24from silkaj import tui
25from silkaj.blockchain.tools import get_head_block
26from silkaj.constants import ALL, BMA_SLEEP
27from silkaj.network import client_instance
28from silkaj.wot.tools import identity_of
31@click.command("blocks", help="Display blocks: default: 0 for current window size")
32@click.argument("number", default=0, type=click.IntRange(0, 5000))
33@click.option(
34 "--detailed",
35 "-d",
36 is_flag=True,
37 help="Force detailed view. Compact view happen over 30 blocks",
38)
39def list_blocks(number: int, detailed: bool) -> None:
40 head_block = get_head_block()
41 current_nbr = head_block["number"]
42 if number == 0:
43 number = head_block["issuersFrame"]
44 client = client_instance()
45 blocks = client(bma.blockchain.blocks, number, current_nbr - number + 1)
46 issuers = []
47 issuers_dict = {}
48 for block in blocks:
49 issuer = {}
50 issuer["pubkey"] = block["issuer"]
51 if detailed or number <= 30:
52 issuer["block"] = block["number"]
53 issuer["gentime"] = pendulum.from_timestamp(
54 block["time"], tz="local"
55 ).format(ALL)
56 issuer["mediantime"] = pendulum.from_timestamp(
57 block["medianTime"], tz="local"
58 ).format(ALL)
59 issuer["hash"] = block["hash"][:10]
60 issuer["powMin"] = block["powMin"]
61 issuers_dict[issuer["pubkey"]] = issuer
62 issuers.append(issuer)
63 for pubkey in issuers_dict.items():
64 issuer = issuers_dict[pubkey[0]]
65 time.sleep(BMA_SLEEP)
66 try:
67 idty = identity_of(issuer["pubkey"])
68 except HTTPError:
69 idty = None
70 for issuer2 in issuers:
71 if (
72 issuer2.get("pubkey") is not None
73 and issuer.get("pubkey") is not None
74 and issuer2["pubkey"] == issuer["pubkey"]
75 ):
76 issuer2["uid"] = idty["uid"] if idty else None
77 issuer2.pop("pubkey")
78 print_blocks_views(issuers, current_nbr, number, detailed)
81def print_blocks_views(issuers, current_nbr, number, detailed):
82 header = (
83 f"Last {number} blocks from n°{current_nbr - number + 1} to n°{current_nbr}"
84 )
85 print(header, end=" ")
86 if detailed or number <= 30:
87 sorted_list = sorted(issuers, key=itemgetter("block"), reverse=True)
89 table = tui.Table(style="columns")
90 table.set_cols_align(["r", "r", "r", "r", "r", "l"])
91 table.set_cols_dtype(["i", "t", "t", "t", "i", "t"])
92 table.fill_from_dict_list(sorted_list)
93 table.set_cols_align(["r", "r", "r", "r", "r", "l"])
94 table.set_cols_dtype(["i", "t", "t", "t", "i", "t"])
95 print(f"\n{table.draw()}")
97 else:
98 list_issued = []
99 for issuer in issuers:
100 found = False
101 for issued in list_issued:
102 if issued.get("uid") is not None and issued["uid"] == issuer["uid"]:
103 issued["blocks"] += 1
104 found = True
105 break
106 if not found:
107 issued = {}
108 issued["uid"] = issuer["uid"]
109 issued["blocks"] = 1
110 list_issued.append(issued)
111 for issued in list_issued:
112 issued["percent"] = round(issued["blocks"] / number * 100)
113 sorted_list = sorted(list_issued, key=itemgetter("blocks"), reverse=True)
114 table = tui.Table(style="columns")
115 table.fill_from_dict_list(sorted_list)
116 table.set_cols_align(["l", "r", "r"])
117 table.set_cols_dtype(["t", "i", "i"])
118 print(f"from {len(list_issued)} issuers\n{table.draw()}")