From ce0179d1a96413db5e1308d83c1a2694ea1b4fa4 Mon Sep 17 00:00:00 2001 From: woodser Date: Tue, 4 Jun 2019 10:41:42 -0400 Subject: [PATCH] switched structs to use shared_ptrs --- src/daemon/MoneroDaemon.h | 154 ++++++++++++++---------------------- src/wallet/MoneroWallet.cpp | 60 +++++++------- src/wallet/MoneroWallet.h | 114 +++++++------------------- 3 files changed, 119 insertions(+), 209 deletions(-) diff --git a/src/daemon/MoneroDaemon.h b/src/daemon/MoneroDaemon.h index 311118d..8391013 100644 --- a/src/daemon/MoneroDaemon.h +++ b/src/daemon/MoneroDaemon.h @@ -20,6 +20,8 @@ namespace monero { /** * Models a connection to a daemon. + * + * TODO: shared_ptr? */ struct MoneroRpcConnection { string uri; @@ -38,42 +40,42 @@ namespace monero { */ struct MoneroTx { shared_ptr block; - string id; - string version; - bool isCoinbase; - string paymentId; - uint64_t fee; - uint32_t mixin; - bool doNotRelay; - bool isRelayed; - bool isConfirmed; - bool inTxPool; - uint64_t numConfirmations; - uint64_t unlockTime; - uint64_t lastRelayedTimestamp; - uint64_t receivedTimestamp; - bool isDoubleSpend; - string key; - string fullHex; - string prunedHex; - string prunableHex; - string prunableHash; - uint32_t size; - uint32_t weight; + shared_ptr id; + shared_ptr version; + shared_ptr isCoinbase; + shared_ptr paymentId; + shared_ptr fee; + shared_ptr mixin; + shared_ptr doNotRelay; + shared_ptr isRelayed; + shared_ptr isConfirmed; + shared_ptr inTxPool; + shared_ptr numConfirmations; + shared_ptr unlockTime; + shared_ptr lastRelayedTimestamp; + shared_ptr receivedTimestamp; + shared_ptr isDoubleSpend; + shared_ptr key; + shared_ptr fullHex; + shared_ptr prunedHex; + shared_ptr prunableHex; + shared_ptr prunableHash; + shared_ptr size; + shared_ptr weight; vector vins; vector vouts; vector outputIndices; - string metadata; - string commonTxSets; + shared_ptr metadata; + shared_ptr commonTxSets; vector extra; - string rctSignatures; // TODO: implement - string rctSigPrunable; // TODO: implement - bool isKeptByBlock; - bool isFailed; + shared_ptr rctSignatures; // TODO: implement + shared_ptr rctSigPrunable; // TODO: implement + shared_ptr isKeptByBlock; + shared_ptr isFailed; shared_ptr lastFailedHeight; - string lastFailedId; + shared_ptr lastFailedId; shared_ptr maxUsedBlockHeight; - string maxUsedBlockId; + shared_ptr maxUsedBlockId; vector signatures; }; @@ -81,92 +83,52 @@ namespace monero { * Models a Monero block header which contains information about the block. */ struct MoneroBlockHeader { - string id; - uint64_t height; - uint64_t timestamp; - uint64_t size; - uint64_t weight; - uint64_t longTermWeight; - uint64_t depth; - uint64_t difficulty; - uint64_t cumulativeDifficulty; - uint32_t majorVersion; - uint32_t minorVersion; - uint64_t nonce; - string coinbaseTxId; - uint32_t numTxs; - bool orphanStatus; - string prevId; - uint64_t reward; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(id) - KV_SERIALIZE(height) - KV_SERIALIZE(timestamp) - KV_SERIALIZE(size) - KV_SERIALIZE(weight) - KV_SERIALIZE(longTermWeight) - KV_SERIALIZE(depth) - KV_SERIALIZE(difficulty) - KV_SERIALIZE(cumulativeDifficulty) - KV_SERIALIZE(majorVersion) - KV_SERIALIZE(minorVersion) - KV_SERIALIZE(nonce) - KV_SERIALIZE(coinbaseTxId) - KV_SERIALIZE(numTxs) - KV_SERIALIZE(orphanStatus) - KV_SERIALIZE(prevId) - KV_SERIALIZE(reward) - END_KV_SERIALIZE_MAP() + shared_ptr id; + shared_ptr height; + shared_ptr timestamp; + shared_ptr size; + shared_ptr weight; + shared_ptr longTermWeight; + shared_ptr depth; + shared_ptr difficulty; + shared_ptr cumulativeDifficulty; + shared_ptr majorVersion; + shared_ptr minorVersion; + shared_ptr nonce; + shared_ptr coinbaseTxId; + shared_ptr numTxs; + shared_ptr orphanStatus; + shared_ptr prevId; + shared_ptr reward; }; /** * Models a Monero block in the blockchain. */ struct MoneroBlock : public MoneroBlockHeader { - string hex; - MoneroTx coinbaseTx; + shared_ptr hex; + shared_ptr coinbaseTx; vector txs; vector txIds; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(hex) - KV_SERIALIZE(coinbaseTx) - KV_SERIALIZE(txs) - KV_SERIALIZE(txIds) - END_KV_SERIALIZE_MAP() }; /** * Models a Monero key image. */ struct MoneroKeyImage { - string hex; - string signature; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(hex) - KV_SERIALIZE(signature) - END_KV_SERIALIZE_MAP() + shared_ptr hex; + shared_ptr signature; }; /** * Models a Monero transaction output. */ struct MoneroOutput { - MoneroTx* tx; - MoneroKeyImage keyImage; - uint64_t amount; - uint32_t index; + shared_ptr tx; + shared_ptr keyImage; + shared_ptr amount; + shared_ptr index; vector ringOutputIndices; - string stealthPublicKey; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(keyImage) - KV_SERIALIZE(amount) - KV_SERIALIZE(index) - KV_SERIALIZE(ringOutputIndices) - KV_SERIALIZE(stealthPublicKey) - END_KV_SERIALIZE_MAP() + shared_ptr stealthPublicKey; }; } diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index 2232f52..cade5cd 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -69,20 +69,20 @@ namespace monero { // notify listener of block if (this->listener != nullptr) { - MoneroBlock block; - block.height = height; + MoneroBlock block; + block.height = shared_ptr(make_shared(height)); listener->onNewBlock(block); } // notify listeners of sync progress if (syncStartHeight != nullptr && height > *syncStartHeight) { - if (height > *syncEndHeight) *syncEndHeight = height; // increase end height if necessary - uint64_t numBlocksDone = height - *syncStartHeight + 1; - uint64_t numBlocksTotal = *syncEndHeight - *syncStartHeight + 1; - double percentDone = numBlocksDone / (double) numBlocksTotal; - string message = string("Synchronizing"); - if (this->listener != nullptr) this->listener->onSyncProgress(*syncStartHeight, numBlocksDone, numBlocksTotal, percentDone, message); - if (this->syncListener != nullptr) this->syncListener->onSyncProgress(*syncStartHeight, numBlocksDone, numBlocksTotal, percentDone, message); + if (height > *syncEndHeight) *syncEndHeight = height; // increase end height if necessary + uint64_t numBlocksDone = height - *syncStartHeight + 1; + uint64_t numBlocksTotal = *syncEndHeight - *syncStartHeight + 1; + double percentDone = numBlocksDone / (double) numBlocksTotal; + string message = string("Synchronizing"); + if (this->listener != nullptr) this->listener->onSyncProgress(*syncStartHeight, numBlocksDone, numBlocksTotal, percentDone, message); + if (this->syncListener != nullptr) this->syncListener->onSyncProgress(*syncStartHeight, numBlocksDone, numBlocksTotal, percentDone, message); } } @@ -224,8 +224,8 @@ namespace monero { // return indices in subaddress MoneroSubaddress subaddress; cryptonote::subaddress_index cnIndex = *index; - subaddress.accountIndex = cnIndex.major; - subaddress.index = cnIndex.minor; + subaddress.accountIndex = shared_ptr(make_shared(cnIndex.major)); + subaddress.index = shared_ptr(make_shared(cnIndex.minor)); return subaddress; } @@ -333,10 +333,10 @@ namespace monero { vector accounts; for (uint32_t accountIdx = 0; accountIdx < wallet2->get_num_subaddress_accounts(); accountIdx++) { MoneroAccount account; - account.index = accountIdx; - account.primaryAddress = getAddress(accountIdx, 0); - account.balance = wallet2->balance(accountIdx); - account.unlockedBalance = wallet2->unlocked_balance(accountIdx); + account.index = shared_ptr(make_shared(accountIdx)); + account.primaryAddress = shared_ptr(make_shared(getAddress(accountIdx, 0))); + account.balance = shared_ptr(make_shared(wallet2->balance(accountIdx))); + account.unlockedBalance = shared_ptr(make_shared(wallet2->unlocked_balance(accountIdx))); if (includeSubaddresses) account.subaddresses = getSubaddressesAux(accountIdx, vector(), transfers); accounts.push_back(account); } @@ -357,10 +357,10 @@ namespace monero { // build and return account MoneroAccount account; - account.index = accountIdx; - account.primaryAddress = getAddress(accountIdx, 0); - account.balance = wallet2->balance(accountIdx); - account.unlockedBalance = wallet2->unlocked_balance(accountIdx); + account.index = shared_ptr(make_shared(accountIdx)); + account.primaryAddress = shared_ptr(make_shared(getAddress(accountIdx, 0))); + account.balance = shared_ptr(make_shared(wallet2->balance(accountIdx))); + account.unlockedBalance = shared_ptr(make_shared(wallet2->unlocked_balance(accountIdx))); if (includeSubaddresses) account.subaddresses = getSubaddressesAux(accountIdx, vector(), transfers); return account; } @@ -405,18 +405,18 @@ namespace monero { // initialize subaddresses at indices for (uint32_t subaddressIndicesIdx = 0; subaddressIndicesIdx < subaddressIndices.size(); subaddressIndicesIdx++) { MoneroSubaddress subaddress; - subaddress.accountIndex = accountIdx; + subaddress.accountIndex = shared_ptr(make_shared(accountIdx)); uint32_t subaddressIdx = subaddressIndices.at(subaddressIndicesIdx); - subaddress.index = subaddressIdx; - subaddress.address = getAddress(accountIdx, subaddressIdx); + subaddress.index = shared_ptr(make_shared(subaddressIdx)); + subaddress.address = shared_ptr(make_shared(getAddress(accountIdx, subaddressIdx))); auto iter1 = balancePerSubaddress.find(subaddressIdx); - subaddress.balance = iter1 == balancePerSubaddress.end() ? 0 : iter1->second; + subaddress.balance = shared_ptr(make_shared(iter1 == balancePerSubaddress.end() ? 0 : iter1->second)); auto iter2 = unlockedBalancePerSubaddress.find(subaddressIdx); - subaddress.unlockedBalance = iter2 == unlockedBalancePerSubaddress.end() ? 0 : iter2->second.first; - subaddress.numBlocksToUnlock = iter2 == unlockedBalancePerSubaddress.end() ? 0 : iter2->second.second; + subaddress.unlockedBalance = shared_ptr(make_shared(iter2 == unlockedBalancePerSubaddress.end() ? 0 : iter2->second.first)); + subaddress.numBlocksToUnlock = shared_ptr(make_shared(iter2 == unlockedBalancePerSubaddress.end() ? 0 : iter2->second.second)); cryptonote::subaddress_index index = {accountIdx, subaddressIdx}; - subaddress.numUnspentOutputs = count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; }); - subaddress.isUsed = find_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return td.m_subaddr_index == index; }) != transfers.end(); + subaddress.numUnspentOutputs = shared_ptr(make_shared(count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; }))); + subaddress.isUsed = shared_ptr(make_shared(find_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return td.m_subaddr_index == index; }) != transfers.end())); subaddresses.push_back(subaddress); } @@ -475,8 +475,12 @@ namespace monero { // sync wallet wallet2Listener->onSyncStart(syncStartHeight, listener); + + MoneroSyncResult result; - wallet2->refresh(wallet2->is_trusted_daemon(), syncStartHeight, result.numBlocksFetched, result.receivedMoney, true); + result.numBlocksFetched = shared_ptr(new uint64_t()); + result.receivedMoney = shared_ptr(new bool()); + wallet2->refresh(wallet2->is_trusted_daemon(), syncStartHeight, *result.numBlocksFetched, *result.receivedMoney, true); wallet2Listener->onSyncEnd(); return result; } diff --git a/src/wallet/MoneroWallet.h b/src/wallet/MoneroWallet.h index 6c14dc2..598de94 100644 --- a/src/wallet/MoneroWallet.h +++ b/src/wallet/MoneroWallet.h @@ -17,60 +17,38 @@ namespace monero { * Models a result of syncing a wallet. */ struct MoneroSyncResult { - uint64_t numBlocksFetched; - bool receivedMoney; + shared_ptr numBlocksFetched; + shared_ptr receivedMoney; MoneroSyncResult() {} - MoneroSyncResult(const uint64_t numBlocksFetched, const bool receivedMoney) : numBlocksFetched(numBlocksFetched), receivedMoney(receivedMoney) {} + MoneroSyncResult(const shared_ptr numBlocksFetched, const shared_ptr receivedMoney) : numBlocksFetched(numBlocksFetched), receivedMoney(receivedMoney) {} }; /** * Models a Monero subaddress. */ struct MoneroSubaddress { - uint32_t accountIndex; - uint32_t index; - string address; - string label; - uint64_t balance; - uint64_t unlockedBalance; - uint32_t numUnspentOutputs; - bool isUsed; - uint32_t numBlocksToUnlock; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(accountIndex) - KV_SERIALIZE(index) - KV_SERIALIZE(address) - KV_SERIALIZE(label) - KV_SERIALIZE(balance) - KV_SERIALIZE(unlockedBalance) - KV_SERIALIZE(numUnspentOutputs) - KV_SERIALIZE(isUsed) - KV_SERIALIZE(numBlocksToUnlock) - END_KV_SERIALIZE_MAP() + shared_ptr accountIndex; + shared_ptr index; + shared_ptr address; + shared_ptr label; + shared_ptr balance; + shared_ptr unlockedBalance; + shared_ptr numUnspentOutputs; + shared_ptr isUsed; + shared_ptr numBlocksToUnlock; }; /** * Models a Monero account. */ struct MoneroAccount { - uint32_t index; - string primaryAddress; - string label; - uint64_t balance; - uint64_t unlockedBalance; - string tag; + shared_ptr index; + shared_ptr primaryAddress; + shared_ptr label; + shared_ptr balance; + shared_ptr unlockedBalance; + shared_ptr tag; vector subaddresses; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(index) - KV_SERIALIZE(primaryAddress) - KV_SERIALIZE(label) - KV_SERIALIZE(balance) - KV_SERIALIZE(unlockedBalance) - KV_SERIALIZE(tag) - KV_SERIALIZE(subaddresses) - END_KV_SERIALIZE_MAP() }; // forward declarations @@ -81,8 +59,8 @@ namespace monero { * Models an outgoing transfer destination. */ struct MoneroDestination { - string address; - uint64_t amount; + shared_ptr address; + shared_ptr amount; }; /** @@ -90,17 +68,17 @@ namespace monero { */ struct MoneroTransfer { shared_ptr tx; - uint64_t amount; - uint32_t accountIndex; - bool isIncoming; + shared_ptr amount; + shared_ptr accountIndex; + shared_ptr isIncoming; }; /** * Models an incoming transfer of funds to the wallet. */ struct MoneroIncomingTransfer : public MoneroTransfer { - uint32_t subaddressIndex; - string address; + shared_ptr subaddressIndex; + shared_ptr address; }; /** @@ -118,8 +96,8 @@ namespace monero { struct MoneroTxWallet : public MoneroTx { vector incomingTransfers; shared_ptr outgoingTransfer; - uint32_t numSuggestedConfirmations; - string note; + shared_ptr numSuggestedConfirmations; + shared_ptr note; }; /** @@ -131,7 +109,7 @@ namespace monero { shared_ptr amount; shared_ptr accountIndex; shared_ptr isIncoming; - string address; + shared_ptr address; vector addresses; shared_ptr subaddressIndex; vector subaddressIndices; @@ -145,41 +123,7 @@ namespace monero { * * All transactions are returned except those that do not meet the criteria defined in this request. */ - struct MoneroTxRequest { - shared_ptr block; - string id; - string version; - shared_ptr isCoinbase; - string paymentId; - shared_ptr fee; - shared_ptr mixin; - shared_ptr doNotRelay; - shared_ptr isRelayed; - shared_ptr isConfirmed; - shared_ptr inTxPool; - shared_ptr numConfirmations; - shared_ptr unlockTime; - shared_ptr lastRelayedTimestamp; - shared_ptr receivedTimestamp; - shared_ptr isDoubleSpend; - string key; - string fullHex; - string prunedHex; - string prunableHex; - string prunableHash; - shared_ptr size; - shared_ptr weight; - vector vins; - vector vouts; - vector outputIndices; - string metadata; - shared_ptr isFailed; - - vector incomingTransfers; - shared_ptr outgoingTransfer; - shared_ptr numSuggestedConfirmations; - string note; - + struct MoneroTxRequest : public MoneroTxWallet { shared_ptr isOutgoing; shared_ptr isIncoming; vector txIds;