From a3e4586397ed3862823e28f067ed5762146de40a Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Tue, 23 Oct 2018 11:28:46 -0700 Subject: [PATCH] Add batchRPC, and add shielded receive txs to the transactions table. --- src/rpc.cpp | 57 ++++++++++++++++++----------------------- src/transactionitem.cpp | 12 --------- src/transactionitem.h | 19 +++++--------- src/txtablemodel.cpp | 35 ++++++++++++++++++++----- src/txtablemodel.h | 8 ++++-- src/utils.h | 3 ++- zcash-qt-wallet.pro | 1 - 7 files changed, 68 insertions(+), 67 deletions(-) delete mode 100644 src/transactionitem.cpp diff --git a/src/rpc.cpp b/src/rpc.cpp index 3e42b1b..9c135fc 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -368,6 +368,8 @@ void RPC::getReceivedZTrans(QList zaddrs) { return payload; }, [=] (QMap* txidDetails) { + QList txdata; + // Combine them both together. For every zAddr's txid, get the amount, fee, confirmations and time for (auto it = zaddrTxids->constBegin(); it != zaddrTxids->constEnd(); it++) { for (auto& i : it.value().get()) { @@ -380,11 +382,15 @@ void RPC::getReceivedZTrans(QList zaddrs) { // And then find the values auto timestamp = txidInfo["time"].get(); auto amount = i["amount"].get(); + auto confirmations = txidInfo["confirmations"].get(); - std::cout << zaddr.toStdString() << ":" << txid.toStdString() << ":" << timestamp << ":" << amount << std::endl; + TransactionItem tx{ QString("receive"), timestamp, zaddr, txid, amount, confirmations }; + txdata.push_front(tx); } } + transactionsTableModel->addNewData(txdata); + // Cleanup both responses; delete zaddrTxids; delete txidDetails; @@ -419,10 +425,13 @@ void RPC::getInfoThenRefresh() { QIcon i(":/icons/res/connected.png"); main->statusIcon->setPixmap(i.pixmap(16, 16)); - // Refresh everything. - refreshBalances(); - refreshTransactions(); + // Expect 2 data additions, then automatically refresh the table + transactionsTableModel->prepNewData(2); + + // Refresh everything. + refreshBalances(); refreshAddresses(); + refreshTransactions(); // Call to see if the blockchain is syncing. json payload = { @@ -544,46 +553,30 @@ void RPC::refreshBalances() { }); } -void RPC::refreshTransactions() { - auto txdata = new QList(); - /* - auto getZReceivedTransactions = ([=] (const json& reply) { - for (auto& it : reply.get()) { - TransactionItem tx( - QString("receive"), - QDateTime::fromSecsSinceEpoch(it["time"].get()).toLocalTime().toString(), - (it["address"].is_null() ? "" : QString::fromStdString(it["address"])), - QString::fromStdString(it["txid"]), - it["amount"].get(), - it["confirmations"].get() - ); - - txdata->push_front(tx); - } - }); - */ - +void RPC::refreshTransactions() { getTransactions([=] (json reply) { + QList txdata; + for (auto& it : reply.get()) { double fee = 0; if (it.find("fee") != it.end()) { fee = it["fee"].get(); } - TransactionItem tx( + TransactionItem tx{ QString::fromStdString(it["category"]), - QDateTime::fromSecsSinceEpoch(it["time"].get()).toLocalTime().toString(), - (it["address"].is_null() ? "" : QString::fromStdString(it["address"])), + it["time"].get(), + (it["address"].is_null() ? "(shielded)" : QString::fromStdString(it["address"])), QString::fromStdString(it["txid"]), it["amount"].get() + fee, - it["confirmations"].get() - ); + it["confirmations"].get() + }; - txdata->push_front(tx); + txdata.push_back(tx); } // Update model data, which updates the table view - transactionsTableModel->setNewData(txdata); + transactionsTableModel->addNewData(txdata); }); } @@ -630,7 +623,7 @@ void RPC::refreshTxStatus(const QString& newOpid) { ); watchingOps.remove(id); - txTimer->start(10 * 1000); + txTimer->start(Utils::updateSpeed); main->ui->statusBar->showMessage(" Tx " % id % " failed", 15 * 1000); main->loadingLabel->setVisible(false); @@ -638,7 +631,7 @@ void RPC::refreshTxStatus(const QString& newOpid) { msg.exec(); } else if (status == "executing") { // If the operation is executing, then watch every second. - txTimer->start(1 * 1000); + txTimer->start(Utils::quickUpdateSpeed); } } } diff --git a/src/transactionitem.cpp b/src/transactionitem.cpp deleted file mode 100644 index ee377ce..0000000 --- a/src/transactionitem.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "transactionitem.h" - -TransactionItem::TransactionItem(const QString& type, const QString& datetime, const QString& address, const QString& txid, - double amount, int confirmations) -{ - this->type = type; - this->datetime = datetime; - this->address = address; - this->txid = txid; - this->amount = amount; - this->confirmations = confirmations; -} diff --git a/src/transactionitem.h b/src/transactionitem.h index be4f4ff..48480c2 100644 --- a/src/transactionitem.h +++ b/src/transactionitem.h @@ -3,18 +3,13 @@ #include "precompiled.h" -class TransactionItem -{ -public: - TransactionItem(const QString& type, const QString& datetime, const QString& address, const QString& txid, - double amount, int confirmations); - - QString type; - QString datetime; - QString address; - QString txid; - double amount; - int confirmations; +struct TransactionItem { + QString type; + unsigned long datetime; + QString address; + QString txid; + double amount; + unsigned long confirmations; }; #endif // TRANSACTIONITEM_H diff --git a/src/txtablemodel.cpp b/src/txtablemodel.cpp index cf82c27..964c785 100644 --- a/src/txtablemodel.cpp +++ b/src/txtablemodel.cpp @@ -11,13 +11,34 @@ TxTableModel::~TxTableModel() { delete modeldata; } -void TxTableModel::setNewData(QList* data) { - delete modeldata; - modeldata = data; +void TxTableModel::prepNewData(int expect) { + newmodeldata = new QList(); + expectedData = expect; +} - dataChanged(index(0, 0), index(modeldata->size()-1, columnCount(index(0,0))-1)); - layoutChanged(); - } +void TxTableModel::addNewData(const QList& data) { + // Make sure we're expecting some data. + Q_ASSERT(expectedData > 0); + + // Add all + std::copy(data.begin(), data.end(), std::back_inserter(*newmodeldata)); + expectedData--; + + if (expectedData == 0) { + delete modeldata; + + modeldata = newmodeldata; + newmodeldata = nullptr; + + // Sort by reverse time + std::sort(modeldata->begin(), modeldata->end(), [=] (auto a, auto b) { + return a.datetime > b.datetime; // reverse sort + }); + + dataChanged(index(0, 0), index(modeldata->size()-1, columnCount(index(0,0))-1)); + layoutChanged(); + } +} int TxTableModel::rowCount(const QModelIndex&) const { @@ -53,7 +74,7 @@ void TxTableModel::setNewData(QList* data) { switch (index.column()) { case 0: return modeldata->at(index.row()).type; case 1: return modeldata->at(index.row()).address; - case 2: return modeldata->at(index.row()).datetime; + case 2: return QDateTime::fromSecsSinceEpoch(modeldata->at(index.row()).datetime).toLocalTime().toString(); case 3: { if (role == Qt::DisplayRole) return Settings::getInstance()->getZECDisplayFormat(modeldata->at(index.row()).amount); diff --git a/src/txtablemodel.h b/src/txtablemodel.h index c461c7d..03bfd78 100644 --- a/src/txtablemodel.h +++ b/src/txtablemodel.h @@ -10,7 +10,8 @@ public: TxTableModel(QObject* parent); ~TxTableModel(); - void setNewData(QList* addresses); + void prepNewData (int expectedData); + void addNewData (const QList& data); QString getTxId(int row); @@ -20,7 +21,10 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role) const; private: - QList* modeldata = nullptr; + QList* modeldata = nullptr; + QList* newmodeldata = nullptr; + int expectedData; + QList headers; }; diff --git a/src/utils.h b/src/utils.h index d6250c9..e11a1fe 100644 --- a/src/utils.h +++ b/src/utils.h @@ -18,7 +18,8 @@ public: static double getDevFee(); static double getTotalFee(); - static const int updateSpeed = 20 * 1000; // 20 sec + static const int updateSpeed = 20 * 1000; // 20 sec + static const int quickUpdateSpeed = 5 * 1000; // 5 sec private: Utils() = delete; }; diff --git a/zcash-qt-wallet.pro b/zcash-qt-wallet.pro index 5130567..522771b 100644 --- a/zcash-qt-wallet.pro +++ b/zcash-qt-wallet.pro @@ -49,7 +49,6 @@ SOURCES += \ src/settings.cpp \ src/sendtab.cpp \ src/txtablemodel.cpp \ - src/transactionitem.cpp \ src/utils.cpp HEADERS += \