From a2dcd4651f6f3fc24b155b1779205ba7994e04de Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Sun, 15 Mar 2015 21:20:04 +0100 Subject: [PATCH] Renamed "Person.name" to "Person.uid" --- src/cutecoin/core/graph.py | 6 ++-- src/cutecoin/core/net/network.py | 16 +++++++++++ src/cutecoin/core/net/node.py | 47 ++++++++++++++++++++++--------- src/cutecoin/core/person.py | 31 +++++++++++--------- src/cutecoin/gui/certification.py | 2 +- src/cutecoin/gui/member.py | 2 +- src/cutecoin/gui/network_tab.py | 1 - src/cutecoin/models/members.py | 2 +- src/cutecoin/models/txhistory.py | 9 ++---- 9 files changed, 75 insertions(+), 41 deletions(-) diff --git a/src/cutecoin/core/graph.py b/src/cutecoin/core/graph.py index ee11732d..6ffa7f9a 100644 --- a/src/cutecoin/core/graph.py +++ b/src/cutecoin/core/graph.py @@ -45,7 +45,7 @@ class Graph(object): """ path = list() - logging.debug("path between %s to %s..." % (from_person.name, to_person.name)) + logging.debug("path between %s to %s..." % (from_person.uid, to_person.uid)) if from_person.pubkey not in self._graph.keys(): self.add_person(from_person) certifier_list = from_person.certifiers_of(self.community) @@ -79,7 +79,7 @@ class Graph(object): # functions keywords args are persistent... Need to reset it with None trick connected = connected or (list() and (connected is None)) done = done or (list() and (done is None)) - logging.debug("search %s in " % person.name) + logging.debug("search %s in " % person.uid) logging.debug([self._graph[pubkey]['text'] for pubkey in connected]) # for each pubkey connected... for pubkey in tuple(connected): @@ -261,7 +261,7 @@ class Graph(object): self._graph[person.pubkey] = { 'id': person.pubkey, 'arcs': arcs, - 'text': person.name, + 'text': person.uid, 'tooltip': person.pubkey, 'status': status, 'connected': connected diff --git a/src/cutecoin/core/net/network.py b/src/cutecoin/core/net/network.py index 37990432..0dd8b9ca 100644 --- a/src/cutecoin/core/net/network.py +++ b/src/cutecoin/core/net/network.py @@ -61,7 +61,14 @@ class Network(QObject): node = Node.from_json(self.currency, data) self._nodes.append(node) logging.debug("Loading : {:}".format(data['pubkey'])) + for n in self._nodes: + try: + n.changed.disconnect() + except TypeError: + pass self._nodes = self.crawling() + for n in self._nodes: + n.changed.connect(self.nodes_changed) @classmethod def from_json(cls, currency, json_data): @@ -161,6 +168,15 @@ class Network(QObject): for node in [n for n in nodes if n.state == Node.ONLINE]: node.check_sync(block_max) + for node in self._nodes: + if node.last_change + 3600 < time.time() and \ + node.state in (Node.OFFLINE, Node.CORRUPTED): + try: + node.changed.disconnect() + except TypeError: + pass + self._nodes.remove(node) + #TODO: Offline nodes for too long have to be removed #TODO: Corrupted nodes should maybe be removed faster ? diff --git a/src/cutecoin/core/net/node.py b/src/cutecoin/core/net/node.py index 974dce8c..37035da4 100644 --- a/src/cutecoin/core/net/node.py +++ b/src/cutecoin/core/net/node.py @@ -33,7 +33,8 @@ class Node(QObject): changed = pyqtSignal() - def __init__(self, currency, endpoints, uid, pubkey, block, state): + def __init__(self, currency, endpoints, uid, pubkey, block, + state, last_change): ''' Constructor ''' @@ -45,6 +46,7 @@ class Node(QObject): self._state = state self._neighbours = [] self._currency = currency + self._last_change = last_change @classmethod def from_address(cls, currency, address, port): @@ -65,7 +67,8 @@ class Node(QObject): if peer.currency != currency: raise InvalidNodeCurrency(peer.currency, currency) - node = cls(peer.currency, peer.endpoints, "", peer.pubkey, 0, Node.ONLINE) + node = cls(peer.currency, peer.endpoints, "", peer.pubkey, 0, + Node.ONLINE, time.time()) node.refresh_state() return node @@ -82,7 +85,8 @@ class Node(QObject): if peer.currency != currency: raise InvalidNodeCurrency(peer.currency, currency) - node = cls(peer.currency, peer.endpoints, "", "", 0, Node.ONLINE) + node = cls(peer.currency, peer.endpoints, "", "", 0, + Node.ONLINE, time.time()) node.refresh_state() return node @@ -91,6 +95,7 @@ class Node(QObject): endpoints = [] uid = "" pubkey = "" + last_change = time.time() for endpoint_data in data['endpoints']: endpoints.append(Endpoint.from_inline(endpoint_data)) @@ -98,20 +103,25 @@ class Node(QObject): if currency in data: currency = data['currency'] - if uid in data: + if 'uid' in data: uid = data['uid'] - if pubkey in data: + if 'pubkey' in data: pubkey = data['pubkey'] - node = cls(currency, endpoints, uid, pubkey, 0, Node.ONLINE) + if 'last_change' in data: + last_change = data['last_change'] + + node = cls(currency, endpoints, uid, pubkey, 0, + Node.ONLINE, last_change) node.refresh_state() return node def jsonify(self): data = {'pubkey': self._pubkey, 'uid': self._uid, - 'currency': self._currency} + 'currency': self._currency, + 'last_change': self._last_change} endpoints = [] for e in self._endpoints: endpoints.append(e.inline()) @@ -146,11 +156,20 @@ class Node(QObject): def uid(self): return self._uid + @property + def last_change(self): + return self._last_change + + def _change_state(self, new_state): + if self.state != new_state: + self._last_change = time.time() + self._state = new_state + def check_sync(self, block): if self._block < block: - self._state = Node.DESYNCED + self._change_state(Node.DESYNCED) else: - self._state = Node.ONLINE + self._change_state(Node.ONLINE) def _request_uid(self): uid = "" @@ -189,14 +208,14 @@ class Node(QObject): if '404' in e: block_number = 0 except RequestException: - self._state = Node.OFFLINE + self._change_state(Node.OFFLINE) emit_change = True # If not is offline, do not refresh last data - if self._state != Node.OFFLINE: + if self.state != Node.OFFLINE: # If not changed its currency, consider it corrupted if node_currency != self._currency: - self.state = Node.CORRUPTED + self._change_state(Node.CORRUPTED) emit_change = True else: node_uid = self._request_uid() @@ -234,7 +253,7 @@ class Node(QObject): if self.pubkey not in [n.pubkey for n in found_nodes]: # if node is corrupted remove it - if self._state != Node.CORRUPTED: + if self.state != Node.CORRUPTED: found_nodes.append(self) try: logging.debug(self.neighbours) @@ -252,7 +271,7 @@ class Node(QObject): traversed_pubkeys, interval) time.sleep(interval) except RequestException as e: - self._state = Node.OFFLINE + self._change_state(Node.OFFLINE) def __str__(self): return ','.join([str(self.pubkey), str(self.endpoint.server), str(self.endpoint.port), str(self.block), diff --git a/src/cutecoin/core/person.py b/src/cutecoin/core/person.py index 3dc466f0..70434884 100644 --- a/src/cutecoin/core/person.py +++ b/src/cutecoin/core/person.py @@ -70,19 +70,19 @@ class cached(object): #TODO: Change Person to Identity ? class Person(object): ''' - A person with a name and a pubkey + A person with a uid and a pubkey ''' _instances = {} - def __init__(self, name, pubkey, cache): + def __init__(self, uid, pubkey, cache): ''' Initializing a person object. - :param str name: The person name, also known as its uid on the network + :param str uid: The person uid, also known as its uid on the network :param str pubkey: The person pubkey :param cache: The last returned values of the person properties. ''' - self.name = name + self.uid = uid self.pubkey = pubkey self._cache = cache self._cache_mutex = QMutex() @@ -118,9 +118,9 @@ class Person(object): for uid in uids: if uid["meta"]["timestamp"] > timestamp: timestamp = uid["meta"]["timestamp"] - name = uid["uid"] + uid = uid["uid"] - person = cls(name, pubkey, {}) + person = cls(uid, pubkey, {}) Person._instances[pubkey] = person logging.debug("{0}".format(Person._instances.keys())) return person @@ -130,18 +130,18 @@ class Person(object): def from_metadata(cls, metadata): ''' Get a person from a metadata dict. - A metadata dict has a 'text' key corresponding to the person name, + A metadata dict has a 'text' key corresponding to the person uid, and a 'id' key corresponding to the person pubkey. :param dict metadata: The person metadata :return: A new person if pubkey wasn't knwon, else the existing instance. ''' - name = metadata['text'] + uid = metadata['text'] pubkey = metadata['id'] if pubkey in Person._instances: return Person._instances[pubkey] else: - person = cls(name, pubkey, {}) + person = cls(uid, pubkey, {}) Person._instances[pubkey] = person return person @@ -157,13 +157,16 @@ class Person(object): if pubkey in Person._instances: return Person._instances[pubkey] else: - name = json_data['name'] + if 'name' in json_data: + uid = json_data['name'] + else: + uid = json_data['uid'] if 'cache' in json_data: cache = json_data['cache'] else: cache = {} - person = cls(name, pubkey, cache) + person = cls(uid, pubkey, cache) Person._instances[pubkey] = person return person @@ -185,14 +188,14 @@ class Person(object): for uid in uids: if uid["meta"]["timestamp"] > timestamp: timestamp = uid["meta"]["timestamp"] - name = uid["uid"] + uid = uid["uid"] signature = uid["self"] return SelfCertification(PROTOCOL_VERSION, community.currency, self.pubkey, timestamp, - name, + uid, signature) raise PersonNotFoundError(self.pubkey, community.name) @@ -376,7 +379,7 @@ class Person(object): Get the community as dict in json format. :return: The community as a dict in json format ''' - data = {'name': self.name, + data = {'uid': self.uid, 'pubkey': self.pubkey, 'cache': self._cache} return data diff --git a/src/cutecoin/gui/certification.py b/src/cutecoin/gui/certification.py index 1492c6cd..44244501 100644 --- a/src/cutecoin/gui/certification.py +++ b/src/cutecoin/gui/certification.py @@ -28,7 +28,7 @@ class CertificationDialog(QDialog, Ui_CertificationDialog): self.combo_community.addItem(community.currency) for contact in certifier.contacts: - self.combo_contact.addItem(contact.name) + self.combo_contact.addItem(contact['name']) def accept(self): if self.radio_contact.isChecked(): diff --git a/src/cutecoin/gui/member.py b/src/cutecoin/gui/member.py index 36da3fd5..fdfd92ac 100644 --- a/src/cutecoin/gui/member.py +++ b/src/cutecoin/gui/member.py @@ -21,7 +21,7 @@ class MemberDialog(QDialog, Ui_DialogMember): self.community = community self.account = account self.person = person - self.label_uid.setText(person.name) + self.label_uid.setText(person.uid) join_date = self.person.get_join_date(self.community) join_date = datetime.datetime.fromtimestamp(join_date).strftime("%d/%m/%Y %I:%M") diff --git a/src/cutecoin/gui/network_tab.py b/src/cutecoin/gui/network_tab.py index ca32208b..4c851d30 100644 --- a/src/cutecoin/gui/network_tab.py +++ b/src/cutecoin/gui/network_tab.py @@ -49,4 +49,3 @@ class NetworkTabWidget(QWidget, Ui_NetworkTabWidget): def showEvent(self, event): super().showEvent(event) self.watcher_thread.start() - diff --git a/src/cutecoin/models/members.py b/src/cutecoin/models/members.py index fe48c7c2..761c0bac 100644 --- a/src/cutecoin/models/members.py +++ b/src/cutecoin/models/members.py @@ -96,7 +96,7 @@ class MembersTableModel(QAbstractTableModel): join_date = self.community.get_block(join_block).mediantime parameters = self.community.parameters expiration_date = join_date + parameters['sigValidity'] - return (person.name, pubkey, join_date, expiration_date) + return (person.uid, pubkey, join_date, expiration_date) def data(self, index, role): if role == Qt.DisplayRole: diff --git a/src/cutecoin/models/txhistory.py b/src/cutecoin/models/txhistory.py index 8f24a105..0ac70eb9 100644 --- a/src/cutecoin/models/txhistory.py +++ b/src/cutecoin/models/txhistory.py @@ -56,9 +56,9 @@ class TxFilterProxyModel(QSortFilterProxyModel): elif right_data == "": return self.sortOrder() == Qt.AscendingOrder if left_data.__class__ is Person: - left_data = left_data.name + left_data = left_data.uid if right_data.__class__ is Person: - right_data = right_data.name + right_data = right_data.uid return (left_data < right_data) @@ -71,7 +71,7 @@ class TxFilterProxyModel(QSortFilterProxyModel): if role == Qt.DisplayRole: if source_index.column() == self.sourceModel().column_types.index('uid'): if source_data.__class__ == Person: - tx_person = source_data.name + tx_person = source_data.uid else: tx_person = "pub:{0}".format(source_data[:5]) source_data = tx_person @@ -178,10 +178,8 @@ class HistoryTableModel(QAbstractTableModel): comment = transfer.txdoc.comment pubkey = transfer.metadata['issuer'] try: - #sender = Person.lookup(pubkey, self.community).name sender = Person.lookup(pubkey, self.community) except PersonNotFoundError: - #sender = "pub:{0}".format(pubkey[:5]) sender = pubkey date_ts = transfer.metadata['time'] @@ -196,7 +194,6 @@ class HistoryTableModel(QAbstractTableModel): comment = transfer.txdoc.comment pubkey = transfer.metadata['receiver'] try: - #receiver = Person.lookup(pubkey, self.community).name receiver = Person.lookup(pubkey, self.community) except PersonNotFoundError: #receiver = "pub:{0}".format(pubkey[:5]) -- GitLab