diff --git a/silkaj/blockchain_tools.py b/silkaj/blockchain_tools.py new file mode 100644 index 0000000000000000000000000000000000000000..837d44f2ee532a2231cd9c175902d2a129a85d98 --- /dev/null +++ b/silkaj/blockchain_tools.py @@ -0,0 +1,13 @@ +from silkaj.network_tools import get_request + + +class BlockchainParams(object): + __instance = None + + def __new__(cls, ep): + if BlockchainParams.__instance is None: + BlockchainParams.__instance = object.__new__(cls) + return BlockchainParams.__instance + + def __init__(self, ep): + self.params = get_request(ep, "blockchain/parameters") diff --git a/silkaj/silkaj.py b/silkaj/silkaj.py index 1f061b323dde864e0c3e8925304066d2f88d9582..0779e4de1f5f72ee8c96adb01152c70926eab47f 100644 --- a/silkaj/silkaj.py +++ b/silkaj/silkaj.py @@ -97,14 +97,8 @@ def cli(): ep["domain"] = ep["domain"][1:-1] return ep, cli_args -def get_parameters(ep): - head_block = get_current_block(ep) - params = get_request(ep, "blockchain/parameters") - return params, head_block def manage_cmd(ep, cli_args): - - params, head_block = get_parameters(ep) if cli_args.subcmd == "about": about() elif cli_args.subcmd == "info": @@ -142,7 +136,7 @@ def manage_cmd(ep, cli_args): id_pubkey_correspondence(ep, cli_args.subsubcmd) elif cli_args.subcmd == "wot": - received_sent_certifications(ep, params, cli_args.subsubcmd) + received_sent_certifications(ep, cli_args.subsubcmd) elif cli_args.subcmd == "license": display_license() diff --git a/silkaj/tools.py b/silkaj/tools.py index 6a965d9f8e44105644bf4690b099988bf6ae0b29..3f623957cfc73b0115b64315d9c8e5c21d347ca6 100644 --- a/silkaj/tools.py +++ b/silkaj/tools.py @@ -4,6 +4,7 @@ from re import compile, search from sys import exit from silkaj.constants import G1_SYMBOL, GTEST_SYMBOL +from silkaj.blockchain_tools import BlockchainParams def convert_time(timestamp, kind): diff --git a/silkaj/wot.py b/silkaj/wot.py index e683496c682d7b973599327af18174cee9aa263c..8f438ae332de9b4739a5f5b37878b5a07af4d57d 100644 --- a/silkaj/wot.py +++ b/silkaj/wot.py @@ -5,6 +5,7 @@ from collections import OrderedDict from silkaj.network_tools import get_request from silkaj.tools import message_exit, check_public_key, convert_time +from silkaj.blockchain_tools import BlockchainParams from silkaj.constants import NO_MATCHING_ID @@ -18,7 +19,7 @@ def get_sent_certifications(certs, time_first_block, params): return sent, expire -def received_sent_certifications(ep, params, id): +def received_sent_certifications(ep, id): """ get searched id get id of received and sent certifications @@ -28,6 +29,7 @@ def received_sent_certifications(ep, params, id): id_certs = get_informations_for_identity(ep, id) certifications = OrderedDict() system("clear") + params = BlockchainParams(ep).params for certs in id_certs["uids"]: if certs["uid"].lower() == id.lower(): pubkey = id_certs["pubkey"] @@ -37,13 +39,13 @@ def received_sent_certifications(ep, params, id): for cert in certs["others"]: certifications["received_expire"].append(expiration_date_from_block_id(cert["meta"]["block_number"], time_first_block, params)) certifications["received"].append(cert_written_in_the_blockchain(req["certifications"], cert)) - certifications["sent"], certifications["sent_expire"] = get_sent_certifications(id_certs, time_first_block, params) + certifications["sent"], certifications["sent_expire"] = get_sent_certifications(id_certs, time_first_block,params) nbr_sent_certs = len(certifications["sent"]) if "sent" in certifications else 0 print("{0} ({1}) from block #{2}\nreceived {3} and sent {4}/{5} certifications:\n{6}\n" .format(id, pubkey[:5] + "…", certs["meta"]["timestamp"][:15] + "…", len(certifications["received"]), nbr_sent_certs, params["sigStock"], tabulate(certifications, headers="keys", tablefmt="orgtbl", stralign="center"))) - membership_status(ep, params, certifications, certs, pubkey, req) + membership_status(ep, certifications, certs, pubkey, req) def cert_written_in_the_blockchain(written_certs, certifieur): @@ -53,7 +55,8 @@ def cert_written_in_the_blockchain(written_certs, certifieur): return certifieur["uids"][0] -def membership_status(ep, params, certifications, certs, pubkey, req): +def membership_status(ep, certifications, certs, pubkey, req): + params = BlockchainParams(ep).params if len(certifications["received"]) >= params["sigQty"]: print("Membership expiration due to certifications expirations: " + certifications["received_expire"][len(certifications["received"]) - params["sigQty"]]) member = is_member(ep, pubkey, certs["uid"])