From a8f625e61f2a84bd4ecceec6322f010388c19276 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Mon, 2 Feb 2015 12:39:06 +0100
Subject: [PATCH] Lifecycle : awaiting transaction now become validated when
 they can

---
 lib/ucoinpy/documents/block.py   |  2 +-
 src/cutecoin/core/transfer.py    | 10 +++++++---
 src/cutecoin/core/wallet.py      | 11 +++++++----
 src/cutecoin/gui/mainwindow.py   |  4 +++-
 src/cutecoin/gui/transfer.py     | 10 ++++++++--
 src/cutecoin/models/txhistory.py |  5 ++++-
 6 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/lib/ucoinpy/documents/block.py b/lib/ucoinpy/documents/block.py
index 6a023cd6..64c3a7c7 100644
--- a/lib/ucoinpy/documents/block.py
+++ b/lib/ucoinpy/documents/block.py
@@ -224,7 +224,7 @@ BOTTOM_SIGNATURE
                 for i in range(n, tx_max):
                     tx_lines += lines[n]
                     n = n + 1
-                transaction = Transaction.from_compact(version, tx_lines)
+                transaction = Transaction.from_compact(currency, tx_lines)
                 transactions.append(transaction)
 
         signature = Block.re_signature.match(lines[n]).group(1)
diff --git a/src/cutecoin/core/transfer.py b/src/cutecoin/core/transfer.py
index 7718396c..313247e4 100644
--- a/src/cutecoin/core/transfer.py
+++ b/src/cutecoin/core/transfer.py
@@ -28,10 +28,14 @@ class Transfer(object):
         self.metadata = metadata
 
     @classmethod
-    def initiate(cls, txdoc, block, amount):
+    def initiate(cls, txdoc, block, time, amount):
+        receivers = [o.pubkey for o in txdoc.outputs
+                             if o.pubkey != txdoc.issuers[0]]
         return cls(txdoc, Transfer.TO_SEND, {'block': block,
+                                             'time': time,
                                              'amount': amount,
-                                             'issuer': txdoc.issuers[0]})
+                                             'issuer': txdoc.issuers[0],
+                                             'receiver': receivers[0]})
 
     @classmethod
     def create_validated(cls, txdoc, metadata):
@@ -57,7 +61,7 @@ class Transfer(object):
                 self.state = Transfer.REFUSED
             raise
         finally:
-            self.metadata['block'] = community.current_blockid['number']
+            self.metadata['block'] = community.current_blockid()['number']
             self.metadata['time'] = community.get_block().time
 
     def check_registered(self, tx, metadata):
diff --git a/src/cutecoin/core/wallet.py b/src/cutecoin/core/wallet.py
index d30607c1..69258251 100644
--- a/src/cutecoin/core/wallet.py
+++ b/src/cutecoin/core/wallet.py
@@ -103,8 +103,10 @@ class Cache():
 
                         awaiting = [t for t in self._transfers
                                     if t.state == Transfer.AWAITING]
-                        awaiting_docs = [t.txdoc for t in awaiting]
-                        if tx not in awaiting_docs:
+                        awaiting_docs = [t.txdoc.signed_raw() for t in awaiting]
+                        logging.debug(tx.signed_raw())
+                        logging.debug(awaiting_docs)
+                        if tx.signed_raw() not in awaiting_docs:
                             transfer = Transfer.create_validated(tx, metadata)
                             self._transfers.append(transfer)
                         for transfer in awaiting:
@@ -259,8 +261,9 @@ class Wallet(object):
 
         block_number = community.current_blockid()['number']
 
-        transfer = Transfer.initiate(tx, block_number, amount)
-        transfer.send()
+        time = community.get_block().time
+        transfer = Transfer.initiate(tx, block_number, time, amount)
+        transfer.send(community)
         self.caches[community.currency]._transfers.append(transfer)
 
     def sources(self, community):
diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py
index 7258f483..bf802833 100644
--- a/src/cutecoin/gui/mainwindow.py
+++ b/src/cutecoin/gui/mainwindow.py
@@ -140,9 +140,11 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         dialog.accepted.connect(self.refresh_wallets)
         dialog.exec_()
         currency_tab = self.currencies_tabwidget.currentWidget()
-        currency_tab.table_history.model().dataChanged.emit(
+        '''currency_tab.table_history.model().sourceModel().dataChanged.emit(
                                                              QModelIndex(),
                                                              QModelIndex(), ())
+                                                             '''
+        currency_tab.table_history.model().invalidate()
 
     def open_certification_dialog(self):
         dialog = CertificationDialog(self.app.current_account,
diff --git a/src/cutecoin/gui/transfer.py b/src/cutecoin/gui/transfer.py
index 344a7d13..c0e2333e 100644
--- a/src/cutecoin/gui/transfer.py
+++ b/src/cutecoin/gui/transfer.py
@@ -105,7 +105,10 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
     def change_current_community(self, index):
         self.community = self.sender.communities[index]
         self.dividend = self.community.dividend()
-        self.label_total.setText(self.wallet.get_text(self.community))
+        amount = self.wallet.value(self.community)
+        ref_amount = self.sender.units_to_ref(amount, self.community)
+        ref_name = self.sender.ref_name(self.community.currency)
+        self.label_total.setText("{0} {1}".format(ref_amount, ref_name))
         self.spinbox_amount.setSuffix(" " + self.community.currency)
         self.spinbox_amount.setValue(0)
         amount = self.wallet.value(self.community)
@@ -115,7 +118,10 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
 
     def change_displayed_wallet(self, index):
         self.wallet = self.sender.wallets[index]
-        self.label_total.setText(self.wallet.get_text(self.community))
+        amount = self.wallet.value(self.community)
+        ref_amount = self.sender.units_to_ref(amount, self.community)
+        ref_name = self.sender.ref_name(self.community.currency)
+        self.label_total.setText("{0} {1}".format(ref_amount, ref_name))
         self.spinbox_amount.setValue(0)
         amount = self.wallet.value(self.community)
         relative = amount / self.dividend
diff --git a/src/cutecoin/models/txhistory.py b/src/cutecoin/models/txhistory.py
index 6e01cd05..2b48c869 100644
--- a/src/cutecoin/models/txhistory.py
+++ b/src/cutecoin/models/txhistory.py
@@ -73,7 +73,10 @@ class HistoryTableModel(QAbstractTableModel):
         self.account = account
         self.community = community
         self.columns = ('Date', 'UID/Public key', 'Payment', 'Deposit', 'Comment')
-        self.transfers = self.account.transfers(community)
+
+    @property
+    def transfers(self):
+        return self.account.transfers(self.community)
 
     def rowCount(self, parent):
         return len(self.transfers)
-- 
GitLab