Commit 953c3529 authored by Moul's avatar Moul

[enh] #135, #189, #206: wot requests improvements:

- #135: remove get_uid_from_pubkey()
 Replace two uppers function with identity_of() which requests
/wot/identity-of/<pubkey/uid> and only returns members identities with
their pubkey and uid, which improves performances.
- change is_member() as a wrapper of identity_of()
- change usage in `id`, `cert`, `tx`, `net`, `blocks`

- #189: Handle in a good way wot/identity-of requests exceptions

- #206: Set a sleep for async requests: `blocks` and `net` for leaves
parent ba80b8e5
...@@ -26,7 +26,7 @@ from silkaj.tools import convert_time, message_exit, coroutine ...@@ -26,7 +26,7 @@ from silkaj.tools import convert_time, message_exit, coroutine
from silkaj.network_tools import ClientInstance, HeadBlock from silkaj.network_tools import ClientInstance, HeadBlock
from silkaj.blockchain_tools import BlockchainParams from silkaj.blockchain_tools import BlockchainParams
from silkaj.license import license_approval from silkaj.license import license_approval
from silkaj.wot import is_member, get_uid_from_pubkey, get_informations_for_identity from silkaj.wot import is_member, get_informations_for_identity
@command("cert", help="Send certification") @command("cert", help="Send certification")
...@@ -42,8 +42,8 @@ async def send_certification(id_to_certify): ...@@ -42,8 +42,8 @@ async def send_certification(id_to_certify):
# Check whether current user is member # Check whether current user is member
issuer_pubkey = key.pubkey issuer_pubkey = key.pubkey
issuer_id = await get_uid_from_pubkey(issuer_pubkey) issuer = await is_member(issuer_pubkey)
if not await is_member(issuer_pubkey, issuer_id): if not issuer:
message_exit("Current identity is not member.") message_exit("Current identity is not member.")
if issuer_pubkey == id_to_certify["pubkey"]: if issuer_pubkey == id_to_certify["pubkey"]:
...@@ -74,7 +74,7 @@ async def send_certification(id_to_certify): ...@@ -74,7 +74,7 @@ async def send_certification(id_to_certify):
# Certification confirmation # Certification confirmation
await certification_confirmation( await certification_confirmation(
issuer_id, issuer_pubkey, id_to_certify, main_id_to_certify issuer, issuer_pubkey, id_to_certify, main_id_to_certify
) )
identity = Identity( identity = Identity(
...@@ -110,11 +110,11 @@ async def send_certification(id_to_certify): ...@@ -110,11 +110,11 @@ async def send_certification(id_to_certify):
async def certification_confirmation( async def certification_confirmation(
issuer_id, issuer_pubkey, id_to_certify, main_id_to_certify issuer, issuer_pubkey, id_to_certify, main_id_to_certify
): ):
cert = list() cert = list()
cert.append(["Cert", "From", "–>", "To"]) cert.append(["Cert", "From", "–>", "To"])
cert.append(["ID", issuer_id, "–>", main_id_to_certify["uid"]]) cert.append(["ID", issuer["uid"], "–>", main_id_to_certify["uid"]])
cert.append(["Pubkey", issuer_pubkey, "–>", id_to_certify["pubkey"]]) cert.append(["Pubkey", issuer_pubkey, "–>", id_to_certify["pubkey"]])
params = await BlockchainParams().params params = await BlockchainParams().params
cert_begins = convert_time(time(), "date") cert_begins = convert_time(time(), "date")
......
...@@ -17,11 +17,11 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. ...@@ -17,11 +17,11 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from click import command, option, argument, IntRange, get_terminal_size from click import command, option, argument, IntRange, get_terminal_size
from datetime import datetime from datetime import datetime
from time import sleep
from os import system, popen from os import system, popen
from collections import OrderedDict from collections import OrderedDict
from tabulate import tabulate from tabulate import tabulate
from operator import itemgetter from operator import itemgetter
from asyncio import sleep
import aiohttp import aiohttp
from _socket import gaierror from _socket import gaierror
import jsonschema import jsonschema
...@@ -30,7 +30,7 @@ from duniterpy.api.client import Client, parse_text ...@@ -30,7 +30,7 @@ from duniterpy.api.client import Client, parse_text
from duniterpy.api.bma import blockchain, node, ws from duniterpy.api.bma import blockchain, node, ws
from silkaj.tools import coroutine from silkaj.tools import coroutine
from silkaj.wot import get_uid_from_pubkey from silkaj.wot import identity_of
from silkaj.network_tools import ( from silkaj.network_tools import (
discover_peers, discover_peers,
best_endpoint_address, best_endpoint_address,
...@@ -39,7 +39,7 @@ from silkaj.network_tools import ( ...@@ -39,7 +39,7 @@ from silkaj.network_tools import (
HeadBlock, HeadBlock,
) )
from silkaj.tools import convert_time, message_exit, CurrencySymbol from silkaj.tools import convert_time, message_exit, CurrencySymbol
from silkaj.constants import NO_MATCHING_ID from silkaj.constants import ASYNC_SLEEP
@command("info", help="Display information about currency") @command("info", help="Display information about currency")
...@@ -201,16 +201,14 @@ async def network_info(discover, sort): ...@@ -201,16 +201,14 @@ async def network_info(discover, sort):
best_ep = best_endpoint_address(info, False) best_ep = best_endpoint_address(info, False)
print(best_ep if best_ep is None else info[best_ep], end=" ") print(best_ep if best_ep is None else info[best_ep], end=" ")
print(info["port"]) print(info["port"])
await sleep(ASYNC_SLEEP)
try: try:
info["uid"] = await get_uid_from_pubkey(ep, info["pubkey"]) info["uid"] = await identity_of(info["pubkey"])
if info["uid"] is NO_MATCHING_ID: info["uid"] = info["uid"]["uid"]
info["uid"] = None info["member"] = "yes"
else: members += 1
info["member"] = "yes"
members += 1
except: except:
pass info["uid"] = None
if info.get("member") is None:
info["member"] = "no" info["member"] = "no"
info["pubkey"] = info["pubkey"][:5] + "…" info["pubkey"] = info["pubkey"][:5] + "…"
for d in diffi["levels"]: for d in diffi["levels"]:
...@@ -286,15 +284,16 @@ async def list_blocks(number, detailed): ...@@ -286,15 +284,16 @@ async def list_blocks(number, detailed):
j += 1 j += 1
for pubkey in issuers_dict.keys(): for pubkey in issuers_dict.keys():
issuer = issuers_dict[pubkey] issuer = issuers_dict[pubkey]
uid = await get_uid_from_pubkey(issuer["pubkey"]) idty = await identity_of(issuer["pubkey"])
for issuer2 in list_issuers: for issuer2 in list_issuers:
if ( if (
issuer2.get("pubkey") is not None issuer2.get("pubkey") is not None
and issuer.get("pubkey") is not None and issuer.get("pubkey") is not None
and issuer2["pubkey"] == issuer["pubkey"] and issuer2["pubkey"] == issuer["pubkey"]
): ):
issuer2["uid"] = uid issuer2["uid"] = idty["uid"]
issuer2.pop("pubkey") issuer2.pop("pubkey")
await sleep(ASYNC_SLEEP)
await client.close() await client.close()
print( print(
"Last {0} blocks from n°{1} to n°{2}".format( "Last {0} blocks from n°{1} to n°{2}".format(
......
...@@ -22,4 +22,5 @@ GTEST_SYMBOL = "ĞTest" ...@@ -22,4 +22,5 @@ GTEST_SYMBOL = "ĞTest"
G1_DEFAULT_ENDPOINT = "g1.duniter.org", "443" G1_DEFAULT_ENDPOINT = "g1.duniter.org", "443"
G1_TEST_DEFAULT_ENDPOINT = "g1-test.duniter.org", "443" G1_TEST_DEFAULT_ENDPOINT = "g1-test.duniter.org", "443"
CONNECTION_TIMEOUT = 10 CONNECTION_TIMEOUT = 10
ASYNC_SLEEP = 0.1
SOURCES_PER_TX = 40 SOURCES_PER_TX = 40
...@@ -21,6 +21,7 @@ import socket ...@@ -21,6 +21,7 @@ import socket
import logging import logging
from sys import exit, stderr from sys import exit, stderr
from click import pass_context from click import pass_context
from asyncio import sleep
from duniterpy.api.client import Client from duniterpy.api.client import Client
from duniterpy.api.bma import blockchain, network from duniterpy.api.bma import blockchain, network
...@@ -28,6 +29,7 @@ from silkaj.constants import ( ...@@ -28,6 +29,7 @@ from silkaj.constants import (
G1_DEFAULT_ENDPOINT, G1_DEFAULT_ENDPOINT,
G1_TEST_DEFAULT_ENDPOINT, G1_TEST_DEFAULT_ENDPOINT,
CONNECTION_TIMEOUT, CONNECTION_TIMEOUT,
ASYNC_SLEEP,
) )
...@@ -75,6 +77,7 @@ async def get_peers_among_leaves(client): ...@@ -75,6 +77,7 @@ async def get_peers_among_leaves(client):
leaves = await client(network.peers, leaves=True) leaves = await client(network.peers, leaves=True)
peers = list() peers = list()
for leaf in leaves["leaves"]: for leaf in leaves["leaves"]:
await sleep(ASYNC_SLEEP + 0.05)
leaf_response = await client(network.peers, leaf=leaf) leaf_response = await client(network.peers, leaf=leaf)
peers.append(leaf_response["leaf"]["value"]) peers.append(leaf_response["leaf"]["value"])
return parse_endpoints(peers) return parse_endpoints(peers)
......
...@@ -25,14 +25,14 @@ from silkaj.network_tools import ClientInstance, HeadBlock ...@@ -25,14 +25,14 @@ from silkaj.network_tools import ClientInstance, HeadBlock
from silkaj.crypto_tools import check_public_key from silkaj.crypto_tools import check_public_key
from silkaj.tools import message_exit, CurrencySymbol, coroutine from silkaj.tools import message_exit, CurrencySymbol, coroutine
from silkaj.auth import auth_method from silkaj.auth import auth_method
from silkaj.wot import get_uid_from_pubkey from silkaj.wot import is_member
from silkaj.money import ( from silkaj.money import (
get_sources, get_sources,
get_amount_from_pubkey, get_amount_from_pubkey,
UDValue, UDValue,
amount_in_current_base, amount_in_current_base,
) )
from silkaj.constants import NO_MATCHING_ID, SOURCES_PER_TX from silkaj.constants import SOURCES_PER_TX
from duniterpy.api.bma.tx import process from duniterpy.api.bma.tx import process
from duniterpy.documents import BlockUID, Transaction from duniterpy.documents import BlockUID, Transaction
...@@ -165,19 +165,19 @@ async def transaction_confirmation( ...@@ -165,19 +165,19 @@ async def transaction_confirmation(
] ]
) )
tx.append(["from (pubkey)", issuer_pubkey]) tx.append(["from (pubkey)", issuer_pubkey])
id_from = await get_uid_from_pubkey(issuer_pubkey) id_from = await is_member(issuer_pubkey)
if id_from is not NO_MATCHING_ID: if id_from:
tx.append(["from (id)", id_from]) tx.append(["from (id)", id_from["uid"]])
for outputAddress in outputAddresses: for outputAddress in outputAddresses:
tx.append(["to (pubkey)", outputAddress]) tx.append(["to (pubkey)", outputAddress])
id_to = await get_uid_from_pubkey(outputAddress) id_to = await is_member(outputAddress)
if id_to is not NO_MATCHING_ID: if id_to:
tx.append(["to (id)", id_to]) tx.append(["to (id)", id_to["uid"]])
if outputBackChange: if outputBackChange:
tx.append(["Backchange (pubkey)", outputBackChange]) tx.append(["Backchange (pubkey)", outputBackChange])
id_backchange = await get_uid_from_pubkey(outputBackChange) id_backchange = await is_member(outputBackChange)
if id_backchange is not NO_MATCHING_ID: if id_backchange:
tx.append(["Backchange (id)", id_backchange]) tx.append(["Backchange (id)", id_backchange["uid"]])
tx.append(["comment", comment]) tx.append(["comment", comment])
return tx return tx
......
...@@ -20,6 +20,7 @@ from time import time ...@@ -20,6 +20,7 @@ from time import time
from tabulate import tabulate from tabulate import tabulate
from collections import OrderedDict from collections import OrderedDict
from duniterpy.api.bma import wot, blockchain from duniterpy.api.bma import wot, blockchain
from duniterpy.api.errors import DuniterError
from silkaj.network_tools import ClientInstance from silkaj.network_tools import ClientInstance
from silkaj.crypto_tools import check_public_key from silkaj.crypto_tools import check_public_key
...@@ -119,7 +120,9 @@ async def membership_status(certifications, certs, pubkey, req): ...@@ -119,7 +120,9 @@ async def membership_status(certifications, certs, pubkey, req):
len(certifications["received"]) - params["sigQty"] len(certifications["received"]) - params["sigQty"]
] ]
) )
member = await is_member(pubkey, certs["uid"]) member = await is_member(pubkey)
if member:
member = True
print("member:", member) print("member:", member)
if req["revoked"]: if req["revoked"]:
print( print(
...@@ -159,7 +162,7 @@ async def id_pubkey_correspondence(id_pubkey): ...@@ -159,7 +162,7 @@ async def id_pubkey_correspondence(id_pubkey):
if check_public_key(id_pubkey, False): if check_public_key(id_pubkey, False):
print( print(
"{} public key corresponds to identity: {}".format( "{} public key corresponds to identity: {}".format(
id_pubkey, await get_uid_from_pubkey(id_pubkey) id_pubkey, await identity_of(id_pubkey)
) )
) )
else: else:
...@@ -193,15 +196,30 @@ async def get_informations_for_identity(id): ...@@ -193,15 +196,30 @@ async def get_informations_for_identity(id):
message_exit(NO_MATCHING_ID) message_exit(NO_MATCHING_ID)
async def get_uid_from_pubkey(pubkey): async def identity_of(pubkey_uid):
"""
Only works for members
Not able to get corresponding uid from a non-member identity
Able to know if an identity is member or not
"""
client = ClientInstance().client
try:
return await client(wot.identity_of, pubkey_uid)
except DuniterError as e:
raise DuniterError(e)
except ValueError as e:
pass
async def is_member(pubkey_uid):
"""
Check identity is member
If member, return corresponding identity, else: False
"""
try: try:
client = ClientInstance().client return await identity_of(pubkey_uid)
lookups = await client(wot.lookup, pubkey)
except: except:
return NO_MATCHING_ID return False
for lookup in lookups["results"]:
if lookup["uids"][0]["uid"] != pubkey:
return lookup["uids"][0]["uid"]
async def get_informations_for_identities(identifier): async def get_informations_for_identities(identifier):
...@@ -216,12 +234,3 @@ async def get_informations_for_identities(identifier): ...@@ -216,12 +234,3 @@ async def get_informations_for_identities(identifier):
except: except:
return NO_MATCHING_ID return NO_MATCHING_ID
return results["results"] return results["results"]
async def is_member(pubkey, uid):
client = ClientInstance().client
members = await client(wot.members)
for member in members["results"]:
if pubkey in member["pubkey"] and uid in member["uid"]:
return True
return False
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment