From b7c31ad80ef8deeff107114e9835ca6cdfe4b934 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Tue, 15 Nov 2022 15:33:21 +0100 Subject: [PATCH] qml: move tx verified event handling to transactionlistmodel, refresh history after broadcast --- electrum/gui/qml/components/TxDetails.qml | 1 + electrum/gui/qml/qetransactionlistmodel.py | 22 ++++++++++++++++++++-- electrum/gui/qml/qewallet.py | 12 ++++-------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/electrum/gui/qml/components/TxDetails.qml b/electrum/gui/qml/components/TxDetails.qml index c87842298..8aea9f65e 100644 --- a/electrum/gui/qml/components/TxDetails.qml +++ b/electrum/gui/qml/components/TxDetails.qml @@ -415,6 +415,7 @@ Pane { qsTr('Note: this is an offline transaction, if you want the network to see it, you need to broadcast it.') }) dialog.open() + root.close() } onSaveTxError: { var dialog = app.messageDialog.createObject(app, { diff --git a/electrum/gui/qml/qetransactionlistmodel.py b/electrum/gui/qml/qetransactionlistmodel.py index 71237701b..c61133a14 100644 --- a/electrum/gui/qml/qetransactionlistmodel.py +++ b/electrum/gui/qml/qetransactionlistmodel.py @@ -7,13 +7,19 @@ from electrum.logging import get_logger from electrum.util import Satoshis, TxMinedInfo from .qetypes import QEAmount +from .util import QtEventListener, qt_event_listener -class QETransactionListModel(QAbstractListModel): +class QETransactionListModel(QAbstractListModel, QtEventListener): def __init__(self, wallet, parent=None, *, onchain_domain=None, include_lightning=True): super().__init__(parent) self.wallet = wallet self.onchain_domain = onchain_domain self.include_lightning = include_lightning + + self.register_callbacks() + self.destroyed.connect(lambda: self.on_destroy()) + self.requestRefresh.connect(lambda: self.init_model()) + self.init_model() _logger = get_logger(__name__) @@ -26,6 +32,17 @@ class QETransactionListModel(QAbstractListModel): _ROLE_MAP = dict(zip(_ROLE_KEYS, [bytearray(x.encode()) for x in _ROLE_NAMES])) _ROLE_RMAP = dict(zip(_ROLE_NAMES, _ROLE_KEYS)) + requestRefresh = pyqtSignal() + + def on_destroy(self): + self.unregister_callbacks() + + @qt_event_listener + def on_event_verified(self, wallet, txid, info): + if wallet == self.wallet: + self._logger.debug('verified event for txid %s' % txid) + self.on_tx_verified(txid, info) + def rowCount(self, index): return len(self.tx_history) @@ -117,6 +134,7 @@ class QETransactionListModel(QAbstractListModel): # initial model data @pyqtSlot() def init_model(self): + self._logger.debug('retrieving history') history = self.wallet.get_full_history(onchain_domain=self.onchain_domain, include_lightning=self.include_lightning) txs = [] @@ -129,7 +147,7 @@ class QETransactionListModel(QAbstractListModel): self.tx_history.reverse() self.endInsertRows() - def update_tx(self, txid, info): + def on_tx_verified(self, txid, info): i = 0 for tx in self.tx_history: if 'txid' in tx and tx['txid'] == txid: diff --git a/electrum/gui/qml/qewallet.py b/electrum/gui/qml/qewallet.py index b62a34c87..6e45ac506 100644 --- a/electrum/gui/qml/qewallet.py +++ b/electrum/gui/qml/qewallet.py @@ -176,11 +176,6 @@ class QEWallet(AuthMixin, QObject, QtEventListener): self.addressModel.setDirty() self.historyModel.init_model() # TODO: be less dramatic - @qt_event_listener - def on_event_verified(self, wallet, txid, info): - if wallet == self.wallet: - self.historyModel.update_tx(txid, info) - @event_listener def on_event_wallet_updated(self, wallet): if wallet == self.wallet: @@ -489,13 +484,12 @@ class QEWallet(AuthMixin, QObject, QtEventListener): def broadcast(self, tx): assert tx.is_complete() - network = self.wallet.network # TODO not always defined? + # network = self.wallet.network # TODO not always defined? def broadcast_thread(): try: self._logger.info('running broadcast in thread') - result = network.run_from_another_thread(network.broadcast_transaction(tx)) - self._logger.info(repr(result)) + self.wallet.network.run_from_another_thread(self.wallet.network.broadcast_transaction(tx)) except TxBroadcastError as e: self._logger.error(repr(e)) self.broadcastFailed.emit(tx.txid(),'',repr(e)) @@ -503,7 +497,9 @@ class QEWallet(AuthMixin, QObject, QtEventListener): self._logger.error(repr(e)) self.broadcastFailed.emit(tx.txid(),'',repr(e)) else: + self._logger.info('broadcast success') self.broadcastSucceeded.emit(tx.txid()) + self.historyModel.requestRefresh.emit() # via qt thread threading.Thread(target=broadcast_thread).start()