Merge bitcoin-core/gui#121: Early subscribe core signals in transaction table model

cafef080a2 qt: Refactor to remove unnecessary block in DispatchNotifications (João Barbosa)
57785fb7f6 qt: Early subscribe core signals in transaction table model (João Barbosa)
c6cbdf1a90 qt: Refactor ShowProgress to DispatchNotifications (João Barbosa)
3bccd50ad2 qt: Set flag after inital load on transaction table model (João Barbosa)

Pull request description:

  This fixes the case where transaction notifications arrive between `getWalletTxs` and `subscribeToCoreSignals`. Basically notifications are queued until `getWalletTxs` and wallet rescan complete.

  This is also a requirement to call `getWalletTxs` in a background thread.

  Motivated by https://github.com/bitcoin/bitcoin/issues/20241.

ACKs for top commit:
  jonatack:
    tACK cafef080a2
  ryanofsky:
    Code review ACK cafef080a2. Only change since last review is splitting commits and replacing m_progress with m_loading.
  meshcollider:
    Code review ACK cafef080a2

Tree-SHA512: 003caab2f2ae3522619711c8d02d521d2b8f7f280a467f6c3d08abf37ca81cc66b4b9fa10acfdf34e5fe250da7b696cfeec435f72b53c1ea97ccda96d8b4be33
This commit is contained in:
Hennadii Stepanov
2021-05-26 13:24:01 +03:00

View File

@@ -96,18 +96,20 @@ public:
*/ */
QList<TransactionRecord> cachedWallet; QList<TransactionRecord> cachedWallet;
bool fQueueNotifications = false; /** True when model finishes loading all wallet transactions on start */
bool m_loaded = false;
/** True when transactions are being notified, for instance when scanning */
bool m_loading = false;
std::vector< TransactionNotification > vQueueNotifications; std::vector< TransactionNotification > vQueueNotifications;
void NotifyTransactionChanged(const uint256 &hash, ChangeType status); void NotifyTransactionChanged(const uint256 &hash, ChangeType status);
void ShowProgress(const std::string &title, int nProgress); void DispatchNotifications();
/* Query entire wallet anew from core. /* Query entire wallet anew from core.
*/ */
void refreshWallet(interfaces::Wallet& wallet) void refreshWallet(interfaces::Wallet& wallet)
{ {
qDebug() << "TransactionTablePriv::refreshWallet"; assert(!m_loaded);
cachedWallet.clear();
{ {
for (const auto& wtx : wallet.getWalletTxs()) { for (const auto& wtx : wallet.getWalletTxs()) {
if (TransactionRecord::showTransaction()) { if (TransactionRecord::showTransaction()) {
@@ -115,6 +117,8 @@ public:
} }
} }
} }
m_loaded = true;
DispatchNotifications();
} }
/* Update our model of the wallet incrementally, to synchronize our model of the wallet /* Update our model of the wallet incrementally, to synchronize our model of the wallet
@@ -249,12 +253,12 @@ TransactionTableModel::TransactionTableModel(const PlatformStyle *_platformStyle
fProcessingQueuedTransactions(false), fProcessingQueuedTransactions(false),
platformStyle(_platformStyle) platformStyle(_platformStyle)
{ {
subscribeToCoreSignals();
columns << QString() << QString() << tr("Date") << tr("Type") << tr("Label") << BitcoinUnits::getAmountColumnTitle(walletModel->getOptionsModel()->getDisplayUnit()); columns << QString() << QString() << tr("Date") << tr("Type") << tr("Label") << BitcoinUnits::getAmountColumnTitle(walletModel->getOptionsModel()->getDisplayUnit());
priv->refreshWallet(walletModel->wallet()); priv->refreshWallet(walletModel->wallet());
connect(walletModel->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &TransactionTableModel::updateDisplayUnit); connect(walletModel->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &TransactionTableModel::updateDisplayUnit);
subscribeToCoreSignals();
} }
TransactionTableModel::~TransactionTableModel() TransactionTableModel::~TransactionTableModel()
@@ -719,7 +723,7 @@ void TransactionTablePriv::NotifyTransactionChanged(const uint256 &hash, ChangeT
TransactionNotification notification(hash, status, showTransaction); TransactionNotification notification(hash, status, showTransaction);
if (fQueueNotifications) if (!m_loaded || m_loading)
{ {
vQueueNotifications.push_back(notification); vQueueNotifications.push_back(notification);
return; return;
@@ -727,36 +731,34 @@ void TransactionTablePriv::NotifyTransactionChanged(const uint256 &hash, ChangeT
notification.invoke(parent); notification.invoke(parent);
} }
void TransactionTablePriv::ShowProgress(const std::string &title, int nProgress) void TransactionTablePriv::DispatchNotifications()
{ {
if (nProgress == 0) if (!m_loaded || m_loading) return;
fQueueNotifications = true;
if (nProgress == 100) if (vQueueNotifications.size() > 10) { // prevent balloon spam, show maximum 10 balloons
bool invoked = QMetaObject::invokeMethod(parent, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, true));
assert(invoked);
}
for (unsigned int i = 0; i < vQueueNotifications.size(); ++i)
{ {
fQueueNotifications = false; if (vQueueNotifications.size() - i <= 10) {
if (vQueueNotifications.size() > 10) { // prevent balloon spam, show maximum 10 balloons bool invoked = QMetaObject::invokeMethod(parent, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, false));
bool invoked = QMetaObject::invokeMethod(parent, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, true));
assert(invoked); assert(invoked);
} }
for (unsigned int i = 0; i < vQueueNotifications.size(); ++i)
{
if (vQueueNotifications.size() - i <= 10) {
bool invoked = QMetaObject::invokeMethod(parent, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, false));
assert(invoked);
}
vQueueNotifications[i].invoke(parent); vQueueNotifications[i].invoke(parent);
}
vQueueNotifications.clear();
} }
vQueueNotifications.clear();
} }
void TransactionTableModel::subscribeToCoreSignals() void TransactionTableModel::subscribeToCoreSignals()
{ {
// Connect signals to wallet // Connect signals to wallet
m_handler_transaction_changed = walletModel->wallet().handleTransactionChanged(std::bind(&TransactionTablePriv::NotifyTransactionChanged, priv, std::placeholders::_1, std::placeholders::_2)); m_handler_transaction_changed = walletModel->wallet().handleTransactionChanged(std::bind(&TransactionTablePriv::NotifyTransactionChanged, priv, std::placeholders::_1, std::placeholders::_2));
m_handler_show_progress = walletModel->wallet().handleShowProgress(std::bind(&TransactionTablePriv::ShowProgress, priv, std::placeholders::_1, std::placeholders::_2)); m_handler_show_progress = walletModel->wallet().handleShowProgress([this](const std::string&, int progress) {
priv->m_loading = progress < 100;
priv->DispatchNotifications();
});
} }
void TransactionTableModel::unsubscribeFromCoreSignals() void TransactionTableModel::unsubscribeFromCoreSignals()