From 1fa874221085ffe2fbe5200e8c128beefbd46402 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Wed, 31 Dec 2014 07:24:57 +0100
Subject: [PATCH] Fixed a bug in peers parsing when loading a community

---
 res/ui/currency_tab.ui                    |  2 +-
 src/cutecoin/core/community.py            | 11 +++++++----
 src/cutecoin/core/wallet.py               | 13 ++++++++++---
 src/cutecoin/gui/process_cfg_community.py | 14 ++++++++++----
 4 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/res/ui/currency_tab.ui b/res/ui/currency_tab.ui
index afc46e24..8218af48 100644
--- a/res/ui/currency_tab.ui
+++ b/res/ui/currency_tab.ui
@@ -33,7 +33,7 @@
          <bool>false</bool>
         </property>
         <property name="currentIndex">
-         <number>1</number>
+         <number>0</number>
         </property>
         <widget class="QWidget" name="tab_wallets">
          <attribute name="icon">
diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py
index 5f48889c..f7ec25dd 100644
--- a/src/cutecoin/core/community.py
+++ b/src/cutecoin/core/community.py
@@ -47,11 +47,14 @@ class Community(object):
             endpoint_inline = next(e for e in data['endpoints']
                             if Endpoint.from_inline(e) is not None)
             endpoint = Endpoint.from_inline(endpoint_inline)
-            peering = bma.network.Peering(endpoint.conn_handler())
-            peer_data = peering.get()
-            peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'],
+            try:
+                peering = bma.network.Peering(endpoint.conn_handler())
+                peer_data = peering.get()
+                peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'],
                                                   peer_data['signature']))
-            peers.append(peer)
+                peers.append(peer)
+            except:
+                pass
 
         community = cls(currency, peers)
         return community
diff --git a/src/cutecoin/core/wallet.py b/src/cutecoin/core/wallet.py
index a8c09bfd..4d65c372 100644
--- a/src/cutecoin/core/wallet.py
+++ b/src/cutecoin/core/wallet.py
@@ -75,22 +75,29 @@ class Cache():
         with_tx = community.request(bma.blockchain.TX)
         # We parse only blocks with transactions
         parsed_blocks = reversed(range(self.latest_block,
-                                           current_block['number']))
-        parsed_blocks = [n for n in parsed_blocks if n in with_tx['result']['blocks']]
+                                           current_block['number'] + 1))
+        parsed_blocks = [n for n in parsed_blocks
+                         if n in with_tx['result']['blocks']]
         for block_number in parsed_blocks:
             block = community.request(bma.blockchain.Block,
                               req_args={'number': block_number})
             signed_raw = "{0}{1}\n".format(block['raw'], block['signature'])
             block_doc = Block.from_signed_raw(signed_raw)
             for tx in block_doc.transactions:
-                in_outputs = [o for o in tx.outputs if o.pubkey == self.wallet.pubkey]
+                in_outputs = [o for o in tx.outputs
+                              if o.pubkey == self.wallet.pubkey]
                 if len(in_outputs) > 0:
                     self.tx_received.append(tx)
 
                 in_inputs = [i for i in tx.issuers if i == self.wallet.pubkey]
                 if len(in_inputs) > 0:
+                    logging.debug("TX:{0}".format(tx.compact()))
                     # remove from waiting transactions list the one which were
                     # validated in the blockchain
+                    confirmed_tx = [awaiting for awaiting in self.awaiting_tx
+                                         if awaiting.compact() == tx.compact()]
+                    for c in confirmed_tx:
+                        logging.debug("Awaiting:{0}".format(c.compact()))
                     self.awaiting_tx = [awaiting for awaiting in self.awaiting_tx
                                          if awaiting.compact() != tx.compact()]
                     self.tx_sent.append(tx)
diff --git a/src/cutecoin/gui/process_cfg_community.py b/src/cutecoin/gui/process_cfg_community.py
index 07b562e3..5cf49b38 100644
--- a/src/cutecoin/gui/process_cfg_community.py
+++ b/src/cutecoin/gui/process_cfg_community.py
@@ -139,10 +139,16 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog):
         '''
         server = self.lineedit_server.text()
         port = self.spinbox_port.value()
-        if self.community is not None:
-            self.community.add_peer(server, port)
-            self.tree_peers.setModel(PeeringTreeModel(self.community,
-                                                    self.peers))
+        try:
+            peer_data = bma.network.Peering(ConnectionHandler(server, port))
+
+            peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'],
+                                                      peer_data['signature']))
+            self.community.peers.append(peer)
+        except:
+            QMessageBox.critical(self, "Server error",
+                              "Cannot get node peering")
+        self.tree_peers.setModel(PeeringTreeModel(self.community))
 
     def showContextMenu(self, point):
         if self.stacked_pages.currentIndex() == 1:
-- 
GitLab