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

Last network image saved and loaded on statup

parent 98eb1036
No related branches found
No related tags found
No related merge requests found
......@@ -100,6 +100,19 @@ class Application(QObject):
community_path = os.path.join(config.parameters['home'],
account.name, '__cache__',
community.currency)
network_path = os.path.join(config.parameters['home'],
account.name, '__cache__',
community.currency + '_network')
if os.path.exists(network_path):
with open(network_path, 'r') as json_data:
data = json.load(json_data)
if 'version' in data and data['version'] == __version__:
community.load_network(data)
else:
os.remove(network_path)
if os.path.exists(community_path):
with open(community_path, 'r') as json_data:
data = json.load(json_data)
......@@ -152,6 +165,16 @@ class Application(QObject):
community_path = os.path.join(config.parameters['home'],
account.name, '__cache__',
community.currency)
network_path = os.path.join(config.parameters['home'],
account.name, '__cache__',
community.currency + '_network')
with open(network_path, 'w') as outfile:
data = community.jsonify_network()
data['version'] = __version__
json.dump(data, outfile, indent=4, sort_keys=True)
with open(community_path, 'w') as outfile:
data = community.jsonify_cache()
data['version'] = __version__
......
......@@ -94,18 +94,22 @@ class Community(object):
@classmethod
def load(cls, json_data):
currency = json_data['currency']
network = Network.from_json(currency, json_data['peers'])
community = cls(currency, network)
return community
def load_network(self, json_data):
self._network.merge_with_json(json_data['network'])
def load_cache(self, json_data):
self._cache.load_from_json(json_data)
def jsonify_cache(self):
return self._cache.jsonify()
def jsonify_network(self):
return {'network': self._network.jsonify()}
def name(self):
return self.currency
......
......@@ -31,10 +31,13 @@ class Network(QObject):
for n in self._nodes:
n.changed.connect(self.nodes_changed)
self.must_crawl = False
#TODO: Crawl nodes at startup
@classmethod
def from_json(cls, currency, json_data):
'''
We load the nodes which we know for sure since we
used them at the community creation
'''
nodes = []
for data in json_data:
node = Node.from_json(currency, data)
......@@ -45,6 +48,17 @@ class Network(QObject):
node.check_sync(block_max)
return cls(currency, nodes)
def merge_with_json(self, json_data):
'''
We merge with dynamic nodes detected when we
last stopped cutecoin
'''
for data in json_data:
node = Node.from_json(self.currency, data)
self._nodes.append(node)
logging.debug("Loading : {:}".format(data['pubkey']))
self._nodes = self.crawling()
@classmethod
def create(cls, node):
nodes = [node]
......
......@@ -50,7 +50,7 @@ class Node(QObject):
if peer.currency != currency:
raise InvalidNodeCurrency(peer.currency, currency)
node = cls(peer.currency, peer.endpoints, peer.pubkey, 0, Node.ONLINE)
node = cls(peer.currency, peer.endpoints, peer.pubkey, 0, Node.ONLINE, 0)
node.refresh_state()
return node
......@@ -137,10 +137,13 @@ class Node(QObject):
self._state = Node.OFFLINE
emit_change = True
# If not is offline, do not refresh last data
if self._state != Node.OFFLINE:
# If not changed its currency, consider it corrupted
if node_currency != self._currency:
self.state = Node.CORRUPTED
emit_change = True
else:
if block_number != self._block:
self._block = block_number
emit_change = True
......@@ -167,9 +170,11 @@ class Node(QObject):
logging.debug("Read {0} peering".format(self.pubkey))
traversed_pubkeys.append(self.pubkey)
self.refresh_state()
if self.pubkey not in [n.pubkey for n in found_nodes]:
# if node is corrupted remove it
if self._state != Node.CORRUPTED:
found_nodes.append(self)
try:
logging.debug(self.neighbours)
for n in self.neighbours:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment