From 22dfd7359863217eb8caef75084cfa8fa8e1d8fb Mon Sep 17 00:00:00 2001 From: coderrr Date: Wed, 1 Aug 2012 12:48:42 -0400 Subject: Add address groupings RPC from the coincontrol patches. Signed-off-by: Gregory Maxwell --- src/wallet.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) (limited to 'src/wallet.cpp') diff --git a/src/wallet.cpp b/src/wallet.cpp index 07a5047cef..87792e50ee 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -1620,6 +1620,129 @@ int64 CWallet::GetOldestKeyPoolTime() return keypool.nTime; } +std::map CWallet::GetAddressBalances() +{ + map 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 (int i = 0; i < pcoin->vout.size(); i++) + { + if (!IsMine(pcoin->vout[i])) + continue; + + int64 n = pcoin->IsSpent(i) ? 0 : pcoin->vout[i].nValue; + + string addr = pcoin->GetAddressOfTxOut(i); + if (!balances.count(addr)) + balances[addr] = 0; + balances[addr] += n; + } + } + } + + return balances; +} + +set< set > CWallet::GetAddressGroupings() +{ + set< set > groupings; + set grouping; + + 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; + + if (pcoin->vin.size() > 0 && IsMine(pcoin->vin[0])) + { + // group all input addresses with each other + BOOST_FOREACH(CTxIn txin, pcoin->vin) + grouping.insert(mapWallet[txin.prevout.hash].GetAddressOfTxOut(txin.prevout.n)); + + // group change with input addresses + BOOST_FOREACH(CTxOut txout, pcoin->vout) + if (IsChange(txout)) + { + CWalletTx tx = mapWallet[pcoin->vin[0].prevout.hash]; + string addr = tx.GetAddressOfTxOut(pcoin->vin[0].prevout.n); + CTxDestination txoutAddr; + ExtractDestination(txout.scriptPubKey, txoutAddr); + grouping.insert(CBitcoinAddress(txoutAddr).ToString()); + } + groupings.insert(grouping); + grouping.clear(); + } + + // group lone addrs by themselves + for (int i = 0; i < pcoin->vout.size(); i++) + if (IsMine(pcoin->vout[i])) + { + grouping.insert(pcoin->GetAddressOfTxOut(i)); + groupings.insert(grouping); + grouping.clear(); + } + } + + set< set* > uniqueGroupings; // a set of pointers to groups of addresses + map< string, set* > setmap; // map addresses to the unique group containing it + BOOST_FOREACH(set grouping, groupings) + { + // make a set of all the groups hit by this new group + set< set* > hits; + map< string, set* >::iterator it; + BOOST_FOREACH(string 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* merged = new set(grouping); + BOOST_FOREACH(set* hit, hits) + { + merged->insert(hit->begin(), hit->end()); + uniqueGroupings.erase(hit); + delete hit; + } + uniqueGroupings.insert(merged); + + // update setmap + BOOST_FOREACH(string element, *merged) + setmap[element] = merged; + } + + set< set > ret; + BOOST_FOREACH(set* uniqueGrouping, uniqueGroupings) + { + ret.insert(*uniqueGrouping); + delete uniqueGrouping; + } + + return ret; +} + CPubKey CReserveKey::GetReservedKey() { if (nIndex == -1) -- cgit v1.2.3 From b1093efa833376a7883deb0cbcddd0aed364de84 Mon Sep 17 00:00:00 2001 From: Gregory Maxwell Date: Mon, 20 Aug 2012 13:43:33 -0400 Subject: Change CWallet addressgrouping to use CTxDestination instead of strings. This is cleanup for the listaddressgroupings code. Also add some real help text. --- src/wallet.cpp | 68 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'src/wallet.cpp') diff --git a/src/wallet.cpp b/src/wallet.cpp index 87792e50ee..a5a4e7e431 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -1620,9 +1620,9 @@ int64 CWallet::GetOldestKeyPoolTime() return keypool.nTime; } -std::map CWallet::GetAddressBalances() +std::map CWallet::GetAddressBalances() { - map balances; + map balances; { LOCK(cs_wallet); @@ -1640,14 +1640,16 @@ std::map CWallet::GetAddressBalances() if (nDepth < (pcoin->IsFromMe() ? 0 : 1)) continue; - for (int i = 0; i < pcoin->vout.size(); i++) + 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; - string addr = pcoin->GetAddressOfTxOut(i); if (!balances.count(addr)) balances[addr] = 0; balances[addr] += n; @@ -1658,69 +1660,67 @@ std::map CWallet::GetAddressBalances() return balances; } -set< set > CWallet::GetAddressGroupings() +set< set > CWallet::GetAddressGroupings() { - set< set > groupings; - set grouping; + set< set > groupings; + set grouping; 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; - if (pcoin->vin.size() > 0 && IsMine(pcoin->vin[0])) { // group all input addresses with each other BOOST_FOREACH(CTxIn txin, pcoin->vin) - grouping.insert(mapWallet[txin.prevout.hash].GetAddressOfTxOut(txin.prevout.n)); + { + 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]; - string addr = tx.GetAddressOfTxOut(pcoin->vin[0].prevout.n); CTxDestination txoutAddr; - ExtractDestination(txout.scriptPubKey, txoutAddr); - grouping.insert(CBitcoinAddress(txoutAddr).ToString()); + if(!ExtractDestination(txout.scriptPubKey, txoutAddr)) + continue; + grouping.insert(txoutAddr); } groupings.insert(grouping); grouping.clear(); } // group lone addrs by themselves - for (int i = 0; i < pcoin->vout.size(); i++) + for (unsigned int i = 0; i < pcoin->vout.size(); i++) if (IsMine(pcoin->vout[i])) { - grouping.insert(pcoin->GetAddressOfTxOut(i)); + CTxDestination address; + if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address)) + continue; + grouping.insert(address); groupings.insert(grouping); grouping.clear(); } } - set< set* > uniqueGroupings; // a set of pointers to groups of addresses - map< string, set* > setmap; // map addresses to the unique group containing it - BOOST_FOREACH(set grouping, groupings) + set< set* > uniqueGroupings; // a set of pointers to groups of addresses + map< CTxDestination, set* > setmap; // map addresses to the unique group containing it + BOOST_FOREACH(set grouping, groupings) { // make a set of all the groups hit by this new group - set< set* > hits; - map< string, set* >::iterator it; - BOOST_FOREACH(string address, grouping) + set< set* > hits; + map< CTxDestination, set* >::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* merged = new set(grouping); - BOOST_FOREACH(set* hit, hits) + set* merged = new set(grouping); + BOOST_FOREACH(set* hit, hits) { merged->insert(hit->begin(), hit->end()); uniqueGroupings.erase(hit); @@ -1729,12 +1729,12 @@ set< set > CWallet::GetAddressGroupings() uniqueGroupings.insert(merged); // update setmap - BOOST_FOREACH(string element, *merged) + BOOST_FOREACH(CTxDestination element, *merged) setmap[element] = merged; } - set< set > ret; - BOOST_FOREACH(set* uniqueGrouping, uniqueGroupings) + set< set > ret; + BOOST_FOREACH(set* uniqueGrouping, uniqueGroupings) { ret.insert(*uniqueGrouping); delete uniqueGrouping; -- cgit v1.2.3