diff --git a/src/cutecoin/core/graph.py b/src/cutecoin/core/graph.py index 191979710d52ddc9425cbd0c27bb8aa469eba9a3..96576a1315c2925116e4f63af2b24fc04383e104 100644 --- a/src/cutecoin/core/graph.py +++ b/src/cutecoin/core/graph.py @@ -18,6 +18,8 @@ class Graph(object): """ self.app = app self.community = community + self.signature_validity = 0 + self.ARC_STATUS_STRONG_time = 0 # graph empty if None parameter self._graph = graph or (dict() and (graph is None)) @@ -157,75 +159,76 @@ class Graph(object): :param identity identity_account: Account identity instance :return: """ - yield from self.refresh_signature_validity() - # Â add certifiers of uid - for certifier in tuple(certifier_list): - # add only valid certification... - if (time.time() - certifier['cert_time']) > self.signature_validity: - continue - # new node - if certifier['identity'].pubkey not in self._graph.keys(): - node_status = 0 - is_member = yield from certifier['identity'].is_member(self.community) - if certifier['identity'].pubkey == identity_account.pubkey: - node_status += NODE_STATUS_HIGHLIGHTED - if is_member is False: - node_status += NODE_STATUS_OUT - self._graph[certifier['identity'].pubkey] = { - 'id': certifier['identity'].pubkey, - 'arcs': list(), - 'text': certifier['identity'].uid, - 'tooltip': certifier['identity'].pubkey, - 'status': node_status, - 'connected': [identity.pubkey] - } - - # keep only the latest certification - if self._graph[certifier['identity'].pubkey]['arcs']: - if certifier['cert_time'] < self._graph[certifier['identity'].pubkey]['arcs'][0]['cert_time']: + if self.community: + yield from self.refresh_signature_validity() + # Â add certifiers of uid + for certifier in tuple(certifier_list): + # add only valid certification... + if (time.time() - certifier['cert_time']) > self.signature_validity: continue - # display validity status - if (time.time() - certifier['cert_time']) > self.ARC_STATUS_STRONG_time: - arc_status = ARC_STATUS_WEAK - else: - arc_status = ARC_STATUS_STRONG + # new node + if certifier['identity'].pubkey not in self._graph.keys(): + node_status = 0 + is_member = yield from certifier['identity'].is_member(self.community) + if certifier['identity'].pubkey == identity_account.pubkey: + node_status += NODE_STATUS_HIGHLIGHTED + if is_member is False: + node_status += NODE_STATUS_OUT + self._graph[certifier['identity'].pubkey] = { + 'id': certifier['identity'].pubkey, + 'arcs': list(), + 'text': certifier['identity'].uid, + 'tooltip': certifier['identity'].pubkey, + 'status': node_status, + 'connected': [identity.pubkey] + } - arc = { - 'id': identity.pubkey, - 'status': arc_status, - 'tooltip': QLocale.toString( - QLocale(), - QDateTime.fromTime_t(certifier['cert_time'] + self.signature_validity).date(), - QLocale.dateFormat(QLocale(), QLocale.ShortFormat) - ), - 'cert_time': certifier['cert_time'] - } + # keep only the latest certification + if self._graph[certifier['identity'].pubkey]['arcs']: + if certifier['cert_time'] < self._graph[certifier['identity'].pubkey]['arcs'][0]['cert_time']: + continue + # display validity status + if (time.time() - certifier['cert_time']) > self.ARC_STATUS_STRONG_time: + arc_status = ARC_STATUS_WEAK + else: + arc_status = ARC_STATUS_STRONG - if certifier['block_number']: - current_validations = self.community.network.latest_block_number - certifier['block_number'] - else: - current_validations = 0 - members_pubkeys = yield from self.community.members_pubkeys() - max_validation = self.community.network.fork_window(members_pubkeys) + 1 + arc = { + 'id': identity.pubkey, + 'status': arc_status, + 'tooltip': QLocale.toString( + QLocale(), + QDateTime.fromTime_t(certifier['cert_time'] + self.signature_validity).date(), + QLocale.dateFormat(QLocale(), QLocale.ShortFormat) + ), + 'cert_time': certifier['cert_time'] + } - # Current validation can be negative if self.community.network.latest_block_number - # is not refreshed yet - if max_validation > current_validations > 0: - if self.app.preferences['expert_mode']: - arc['validation_text'] = "{0}/{1}".format(current_validations, - max_validation) + if certifier['block_number']: + current_validations = self.community.network.latest_block_number - certifier['block_number'] else: - validation = current_validations / max_validation * 100 - arc['validation_text'] = "{0} %".format(QLocale().toString(float(validation), 'f', 0)) - else: - arc['validation_text'] = None + current_validations = 0 + members_pubkeys = yield from self.community.members_pubkeys() + max_validation = self.community.network.fork_window(members_pubkeys) + 1 + + # Current validation can be negative if self.community.network.latest_block_number + # is not refreshed yet + if max_validation > current_validations > 0: + if self.app.preferences['expert_mode']: + arc['validation_text'] = "{0}/{1}".format(current_validations, + max_validation) + else: + validation = current_validations / max_validation * 100 + arc['validation_text'] = "{0} %".format(QLocale().toString(float(validation), 'f', 0)) + else: + arc['validation_text'] = None - # Â add arc to certifier - self._graph[certifier['identity'].pubkey]['arcs'].append(arc) - # if certifier node not in identity nodes - if certifier['identity'].pubkey not in tuple(self._graph[identity.pubkey]['connected']): - #Â add certifier node to identity node - self._graph[identity.pubkey]['connected'].append(certifier['identity'].pubkey) + # Â add arc to certifier + self._graph[certifier['identity'].pubkey]['arcs'].append(arc) + # if certifier node not in identity nodes + if certifier['identity'].pubkey not in tuple(self._graph[identity.pubkey]['connected']): + #Â add certifier node to identity node + self._graph[identity.pubkey]['connected'].append(certifier['identity'].pubkey) @asyncio.coroutine def add_certified_list(self, certified_list, identity, identity_account): diff --git a/src/cutecoin/core/net/api/bma/access.py b/src/cutecoin/core/net/api/bma/access.py index 5c64b38211a10a3f620a8c8b68a4a7b9648a0900..1ee9ab476890087366aa0b3bef1750eb580f6749 100644 --- a/src/cutecoin/core/net/api/bma/access.py +++ b/src/cutecoin/core/net/api/bma/access.py @@ -153,8 +153,9 @@ class BmaAccess(QObject): strdata = bytes(reply.readAll()).decode('utf-8') json_data = json.loads(strdata) self._update_cache(request, req_args, get_args, json_data) - future_data.set_result(json_data) - else: + if not future_data.cancelled(): + future_data.set_result(json_data) + elif not future_data.cancelled(): future_data.set_result(request.null_value) future_data = asyncio.Future() diff --git a/src/cutecoin/core/registry/identities.py b/src/cutecoin/core/registry/identities.py index 4749dc2e086ed9acf910e99a9a7ea9ac25a6c74c..65935c537732ea212d618557649d3c2ad89c3498 100644 --- a/src/cutecoin/core/registry/identities.py +++ b/src/cutecoin/core/registry/identities.py @@ -59,7 +59,8 @@ class IdentitiesRegistry: identity.local_state = LocalState.PARTIAL identity.blockchain_state = BlockchainState.VALIDATED logging.debug("Lookup : found {0}".format(identity)) - future_identity.set_result(True) + if not future_identity.cancelled(): + future_identity.set_result(True) else: reply = community.bma_access.simple_request(qtbma.wot.Lookup, req_args={'search': pubkey}) @@ -67,7 +68,7 @@ class IdentitiesRegistry: elif tries < 3: reply = community.bma_access.simple_request(qtbma.wot.CertifiersOf, req_args={'search': pubkey}) reply.finished.connect(lambda: handle_certifiersof_reply(reply, tries=tries+1)) - else: + elif not future_identity.cancelled(): future_identity.set_result(True) def handle_lookup_reply(reply, tries=0): @@ -89,26 +90,28 @@ class IdentitiesRegistry: identity.blockchain_state = BlockchainState.BUFFERED identity.local_state = LocalState.PARTIAL logging.debug("Lookup : found {0}".format(identity)) - future_identity.set_result(True) + if not future_identity.cancelled(): + future_identity.set_result(identity) return - future_identity.set_result(True) + if not future_identity.cancelled(): + future_identity.set_result(identity) elif tries < 3: reply = community.bma_access.simple_request(qtbma.wot.Lookup, req_args={'search': pubkey}) reply.finished.connect(lambda: handle_lookup_reply(reply, tries=tries+1)) - else: - future_identity.set_result(True) + elif not future_identity.cancelled(): + future_identity.set_result(identity) future_identity = asyncio.Future() if pubkey in self._instances: identity = self._instances[pubkey] - future_identity.set_result(True) + if not future_identity.cancelled(): + future_identity.set_result(identity) else: identity = Identity.empty(pubkey) self._instances[pubkey] = identity reply = community.bma_access.simple_request(qtbma.wot.CertifiersOf, req_args={'search': pubkey}) reply.finished.connect(lambda: handle_certifiersof_reply(reply)) - yield from future_identity - return identity + return future_identity def from_handled_data(self, uid, pubkey, blockchain_state): """ diff --git a/src/cutecoin/gui/wot_tab.py b/src/cutecoin/gui/wot_tab.py index c4f3b80e588ba054401ae5d0d54ced1dbef8c22d..89fbaf5d4e4bcdf59de3140d7ebfdca3611ff0c1 100644 --- a/src/cutecoin/gui/wot_tab.py +++ b/src/cutecoin/gui/wot_tab.py @@ -48,6 +48,7 @@ class WotTabWidget(QWidget, Ui_WotTabWidget): self.community = None self.password_asker = None self.app = app + self.draw_task = None #Â nodes list for menu from search self.nodes = list() @@ -60,6 +61,9 @@ class WotTabWidget(QWidget, Ui_WotTabWidget): self.password_asker = password_asker def change_community(self, community): + if self.draw_task and not self.draw_task.done: + self.draw_task.cancel() + if self.community: self.community.network.new_block_mined.disconnect(self.refresh) if community: @@ -157,9 +161,17 @@ class WotTabWidget(QWidget, Ui_WotTabWidget): ) ) - @asyncify - @asyncio.coroutine def draw_graph(self, identity): + if self.draw_task and not self.draw_task.done(): + self.draw_task.cancel() + + try: + self.draw_task = asyncio.async(self.cor_draw_graph(identity)) + except asyncio.CancelledError: + logging.debug("Cancelled drawing task") + + @asyncio.coroutine + def cor_draw_graph(self, identity): """ Draw community graph centered on the identity