From 3ae2e270e9378631efd8a4be060de44197da46f9 Mon Sep 17 00:00:00 2001 From: levonpetrosyan93 Date: Sun, 3 Nov 2024 07:10:00 +0400 Subject: [PATCH] Review commits applied --- src/qt/clientmodel.cpp | 19 ++++++++++++++----- src/qt/clientmodel.h | 4 ++++ src/qt/sparkmodel.cpp | 5 +++-- src/qt/sparkmodel.h | 2 ++ src/qt/transactiontablemodel.cpp | 25 ++++++++++++++++++++----- src/qt/transactiontablemodel.h | 2 +- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp index c85925bf40..2c9458b295 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -38,6 +38,8 @@ ClientModel::ClientModel(OptionsModel *_optionsModel, QObject *parent) : { cachedBestHeaderHeight = -1; cachedBestHeaderTime = -1; + cachedNumBlocks = 0; + cachedLastBlockDate = QDateTime(); peerTableModel = new PeerTableModel(this); banTableModel = new BanTableModel(this); pollTimer = new QTimer(this); @@ -102,10 +104,10 @@ void ClientModel::refreshMasternodeList() int ClientModel::getNumBlocks() const { TRY_LOCK(cs_main,lock); - if (!lock) { - return -1; + if (lock) { + cachedNumBlocks = chainActive.Height(); } - return chainActive.Height(); + return cachedNumBlocks; } int ClientModel::getHeaderTipHeight() const @@ -158,8 +160,13 @@ QDateTime ClientModel::getLastBlockDate() const { TRY_LOCK(cs_main,lock); - if (chainActive.Tip()) - return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime()); + if (!lock) + cachedLastBlockDate; + + if (chainActive.Tip()) { + cachedLastBlockDate = QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime()); + return cachedLastBlockDate; + } return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network } @@ -180,6 +187,8 @@ double ClientModel::getVerificationProgress(const CBlockIndex *tipIn) const if (!tip) { TRY_LOCK(cs_main,lock); + if (!lock) + return 0; tip = chainActive.Tip(); } return GuessVerificationProgress(Params().TxData(), tip); diff --git a/src/qt/clientmodel.h b/src/qt/clientmodel.h index 750633c15a..90312f0f7b 100644 --- a/src/qt/clientmodel.h +++ b/src/qt/clientmodel.h @@ -93,6 +93,10 @@ class ClientModel : public QObject mutable std::atomic cachedBestHeaderHeight; mutable std::atomic cachedBestHeaderTime; + mutable std::atomic cachedNumBlocks; + mutable QDateTime cachedLastBlockDate; + + private: OptionsModel *optionsModel; PeerTableModel *peerTableModel; diff --git a/src/qt/sparkmodel.cpp b/src/qt/sparkmodel.cpp index 05c970cc63..6b7c36cb97 100644 --- a/src/qt/sparkmodel.cpp +++ b/src/qt/sparkmodel.cpp @@ -39,10 +39,10 @@ CAmount SparkModel::getMintableSparkAmount() { TRY_LOCK(cs_main,lock_main); if (!lock_main) - return 0; + return cachedMintableSparkAmount; TRY_LOCK(wallet->cs_wallet,lock_wallet); if (!lock_wallet) - return 0; + return cachedMintableSparkAmount; pwalletMain->AvailableCoinsForLMint(valueAndUTXO, nullptr); } @@ -51,6 +51,7 @@ CAmount SparkModel::getMintableSparkAmount() s += val.first; } + cachedMintableSparkAmount = s; return s; } diff --git a/src/qt/sparkmodel.h b/src/qt/sparkmodel.h index 6564cf0521..04d2e6d585 100644 --- a/src/qt/sparkmodel.h +++ b/src/qt/sparkmodel.h @@ -40,6 +40,8 @@ class SparkModel : public QObject public: mutable CCriticalSection cs; + mutable std::atomic cachedMintableSparkAmount; + Q_SIGNALS: void askMintSparkAll(AutoMintSparkMode); void ackMintSparkAll(AutoMintSparkAck ack, CAmount minted, QString error); diff --git a/src/qt/transactiontablemodel.cpp b/src/qt/transactiontablemodel.cpp index d769933200..d15ae81e62 100644 --- a/src/qt/transactiontablemodel.cpp +++ b/src/qt/transactiontablemodel.cpp @@ -16,7 +16,6 @@ #include "core_io.h" #include "validation.h" #include "sync.h" -#include "uint256.h" #include "util.h" #include "wallet/wallet.h" @@ -74,6 +73,7 @@ class TransactionTablePriv * this is sorted by sha256. */ QList cachedWallet; + std::vector>> cachedUpdatedTx; /* Query entire wallet anew from core. */ @@ -137,11 +137,15 @@ class TransactionTablePriv if(showTransaction) { TRY_LOCK(cs_main,lock_main); - if (!lock_main) + if (!lock_main) { + cachedUpdatedTx.push_back(std::make_pair(hash, std::make_pair(status, showTransaction))); return; + } TRY_LOCK(wallet->cs_wallet,lock_wallet); - if (!lock_wallet) + if (!lock_wallet) { + cachedUpdatedTx.push_back(std::make_pair(hash, std::make_pair(status, showTransaction))); return; + } // Find transaction in wallet std::map::iterator mi = wallet->mapWallet.find(hash); if(mi == wallet->mapWallet.end()) @@ -289,8 +293,19 @@ void TransactionTableModel::updateTransaction(const QString &hash, int status, b { uint256 updated; updated.SetHex(hash.toStdString()); - - priv->updateWallet(updated, status, showTransaction); + priv->cachedUpdatedTx.push_back(std::make_pair(updated, std::make_pair(status, showTransaction))); + size_t currentSize = priv->cachedUpdatedTx.size(); + for (auto itr = priv->cachedUpdatedTx.rbegin(); itr!= priv->cachedUpdatedTx.rend();) + { + std::pair> current = *itr; + priv->cachedUpdatedTx.erase(--priv->cachedUpdatedTx.end()); + itr++; + priv->updateWallet(current.first, current.second.first, current.second.second); + // this thread was not able to perform the update, stop and do it next time + if (currentSize == priv->cachedUpdatedTx.size()) + break; + currentSize = priv->cachedUpdatedTx.size(); + } } void TransactionTableModel::updateConfirmations() diff --git a/src/qt/transactiontablemodel.h b/src/qt/transactiontablemodel.h index 8ec4a61554..d25a4acde3 100644 --- a/src/qt/transactiontablemodel.h +++ b/src/qt/transactiontablemodel.h @@ -6,7 +6,7 @@ #define BITCOIN_QT_TRANSACTIONTABLEMODEL_H #include "bitcoinunits.h" - +#include "uint256.h" #include #include