Skip to content
Snippets Groups Projects
Commit 09c9e4a6 authored by inso's avatar inso
Browse files

Start sakia with files version 0.12

parent 6f401a18
No related branches found
No related tags found
No related merge requests found
...@@ -9,8 +9,7 @@ import logging ...@@ -9,8 +9,7 @@ import logging
import aiohttp import aiohttp
import time import time
import asyncio import asyncio
from ucoinpy.documents.peer import Peer from ucoinpy.documents import Peer, Block, BlockUID, MalformedDocumentError
from ucoinpy.documents.block import Block, BlockUID
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QTimer from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QTimer
from collections import Counter from collections import Counter
...@@ -67,7 +66,8 @@ class Network(QObject): ...@@ -67,7 +66,8 @@ class Network(QObject):
:param NormalizedVersion file_version: The node version :param NormalizedVersion file_version: The node version
""" """
for data in json_data: for data in json_data:
node = Node.from_json(self.currency, data, file_version) try:
node = Node.from_json(self.currency, data, file_version, self.session)
if node.pubkey not in [n.pubkey for n in self.nodes]: if node.pubkey not in [n.pubkey for n in self.nodes]:
self.add_node(node) self.add_node(node)
logging.debug("Loading : {:}".format(data['pubkey'])) logging.debug("Loading : {:}".format(data['pubkey']))
...@@ -87,6 +87,8 @@ class Network(QObject): ...@@ -87,6 +87,8 @@ class Network(QObject):
other_node.set_block(node.block) other_node.set_block(node.block)
other_node.last_change = node.last_change other_node.last_change = node.last_change
other_node.state = node.state other_node.state = node.state
except MalformedDocumentError:
logging.debug("Could not load node {0}".format(data))
@classmethod @classmethod
def from_json(cls, currency, json_data, file_version): def from_json(cls, currency, json_data, file_version):
...@@ -100,8 +102,11 @@ class Network(QObject): ...@@ -100,8 +102,11 @@ class Network(QObject):
session = aiohttp.ClientSession() session = aiohttp.ClientSession()
nodes = [] nodes = []
for data in json_data: for data in json_data:
try:
node = Node.from_json(currency, data, file_version, session) node = Node.from_json(currency, data, file_version, session)
nodes.append(node) nodes.append(node)
except MalformedDocumentError:
logging.debug("Could not load node {0}".format(data))
network = cls(currency, nodes, session) network = cls(currency, nodes, session)
return network return network
...@@ -123,6 +128,9 @@ class Network(QObject): ...@@ -123,6 +128,9 @@ class Network(QObject):
""" """
synced = len(self.synced_nodes) synced = len(self.synced_nodes)
total = len(self.nodes) total = len(self.nodes)
if total == 0:
ratio_synced = 0
else:
ratio_synced = synced / total ratio_synced = synced / total
return ratio_synced return ratio_synced
...@@ -141,6 +149,7 @@ class Network(QObject): ...@@ -141,6 +149,7 @@ class Network(QObject):
close_tasks = [] close_tasks = []
for node in self.nodes: for node in self.nodes:
close_tasks.append(asyncio.ensure_future(node.close_ws())) close_tasks.append(asyncio.ensure_future(node.close_ws()))
if len(close_tasks) > 0:
await asyncio.wait(close_tasks, timeout=15) await asyncio.wait(close_tasks, timeout=15)
await self._client_session.close() await self._client_session.close()
logging.debug("Closed") logging.debug("Closed")
...@@ -336,6 +345,7 @@ class Network(QObject): ...@@ -336,6 +345,7 @@ class Network(QObject):
if not first_loop: if not first_loop:
await asyncio.sleep(15) await asyncio.sleep(15)
first_loop = False first_loop = False
await asyncio.sleep(15)
logging.debug("End of network discovery") logging.debug("End of network discovery")
......
from ucoinpy.api import bma from ucoinpy.api import bma
from ucoinpy.documents import BlockUID from ucoinpy.documents import BlockUID
from .identity import Identity, LocalState, BlockchainState from .identity import Identity, LocalState, BlockchainState
from pkg_resources import parse_version
import asyncio import asyncio
from aiohttp.errors import ClientError from aiohttp.errors import ClientError
from ...tools.exceptions import NoPeerAvailable from ...tools.exceptions import NoPeerAvailable
...@@ -28,12 +28,13 @@ class IdentitiesRegistry: ...@@ -28,12 +28,13 @@ class IdentitiesRegistry:
:param dict json_data: The identities in json format :param dict json_data: The identities in json format
""" """
instances = {} instances = {}
version = parse_version(json_data['version'])
for currency in json_data['registry']: for currency in json_data['registry']:
instances[currency] = {} instances[currency] = {}
for person_data in json_data['registry'][currency]: for person_data in json_data['registry'][currency]:
pubkey = person_data['pubkey'] pubkey = person_data['pubkey']
if pubkey not in instances: if pubkey not in instances:
person = Identity.from_json(person_data) person = Identity.from_json(person_data, version)
instances[currency][person.pubkey] = person instances[currency][person.pubkey] = person
self._instances = instances self._instances = instances
......
...@@ -7,6 +7,7 @@ Created on 11 févr. 2014 ...@@ -7,6 +7,7 @@ Created on 11 févr. 2014
import logging import logging
import time import time
from enum import Enum from enum import Enum
from pkg_resources import parse_version
from ucoinpy.documents import BlockUID, SelfCertification, MalformedDocumentError from ucoinpy.documents import BlockUID, SelfCertification, MalformedDocumentError
from ucoinpy.api import bma as bma from ucoinpy.api import bma as bma
...@@ -76,7 +77,7 @@ class Identity(QObject): ...@@ -76,7 +77,7 @@ class Identity(QObject):
return cls(uid, pubkey, sigdate, LocalState.COMPLETED, blockchain_state) return cls(uid, pubkey, sigdate, LocalState.COMPLETED, blockchain_state)
@classmethod @classmethod
def from_json(cls, json_data): def from_json(cls, json_data, version):
""" """
Create a person from json data Create a person from json data
...@@ -85,9 +86,12 @@ class Identity(QObject): ...@@ -85,9 +86,12 @@ class Identity(QObject):
""" """
pubkey = json_data['pubkey'] pubkey = json_data['pubkey']
uid = json_data['uid'] uid = json_data['uid']
sigdate = BlockUID.from_str(json_data['sigdate'])
local_state = LocalState[json_data['local_state']] local_state = LocalState[json_data['local_state']]
blockchain_state = BlockchainState[json_data['blockchain_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) return cls(uid, pubkey, sigdate, local_state, blockchain_state)
...@@ -512,7 +516,7 @@ class Identity(QObject): ...@@ -512,7 +516,7 @@ class Identity(QObject):
""" """
data = {'uid': self.uid, data = {'uid': self.uid,
'pubkey': self.pubkey, 'pubkey': self.pubkey,
'sigdate': self._sigdate, 'sigdate': str(self._sigdate),
'local_state': self.local_state.name, 'local_state': self.local_state.name,
'blockchain_state': self.blockchain_state.name} 'blockchain_state': self.blockchain_state.name}
return data return data
......
...@@ -28,12 +28,19 @@ class TxHistory(): ...@@ -28,12 +28,19 @@ class TxHistory():
def latest_block(self, value): def latest_block(self, value):
self._latest_block = 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 = [] self._transfers = []
data_sent = data['transfers'] data_sent = data['transfers']
for s in data_sent: for s in data_sent:
self._transfers.append(Transfer.load(s)) self._transfers.append(Transfer.load(s, version))
for s in data['sources']: for s in data['sources']:
self.available_sources.append(InputSource.from_inline(s['inline'])) self.available_sources.append(InputSource.from_inline(s['inline']))
......
...@@ -14,6 +14,7 @@ from ..tools.exceptions import NotEnoughMoneyError, NoPeerAvailable, LookupFailu ...@@ -14,6 +14,7 @@ from ..tools.exceptions import NotEnoughMoneyError, NoPeerAvailable, LookupFailu
from .transfer import Transfer from .transfer import Transfer
from .txhistory import TxHistory from .txhistory import TxHistory
from pkg_resources import parse_version
from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtCore import QObject, pyqtSignal
import logging import logging
...@@ -78,10 +79,12 @@ class Wallet(QObject): ...@@ -78,10 +79,12 @@ class Wallet(QObject):
:param dict json_data: The caches as a dict in json format :param dict json_data: The caches as a dict in json format
""" """
version = parse_version(json_data['version'])
for currency in json_data: for currency in json_data:
if currency != 'version': if currency != 'version':
self.caches[currency] = TxHistory(app, self) 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): def jsonify_caches(self):
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment