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): ...@@ -100,6 +100,19 @@ class Application(QObject):
community_path = os.path.join(config.parameters['home'], community_path = os.path.join(config.parameters['home'],
account.name, '__cache__', account.name, '__cache__',
community.currency) 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): if os.path.exists(community_path):
with open(community_path, 'r') as json_data: with open(community_path, 'r') as json_data:
data = json.load(json_data) data = json.load(json_data)
...@@ -152,6 +165,16 @@ class Application(QObject): ...@@ -152,6 +165,16 @@ class Application(QObject):
community_path = os.path.join(config.parameters['home'], community_path = os.path.join(config.parameters['home'],
account.name, '__cache__', account.name, '__cache__',
community.currency) 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: with open(community_path, 'w') as outfile:
data = community.jsonify_cache() data = community.jsonify_cache()
data['version'] = __version__ data['version'] = __version__
......
...@@ -94,18 +94,22 @@ class Community(object): ...@@ -94,18 +94,22 @@ class Community(object):
@classmethod @classmethod
def load(cls, json_data): def load(cls, json_data):
currency = json_data['currency'] currency = json_data['currency']
network = Network.from_json(currency, json_data['peers']) network = Network.from_json(currency, json_data['peers'])
community = cls(currency, network) community = cls(currency, network)
return community return community
def load_network(self, json_data):
self._network.merge_with_json(json_data['network'])
def load_cache(self, json_data): def load_cache(self, json_data):
self._cache.load_from_json(json_data) self._cache.load_from_json(json_data)
def jsonify_cache(self): def jsonify_cache(self):
return self._cache.jsonify() return self._cache.jsonify()
def jsonify_network(self):
return {'network': self._network.jsonify()}
def name(self): def name(self):
return self.currency return self.currency
......
...@@ -31,10 +31,13 @@ class Network(QObject): ...@@ -31,10 +31,13 @@ class Network(QObject):
for n in self._nodes: for n in self._nodes:
n.changed.connect(self.nodes_changed) n.changed.connect(self.nodes_changed)
self.must_crawl = False self.must_crawl = False
#TODO: Crawl nodes at startup
@classmethod @classmethod
def from_json(cls, currency, json_data): 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 = [] nodes = []
for data in json_data: for data in json_data:
node = Node.from_json(currency, data) node = Node.from_json(currency, data)
...@@ -45,6 +48,17 @@ class Network(QObject): ...@@ -45,6 +48,17 @@ class Network(QObject):
node.check_sync(block_max) node.check_sync(block_max)
return cls(currency, nodes) 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 @classmethod
def create(cls, node): def create(cls, node):
nodes = [node] nodes = [node]
......
...@@ -50,7 +50,7 @@ class Node(QObject): ...@@ -50,7 +50,7 @@ class Node(QObject):
if peer.currency != currency: if peer.currency != currency:
raise InvalidNodeCurrency(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() node.refresh_state()
return node return node
...@@ -137,27 +137,30 @@ class Node(QObject): ...@@ -137,27 +137,30 @@ class Node(QObject):
self._state = Node.OFFLINE self._state = Node.OFFLINE
emit_change = True emit_change = True
if node_currency != self._currency: # If not is offline, do not refresh last data
self.state = Node.CORRUPTED if self._state != Node.OFFLINE:
emit_change = True # If not changed its currency, consider it corrupted
if node_currency != self._currency:
if block_number != self._block: self.state = Node.CORRUPTED
self._block = block_number emit_change = True
emit_change = True else:
if block_number != self._block:
if node_pubkey != self._pubkey: self._block = block_number
self._pubkey = node_pubkey emit_change = True
emit_change = True
if node_pubkey != self._pubkey:
logging.debug(neighbours) self._pubkey = node_pubkey
new_inlines = [e.inline() for n in neighbours for e in n] emit_change = True
last_inlines = [e.inline() for n in self._neighbours for e in n]
logging.debug(neighbours)
hash_new_neighbours = hash(tuple(frozenset(sorted(new_inlines)))) new_inlines = [e.inline() for n in neighbours for e in n]
hash_last_neighbours = hash(tuple(frozenset(sorted(last_inlines)))) last_inlines = [e.inline() for n in self._neighbours for e in n]
if hash_new_neighbours != hash_last_neighbours:
self._neighbours = neighbours hash_new_neighbours = hash(tuple(frozenset(sorted(new_inlines))))
emit_change = True hash_last_neighbours = hash(tuple(frozenset(sorted(last_inlines))))
if hash_new_neighbours != hash_last_neighbours:
self._neighbours = neighbours
emit_change = True
if emit_change: if emit_change:
self.changed.emit() self.changed.emit()
...@@ -167,9 +170,11 @@ class Node(QObject): ...@@ -167,9 +170,11 @@ class Node(QObject):
logging.debug("Read {0} peering".format(self.pubkey)) logging.debug("Read {0} peering".format(self.pubkey))
traversed_pubkeys.append(self.pubkey) traversed_pubkeys.append(self.pubkey)
self.refresh_state() self.refresh_state()
if self.pubkey not in [n.pubkey for n in found_nodes]:
found_nodes.append(self)
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: try:
logging.debug(self.neighbours) logging.debug(self.neighbours)
for n in self.neighbours: for n in self.neighbours:
......
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