diff options
author | Gregory Maxwell <greg@xiph.org> | 2012-08-24 00:46:24 -0700 |
---|---|---|
committer | Gregory Maxwell <greg@xiph.org> | 2012-08-24 00:46:24 -0700 |
commit | c68c4bc7a45529735c868428632906d3c20a175a (patch) | |
tree | 6edd9d67279c0c15729c752c12794cf34864c930 /src/wallet.cpp | |
parent | bdab0cf58c2ac1b90ba2723e8f9ddc1817b3d811 (diff) | |
parent | b1093efa833376a7883deb0cbcddd0aed364de84 (diff) |
Merge pull request #1672 from gmaxwell/filter_listunspent
Listunspent txout address filtering and listaddressgroupings
Diffstat (limited to 'src/wallet.cpp')
-rw-r--r-- | src/wallet.cpp | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/wallet.cpp b/src/wallet.cpp index 5ca501b1af..dc019d4924 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -1608,6 +1608,129 @@ int64 CWallet::GetOldestKeyPoolTime() return keypool.nTime; } +std::map<CTxDestination, int64> CWallet::GetAddressBalances() +{ + map<CTxDestination, int64> balances; + + { + LOCK(cs_wallet); + BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet) + { + CWalletTx *pcoin = &walletEntry.second; + + if (!pcoin->IsFinal() || !pcoin->IsConfirmed()) + continue; + + if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0) + continue; + + int nDepth = pcoin->GetDepthInMainChain(); + if (nDepth < (pcoin->IsFromMe() ? 0 : 1)) + continue; + + for (unsigned int i = 0; i < pcoin->vout.size(); i++) + { + CTxDestination addr; + if (!IsMine(pcoin->vout[i])) + continue; + if(!ExtractDestination(pcoin->vout[i].scriptPubKey, addr)) + continue; + + int64 n = pcoin->IsSpent(i) ? 0 : pcoin->vout[i].nValue; + + if (!balances.count(addr)) + balances[addr] = 0; + balances[addr] += n; + } + } + } + + return balances; +} + +set< set<CTxDestination> > CWallet::GetAddressGroupings() +{ + set< set<CTxDestination> > groupings; + set<CTxDestination> grouping; + + BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet) + { + CWalletTx *pcoin = &walletEntry.second; + + if (pcoin->vin.size() > 0 && IsMine(pcoin->vin[0])) + { + // group all input addresses with each other + BOOST_FOREACH(CTxIn txin, pcoin->vin) + { + CTxDestination address; + if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address)) + continue; + grouping.insert(address); + } + + // group change with input addresses + BOOST_FOREACH(CTxOut txout, pcoin->vout) + if (IsChange(txout)) + { + CWalletTx tx = mapWallet[pcoin->vin[0].prevout.hash]; + CTxDestination txoutAddr; + if(!ExtractDestination(txout.scriptPubKey, txoutAddr)) + continue; + grouping.insert(txoutAddr); + } + groupings.insert(grouping); + grouping.clear(); + } + + // group lone addrs by themselves + for (unsigned int i = 0; i < pcoin->vout.size(); i++) + if (IsMine(pcoin->vout[i])) + { + CTxDestination address; + if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address)) + continue; + grouping.insert(address); + groupings.insert(grouping); + grouping.clear(); + } + } + + set< set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses + map< CTxDestination, set<CTxDestination>* > setmap; // map addresses to the unique group containing it + BOOST_FOREACH(set<CTxDestination> grouping, groupings) + { + // make a set of all the groups hit by this new group + set< set<CTxDestination>* > hits; + map< CTxDestination, set<CTxDestination>* >::iterator it; + BOOST_FOREACH(CTxDestination address, grouping) + if ((it = setmap.find(address)) != setmap.end()) + hits.insert((*it).second); + + // merge all hit groups into a new single group and delete old groups + set<CTxDestination>* merged = new set<CTxDestination>(grouping); + BOOST_FOREACH(set<CTxDestination>* hit, hits) + { + merged->insert(hit->begin(), hit->end()); + uniqueGroupings.erase(hit); + delete hit; + } + uniqueGroupings.insert(merged); + + // update setmap + BOOST_FOREACH(CTxDestination element, *merged) + setmap[element] = merged; + } + + set< set<CTxDestination> > ret; + BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings) + { + ret.insert(*uniqueGrouping); + delete uniqueGrouping; + } + + return ret; +} + CPubKey CReserveKey::GetReservedKey() { if (nIndex == -1) |