From 1b68ee606c11bb3a13babb810eaf69290310fc09 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Tue, 20 May 2014 18:08:02 +0200
Subject: [PATCH] WHT pushing and pulling : OK

---
 src/cutecoin/gui/processConfigureCommunity.py |  9 +++++++-
 .../models/account/wallets/__init__.py        | 12 +++++++++--
 src/cutecoin/models/node/__init__.py          | 11 ++++++++++
 src/cutecoin/models/wallet/__init__.py        | 21 +++++++++----------
 4 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/src/cutecoin/gui/processConfigureCommunity.py b/src/cutecoin/gui/processConfigureCommunity.py
index 1b9d5ddf..307d78ab 100644
--- a/src/cutecoin/gui/processConfigureCommunity.py
+++ b/src/cutecoin/gui/processConfigureCommunity.py
@@ -46,7 +46,6 @@ class StepPageInit(Step):
         default_node = Node.create(server, port, trust=True, hoster=True)
         account = self.config_dialog.account
         self.config_dialog.community = account.add_community(default_node)
-        self.config_dialog.nodes.append(default_node)
 
     def display_page(self):
         self.config_dialog.button_previous.setEnabled(False)
@@ -67,6 +66,12 @@ class StepPageAddNodes(Step):
         pass
 
     def display_page(self):
+        # We add already known nodes to the displayed list
+        for wallet in self.config_dialog.account.wallets:
+            for node in wallet.nodes:
+                if node not in self.config_dialog.nodes:
+                    self.config_dialog.nodes.append(node)
+
         tree_model = NodesTreeModel(self.config_dialog.nodes,
                                     self.config_dialog.community.name())
         self.config_dialog.tree_nodes.setModel(tree_model)
@@ -135,6 +140,8 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog):
             self.step = step_init
             self.setWindowTitle("Add a community")
 
+        self.step.display_page()
+
     def next(self):
         if self.step.next_step is not None:
             if self.step.is_valid():
diff --git a/src/cutecoin/models/account/wallets/__init__.py b/src/cutecoin/models/account/wallets/__init__.py
index 6f7c8683..e0432c98 100644
--- a/src/cutecoin/models/account/wallets/__init__.py
+++ b/src/cutecoin/models/account/wallets/__init__.py
@@ -50,11 +50,19 @@ class Wallets(object):
                    required_trusts=1, name="Main Wallet"):
         '''
         Create a new wallet of a specific currency.
-        This wallet must not already be present in the account,
-        it means the wallet must have a different name or a different currency.
         '''
         wallet = Wallet.create(keyid, community, node,
                                required_trusts, name)
+        # We try to add already present nodes to the wallet
+        try:
+            present_nodes = wallet.get_nodes_in_peering(wallet.pull_wht())
+        except ValueError:
+            present_nodes = []
+
+        for present_node in present_nodes:
+            if present_node not in wallet.nodes:
+                wallet.nodes.append(present_node)
+
         if wallet not in self._wallets_list:
             self._wallets_list.append(wallet)
             return wallet
diff --git a/src/cutecoin/models/node/__init__.py b/src/cutecoin/models/node/__init__.py
index cb27cf01..bfba556a 100644
--- a/src/cutecoin/models/node/__init__.py
+++ b/src/cutecoin/models/node/__init__.py
@@ -5,6 +5,7 @@ Created on 1 févr. 2014
 '''
 
 import ucoin
+import re
 
 
 class Node(object):
@@ -26,6 +27,16 @@ class Node(object):
     def create(cls, server, port, trust=False, hoster=False):
         return cls(server, port, trust, hoster)
 
+    @classmethod
+    def from_endpoints(cls, endpoints):
+        #TODO: Manage multiple endpoints
+        for endpoint in endpoints:
+            bma_endpoints = re.compile('^BASIC_MERKLED_API( ([a-z_][a-z0-9-_.]+))?( ([0-9.]+))?( ([0-9a-f:]+))?( ([0-9]+))$')
+            m = bma_endpoints.match(endpoint)
+            server = m.group(4)
+            port = int(m.group(8))
+        return cls(server, port, False, False)
+
     @classmethod
     def load(cls, json_data):
         server = json_data['server']
diff --git a/src/cutecoin/models/wallet/__init__.py b/src/cutecoin/models/wallet/__init__.py
index 54971e9b..b3078a1c 100644
--- a/src/cutecoin/models/wallet/__init__.py
+++ b/src/cutecoin/models/wallet/__init__.py
@@ -93,11 +93,8 @@ class Wallet(object):
         return sent
 
     def pull_wht(self):
-        try:
-            wht = self.request(ucoin.network.Wallet(self.fingerprint()))
-            return wht['entries']
-        except ValueError:
-            return None
+        wht = self.request(ucoin.network.Wallet(self.fingerprint()))
+        return wht['entry']
 
     def push_wht(self):
         hosters_fg = []
@@ -145,9 +142,9 @@ Hosters:
                 for peer in next_node.peers():
                     # Look for next node informations
                     found = self._search_node_by_fingerprint(
-                        node_fg, Node.create(
-                            peer['ipv4'], int(
-                                peer['port'])), traversed_nodes)
+                        node_fg,
+                        Node.from_endpoints(peer['value']['endpoints']),
+                        traversed_nodes)
                     if found is not None:
                         return found
         return None
@@ -155,10 +152,12 @@ Hosters:
     def get_nodes_in_peering(self, fingerprints):
         nodes = []
         for node_fg in fingerprints:
-            nodes.append(
-                self._search_node_by_fingerprint(
+            node = self._search_node_by_fingerprint(
                     node_fg,
-                    self.trusts()[0]))
+                    self.trusts()[0])
+            if node is not None:
+                nodes.append(node)
+            #TODO: Check that one trust exists
         return nodes
 
     def request(self, request, get_args={}):
-- 
GitLab