From 5564dfccb03cee115e080a157222ec0c787fab8a Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Thu, 14 Mar 2019 10:38:16 -0700 Subject: [PATCH] Prevent deleting utxos and balances while updating --- src/rpc.cpp | 25 +++++++++++++++---------- src/rpc.h | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/rpc.cpp b/src/rpc.cpp index b3db556..a1bb293 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -713,7 +713,7 @@ void RPC::updateUI(bool anyUnconfirmed) { }; // Function to process reply of the listunspent and z_listunspent API calls, used below. -bool RPC::processUnspent(const json& reply) { +bool RPC::processUnspent(const json& reply, QMap* balancesMap, QList* newUtxos) { bool anyUnconfirmed = false; for (auto& it : reply.get()) { QString qsAddr = QString::fromStdString(it["address"]); @@ -722,12 +722,12 @@ bool RPC::processUnspent(const json& reply) { anyUnconfirmed = true; } - utxos->push_back( + newUtxos->push_back( UnspentOutput{ qsAddr, QString::fromStdString(it["txid"]), Settings::getDecimalString(it["amount"].get()), (int)confirmations, it["spendable"].get() }); - (*allBalances)[qsAddr] = (*allBalances)[qsAddr] + it["amount"].get(); + (*balancesMap)[qsAddr] = (*balancesMap)[qsAddr] + it["amount"].get(); } return anyUnconfirmed; }; @@ -754,18 +754,23 @@ void RPC::refreshBalances() { }); // 2. Get the UTXOs - // First, create a new UTXO list, deleting the old one; - delete utxos; - utxos = new QList(); - delete allBalances; - allBalances = new QMap(); + // First, create a new UTXO list. It will be replacing the existing list when everything is processed. + auto newUtxos = new QList(); + auto newBalances = new QMap(); // Call the Transparent and Z unspent APIs serially and then, once they're done, update the UI getTransparentUnspent([=] (json reply) { - auto anyTUnconfirmed = processUnspent(reply); + auto anyTUnconfirmed = processUnspent(reply, newBalances, newUtxos); getZUnspent([=] (json reply) { - auto anyZUnconfirmed = processUnspent(reply); + auto anyZUnconfirmed = processUnspent(reply, newBalances, newUtxos); + + // Swap out the balances and UTXOs + delete allBalances; + delete utxos; + + allBalances = newBalances; + utxos = newUtxos; updateUI(anyTUnconfirmed || anyZUnconfirmed); }); diff --git a/src/rpc.h b/src/rpc.h index 720f286..ec41e70 100644 --- a/src/rpc.h +++ b/src/rpc.h @@ -94,7 +94,7 @@ private: void refreshSentZTrans(); void refreshReceivedZTrans(QList zaddresses); - bool processUnspent (const json& reply); + bool processUnspent (const json& reply, QMap* newBalances, QList* newUtxos); void updateUI (bool anyUnconfirmed); void getInfoThenRefresh(bool force);