Browse Source

Reconcile getbalance and listaccounts 0 in the shared-wallet case

If you copied your wallet and used it on two different machines, the balance reported by getbalance and the sum(listaccounts) could disagree, because you might receive payments for an address that is in your wallet but not your address book.  Now all such transactions are credited to the default empty-string account.
0.8
Gavin Andresen 14 years ago
parent
commit
d9574c2f14
  1. 3
      init.cpp
  2. 20
      main.cpp
  3. 8
      rpc.cpp

3
init.cpp

@ -346,6 +346,9 @@ bool AppInit2(int argc, char* argv[]) @@ -346,6 +346,9 @@ bool AppInit2(int argc, char* argv[])
return false;
}
if (GetBoolArg("-rescan"))
ScanForWalletTransactions(pindexGenesisBlock);
// Add wallet transactions that aren't already in a block to mapTransactions
ReacceptWalletTransactions();

20
main.cpp

@ -442,8 +442,13 @@ void CWalletTx::GetAmounts(int64& nGenerated, list<pair<string, int64> >& listRe @@ -442,8 +442,13 @@ void CWalletTx::GetAmounts(int64& nGenerated, list<pair<string, int64> >& listRe
else if (ExtractPubKey(txout.scriptPubKey, false, vchPubKey))
address = PubKeyToAddress(vchPubKey);
else
address = " unknown "; // some type of weird non-standard transaction?
{
printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
this->GetHash().ToString().c_str());
address = " unknown ";
}
// Don't report 'change' txouts
if (nDebit > 0 && txout.IsChange())
continue;
@ -479,8 +484,19 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, i @@ -479,8 +484,19 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, i
CRITICAL_BLOCK(cs_mapAddressBook)
{
foreach(const PAIRTYPE(string,int64)& r, listReceived)
if (mapAddressBook.count(r.first) && mapAddressBook[r.first] == strAccount)
{
if (mapAddressBook.count(r.first))
{
if (mapAddressBook[r.first] == strAccount)
{
nReceived += r.second;
}
}
else if (strAccount.empty())
{
nReceived += r.second;
}
}
}
}

8
rpc.cpp

@ -626,6 +626,7 @@ Value getbalance(const Array& params, bool fHelp) @@ -626,6 +626,7 @@ Value getbalance(const Array& params, bool fHelp)
// (GetBalance() sums up all unspent TxOuts)
// getbalance and getbalance '*' should always return the same number.
int64 nBalance = 0;
vector<string> vAccounts;
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const CWalletTx& wtx = (*it).second;
@ -636,12 +637,17 @@ Value getbalance(const Array& params, bool fHelp) @@ -636,12 +637,17 @@ Value getbalance(const Array& params, bool fHelp)
list<pair<string, int64> > listSent;
wtx.GetAmounts(allGenerated, listReceived, listSent, allFee, strSentAccount);
foreach(const PAIRTYPE(string,int64)& r, listReceived)
{
nBalance += r.second;
if (!count(vAccounts.begin(), vAccounts.end(), r.first))
vAccounts.push_back(r.first);
}
foreach(const PAIRTYPE(string,int64)& r, listSent)
nBalance -= r.second;
nBalance -= allFee;
nBalance += allGenerated;
}
printf("Found %d accounts\n", vAccounts.size());
return (double)nBalance / (double)COIN;
}
@ -1080,6 +1086,8 @@ Value listaccounts(const Array& params, bool fHelp) @@ -1080,6 +1086,8 @@ Value listaccounts(const Array& params, bool fHelp)
foreach(const PAIRTYPE(string, int64)& r, listReceived)
if (mapAddressBook.count(r.first))
mapAccountBalances[mapAddressBook[r.first]] += r.second;
else
mapAccountBalances[""] += r.second;
}
}
}

Loading…
Cancel
Save