1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include "ui_interface.h"
15 //////////////////////////////////////////////////////////////////////////////
20 struct CompareValueOnly
22 bool operator()(const pair<int64, pair<const CWalletTx*, unsigned int> >& t1,
23 const pair<int64, pair<const CWalletTx*, unsigned int> >& t2) const
25 return t1.first < t2.first;
29 CPubKey CWallet::GenerateNewKey()
31 bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
35 key.MakeNewKey(fCompressed);
37 // Compressed public keys were introduced in version 0.6.0
39 SetMinVersion(FEATURE_COMPRPUBKEY);
42 throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
43 return key.GetPubKey();
46 bool CWallet::AddKey(const CKey& key)
48 if (!CCryptoKeyStore::AddKey(key))
53 return CWalletDB(strWalletFile).WriteKey(key.GetPubKey(), key.GetPrivKey());
57 bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector<unsigned char> &vchCryptedSecret)
59 if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
65 if (pwalletdbEncryption)
66 return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret);
68 return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret);
73 bool CWallet::AddCScript(const CScript& redeemScript)
75 if (!CCryptoKeyStore::AddCScript(redeemScript))
79 return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
82 bool CWallet::Unlock(const SecureString& strWalletPassphrase)
88 CKeyingMaterial vMasterKey;
92 BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
94 if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
96 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
98 if (CCryptoKeyStore::Unlock(vMasterKey))
105 bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
107 bool fWasLocked = IsLocked();
114 CKeyingMaterial vMasterKey;
115 BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
117 if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
119 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
121 if (CCryptoKeyStore::Unlock(vMasterKey))
123 int64 nStartTime = GetTimeMillis();
124 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
125 pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
127 nStartTime = GetTimeMillis();
128 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
129 pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
131 if (pMasterKey.second.nDeriveIterations < 25000)
132 pMasterKey.second.nDeriveIterations = 25000;
134 printf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
136 if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
138 if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
140 CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second);
151 void CWallet::SetBestChain(const CBlockLocator& loc)
153 CWalletDB walletdb(strWalletFile);
154 walletdb.WriteBestBlock(loc);
157 // This class implements an addrIncoming entry that causes pre-0.4
158 // clients to crash on startup if reading a private-key-encrypted wallet.
159 class CCorruptAddress
164 if (nType & SER_DISK)
169 bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
171 if (nWalletVersion >= nVersion)
174 // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
175 if (fExplicit && nVersion > nWalletMaxVersion)
176 nVersion = FEATURE_LATEST;
178 nWalletVersion = nVersion;
180 if (nVersion > nWalletMaxVersion)
181 nWalletMaxVersion = nVersion;
185 CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
186 if (nWalletVersion >= 40000)
188 // Versions prior to 0.4.0 did not support the "minversion" record.
189 // Use a CCorruptAddress to make them crash instead.
190 CCorruptAddress corruptAddress;
191 pwalletdb->WriteSetting("addrIncoming", corruptAddress);
193 if (nWalletVersion > 40000)
194 pwalletdb->WriteMinVersion(nWalletVersion);
202 bool CWallet::SetMaxVersion(int nVersion)
204 // cannot downgrade below current version
205 if (nWalletVersion > nVersion)
208 nWalletMaxVersion = nVersion;
213 bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
218 CKeyingMaterial vMasterKey;
219 RandAddSeedPerfmon();
221 vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
222 RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
224 CMasterKey kMasterKey;
226 RandAddSeedPerfmon();
227 kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
228 RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
231 int64 nStartTime = GetTimeMillis();
232 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
233 kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime));
235 nStartTime = GetTimeMillis();
236 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
237 kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
239 if (kMasterKey.nDeriveIterations < 25000)
240 kMasterKey.nDeriveIterations = 25000;
242 printf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
244 if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
246 if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey))
251 mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
254 pwalletdbEncryption = new CWalletDB(strWalletFile);
255 if (!pwalletdbEncryption->TxnBegin())
257 pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
260 if (!EncryptKeys(vMasterKey))
263 pwalletdbEncryption->TxnAbort();
264 exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet.
267 // Encryption was introduced in version 0.4.0
268 SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
272 if (!pwalletdbEncryption->TxnCommit())
273 exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet.
275 delete pwalletdbEncryption;
276 pwalletdbEncryption = NULL;
280 Unlock(strWalletPassphrase);
284 // Need to completely rewrite the wallet file; if we don't, bdb might keep
285 // bits of the unencrypted private key in slack space in the database file.
286 CDB::Rewrite(strWalletFile);
289 NotifyStatusChanged(this);
294 int64 CWallet::IncOrderPosNext()
296 int64 nRet = nOrderPosNext;
297 CWalletDB(strWalletFile).WriteOrderPosNext(++nOrderPosNext);
301 CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount)
303 CWalletDB walletdb(strWalletFile);
305 // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap.
308 // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry
309 // would make this much faster for applications that do this a lot.
310 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
312 CWalletTx* wtx = &((*it).second);
313 txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0)));
316 walletdb.ListAccountCreditDebit(strAccount, acentries);
317 BOOST_FOREACH(CAccountingEntry& entry, acentries)
319 txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry)));
325 void CWallet::WalletUpdateSpent(const CTransaction &tx)
327 // Anytime a signature is successfully verified, it's proof the outpoint is spent.
328 // Update the wallet spent flag if it doesn't know due to wallet.dat being
329 // restored from backup or the user making copies of wallet.dat.
332 BOOST_FOREACH(const CTxIn& txin, tx.vin)
334 map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
335 if (mi != mapWallet.end())
337 CWalletTx& wtx = (*mi).second;
338 if (txin.prevout.n >= wtx.vout.size())
339 printf("WalletUpdateSpent: bad wtx %s\n", wtx.GetHash().ToString().c_str());
340 else if (!wtx.IsSpent(txin.prevout.n) && IsMine(wtx.vout[txin.prevout.n]))
342 printf("WalletUpdateSpent found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
343 wtx.MarkSpent(txin.prevout.n);
345 NotifyTransactionChanged(this, txin.prevout.hash, CT_UPDATED);
352 void CWallet::MarkDirty()
356 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
357 item.second.MarkDirty();
361 bool CWallet::AddToWallet(const CWalletTx& wtxIn)
363 uint256 hash = wtxIn.GetHash();
366 // Inserts only if not already there, returns tx inserted or tx found
367 pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
368 CWalletTx& wtx = (*ret.first).second;
369 wtx.BindWallet(this);
370 bool fInsertedNew = ret.second;
373 wtx.nTimeReceived = GetAdjustedTime();
374 wtx.nOrderPos = IncOrderPosNext();
376 wtx.nTimeSmart = wtx.nTimeReceived;
377 if (wtxIn.hashBlock != 0)
379 if (mapBlockIndex.count(wtxIn.hashBlock))
381 unsigned int latestNow = wtx.nTimeReceived;
382 unsigned int latestEntry = 0;
384 // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
385 int64 latestTolerated = latestNow + 300;
386 std::list<CAccountingEntry> acentries;
387 TxItems txOrdered = OrderedTxItems(acentries);
388 for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
390 CWalletTx *const pwtx = (*it).second.first;
393 CAccountingEntry *const pacentry = (*it).second.second;
397 nSmartTime = pwtx->nTimeSmart;
399 nSmartTime = pwtx->nTimeReceived;
402 nSmartTime = pacentry->nTime;
403 if (nSmartTime <= latestTolerated)
405 latestEntry = nSmartTime;
406 if (nSmartTime > latestNow)
407 latestNow = nSmartTime;
413 unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime;
414 wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
417 printf("AddToWallet() : found %s in block %s not in index\n",
418 wtxIn.GetHash().ToString().substr(0,10).c_str(),
419 wtxIn.hashBlock.ToString().c_str());
423 bool fUpdated = false;
427 if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
429 wtx.hashBlock = wtxIn.hashBlock;
432 if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
434 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
435 wtx.nIndex = wtxIn.nIndex;
438 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
440 wtx.fFromMe = wtxIn.fFromMe;
443 fUpdated |= wtx.UpdateSpent(wtxIn.vfSpent);
447 printf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().substr(0,10).c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
450 if (fInsertedNew || fUpdated)
451 if (!wtx.WriteToDisk())
454 // If default receiving address gets used, replace it with a new one
455 CScript scriptDefaultKey;
456 scriptDefaultKey.SetDestination(vchDefaultKey.GetID());
457 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
459 if (txout.scriptPubKey == scriptDefaultKey)
461 CPubKey newDefaultKey;
462 if (GetKeyFromPool(newDefaultKey, false))
464 SetDefaultKey(newDefaultKey);
465 SetAddressBookName(vchDefaultKey.GetID(), "");
470 // since AddToWallet is called directly for self-originating transactions, check for consumption of own coins
471 WalletUpdateSpent(wtx);
473 // Notify UI of new or updated transaction
474 NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
479 // Add a transaction to the wallet, or update it.
480 // pblock is optional, but should be provided if the transaction is known to be in a block.
481 // If fUpdate is true, existing transactions will be updated.
482 bool CWallet::AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate, bool fFindBlock)
486 bool fExisted = mapWallet.count(hash);
487 if (fExisted && !fUpdate) return false;
488 if (fExisted || IsMine(tx) || IsFromMe(tx))
490 CWalletTx wtx(this,tx);
491 // Get merkle branch if transaction was found in a block
493 wtx.SetMerkleBranch(pblock);
494 return AddToWallet(wtx);
497 WalletUpdateSpent(tx);
502 bool CWallet::EraseFromWallet(uint256 hash)
508 if (mapWallet.erase(hash))
509 CWalletDB(strWalletFile).EraseTx(hash);
515 bool CWallet::IsMine(const CTxIn &txin) const
519 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
520 if (mi != mapWallet.end())
522 const CWalletTx& prev = (*mi).second;
523 if (txin.prevout.n < prev.vout.size())
524 if (IsMine(prev.vout[txin.prevout.n]))
531 int64 CWallet::GetDebit(const CTxIn &txin) const
535 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
536 if (mi != mapWallet.end())
538 const CWalletTx& prev = (*mi).second;
539 if (txin.prevout.n < prev.vout.size())
540 if (IsMine(prev.vout[txin.prevout.n]))
541 return prev.vout[txin.prevout.n].nValue;
547 bool CWallet::IsChange(const CTxOut& txout) const
549 CTxDestination address;
551 // TODO: fix handling of 'change' outputs. The assumption is that any
552 // payment to a TX_PUBKEYHASH that is mine but isn't in the address book
553 // is change. That assumption is likely to break when we implement multisignature
554 // wallets that return change back into a multi-signature-protected address;
555 // a better way of identifying which outputs are 'the send' and which are
556 // 'the change' will need to be implemented (maybe extend CWalletTx to remember
557 // which output, if any, was change).
558 if (ExtractDestination(txout.scriptPubKey, address) && ::IsMine(*this, address))
561 if (!mapAddressBook.count(address))
567 int64 CWalletTx::GetTxTime() const
569 int64 n = nTimeSmart;
570 return n ? n : nTimeReceived;
573 int CWalletTx::GetRequestCount() const
575 // Returns -1 if it wasn't being tracked
578 LOCK(pwallet->cs_wallet);
584 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
585 if (mi != pwallet->mapRequestCount.end())
586 nRequests = (*mi).second;
591 // Did anyone request this transaction?
592 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
593 if (mi != pwallet->mapRequestCount.end())
595 nRequests = (*mi).second;
597 // How about the block it's in?
598 if (nRequests == 0 && hashBlock != 0)
600 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
601 if (mi != pwallet->mapRequestCount.end())
602 nRequests = (*mi).second;
604 nRequests = 1; // If it's in someone else's block it must have got out
612 void CWalletTx::GetAmounts(list<pair<CTxDestination, int64> >& listReceived,
613 list<pair<CTxDestination, int64> >& listSent, int64& nFee, string& strSentAccount) const
616 listReceived.clear();
618 strSentAccount = strFromAccount;
621 int64 nDebit = GetDebit();
622 if (nDebit > 0) // debit>0 means we signed/sent this transaction
624 int64 nValueOut = GetValueOut();
625 nFee = nDebit - nValueOut;
629 BOOST_FOREACH(const CTxOut& txout, vout)
631 CTxDestination address;
632 vector<unsigned char> vchPubKey;
633 if (!ExtractDestination(txout.scriptPubKey, address))
635 printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
636 this->GetHash().ToString().c_str());
639 // Don't report 'change' txouts
640 if (nDebit > 0 && pwallet->IsChange(txout))
644 listSent.push_back(make_pair(address, txout.nValue));
646 if (pwallet->IsMine(txout))
647 listReceived.push_back(make_pair(address, txout.nValue));
652 void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nReceived,
653 int64& nSent, int64& nFee) const
655 nReceived = nSent = nFee = 0;
658 string strSentAccount;
659 list<pair<CTxDestination, int64> > listReceived;
660 list<pair<CTxDestination, int64> > listSent;
661 GetAmounts(listReceived, listSent, allFee, strSentAccount);
663 if (strAccount == strSentAccount)
665 BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& s, listSent)
670 LOCK(pwallet->cs_wallet);
671 BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& r, listReceived)
673 if (pwallet->mapAddressBook.count(r.first))
675 map<CTxDestination, string>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
676 if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount)
677 nReceived += r.second;
679 else if (strAccount.empty())
681 nReceived += r.second;
687 void CWalletTx::AddSupportingTransactions()
691 const int COPY_DEPTH = 3;
692 if (SetMerkleBranch() < COPY_DEPTH)
694 vector<uint256> vWorkQueue;
695 BOOST_FOREACH(const CTxIn& txin, vin)
696 vWorkQueue.push_back(txin.prevout.hash);
699 LOCK(pwallet->cs_wallet);
700 map<uint256, const CMerkleTx*> mapWalletPrev;
701 set<uint256> setAlreadyDone;
702 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
704 uint256 hash = vWorkQueue[i];
705 if (setAlreadyDone.count(hash))
707 setAlreadyDone.insert(hash);
710 map<uint256, CWalletTx>::const_iterator mi = pwallet->mapWallet.find(hash);
711 if (mi != pwallet->mapWallet.end())
714 BOOST_FOREACH(const CMerkleTx& txWalletPrev, (*mi).second.vtxPrev)
715 mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
717 else if (mapWalletPrev.count(hash))
719 tx = *mapWalletPrev[hash];
722 int nDepth = tx.SetMerkleBranch();
723 vtxPrev.push_back(tx);
725 if (nDepth < COPY_DEPTH)
727 BOOST_FOREACH(const CTxIn& txin, tx.vin)
728 vWorkQueue.push_back(txin.prevout.hash);
734 reverse(vtxPrev.begin(), vtxPrev.end());
737 bool CWalletTx::WriteToDisk()
739 return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
742 // Scan the block chain (starting in pindexStart) for transactions
743 // from or to us. If fUpdate is true, found transactions that already
744 // exist in the wallet will be updated.
745 int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
749 CBlockIndex* pindex = pindexStart;
755 block.ReadFromDisk(pindex, true);
756 BOOST_FOREACH(CTransaction& tx, block.vtx)
758 if (AddToWalletIfInvolvingMe(tx.GetHash(), tx, &block, fUpdate))
761 pindex = pindex->pnext;
767 void CWallet::ReacceptWalletTransactions()
774 bool fMissing = false;
775 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
777 CWalletTx& wtx = item.second;
778 if (wtx.IsCoinBase() && wtx.IsSpent(0))
782 bool fUpdated = false;
783 bool fNotFound = pcoinsTip->GetCoins(wtx.GetHash(), coins);
784 if (!fNotFound || wtx.GetDepthInMainChain() > 0)
786 // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
787 for (unsigned int i = 0; i < wtx.vout.size(); i++)
791 if ((i >= coins.vout.size() || coins.vout[i].IsNull()) && IsMine(wtx.vout[i]))
800 printf("ReacceptWalletTransactions found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
807 // Re-accept any txes of ours that aren't already in a block
808 if (!wtx.IsCoinBase())
809 wtx.AcceptWalletTransaction(false);
814 // TODO: optimize this to scan just part of the block chain?
815 if (ScanForWalletTransactions(pindexGenesisBlock))
816 fRepeat = true; // Found missing transactions: re-do re-accept.
821 void CWalletTx::RelayWalletTransaction()
823 CCoinsViewCache& coins = *pcoinsTip;
824 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
826 if (!tx.IsCoinBase())
828 uint256 hash = tx.GetHash();
829 if (!coins.HaveCoins(hash))
830 RelayMessage(CInv(MSG_TX, hash), (CTransaction)tx);
835 uint256 hash = GetHash();
836 if (!coins.HaveCoins(hash))
838 printf("Relaying wtx %s\n", hash.ToString().substr(0,10).c_str());
839 RelayMessage(CInv(MSG_TX, hash), (CTransaction)*this);
844 void CWallet::ResendWalletTransactions()
846 // Do this infrequently and randomly to avoid giving away
847 // that these are our transactions.
848 static int64 nNextTime;
849 if (GetTime() < nNextTime)
851 bool fFirst = (nNextTime == 0);
852 nNextTime = GetTime() + GetRand(30 * 60);
856 // Only do it if there's been a new block since last time
857 static int64 nLastTime;
858 if (nTimeBestReceived < nLastTime)
860 nLastTime = GetTime();
862 // Rebroadcast any of our txes that aren't in a block yet
863 printf("ResendWalletTransactions()\n");
866 // Sort them in chronological order
867 multimap<unsigned int, CWalletTx*> mapSorted;
868 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
870 CWalletTx& wtx = item.second;
871 // Don't rebroadcast until it's had plenty of time that
872 // it should have gotten in already by now.
873 if (nTimeBestReceived - (int64)wtx.nTimeReceived > 5 * 60)
874 mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
876 BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
878 CWalletTx& wtx = *item.second;
879 wtx.RelayWalletTransaction();
889 //////////////////////////////////////////////////////////////////////////////
895 int64 CWallet::GetBalance() const
900 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
902 const CWalletTx* pcoin = &(*it).second;
903 if (pcoin->IsFinal() && pcoin->IsConfirmed())
904 nTotal += pcoin->GetAvailableCredit();
911 int64 CWallet::GetUnconfirmedBalance() const
916 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
918 const CWalletTx* pcoin = &(*it).second;
919 if (!pcoin->IsFinal() || !pcoin->IsConfirmed())
920 nTotal += pcoin->GetAvailableCredit();
926 int64 CWallet::GetImmatureBalance() const
931 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
933 const CWalletTx& pcoin = (*it).second;
934 if (pcoin.IsCoinBase() && pcoin.GetBlocksToMaturity() > 0 && pcoin.IsInMainChain())
935 nTotal += GetCredit(pcoin);
941 // populate vCoins with vector of spendable COutputs
942 void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed) const
948 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
950 const CWalletTx* pcoin = &(*it).second;
952 if (!pcoin->IsFinal())
955 if (fOnlyConfirmed && !pcoin->IsConfirmed())
958 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
961 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
962 if (!(pcoin->IsSpent(i)) && IsMine(pcoin->vout[i]) && pcoin->vout[i].nValue > 0)
963 vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain()));
968 static void ApproximateBestSubset(vector<pair<int64, pair<const CWalletTx*,unsigned int> > >vValue, int64 nTotalLower, int64 nTargetValue,
969 vector<char>& vfBest, int64& nBest, int iterations = 1000)
971 vector<char> vfIncluded;
973 vfBest.assign(vValue.size(), true);
976 for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
978 vfIncluded.assign(vValue.size(), false);
980 bool fReachedTarget = false;
981 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
983 for (unsigned int i = 0; i < vValue.size(); i++)
985 if (nPass == 0 ? rand() % 2 : !vfIncluded[i])
987 nTotal += vValue[i].first;
988 vfIncluded[i] = true;
989 if (nTotal >= nTargetValue)
991 fReachedTarget = true;
997 nTotal -= vValue[i].first;
998 vfIncluded[i] = false;
1006 bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, vector<COutput> vCoins,
1007 set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
1009 setCoinsRet.clear();
1012 // List of values less than target
1013 pair<int64, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
1014 coinLowestLarger.first = std::numeric_limits<int64>::max();
1015 coinLowestLarger.second.first = NULL;
1016 vector<pair<int64, pair<const CWalletTx*,unsigned int> > > vValue;
1017 int64 nTotalLower = 0;
1019 random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
1021 BOOST_FOREACH(COutput output, vCoins)
1023 const CWalletTx *pcoin = output.tx;
1025 if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
1029 int64 n = pcoin->vout[i].nValue;
1031 pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
1033 if (n == nTargetValue)
1035 setCoinsRet.insert(coin.second);
1036 nValueRet += coin.first;
1039 else if (n < nTargetValue + CENT)
1041 vValue.push_back(coin);
1044 else if (n < coinLowestLarger.first)
1046 coinLowestLarger = coin;
1050 if (nTotalLower == nTargetValue)
1052 for (unsigned int i = 0; i < vValue.size(); ++i)
1054 setCoinsRet.insert(vValue[i].second);
1055 nValueRet += vValue[i].first;
1060 if (nTotalLower < nTargetValue)
1062 if (coinLowestLarger.second.first == NULL)
1064 setCoinsRet.insert(coinLowestLarger.second);
1065 nValueRet += coinLowestLarger.first;
1069 // Solve subset sum by stochastic approximation
1070 sort(vValue.rbegin(), vValue.rend(), CompareValueOnly());
1071 vector<char> vfBest;
1074 ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000);
1075 if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT)
1076 ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000);
1078 // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
1079 // or the next bigger coin is closer), return the bigger coin
1080 if (coinLowestLarger.second.first &&
1081 ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest))
1083 setCoinsRet.insert(coinLowestLarger.second);
1084 nValueRet += coinLowestLarger.first;
1087 for (unsigned int i = 0; i < vValue.size(); i++)
1090 setCoinsRet.insert(vValue[i].second);
1091 nValueRet += vValue[i].first;
1095 printf("SelectCoins() best subset: ");
1096 for (unsigned int i = 0; i < vValue.size(); i++)
1098 printf("%s ", FormatMoney(vValue[i].first).c_str());
1099 printf("total %s\n", FormatMoney(nBest).c_str());
1105 bool CWallet::SelectCoins(int64 nTargetValue, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
1107 vector<COutput> vCoins;
1108 AvailableCoins(vCoins);
1110 return (SelectCoinsMinConf(nTargetValue, 1, 6, vCoins, setCoinsRet, nValueRet) ||
1111 SelectCoinsMinConf(nTargetValue, 1, 1, vCoins, setCoinsRet, nValueRet) ||
1112 SelectCoinsMinConf(nTargetValue, 0, 1, vCoins, setCoinsRet, nValueRet));
1118 bool CWallet::CreateTransaction(const vector<pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet)
1121 BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1127 if (vecSend.empty() || nValue < 0)
1130 wtxNew.BindWallet(this);
1133 LOCK2(cs_main, cs_wallet);
1135 nFeeRet = nTransactionFee;
1139 wtxNew.vout.clear();
1140 wtxNew.fFromMe = true;
1142 int64 nTotalValue = nValue + nFeeRet;
1143 double dPriority = 0;
1144 // vouts to the payees
1145 BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1146 wtxNew.vout.push_back(CTxOut(s.second, s.first));
1148 // Choose coins to use
1149 set<pair<const CWalletTx*,unsigned int> > setCoins;
1151 if (!SelectCoins(nTotalValue, setCoins, nValueIn))
1153 BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1155 int64 nCredit = pcoin.first->vout[pcoin.second].nValue;
1156 dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain();
1159 int64 nChange = nValueIn - nValue - nFeeRet;
1160 // if sub-cent change is required, the fee must be raised to at least MIN_TX_FEE
1161 // or until nChange becomes zero
1162 // NOTE: this depends on the exact behaviour of GetMinFee
1163 if (nFeeRet < MIN_TX_FEE && nChange > 0 && nChange < CENT)
1165 int64 nMoveToFee = min(nChange, MIN_TX_FEE - nFeeRet);
1166 nChange -= nMoveToFee;
1167 nFeeRet += nMoveToFee;
1172 // Note: We use a new key here to keep it from being obvious which side is the change.
1173 // The drawback is that by not reusing a previous key, the change may be lost if a
1174 // backup is restored, if the backup doesn't have the new private key for the change.
1175 // If we reused the old key, it would be possible to add code to look for and
1176 // rediscover unknown transactions that were written with keys of ours to recover
1177 // post-backup change.
1179 // Reserve a new key pair from key pool
1180 CPubKey vchPubKey = reservekey.GetReservedKey();
1181 // assert(mapKeys.count(vchPubKey));
1183 // Fill a vout to ourself
1184 // TODO: pass in scriptChange instead of reservekey so
1185 // change transaction isn't always pay-to-bitcoin-address
1186 CScript scriptChange;
1187 scriptChange.SetDestination(vchPubKey.GetID());
1189 // Insert change txn at random position:
1190 vector<CTxOut>::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size());
1191 wtxNew.vout.insert(position, CTxOut(nChange, scriptChange));
1194 reservekey.ReturnKey();
1197 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1198 wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
1202 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1203 if (!SignSignature(*this, *coin.first, wtxNew, nIn++))
1207 unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
1208 if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
1210 dPriority /= nBytes;
1212 // Check that enough fee is included
1213 int64 nPayFee = nTransactionFee * (1 + (int64)nBytes / 1000);
1214 bool fAllowFree = CTransaction::AllowFree(dPriority);
1215 int64 nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND);
1216 if (nFeeRet < max(nPayFee, nMinFee))
1218 nFeeRet = max(nPayFee, nMinFee);
1222 // Fill vtxPrev by copying from previous transactions vtxPrev
1223 wtxNew.AddSupportingTransactions();
1224 wtxNew.fTimeReceivedIsTxTime = true;
1233 bool CWallet::CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet)
1235 vector< pair<CScript, int64> > vecSend;
1236 vecSend.push_back(make_pair(scriptPubKey, nValue));
1237 return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet);
1240 // Call after CreateTransaction unless you want to abort
1241 bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
1244 LOCK2(cs_main, cs_wallet);
1245 printf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
1247 // This is only to keep the database open to defeat the auto-flush for the
1248 // duration of this scope. This is the only place where this optimization
1249 // maybe makes sense; please don't do it anywhere else.
1250 CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
1252 // Take key pair from key pool so it won't be used again
1253 reservekey.KeepKey();
1255 // Add tx to wallet, because if it has change it's also ours,
1256 // otherwise just for transaction history.
1257 AddToWallet(wtxNew);
1259 // Mark old coins as spent
1260 set<CWalletTx*> setCoins;
1261 BOOST_FOREACH(const CTxIn& txin, wtxNew.vin)
1263 CWalletTx &coin = mapWallet[txin.prevout.hash];
1264 coin.BindWallet(this);
1265 coin.MarkSpent(txin.prevout.n);
1267 NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
1274 // Track how many getdata requests our transaction gets
1275 mapRequestCount[wtxNew.GetHash()] = 0;
1278 if (!wtxNew.AcceptToMemoryPool())
1280 // This must not fail. The transaction has already been signed and recorded.
1281 printf("CommitTransaction() : Error: Transaction not valid");
1284 wtxNew.RelayWalletTransaction();
1292 string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
1294 CReserveKey reservekey(this);
1299 string strError = _("Error: Wallet locked, unable to create transaction ");
1300 printf("SendMoney() : %s", strError.c_str());
1303 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired))
1306 if (nValue + nFeeRequired > GetBalance())
1307 strError = strprintf(_("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds "), FormatMoney(nFeeRequired).c_str());
1309 strError = _("Error: Transaction creation failed ");
1310 printf("SendMoney() : %s", strError.c_str());
1314 if (fAskFee && !uiInterface.ThreadSafeAskFee(nFeeRequired, _("Sending...")))
1317 if (!CommitTransaction(wtxNew, reservekey))
1318 return _("Error: The transaction was rejected. This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
1325 string CWallet::SendMoneyToDestination(const CTxDestination& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
1329 return _("Invalid amount");
1330 if (nValue + nTransactionFee > GetBalance())
1331 return _("Insufficient funds");
1333 // Parse Bitcoin address
1334 CScript scriptPubKey;
1335 scriptPubKey.SetDestination(address);
1337 return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
1343 DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
1347 fFirstRunRet = false;
1348 DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this);
1349 if (nLoadWalletRet == DB_NEED_REWRITE)
1351 if (CDB::Rewrite(strWalletFile, "\x04pool"))
1354 // Note: can't top-up keypool here, because wallet is locked.
1355 // User will be prompted to unlock wallet the next operation
1356 // the requires a new key.
1360 if (nLoadWalletRet != DB_LOAD_OK)
1361 return nLoadWalletRet;
1362 fFirstRunRet = !vchDefaultKey.IsValid();
1364 NewThread(ThreadFlushWalletDB, &strWalletFile);
1369 bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName)
1371 std::map<CTxDestination, std::string>::iterator mi = mapAddressBook.find(address);
1372 mapAddressBook[address] = strName;
1373 NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED);
1376 return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName);
1379 bool CWallet::DelAddressBookName(const CTxDestination& address)
1381 mapAddressBook.erase(address);
1382 NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), CT_DELETED);
1385 return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString());
1389 void CWallet::PrintWallet(const CBlock& block)
1393 if (mapWallet.count(block.vtx[0].GetHash()))
1395 CWalletTx& wtx = mapWallet[block.vtx[0].GetHash()];
1396 printf(" mine: %d %d %"PRI64d"", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
1402 bool CWallet::GetTransaction(const uint256 &hashTx, CWalletTx& wtx)
1406 map<uint256, CWalletTx>::iterator mi = mapWallet.find(hashTx);
1407 if (mi != mapWallet.end())
1416 bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
1420 if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey))
1423 vchDefaultKey = vchPubKey;
1427 bool GetWalletFile(CWallet* pwallet, string &strWalletFileOut)
1429 if (!pwallet->fFileBacked)
1431 strWalletFileOut = pwallet->strWalletFile;
1436 // Mark old keypool keys as used,
1437 // and generate all new keys
1439 bool CWallet::NewKeyPool()
1443 CWalletDB walletdb(strWalletFile);
1444 BOOST_FOREACH(int64 nIndex, setKeyPool)
1445 walletdb.ErasePool(nIndex);
1451 int64 nKeys = max(GetArg("-keypool", 100), (int64)0);
1452 for (int i = 0; i < nKeys; i++)
1455 walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey()));
1456 setKeyPool.insert(nIndex);
1458 printf("CWallet::NewKeyPool wrote %"PRI64d" new keys\n", nKeys);
1463 bool CWallet::TopUpKeyPool()
1471 CWalletDB walletdb(strWalletFile);
1474 unsigned int nTargetSize = max(GetArg("-keypool", 100), 0LL);
1475 while (setKeyPool.size() < (nTargetSize + 1))
1478 if (!setKeyPool.empty())
1479 nEnd = *(--setKeyPool.end()) + 1;
1480 if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
1481 throw runtime_error("TopUpKeyPool() : writing generated key failed");
1482 setKeyPool.insert(nEnd);
1483 printf("keypool added key %"PRI64d", size=%"PRIszu"\n", nEnd, setKeyPool.size());
1489 void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
1492 keypool.vchPubKey = CPubKey();
1499 // Get the oldest key
1500 if(setKeyPool.empty())
1503 CWalletDB walletdb(strWalletFile);
1505 nIndex = *(setKeyPool.begin());
1506 setKeyPool.erase(setKeyPool.begin());
1507 if (!walletdb.ReadPool(nIndex, keypool))
1508 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
1509 if (!HaveKey(keypool.vchPubKey.GetID()))
1510 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
1511 assert(keypool.vchPubKey.IsValid());
1512 printf("keypool reserve %"PRI64d"\n", nIndex);
1516 int64 CWallet::AddReserveKey(const CKeyPool& keypool)
1519 LOCK2(cs_main, cs_wallet);
1520 CWalletDB walletdb(strWalletFile);
1522 int64 nIndex = 1 + *(--setKeyPool.end());
1523 if (!walletdb.WritePool(nIndex, keypool))
1524 throw runtime_error("AddReserveKey() : writing added key failed");
1525 setKeyPool.insert(nIndex);
1531 void CWallet::KeepKey(int64 nIndex)
1533 // Remove from key pool
1536 CWalletDB walletdb(strWalletFile);
1537 walletdb.ErasePool(nIndex);
1539 printf("keypool keep %"PRI64d"\n", nIndex);
1542 void CWallet::ReturnKey(int64 nIndex)
1544 // Return to key pool
1547 setKeyPool.insert(nIndex);
1549 printf("keypool return %"PRI64d"\n", nIndex);
1552 bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse)
1558 ReserveKeyFromKeyPool(nIndex, keypool);
1561 if (fAllowReuse && vchDefaultKey.IsValid())
1563 result = vchDefaultKey;
1566 if (IsLocked()) return false;
1567 result = GenerateNewKey();
1571 result = keypool.vchPubKey;
1576 int64 CWallet::GetOldestKeyPoolTime()
1580 ReserveKeyFromKeyPool(nIndex, keypool);
1584 return keypool.nTime;
1587 std::map<CTxDestination, int64> CWallet::GetAddressBalances()
1589 map<CTxDestination, int64> balances;
1593 BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
1595 CWalletTx *pcoin = &walletEntry.second;
1597 if (!pcoin->IsFinal() || !pcoin->IsConfirmed())
1600 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
1603 int nDepth = pcoin->GetDepthInMainChain();
1604 if (nDepth < (pcoin->IsFromMe() ? 0 : 1))
1607 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
1609 CTxDestination addr;
1610 if (!IsMine(pcoin->vout[i]))
1612 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, addr))
1615 int64 n = pcoin->IsSpent(i) ? 0 : pcoin->vout[i].nValue;
1617 if (!balances.count(addr))
1619 balances[addr] += n;
1627 set< set<CTxDestination> > CWallet::GetAddressGroupings()
1629 set< set<CTxDestination> > groupings;
1630 set<CTxDestination> grouping;
1632 BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
1634 CWalletTx *pcoin = &walletEntry.second;
1636 if (pcoin->vin.size() > 0 && IsMine(pcoin->vin[0]))
1638 // group all input addresses with each other
1639 BOOST_FOREACH(CTxIn txin, pcoin->vin)
1641 CTxDestination address;
1642 if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address))
1644 grouping.insert(address);
1647 // group change with input addresses
1648 BOOST_FOREACH(CTxOut txout, pcoin->vout)
1649 if (IsChange(txout))
1651 CWalletTx tx = mapWallet[pcoin->vin[0].prevout.hash];
1652 CTxDestination txoutAddr;
1653 if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
1655 grouping.insert(txoutAddr);
1657 groupings.insert(grouping);
1661 // group lone addrs by themselves
1662 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
1663 if (IsMine(pcoin->vout[i]))
1665 CTxDestination address;
1666 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address))
1668 grouping.insert(address);
1669 groupings.insert(grouping);
1674 set< set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
1675 map< CTxDestination, set<CTxDestination>* > setmap; // map addresses to the unique group containing it
1676 BOOST_FOREACH(set<CTxDestination> grouping, groupings)
1678 // make a set of all the groups hit by this new group
1679 set< set<CTxDestination>* > hits;
1680 map< CTxDestination, set<CTxDestination>* >::iterator it;
1681 BOOST_FOREACH(CTxDestination address, grouping)
1682 if ((it = setmap.find(address)) != setmap.end())
1683 hits.insert((*it).second);
1685 // merge all hit groups into a new single group and delete old groups
1686 set<CTxDestination>* merged = new set<CTxDestination>(grouping);
1687 BOOST_FOREACH(set<CTxDestination>* hit, hits)
1689 merged->insert(hit->begin(), hit->end());
1690 uniqueGroupings.erase(hit);
1693 uniqueGroupings.insert(merged);
1696 BOOST_FOREACH(CTxDestination element, *merged)
1697 setmap[element] = merged;
1700 set< set<CTxDestination> > ret;
1701 BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings)
1703 ret.insert(*uniqueGrouping);
1704 delete uniqueGrouping;
1710 CPubKey CReserveKey::GetReservedKey()
1715 pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
1717 vchPubKey = keypool.vchPubKey;
1720 printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
1721 vchPubKey = pwallet->vchDefaultKey;
1724 assert(vchPubKey.IsValid());
1728 void CReserveKey::KeepKey()
1731 pwallet->KeepKey(nIndex);
1733 vchPubKey = CPubKey();
1736 void CReserveKey::ReturnKey()
1739 pwallet->ReturnKey(nIndex);
1741 vchPubKey = CPubKey();
1744 void CWallet::GetAllReserveKeys(set<CKeyID>& setAddress)
1748 CWalletDB walletdb(strWalletFile);
1750 LOCK2(cs_main, cs_wallet);
1751 BOOST_FOREACH(const int64& id, setKeyPool)
1754 if (!walletdb.ReadPool(id, keypool))
1755 throw runtime_error("GetAllReserveKeyHashes() : read failed");
1756 assert(keypool.vchPubKey.IsValid());
1757 CKeyID keyID = keypool.vchPubKey.GetID();
1758 if (!HaveKey(keyID))
1759 throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool");
1760 setAddress.insert(keyID);
1764 void CWallet::UpdatedTransaction(const uint256 &hashTx)
1768 // Only notify UI if this transaction is in this wallet
1769 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(hashTx);
1770 if (mi != mapWallet.end())
1771 NotifyTransactionChanged(this, hashTx, CT_UPDATED);