From 6c6db2ac5d02a7cb962a95f805285c62d9f3eb67 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Sat, 19 Sep 2015 09:09:08 +0200
Subject: [PATCH] Fix problems in network parsing

- Check sync only with online or desynced nodes
- New nodes should be offline before contacting them
---
 src/cutecoin/core/net/network.py | 8 ++++----
 src/cutecoin/core/net/node.py    | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/cutecoin/core/net/network.py b/src/cutecoin/core/net/network.py
index e600aab4..c00c8fac 100644
--- a/src/cutecoin/core/net/network.py
+++ b/src/cutecoin/core/net/network.py
@@ -198,18 +198,18 @@ class Network(QObject):
         4 : The biggest number or timestamp
         """
         # rule number 1 : block of the majority
-        blocks = [n.block['hash'] for n in self.nodes if n.block]
+        blocks = [n.block['hash'] for n in self.online_nodes if n.block]
         blocks_occurences = Counter(blocks)
         blocks_by_occurences = {}
         for key, value in blocks_occurences.items():
-            the_block = [n.block for n in self.nodes if n.block and n.block['hash'] == key][0]
+            the_block = [n.block for n in self.online_nodes if n.block and n.block['hash'] == key][0]
             if value not in blocks_by_occurences:
                 blocks_by_occurences[value] = [the_block]
             else:
                 blocks_by_occurences[value].append(the_block)
 
         if len(blocks_by_occurences) == 0:
-            for n in [n for n in self._nodes if n.state in (Node.ONLINE, Node.DESYNCED)]:
+            for n in [n for n in self.online_nodes if n.state in (Node.ONLINE, Node.DESYNCED)]:
                 n.state = Node.ONLINE
             return
 
@@ -243,7 +243,7 @@ class Network(QObject):
         else:
             synced_block_hash = blocks_by_occurences[most_present][0]['hash']
 
-        for n in [n for n in self._nodes if n.state in (Node.ONLINE, Node.DESYNCED)]:
+        for n in self.online_nodes:
             if n.block and n.block['hash'] == synced_block_hash:
                 n.state = Node.ONLINE
             else:
diff --git a/src/cutecoin/core/net/node.py b/src/cutecoin/core/net/node.py
index 916bc6d9..28d44778 100644
--- a/src/cutecoin/core/net/node.py
+++ b/src/cutecoin/core/net/node.py
@@ -104,7 +104,7 @@ class Node(QObject):
 
         node = cls(peer.currency, peer.endpoints,
                    "", pubkey, None,
-                   Node.ONLINE, time.time(),
+                   Node.OFFLINE, time.time(),
                    {'root': "", 'leaves': []},
                    "", "", 0)
         logging.debug("Node from peer : {:}".format(str(node)))
-- 
GitLab