add caching to CWalletTx::GetAmounts and undo some other changes

This commit is contained in:
Casey Fleser 2014-02-22 09:05:52 -06:00
parent c4cc936890
commit a4a57df059
3 changed files with 47 additions and 74 deletions

View File

@ -1124,9 +1124,7 @@ Value listaccounts(const Array& params, bool fHelp)
if (params.size() > 0)
nMinDepth = params[0].get_int();
std::map<std::string, int64> mapAccountBalances;
std::list<std::pair<CTxDestination, int64> > listCredits;
map<string, int64> 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<uint256, CWalletTx>::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<pair<CTxDestination, int64> > listReceived;
list<pair<CTxDestination, int64> > 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<CAccountingEntry> acentries;

View File

@ -646,52 +646,11 @@ int CWalletTx::GetRequestCount() const
return nRequests;
}
void CWalletTx::GetCredits(
std::list<std::pair<CTxDestination, int64> >& 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<pair<CTxDestination, int64> >& listReceived,
list<pair<CTxDestination, int64> >& listSent, int64& nFee, string& strSentAccount) const
{
CTxDestination address;
nFee = 0;
listReceived.clear();
listSent.clear();
@ -705,30 +664,43 @@ void CWalletTx::GetAmounts(list<pair<CTxDestination, int64> >& listReceived,
nFee = nDebit - nValueOut;
}
// Sent/received.
BOOST_FOREACH(const CTxOut& txout, vout)
{
CTxDestination address;
vector<unsigned char> 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,

View File

@ -634,8 +634,6 @@ public:
return nChangeCached;
}
void GetCredits(std::list<std::pair<CTxDestination, int64> >& listCredits) const;
void GetAmounts(std::list<std::pair<CTxDestination, int64> >& listReceived,
std::list<std::pair<CTxDestination, int64> >& listSent, int64& nFee, std::string& strSentAccount) const;