From deff437c1dc3abb8193133e2c86b8d248ae7aa90 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Sat, 26 Oct 2019 19:09:16 -0700 Subject: [PATCH] Show unconfirmed addresses in the balances table --- src/balancestablemodel.cpp | 13 ++++++----- src/balancestablemodel.h | 4 ++-- src/controller.cpp | 34 +++++++++++++++++---------- src/controller.h | 4 ++-- src/datamodel.h | 1 + src/mainwindow.ui | 47 ++++++++++++++++++++------------------ 6 files changed, 59 insertions(+), 44 deletions(-) diff --git a/src/balancestablemodel.cpp b/src/balancestablemodel.cpp index e1d0b2e..353e2e4 100644 --- a/src/balancestablemodel.cpp +++ b/src/balancestablemodel.cpp @@ -14,11 +14,11 @@ void BalancesTableModel::setNewData(const QList zaddrs, const QList(); + delete unspentOutputs; + unspentOutputs = new QList(); // This is a QList deep copy. - *utxos = outputs; + *unspentOutputs = outputs; // Process the address balances into a list delete modeldata; @@ -50,7 +50,7 @@ void BalancesTableModel::setNewData(const QList zaddrs, const QList(modeldata->at(index.row())); - for (auto utxo : *utxos) { - if (utxo.address == addr && !utxo.spendable) { + for (auto unconfirmedOutput : *unspentOutputs) { + if (unconfirmedOutput.address == addr && + (!unconfirmedOutput.spendable || unconfirmedOutput.pending)) { QBrush b; b.setColor(Qt::red); return b; diff --git a/src/balancestablemodel.h b/src/balancestablemodel.h index fe44bb8..6c1782b 100644 --- a/src/balancestablemodel.h +++ b/src/balancestablemodel.h @@ -18,8 +18,8 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role) const; private: - QList>* modeldata = nullptr; - QList* utxos = nullptr; + QList>* modeldata = nullptr; + QList* unspentOutputs = nullptr; bool loading = true; }; diff --git a/src/controller.cpp b/src/controller.cpp index b96b4dd..0161b55 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -254,26 +254,30 @@ void Controller::updateUI(bool anyUnconfirmed) { }; // Function to process reply of the listunspent and z_listunspent API calls, used below. -bool Controller::processUnspent(const json& reply, QMap* balancesMap, QList* newUtxos) { - bool anyUnconfirmed = false; - - auto processFn = [=](const json& array) { +void Controller::processUnspent(const json& reply, QMap* balancesMap, QList* unspentOutputs) { + auto processFn = [=](const json& array) -> bool { for (auto& it : array) { QString qsAddr = QString::fromStdString(it["address"]); int block = it["created_in_block"].get(); QString txid = QString::fromStdString(it["created_in_txid"]); QString amount = Settings::getDecimalString(it["value"].get()); - newUtxos->push_back(UnspentOutput{ qsAddr, txid, amount, block, true }); + bool spendable = it["unconfirmed_spent"].is_null() && it["spent"].is_null(); // TODO: Wait for 4 confirmations + bool pending = !it["unconfirmed_spent"].is_null(); - (*balancesMap)[qsAddr] = (*balancesMap)[qsAddr] + it["value"].get(); - } + qDebug() << "For address" << qsAddr << "spendable, pending" << spendable << ":" << pending; + + unspentOutputs->push_back(UnspentOutput{ qsAddr, txid, amount, block, spendable, pending }); + if (spendable) { + (*balancesMap)[qsAddr] = (*balancesMap)[qsAddr] + it["value"].get(); + } + } }; processFn(reply["unspent_notes"].get()); processFn(reply["utxos"].get()); - - return anyUnconfirmed; + processFn(reply["pending_notes"].get()); + processFn(reply["pending_utxos"].get()); }; void Controller::refreshBalances() { @@ -300,16 +304,22 @@ void Controller::refreshBalances() { // 2. Get the UTXOs // First, create a new UTXO list. It will be replacing the existing list when everything is processed. - auto newUtxos = new QList(); + auto newUnspentOutputs = new QList(); auto newBalances = new QMap(); // Call the Transparent and Z unspent APIs serially and then, once they're done, update the UI zrpc->fetchUnspent([=] (json reply) { - auto anyUnconfirmed = processUnspent(reply, newBalances, newUtxos); + processUnspent(reply, newBalances, newUnspentOutputs); // Swap out the balances and UTXOs model->replaceBalances(newBalances); - model->replaceUTXOs(newUtxos); + model->replaceUTXOs(newUnspentOutputs); + + // Find if any output is not spendable or is pending + bool anyUnconfirmed = std::find_if(newUnspentOutputs->constBegin(), newUnspentOutputs->constEnd(), + [=](const UnspentOutput& u) -> bool { + return !u.spendable || u.pending; + }) != newUnspentOutputs->constEnd(); updateUI(anyUnconfirmed); diff --git a/src/controller.h b/src/controller.h index 811ac18..d896b62 100644 --- a/src/controller.h +++ b/src/controller.h @@ -78,7 +78,7 @@ private: void refreshTransactions(); - bool processUnspent (const json& reply, QMap* newBalances, QList* newUtxos); + void processUnspent (const json& reply, QMap* newBalances, QList* newUnspentOutputs); void updateUI (bool anyUnconfirmed); void getInfoThenRefresh(bool force); @@ -89,7 +89,7 @@ private: BalancesTableModel* balancesTableModel = nullptr; DataModel* model; - LiteInterface* zrpc; + LiteInterface* zrpc; QTimer* timer; QTimer* txTimer; diff --git a/src/datamodel.h b/src/datamodel.h index f69b3d8..00ad43c 100644 --- a/src/datamodel.h +++ b/src/datamodel.h @@ -10,6 +10,7 @@ struct UnspentOutput { QString amount; int blockCreated; bool spendable; + bool pending; }; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index b578c02..0b03762 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -6,8 +6,8 @@ 0 0 - 968 - 616 + 1274 + 779 @@ -22,7 +22,7 @@ - 2 + 0 @@ -148,26 +148,13 @@ - - - - Qt::Vertical - - - - 20 - 40 - - - - color:red; - Your node is still syncing, balances may not be updated + Your node is still syncing, balances may not be updated. true @@ -186,10 +173,26 @@ color: red; - Some transactions are not yet confirmed + Some transactions are not yet confirmed. Balances may change. + + + true + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -359,8 +362,8 @@ 0 0 - 920 - 301 + 1162 + 344 @@ -1051,8 +1054,8 @@ 0 0 - 968 - 22 + 1274 + 39