From a4a57df0598e6e663225e78818d6511bc35c085d Mon Sep 17 00:00:00 2001 From: Casey Fleser Date: Sat, 22 Feb 2014 09:05:52 -0600 Subject: [PATCH] add caching to CWalletTx::GetAmounts and undo some other changes --- src/rpcwallet.cpp | 33 +++++++++--------- src/wallet.cpp | 86 ++++++++++++++++------------------------------- src/wallet.h | 2 -- 3 files changed, 47 insertions(+), 74 deletions(-) diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index a73cdfa60..ec83914dc 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -1124,9 +1124,7 @@ Value listaccounts(const Array& params, bool fHelp) if (params.size() > 0) nMinDepth = params[0].get_int(); - std::map mapAccountBalances; - std::list > listCredits; - + map mapAccountBalances; BOOST_FOREACH(const PAIRTYPE(CTxDestination, string)& entry, pwalletMain->mapAddressBook) { if (IsMine(*pwalletMain, entry.first)) // This address belongs to me mapAccountBalances[entry.second] = 0; @@ -1134,18 +1132,23 @@ Value listaccounts(const Array& params, bool fHelp) for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) { - const CWalletTx &wtx = (*it).second; - - if (wtx.GetDepthInMainChain() >= nMinDepth) { - wtx.GetCredits(listCredits); - - BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64)& r, listCredits) { - if (pwalletMain->mapAddressBook.count(r.first)) - mapAccountBalances[pwalletMain->mapAddressBook[r.first]] += r.second; - else - mapAccountBalances[""] += r.second; - } - } + const CWalletTx& wtx = (*it).second; + int64 nFee; + string strSentAccount; + list > listReceived; + list > listSent; + wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount); + mapAccountBalances[strSentAccount] -= nFee; + BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64)& s, listSent) + mapAccountBalances[strSentAccount] -= s.second; + if (wtx.GetDepthInMainChain() >= nMinDepth) + { + BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64)& r, listReceived) + if (pwalletMain->mapAddressBook.count(r.first)) + mapAccountBalances[pwalletMain->mapAddressBook[r.first]] += r.second; + else + mapAccountBalances[""] += r.second; + } } list acentries; diff --git a/src/wallet.cpp b/src/wallet.cpp index c14c0074f..e506ba878 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -646,52 +646,11 @@ int CWalletTx::GetRequestCount() const return nRequests; } -void CWalletTx::GetCredits( - std::list >& listCredits) const -{ - listCredits.clear(); - - if (!IsCoinBase() || GetBlocksToMaturity() <= 0) { - if (!fMineCached) - vfMine.resize(vout.size()); - - for (unsigned int i = 0; i < vout.size(); i++) { - if (!IsSpent(i)) { - const CTxOut &txout = vout[i]; - int64 credit = txout.nValue; - - if (credit) { - CTxDestination address; - bool isMine = false; - - if (!MoneyRange(credit)) { - throw std::runtime_error("CWalletTx::GetCredits() : value out of range"); - } - - if (!fMineCached) { - ExtractDestinationAndMine(*pwallet, txout.scriptPubKey, address, &isMine); - vfMine[i] = isMine; - } - else { - if (vfMine[i]) { - ExtractDestinationAndMine(*pwallet, txout.scriptPubKey, address, &isMine); - } - } - - if (isMine) { - listCredits.push_back(make_pair(address, credit)); - } - } - } - } - - fMineCached = true; - } -} - void CWalletTx::GetAmounts(list >& listReceived, list >& listSent, int64& nFee, string& strSentAccount) const { + CTxDestination address; + nFee = 0; listReceived.clear(); listSent.clear(); @@ -705,30 +664,43 @@ void CWalletTx::GetAmounts(list >& listReceived, nFee = nDebit - nValueOut; } - // Sent/received. - BOOST_FOREACH(const CTxOut& txout, vout) - { - CTxDestination address; - vector vchPubKey; - bool isMine; + if (!fMineCached) + vfMine.resize(vout.size()); - if (!ExtractDestinationAndMine(*pwallet, txout.scriptPubKey, address, &isMine)) - { - printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n", - this->GetHash().ToString().c_str()); + // Sent/received. + for (unsigned int i = 0; i < vout.size(); i++) { + const CTxOut &txout = vout[i]; + bool isMine = false; + bool warnUnkownTX = false; + + if (!fMineCached) { + address = CNoDestination(); + warnUnkownTX = !ExtractDestinationAndMine(*pwallet, txout.scriptPubKey, address, &isMine); + vfMine[i] = isMine; + } + else { + if (vfMine[i]) { + isMine = true; // already know this is ours, just fetch address + address = CNoDestination(); + warnUnkownTX = !ExtractDestination(txout.scriptPubKey, address); + } + } + if (warnUnkownTX) { + printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n", this->GetHash().ToString().c_str()); } - // Don't report 'change' txouts - if (nDebit > 0 && isMine && !pwallet->HasAddress(address)) // equivalent to CWallet::IsChange, but avoids an additional call to Solver - continue; + if (nDebit > 0) { + if (isMine && !pwallet->HasAddress(address)) // Don't report 'change' txouts + continue; - if (nDebit > 0) listSent.push_back(make_pair(address, txout.nValue)); + } if (isMine) listReceived.push_back(make_pair(address, txout.nValue)); } + fMineCached = true; } void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nReceived, diff --git a/src/wallet.h b/src/wallet.h index 5908ffc4a..6fdbdf82f 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -634,8 +634,6 @@ public: return nChangeCached; } - void GetCredits(std::list >& listCredits) const; - void GetAmounts(std::list >& listReceived, std::list >& listSent, int64& nFee, std::string& strSentAccount) const;