From 1de513fa8938e746df7c1b2531c77f83e2a80ac6 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Tue, 6 Jan 2015 21:13:45 +0100
Subject: [PATCH] Added broadcast to known peers

---
 src/cutecoin/core/account.py   |  6 +++---
 src/cutecoin/core/community.py | 14 +++++++++++++-
 src/cutecoin/core/wallet.py    |  2 +-
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py
index 062a0e72..b81b5405 100644
--- a/src/cutecoin/core/account.py
+++ b/src/cutecoin/core/account.py
@@ -130,7 +130,7 @@ class Account(object):
                         'self_': selfcert.signed_raw(),
                         'other': "{0}\n".format(certification.inline())}
         logging.debug("Posted data : {0}".format(data))
-        community.post(bma.wot.Add, {}, data)
+        community.broadcast(bma.wot.Add, {}, data)
 
     def sources(self, community):
         sources = []
@@ -189,7 +189,7 @@ class Account(object):
         key = SigningKey(self.salt, password)
         selfcert.sign([key])
         logging.debug("Key publish : {0}".format(selfcert.signed_raw()))
-        community.post(bma.wot.Add, {}, {'pubkey': self.pubkey,
+        community.broadcast(bma.wot.Add, {}, {'pubkey': self.pubkey,
                                     'self_': selfcert.signed_raw(),
                                     'other': []})
 
@@ -206,7 +206,7 @@ class Account(object):
         key = SigningKey(self.salt, password)
         membership.sign([key])
         logging.debug("Membership : {0}".format(membership.signed_raw()))
-        community.post(bma.blockchain.Membership, {},
+        community.broadcast(bma.blockchain.Membership, {},
                        {'membership': membership.signed_raw()})
 
     def jsonify(self):
diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py
index 43f50d1d..6686aa86 100644
--- a/src/cutecoin/core/community.py
+++ b/src/cutecoin/core/community.py
@@ -141,7 +141,6 @@ class Community(object):
                     for d in data:
                         cached_data.append(d)
                     self.requests_cache[cache_key] = cached_data
-                    logging.debug("Got a generator !")
                 else:
                     self.requests_cache[cache_key] = data
             return self.requests_cache[cache_key]
@@ -159,6 +158,19 @@ class Community(object):
                 pass
             return
 
+    def broadcast(self, request, req_args={}, post_args={}):
+        for peer in self.peers:
+            e = next(e for e in peer.endpoints if type(e) is BMAEndpoint)
+            logging.debug("Trying to connect to : " + peer.pubkey)
+            req = request(e.conn_handler(), **req_args)
+            try:
+                req.post(**post_args)
+            except ValueError as e:
+                if peer == self.peers[0]:
+                    raise
+            except:
+                pass
+
     def jsonify_peers_list(self):
         data = []
         for peer in self.peers:
diff --git a/src/cutecoin/core/wallet.py b/src/cutecoin/core/wallet.py
index ad02a6ef..9fdc9aa0 100644
--- a/src/cutecoin/core/wallet.py
+++ b/src/cutecoin/core/wallet.py
@@ -216,7 +216,7 @@ class Wallet(object):
         tx.sign([key])
         logging.debug("Transaction : {0}".format(tx.signed_raw()))
         try:
-            community.post(bma.tx.Process,
+            community.broadcast(bma.tx.Process,
                         post_args={'transaction': tx.signed_raw()})
             self.cache.awaiting_tx.append(tx)
         except:
-- 
GitLab