From fa6cab5aa62f64c89e04e3d73225a186989d4090 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Thu, 23 Aug 2018 19:00:43 +0200 Subject: [PATCH] [enh] #42: use a singleton for blockchain parameters. --- silkaj/blockchain_tools.py | 13 +++++++++++++ silkaj/silkaj.py | 8 +------- silkaj/tools.py | 1 + silkaj/wot.py | 11 +++++++---- 4 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 silkaj/blockchain_tools.py diff --git a/silkaj/blockchain_tools.py b/silkaj/blockchain_tools.py new file mode 100644 index 00000000..837d44f2 --- /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 1f061b32..0779e4de 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 6a965d9f..3f623957 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 e683496c..8f438ae3 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"]) -- GitLab