From bc3e23a4ba7749bcfef84bab8a9d9bb303264934 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Thu, 5 Feb 2015 21:55:06 +0100
Subject: [PATCH] Added the ability to send again a transaction without enough
 sources

---
 src/cutecoin/core/transfer.py    | 19 ++++++++-----
 src/cutecoin/core/wallet.py      | 48 ++++++++++++++++++--------------
 src/cutecoin/gui/currency_tab.py |  2 +-
 src/cutecoin/gui/transfer.py     |  7 ++---
 src/cutecoin/models/txhistory.py | 13 +++++++--
 5 files changed, 53 insertions(+), 36 deletions(-)

diff --git a/src/cutecoin/core/transfer.py b/src/cutecoin/core/transfer.py
index a8e13467..23587de0 100644
--- a/src/cutecoin/core/transfer.py
+++ b/src/cutecoin/core/transfer.py
@@ -28,14 +28,12 @@ class Transfer(object):
         self.metadata = metadata
 
     @classmethod
-    def initiate(cls, txdoc, block, time, amount):
-        receivers = [o.pubkey for o in txdoc.outputs
-                             if o.pubkey != txdoc.issuers[0]]
+    def initiate(cls, txdoc, block, time, amount, issuer, receiver):
         return cls(txdoc, Transfer.TO_SEND, {'block': block,
                                              'time': time,
                                              'amount': amount,
-                                             'issuer': txdoc.issuers[0],
-                                             'receiver': receivers[0]})
+                                             'issuer': issuer,
+                                             'receiver': receiver})
 
     @classmethod
     def create_validated(cls, txdoc, metadata):
@@ -43,11 +41,18 @@ class Transfer(object):
 
     @classmethod
     def load(cls, data):
-        txdoc = Transaction.from_signed_raw(data['txdoc'])
+        if data['state'] is Transfer.TO_SEND:
+            txdoc = None
+        else:
+            txdoc = Transaction.from_signed_raw(data['txdoc'])
         return cls(txdoc, data['state'], data['metadata'])
 
     def jsonify(self):
-        return {'txdoc': self.txdoc.signed_raw(),
+        if self.txdoc:
+            txraw = self.txdoc.signed_raw()
+        else:
+            txraw = None
+        return {'txdoc': txraw,
                 'state': self.state,
                 'metadata': self.metadata}
 
diff --git a/src/cutecoin/core/wallet.py b/src/cutecoin/core/wallet.py
index e0ad577f..4d7b490b 100644
--- a/src/cutecoin/core/wallet.py
+++ b/src/cutecoin/core/wallet.py
@@ -238,18 +238,8 @@ class Wallet(object):
 
     def send_money(self, salt, password, community,
                    recipient, amount, message):
-
-        result = self.tx_inputs(int(amount), community)
-        inputs = result[0]
-        self.caches[community.currency].available_sources = result[1]
-        logging.debug("Inputs : {0}".format(inputs))
-
-        outputs = self.tx_outputs(recipient, amount, inputs)
-        logging.debug("Outputs : {0}".format(outputs))
-        tx = Transaction(PROTOCOL_VERSION, community.currency,
-                         [self.pubkey], inputs,
-                         outputs, message, None)
-        logging.debug("TX : {0}".format(tx.raw()))
+        time = community.get_block().time
+        block_number = community.current_blockid()['number']
         key = None
         logging.debug("Key : {0} : {1}".format(salt, password))
         if self.walletid == 0:
@@ -258,15 +248,31 @@ class Wallet(object):
             key = SigningKey("{0}{1}".format(salt, self.walletid), password)
         logging.debug("Sender pubkey:{0}".format(key.pubkey))
 
-        tx.sign([key])
-        logging.debug("Transaction : {0}".format(tx.signed_raw()))
-
-        block_number = community.current_blockid()['number']
-
-        time = community.get_block().time
-        transfer = Transfer.initiate(tx, block_number, time, amount)
-        transfer.send(community)
-        self.caches[community.currency]._transfers.append(transfer)
+        try:
+            result = self.tx_inputs(int(amount), community)
+            inputs = result[0]
+            self.caches[community.currency].available_sources = result[1]
+            logging.debug("Inputs : {0}".format(inputs))
+
+            outputs = self.tx_outputs(recipient, amount, inputs)
+            logging.debug("Outputs : {0}".format(outputs))
+            tx = Transaction(PROTOCOL_VERSION, community.currency,
+                             [self.pubkey], inputs,
+                             outputs, message, None)
+            logging.debug("TX : {0}".format(tx.raw()))
+
+            tx.sign([key])
+            logging.debug("Transaction : {0}".format(tx.signed_raw()))
+
+            transfer = Transfer.initiate(tx, block_number, time, amount,
+                                         key.pubkey, recipient)
+            transfer.send(community)
+            self.caches[community.currency]._transfers.append(transfer)
+        except NotEnoughMoneyError:
+            transfer = Transfer.initiate(None, block_number, time, amount,
+                                         key.pubkey, recipient)
+            self.caches[community.currency]._transfers.append(transfer)
+            raise
 
     def sources(self, community):
         data = community.request(bma.tx.Sources,
diff --git a/src/cutecoin/gui/currency_tab.py b/src/cutecoin/gui/currency_tab.py
index 909ced55..bf916d8d 100644
--- a/src/cutecoin/gui/currency_tab.py
+++ b/src/cutecoin/gui/currency_tab.py
@@ -213,7 +213,7 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
                                                     pubkey_col)
             person = model.sourceModel().data(person_index, Qt.DisplayRole)
             transfer = model.sourceModel().transfers[source_index.row()]
-            if state_data == Transfer.REFUSED:
+            if state_data == Transfer.REFUSED or state_data == Transfer.TO_SEND:
                 send_back = QAction("Send again", self)
                 send_back.triggered.connect(self.send_again)
                 send_back.setData(transfer)
diff --git a/src/cutecoin/gui/transfer.py b/src/cutecoin/gui/transfer.py
index c0e2333e..da151009 100644
--- a/src/cutecoin/gui/transfer.py
+++ b/src/cutecoin/gui/transfer.py
@@ -72,10 +72,9 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
                                  QMessageBox.Ok)
             return
         except NotEnoughMoneyError as e:
