From 43b9bba528e814058868f08819844c022d95d07c Mon Sep 17 00:00:00 2001
From: inso <insomniak.fr@gmaiL.com>
Date: Mon, 10 Apr 2017 20:36:06 +0200
Subject: [PATCH] Fix bug #671 : sent tx not added to local keys

---
 src/sakia/data/entities/transaction.py        |  9 ++++++++-
 src/sakia/gui/dialogs/transfer/model.py       |  9 ++++++---
 .../gui/navigation/txhistory/table_model.py   |  4 ++--
 src/sakia/services/documents.py               |  2 --
 src/sakia/services/transactions.py            | 19 ++++++++++++++++++-
 5 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/sakia/data/entities/transaction.py b/src/sakia/data/entities/transaction.py
index 1a282b9d..e321d013 100644
--- a/src/sakia/data/entities/transaction.py
+++ b/src/sakia/data/entities/transaction.py
@@ -1,5 +1,6 @@
 import attr
 from duniterpy.documents import block_uid
+from duniterpy.documents import Transaction as TransactionDoc
 from duniterpy.documents.transaction import reduce_base
 from sakia.helpers import attrs_tuple_of_str
 import math
@@ -77,7 +78,7 @@ class Transaction:
     :param str currency: the currency of the transaction
     :param str sha_hash: the hash of the transaction
     :param int written_block: the number of the block
-    :param str blockstamp: the blockstamp of the transaction
+    :param duniterpy.documents.BlockUID blockstamp: the blockstamp of the transaction
     :param int timestamp: the timestamp of the transaction
     :param str signature: the signature
     :param str issuer: the pubkey of the issuer
@@ -110,3 +111,9 @@ class Transaction:
     state         = attr.ib(convert=int, cmp=False)
     local         = attr.ib(convert=bool, cmp=False, default=False)
     raw           = attr.ib(convert=str, cmp=False, default="")
+
+    def txdoc(self):
+        """
+        :rtype: duniterpy.documents.Transaction
+        """
+        return TransactionDoc.from_signed_raw(self.raw)
diff --git a/src/sakia/gui/dialogs/transfer/model.py b/src/sakia/gui/dialogs/transfer/model.py
index 7231b386..86b75859 100644
--- a/src/sakia/gui/dialogs/transfer/model.py
+++ b/src/sakia/gui/dialogs/transfer/model.py
@@ -95,10 +95,13 @@ class TransferModel(QObject):
                                                              recipient, amount, amount_base, comment)
         for transaction in transactions:
             self.app.sources_service.parse_transaction(self.connection.pubkey, transaction)
-            if recipient in self._connections_processor.pubkeys():
-                self.app.sources_service.parse_transaction(recipient, transaction)
-            self.app.db.commit()
+            for conn in self._connections_processor.connections():
+                if conn.pubkey == recipient:
+                    self.app.sources_service.parse_transaction(recipient, transaction)
+                    new_tx = self.app.transactions_service.parse_sent_transaction(recipient, transaction)
+                    self.app.new_transfer.emit(conn, new_tx)
             self.app.sources_refreshed.emit()
+            self.app.db.commit()
         return result, transactions
 
     def notifications(self):
diff --git a/src/sakia/gui/navigation/txhistory/table_model.py b/src/sakia/gui/navigation/txhistory/table_model.py
index 4bc59109..a50b2381 100644
--- a/src/sakia/gui/navigation/txhistory/table_model.py
+++ b/src/sakia/gui/navigation/txhistory/table_model.py
@@ -218,7 +218,7 @@ class HistoryTableModel(QAbstractTableModel):
     def add_transfer(self, connection, transfer):
         if self.connection == connection:
             self.beginInsertRows(QModelIndex(), len(self.transfers_data), len(self.transfers_data))
-            if transfer.issuer == self.connection.pubkey:
+            if self.connection.pubkey in transfer.issuers:
                 self.transfers_data.append(self.data_sent(transfer))
             if self.connection.pubkey in transfer.receivers:
                 self.transfers_data.append(self.data_received(transfer))
@@ -238,7 +238,7 @@ class HistoryTableModel(QAbstractTableModel):
                     self.transfers_data.pop(i)
                     self.endRemoveRows()
                 else:
-                    if transfer.issuer == self.connection.pubkey:
+                    if self.connection.pubkey in transfer.issuers:
                         self.transfers_data[i] = self.data_sent(transfer)
                         self.dataChanged.emit(self.index(i, 0), self.index(i, len(HistoryTableModel.columns_types)))
                     if self.connection.pubkey in transfer.receivers:
diff --git a/src/sakia/services/documents.py b/src/sakia/services/documents.py
index c56d1ce3..4585952b 100644
--- a/src/sakia/services/documents.py
+++ b/src/sakia/services/documents.py
@@ -435,8 +435,6 @@ class DocumentsService:
 
             for i, tx in enumerate(tx_entities):
                 logging.debug("Transaction : [{0}]".format(tx.raw))
-                txid = self._transactions_processor.next_txid(connection.currency, blockstamp.number)
-
                 tx_res, tx_entities[i] = await self._transactions_processor.send(tx, connection.currency)
 
                 # Result can be negative if a tx is not accepted by the network
diff --git a/src/sakia/services/transactions.py b/src/sakia/services/transactions.py
index 15f6bbd3..eeeb5060 100644
--- a/src/sakia/services/transactions.py
+++ b/src/sakia/services/transactions.py
@@ -1,5 +1,5 @@
 from PyQt5.QtCore import QObject
-from sakia.data.entities.transaction import parse_transaction_doc
+from sakia.data.entities.transaction import parse_transaction_doc, Transaction
 from duniterpy.documents import Transaction as TransactionDoc
 from duniterpy.documents import SimpleTransaction, Block
 from sakia.data.entities import Dividend
@@ -34,6 +34,23 @@ class TransactionsService(QObject):
         self.currency = currency
         self._logger = logging.getLogger('sakia')
 
+    def parse_sent_transaction(self, pubkey, transaction):
+        """
+        Parse a block
+        :param sakia.data.entities.Transaction transaction: The transaction to parse
+        :return: The list of transfers sent
+        """
+        if not self._transactions_processor.find_by_hash(pubkey, transaction.sha_hash):
+            txid = self._transactions_processor.next_txid(transaction.currency, -1)
+            tx = parse_transaction_doc(transaction.txdoc(), pubkey,
+                                       transaction.blockstamp.number,  transaction.timestamp, txid+1)
+            tx.state = Transaction.AWAITING
+            if tx:
+                self._transactions_processor.commit(tx)
+                return tx
+            else:
+                logging.debug("Error during transfer parsing")
+
     def _parse_block(self, block_doc, txid):
         """
         Parse a block
-- 
GitLab