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

Stabilized network cache saving and loading

Added debug traces in network crawling
Better handling of saving and loading of network cache
Added "synced_nodes" property
parent 36da49f9
No related branches found
No related tags found
No related merge requests found
...@@ -181,6 +181,7 @@ class Application(QObject): ...@@ -181,6 +181,7 @@ class Application(QObject):
with open(network_path, 'r') as json_data: with open(network_path, 'r') as json_data:
data = json.load(json_data) data = json.load(json_data)
if 'version' in data and data['version'] == __version__: if 'version' in data and data['version'] == __version__:
logging.debug("Merging network : {0}".format(data))
community.load_merge_network(data['network']) community.load_merge_network(data['network'])
else: else:
os.remove(network_path) os.remove(network_path)
......
...@@ -380,7 +380,7 @@ class Community(QObject): ...@@ -380,7 +380,7 @@ class Community(QObject):
if cached: if cached:
return self._cache.request(request, req_args, get_args) return self._cache.request(request, req_args, get_args)
else: else:
nodes = self._network.online_nodes nodes = self._network.synced_nodes
for node in nodes: for node in nodes:
try: try:
req = request(node.endpoint.conn_handler(), **req_args) req = request(node.endpoint.conn_handler(), **req_args)
...@@ -469,6 +469,11 @@ class Community(QObject): ...@@ -469,6 +469,11 @@ class Community(QObject):
:return: The community as a dict in json format. :return: The community as a dict in json format.
''' '''
nodes_data = []
for node in self._network.online_nodes:
nodes_data.append(node.jsonify())
data = {'currency': self.currency, data = {'currency': self.currency,
'peers': self._network.jsonify()} 'peers': nodes_data}
return data return data
\ No newline at end of file
...@@ -84,7 +84,6 @@ class Network(QObject): ...@@ -84,7 +84,6 @@ class Network(QObject):
for data in json_data: for data in json_data:
node = Node.from_json(currency, data) node = Node.from_json(currency, data)
nodes.append(node) nodes.append(node)
logging.debug("Loading : {:}".format(data['pubkey']))
block_max = max([n.block for n in nodes]) block_max = max([n.block for n in nodes])
for node in nodes: for node in nodes:
node.check_sync(block_max) node.check_sync(block_max)
...@@ -114,12 +113,19 @@ class Network(QObject): ...@@ -114,12 +113,19 @@ class Network(QObject):
return True return True
@property @property
def online_nodes(self): def synced_nodes(self):
''' '''
Get nodes which are in the ONLINE state. Get nodes which are in the ONLINE state.
''' '''
return [n for n in self._nodes if n.state == Node.ONLINE] return [n for n in self._nodes if n.state == Node.ONLINE]
@property
def online_nodes(self):
'''
Get nodes which are in the ONLINE state.
'''
return [n for n in self._nodes if n.state in (Node.ONLINE, Node.DESYNCED)]
@property @property
def all_nodes(self): def all_nodes(self):
''' '''
...@@ -178,14 +184,18 @@ class Network(QObject): ...@@ -178,14 +184,18 @@ class Network(QObject):
for node in [n for n in nodes if n.state == Node.ONLINE]: for node in [n for n in nodes if n.state == Node.ONLINE]:
node.check_sync(block_max) node.check_sync(block_max)
for node in self._nodes: for node in nodes:
if node.last_change + 3600 < time.time() and \ if node.last_change + 3600 < time.time() and \
node.state in (Node.OFFLINE, Node.CORRUPTED): node.state in (Node.OFFLINE, Node.CORRUPTED):
try: try:
node.changed.disconnect() node.changed.disconnect()
except TypeError: except TypeError:
logging.debug("Error : {0} not connected".format(node.pubkey))
pass pass
self._nodes.remove(node) nodes.remove(node)
for node in nodes:
logging.debug("Syncing : {0} : last changed {1} : unsynced : {2}".format(node.pubkey[:5],
node.last_change, time.time() - node.last_change))
logging.debug("Nodes found : {0}".format(nodes)) logging.debug("Nodes found : {0}".format(nodes))
return nodes return nodes
...@@ -96,7 +96,8 @@ class Node(QObject): ...@@ -96,7 +96,8 @@ class Node(QObject):
uid = "" uid = ""
pubkey = "" pubkey = ""
last_change = time.time() last_change = time.time()
state = Node.ONLINE
logging.debug(data)
for endpoint_data in data['endpoints']: for endpoint_data in data['endpoints']:
endpoints.append(Endpoint.from_inline(endpoint_data)) endpoints.append(Endpoint.from_inline(endpoint_data))
...@@ -112,8 +113,13 @@ class Node(QObject): ...@@ -112,8 +113,13 @@ class Node(QObject):
if 'last_change' in data: if 'last_change' in data:
last_change = data['last_change'] last_change = data['last_change']
if 'state' in data:
state = data['state']
else:
logging.debug("Error : no state in node")
node = cls(currency, endpoints, uid, pubkey, 0, node = cls(currency, endpoints, uid, pubkey, 0,
Node.ONLINE, last_change) state, last_change)
node.refresh_state() node.refresh_state()
return node return node
...@@ -121,6 +127,7 @@ class Node(QObject): ...@@ -121,6 +127,7 @@ class Node(QObject):
data = {'pubkey': self._pubkey, data = {'pubkey': self._pubkey,
'uid': self._uid, 'uid': self._uid,
'currency': self._currency, 'currency': self._currency,
'state': self._state,
'last_change': self._last_change} 'last_change': self._last_change}
endpoints = [] endpoints = []
for e in self._endpoints: for e in self._endpoints:
...@@ -160,12 +167,21 @@ class Node(QObject): ...@@ -160,12 +167,21 @@ class Node(QObject):
def last_change(self): def last_change(self):
return self._last_change return self._last_change
@last_change.setter
def last_change(self, val):
logging.debug("{:} | Changed state : {:}".format(self.pubkey[:5],
val))
self._last_change = val
def _change_state(self, new_state): def _change_state(self, new_state):
logging.debug("{:} | Last state : {:} / new state : {:}".format(self.pubkey[:5],
self.state, new_state))
if self.state != new_state: if self.state != new_state:
self._last_change = time.time() self.last_change = time.time()
self._state = new_state self._state = new_state
def check_sync(self, block): def check_sync(self, block):
logging.debug("Check sync")
if self._block < block: if self._block < block:
self._change_state(Node.DESYNCED) self._change_state(Node.DESYNCED)
else: else:
...@@ -190,6 +206,7 @@ class Node(QObject): ...@@ -190,6 +206,7 @@ class Node(QObject):
return uid return uid
def refresh_state(self): def refresh_state(self):
logging.debug("Refresh state")
emit_change = False emit_change = False
try: try:
informations = bma.network.Peering(self.endpoint.conn_handler()).get() informations = bma.network.Peering(self.endpoint.conn_handler()).get()
...@@ -270,7 +287,7 @@ class Node(QObject): ...@@ -270,7 +287,7 @@ class Node(QObject):
(node.pubkey not in traversed_pubkeys))) (node.pubkey not in traversed_pubkeys)))
if node.pubkey not in traversed_pubkeys and continue_crawling(): if node.pubkey not in traversed_pubkeys and continue_crawling():
node.peering_traversal(found_nodes, node.peering_traversal(found_nodes,
traversed_pubkeys, interval, continue_crawling()) traversed_pubkeys, interval, continue_crawling)
time.sleep(interval) time.sleep(interval)
except RequestException as e: except RequestException as e:
self._change_state(Node.OFFLINE) self._change_state(Node.OFFLINE)
......
...@@ -28,7 +28,6 @@ class Cache(): ...@@ -28,7 +28,6 @@ class Cache():
def load_from_json(self, data): def load_from_json(self, data):
self._transfers = [] self._transfers = []
logging.debug(data)
data_sent = data['transfers'] data_sent = data['transfers']
for s in data_sent: for s in data_sent:
......
...@@ -32,14 +32,11 @@ class CommunityTabWidget(QWidget, Ui_CommunityTabWidget): ...@@ -32,14 +32,11 @@ class CommunityTabWidget(QWidget, Ui_CommunityTabWidget):
Constructor Constructor
''' '''
super().__init__() super().__init__()
logging.debug("Info")
self.setupUi(self) self.setupUi(self)
self.community = community self.community = community
self.account = account self.account = account
self.password_asker = password_asker self.password_asker = password_asker
logging.debug("Table")
members_model = MembersTableModel(community) members_model = MembersTableModel(community)
logging.debug("Filter")
proxy_members = MembersFilterProxyModel() proxy_members = MembersFilterProxyModel()
proxy_members.setSourceModel(members_model) proxy_members.setSourceModel(members_model)
self.table_community_members.setModel(proxy_members) self.table_community_members.setModel(proxy_members)
......
...@@ -36,26 +36,22 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): ...@@ -36,26 +36,22 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
self.community = community self.community = community
self.password_asker = password_asker self.password_asker = password_asker
self.status_label = status_label self.status_label = status_label
logging.debug("Com")
self.tab_community = CommunityTabWidget(self.app.current_account, self.tab_community = CommunityTabWidget(self.app.current_account,
self.community, self.community,
self.password_asker) self.password_asker)
logging.debug("Wal")
self.tab_wallets = WalletsTabWidget(self.app, self.tab_wallets = WalletsTabWidget(self.app,
self.app.current_account, self.app.current_account,
self.community, self.community,
self.password_asker) self.password_asker)
logging.debug("Net")
self.tab_network = NetworkTabWidget(self.community) self.tab_network = NetworkTabWidget(self.community)
logging.debug("Connect")
self.community.new_block_mined.connect(self.refresh_block) self.community.new_block_mined.connect(self.refresh_block)
persons_watcher = self.app.monitor.persons_watcher(self.community) persons_watcher = self.app.monitor.persons_watcher(self.community)
persons_watcher.person_changed.connect(self.tab_community.refresh_person) persons_watcher.person_changed.connect(self.tab_community.refresh_person)
bc_watcher = self.app.monitor.blockchain_watcher(self.community) bc_watcher = self.app.monitor.blockchain_watcher(self.community)
bc_watcher.error.connect(self.display_error) bc_watcher.error.connect(self.display_error)
logging.debug("Connected")
person = Person.lookup(self.app.current_account.pubkey, self.community) person = Person.lookup(self.app.current_account.pubkey, self.community)
try: try:
......
...@@ -96,7 +96,7 @@ class PeeringTreeModel(QAbstractItemModel): ...@@ -96,7 +96,7 @@ class PeeringTreeModel(QAbstractItemModel):
Constructor Constructor
''' '''
super().__init__(None) super().__init__(None)
self.nodes = community.nodes self.nodes = community.online_nodes
self.root_item = RootItem(community.currency) self.root_item = RootItem(community.currency)
self.refresh_tree() self.refresh_tree()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment