Skip to content

Commit

Permalink
gui: Avoid redundant tx status updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanofsky committed Dec 6, 2018
1 parent 759ae0d commit da8719d
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/interfaces/capnp/ipc.cpp
Expand Up @@ -123,6 +123,7 @@ void IpcProtocolImpl::listen(kj::Own<kj::ConnectionReceiver>&& listener)
})));
}

// FIXME move above to match class method order
std::unique_ptr<interfaces::Init> IpcProtocolImpl::connect(int fd)
{
std::promise<messages::Init::Client> init_promise;
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/capnp/messages.cpp
Expand Up @@ -29,7 +29,7 @@

#include <boost/core/explicit_operator_bool.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/optional/optional.hpp>
#include <boost/optional.hpp>
#include <boost/variant/get.hpp>
#include <capnp/blob.h>
#include <capnp/list.h>
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/capnp/proxy-codegen.cpp
@@ -1,6 +1,6 @@
#include <algorithm>
#include <boost/core/explicit_operator_bool.hpp>
#include <boost/optional/optional.hpp>
#include <boost/optional.hpp>
#include <capnp/blob.h>
#include <capnp/schema-parser.h>
#include <capnp/schema.capnp.h>
Expand Down
4 changes: 2 additions & 2 deletions src/qt/bitcoin.cpp
Expand Up @@ -482,14 +482,14 @@ void BitcoinApplication::initializeResult(bool success)

#ifdef ENABLE_WALLET
m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) {
WalletModel* wallet_model = new WalletModel(std::move(wallet), m_node, platformStyle, optionsModel, nullptr);
WalletModel* wallet_model = new WalletModel(std::move(wallet), m_node, platformStyle, optionsModel, clientModel);
// Fix wallet model thread affinity.
wallet_model->moveToThread(thread());
QMetaObject::invokeMethod(this, "addWallet", Qt::QueuedConnection, Q_ARG(WalletModel*, wallet_model));
});

for (auto& wallet : m_node.getWallets()) {
addWallet(new WalletModel(std::move(wallet), m_node, platformStyle, optionsModel));
addWallet(new WalletModel(std::move(wallet), m_node, platformStyle, optionsModel, clientModel));
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/qt/bitcoingui.cpp
Expand Up @@ -1138,7 +1138,7 @@ void BitcoinGUI::updateWalletStatus()
}
WalletModel * const walletModel = walletView->getWalletModel();
setEncryptionStatus(walletModel->getEncryptionStatus());
setHDStatus(walletModel->privateKeysDisabled(), walletModel->wallet().hdEnabled());
setHDStatus(walletModel->wallet().privateKeysDisabled(), walletModel->wallet().hdEnabled());
}
#endif // ENABLE_WALLET

Expand Down
11 changes: 11 additions & 0 deletions src/qt/clientmodel.cpp
Expand Up @@ -41,6 +41,7 @@ ClientModel::ClientModel(interfaces::Node& node, OptionsModel *_optionsModel, QO
{
cachedBestHeaderHeight = -1;
cachedBestHeaderTime = -1;
cachedNumBlocks = -1;
peerTableModel = new PeerTableModel(m_node, this);
banTableModel = new BanTableModel(m_node, this);
pollTimer = new QTimer(this);
Expand Down Expand Up @@ -97,6 +98,14 @@ int64_t ClientModel::getHeaderTipTime() const
return cachedBestHeaderTime;
}

int ClientModel::getNumBlocks() const
{
if (cachedNumBlocks == -1) {
cachedNumBlocks = m_node.getNumBlocks();
}
return cachedNumBlocks;
}

void ClientModel::updateTimer()
{
// no locking required at this point
Expand Down Expand Up @@ -236,6 +245,8 @@ static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, int heig
// cache best headers time and height to reduce future cs_main locks
clientmodel->cachedBestHeaderHeight = height;
clientmodel->cachedBestHeaderTime = blockTime;
} else {
clientmodel->cachedNumBlocks = height;
}
// if we are in-sync, update the UI regardless of last update time
if (!initialSync || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
Expand Down
4 changes: 3 additions & 1 deletion src/qt/clientmodel.h
Expand Up @@ -56,6 +56,7 @@ class ClientModel : public QObject

//! Return number of connections, default is in- and outbound (total)
int getNumConnections(unsigned int flags = CONNECTIONS_ALL) const;
int getNumBlocks() const;
int getHeaderTipHeight() const;
int64_t getHeaderTipTime() const;

Expand All @@ -73,9 +74,10 @@ class ClientModel : public QObject

bool getProxyInfo(std::string& ip_port) const;

// caches for the best header
// caches for the best header, number of blocks
mutable std::atomic<int> cachedBestHeaderHeight;
mutable std::atomic<int64_t> cachedBestHeaderTime;
mutable std::atomic<int> cachedNumBlocks;

private:
interfaces::Node& m_node;
Expand Down
8 changes: 4 additions & 4 deletions src/qt/overviewpage.cpp
Expand Up @@ -161,7 +161,7 @@ void OverviewPage::setBalance(const interfaces::WalletBalances& balances)
{
int unit = walletModel->getOptionsModel()->getDisplayUnit();
m_balances = balances;
if (walletModel->privateKeysDisabled()) {
if (walletModel->wallet().privateKeysDisabled()) {
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balances.watch_only_balance, false, BitcoinUnits::separatorAlways));
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, balances.unconfirmed_watch_only_balance, false, BitcoinUnits::separatorAlways));
ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, balances.immature_watch_only_balance, false, BitcoinUnits::separatorAlways));
Expand All @@ -184,7 +184,7 @@ void OverviewPage::setBalance(const interfaces::WalletBalances& balances)
// for symmetry reasons also show immature label when the watch-only one is shown
ui->labelImmature->setVisible(showImmature || showWatchOnlyImmature);
ui->labelImmatureText->setVisible(showImmature || showWatchOnlyImmature);
ui->labelWatchImmature->setVisible(!walletModel->privateKeysDisabled() && showWatchOnlyImmature); // show watch-only immature balance
ui->labelWatchImmature->setVisible(!walletModel->wallet().privateKeysDisabled() && showWatchOnlyImmature); // show watch-only immature balance
}

