From 523f7c3b9cb433d176e100b2e70c3a0992004b89 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Fri, 6 Mar 2015 06:52:21 +0100
Subject: [PATCH] Last network image saved and loaded on statup

---
 src/cutecoin/core/app.py         | 23 ++++++++++++++
 src/cutecoin/core/community.py   |  8 +++--
 src/cutecoin/core/net/network.py | 16 +++++++++-
 src/cutecoin/core/net/node.py    | 53 +++++++++++++++++---------------
 4 files changed, 73 insertions(+), 27 deletions(-)

diff --git a/src/cutecoin/core/app.py b/src/cutecoin/core/app.py
index 33e853db..87e57836 100644
--- a/src/cutecoin/core/app.py
+++ b/src/cutecoin/core/app.py
@@ -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__
diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py
index 12def58c..c0999f36 100644
--- a/src/cutecoin/core/community.py
+++ b/src/cutecoin/core/community.py
@@ -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
 
diff --git a/src/cutecoin/core/net/network.py b/src/cutecoin/core/net/network.py
index 68a41729..af0c247a 100644
--- a/src/cutecoin/core/net/network.py
+++ b/src/cutecoin/core/net/network.py
@@ -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]
diff --git a/src/cutecoin/core/net/node.py b/src/cutecoin/core/net/node.py
index 5c84c5f0..b7e1366e 100644
--- a/src/cutecoin/core/net/node.py
+++ b/src/cutecoin/core/net/node.py
@@ -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,27 +137,30 @@ class Node(QObject):
             self._state = Node.OFFLINE
             emit_change = True
 
-        if node_currency != self._currency:
-            self.state = Node.CORRUPTED
-            emit_change = True
-
-        if block_number != self._block:
-            self._block = block_number
-            emit_change = True
-
-        if node_pubkey != self._pubkey:
-            self._pubkey = node_pubkey
-            emit_change = True
-
-        logging.debug(neighbours)
-        new_inlines = [e.inline() for n in neighbours for e in n]
-        last_inlines = [e.inline() for n in self._neighbours for e in n]
-
-        hash_new_neighbours = hash(tuple(frozenset(sorted(new_inlines))))
-        hash_last_neighbours = hash(tuple(frozenset(sorted(last_inlines))))
-        if hash_new_neighbours != hash_last_neighbours:
-            self._neighbours = neighbours
-            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
+
+                if node_pubkey != self._pubkey:
+                    self._pubkey = node_pubkey
+                    emit_change = True
+
+                logging.debug(neighbours)
+                new_inlines = [e.inline() for n in neighbours for e in n]
+                last_inlines = [e.inline() for n in self._neighbours for e in n]
+
+                hash_new_neighbours = hash(tuple(frozenset(sorted(new_inlines))))
+                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:
             self.changed.emit()
@@ -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]:
-            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:
             logging.debug(self.neighbours)
             for n in self.neighbours:
-- 
GitLab