aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2011-01-03 14:45:49 -0500
committerGavin Andresen <gavinandresen@gmail.com>2011-01-03 14:45:49 -0500
commit0ded557af65282350509654e4ec7fb48d7903e8d (patch)
tree3128e60c94f71c33b5eff355c71da30f3eb163e7
parent72b9861635dcdb9fd73b84bbb43b3f755bc9f64a (diff)
parentddb68acebe0c41e1958a783c38a272ae11671f86 (diff)
downloadbitcoin-0ded557af65282350509654e4ec7fb48d7903e8d.tar.xz
Merge branch 'listaddresses' of https://github.com/gavinandresen/bitcoin-git
-rw-r--r--main.cpp55
-rw-r--r--main.h2
-rw-r--r--rpc.cpp44
3 files changed, 67 insertions, 34 deletions
diff --git a/main.cpp b/main.cpp
index 8db6c394e8..b7dfd9fabe 100644
--- a/main.cpp
+++ b/main.cpp
@@ -395,9 +395,12 @@ int CWalletTx::GetRequestCount() const
}
void CWalletTx::GetAmounts(int64& nGenerated, list<pair<string, int64> >& listReceived,
- int64& nSent, int64& nFee, string& strSentAccount) const
+ list<pair<string, int64> >& listSent, int64& nFee, string& strSentAccount) const
{
- nGenerated = nSent = nFee = 0;
+ nGenerated = nFee = 0;
+ listReceived.clear();
+ listSent.clear();
+ strSentAccount = strFromAccount;
if (IsCoinBase())
{
@@ -406,24 +409,38 @@ void CWalletTx::GetAmounts(int64& nGenerated, list<pair<string, int64> >& listRe
return;
}
- // Received. Standard client will never generate a send-to-multiple-recipients,
+ // Compute fee:
+ int64 nDebit = GetDebit();
+ if (nDebit > 0) // debit>0 means we signed/sent this transaction
+ {
+ int64 nValueOut = GetValueOut();
+ nFee = nDebit - nValueOut;
+ }
+
+ // Sent/received. Standard client will never generate a send-to-multiple-recipients,
// but non-standard clients might (so return a list of address/amount pairs)
foreach(const CTxOut& txout, vout)
{
+ string address;
+ uint160 hash160;
vector<unsigned char> vchPubKey;
- if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey))
- listReceived.push_back(make_pair(PubKeyToAddress(vchPubKey), txout.nValue));
- }
+ if (ExtractHash160(txout.scriptPubKey, hash160))
+ address = Hash160ToAddress(hash160);
+ else if (ExtractPubKey(txout.scriptPubKey, false, vchPubKey))
+ address = PubKeyToAddress(vchPubKey);
+ else
+ address = " unknown "; // some type of weird non-standard transaction?
- // Sent
- int64 nDebit = GetDebit();
- if (nDebit > 0)
- {
- int64 nValueOut = GetValueOut();
- nFee = nDebit - nValueOut;
- nSent = nValueOut - GetChange();
- strSentAccount = strFromAccount;
+ if (nDebit > 0 && txout.IsChange())
+ continue;
+
+ if (nDebit > 0)
+ listSent.push_back(make_pair(address, txout.nValue));
+
+ if (txout.IsMine())
+ listReceived.push_back(make_pair(address, txout.nValue));
}
+
}
void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, int64& nReceived,
@@ -431,17 +448,19 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, i
{
nGenerated = nReceived = nSent = nFee = 0;
- int64 allGenerated, allSent, allFee;
- allGenerated = allSent = allFee = 0;
+ int64 allGenerated, allFee;
+ allGenerated = allFee = 0;
string strSentAccount;
list<pair<string, int64> > listReceived;
- GetAmounts(allGenerated, listReceived, allSent, allFee, strSentAccount);
+ list<pair<string, int64> > listSent;
+ GetAmounts(allGenerated, listReceived, listSent, allFee, strSentAccount);
if (strAccount == "")
nGenerated = allGenerated;
if (strAccount == strSentAccount)
{
- nSent = allSent;
+ foreach(const PAIRTYPE(string,int64)& s, listSent)
+ nSent += s.second;
nFee = allFee;
}
CRITICAL_BLOCK(cs_mapAddressBook)
diff --git a/main.h b/main.h
index 11a5862cb0..3c7bcb22f0 100644
--- a/main.h
+++ b/main.h
@@ -874,7 +874,7 @@ public:
}
void GetAmounts(int64& nGenerated, list<pair<string /* address */, int64> >& listReceived,
- int64& nSent, int64& nFee, string& strSentAccount) const;
+ list<pair<string /* address */, int64> >& listSent, int64& nFee, string& strSentAccount) const;
void GetAccountAmounts(const string& strAccount, int64& nGenerated, int64& nReceived,
int64& nSent, int64& nFee) const;
diff --git a/rpc.cpp b/rpc.cpp
index 602de73e44..aede2e4c4c 100644
--- a/rpc.cpp
+++ b/rpc.cpp
@@ -859,10 +859,11 @@ Value listreceivedbyaccount(const Array& params, bool fHelp)
void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDepth, Array& ret)
{
- int64 nGenerated, nSent, nFee;
+ int64 nGenerated, nFee;
string strSentAccount;
list<pair<string, int64> > listReceived;
- wtx.GetAmounts(nGenerated, listReceived, nSent, nFee, strSentAccount);
+ list<pair<string, int64> > listSent;
+ wtx.GetAmounts(nGenerated, listReceived, listSent, nFee, strSentAccount);
bool fAllAccounts = (strAccount == string("*"));
@@ -878,15 +879,19 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
}
// Sent
- if ((nSent != 0 || nFee != 0) && (fAllAccounts || strAccount == strSentAccount))
+ if ((!listSent.empty() || nFee != 0) && (fAllAccounts || strAccount == strSentAccount))
{
- Object entry;
- entry.push_back(Pair("account", strSentAccount));
- entry.push_back(Pair("category", "send"));
- entry.push_back(Pair("amount", ValueFromAmount(-nSent)));
- entry.push_back(Pair("fee", ValueFromAmount(-nFee)));
- WalletTxToJSON(wtx, entry);
- ret.push_back(entry);
+ foreach(const PAIRTYPE(string, int64)& s, listSent)
+ {
+ Object entry;
+ entry.push_back(Pair("account", strSentAccount));
+ entry.push_back(Pair("address", s.first));
+ entry.push_back(Pair("category", "send"));
+ entry.push_back(Pair("amount", ValueFromAmount(-s.second)));
+ entry.push_back(Pair("fee", ValueFromAmount(-nFee)));
+ WalletTxToJSON(wtx, entry);
+ ret.push_back(entry);
+ }
}
// Received
@@ -894,15 +899,21 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
CRITICAL_BLOCK(cs_mapAddressBook)
{
foreach(const PAIRTYPE(string, int64)& r, listReceived)
- if (mapAddressBook.count(r.first) && (fAllAccounts || mapAddressBook[r.first] == strAccount))
+ {
+ string account;
+ if (mapAddressBook.count(r.first))
+ account = mapAddressBook[r.first];
+ if (fAllAccounts || (account == strAccount))
{
Object entry;
- entry.push_back(Pair("account", mapAddressBook[r.first]));
+ entry.push_back(Pair("account", account));
+ entry.push_back(Pair("address", r.first));
entry.push_back(Pair("category", "receive"));
entry.push_back(Pair("amount", ValueFromAmount(r.second)));
WalletTxToJSON(wtx, entry);
ret.push_back(entry);
}
+ }
}
}
@@ -1007,11 +1018,14 @@ Value listaccounts(const Array& params, bool fHelp)
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const CWalletTx& wtx = (*it).second;
- int64 nGenerated, nSent, nFee;
+ int64 nGenerated, nFee;
string strSentAccount;
list<pair<string, int64> > listReceived;
- wtx.GetAmounts(nGenerated, listReceived, nSent, nFee, strSentAccount);
- mapAccountBalances[strSentAccount] -= nSent+nFee;
+ list<pair<string, int64> > listSent;
+ wtx.GetAmounts(nGenerated, listReceived, listSent, nFee, strSentAccount);
+ mapAccountBalances[strSentAccount] -= nFee;
+ foreach(const PAIRTYPE(string, int64)& s, listSent)
+ mapAccountBalances[strSentAccount] -= s.second;
if (wtx.GetDepthInMainChain() >= nMinDepth)
{
mapAccountBalances[""] += nGenerated;