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

Remove difficulty cmd (#429)

parent 49d7e914
No related branches found
No related tags found
1 merge request!232#429: Remove blockchain difficulty and verify commands
# Copyright 2016-2023 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 collections import OrderedDict
from operator import itemgetter
from os import system
from typing import Dict, Tuple
import click
import jsonschema
from duniterpy.api import bma
from duniterpy.api.client import WSConnection
from pendulum import from_timestamp
from websocket._exceptions import WebSocketConnectionClosedException
from silkaj import network, tui
from silkaj.constants import ALL
@click.command(
"difficulty",
help="Display the current Proof of Work difficulty level to generate the next block",
)
def difficulties() -> None:
client = network.client_instance()
try:
ws = client(bma.ws.block)
while True:
current = ws.receive_json()
jsonschema.validate(current, bma.ws.WS_BLOCK_SCHEMA)
diffi = client(bma.blockchain.difficulties)
display_diffi(current, diffi)
except (jsonschema.ValidationError, WebSocketConnectionClosedException) as e:
print(f"{str(e.__class__.__name__)}: {str(e)}")
def display_diffi(current: WSConnection, diffi: Dict) -> None:
levels = [OrderedDict((i, d[i]) for i in ("uid", "level")) for d in diffi["levels"]]
diffi["levels"] = levels
issuers = 0
sorted_diffi = sorted(diffi["levels"], key=itemgetter("level"), reverse=True)
for d in diffi["levels"]:
if d["level"] / 2 < current["powMin"]:
issuers += 1
d["match"] = match_pattern(d["level"])[0][:20]
d["Π diffi"] = compute_power(match_pattern(d["level"])[1])
d["Σ diffi"] = d.pop("level")
system("cls||clear")
block_gen = from_timestamp(current["time"], tz="local").format(ALL)
match = match_pattern(int(current["powMin"]))[0]
table = tui.Table(style="columns").set_cols_dtype(["t", "t", "t", "i"])
table.fill_from_dict_list(sorted_diffi)
content = f'Current block: n°{current["number"]}, generated on {block_gen}\n\
Generation of next block n°{diffi["block"]} \
possible by at least {issuers}/{len(diffi["levels"])} members\n\
Common Proof-of-Work difficulty level: {current["powMin"]}, hash starting with `{match}`\n\
{table.draw()}'
print(content)
def match_pattern(_pow: int, match: str = "", p: int = 1) -> Tuple[str, int]:
while _pow > 0:
if _pow >= 16:
match += "0"
_pow -= 16
p *= 16
else:
match += f"[0-{hex(15 - _pow)[2:].upper()}]"
p *= _pow
_pow = 0
return f"{match}*", p
def compute_power(nbr: float, power: int = 0) -> str:
while nbr >= 10:
nbr /= 10
power += 1
return f"{nbr:.1f} × 10^{power}"
...@@ -21,7 +21,6 @@ from duniterpy.api.endpoint import endpoint as du_endpoint ...@@ -21,7 +21,6 @@ from duniterpy.api.endpoint import endpoint as du_endpoint
from silkaj.about import about from silkaj.about import about
from silkaj.auth import generate_auth_file from silkaj.auth import generate_auth_file
from silkaj.blockchain.blocks import list_blocks from silkaj.blockchain.blocks import list_blocks
from silkaj.blockchain.difficulty import difficulties
from silkaj.blockchain.information import currency_info from silkaj.blockchain.information import currency_info
from silkaj.blockchain.verify import verify_blocks_signatures from silkaj.blockchain.verify import verify_blocks_signatures
from silkaj.checksum import checksum_command from silkaj.checksum import checksum_command
...@@ -137,7 +136,6 @@ def blockchain_group() -> None: ...@@ -137,7 +136,6 @@ def blockchain_group() -> None:
blockchain_group.add_command(list_blocks) blockchain_group.add_command(list_blocks)
blockchain_group.add_command(difficulties)
blockchain_group.add_command(currency_info) blockchain_group.add_command(currency_info)
blockchain_group.add_command(verify_blocks_signatures) blockchain_group.add_command(verify_blocks_signatures)
......
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