-            QMessageBox.critical(self, "Money transfer",
-                                 "You don't have enough money available in this block : \n{0}"
-                                 .format(e.message))
-            return
+            QMessageBox.warning(self, "Money transfer",
+                                 """This transaction could not be sent on this block
+Please try again later""")
         except NoPeerAvailable as e:
             QMessageBox.critical(self, "Money transfer",
                                  "Couldn't connect to network : {0}".format(e),
diff --git a/src/cutecoin/models/txhistory.py b/src/cutecoin/models/txhistory.py
index aaea42e8..24db0bc3 100644
--- a/src/cutecoin/models/txhistory.py
+++ b/src/cutecoin/models/txhistory.py
@@ -87,6 +87,8 @@ class TxFilterProxyModel(QSortFilterProxyModel):
                 font.setItalic(True)
             elif state_data == Transfer.REFUSED:
                 font.setItalic(True)
+            elif state_data == Transfer.TO_SEND:
+                font.setBold(True)
             else:
                 font.setItalic(False)
             return font
@@ -94,6 +96,8 @@ class TxFilterProxyModel(QSortFilterProxyModel):
         if role == Qt.ForegroundRole:
             if state_data == Transfer.REFUSED:
                 return QColor(Qt.red)
+            elif state_data == Transfer.TO_SEND:
+                return QColor(Qt.blue)
         return source_data
 
 
@@ -129,7 +133,9 @@ class HistoryTableModel(QAbstractTableModel):
 
     def data_received(self, transfer):
         amount = transfer.metadata['amount']
-        comment = transfer.txdoc.comment
+        comment = ""
+        if transfer.txdoc:
+            comment = transfer.txdoc.comment
         pubkey = transfer.metadata['issuer']
         try:
             #sender = Person.lookup(pubkey, self.community).name
@@ -148,8 +154,9 @@ class HistoryTableModel(QAbstractTableModel):
 
     def data_sent(self, transfer):
         amount = transfer.metadata['amount']
-
-        comment = transfer.txdoc.comment
+        comment = ""
+        if transfer.txdoc:
+            comment = transfer.txdoc.comment
         pubkey = transfer.metadata['receiver']
         try:
             #receiver = Person.lookup(pubkey, self.community).name
-- 
GitLab