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.
10 #include "ui_interface.h"
13 #include "coincontrol.h"
14 #include <boost/algorithm/string/replace.hpp>
17 extern int nStakeMaxAge;
20 //////////////////////////////////////////////////////////////////////////////
25 struct CompareValueOnly
27 bool operator()(const pair<int64, pair<const CWalletTx*, unsigned int> >& t1,
28 const pair<int64, pair<const CWalletTx*, unsigned int> >& t2) const
30 return t1.first < t2.first;
34 CPubKey CWallet::GenerateNewKey()
36 bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
40 key.MakeNewKey(fCompressed);
42 // Compressed public keys were introduced in version 0.6.0
44 SetMinVersion(FEATURE_COMPRPUBKEY);
46 CPubKey pubkey = key.GetPubKey();
48 // Create new metadata
49 int64 nCreationTime = GetTime();
50 mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime);
51 if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
52 nTimeFirstKey = nCreationTime;
55 throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
56 return key.GetPubKey();
59 bool CWallet::AddKey(const CKey& key)
61 CPubKey pubkey = key.GetPubKey();
63 if (!CCryptoKeyStore::AddKey(key))
68 return CWalletDB(strWalletFile).WriteKey(pubkey, key.GetPrivKey(), mapKeyMetadata[pubkey.GetID()]);
72 bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector<unsigned char> &vchCryptedSecret)
74 if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
80 if (pwalletdbEncryption)
81 return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
83 return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
88 bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
90 if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
91 nTimeFirstKey = meta.nCreateTime;
93 mapKeyMetadata[pubkey.GetID()] = meta;
97 bool CWallet::AddCScript(const CScript& redeemScript)
99 if (!CCryptoKeyStore::AddCScript(redeemScript))
103 return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
106 // ppcoin: optional setting to unlock wallet for block minting only;
107 // serves to disable the trivial sendmoney when OS account compromised
108 bool fWalletUnlockMintOnly = false;
110 bool CWallet::Unlock(const SecureString& strWalletPassphrase)
116 CKeyingMaterial vMasterKey;
120 BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
122 if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
124 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
126 if (CCryptoKeyStore::Unlock(vMasterKey))
133 bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
135 bool fWasLocked = IsLocked();
142 CKeyingMaterial vMasterKey;
143 BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
145 if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
147 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
149 if (CCryptoKeyStore::Unlock(vMasterKey))
151 int64 nStartTime = GetTimeMillis();
152 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
153 pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
155 nStartTime = GetTimeMillis();
156 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
157 pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
159 if (pMasterKey.second.nDeriveIterations < 25000)
160 pMasterKey.second.nDeriveIterations = 25000;
162 printf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
164 if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
166 if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
168 CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second);
179 void CWallet::SetBestChain(const CBlockLocator& loc)
181 CWalletDB walletdb(strWalletFile);
182 walletdb.WriteBestBlock(loc);
185 // This class implements an addrIncoming entry that causes pre-0.4
186 // clients to crash on startup if reading a private-key-encrypted wallet.
187 class CCorruptAddress
192 if (nType & SER_DISK)
197 bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
199 if (nWalletVersion >= nVersion)
202 // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
203 if (fExplicit && nVersion > nWalletMaxVersion)
204 nVersion = FEATURE_LATEST;
206 nWalletVersion = nVersion;
208 if (nVersion > nWalletMaxVersion)
209 nWalletMaxVersion = nVersion;
213 CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
214 if (nWalletVersion >= 40000)
216 // Versions prior to 0.4.0 did not support the "minversion" record.
217 // Use a CCorruptAddress to make them crash instead.
218 CCorruptAddress corruptAddress;
219 pwalletdb->WriteSetting("addrIncoming", corruptAddress);
221 if (nWalletVersion > 40000)
222 pwalletdb->WriteMinVersion(nWalletVersion);
230 bool CWallet::SetMaxVersion(int nVersion)
232 // cannot downgrade below current version
233 if (nWalletVersion > nVersion)
236 nWalletMaxVersion = nVersion;
241 bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
246 CKeyingMaterial vMasterKey;
247 RandAddSeedPerfmon();
249 vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
250 RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
252 CMasterKey kMasterKey(nDerivationMethodIndex);
254 RandAddSeedPerfmon();
255 kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
256 RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
259 int64 nStartTime = GetTimeMillis();
260 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
261 kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime));
263 nStartTime = GetTimeMillis();
264 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
265 kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
267 if (kMasterKey.nDeriveIterations < 25000)
268 kMasterKey.nDeriveIterations = 25000;
270 printf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
272 if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
274 if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey))
279 mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
282 pwalletdbEncryption = new CWalletDB(strWalletFile);
283 if (!pwalletdbEncryption->TxnBegin())
285 pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
288 if (!EncryptKeys(vMasterKey))
291 pwalletdbEncryption->TxnAbort();
292 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.
295 // Encryption was introduced in version 0.4.0
296 SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
300 if (!pwalletdbEncryption->TxnCommit())
301 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.
303 delete pwalletdbEncryption;
304 pwalletdbEncryption = NULL;
308 Unlock(strWalletPassphrase);
312 // Need to completely rewrite the wallet file; if we don't, bdb might keep
313 // bits of the unencrypted private key in slack space in the database file.
314 CDB::Rewrite(strWalletFile);
317 NotifyStatusChanged(this);
322 int64 CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
324 int64 nRet = nOrderPosNext++;
326 pwalletdb->WriteOrderPosNext(nOrderPosNext);
328 CWalletDB(strWalletFile).WriteOrderPosNext(nOrderPosNext);
333 CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount)
335 CWalletDB walletdb(strWalletFile);
337 // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap.
340 // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry
341 // would make this much faster for applications that do this a lot.
342 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
344 CWalletTx* wtx = &((*it).second);
345 txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0)));
348 walletdb.ListAccountCreditDebit(strAccount, acentries);
349 BOOST_FOREACH(CAccountingEntry& entry, acentries)
351 txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry)));
357 void CWallet::WalletUpdateSpent(const CTransaction &tx, bool fBlock)
359 // Anytime a signature is successfully verified, it's proof the outpoint is spent.
360 // Update the wallet spent flag if it doesn't know due to wallet.dat being
361 // restored from backup or the user making copies of wallet.dat.
364 BOOST_FOREACH(const CTxIn& txin, tx.vin)
366 map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
367 if (mi != mapWallet.end())
369 CWalletTx& wtx = (*mi).second;
370 if (txin.prevout.n >= wtx.vout.size())
371 printf("WalletUpdateSpent: bad wtx %s\n", wtx.GetHash().ToString().c_str());
372 else if (!wtx.IsSpent(txin.prevout.n) && IsMine(wtx.vout[txin.prevout.n]))
374 printf("WalletUpdateSpent found spent coin %snvc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
375 wtx.MarkSpent(txin.prevout.n);
377 NotifyTransactionChanged(this, txin.prevout.hash, CT_UPDATED);
384 uint256 hash = tx.GetHash();
385 map<uint256, CWalletTx>::iterator mi = mapWallet.find(hash);
386 CWalletTx& wtx = (*mi).second;
388 BOOST_FOREACH(const CTxOut& txout, tx.vout)
392 wtx.MarkUnspent(&txout - &tx.vout[0]);
394 NotifyTransactionChanged(this, hash, CT_UPDATED);
402 void CWallet::MarkDirty()
406 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
407 item.second.MarkDirty();
411 bool CWallet::AddToWallet(const CWalletTx& wtxIn)
413 uint256 hash = wtxIn.GetHash();
416 // Inserts only if not already there, returns tx inserted or tx found
417 pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
418 CWalletTx& wtx = (*ret.first).second;
419 wtx.BindWallet(this);
420 bool fInsertedNew = ret.second;
423 wtx.nTimeReceived = GetAdjustedTime();
424 wtx.nOrderPos = IncOrderPosNext();
426 wtx.nTimeSmart = wtx.nTimeReceived;
427 if (wtxIn.hashBlock != 0)
429 if (mapBlockIndex.count(wtxIn.hashBlock))
431 unsigned int latestNow = wtx.nTimeReceived;
432 unsigned int latestEntry = 0;
434 // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
435 int64 latestTolerated = latestNow + 300;
436 std::list<CAccountingEntry> acentries;
437 TxItems txOrdered = OrderedTxItems(acentries);
438 for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
440 CWalletTx *const pwtx = (*it).second.first;
443 CAccountingEntry *const pacentry = (*it).second.second;
447 nSmartTime = pwtx->nTimeSmart;
449 nSmartTime = pwtx->nTimeReceived;
452 nSmartTime = pacentry->nTime;
453 if (nSmartTime <= latestTolerated)
455 latestEntry = nSmartTime;
456 if (nSmartTime > latestNow)
457 latestNow = nSmartTime;
463 unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime;
464 wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
467 printf("AddToWallet() : found %s in block %s not in index\n",
468 wtxIn.GetHash().ToString().substr(0,10).c_str(),
469 wtxIn.hashBlock.ToString().c_str());
473 bool fUpdated = false;
477 if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
479 wtx.hashBlock = wtxIn.hashBlock;
482 if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
484 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
485 wtx.nIndex = wtxIn.nIndex;
488 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
490 wtx.fFromMe = wtxIn.fFromMe;
493 fUpdated |= wtx.UpdateSpent(wtxIn.vfSpent);
497 printf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().substr(0,10).c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
500 if (fInsertedNew || fUpdated)
501 if (!wtx.WriteToDisk())
504 // If default receiving address gets used, replace it with a new one
505 CScript scriptDefaultKey;
506 scriptDefaultKey.SetDestination(vchDefaultKey.GetID());
507 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
509 if (txout.scriptPubKey == scriptDefaultKey)
511 CPubKey newDefaultKey;
512 if (GetKeyFromPool(newDefaultKey, false))
514 SetDefaultKey(newDefaultKey);
515 SetAddressBookName(vchDefaultKey.GetID(), "");
520 // since AddToWallet is called directly for self-originating transactions, check for consumption of own coins
521 WalletUpdateSpent(wtx, (wtxIn.hashBlock != 0));
523 // Notify UI of new or updated transaction
524 NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
526 // notify an external script when a wallet transaction comes in or is updated
527 std::string strCmd = GetArg("-walletnotify", "");
529 if ( !strCmd.empty())
531 boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
532 boost::thread t(runCommand, strCmd); // thread runs free
539 // Add a transaction to the wallet, or update it.
540 // pblock is optional, but should be provided if the transaction is known to be in a block.
541 // If fUpdate is true, existing transactions will be updated.
542 bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate, bool fFindBlock)
544 uint256 hash = tx.GetHash();
547 bool fExisted = mapWallet.count(hash);
548 if (fExisted && !fUpdate) return false;
549 if (fExisted || IsMine(tx) || IsFromMe(tx))
551 CWalletTx wtx(this,tx);
552 // Get merkle branch if transaction was found in a block
554 wtx.SetMerkleBranch(pblock);
555 return AddToWallet(wtx);
558 WalletUpdateSpent(tx);
563 bool CWallet::EraseFromWallet(uint256 hash)
569 if (mapWallet.erase(hash))
570 CWalletDB(strWalletFile).EraseTx(hash);
576 bool CWallet::IsMine(const CTxIn &txin) const
580 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
581 if (mi != mapWallet.end())
583 const CWalletTx& prev = (*mi).second;
584 if (txin.prevout.n < prev.vout.size())
585 if (IsMine(prev.vout[txin.prevout.n]))
592 int64 CWallet::GetDebit(const CTxIn &txin) const
596 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
597 if (mi != mapWallet.end())
599 const CWalletTx& prev = (*mi).second;
600 if (txin.prevout.n < prev.vout.size())
601 if (IsMine(prev.vout[txin.prevout.n]))
602 return prev.vout[txin.prevout.n].nValue;
608 bool CWallet::IsChange(const CTxOut& txout) const
610 CTxDestination address;
612 // TODO: fix handling of 'change' outputs. The assumption is that any
613 // payment to a TX_PUBKEYHASH that is mine but isn't in the address book
614 // is change. That assumption is likely to break when we implement multisignature
615 // wallets that return change back into a multi-signature-protected address;
616 // a better way of identifying which outputs are 'the send' and which are
617 // 'the change' will need to be implemented (maybe extend CWalletTx to remember
618 // which output, if any, was change).
619 if (ExtractDestination(txout.scriptPubKey, address) && ::IsMine(*this, address))
622 if (!mapAddressBook.count(address))
628 int64 CWalletTx::GetTxTime() const
630 int64 n = nTimeSmart;
631 return n ? n : nTimeReceived;
634 int CWalletTx::GetRequestCount() const
636 // Returns -1 if it wasn't being tracked
639 LOCK(pwallet->cs_wallet);
640 if (IsCoinBase() || IsCoinStake())
645 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
646 if (mi != pwallet->mapRequestCount.end())
647 nRequests = (*mi).second;
652 // Did anyone request this transaction?
653 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
654 if (mi != pwallet->mapRequestCount.end())
656 nRequests = (*mi).second;
658 // How about the block it's in?
659 if (nRequests == 0 && hashBlock != 0)
661 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
662 if (mi != pwallet->mapRequestCount.end())
663 nRequests = (*mi).second;
665 nRequests = 1; // If it's in someone else's block it must have got out
673 void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, list<pair<CTxDestination, int64> >& listReceived,
674 list<pair<CTxDestination, int64> >& listSent, int64& nFee, string& strSentAccount) const
676 nGeneratedImmature = nGeneratedMature = nFee = 0;
677 listReceived.clear();
679 strSentAccount = strFromAccount;
681 if (IsCoinBase() || IsCoinStake())
683 if (GetBlocksToMaturity() > 0)
684 nGeneratedImmature = pwallet->GetCredit(*this);
686 nGeneratedMature = GetCredit();
691 int64 nDebit = GetDebit();
692 if (nDebit > 0) // debit>0 means we signed/sent this transaction
694 int64 nValueOut = GetValueOut();
695 nFee = nDebit - nValueOut;
699 BOOST_FOREACH(const CTxOut& txout, vout)
701 CTxDestination address;
702 vector<unsigned char> vchPubKey;
703 if (!ExtractDestination(txout.scriptPubKey, address))
705 printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
706 this->GetHash().ToString().c_str());
709 // Don't report 'change' txouts
710 if (nDebit > 0 && pwallet->IsChange(txout))
714 listSent.push_back(make_pair(address, txout.nValue));
716 if (pwallet->IsMine(txout))
717 listReceived.push_back(make_pair(address, txout.nValue));
722 void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, int64& nReceived,
723 int64& nSent, int64& nFee) const
725 nGenerated = nReceived = nSent = nFee = 0;
727 int64 allGeneratedImmature, allGeneratedMature, allFee;
728 allGeneratedImmature = allGeneratedMature = allFee = 0;
729 string strSentAccount;
730 list<pair<CTxDestination, int64> > listReceived;
731 list<pair<CTxDestination, int64> > listSent;
732 GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount);
734 if (strAccount == "")
735 nGenerated = allGeneratedMature;
736 if (strAccount == strSentAccount)
738 BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& s, listSent)
743 LOCK(pwallet->cs_wallet);
744 BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& r, listReceived)
746 if (pwallet->mapAddressBook.count(r.first))
748 map<CTxDestination, string>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
749 if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount)
750 nReceived += r.second;
752 else if (strAccount.empty())
754 nReceived += r.second;
760 void CWalletTx::AddSupportingTransactions(CTxDB& txdb)
764 const int COPY_DEPTH = 3;
765 if (SetMerkleBranch() < COPY_DEPTH)
767 vector<uint256> vWorkQueue;
768 BOOST_FOREACH(const CTxIn& txin, vin)
769 vWorkQueue.push_back(txin.prevout.hash);
771 // This critsect is OK because txdb is already open
773 LOCK(pwallet->cs_wallet);
774 map<uint256, const CMerkleTx*> mapWalletPrev;
775 set<uint256> setAlreadyDone;
776 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
778 uint256 hash = vWorkQueue[i];
779 if (setAlreadyDone.count(hash))
781 setAlreadyDone.insert(hash);
784 map<uint256, CWalletTx>::const_iterator mi = pwallet->mapWallet.find(hash);
785 if (mi != pwallet->mapWallet.end())
788 BOOST_FOREACH(const CMerkleTx& txWalletPrev, (*mi).second.vtxPrev)
789 mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
791 else if (mapWalletPrev.count(hash))
793 tx = *mapWalletPrev[hash];
795 else if (!fClient && txdb.ReadDiskTx(hash, tx))
801 printf("ERROR: AddSupportingTransactions() : unsupported transaction\n");
805 int nDepth = tx.SetMerkleBranch();
806 vtxPrev.push_back(tx);
808 if (nDepth < COPY_DEPTH)
810 BOOST_FOREACH(const CTxIn& txin, tx.vin)
811 vWorkQueue.push_back(txin.prevout.hash);
817 reverse(vtxPrev.begin(), vtxPrev.end());
820 bool CWalletTx::WriteToDisk()
822 return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
825 // Scan the block chain (starting in pindexStart) for transactions
826 // from or to us. If fUpdate is true, found transactions that already
827 // exist in the wallet will be updated.
828 int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
832 CBlockIndex* pindex = pindexStart;
838 block.ReadFromDisk(pindex, true);
839 BOOST_FOREACH(CTransaction& tx, block.vtx)
841 if (AddToWalletIfInvolvingMe(tx, &block, fUpdate))
844 pindex = pindex->pnext;
850 int CWallet::ScanForWalletTransaction(const uint256& hashTx)
853 tx.ReadFromDisk(COutPoint(hashTx, 0));
854 if (AddToWalletIfInvolvingMe(tx, NULL, true, true))
859 void CWallet::ReacceptWalletTransactions()
867 vector<CDiskTxPos> vMissingTx;
868 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
870 CWalletTx& wtx = item.second;
871 if ((wtx.IsCoinBase() && wtx.IsSpent(0)) || (wtx.IsCoinStake() && wtx.IsSpent(1)))
875 bool fUpdated = false;
876 if (txdb.ReadTxIndex(wtx.GetHash(), txindex))
878 // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
879 if (txindex.vSpent.size() != wtx.vout.size())
881 printf("ERROR: ReacceptWalletTransactions() : txindex.vSpent.size() %"PRIszu" != wtx.vout.size() %"PRIszu"\n", txindex.vSpent.size(), wtx.vout.size());
884 for (unsigned int i = 0; i < txindex.vSpent.size(); i++)
888 if (!txindex.vSpent[i].IsNull() && IsMine(wtx.vout[i]))
892 vMissingTx.push_back(txindex.vSpent[i]);
897 printf("ReacceptWalletTransactions found spent coin %snvc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
904 // Re-accept any txes of ours that aren't already in a block
905 if (!(wtx.IsCoinBase() || wtx.IsCoinStake()))
906 wtx.AcceptWalletTransaction(txdb, false);
909 if (!vMissingTx.empty())
911 // TODO: optimize this to scan just part of the block chain?
912 if (ScanForWalletTransactions(pindexGenesisBlock))
913 fRepeat = true; // Found missing transactions: re-do re-accept.
918 void CWalletTx::RelayWalletTransaction(CTxDB& txdb)
920 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
922 if (!(tx.IsCoinBase() || tx.IsCoinStake()))
924 uint256 hash = tx.GetHash();
925 if (!txdb.ContainsTx(hash))
926 RelayTransaction((CTransaction)tx, hash);
929 if (!(IsCoinBase() || IsCoinStake()))
931 uint256 hash = GetHash();
932 if (!txdb.ContainsTx(hash))
934 printf("Relaying wtx %s\n", hash.ToString().substr(0,10).c_str());
935 RelayTransaction((CTransaction)*this, hash);
940 void CWalletTx::RelayWalletTransaction()
943 RelayWalletTransaction(txdb);
946 void CWallet::ResendWalletTransactions()
948 // Do this infrequently and randomly to avoid giving away
949 // that these are our transactions.
950 static int64 nNextTime;
951 if (GetTime() < nNextTime)
953 bool fFirst = (nNextTime == 0);
954 nNextTime = GetTime() + GetRand(30 * 60);
958 // Only do it if there's been a new block since last time
959 static int64 nLastTime;
960 if (nTimeBestReceived < nLastTime)
962 nLastTime = GetTime();
964 // Rebroadcast any of our txes that aren't in a block yet
965 printf("ResendWalletTransactions()\n");
969 // Sort them in chronological order
970 multimap<unsigned int, CWalletTx*> mapSorted;
971 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
973 CWalletTx& wtx = item.second;
974 // Don't rebroadcast until it's had plenty of time that
975 // it should have gotten in already by now.
976 if (nTimeBestReceived - (int64)wtx.nTimeReceived > 5 * 60)
977 mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
979 BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
981 CWalletTx& wtx = *item.second;
982 if (wtx.CheckTransaction())
983 wtx.RelayWalletTransaction(txdb);
985 printf("ResendWalletTransactions() : CheckTransaction failed for transaction %s\n", wtx.GetHash().ToString().c_str());
995 //////////////////////////////////////////////////////////////////////////////
1001 int64 CWallet::GetBalance() const
1006 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1008 const CWalletTx* pcoin = &(*it).second;
1009 if (pcoin->IsConfirmed())
1010 nTotal += pcoin->GetAvailableCredit();
1017 int64 CWallet::GetUnconfirmedBalance() const
1022 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1024 const CWalletTx* pcoin = &(*it).second;
1025 if (!pcoin->IsFinal() || !pcoin->IsConfirmed())
1026 nTotal += pcoin->GetAvailableCredit();
1032 int64 CWallet::GetImmatureBalance() const
1037 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1039 const CWalletTx& pcoin = (*it).second;
1040 if (pcoin.IsCoinBase() && pcoin.GetBlocksToMaturity() > 0 && pcoin.IsInMainChain())
1041 nTotal += GetCredit(pcoin);
1047 // populate vCoins with vector of spendable COutputs
1048 void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const CCoinControl *coinControl) const
1054 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1056 const CWalletTx* pcoin = &(*it).second;
1058 if (!pcoin->IsFinal())
1061 if (fOnlyConfirmed && !pcoin->IsConfirmed())
1064 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
1067 if(pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0)
1070 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
1071 if (!(pcoin->IsSpent(i)) && IsMine(pcoin->vout[i]) && pcoin->vout[i].nValue >= nMinimumInputValue &&
1072 (!coinControl || !coinControl->HasSelected() || coinControl->IsSelected((*it).first, i)))
1073 vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain()));
1079 void CWallet::AvailableCoinsMinConf(vector<COutput>& vCoins, int nConf) const
1085 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1087 const CWalletTx* pcoin = &(*it).second;
1089 if (!pcoin->IsFinal())
1092 if(pcoin->GetDepthInMainChain() < nConf)
1095 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
1096 if (!(pcoin->IsSpent(i)) && IsMine(pcoin->vout[i]) && pcoin->vout[i].nValue >= nMinimumInputValue)
1097 vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain()));
1102 static void ApproximateBestSubset(vector<pair<int64, pair<const CWalletTx*,unsigned int> > >vValue, int64 nTotalLower, int64 nTargetValue,
1103 vector<char>& vfBest, int64& nBest, int iterations = 1000)
1105 vector<char> vfIncluded;
1107 vfBest.assign(vValue.size(), true);
1108 nBest = nTotalLower;
1110 for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
1112 vfIncluded.assign(vValue.size(), false);
1114 bool fReachedTarget = false;
1115 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
1117 for (unsigned int i = 0; i < vValue.size(); i++)
1119 if (nPass == 0 ? rand() % 2 : !vfIncluded[i])
1121 nTotal += vValue[i].first;
1122 vfIncluded[i] = true;
1123 if (nTotal >= nTargetValue)
1125 fReachedTarget = true;
1129 vfBest = vfIncluded;
1131 nTotal -= vValue[i].first;
1132 vfIncluded[i] = false;
1140 // ppcoin: total coins staked (non-spendable until maturity)
1141 int64 CWallet::GetStake() const
1145 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1147 const CWalletTx* pcoin = &(*it).second;
1148 if (pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1149 nTotal += CWallet::GetCredit(*pcoin);
1154 int64 CWallet::GetNewMint() const
1158 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1160 const CWalletTx* pcoin = &(*it).second;
1161 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1162 nTotal += CWallet::GetCredit(*pcoin);
1167 bool CWallet::SelectCoinsMinConf(int64 nTargetValue, unsigned int nSpendTime, int nConfMine, int nConfTheirs, vector<COutput> vCoins, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
1169 setCoinsRet.clear();
1172 // List of values less than target
1173 pair<int64, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
1174 coinLowestLarger.first = std::numeric_limits<int64>::max();
1175 coinLowestLarger.second.first = NULL;
1176 vector<pair<int64, pair<const CWalletTx*,unsigned int> > > vValue;
1177 int64 nTotalLower = 0;
1179 random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
1181 BOOST_FOREACH(COutput output, vCoins)
1183 const CWalletTx *pcoin = output.tx;
1185 if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
1190 // Follow the timestamp rules
1191 if (pcoin->nTime > nSpendTime)
1194 int64 n = pcoin->vout[i].nValue;
1196 pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
1198 if (n == nTargetValue)
1200 setCoinsRet.insert(coin.second);
1201 nValueRet += coin.first;
1204 else if (n < nTargetValue + CENT)
1206 vValue.push_back(coin);
1209 else if (n < coinLowestLarger.first)
1211 coinLowestLarger = coin;
1215 if (nTotalLower == nTargetValue)
1217 for (unsigned int i = 0; i < vValue.size(); ++i)
1219 setCoinsRet.insert(vValue[i].second);
1220 nValueRet += vValue[i].first;
1225 if (nTotalLower < nTargetValue)
1227 if (coinLowestLarger.second.first == NULL)
1229 setCoinsRet.insert(coinLowestLarger.second);
1230 nValueRet += coinLowestLarger.first;
1234 // Solve subset sum by stochastic approximation
1235 sort(vValue.rbegin(), vValue.rend(), CompareValueOnly());
1236 vector<char> vfBest;
1239 ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000);
1240 if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT)
1241 ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000);
1243 // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
1244 // or the next bigger coin is closer), return the bigger coin
1245 if (coinLowestLarger.second.first &&
1246 ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest))
1248 setCoinsRet.insert(coinLowestLarger.second);
1249 nValueRet += coinLowestLarger.first;
1252 for (unsigned int i = 0; i < vValue.size(); i++)
1255 setCoinsRet.insert(vValue[i].second);
1256 nValueRet += vValue[i].first;
1259 if (fDebug && GetBoolArg("-printpriority"))
1262 printf("SelectCoins() best subset: ");
1263 for (unsigned int i = 0; i < vValue.size(); i++)
1265 printf("%s ", FormatMoney(vValue[i].first).c_str());
1266 printf("total %s\n", FormatMoney(nBest).c_str());
1273 bool CWallet::SelectCoins(int64 nTargetValue, unsigned int nSpendTime, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet, const CCoinControl* coinControl) const
1275 vector<COutput> vCoins;
1276 AvailableCoins(vCoins, true, coinControl);
1278 // coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
1279 if (coinControl && coinControl->HasSelected())
1281 BOOST_FOREACH(const COutput& out, vCoins)
1283 nValueRet += out.tx->vout[out.i].nValue;
1284 setCoinsRet.insert(make_pair(out.tx, out.i));
1286 return (nValueRet >= nTargetValue);
1289 return (SelectCoinsMinConf(nTargetValue, nSpendTime, 1, 6, vCoins, setCoinsRet, nValueRet) ||
1290 SelectCoinsMinConf(nTargetValue, nSpendTime, 1, 1, vCoins, setCoinsRet, nValueRet) ||
1291 SelectCoinsMinConf(nTargetValue, nSpendTime, 0, 1, vCoins, setCoinsRet, nValueRet));
1294 // Select some coins without random shuffle or best subset approximation
1295 bool CWallet::SelectCoinsSimple(int64 nTargetValue, unsigned int nSpendTime, int nMinConf, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
1297 vector<COutput> vCoins;
1298 AvailableCoinsMinConf(vCoins, nMinConf);
1300 setCoinsRet.clear();
1303 BOOST_FOREACH(COutput output, vCoins)
1305 const CWalletTx *pcoin = output.tx;
1308 // Stop if we've chosen enough inputs
1309 if (nValueRet >= nTargetValue)
1312 // Follow the timestamp rules
1313 if (pcoin->nTime > nSpendTime)
1316 int64 n = pcoin->vout[i].nValue;
1318 pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
1320 if (n >= nTargetValue)
1322 // If input value is greater or equal to target then simply insert
1323 // it into the current subset and exit
1324 setCoinsRet.insert(coin.second);
1325 nValueRet += coin.first;
1328 else if (n < nTargetValue + CENT)
1330 setCoinsRet.insert(coin.second);
1331 nValueRet += coin.first;
1338 bool CWallet::CreateTransaction(const vector<pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, const CCoinControl* coinControl)
1341 BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1347 if (vecSend.empty() || nValue < 0)
1350 wtxNew.BindWallet(this);
1353 LOCK2(cs_main, cs_wallet);
1354 // txdb must be opened before the mapWallet lock
1357 nFeeRet = nTransactionFee;
1361 wtxNew.vout.clear();
1362 wtxNew.fFromMe = true;
1364 int64 nTotalValue = nValue + nFeeRet;
1365 double dPriority = 0;
1366 // vouts to the payees
1367 BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1368 wtxNew.vout.push_back(CTxOut(s.second, s.first));
1370 // Choose coins to use
1371 set<pair<const CWalletTx*,unsigned int> > setCoins;
1373 if (!SelectCoins(nTotalValue, wtxNew.nTime, setCoins, nValueIn, coinControl))
1375 BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1377 int64 nCredit = pcoin.first->vout[pcoin.second].nValue;
1378 dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain();
1381 int64 nChange = nValueIn - nValue - nFeeRet;
1382 // if sub-cent change is required, the fee must be raised to at least MIN_TX_FEE
1383 // or until nChange becomes zero
1384 // NOTE: this depends on the exact behaviour of GetMinFee
1385 if (nFeeRet < MIN_TX_FEE && nChange > 0 && nChange < CENT)
1387 int64 nMoveToFee = min(nChange, MIN_TX_FEE - nFeeRet);
1388 nChange -= nMoveToFee;
1389 nFeeRet += nMoveToFee;
1392 // ppcoin: sub-cent change is moved to fee
1393 if (nChange > 0 && nChange < MIN_TXOUT_AMOUNT)
1401 // Fill a vout to ourself
1402 // TODO: pass in scriptChange instead of reservekey so
1403 // change transaction isn't always pay-to-bitcoin-address
1404 CScript scriptChange;
1406 // coin control: send change to custom address
1407 if (coinControl && !boost::get<CNoDestination>(&coinControl->destChange))
1408 scriptChange.SetDestination(coinControl->destChange);
1410 // no coin control: send change to newly generated address
1413 // Note: We use a new key here to keep it from being obvious which side is the change.
1414 // The drawback is that by not reusing a previous key, the change may be lost if a
1415 // backup is restored, if the backup doesn't have the new private key for the change.
1416 // If we reused the old key, it would be possible to add code to look for and
1417 // rediscover unknown transactions that were written with keys of ours to recover
1418 // post-backup change.
1420 // Reserve a new key pair from key pool
1421 CPubKey vchPubKey = reservekey.GetReservedKey();
1423 scriptChange.SetDestination(vchPubKey.GetID());
1426 // Insert change txn at random position:
1427 vector<CTxOut>::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size());
1428 wtxNew.vout.insert(position, CTxOut(nChange, scriptChange));
1431 reservekey.ReturnKey();
1434 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1435 wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
1439 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1440 if (!SignSignature(*this, *coin.first, wtxNew, nIn++))
1444 unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
1445 if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
1447 dPriority /= nBytes;
1449 // Check that enough fee is included
1450 int64 nPayFee = nTransactionFee * (1 + (int64)nBytes / 1000);
1451 int64 nMinFee = wtxNew.GetMinFee(1, false, GMF_SEND, nBytes);
1453 if (nFeeRet < max(nPayFee, nMinFee))
1455 nFeeRet = max(nPayFee, nMinFee);
1459 // Fill vtxPrev by copying from previous transactions vtxPrev
1460 wtxNew.AddSupportingTransactions(txdb);
1461 wtxNew.fTimeReceivedIsTxTime = true;
1470 bool CWallet::CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, const CCoinControl* coinControl)
1472 vector< pair<CScript, int64> > vecSend;
1473 vecSend.push_back(make_pair(scriptPubKey, nValue));
1474 return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, coinControl);
1477 // NovaCoin: get current stake weight
1478 bool CWallet::GetStakeWeight(const CKeyStore& keystore, uint64& nMinWeight, uint64& nMaxWeight, uint64& nWeight)
1480 // Choose coins to use
1481 int64 nBalance = GetBalance();
1482 int64 nReserveBalance = 0;
1484 if (mapArgs.count("-reservebalance") && !ParseMoney(mapArgs["-reservebalance"], nReserveBalance))
1486 error("GetStakeWeight : invalid reserve balance amount");
1490 if (nBalance <= nReserveBalance)
1493 vector<const CWalletTx*> vwtxPrev;
1496 * TODO: performance comparison
1498 static set<pair<const CWalletTx*,unsigned int> > setCoins;
1499 static uint256 hashPrevBlock;
1500 static int64 nValueIn = 0;
1502 // Cache outputs unless best block changed
1503 if (hashPrevBlock != pindexBest->GetBlockHash())
1505 if (!SelectCoinsSimple(nBalance - nReserveBalance, GetAdjustedTime(), nCoinbaseMaturity * 10, setCoins, nValueIn))
1508 if (setCoins.empty())
1511 hashPrevBlock == pindexBest->GetBlockHash();
1515 set<pair<const CWalletTx*,unsigned int> > setCoins;
1518 if (!SelectCoinsSimple(nBalance - nReserveBalance, GetTime(), nCoinbaseMaturity * 10, setCoins, nValueIn))
1521 if (setCoins.empty())
1525 BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1529 LOCK2(cs_main, cs_wallet);
1530 if (!txdb.ReadTxIndex(pcoin.first->GetHash(), txindex))
1534 int64 nTimeWeight = GetWeight((int64)pcoin.first->nTime, (int64)GetTime());
1535 CBigNum bnCoinDayWeight = CBigNum(pcoin.first->vout[pcoin.second].nValue) * nTimeWeight / COIN / (24 * 60 * 60);
1537 // Weight is greater than zero
1538 if (nTimeWeight > 0)
1540 nWeight += bnCoinDayWeight.getuint64();
1543 // Weight is greater than zero, but the maximum value isn't reached yet
1544 if (nTimeWeight > 0 && nTimeWeight < nStakeMaxAge)
1546 nMinWeight += bnCoinDayWeight.getuint64();
1549 // Maximum weight was reached
1550 if (nTimeWeight == nStakeMaxAge)
1552 nMaxWeight += bnCoinDayWeight.getuint64();
1559 bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64 nSearchInterval, CTransaction& txNew, CKey& key)
1561 // The following combine threshold is important to security
1562 // Should not be adjusted if you don't understand the consequences
1563 int64 nCombineThreshold = GetProofOfWorkReward(GetLastBlockIndex(pindexBest, false)->nBits) / 3;
1565 CBigNum bnTargetPerCoinDay;
1566 bnTargetPerCoinDay.SetCompact(nBits);
1571 // Mark coin stake transaction
1572 CScript scriptEmpty;
1573 scriptEmpty.clear();
1574 txNew.vout.push_back(CTxOut(0, scriptEmpty));
1576 // Choose coins to use
1577 int64 nBalance = GetBalance();
1578 int64 nReserveBalance = 0;
1580 if (mapArgs.count("-reservebalance") && !ParseMoney(mapArgs["-reservebalance"], nReserveBalance))
1581 return error("CreateCoinStake : invalid reserve balance amount");
1583 if (nBalance <= nReserveBalance)
1586 vector<const CWalletTx*> vwtxPrev;
1589 * TODO: performance comparison
1591 static set<pair<const CWalletTx*,unsigned int> > setCoins;
1592 static uint256 hashPrevBlock;
1593 static int64 nValueIn = 0;
1595 // Cache outputs unless best block changed
1596 if (hashPrevBlock != pindexBest->GetBlockHash())
1598 if (!SelectCoinsSimple(nBalance - nReserveBalance, txNew.nTime, nCoinbaseMaturity * 10, setCoins, nValueIn))
1601 if (setCoins.empty())
1604 hashPrevBlock == pindexBest->GetBlockHash();
1608 set<pair<const CWalletTx*,unsigned int> > setCoins;
1611 // Select coins with suitable depth
1612 if (!SelectCoinsSimple(nBalance - nReserveBalance, txNew.nTime, nCoinbaseMaturity * 10, setCoins, nValueIn))
1615 if (setCoins.empty())
1619 CScript scriptPubKeyKernel;
1621 BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1625 LOCK2(cs_main, cs_wallet);
1626 if (!txdb.ReadTxIndex(pcoin.first->GetHash(), txindex))
1630 // Read block header
1633 LOCK2(cs_main, cs_wallet);
1634 if (!block.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, false))
1638 static int nMaxStakeSearchInterval = 60;
1639 if (block.GetBlockTime() + nStakeMinAge > txNew.nTime - nMaxStakeSearchInterval)
1640 continue; // only count coins meeting min age requirement
1642 bool fKernelFound = false;
1643 for (unsigned int n=0; n<min(nSearchInterval,(int64)nMaxStakeSearchInterval) && !fKernelFound && !fShutdown; n++)
1645 // Search backward in time from the given txNew timestamp
1646 // Search nSearchInterval seconds back up to nMaxStakeSearchInterval
1647 uint256 hashProofOfStake = 0, targetProofOfStake = 0;
1648 COutPoint prevoutStake = COutPoint(pcoin.first->GetHash(), pcoin.second);
1649 if (CheckStakeKernelHash(nBits, block, txindex.pos.nTxPos - txindex.pos.nBlockPos, *pcoin.first, prevoutStake, txNew.nTime - n, hashProofOfStake, targetProofOfStake))
1652 if (fDebug && GetBoolArg("-printcoinstake"))
1653 printf("CreateCoinStake : kernel found\n");
1654 vector<valtype> vSolutions;
1655 txnouttype whichType;
1656 CScript scriptPubKeyOut;
1657 scriptPubKeyKernel = pcoin.first->vout[pcoin.second].scriptPubKey;
1658 if (!Solver(scriptPubKeyKernel, whichType, vSolutions))
1660 if (fDebug && GetBoolArg("-printcoinstake"))
1661 printf("CreateCoinStake : failed to parse kernel\n");
1664 if (fDebug && GetBoolArg("-printcoinstake"))
1665 printf("CreateCoinStake : parsed kernel type=%d\n", whichType);
1666 if (whichType != TX_PUBKEY && whichType != TX_PUBKEYHASH)
1668 if (fDebug && GetBoolArg("-printcoinstake"))
1669 printf("CreateCoinStake : no support for kernel type=%d\n", whichType);
1670 break; // only support pay to public key and pay to address
1672 if (whichType == TX_PUBKEYHASH) // pay to address type
1674 // convert to pay to public key type
1675 if (!keystore.GetKey(uint160(vSolutions[0]), key))
1677 if (fDebug && GetBoolArg("-printcoinstake"))
1678 printf("CreateCoinStake : failed to get key for kernel type=%d\n", whichType);
1679 break; // unable to find corresponding public key
1681 scriptPubKeyOut << key.GetPubKey() << OP_CHECKSIG;
1683 if (whichType == TX_PUBKEY)
1685 valtype& vchPubKey = vSolutions[0];
1686 if (!keystore.GetKey(Hash160(vchPubKey), key))
1688 if (fDebug && GetBoolArg("-printcoinstake"))
1689 printf("CreateCoinStake : failed to get key for kernel type=%d\n", whichType);
1690 break; // unable to find corresponding public key
1693 if (key.GetPubKey() != vchPubKey)
1695 if (fDebug && GetBoolArg("-printcoinstake"))
1696 printf("CreateCoinStake : invalid key for kernel type=%d\n", whichType);
1697 break; // keys mismatch
1700 scriptPubKeyOut = scriptPubKeyKernel;
1704 txNew.vin.push_back(CTxIn(pcoin.first->GetHash(), pcoin.second));
1705 nCredit += pcoin.first->vout[pcoin.second].nValue;
1706 vwtxPrev.push_back(pcoin.first);
1707 txNew.vout.push_back(CTxOut(0, scriptPubKeyOut));
1709 if (GetWeight(block.GetBlockTime(), (int64)txNew.nTime) < nStakeMaxAge)
1710 txNew.vout.push_back(CTxOut(0, scriptPubKeyOut)); //split stake
1711 if (fDebug && GetBoolArg("-printcoinstake"))
1712 printf("CreateCoinStake : added kernel type=%d\n", whichType);
1713 fKernelFound = true;
1718 if (fKernelFound || fShutdown)
1719 break; // if kernel is found stop searching
1722 if (nCredit == 0 || nCredit > nBalance - nReserveBalance)
1725 BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1727 // Attempt to add more inputs
1728 // Only add coins of the same key/address as kernel
1729 if (txNew.vout.size() == 2 && ((pcoin.first->vout[pcoin.second].scriptPubKey == scriptPubKeyKernel || pcoin.first->vout[pcoin.second].scriptPubKey == txNew.vout[1].scriptPubKey))
1730 && pcoin.first->GetHash() != txNew.vin[0].prevout.hash)
1732 int64 nTimeWeight = GetWeight((int64)pcoin.first->nTime, (int64)txNew.nTime);
1734 // Stop adding more inputs if already too many inputs
1735 if (txNew.vin.size() >= 100)
1737 // Stop adding more inputs if value is already pretty significant
1738 if (nCredit > nCombineThreshold)
1740 // Stop adding inputs if reached reserve limit
1741 if (nCredit + pcoin.first->vout[pcoin.second].nValue > nBalance - nReserveBalance)
1743 // Do not add additional significant input
1744 if (pcoin.first->vout[pcoin.second].nValue > nCombineThreshold)
1746 // Do not add input that is still too young
1747 if (nTimeWeight < nStakeMaxAge)
1750 txNew.vin.push_back(CTxIn(pcoin.first->GetHash(), pcoin.second));
1751 nCredit += pcoin.first->vout[pcoin.second].nValue;
1752 vwtxPrev.push_back(pcoin.first);
1756 // Calculate coin age reward
1760 if (!txNew.GetCoinAge(txdb, nCoinAge))
1761 return error("CreateCoinStake : failed to calculate coin age");
1762 nCredit += GetProofOfStakeReward(nCoinAge, nBits, txNew.nTime);
1768 // Set output amount
1769 if (txNew.vout.size() == 3)
1771 txNew.vout[1].nValue = ((nCredit - nMinFee) / 2 / CENT) * CENT;
1772 txNew.vout[2].nValue = nCredit - nMinFee - txNew.vout[1].nValue;
1775 txNew.vout[1].nValue = nCredit - nMinFee;
1779 BOOST_FOREACH(const CWalletTx* pcoin, vwtxPrev)
1781 if (!SignSignature(*this, *pcoin, txNew, nIn++))
1782 return error("CreateCoinStake : failed to sign coinstake");
1786 unsigned int nBytes = ::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION);
1787 if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
1788 return error("CreateCoinStake : exceeded coinstake size limit");
1790 // Check enough fee is paid
1791 if (nMinFee < txNew.GetMinFee() - MIN_TX_FEE)
1793 nMinFee = txNew.GetMinFee() - MIN_TX_FEE;
1794 continue; // try signing again
1798 if (fDebug && GetBoolArg("-printfee"))
1799 printf("CreateCoinStake : fee for coinstake %s\n", FormatMoney(nMinFee).c_str());
1804 // Successfully generated coinstake
1809 // Call after CreateTransaction unless you want to abort
1810 bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
1813 LOCK2(cs_main, cs_wallet);
1814 printf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
1816 // This is only to keep the database open to defeat the auto-flush for the
1817 // duration of this scope. This is the only place where this optimization
1818 // maybe makes sense; please don't do it anywhere else.
1819 CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
1821 // Take key pair from key pool so it won't be used again
1822 reservekey.KeepKey();
1824 // Add tx to wallet, because if it has change it's also ours,
1825 // otherwise just for transaction history.
1826 AddToWallet(wtxNew);
1828 // Mark old coins as spent
1829 set<CWalletTx*> setCoins;
1830 BOOST_FOREACH(const CTxIn& txin, wtxNew.vin)
1832 CWalletTx &coin = mapWallet[txin.prevout.hash];
1833 coin.BindWallet(this);
1834 coin.MarkSpent(txin.prevout.n);
1836 NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
1843 // Track how many getdata requests our transaction gets
1844 mapRequestCount[wtxNew.GetHash()] = 0;
1847 if (!wtxNew.AcceptToMemoryPool())
1849 // This must not fail. The transaction has already been signed and recorded.
1850 printf("CommitTransaction() : Error: Transaction not valid");
1853 wtxNew.RelayWalletTransaction();
1861 string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
1863 CReserveKey reservekey(this);
1868 string strError = _("Error: Wallet locked, unable to create transaction ");
1869 printf("SendMoney() : %s", strError.c_str());
1872 if (fWalletUnlockMintOnly)
1874 string strError = _("Error: Wallet unlocked for block minting only, unable to create transaction.");
1875 printf("SendMoney() : %s", strError.c_str());
1878 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired))
1881 if (nValue + nFeeRequired > GetBalance())
1882 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());
1884 strError = _("Error: Transaction creation failed ");
1885 printf("SendMoney() : %s", strError.c_str());
1889 if (fAskFee && !uiInterface.ThreadSafeAskFee(nFeeRequired, _("Sending...")))
1892 if (!CommitTransaction(wtxNew, reservekey))
1893 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.");
1900 string CWallet::SendMoneyToDestination(const CTxDestination& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
1904 return _("Invalid amount");
1905 if (nValue + nTransactionFee > GetBalance())
1906 return _("Insufficient funds");
1908 // Parse Bitcoin address
1909 CScript scriptPubKey;
1910 scriptPubKey.SetDestination(address);
1912 return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
1918 DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
1922 fFirstRunRet = false;
1923 DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this);
1924 if (nLoadWalletRet == DB_NEED_REWRITE)
1926 if (CDB::Rewrite(strWalletFile, "\x04pool"))
1929 // Note: can't top-up keypool here, because wallet is locked.
1930 // User will be prompted to unlock wallet the next operation
1931 // the requires a new key.
1935 if (nLoadWalletRet != DB_LOAD_OK)
1936 return nLoadWalletRet;
1937 fFirstRunRet = !vchDefaultKey.IsValid();
1939 NewThread(ThreadFlushWalletDB, &strWalletFile);
1944 bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName)
1946 std::map<CTxDestination, std::string>::iterator mi = mapAddressBook.find(address);
1947 mapAddressBook[address] = strName;
1948 NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED);
1951 return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName);
1954 bool CWallet::DelAddressBookName(const CTxDestination& address)
1956 mapAddressBook.erase(address);
1957 NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), CT_DELETED);
1960 return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString());
1964 void CWallet::PrintWallet(const CBlock& block)
1968 if (block.IsProofOfWork() && mapWallet.count(block.vtx[0].GetHash()))
1970 CWalletTx& wtx = mapWallet[block.vtx[0].GetHash()];
1971 printf(" mine: %d %d %"PRI64d"", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
1973 if (block.IsProofOfStake() && mapWallet.count(block.vtx[1].GetHash()))
1975 CWalletTx& wtx = mapWallet[block.vtx[1].GetHash()];
1976 printf(" stake: %d %d %"PRI64d"", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
1983 bool CWallet::GetTransaction(const uint256 &hashTx, CWalletTx& wtx)
1987 map<uint256, CWalletTx>::iterator mi = mapWallet.find(hashTx);
1988 if (mi != mapWallet.end())
1997 bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
2001 if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey))
2004 vchDefaultKey = vchPubKey;
2008 bool GetWalletFile(CWallet* pwallet, string &strWalletFileOut)
2010 if (!pwallet->fFileBacked)
2012 strWalletFileOut = pwallet->strWalletFile;
2017 // Mark old keypool keys as used,
2018 // and generate all new keys
2020 bool CWallet::NewKeyPool()
2024 CWalletDB walletdb(strWalletFile);
2025 BOOST_FOREACH(int64 nIndex, setKeyPool)
2026 walletdb.ErasePool(nIndex);
2032 int64 nKeys = max(GetArg("-keypool", 100), (int64)0);
2033 for (int i = 0; i < nKeys; i++)
2036 walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey()));
2037 setKeyPool.insert(nIndex);
2039 printf("CWallet::NewKeyPool wrote %"PRI64d" new keys\n", nKeys);
2044 bool CWallet::TopUpKeyPool(unsigned int nSize)
2052 CWalletDB walletdb(strWalletFile);
2055 unsigned int nTargetSize;
2057 nTargetSize = nSize;
2059 nTargetSize = max(GetArg("-keypool", 100), 0LL);
2061 while (setKeyPool.size() < (nTargetSize + 1))
2064 if (!setKeyPool.empty())
2065 nEnd = *(--setKeyPool.end()) + 1;
2066 if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
2067 throw runtime_error("TopUpKeyPool() : writing generated key failed");
2068 setKeyPool.insert(nEnd);
2069 printf("keypool added key %"PRI64d", size=%"PRIszu"\n", nEnd, setKeyPool.size());
2075 void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
2078 keypool.vchPubKey = CPubKey();
2085 // Get the oldest key
2086 if(setKeyPool.empty())
2089 CWalletDB walletdb(strWalletFile);
2091 nIndex = *(setKeyPool.begin());
2092 setKeyPool.erase(setKeyPool.begin());
2093 if (!walletdb.ReadPool(nIndex, keypool))
2094 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
2095 if (!HaveKey(keypool.vchPubKey.GetID()))
2096 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
2097 assert(keypool.vchPubKey.IsValid());
2098 if (fDebug && GetBoolArg("-printkeypool"))
2099 printf("keypool reserve %"PRI64d"\n", nIndex);
2103 int64 CWallet::AddReserveKey(const CKeyPool& keypool)
2106 LOCK2(cs_main, cs_wallet);
2107 CWalletDB walletdb(strWalletFile);
2109 int64 nIndex = 1 + *(--setKeyPool.end());
2110 if (!walletdb.WritePool(nIndex, keypool))
2111 throw runtime_error("AddReserveKey() : writing added key failed");
2112 setKeyPool.insert(nIndex);
2118 void CWallet::KeepKey(int64 nIndex)
2120 // Remove from key pool
2123 CWalletDB walletdb(strWalletFile);
2124 walletdb.ErasePool(nIndex);
2127 printf("keypool keep %"PRI64d"\n", nIndex);
2130 void CWallet::ReturnKey(int64 nIndex)
2132 // Return to key pool
2135 setKeyPool.insert(nIndex);
2138 printf("keypool return %"PRI64d"\n", nIndex);
2141 bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse)
2147 ReserveKeyFromKeyPool(nIndex, keypool);
2150 if (fAllowReuse && vchDefaultKey.IsValid())
2152 result = vchDefaultKey;
2155 if (IsLocked()) return false;
2156 result = GenerateNewKey();
2160 result = keypool.vchPubKey;
2165 int64 CWallet::GetOldestKeyPoolTime()
2169 ReserveKeyFromKeyPool(nIndex, keypool);
2173 return keypool.nTime;
2176 std::map<CTxDestination, int64> CWallet::GetAddressBalances()
2178 map<CTxDestination, int64> balances;
2182 BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
2184 CWalletTx *pcoin = &walletEntry.second;
2186 if (!pcoin->IsFinal() || !pcoin->IsConfirmed())
2189 if ((pcoin->IsCoinBase() || pcoin->IsCoinStake()) && pcoin->GetBlocksToMaturity() > 0)
2192 int nDepth = pcoin->GetDepthInMainChain();
2193 if (nDepth < (pcoin->IsFromMe() ? 0 : 1))
2196 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
2198 CTxDestination addr;
2199 if (!IsMine(pcoin->vout[i]))
2201 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, addr))
2204 int64 n = pcoin->IsSpent(i) ? 0 : pcoin->vout[i].nValue;
2206 if (!balances.count(addr))
2208 balances[addr] += n;
2216 set< set<CTxDestination> > CWallet::GetAddressGroupings()
2218 set< set<CTxDestination> > groupings;
2219 set<CTxDestination> grouping;
2221 BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
2223 CWalletTx *pcoin = &walletEntry.second;
2225 if (pcoin->vin.size() > 0 && IsMine(pcoin->vin[0]))
2227 // group all input addresses with each other
2228 BOOST_FOREACH(CTxIn txin, pcoin->vin)
2230 CTxDestination address;
2231 if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address))
2233 grouping.insert(address);
2236 // group change with input addresses
2237 BOOST_FOREACH(CTxOut txout, pcoin->vout)
2238 if (IsChange(txout))
2240 CWalletTx tx = mapWallet[pcoin->vin[0].prevout.hash];
2241 CTxDestination txoutAddr;
2242 if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
2244 grouping.insert(txoutAddr);
2246 groupings.insert(grouping);
2250 // group lone addrs by themselves
2251 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
2252 if (IsMine(pcoin->vout[i]))
2254 CTxDestination address;
2255 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address))
2257 grouping.insert(address);
2258 groupings.insert(grouping);
2263 set< set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
2264 map< CTxDestination, set<CTxDestination>* > setmap; // map addresses to the unique group containing it
2265 BOOST_FOREACH(set<CTxDestination> grouping, groupings)
2267 // make a set of all the groups hit by this new group
2268 set< set<CTxDestination>* > hits;
2269 map< CTxDestination, set<CTxDestination>* >::iterator it;
2270 BOOST_FOREACH(CTxDestination address, grouping)
2271 if ((it = setmap.find(address)) != setmap.end())
2272 hits.insert((*it).second);
2274 // merge all hit groups into a new single group and delete old groups
2275 set<CTxDestination>* merged = new set<CTxDestination>(grouping);
2276 BOOST_FOREACH(set<CTxDestination>* hit, hits)
2278 merged->insert(hit->begin(), hit->end());
2279 uniqueGroupings.erase(hit);
2282 uniqueGroupings.insert(merged);
2285 BOOST_FOREACH(CTxDestination element, *merged)
2286 setmap[element] = merged;
2289 set< set<CTxDestination> > ret;
2290 BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings)
2292 ret.insert(*uniqueGrouping);
2293 delete uniqueGrouping;
2299 // ppcoin: check 'spent' consistency between wallet and txindex
2300 // ppcoin: fix wallet spent state according to txindex
2301 void CWallet::FixSpentCoins(int& nMismatchFound, int64& nBalanceInQuestion, bool fCheckOnly)
2304 nBalanceInQuestion = 0;
2307 vector<CWalletTx*> vCoins;
2308 vCoins.reserve(mapWallet.size());
2309 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
2310 vCoins.push_back(&(*it).second);
2313 BOOST_FOREACH(CWalletTx* pcoin, vCoins)
2315 // Find the corresponding transaction index
2317 if (!txdb.ReadTxIndex(pcoin->GetHash(), txindex))
2319 for (unsigned int n=0; n < pcoin->vout.size(); n++)
2321 if (IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && (txindex.vSpent.size() <= n || txindex.vSpent[n].IsNull()))
2323 printf("FixSpentCoins found lost coin %sppc %s[%d], %s\n",
2324 FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
2326 nBalanceInQuestion += pcoin->vout[n].nValue;
2329 pcoin->MarkUnspent(n);
2330 pcoin->WriteToDisk();
2333 else if (IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && (txindex.vSpent.size() > n && !txindex.vSpent[n].IsNull()))
2335 printf("FixSpentCoins found spent coin %sppc %s[%d], %s\n",
2336 FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
2338 nBalanceInQuestion += pcoin->vout[n].nValue;
2341 pcoin->MarkSpent(n);
2342 pcoin->WriteToDisk();
2349 // ppcoin: disable transaction (only for coinstake)
2350 void CWallet::DisableTransaction(const CTransaction &tx)
2352 if (!tx.IsCoinStake() || !IsFromMe(tx))
2353 return; // only disconnecting coinstake requires marking input unspent
2356 BOOST_FOREACH(const CTxIn& txin, tx.vin)
2358 map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
2359 if (mi != mapWallet.end())
2361 CWalletTx& prev = (*mi).second;
2362 if (txin.prevout.n < prev.vout.size() && IsMine(prev.vout[txin.prevout.n]))
2364 prev.MarkUnspent(txin.prevout.n);
2371 CPubKey CReserveKey::GetReservedKey()
2376 pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
2378 vchPubKey = keypool.vchPubKey;
2381 printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
2382 vchPubKey = pwallet->vchDefaultKey;
2385 assert(vchPubKey.IsValid());
2389 void CReserveKey::KeepKey()
2392 pwallet->KeepKey(nIndex);
2394 vchPubKey = CPubKey();
2397 void CReserveKey::ReturnKey()
2400 pwallet->ReturnKey(nIndex);
2402 vchPubKey = CPubKey();
2405 void CWallet::GetAllReserveKeys(set<CKeyID>& setAddress) const
2409 CWalletDB walletdb(strWalletFile);
2411 LOCK2(cs_main, cs_wallet);
2412 BOOST_FOREACH(const int64& id, setKeyPool)
2415 if (!walletdb.ReadPool(id, keypool))
2416 throw runtime_error("GetAllReserveKeyHashes() : read failed");
2417 assert(keypool.vchPubKey.IsValid());
2418 CKeyID keyID = keypool.vchPubKey.GetID();
2419 if (!HaveKey(keyID))
2420 throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool");
2421 setAddress.insert(keyID);
2425 void CWallet::UpdatedTransaction(const uint256 &hashTx)
2429 // Only notify UI if this transaction is in this wallet
2430 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(hashTx);
2431 if (mi != mapWallet.end())
2432 NotifyTransactionChanged(this, hashTx, CT_UPDATED);
2436 void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64> &mapKeyBirth) const {
2437 mapKeyBirth.clear();
2439 // get birth times for keys with metadata
2440 for (std::map<CKeyID, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++)
2441 if (it->second.nCreateTime)
2442 mapKeyBirth[it->first] = it->second.nCreateTime;
2444 // map in which we'll infer heights of other keys
2445 CBlockIndex *pindexMax = FindBlockByHeight(std::max(0, nBestHeight - 144)); // the tip can be reorganised; use a 144-block safety margin
2446 std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
2447 std::set<CKeyID> setKeys;
2449 BOOST_FOREACH(const CKeyID &keyid, setKeys) {
2450 if (mapKeyBirth.count(keyid) == 0)
2451 mapKeyFirstBlock[keyid] = pindexMax;
2455 // if there are no such keys, we're done
2456 if (mapKeyFirstBlock.empty())
2459 // find first block that affects those keys, if there are any left
2460 std::vector<CKeyID> vAffected;
2461 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
2462 // iterate over all wallet transactions...
2463 const CWalletTx &wtx = (*it).second;
2464 std::map<uint256, CBlockIndex*>::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
2465 if (blit != mapBlockIndex.end() && blit->second->IsInMainChain()) {
2466 // ... which are already in a block
2467 int nHeight = blit->second->nHeight;
2468 BOOST_FOREACH(const CTxOut &txout, wtx.vout) {
2469 // iterate over all their outputs
2470 ::ExtractAffectedKeys(*this, txout.scriptPubKey, vAffected);
2471 BOOST_FOREACH(const CKeyID &keyid, vAffected) {
2472 // ... and all their affected keys
2473 std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find(keyid);
2474 if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight)
2475 rit->second = blit->second;
2482 // Extract block timestamps for those keys
2483 for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
2484 mapKeyBirth[it->first] = it->second->nTime - 7200; // block times can be 2h off