diff --git a/src/sakia/data/entities/transaction.py b/src/sakia/data/entities/transaction.py index 66b5a30b9af5c811e417c00ce6760e333bae02a8..3180f8ea34079966ccf0bffca9afebd96a8e4e27 100644 --- a/src/sakia/data/entities/transaction.py +++ b/src/sakia/data/entities/transaction.py @@ -17,15 +17,20 @@ def parse_transaction_doc(tx_doc, pubkey, block_number, mediantime, txid): receivers = [o.conditions.left.pubkey for o in tx_doc.outputs if o.conditions.left.pubkey != tx_doc.issuers[0]] - if len(receivers) == 0: - receivers = [tx_doc.issuers[0]] - in_issuers = len([i for i in tx_doc.issuers if i == pubkey]) > 0 in_outputs = len([o for o in tx_doc.outputs if o.conditions.left.pubkey == pubkey]) > 0 - if in_issuers or in_outputs: + if len(receivers) == 0: + receivers = [tx_doc.issuers[0]] + # Transaction to self + outputs = [o for o in tx_doc.outputs] + amount = 0 + for o in outputs: + amount += o.amount * math.pow(10, o.base) + amount, amount_base = reduce_base(amount, 0) + elif in_issuers or in_outputs: # If the wallet pubkey is in the issuers we sent this transaction if in_issuers: outputs = [o for o in tx_doc.outputs @@ -42,23 +47,24 @@ def parse_transaction_doc(tx_doc, pubkey, block_number, mediantime, txid): for o in outputs: amount += o.amount * math.pow(10, o.base) amount, amount_base = reduce_base(amount, 0) + else: + return None - transaction = Transaction(currency=tx_doc.currency, - sha_hash=tx_doc.sha_hash, - written_block=block_number, - blockstamp=tx_doc.blockstamp, - timestamp=mediantime, - signature=tx_doc.signatures[0], - issuer=tx_doc.issuers[0], - receiver=receivers[0], - amount=amount, - amount_base=amount_base, - comment=tx_doc.comment, - txid=txid, - state=Transaction.VALIDATED, - raw=tx_doc.signed_raw()) - return transaction - return None + transaction = Transaction(currency=tx_doc.currency, + sha_hash=tx_doc.sha_hash, + written_block=block_number, + blockstamp=tx_doc.blockstamp, + timestamp=mediantime, + signature=tx_doc.signatures[0], + issuer=tx_doc.issuers[0], + receiver=receivers[0], + amount=amount, + amount_base=amount_base, + comment=tx_doc.comment, + txid=txid, + state=Transaction.VALIDATED, + raw=tx_doc.signed_raw()) + return transaction @attr.s() diff --git a/src/sakia/data/processors/tx_lifecycle.py b/src/sakia/data/processors/tx_lifecycle.py index 37b89567dcc9102a846434ce3eafc568b02ebe24..a1d858a24af3b3c96b33146e334f5d1dfdce9f94 100644 --- a/src/sakia/data/processors/tx_lifecycle.py +++ b/src/sakia/data/processors/tx_lifecycle.py @@ -38,38 +38,6 @@ def _broadcast_failure(tx, ret_codes): """ return 200 not in ret_codes - -def _rollback_and_removed(tx, rollback, block): - """ - Check if the transfer is not in the block anymore - - :param sakia.data.entities.Transaction tx: the transaction - :param bool rollback: True if we are in a rollback procedure - :param duniterpy.documents.Block block: The block to check for the transaction - :return: True if the transfer is not found in the block - """ - if rollback: - if not block or block.blockUID != tx.blockstamp: - return True - else: - return tx.sha_hash not in [t.sha_hash for t in block.transactions] - return False - - -def _rollback_and_local(tx, rollback, block): - """ - Check if the transfer is not in the block anymore - - :param sakia.data.entities.Transaction tx: the transaction - :param bool rollback: True if we are in a rollback procedure - :param duniterpy.documents.Block block: The block to check for the transaction - :return: True if the transfer is found in the block - """ - if rollback and tx.local and block.blockUID == tx.blockstamp: - return tx.sha_hash not in [t.sha_hash for t in block.transactions] - return False - - def _is_locally_created(tx): """ Check if we can send back the transaction if it was locally created @@ -88,8 +56,7 @@ def _be_validated(tx, block): :param bool rollback: True if we are in a rollback procedure :param duniterpy.documents.Block block: The block checked """ - tx.blockstamp = block.blockUID - tx.timestamp = block.mediantime + tx.written_block = block.number def _drop(tx): @@ -116,12 +83,6 @@ states = { (Transaction.AWAITING, (Block,)): ((_found_in_block, _be_validated, Transaction.VALIDATED),), - (Transaction.VALIDATED, (bool,)): - ( - (_rollback_and_removed, lambda tx, r: _drop(tx), Transaction.DROPPED), - (_rollback_and_local, None, Transaction.AWAITING), - ), - (Transaction.REFUSED, ()): ((_is_locally_created, _drop, Transaction.DROPPED),) } diff --git a/src/sakia/gui/navigation/model.py b/src/sakia/gui/navigation/model.py index 26f17d3b06f2344ae58784071b75e4c9601ce10f..71aaa0bebd4e8613add469afc1981ccdaaefdba3 100644 --- a/src/sakia/gui/navigation/model.py +++ b/src/sakia/gui/navigation/model.py @@ -131,6 +131,10 @@ class NavigationModel(QObject): return self.app.identities_service.get_identity(connection.pubkey, connection.uid).member async def remove_connection(self, connection): + for data in self.navigation: + connected_to = self._current_data['misc'].get('connection', None) + if connected_to == connection: + self._current_data['widget'].disconnect() await self.app.remove_connection(connection) async def send_leave(self, connection, password): diff --git a/src/sakia/gui/navigation/txhistory/table_model.py b/src/sakia/gui/navigation/txhistory/table_model.py index 61009dc4a3fa4ac9817549f8e125680dcb5524e7..209b1e7724ae5e021129eee86d1764b71cc56198 100644 --- a/src/sakia/gui/navigation/txhistory/table_model.py +++ b/src/sakia/gui/navigation/txhistory/table_model.py @@ -86,7 +86,7 @@ class TxFilterProxyModel(QSortFilterProxyModel): block_index = model.index(source_index.row(), block_col) block_data = model.data(block_index, Qt.DisplayRole) - if state_data == Transaction.VALIDATED: + if state_data == Transaction.VALIDATED and block_data: current_confirmations = self.blockchain_service.current_buid().number - block_data else: current_confirmations = 0 @@ -215,7 +215,7 @@ class HistoryTableModel(QAbstractTableModel): self.beginInsertRows(QModelIndex(), len(self.transfers_data), len(self.transfers_data)) if transfer.issuer == self.connection.pubkey: self.transfers_data.append(self.data_sent(transfer)) - else: + if transfer.receiver == self.connection.pubkey: self.transfers_data.append(self.data_received(transfer)) self.endInsertRows() @@ -231,12 +231,13 @@ class HistoryTableModel(QAbstractTableModel): self.beginRemoveRows(QModelIndex(), i, i) self.transfers_data.pop(i) self.endRemoveRows() - elif transfer.issuer == self.connection.pubkey: - self.transfers_data[i] = self.data_sent(transfer) - self.dataChanged.emit(self.index(i, 0), self.index(i, len(self.columns_types))) else: - self.transfers_data[i] = self.data_received(transfer) - self.dataChanged.emit(self.index(i, 0), self.index(i, len(self.columns_types))) + if transfer.issuer == self.connection.pubkey: + self.transfers_data[i] = self.data_sent(transfer) + self.dataChanged.emit(self.index(i, 0), self.index(i, len(self.columns_types))) + if transfer.receiver == self.connection.pubkey: + self.transfers_data[i] = self.data_received(transfer) + self.dataChanged.emit(self.index(i, 0), self.index(i, len(self.columns_types))) return def data_received(self, transfer): @@ -309,7 +310,7 @@ class HistoryTableModel(QAbstractTableModel): if transfer.state != Transaction.DROPPED: if transfer.issuer == self.connection.pubkey: self.transfers_data.append(self.data_sent(transfer)) - else: + if transfer.receiver == self.connection.pubkey: self.transfers_data.append(self.data_received(transfer)) dividends = self.dividends() for dividend in dividends: diff --git a/src/sakia/gui/navigation/view.py b/src/sakia/gui/navigation/view.py index 6c5acc029ba1ffda077d4f10c3fe6e8e33dfe1cb..80124e08814fa0233755138688e6f2a3a6215d3c 100644 --- a/src/sakia/gui/navigation/view.py +++ b/src/sakia/gui/navigation/view.py @@ -49,7 +49,3 @@ class NavigationView(QFrame, Ui_Navigation): def add_connection(self, raw_data): self.tree_view.model().insert_node(raw_data) self.tree_view.expandAll() - - def remove_connection(self, raw_data): - self.tree_view.model().remove_node(raw_data) - self.tree_view.expandAll() diff --git a/src/sakia/services/transactions.py b/src/sakia/services/transactions.py index c537d333bad69ffbd1450e88ebbbf0ff2fda7b85..c81691bfa6dae72a4ec517a39f2d4f6ec62f7f25 100644 --- a/src/sakia/services/transactions.py +++ b/src/sakia/services/transactions.py @@ -46,6 +46,7 @@ class TransactionsService(QObject): for tx in [t for t in self._transactions_processor.awaiting(self.currency)]: if self._transactions_processor.run_state_transitions(tx, block_doc): transfers_changed.append(tx) + self._logger.debug("New transaction validated : {0}".format(tx.sha_hash)) new_transactions = [t for t in block_doc.transactions if not self._transactions_processor.find_by_hash(t.sha_hash) diff --git a/tests/technical/test_transactions_service.py b/tests/technical/test_transactions_service.py index 0bb6740ac0f9085b09a5a6f65bfaea22f37c6031..d7184aca3462218463730e171dffd89021b3294b 100644 --- a/tests/technical/test_transactions_service.py +++ b/tests/technical/test_transactions_service.py @@ -12,6 +12,7 @@ async def test_send_tx_then_validate(application_with_one_connection, fake_serve tx_after_send = application_with_one_connection.transactions_service.transfers(bob.key.pubkey) assert len(tx_before_send) + 1 == len(tx_after_send) assert tx_after_send[-1].state is Transaction.AWAITING + assert tx_after_send[-1].written_block == 0 fake_server.forge.forge_block() fake_server.forge.forge_block() fake_server.forge.forge_block() @@ -19,6 +20,7 @@ async def test_send_tx_then_validate(application_with_one_connection, fake_serve await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks) tx_after_parse = application_with_one_connection.transactions_service.transfers(bob.key.pubkey) assert tx_after_parse[-1].state is Transaction.VALIDATED + assert tx_after_parse[-1].written_block == fake_server.forge.blocks[-3].number await fake_server.close()