Prevent deleting utxos and balances while updating

This commit is contained in:
Aditya Kulkarni
2019-03-14 10:38:16 -07:00
parent c051931c6e
commit 5564dfccb0
2 changed files with 16 additions and 11 deletions

View File

@@ -713,7 +713,7 @@ void RPC::updateUI(bool anyUnconfirmed) {
}; };
// Function to process reply of the listunspent and z_listunspent API calls, used below. // 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<QString, double>* balancesMap, QList<UnspentOutput>* newUtxos) {
bool anyUnconfirmed = false; bool anyUnconfirmed = false;
for (auto& it : reply.get<json::array_t>()) { for (auto& it : reply.get<json::array_t>()) {
QString qsAddr = QString::fromStdString(it["address"]); QString qsAddr = QString::fromStdString(it["address"]);
@@ -722,12 +722,12 @@ bool RPC::processUnspent(const json& reply) {
anyUnconfirmed = true; anyUnconfirmed = true;
} }
utxos->push_back( newUtxos->push_back(
UnspentOutput{ qsAddr, QString::fromStdString(it["txid"]), UnspentOutput{ qsAddr, QString::fromStdString(it["txid"]),
Settings::getDecimalString(it["amount"].get<json::number_float_t>()), Settings::getDecimalString(it["amount"].get<json::number_float_t>()),
(int)confirmations, it["spendable"].get<json::boolean_t>() }); (int)confirmations, it["spendable"].get<json::boolean_t>() });
(*allBalances)[qsAddr] = (*allBalances)[qsAddr] + it["amount"].get<json::number_float_t>(); (*balancesMap)[qsAddr] = (*balancesMap)[qsAddr] + it["amount"].get<json::number_float_t>();
} }
return anyUnconfirmed; return anyUnconfirmed;
}; };
@@ -754,18 +754,23 @@ void RPC::refreshBalances() {
}); });
// 2. Get the UTXOs // 2. Get the UTXOs
// First, create a new UTXO list, deleting the old one; // First, create a new UTXO list. It will be replacing the existing list when everything is processed.
delete utxos; auto newUtxos = new QList<UnspentOutput>();
utxos = new QList<UnspentOutput>(); auto newBalances = new QMap<QString, double>();
delete allBalances;
allBalances = new QMap<QString, double>();
// Call the Transparent and Z unspent APIs serially and then, once they're done, update the UI // Call the Transparent and Z unspent APIs serially and then, once they're done, update the UI
getTransparentUnspent([=] (json reply) { getTransparentUnspent([=] (json reply) {
auto anyTUnconfirmed = processUnspent(reply); auto anyTUnconfirmed = processUnspent(reply, newBalances, newUtxos);
getZUnspent([=] (json reply) { 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); updateUI(anyTUnconfirmed || anyZUnconfirmed);
}); });

View File

@@ -94,7 +94,7 @@ private:
void refreshSentZTrans(); void refreshSentZTrans();
void refreshReceivedZTrans(QList<QString> zaddresses); void refreshReceivedZTrans(QList<QString> zaddresses);
bool processUnspent (const json& reply); bool processUnspent (const json& reply, QMap<QString, double>* newBalances, QList<UnspentOutput>* newUtxos);
void updateUI (bool anyUnconfirmed); void updateUI (bool anyUnconfirmed);
void getInfoThenRefresh(bool force); void getInfoThenRefresh(bool force);