// show/hide watch-only labels
Expand Down Expand Up @@ -237,9 +237,9 @@ void OverviewPage::setWalletModel(WalletModel *model)

connect(model->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &OverviewPage::updateDisplayUnit);

updateWatchOnlyLabels(wallet.haveWatchOnly() && !model->privateKeysDisabled());
updateWatchOnlyLabels(wallet.haveWatchOnly() && !model->wallet().privateKeysDisabled());
connect(model, &WalletModel::notifyWatchonlyChanged, [this](bool showWatchOnly) {
updateWatchOnlyLabels(showWatchOnly && !walletModel->privateKeysDisabled());
updateWatchOnlyLabels(showWatchOnly && !walletModel->wallet().privateKeysDisabled());
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/qt/test/addressbooktests.cpp
Expand Up @@ -7,6 +7,7 @@
#include <qt/addressbookpage.h>
#include <qt/addresstablemodel.h>
#include <qt/editaddressdialog.h>
#include <qt/clientmodel.h>
#include <qt/optionsmodel.h>
#include <qt/platformstyle.h>
#include <qt/qvalidatedlineedit.h>
Expand Down Expand Up @@ -105,7 +106,8 @@ void TestAddAddressesToSendBook(interfaces::Init& init)
std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
auto node = interfaces::MakeNode(init);
OptionsModel optionsModel(*node);
WalletModel walletModel(interfaces::MakeWallet(wallet), *node, platformStyle.get(), &optionsModel);
ClientModel clientModel(*node, &optionsModel);
WalletModel walletModel(interfaces::MakeWallet(wallet), *node, platformStyle.get(), &optionsModel, &clientModel);
RemoveWallet(wallet);
EditAddressDialog editAddressDialog(EditAddressDialog::NewSendingAddress);
editAddressDialog.setModel(walletModel.getAddressTableModel());
Expand Down
4 changes: 3 additions & 1 deletion src/qt/test/wallettests.cpp
Expand Up @@ -6,6 +6,7 @@
#include <interfaces/node.h>
#include <base58.h>
#include <qt/bitcoinamountfield.h>
#include <qt/clientmodel.h>
#include <qt/optionsmodel.h>
#include <qt/platformstyle.h>
#include <qt/qvalidatedlineedit.h>
Expand Down Expand Up @@ -157,7 +158,8 @@ void TestGUI(interfaces::Init& init)
TransactionView transactionView(platformStyle.get());
auto node = interfaces::MakeNode(init);
OptionsModel optionsModel(*node);
WalletModel walletModel(interfaces::MakeWallet(wallet), *node, platformStyle.get(), &optionsModel);
ClientModel clientModel(*node, &optionsModel);
WalletModel walletModel(interfaces::MakeWallet(wallet), *node, platformStyle.get(), &optionsModel, &clientModel);
RemoveWallet(wallet);
sendCoinsDialog.setModel(&walletModel);
transactionView.setModel(&walletModel);
Expand Down
12 changes: 7 additions & 5 deletions src/qt/transactiontablemodel.cpp
Expand Up @@ -5,6 +5,7 @@
#include <qt/transactiontablemodel.h>

#include <qt/addresstablemodel.h>
#include <qt/clientmodel.h>
#include <qt/guiconstants.h>
#include <qt/guiutil.h>
#include <qt/optionsmodel.h>
Expand Down Expand Up @@ -178,7 +179,7 @@ class TransactionTablePriv
return cachedWallet.size();
}

TransactionRecord *index(interfaces::Wallet& wallet, int idx)
TransactionRecord *index(interfaces::Wallet& wallet, int cur_num_blocks, int idx)
{
if(idx >= 0 && idx < cachedWallet.size())
{
Expand All @@ -193,7 +194,7 @@ class TransactionTablePriv
// simply re-use the cached status.
interfaces::WalletTxStatus wtx;
int numBlocks;
if (wallet.tryGetTxStatus(rec->hash, wtx, numBlocks) && rec->statusUpdateNeeded(numBlocks)) {
if (rec->statusUpdateNeeded(cur_num_blocks) && wallet.tryGetTxStatus(rec->hash, wtx, numBlocks)) {
rec->updateStatus(wtx, numBlocks);
}
return rec;
Expand All @@ -217,9 +218,10 @@ class TransactionTablePriv
}
};

TransactionTableModel::TransactionTableModel(const PlatformStyle *_platformStyle, WalletModel *parent):
TransactionTableModel::TransactionTableModel(const PlatformStyle *_platformStyle, ClientModel *clientModel, WalletModel *parent):
QAbstractTableModel(parent),
walletModel(parent),
clientModel(clientModel),
priv(new TransactionTablePriv(this)),
fProcessingQueuedTransactions(false),
platformStyle(_platformStyle)
Expand Down Expand Up @@ -664,10 +666,10 @@ QVariant TransactionTableModel::headerData(int section, Qt::Orientation orientat
QModelIndex TransactionTableModel::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(parent);
TransactionRecord *data = priv->index(walletModel->wallet(), row);
TransactionRecord *data = priv->index(walletModel->wallet(), clientModel->getNumBlocks(), row);
if(data)
{
return createIndex(row, column, priv->index(walletModel->wallet(), row));
return createIndex(row, column, data);
}
return QModelIndex();
}
Expand Down
4 changes: 3 additions & 1 deletion src/qt/transactiontablemodel.h
Expand Up @@ -16,6 +16,7 @@ namespace interfaces {
class Handler;
}

class ClientModel;
class PlatformStyle;
class TransactionRecord;
class TransactionTablePriv;
Expand All @@ -28,7 +29,7 @@ class TransactionTableModel : public QAbstractTableModel
Q_OBJECT

public:
explicit TransactionTableModel(const PlatformStyle *platformStyle, WalletModel *parent = 0);
explicit TransactionTableModel(const PlatformStyle *platformStyle, ClientModel* clientModel, WalletModel *parent);
~TransactionTableModel();

enum ColumnIndex {
Expand Down Expand Up @@ -85,6 +86,7 @@ class TransactionTableModel : public QAbstractTableModel

private:
WalletModel *walletModel;
ClientModel *clientModel;
std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
std::unique_ptr<interfaces::Handler> m_handler_show_progress;
QStringList columns;
Expand Down
4 changes: 2 additions & 2 deletions src/qt/walletmodel.cpp
Expand Up @@ -32,7 +32,7 @@
#include <QTimer>


WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet, interfaces::Node& node, const PlatformStyle *platformStyle, OptionsModel *_optionsModel, QObject *parent) :
WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet, interfaces::Node& node, const PlatformStyle *platformStyle, OptionsModel *_optionsModel, ClientModel *clientModel, QObject *parent) :
QObject(parent), m_wallet(std::move(wallet)), m_node(node), optionsModel(_optionsModel), addressTableModel(0),
transactionTableModel(0),
recentRequestsTableModel(0),
Expand All @@ -43,7 +43,7 @@ WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet, interfaces:
fForceCheckBalanceChanged = false;

addressTableModel = new AddressTableModel(this);
transactionTableModel = new TransactionTableModel(platformStyle, this);
transactionTableModel = new TransactionTableModel(platformStyle, clientModel, this);
recentRequestsTableModel = new RecentRequestsTableModel(this);

// This timer will be fired repeatedly to update the balance
Expand Down
3 changes: 2 additions & 1 deletion src/qt/walletmodel.h
Expand Up @@ -30,6 +30,7 @@
enum class OutputType;

class AddressTableModel;
class ClientModel;
class OptionsModel;
class PlatformStyle;
class RecentRequestsTableModel;
Expand Down Expand Up @@ -127,7 +128,7 @@ class WalletModel : public QObject
Q_OBJECT

public:
explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet, interfaces::Node& node, const PlatformStyle *platformStyle, OptionsModel *optionsModel, QObject *parent = 0);
explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet, interfaces::Node& node, const PlatformStyle *platformStyle, OptionsModel *optionsModel, ClientModel *clientModel, QObject *parent = 0);
~WalletModel();

enum StatusCode // Returned by sendCoins
Expand Down
1 change: 0 additions & 1 deletion test/lint/lint-includes.sh
Expand Up @@ -64,7 +64,6 @@ EXPECTED_BOOST_INCLUDES=(
boost/multi_index/sequenced_index.hpp
boost/multi_index_container.hpp
boost/optional.hpp
boost/optional/optional.hpp
boost/preprocessor/cat.hpp
boost/preprocessor/stringize.hpp
boost/signals2/connection.hpp
Expand Down

0 comments on commit da8719d

Please sign in to comment.