From 96c5614c544bdc8a62f606e8ab3f1cb591d64db5 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Fri, 17 Apr 2015 06:53:15 +0200
Subject: [PATCH] Not loading nodes content on startup -> faster starting

---
 src/cutecoin/core/community.py   |  2 +-
 src/cutecoin/core/net/network.py | 13 +++++++------
 src/cutecoin/core/net/node.py    | 23 ++++++++++++++++++-----
 src/cutecoin/gui/contact.py      |  2 ++
 4 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py
index 7a7d3fd6..5325475d 100644
--- a/src/cutecoin/core/community.py
+++ b/src/cutecoin/core/community.py
@@ -478,7 +478,7 @@ class Community(QObject):
 
         nodes_data = []
         for node in self._network.root_nodes:
-            nodes_data.append(node.jsonify())
+            nodes_data.append(node.jsonify_root_node())
 
         data = {'currency': self.currency,
                 'peers': nodes_data}
diff --git a/src/cutecoin/core/net/network.py b/src/cutecoin/core/net/network.py
index 7d3cc303..b33dd097 100644
--- a/src/cutecoin/core/net/network.py
+++ b/src/cutecoin/core/net/network.py
@@ -26,7 +26,7 @@ class Network(Watcher):
         Constructor of a network
 
         :param str currency: The currency name of the community
-        :param list nodes: The nodes of the network
+        :param list nodes: The root nodes of the network
         '''
         super().__init__()
         self._root_nodes = nodes
@@ -63,11 +63,12 @@ class Network(Watcher):
             if node.pubkey not in [n.pubkey for n in self.nodes]:
                 self.add_node(node)
                 logging.debug("Loading : {:}".format(data['pubkey']))
-        for n in self.nodes:
-            try:
-                n.changed.disconnect()
-            except TypeError:
-                pass
+            else:
+                other_node = [n for n in self.nodes if n.pubkey == node.pubkey][0]
+                if other_node.block < node.block:
+                    other_node.block = node.block
+                    other_node.last_change = node.last_change
+                    other_node.state = node.state
 
     @classmethod
     def from_json(cls, currency, json_data):
diff --git a/src/cutecoin/core/net/node.py b/src/cutecoin/core/net/node.py
index 165aba98..3f6cb0b6 100644
--- a/src/cutecoin/core/net/node.py
+++ b/src/cutecoin/core/net/node.py
@@ -71,7 +71,6 @@ class Node(QObject):
 
         node = cls(peer.currency, peer.endpoints, "", peer.pubkey, 0,
                    Node.ONLINE, time.time())
-        node.refresh_state()
         logging.debug("Node from address : {:}".format(str(node)))
         return node
 
@@ -90,7 +89,6 @@ class Node(QObject):
 
         node = cls(peer.currency, peer.endpoints, "", "", 0,
                    Node.ONLINE, time.time())
-        node.refresh_state()
         logging.debug("Node from peer : {:}".format(str(node)))
         return node
 
@@ -99,6 +97,7 @@ class Node(QObject):
         endpoints = []
         uid = ""
         pubkey = ""
+        block = 0
         last_change = time.time()
         state = Node.ONLINE
         logging.debug(data)
@@ -117,24 +116,38 @@ class Node(QObject):
         if 'last_change' in data:
             last_change = data['last_change']
 
+        if 'block' in data:
+            block = data['block']
+
         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, block,
                    state, last_change)
-        node.refresh_state()
         logging.debug("Node from json : {:}".format(str(node)))
         return node
 
+    def jsonify_root_node(self):
+        logging.debug("Saving root node : {:}".format(str(self)))
+        data = {'pubkey': self._pubkey,
+                'uid': self._uid,
+                'currency': self._currency}
+        endpoints = []
+        for e in self._endpoints:
+            endpoints.append(e.inline())
+        data['endpoints'] = endpoints
+        return data
+
     def jsonify(self):
         logging.debug("Saving node : {:}".format(str(self)))
         data = {'pubkey': self._pubkey,
                 'uid': self._uid,
                 'currency': self._currency,
                 'state': self._state,
-                'last_change': self._last_change}
+                'last_change': self._last_change,
+                'block': self.block}
         endpoints = []
         for e in self._endpoints:
             endpoints.append(e.inline())
diff --git a/src/cutecoin/gui/contact.py b/src/cutecoin/gui/contact.py
index a753de20..a176ab22 100644
--- a/src/cutecoin/gui/contact.py
+++ b/src/cutecoin/gui/contact.py
@@ -32,6 +32,8 @@ class ConfigureContactDialog(QDialog, Ui_ConfigureContactDialog):
                             'pubkey': contact.pubkey}
         elif type(contact) is dict:
             self.contact = contact
+        else:
+            self.contact = None
 
         if index_edit is not None:
             self.contact = account.contacts[index_edit]
-- 
GitLab