diff --git a/src/sakia/core/net/network.py b/src/sakia/core/net/network.py index 0681bfcaed04c7a75135d23a6f93b14721ceb0f3..21892a6d1512481736ee41235d0bdf0febb029f5 100644 --- a/src/sakia/core/net/network.py +++ b/src/sakia/core/net/network.py @@ -9,8 +9,7 @@ import logging import aiohttp import time import asyncio -from ucoinpy.documents.peer import Peer -from ucoinpy.documents.block import Block, BlockUID +from ucoinpy.documents import Peer, Block, BlockUID, MalformedDocumentError from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QTimer from collections import Counter @@ -67,26 +66,29 @@ class Network(QObject): :param NormalizedVersion file_version: The node version """ for data in json_data: - node = Node.from_json(self.currency, data, file_version) - if node.pubkey not in [n.pubkey for n in self.nodes]: - self.add_node(node) - logging.debug("Loading : {:}".format(data['pubkey'])) - else: - other_node = [n for n in self.nodes if n.pubkey == node.pubkey][0] - other_node._uid = node.uid - other_node._version = node.version - other_node._software = node.software - other_node._peer = node.peer - switch = False - if other_node.block and node.block: - if other_node.block['hash'] != node.block['hash']: - switch = True + try: + node = Node.from_json(self.currency, data, file_version, self.session) + if node.pubkey not in [n.pubkey for n in self.nodes]: + self.add_node(node) + logging.debug("Loading : {:}".format(data['pubkey'])) else: - switch = True - if switch: - other_node.set_block(node.block) - other_node.last_change = node.last_change - other_node.state = node.state + other_node = [n for n in self.nodes if n.pubkey == node.pubkey][0] + other_node._uid = node.uid + other_node._version = node.version + other_node._software = node.software + other_node._peer = node.peer + switch = False + if other_node.block and node.block: + if other_node.block['hash'] != node.block['hash']: + switch = True + else: + switch = True + if switch: + other_node.set_block(node.block) + other_node.last_change = node.last_change + other_node.state = node.state + except MalformedDocumentError: + logging.debug("Could not load node {0}".format(data)) @classmethod def from_json(cls, currency, json_data, file_version): @@ -100,8 +102,11 @@ class Network(QObject): session = aiohttp.ClientSession() nodes = [] for data in json_data: - node = Node.from_json(currency, data, file_version, session) - nodes.append(node) + try: + node = Node.from_json(currency, data, file_version, session) + nodes.append(node) + except MalformedDocumentError: + logging.debug("Could not load node {0}".format(data)) network = cls(currency, nodes, session) return network @@ -123,7 +128,10 @@ class Network(QObject): """ synced = len(self.synced_nodes) total = len(self.nodes) - ratio_synced = synced / total + if total == 0: + ratio_synced = 0 + else: + ratio_synced = synced / total return ratio_synced def start_coroutines(self): @@ -141,7 +149,8 @@ class Network(QObject): close_tasks = [] for node in self.nodes: close_tasks.append(asyncio.ensure_future(node.close_ws())) - await asyncio.wait(close_tasks, timeout=15) + if len(close_tasks) > 0: + await asyncio.wait(close_tasks, timeout=15) await self._client_session.close() logging.debug("Closed") @@ -336,6 +345,7 @@ class Network(QObject): if not first_loop: await asyncio.sleep(15) first_loop = False + await asyncio.sleep(15) logging.debug("End of network discovery") diff --git a/src/sakia/core/registry/identities.py b/src/sakia/core/registry/identities.py index ff235e818587a791a3e13f542cb1abd9abdd8cdb..b0125279744dfad71127a8bc2fcf10de711e7343 100644 --- a/src/sakia/core/registry/identities.py +++ b/src/sakia/core/registry/identities.py @@ -1,7 +1,7 @@ from ucoinpy.api import bma from ucoinpy.documents import BlockUID from .identity import Identity, LocalState, BlockchainState - +from pkg_resources import parse_version import asyncio from aiohttp.errors import ClientError from ...tools.exceptions import NoPeerAvailable @@ -28,12 +28,13 @@ class IdentitiesRegistry: :param dict json_data: The identities in json format """ instances = {} + version = parse_version(json_data['version']) for currency in json_data['registry']: instances[currency] = {} for person_data in json_data['registry'][currency]: pubkey = person_data['pubkey'] if pubkey not in instances: - person = Identity.from_json(person_data) + person = Identity.from_json(person_data, version) instances[currency][person.pubkey] = person self._instances = instances diff --git a/src/sakia/core/registry/identity.py b/src/sakia/core/registry/identity.py index 427af5ead3eae4429888b7245090a4b5cc9f6bd1..2d4ab03dea64e95227006e96827654bed1220ff2 100644 --- a/src/sakia/core/registry/identity.py +++ b/src/sakia/core/registry/identity.py @@ -7,6 +7,7 @@ Created on 11 févr. 2014 import logging import time from enum import Enum +from pkg_resources import parse_version from ucoinpy.documents import BlockUID, SelfCertification, MalformedDocumentError from ucoinpy.api import bma as bma @@ -76,7 +77,7 @@ class Identity(QObject): return cls(uid, pubkey, sigdate, LocalState.COMPLETED, blockchain_state) @classmethod - def from_json(cls, json_data): + def from_json(cls, json_data, version): """ Create a person from json data @@ -85,9 +86,12 @@ class Identity(QObject): """ pubkey = json_data['pubkey'] uid = json_data['uid'] - sigdate = BlockUID.from_str(json_data['sigdate']) local_state = LocalState[json_data['local_state']] blockchain_state = BlockchainState[json_data['blockchain_state']] + if version < parse_version("0.20.0dev0"): + sigdate = BlockUID.empty() + else: + sigdate = BlockUID.from_str(json_data['sigdate']) return cls(uid, pubkey, sigdate, local_state, blockchain_state) @@ -512,7 +516,7 @@ class Identity(QObject): """ data = {'uid': self.uid, 'pubkey': self.pubkey, - 'sigdate': self._sigdate, + 'sigdate': str(self._sigdate), 'local_state': self.local_state.name, 'blockchain_state': self.blockchain_state.name} return data diff --git a/src/sakia/core/txhistory.py b/src/sakia/core/txhistory.py index 3fadf80aac0c349ededaddcceabc9d0ab7fe41f0..71598a8b877b31fbb2bcf619005e283e2c066c45 100644 --- a/src/sakia/core/txhistory.py +++ b/src/sakia/core/txhistory.py @@ -28,12 +28,19 @@ class TxHistory(): def latest_block(self, value): self._latest_block = value - def load_from_json(self, data): + def load_from_json(self, data, version): + """ + Load the tx history cache from json data + + :param dict data: the data + :param version: the version parsed with pkg_resources.parse_version + :return: + """ self._transfers = [] data_sent = data['transfers'] for s in data_sent: - self._transfers.append(Transfer.load(s)) + self._transfers.append(Transfer.load(s, version)) for s in data['sources']: self.available_sources.append(InputSource.from_inline(s['inline'])) diff --git a/src/sakia/core/wallet.py b/src/sakia/core/wallet.py index 222716d8602b31a01945c17589380877c63c1fb2..86e7ce77f73feb15ad553546bcb08374175e4220 100644 --- a/src/sakia/core/wallet.py +++ b/src/sakia/core/wallet.py @@ -14,6 +14,7 @@ from ..tools.exceptions import NotEnoughMoneyError, NoPeerAvailable, LookupFailu from .transfer import Transfer from .txhistory import TxHistory +from pkg_resources import parse_version from PyQt5.QtCore import QObject, pyqtSignal import logging @@ -78,10 +79,12 @@ class Wallet(QObject): :param dict json_data: The caches as a dict in json format """ + version = parse_version(json_data['version']) for currency in json_data: if currency != 'version': self.caches[currency] = TxHistory(app, self) - self.caches[currency].load_from_json(json_data[currency]) + if version >= parse_version("0.20.dev0"): + self.caches[currency].load_from_json(json_data[currency], version) def jsonify_caches(self): """