diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py index eaac46e4b43642e83e91f5dab9f3f65cbcbeb261..e3e3a4f8e13dbfeef22e6a1445ab5f8844be41d7 100644 --- a/src/cutecoin/core/community.py +++ b/src/cutecoin/core/community.py @@ -14,6 +14,7 @@ import logging import inspect import hashlib import re +import time from requests.exceptions import RequestException @@ -103,7 +104,7 @@ class Community(QObject): but nothing exists in ucoin to assert that a currency name is unique. ''' - new_block_mined = pyqtSignal() + new_block_mined = pyqtSignal(int) def __init__(self, currency, network): ''' @@ -299,10 +300,16 @@ class Community(QObject): @property def parameters(self): ''' - + Return community parameters in bma format ''' return self.request(bma.blockchain.Parameters) + def certification_expired(self, certtime): + ''' + Return True if the certificaton time is too old + ''' + return time.time() - certtime > self.parameters['sigValidity'] + @property def add_peer(self, peer): ''' diff --git a/src/cutecoin/core/person.py b/src/cutecoin/core/person.py index c907d3df6bc7c9f955575b081fb3fc061321f771..f11e87b66d912679ba1ff57c84b48c04ba3f90ce 100644 --- a/src/cutecoin/core/person.py +++ b/src/cutecoin/core/person.py @@ -303,6 +303,25 @@ class Person(object): return certifiers['certifications'] + def unique_valid_certifiers_of(self, community): + certifier_list = self.certifiers_of(community) + unique_valid = [] + # Â add certifiers of uid + for certifier in tuple(certifier_list): + # add only valid certification... + if community.certification_expired(certifier['cert_time']['medianTime']): + continue + + # keep only the latest certification + already_found = [c['pubkey'] for c in unique_valid] + if certifier['pubkey'] in already_found: + index = already_found.index(certifier['pubkey']) + if certifier['cert_time']['medianTime'] > unique_valid[index]['cert_time']['medianTime']: + unique_valid[index] = certifier + else: + unique_valid.append(certifier) + return unique_valid + @cached def certified_by(self, community): ''' @@ -335,6 +354,25 @@ class Person(object): return certified_list['certifications'] + def unique_valid_certified_by(self, community): + certified_list = self.certified_by(community) + unique_valid = [] + # Â add certifiers of uid + for certified in tuple(certified_list): + # add only valid certification... + if community.certification_expired(certified['cert_time']['medianTime']): + continue + + # keep only the latest certification + already_found = [c['pubkey'] for c in unique_valid] + if certified['pubkey'] in already_found: + index = already_found.index(certified['pubkey']) + if certified['cert_time']['medianTime'] > unique_valid[index]['cert_time']['medianTime']: + unique_valid[index] = certified + else: + unique_valid.append(certified) + return unique_valid + def reload(self, func, community): ''' Reload a cached property of this person in a community. diff --git a/src/cutecoin/core/watching/monitor.py b/src/cutecoin/core/watching/monitor.py index 92925342309c0860a592ec4114dda7d69053064b..866453da004f7db59595f1dec05bad76591771f2 100644 --- a/src/cutecoin/core/watching/monitor.py +++ b/src/cutecoin/core/watching/monitor.py @@ -4,10 +4,11 @@ Created on 18 mars 2015 @author: inso ''' -from PyQt5.QtCore import QThread +from PyQt5.QtCore import QThread, Qt from .blockchain import BlockchainWatcher from .persons import PersonsWatcher from .network import NetworkWatcher +import logging class Monitor(object): @@ -34,14 +35,18 @@ class Monitor(object): def persons_watcher(self, community): return self._persons_watchers[community.name] + def restart_persons_watching(self, community): + watcher = self.persons_watcher(community) + thread = watcher.thread() + logging.debug("Persons watching thread is : {0}".format(thread.isFinished())) + thread.start() + def connect_watcher_to_thread(self, watcher): thread = QThread() watcher.moveToThread(thread) thread.started.connect(watcher.watch) - watcher.watching_stopped.connect(thread.exit) - thread.finished.connect(lambda: self.threads_pool.remove(thread)) - thread.finished.connect(watcher.deleteLater) - thread.finished.connect(thread.deleteLater) + success = watcher.watching_stopped.connect(thread.exit, Qt.DirectConnection) + self.threads_pool.append(thread) def prepare_watching(self): @@ -65,9 +70,18 @@ class Monitor(object): def stop_watching(self): for watcher in self._persons_watchers.values(): watcher.stop() + self.threads_pool.remove(watcher.thread()) + watcher.deleteLater() + watcher.thread().deleteLater() for watcher in self._blockchain_watchers.values(): watcher.stop() + self.threads_pool.remove(watcher.thread()) + watcher.deleteLater() + watcher.thread().deleteLater() for watcher in self._network_watchers.values(): watcher.stop() + self.threads_pool.remove(watcher.thread()) + watcher.deleteLater() + watcher.thread().deleteLater() diff --git a/src/cutecoin/core/watching/persons.py b/src/cutecoin/core/watching/persons.py index 8a0bd815f3a2297ec0c2d22da223aa00faa7c029..5d8214cf1dd8eeff1cf36e6a387fbd6fedc2ad0f 100644 --- a/src/cutecoin/core/watching/persons.py +++ b/src/cutecoin/core/watching/persons.py @@ -35,6 +35,7 @@ class PersonsWatcher(Watcher): logging.debug("Change detected on {0} about {1}".format(p.pubkey, func.__name__)) self.person_changed.emit(p.pubkey) + logging.debug("Finished watching persons") self.watching_stopped.emit() def stop(self): diff --git a/src/cutecoin/gui/currency_tab.py b/src/cutecoin/gui/currency_tab.py index e1523c87f0061e5480223b730059ea37344183f3..a558d0346d4f396242cdaebb3f9bcf9f908bc130 100644 --- a/src/cutecoin/gui/currency_tab.py +++ b/src/cutecoin/gui/currency_tab.py @@ -166,8 +166,7 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): QModelIndex(), QModelIndex(), []) - - self.persons_watcher_thread.start() + self.app.monitor.restart_persons_watching(self.community) text = "Connected : Block {0}".format(block_number) self.status_label.setText(text) diff --git a/src/cutecoin/gui/wallets_tab.py b/src/cutecoin/gui/wallets_tab.py index b9e286337f76c97f92d132c520a6b0e662ec1dd5..acacad17f3fa61783a8f4d8f26409d33e329e5ba 100644 --- a/src/cutecoin/gui/wallets_tab.py +++ b/src/cutecoin/gui/wallets_tab.py @@ -44,8 +44,8 @@ class WalletsTabWidget(QWidget, Ui_WalletsTab): try: person = Person.lookup(self.account.pubkey, self.community) membership = person.membership(self.community) - certified = person.certified_by(self.community) - certifiers = person.certifiers_of(self.community) + certified = person.unique_valid_certified_by(self.community) + certifiers = person.unique_valid_certifiers_of(self.community) renew_block = membership['blockNumber'] last_renewal = self.community.get_block(renew_block).mediantime @@ -54,7 +54,6 @@ class WalletsTabWidget(QWidget, Ui_WalletsTab): pass date_renewal = QDateTime.fromTime_t(last_renewal).date().toString() date_expiration = QDateTime.fromTime_t(expiration).date().toString() - # set infos in label self.label_general.setText( """ @@ -68,8 +67,8 @@ class WalletsTabWidget(QWidget, Ui_WalletsTab): "Membership", "Last renewal on {:}, expiration on {:}".format(date_renewal, date_expiration), "Your web of trust :", - "Certified by : {0} members; Certifier of : {1} members".format(len(certified), - len(certifiers)) + "Certified by : {0} members; Certifier of : {1} members".format(len(certifiers), + len(certified)) ) ) diff --git a/src/cutecoin/models/wallets.py b/src/cutecoin/models/wallets.py index 8cc9614dfd0e942e0ee2be5b34d1a0455dda50ed..7dc33409ae35376a2111a6de4f6e862b5f405d1c 100644 --- a/src/cutecoin/models/wallets.py +++ b/src/cutecoin/models/wallets.py @@ -63,6 +63,9 @@ class WalletsTableModel(QAbstractTableModel): super().__init__(parent) self.account = account self.community = community + self.columns_texts = {'name': 'Name', + 'pubkey': 'Pubkey', + 'amount': 'Amount'} self.columns_types = ('name', 'pubkey', 'amount') @property @@ -77,7 +80,8 @@ class WalletsTableModel(QAbstractTableModel): def headerData(self, section, orientation, role): if role == Qt.DisplayRole: - return self.columns_types[section] + col_type = self.columns_types[section] + return self.columns_texts[col_type] def wallet_data(self, row): name = self.wallets[row].name